Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format feature set as hex files when decoding #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/test_suite/multiprocessing_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass, field
from test_suite.constants import OUTPUT_BUFFER_SIZE
from test_suite.context_pb2 import FeatureSet
import test_suite.invoke_pb2 as invoke_pb
import ctypes
from ctypes import c_uint64, c_int, POINTER, Structure
Expand Down Expand Up @@ -131,6 +132,43 @@ def read_fixture(fixture_file: Path) -> str | None:
return instruction_fixture.SerializeToString(deterministic=True)


def format_feature_set(message, indent=0, as_one_line=False):
lines = []
lines.append(" " * indent + f"FeatureSet: {{")
for feature in message.features:
lines.append(" " * (indent + 2) + hex(feature))
lines.append(" " * indent + "}")
return "\n".join(lines)


def recursive_formatter(message, indent=0, as_one_line=False):
"""Recursively format the message and its sub-fields"""
lines = []
for field, value in message.ListFields():
if field.label == field.LABEL_REPEATED:
lines.append(" " * indent + f"{field.name}: [")
for item in value:
if field.type == field.TYPE_MESSAGE:
lines.append(recursive_formatter(item, indent + 2, as_one_line))
else:
lines.append(" " * (indent + 2) + str(item))
lines.append(" " * indent + "]")
elif field.type == field.TYPE_MESSAGE:
# If the field is a message, check if it needs custom formatting
lines.append(" " * indent + f"{field.name}: {{")
if isinstance(value, FeatureSet):
lines.append(format_feature_set(value, indent + 2, as_one_line))
else:
# Recursively format other sub-messages
lines.append(recursive_formatter(value, indent + 2, as_one_line))
lines.append(" " * indent + "}")
else:
# Use the default formatting for non-message fields
lines.append(" " * indent + f"{field.name}: {value}")

return "\n".join(lines)


def decode_single_test_case(test_file: Path) -> int:
Copy link
Collaborator

@mjain-jump mjain-jump Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The protobuf decoded format also allowed us to execute the decoded .txt message, which was really useful for debugging (e.g. it allowed me to easily change parts of the test case by hand and see how it affected the output). This change is going to break that. Can you guard this logic behind a flag within decode_protobuf?

"""
Decode a single test case into a human-readable message
Expand All @@ -154,7 +192,11 @@ def decode_single_test_case(test_file: Path) -> int:

with open(globals.output_dir / (test_file.stem + ".txt"), "w") as f:
f.write(
text_format.MessageToString(instruction_context, print_unknown_fields=False)
text_format.MessageToString(
instruction_context,
print_unknown_fields=False,
message_formatter=recursive_formatter,
)
)
return 1

Expand Down
Loading