MCPcopy Create free account
hub / github.com/pytorch/executorch / assert_programs_equal

Method assert_programs_equal

exir/_serialize/test/test_program.py:122–145  ·  view source on GitHub ↗
(self, program1: Program, program2: Program)

Source from the content-addressed store, hash-verified

120 # important for this test.
121
122 def assert_programs_equal(self, program1: Program, program2: Program) -> None:
123 def prepare_json_string(j: str) -> List[str]:
124 """Formats the JSON and splits it into lines."""
125 return json.dumps(json.loads(j), indent=2, sort_keys=True).splitlines(
126 keepends=True
127 )
128
129 # This JSON comparison is fragile: some parts of the program do not care
130 # about order (like the operators list), so those are technically free
131 # to be reordered. If they become a problem, we can canonicalize them
132 # like we do for the backend delegate data list.
133 json1 = _program_to_json(canonicalize_delegate_indices(program1))
134 json2 = _program_to_json(canonicalize_delegate_indices(program2))
135
136 # Use unified_diff so it only prints the differences instead of the
137 # entire string.
138 diff: str = "".join(
139 difflib.unified_diff(
140 prepare_json_string(json1),
141 prepare_json_string(json2),
142 )
143 )
144 if diff:
145 self.fail(msg="Programs are not equal\n" + diff)
146
147 def get_and_validate_extended_header(self, pte_data: bytes) -> _ExtendedHeader:
148 """When an extended header is expected, check that it exists and is valid.

Calls 2

_program_to_jsonFunction · 0.90

Tested by

no test coverage detected