GB0142

Graph-Break Type

Short name describing what triggered the graph break

Unsupported context manager

Context

Values or code snippet captured at the break point

Attempted SETUP_WITH/BEFORE_WITH/LOAD_SPECIAL on {ctx}

Explanation

Explanation of why the graph break was triggered

Dynamo does not know how to enter a {ctx.python_type_name()} context manager.

Hints

Hints on how to resolve the graph break

Additional Information

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)

Click here to add Additional Info

Back to Registry