pytorch - 💡(How to fix) Fix [Dynamo/Inductor] torch.compile changes output shape of x.norm(dim=-1) [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#182181Fetched 2026-05-02 05:26:51
View on GitHub
Comments
0
Participants
1
Timeline
114
Reactions
0
Participants
Timeline (top)
mentioned ×54subscribed ×54labeled ×6

Fix Action

Fix / Workaround

Vulnerability Tsx async abort: Mitigation; TSX disabled Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Code Example

class TestModule(torch.nn.Module):
    def forward(self, x):
        return x.norm(dim=-1)


m = TestModule()
n = torch.compile(m)

x = torch.rand(8, 3, 1)

print(m(x).shape)
print(n(x).shape)

---

torch.Size([8, 3])
torch.Size([8, 3, 1])
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

norm() behaves differently before and after torch.compile


class TestModule(torch.nn.Module):
    def forward(self, x):
        return x.norm(dim=-1)


m = TestModule()
n = torch.compile(m)

x = torch.rand(8, 3, 1)

print(m(x).shape)
print(n(x).shape)

I get the following output:

torch.Size([8, 3])
torch.Size([8, 3, 1])

Versions

Vulnerability Tsx async abort: Mitigation; TSX disabled Vulnerability Vmscape: Mitigation; IBPB before exit to userspace

Versions of relevant libraries: [pip3] numpy==2.4.4 [pip3] nvidia-cublas==13.1.0.3 [pip3] nvidia-cuda-cupti==13.0.85 [pip3] nvidia-cuda-nvrtc==13.0.88 [pip3] nvidia-cuda-runtime==13.0.96 [pip3] nvidia-cudnn-cu13==9.19.0.56 [pip3] nvidia-cufft==12.0.0.61 [pip3] nvidia-curand==10.4.0.35 [pip3] nvidia-cusolver==12.0.4.66 [pip3] nvidia-cusparse==12.6.3.3 [pip3] nvidia-cusparselt-cu13==0.8.0 [pip3] nvidia-nccl-cu13==2.28.9 [pip3] nvidia-nvjitlink==13.0.88 [pip3] nvidia-nvtx==13.0.85 [pip3] pytorch-lightning==2.6.1 [pip3] torch==2.11.0 [pip3] torchmetrics==1.9.0 [pip3] torchvision==0.26.0 [pip3] triton==3.6.0 [conda] Could not collect

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

extent analysis

TL;DR

The issue can be resolved by ensuring consistent behavior of the norm() function before and after torch.compile by checking the documentation for any changes in behavior introduced by compilation.

Guidance

  • Verify the documentation of torch.compile to see if it introduces any changes in the behavior of the norm() function.
  • Check the PyTorch version (2.11.0) for any known issues related to torch.compile and norm().
  • Test the code with different input shapes to see if the issue is specific to certain dimensions.
  • Compare the output of m(x) and n(x) with other reduction operations to isolate the issue.

Example

# Test with different input shapes
x = torch.rand(8, 3, 1)
print(m(x).shape)
print(n(x).shape)

x = torch.rand(8, 3)
print(m(x).shape)
print(n(x).shape)

Notes

The issue seems to be specific to the norm() function and torch.compile. Further investigation into the PyTorch documentation and version-specific issues is needed to determine the root cause.

Recommendation

Apply a workaround by checking the output shape of n(x) and adjusting it accordingly to match the expected shape of m(x). This can be done by adding a .squeeze() or .unsqueeze() operation to n(x) to match the desired shape.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [Dynamo/Inductor] torch.compile changes output shape of x.norm(dim=-1) [1 participants]