Skip to content

Fix complex rewrite device mismatch - #4513

Open
jloftin-nv wants to merge 1 commit into
pytorch:mainfrom
jloftin-nv:dev-jloftin-complex-mismatch
Open

Fix complex rewrite device mismatch#4513
jloftin-nv wants to merge 1 commit into
pytorch:mainfrom
jloftin-nv:dev-jloftin-complex-mismatch

Conversation

@jloftin-nv

Copy link
Copy Markdown
Contributor

Description

After complex-graph rewrite, FakeTensorProp can mix CUDA activations with CPU _frozen_param* weights and raise FakeTensorDeviceMismatchError, aborting builds that dry-ran cleanly.

Align parameter devices before prop (and keep the non-tensor meta["val"] guard) so rewritten graphs re-propagate on a single device.

Type of change

Please delete options that are not relevant and/or add your own.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • [ X] My code follows the style guidelines of this project (You can use the linters)
  • [ X] I have performed a self-review of my own code
  • [ X] I have commented my code, particularly in hard-to-understand areas and hacks
  • [ X] I have made corresponding changes to the documentation
  • [ X] I have added tests to verify my fix or my feature
  • [ X] New and existing unit tests pass locally with my changes
  • [ X] I have added the relevant labels to my PR in so that relevant reviewers are notified

@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Aug 18, 2026
@meta-cla meta-cla Bot added the cla signed label Aug 18, 2026
@github-actions
github-actions Bot requested a review from zewenli98 August 18, 2026 21:54
@narendasan
narendasan requested review from apbose and removed request for zewenli98 August 18, 2026 22:55
@lanluo-nvidia lanluo-nvidia added this to the v2.14.0 milestone Aug 20, 2026
xq = torch.randn(1, 2, 4, 8)
freqs = torch.polar(torch.ones(1, 2, 4, 4), torch.randn(1, 2, 4, 4))
_export_and_lower(RotaryComplex(), (xq, freqs, scale))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test would pass even without the fix. 0 dim would pass in eager mode and fake tensor mode,but then if you do torch.tensor([2.0]) triggers a mismatch too early FakeTensorDeviceMismatchError—in eager/export—rather than specifically during post-rewrite metadata propagation.

if isinstance(attr, torch.Tensor) and attr.device != self._align_device:
return attr.to(self._align_device)
return attr

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every tensor attribute is moved to the first CUDA input’s device.
The new fetch_attr changes all tensor attributes whenever any CUDA placeholder exists. That includes intentionally CPU attributes or an independent CPU branch, producing incorrect device metadata. Something like below

 class M(torch.nn.Module):
      def __init__(self):
          super().__init__()
          self.register_buffer("cpu_table", torch.arange(4))  # intentionally CPU

      def forward(self, z):
          gpu_result = z * z                 # z is CUDA complex
          cpu_result = self.cpu_table + 1    # independent CPU computation
          return gpu_result, cpu_result

Maybe we should add

  • A real _frozen_param mismatch test proving the target attribute gets aligned.
  • A mixed-device test like the example above proving an unrelated CPU attribute remains CPU.

@jloftin-nv
jloftin-nv force-pushed the dev-jloftin-complex-mismatch branch from e5253a9 to 292d096 Compare August 21, 2026 18:28
@apbose

apbose commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

LGTM. Waiting on CI.

@lanluo-nvidia lanluo-nvidia modified the milestones: v2.14.0, v2.15.0 Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants