areusch commented on a change in pull request #7952:
URL: https://github.com/apache/tvm/pull/7952#discussion_r639382306



##########
File path: tests/python/relay/test_pass_instrument.py
##########
@@ -168,3 +169,329 @@ def run_after_pass(self, mod, info):
     # Out of pass context scope, should be reset
     assert passes_counter.run_before_count == 0
     assert passes_counter.run_after_count == 0
+
+
+def test_enter_pass_ctx_exception(capsys):
+    @pass_instrument
+    class PI:
+        def __init__(self, id):
+            self.id = id
+
+        def enter_pass_ctx(self):
+            print(self.id + " enter ctx")
+
+        def exit_pass_ctx(self):
+            print(self.id + " exit ctx")
+
+    @pass_instrument
+    class PIBroken(PI):
+        def __init__(self, id):
+            super().__init__(id)
+
+        def enter_pass_ctx(self):
+            print(self.id + " enter ctx")
+            raise RuntimeError("Just a dummy error")
+
+    pass_ctx = tvm.transform.PassContext(instruments=[PI("%1"), 
PIBroken("%2"), PI("%3")])
+    with pytest.raises(tvm.error.TVMError):
+        with pass_ctx:
+            pass
+
+    assert "%1 enter ctx\n" "%2 enter ctx\n" == capsys.readouterr().out

Review comment:
       hrm...I guess this one has no analogue in LLVM. but I'm not sure I 
agree--I think if an `__enter__` returns succesfully, the contract is that it's 
expected the `__exit__` will get called. i don't think you can implement 
`__enter__` with knowledge of any following `__enter__`, so this case would 
potentially leave state initialized...
   
   since catching `std::runtime_error` is how we handle errors at the FFI, it 
seems like we should be able to catch that here and invoke the `__exit__` in 
reverse order. on the other hand, this gets messy, since `__exit__` can also 
throw exceptions. @tqchen maybe you have some thoughts on doing such complex 
stuff inside `libtvm_runtime`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to