-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spurious "NumPy array is not writeable" warning on torch.tensor(np_array)
#47160
Comments
Thank you for your bug report, warning is indeed spurious in this case. |
Hello! I am new to open source development and would like to help to resolve this issue. It would help a lot with a bit of guidance here. |
You probably want to suppress the warning from here: pytorch/torch/csrc/utils/tensor_new.cpp Line 273 in 8d5256e
when |
Sure, I'll try to do that. |
Can I ignore this warning if it is coming from |
Yes, I think you can ignore it |
馃悰 Bug
torch.tensor(a)
, for non-writeable numpy array a, produces this warning:UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor.
But since
torch.tensor(a)
always makes a copy, the warning makes no sense. Avoiding the warning requires making a copy of the numpy array before converting to torch.A partial workaround is to do
torch.from_numpy(a.copy())
.To Reproduce
Steps to reproduce the behavior:
Expected behavior
The warning should only be shown when actually aliasing a read-only buffer with a mutable Tensor, not when copying from a read-only buffer.
Environment
Verified on Windows, pytorch 1.5.1, git_version = '3c31d73c875d9a4a6ea8a843b9a0d1b19fbe36f3' (pip version)
And on linux at pytorch 1.8.0a0fb
Additional context
Warning was introduced in pull 33615. #44027 (read-only tensors) would make this irrelevant.
This warning is important for
from_numpy
, because you can easily accidentally do things like create a mutable view onto a Pythonstr
orbytes
object and overwrite it. Having the warning also give trivial false alarms like this can cause real problems to be overlooked.Probably related:
torch.tensor(nparray, pin_memory=True)
doesn't work, probably for similar reasons: it tries to construct a temporary view before making the copy. If you want the torch tensor in pinned memory, there appears to be no way to avoid the warning without introducing a spurious copy.cc @mruberry @rgommers @heitorschueroff
The text was updated successfully, but these errors were encountered: