[dynamo] Graph break on torch.Tensor.data assignment with mismatched dtype (#156623)

Fixes #152162. Discussed with @bdhirsh and decided this is the easiest
workaround for now.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/156623
Approved by: https://github.com/bdhirsh
This commit is contained in:
Ryan Guo
2025-06-24 14:30:41 -07:00
committed by PyTorch MergeBot
parent e8cf5ff564
commit d06a406656
4 changed files with 39 additions and 0 deletions

View File

@ -2175,6 +2175,17 @@ class BuiltinVariable(VariableTracker):
"the mutation out of `torch.compile` region",
],
)
elif obj.dtype != val.dtype: # type: ignore[attr-defined]
unimplemented_v2(
gb_type="Failed to mutate tensor data attribute to different dtype",
context=f"setattr({obj}, {name}, {val})",
explanation="Dyanmo only supports mutating `.data`"
" of tensor to a new one with the same dtype",
hints=[
"Don't mutate `.data` on this tensor, or move "
"the mutation out of `torch.compile` region",
],
)
# Remove the old reference in tracked fakes - if we don't do this
# new .data value size and shape differences will cause