GB0206

Graph-Break Type

Short name describing what triggered the graph break

setattr() on Tensor.requires_grad

Context

Values or code snippet captured at the break point

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

Explanation

Explanation of why the graph break was triggered

setattr() on Tensor.requires_grad not supported. Mutating requires_grad can introduce a new leaf from non-leaf or vice versa in the middle of the graph, which AOTAutograd does not currently know how to handle.

Hints

Hints on how to resolve the graph break

Additional Information

This graph break happens when you assign to a tensor’s requires_grad attribute inside a compiled region. Flipping requires_grad partway through can turn a leaf tensor into a non-leaf (or vice versa), which the autograd engine cannot handle inside the compiled region.

Example code that causes the graph break is:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    x.requires_grad = True
    return x * 2


fn(torch.randn(3))

requires_grad is a property of the tensor, not a per-step computation, so set it once before entering the compiled region:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    return x * 2


x = torch.randn(3)
x.requires_grad = True  # set outside the compiled region
fn(x)

You can also use the in-place tensor.requires_grad_() (or tensor.detach().requires_grad_()) outside the compiled region rather than mutating the attribute inside it:

import torch


@torch.compile(fullgraph=True)
def fn(x):
    return x * 2


x = torch.randn(3)
x.requires_grad_()  # in-place, outside the compiled region
fn(x).sum().backward()

Click here to add Additional Info

Back to Registry