pytorch - ✅(Solved) Fix [PT2 + Numpy] Can't torch.compile `ndarray.flat` [2 pull requests, 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#179582Fetched 2026-04-08 03:00:02
View on GitHub
Comments
0
Participants
1
Timeline
19
Reactions
0
Author
Participants
Assignees
Timeline (top)
mentioned ×4referenced ×4subscribed ×4cross-referenced ×2

Error Message

torch._dynamo.exc.TorchRuntimeError: RuntimeError when making fake tensor call
  Explanation: Dynamo failed to run FX node with fake tensors:
    call_function <built-in function getattr>(*(FakeTensor(..., size=(2, 2), dtype=torch.float64), 'flat'), **{}):
    got AttributeError("'FakeTensor' object has no attribute 'flat'")

Note: removing .flat works fine since np.cumsum without an axis argument already flattens.

PR fix notes

PR #179595: Add numpy ndarray .flat support to torch.compile/dynamo

Description (problem / solution / changelog)

Stack from ghstack (oldest at bottom):

  • -> #179595

ndarray.flat was not handled in dynamo's numpy interop, causing torch.compile to fail when tracing code that accesses .flat (e.g. matplotlib's gridspec.py uses np.cumsum(arr.flat)).

Implement .flat as ravel() on torch._numpy.ndarray and trace it through the graph like .T, .real, .imag.

Fixes #179582

This commit was authored with Claude.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo @azahed98 @mlazos

Changed files

  • test/test_tensorboard.py (modified, +2/-2)
  • test/torch_np/numpy_tests/core/test_multiarray.py (modified, +6/-2)
  • torch/_dynamo/variables/tensor.py (modified, +1/-1)
  • torch/_numpy/_ndarray.py (modified, +4/-0)

PR #179321: Consolidate gcc11 CPU Docker image into clang18

Description (problem / solution / changelog)

Stack from ghstack (oldest at bottom):

  • -> #179321

The pytorch-linux-jammy-py3.10-gcc11 and py3.10-clang18 Docker images were nearly identical. This change consolidates them by installing both GCC 11 and Clang 18 into the same image, and selecting the compiler during runtime using update-alternatives and CC/CXX env vars.

Tweak skips a bit to account for tensorboard dynamo test failure when matplot lib is installed (see https://github.com/pytorch/pytorch/issues/179582 ) and tweak some expectedFailure in test_dynamo_distribtued to be conditional on transformers version (with transformers-4.x tests are passing, but fails with transformers-5)

The inductor-benchmarks gcc11 image is left unchanged for now.

Co-authored-by: Claude [email protected]

Changed files

  • .ci/docker/build.sh (modified, +3/-6)
  • .ci/pytorch/build.sh (modified, +3/-8)
  • .ci/pytorch/common.sh (modified, +13/-0)
  • .github/workflows/docker-builds.yml (modified, +0/-1)
  • .github/workflows/nightly.yml (modified, +1/-1)
  • .github/workflows/pull.yml (modified, +3/-3)
  • .github/workflows/trunk.yml (modified, +1/-1)
  • test/distributed/test_dynamo_distributed.py (modified, +24/-16)
  • test/test_tensorboard.py (modified, +2/-0)

Code Example

import numpy as np
import torch

def fn(x):
    return np.cumsum(x.flat)

x = np.array([[1.0, 2.0], [3.0, 4.0]])
print("Eager result:", fn(x))  # works: [ 1.  3.  6. 10.]

compiled_fn = torch.compile(fn, backend="eager")
print("Compiled result:", compiled_fn(x))  # errors out

---

torch._dynamo.exc.TorchRuntimeError: RuntimeError when making fake tensor call
  Explanation: Dynamo failed to run FX node with fake tensors:
    call_function <built-in function getattr>(*(FakeTensor(..., size=(2, 2), dtype=torch.float64), 'flat'), **{}):
    got AttributeError("'FakeTensor' object has no attribute 'flat'")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.compile fails when tracing code that accesses .flat on a numpy ndarray. The .flat property (which returns a flatiter over the array) is not implemented on FakeTensor, causing dynamo to error out.

This surfaced in CI when matplotlib's gridspec.py uses np.cumsum(arr.flat) — which is valid numpy code that works fine in eager mode.

Minimal reproducer

import numpy as np
import torch

def fn(x):
    return np.cumsum(x.flat)

x = np.array([[1.0, 2.0], [3.0, 4.0]])
print("Eager result:", fn(x))  # works: [ 1.  3.  6. 10.]

compiled_fn = torch.compile(fn, backend="eager")
print("Compiled result:", compiled_fn(x))  # errors out

Error

torch._dynamo.exc.TorchRuntimeError: RuntimeError when making fake tensor call
  Explanation: Dynamo failed to run FX node with fake tensors:
    call_function <built-in function getattr>(*(FakeTensor(..., size=(2, 2), dtype=torch.float64), 'flat'), **{}):
    got AttributeError("'FakeTensor' object has no attribute 'flat'")

Note: removing .flat works fine since np.cumsum without an axis argument already flattens.

Versions

PyTorch 2.11.0/trunk

This issue was authored with Claude.

extent analysis

TL;DR

Modify the code to avoid using the .flat property on numpy ndarrays when working with torch.compile.

Guidance

  • Identify and refactor code sections that access .flat on numpy ndarrays to avoid this property when using torch.compile.
  • Consider removing .flat and relying on np.cumsum's default behavior of flattening the input array, as shown in the minimal reproducer.
  • Verify the fix by re-running the compiled function with the modified code and checking for the expected output.
  • Be aware that this workaround may not be applicable if the .flat property is necessary for other operations or libraries.

Example

import numpy as np
import torch

def fn(x):
    return np.cumsum(x)  # Remove .flat

x = np.array([[1.0, 2.0], [3.0, 4.0]])
print("Eager result:", fn(x))  

compiled_fn = torch.compile(fn, backend="eager")
print("Compiled result:", compiled_fn(x))

Notes

This solution assumes that removing .flat does not affect the correctness of the code. If .flat is necessary, further investigation into implementing the flat property on FakeTensor or alternative workarounds may be required.

Recommendation

Apply workaround: Modify the code to avoid using the .flat property, as shown in the example, to ensure compatibility with torch.compile.

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