GB0109

Graph-Break Type

Short name describing what triggered the graph break

Tensor.tolist() with non-integer tensor

Context

Values or code snippet captured at the break point

call_method {self} to_list

Explanation

Explanation of why the graph break was triggered

Dynamo currently does not support tracing tolist() on non-integer tensors.

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when Tensor.tolist() is called on a non-integer (e.g. floating-point) tensor inside a compiled region. Turning the float values into Python numbers would force torch.compile to read the tensor’s actual contents while building the compiled function, which it cannot do. Integer tensors are handled because torch.compile can represent their elements with placeholder values it can reason about instead.

Example code that causes the graph break is:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    return x + sum(x.tolist())


fn(torch.randn(4))

The best fix is usually to keep the data as a tensor and use tensor operations instead of building a Python list:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    return x + x.sum()


fn(torch.randn(4))

If you genuinely need Python-level values (for example to use them as indices or counts), make sure the tensor has an integer dtype before calling tolist():

import torch


@torch.compile(fullgraph=True)
def fn(x):
    counts = x.to(torch.int64)
    return x + sum(counts.tolist())


fn(torch.randn(4))

Click here to add Additional Info

Back to Registry