Short name describing what triggered the graph break
Unsupported context manager
Values or code snippet captured at the break point
Attempted SETUP_WITH/BEFORE_WITH/LOAD_SPECIAL on {ctx}
Explanation of why the graph break was triggered
Dynamo does not know how to enter a {ctx.python_type_name()} context manager.
Hints on how to resolve the graph break
Example code that causes the graph break is:
def fn(obj):
with obj:
return 1
invalid_context_manager = 3
compiled_fn = torch.compile(fn, backend="eager", fullgraph=True)
compiled_fn(invalid_context_manager)
A sample workaround around this is to create a valid context manager. This can be any class that implements enter and exit:
def fn(obj):
with obj:
print("Inside the context manager.")
return 1
class MyContextManager:
def __enter__(self):
print("Entering context.")
return self
def __exit__(self, exc_type, exc_value, traceback):
print("Exiting context.")
valid_context_manager = MyContextManager()
compiled_fn = torch.compile(fn, backend="eager", fullgraph=True)
result = compiled_fn(valid_context_manager)