Skip to content

Commit

Permalink
[Testing] Use TVMScript's "name" argument for error messages (apache#…
Browse files Browse the repository at this point in the history
…14808)

Prior to this commit, the `tvm.testing.CompareBeforeAfter` utility
performed string replacement to define the names of the
before/after/expected examples.  Since
apache#13934 allowed the name to be
explicitly passed to the printer, this should be used instead.
  • Loading branch information
Lunderberg authored May 9, 2023
1 parent 182de86 commit 440aae2
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions python/tvm/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2065,22 +2065,15 @@ def _is_method(func):
def test_compare(self, before, expected, transform):
"""Unit test to compare the expected TIR PrimFunc to actual"""

def pprint(name, obj):
script = obj.script()
if isinstance(obj, tvm.IRModule):
return script.replace("class Module", f"class {name}")
else:
return script.replace("def func", f"def {name}")

if inspect.isclass(expected) and issubclass(expected, Exception):
with pytest.raises(expected):
after = transform(before)

# This portion through pytest.fail isn't strictly
# necessary, but gives a better error message that
# includes the before/after.
before_str = pprint("before", before)
after_str = pprint("after", after)
before_str = before.script(name="before")
after_str = after.script(name="after")

pytest.fail(
msg=(
Expand All @@ -2095,9 +2088,9 @@ def pprint(name, obj):
try:
tvm.ir.assert_structural_equal(after, expected)
except ValueError as err:
before_str = pprint("before", before)
after_str = pprint("after", after)
expected_str = pprint("expected", expected)
before_str = before.script(name="before")
after_str = after.script(name="after")
expected_str = expected.script(name="expected")
raise ValueError(
f"TIR after transformation did not match expected:\n"
f"{before_str}\n{after_str}\n{expected_str}"
Expand Down

0 comments on commit 440aae2

Please sign in to comment.