GB4198

Graph-Break Type

Short name describing what triggered the graph break

Attempted to copy.deepcopy a tensor

Context

Values or code snippet captured at the break point

copy.deepcopy({self})

Explanation

Explanation of why the graph break was triggered

Dynamo does not support copy.deepcopy() on tensors.

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when copy.deepcopy() is called on a tensor inside a compiled region. This often shows up indirectly, for example when code snapshots state by deep-copying a container that holds tensors.

Example code that causes the graph break is:

import copy
import torch


@torch.compile(fullgraph=True)
def fn(x):
    saved = copy.deepcopy(x)
    return x + saved


fn(torch.randn(3))

Use tensor.clone() instead. clone() is differentiable and is captured into the graph, so it gives you an independent copy without breaking:

import torch


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


fn(torch.randn(3))

If you are deep-copying a container (e.g. a list or dict of tensors), replace the copy.deepcopy with a comprehension that clones each tensor:

import torch


@torch.compile(fullgraph=True)
def fn(xs):
    saved = [t.clone() for t in xs]
    return [a + b for a, b in zip(xs, saved)]


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

Click here to add Additional Info

Back to Registry