GB0220

Graph-Break Type

Short name describing what triggered the graph break

Failed to mutate tensor data attribute to different dtype

Context

Values or code snippet captured at the break point

setattr({obj}, {name}, {val})

Explanation

Explanation of why the graph break was triggered

Dynamo only supports mutating .data of tensor to a new one with the same dtype

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when you assign to a tensor’s .data with a tensor of a different dtype inside a compiled region. Dynamo can model reassigning .data to a tensor of the same dtype, but changing the dtype this way would alter the tensor’s type partway through the compiled region, which is not supported.

Example code that causes the graph break is:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    x.data = x.data.to(torch.int32)  # dtype change via .data
    return x + 1


fn(torch.randn(3))

Mutating .data is rarely what you want inside a compiled function. Produce a new tensor of the target dtype instead and use that:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    y = x.to(torch.int32)
    return y + 1


fn(torch.randn(3))

If you specifically need the dtype conversion to be visible on the original tensor outside the compiled region, perform the conversion outside the compiled region rather than mutating .data inside it.

Click here to add Additional Info

Back to Registry