Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious numpy writable warning #47271

Closed

Conversation

ArtistBanda
Copy link
Contributor

Fixes #47160

@facebook-github-bot
Copy link
Contributor

Hi @ArtistBanda!

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.

In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@dr-ci
Copy link

dr-ci bot commented Nov 3, 2020

💊 CI failures summary and remediations

As of commit 187a07e (more details on the Dr. CI page):


  • 3/3 failures possibly* introduced in this PR
    • 1/3 non-CircleCI failure(s)

🕵️ 2 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build binary_linux_libtorch_3_7m_cpu_devtoolset7_shared-with-deps_build (1/2)

Step: "Checkout pytorch/builder repo" (full log | diagnosis details | 🔁 rerun)

fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878
+ sleep 2 
+ git submodule update --init --recursive 
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 
+ sleep 4 
+ git submodule update --init --recursive 
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 
+ sleep 8 
+ git submodule update --init --recursive 
fatal: reference is not a tree: cd5a9b73c3028d2496666201588111a8c8d84878 
Unable to checkout 'cd5a9b73c3028d2496666201588111a8c8d84878' in submodule path 'third_party/nccl/nccl' 

See CircleCI build pytorch_macos_10_13_py3_test (2/2)

Step: "Test" (full log | diagnosis details | 🔁 rerun)

Nov 10 10:47:28 AssertionError: False is not true : Expected zero exit code but got -15
Nov 10 10:47:28 ---------------------------------------------------------------------- 
Nov 10 10:47:28 Traceback (most recent call last): 
Nov 10 10:47:28   File "/Users/distiller/workspace/miniconda3/lib/python3.7/site-packages/torch/testing/_internal/common_distributed.py", line 278, in wrapper 
Nov 10 10:47:28     self._join_processes(fn) 
Nov 10 10:47:28   File "/Users/distiller/workspace/miniconda3/lib/python3.7/site-packages/torch/testing/_internal/common_distributed.py", line 395, in _join_processes 
Nov 10 10:47:28     self._check_return_codes(elapsed_time) 
Nov 10 10:47:28   File "/Users/distiller/workspace/miniconda3/lib/python3.7/site-packages/torch/testing/_internal/common_distributed.py", line 450, in _check_return_codes 
Nov 10 10:47:28     msg="Expected zero exit code but got {}".format(first_process.exitcode) 
Nov 10 10:47:28   File "/Users/distiller/workspace/miniconda3/lib/python3.7/site-packages/torch/testing/_internal/common_utils.py", line 1183, in assertEqual 
Nov 10 10:47:28     self.assertTrue(result, msg=msg) 
Nov 10 10:47:28 AssertionError: False is not true : Expected zero exit code but got -15 
Nov 10 10:47:28  
Nov 10 10:47:28 ---------------------------------------------------------------------- 
Nov 10 10:47:28 Ran 352 tests in 1435.985s 
Nov 10 10:47:28  
Nov 10 10:47:28 FAILED (failures=1, errors=1, skipped=12) 
Nov 10 10:47:28  
Nov 10 10:47:28 Generating XML reports... 
Nov 10 10:47:28 Generated XML report: test-reports/dist-gloo/TEST-ProcessGroupDdpComparisonTestWithSpawn-20201110102332.xml 
Nov 10 10:47:28 Generated XML report: test-reports/dist-gloo/TEST-ProcessGroupDdpUnderDistAutogradTestWithSpawn-20201110102332.xml 
Nov 10 10:47:28 Generated XML report: test-reports/dist-gloo/TEST-ProcessGroupDistAutogradTestWithSpawn-20201110102332.xml 

ci.pytorch.org: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 34 times.

@codecov
Copy link

codecov bot commented Nov 3, 2020

Codecov Report

Merging #47271 into master will decrease coverage by 20.62%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           master   #47271       +/-   ##
===========================================
- Coverage   81.44%   60.81%   -20.63%     
===========================================
  Files        1798     2748      +950     
  Lines      188223   254027    +65804     
===========================================
+ Hits       153306   154499     +1193     
- Misses      34917    99528    +64611     

@@ -125,13 +125,13 @@ PyObject* tensor_to_numpy(const at::Tensor& tensor) {
return array.release();
}

at::Tensor tensor_from_numpy(PyObject* obj) {
at::Tensor tensor_from_numpy(PyObject* obj, bool copy_numpy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
at::Tensor tensor_from_numpy(PyObject* obj, bool copy_numpy) {
at::Tensor tensor_from_numpy(PyObject* obj, bool copy_numpy=false) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws an error if the default value is passed in the function implementation. So, false is passed during function declaration only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

37518cd added the comment for reference in implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. sorry I meant to comment on the other macro path.

test/test_tensor_creation_ops.py Outdated Show resolved Hide resolved
@@ -7,7 +7,7 @@ namespace torch { namespace utils {
PyObject* tensor_to_numpy(const at::Tensor& tensor) {
throw std::runtime_error("PyTorch was compiled without NumPy support");
}
at::Tensor tensor_from_numpy(PyObject* obj) {
at::Tensor tensor_from_numpy(PyObject* obj, bool copy_numpy=false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also remove defaults here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if the function is called without passing this argument it may fail. And by default, we can assume that a copy is not created and raise a warning for that.

@ejguan ejguan added the module: numpy Related to numpy support, and also numpy compatibility of our operators label Nov 4, 2020
@ejguan ejguan requested a review from mruberry November 4, 2020 21:09
@ejguan ejguan added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 4, 2020
@ArtistBanda ArtistBanda force-pushed the Spurious-numpy-writable-warning branch from 248b9f6 to 37518cd Compare November 5, 2020 14:33
if (!PyArray_Check(obj)) {
throw TypeError("expected np.ndarray (got %s)", Py_TYPE(obj)->tp_name);
}
auto array = (PyArrayObject*)obj;

if (!PyArray_ISWRITEABLE(array)) {
if (!PyArray_ISWRITEABLE(array) && !copy_numpy) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add a comment here explaining why this warning isn't need when copy_numpy is true?

@@ -125,13 +125,13 @@ PyObject* tensor_to_numpy(const at::Tensor& tensor) {
return array.release();
}

at::Tensor tensor_from_numpy(PyObject* obj) {
at::Tensor tensor_from_numpy(PyObject* obj, bool copy_numpy/*=false*/) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a historic issue, but since this PR updates this function, anyway, would you add a brief comment explaining what this function does (and what the copy_numpy bool does)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: what does /*=false*/ do in the signature?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final nit on this signature: is copy_numpy the correct name for the argument? That would imply that it controls the behavior of the function, but actually this is only used to know whether or not to throw a warning, right?

Would a name like, "warn_if_not_writeable" be more descriptive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: what does /*=false*/ do in the signature?

It's just a comment to tell the default parameter passed is false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a410107 changes the parameter name and adds the comment for reference.

@@ -1145,6 +1145,12 @@ def test_full_out(self, device):
self.assertEqual(torch.full(o.shape, 1., out=o).dtype, o.dtype)
self.assertEqual(torch.full(size, 1, out=o).dtype, o.dtype)

def test_tensor_from_non_writable_numpy(self, device):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment with the issue this test addresses so people can reference it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a410107 adds the comment

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the force push remove the comment including the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm confused. Where is the comment with the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

44ff10e This contains the comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArtistBanda I think @mruberry wants you to add a reference to the issue in this comment. Either the issue ID or the URL to the issue (#47160).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, thank you @thomasjo. I was expected a comment like

# Tests for X, see (link to issue)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, sorry I didn't get that before. I'll add it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

187a07e resolves this

@mruberry
Copy link
Collaborator

mruberry commented Nov 6, 2020

Would you rebase this, please?

@ArtistBanda ArtistBanda force-pushed the Spurious-numpy-writable-warning branch from a410107 to 44ff10e Compare November 6, 2020 10:05
@ArtistBanda
Copy link
Contributor Author

Would you rebase this, please?

This is my first time contributing to a huge open source project and I am still learning every nook and corner of this, and I know force pushing is not the right way to go but I would love some feedback and like to improve and contribute more.

if (!PyArray_ISWRITEABLE(array)) {
// warn_if_not_writable is true when a copy of numpy variable is created.
// the warning is suppressed when a copy is being created.
if (!PyArray_ISWRITEABLE(array) && !warn_if_not_writeable) {
Copy link
Collaborator

@mruberry mruberry Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!warn_if_not_writeable -> warn_if_not_writeable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in c9a7efa

@@ -270,7 +270,7 @@ Tensor internal_new_from_data(

if (PyArray_Check(data)) {
TORCH_CHECK(!pin_memory, "Can't pin tensor constructed from numpy");
auto tensor = tensor_from_numpy(data);
auto tensor = tensor_from_numpy(data, copy_numpy);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy_numpy -> !copy_numpy?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, you want to warn_if_not_writeable when this does not copy, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in c9a7efa

@mruberry
Copy link
Collaborator

Would you rebase this, please?

This is my first time contributing to a huge open source project and I am still learning every nook and corner of this, and I know force pushing is not the right way to go but I would love some feedback and like to improve and contribute more.

No worries. This is just a git issue. You can typically just fetch and pull the main branch into your branch.

@ArtistBanda
Copy link
Contributor Author

@mruberry should I rebase again?

@mruberry
Copy link
Collaborator

@mruberry should I rebase again?

No need.

Copy link
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks @ArtistBanda!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mruberry merged this pull request in 2907447.

@levkk
Copy link

levkk commented Sep 5, 2021

I'm still getting the warning in 1.9.0:
image

@mruberry
Copy link
Collaborator

mruberry commented Sep 6, 2021

@levkk Warning looks correct to me. cc @fmassa, torchvision may want to explicitly copy the NumPy array or suppress the warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed Merged module: numpy Related to numpy support, and also numpy compatibility of our operators open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spurious "NumPy array is not writeable" warning on torch.tensor(np_array)
8 participants