GB0222

Graph-Break Type

Short name describing what triggered the graph break

Attempted to wrap a set with tensors

Context

Values or code snippet captured at the break point

Python set containing torch.Tensor elements

Explanation

Explanation of why the graph break was triggered

Dynamo cannot trace sets of tensors. To get a stable ordering, Dynamo needs to convert the set into a list and the order might not be stable if the set contains tensors.

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when Dynamo encounters a Python set that contains tensors. A set has no defined iteration order, so Dynamo cannot give the tensors a stable ordering to trace over.

Example code that causes the graph break is:

import torch


@torch.compile(fullgraph=True)
def fn(tensors):
    return sum(t.sum() for t in tensors)


fn({torch.randn(3), torch.randn(3)})

Use an ordered container instead. If you just need a collection of tensors, a list works directly:

import torch


@torch.compile(fullgraph=True)
def fn(tensors):
    return sum(t.sum() for t in tensors)


fn([torch.randn(3), torch.randn(3)])

If you were using a set for membership/deduplication keyed on tensor identity, use a dict keyed by the tensor (the hint on this break suggests the same), and iterate over its values:

import torch


@torch.compile(fullgraph=True)
def fn(tensor_map):
    return sum(t.sum() for t in tensor_map)


a, b = torch.randn(3), torch.randn(3)
fn({a: None, b: None})

Click here to add Additional Info

Back to Registry