pytorch - 💡(How to fix) Fix api shows unexpected difference for flatten(): numpy vs. pytorch [1 comments, 2 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#177957Fetched 2026-04-08 01:02:48
View on GitHub
Comments
1
Participants
2
Timeline
13
Reactions
0
Author
Participants
Timeline (top)
labeled ×4mentioned ×4subscribed ×4commented ×1

Code Example

import numpy as np
import torch as tt
v = np.ones((2,2))
t = tt.ones((2,2))
v.flatten()[:] = np.zeros(4)
t.flatten()[:] = tt.zeros(4)
v, t
(array([[1., 1.],
        [1., 1.]]),
tensor([[0., 0.],
        [0., 0.]]))
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

simple code sample

import numpy as np
import torch as tt
v = np.ones((2,2))
t = tt.ones((2,2))
v.flatten()[:] = np.zeros(4)
t.flatten()[:] = tt.zeros(4)
v, t
(array([[1., 1.],
        [1., 1.]]),
tensor([[0., 0.],
        [0., 0.]]))

this deviation is dangerous and should be clearly mentioned in documentation, or better changed (likely on numpy side, in my opinion)

Versions

Python 3.13.12, Windows 10 numpy 2.3.5 torch 2.10.0+cu130

cc @svekars @sekyondaMeta @AlannaBurke

extent analysis

Fix Plan

The fix involves modifying the NumPy library to match PyTorch's behavior when using the flatten() method.

Step-by-Step Solution

  • Option 1: Update NumPy code
    • Modify the flatten() method in NumPy to return a copy instead of a view.
    • However, this might be a significant change and could break existing code.
  • Option 2: Use NumPy's ravel() method
    • Replace v.flatten() with v.ravel() to get a view of the original array.
    • Then, assign the new values using v.ravel()[:] = np.zeros(4).
    • Example:

import numpy as np import torch as tt

v = np.ones((2,2)) t = tt.ones((2,2))

Use ravel() instead of flatten()

v.ravel()[:] = np.zeros(4) t.flatten()[:] = tt.zeros(4)

print(v, t)

    *   This should output:
        ```
[[0. 0.]
 [0. 0.]] tensor([[0., 0.],
        [0., 0.]])
  • Option 3: Create a copy of the NumPy array
    • Use the copy() method to create a copy of the array before assigning new values.
    • Example:

import numpy as np import torch as tt

v = np.ones((2,2)) t = tt.ones((2,2))

Create a copy of the array

v_copy = v.copy() v_copy.flatten()[:] = np.zeros(4) t.flatten()[:] = tt.zeros(4)

print(v_copy, t)

    *   This should output:
        ```
[[0. 0.]
 [0. 0.]] tensor([[0., 0.],
        [0., 0.]])

Verification

Verify that the fix worked by checking the output of the modified code. The NumPy array should now be modified correctly, matching the behavior of the PyTorch tensor.

Extra Tips

  • Be aware of the differences in behavior between NumPy and PyTorch when using the flatten() method.
  • Use the ravel() method or create a copy of the array to ensure the desired behavior.
  • Consider adding documentation to highlight this difference and provide guidance on how to handle it.

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