pytorch - 💡(How to fix) Fix Expose internal `warn_if_not_writeable` on Python `torch.from_numpy` [10 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

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#178261Fetched 2026-04-08 01:20:53
View on GitHub
Comments
10
Participants
3
Timeline
41
Reactions
0
Timeline (top)
subscribed ×13mentioned ×12commented ×10labeled ×5

Code Example

torch.from_numpy(ndarray, *, warn_if_not_writeable=True)

---

import numpy as np
import torch
from PIL import Image

img = Image.new("L", (4, 1), color=7)
arr = np.asarray(img)
assert arr.flags.writeable is False
torch.from_numpy(arr) # warn_if_not_writeable=True) > this is what I'm proposing, to prevent the warning
RAW_BUFFERClick to expand / collapse

Pitch

I’d like to propose exposing the existing internal warn_if_not_writeable flag on torch.from_numpy as a keyword argument, defaulting to True:

torch.from_numpy(ndarray, *, warn_if_not_writeable=True)

I have a use case where I intentionally want zero-copy aliasing from a read-only NumPy array, and would like to suppress the warning.

A per-call opt-out seems to me like the right thing here, especially since this is already exposed internally. If this is acceptable, may I open a PR for it (It would be my first contribution to PyTorch, I'll read all documentation etc.)?

Alternatives

Right now the alternatives are:

  1. local warnings.catch_warnings() + filterwarnings(...), which is awkward
  2. global filter, which is too broad and can hide a real bug at other callsites where the warning would be generated.

Example of warning ITW

import numpy as np
import torch
from PIL import Image

img = Image.new("L", (4, 1), color=7)
arr = np.asarray(img)
assert arr.flags.writeable is False
torch.from_numpy(arr) # warn_if_not_writeable=True) > this is what I'm proposing, to prevent the warning

Prints: UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:213.)

cc @mruberry @rgommers @albanD

extent analysis

Fix Plan

To expose the existing internal warn_if_not_writeable flag on torch.from_numpy as a keyword argument, follow these steps:

  • Modify the torch.from_numpy function to accept a warn_if_not_writeable keyword argument with a default value of True.
  • Update the warning logic to check the value of warn_if_not_writeable before emitting the warning.

Example code:

def from_numpy(ndarray, *, warn_if_not_writeable=True):
    if not ndarray.flags.writeable and warn_if_not_writeable:
        warnings.warn("The given NumPy array is not writable, and PyTorch does not support non-writable tensors.")
    # ... rest of the function remains the same ...

To suppress the warning, call torch.from_numpy with warn_if_not_writeable=False:

torch.from_numpy(arr, warn_if_not_writeable=False)

Verification

Verify that the fix works by running the example code with warn_if_not_writeable=False and checking that no warning is emitted:

import numpy as np
import torch
from PIL import Image

img = Image.new("L", (4, 1), color=7)
arr = np.asarray(img)
torch.from_numpy(arr, warn_if_not_writeable=False)  # No warning should be printed

Extra Tips

  • When contributing to PyTorch, make sure to follow the project's coding standards and documentation guidelines.
  • Test your changes thoroughly to ensure that they do not introduce any regressions.

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