Skip to content

Conversation

@ysiraichi
Copy link
Collaborator

@ysiraichi ysiraichi commented Sep 7, 2021

Stack from ghstack:

Fix: #64389

Differential Revision: D31299999

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Sep 7, 2021

🔗 Helpful links

💊 CI failures summary and remediations

As of commit 8e11f36 (more details on the Dr. CI page):


  • 1/1 failures introduced in this PR

🕵️ 1 new failure recognized by patterns

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

See CircleCI build pytorch_linux_xenial_py3_clang7_asan_test2 (1/1)

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

Sep 27 13:32:32 SUMMARY: UndefinedBehaviorSanit.../jenkins/workspace/aten/src/ATen/Utils.cpp:20:3 in
Sep 27 13:32:31     #9 0x5613c90128f2 in PyEval_EvalCode /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Python/ceval.c:731
Sep 27 13:32:31     #10 0x5613c907acd5 in run_mod /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Python/pythonrun.c:1025
Sep 27 13:32:31     #11 0x5613c907cd5d in PyRun_StringFlags /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Python/pythonrun.c:949
Sep 27 13:32:31     #12 0x5613c907cdbb in PyRun_SimpleStringFlags /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Python/pythonrun.c:445
Sep 27 13:32:31     #13 0x5613c907d926 in run_command /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Modules/main.c:301
Sep 27 13:32:31     #14 0x5613c907d926 in Py_Main /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Modules/main.c:749
Sep 27 13:32:31     #15 0x5613c8fb7196 in main /home/builder/ktietz/cos6/ci_cos6/python_1622833237666/work/Programs/python.c:69
Sep 27 13:32:32     #16 0x7f20f882e83f in __libc_start_main /build/glibc-S7Ft5T/glibc-2.23/csu/../csu/libc-start.c:291
Sep 27 13:32:32     #17 0x5613c904733d in _start (/opt/conda/bin/python3.6+0x1a733d)
Sep 27 13:32:32 
Sep 27 13:32:32 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /var/lib/jenkins/workspace/aten/src/ATen/Utils.cpp:20:3 in 
Sep 27 13:32:32 + retcode=1
Sep 27 13:32:32 + set -e
Sep 27 13:32:32 + return 1
Sep 27 13:32:32 + [[ pytorch-linux-xenial-py3-clang7-asan-test2 == *-NO_AVX-* ]]
Sep 27 13:32:32 + [[ '' == \n\o\g\p\u\_\N\O\_\A\V\X ]]
Sep 27 13:32:32 + [[ pytorch-linux-xenial-py3-clang7-asan-test2 == *-NO_AVX2-* ]]
Sep 27 13:32:32 + [[ '' == \n\o\g\p\u\_\N\O\_\A\V\X\2 ]]
Sep 27 13:32:32 + [[ pytorch-linux-xenial-py3-clang7-asan-test2 == *-NO_AVX512-* ]]
Sep 27 13:32:32 + [[ '' == \n\o\g\p\u\_\N\O\_\A\V\X\5\1\2 ]]
Sep 27 13:32:32 ++ mktemp

1 job timed out:

  • pytorch_linux_xenial_py3_clang7_asan_test2

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 to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

ysiraichi added a commit that referenced this pull request Sep 7, 2021
@ysiraichi ysiraichi marked this pull request as ready for review September 7, 2021 11:14
@codecov
Copy link

codecov bot commented Sep 7, 2021

Codecov Report

Merging #64568 (a6c7540) into gh/ysiraichi/28/base (32fbeb1) will increase coverage by 0.07%.
The diff coverage is n/a.

❗ Current head a6c7540 differs from pull request most recent head a02e00c. Consider uploading reports for the commit a02e00c to get more accurate results

@@                   Coverage Diff                    @@
##           gh/ysiraichi/28/base   #64568      +/-   ##
========================================================
+ Coverage                 66.64%   66.72%   +0.07%     
========================================================
  Files                       707      707              
  Lines                     92346    92346              
========================================================
+ Hits                      61546    61619      +73     
+ Misses                    30800    30727      -73     

@ysiraichi ysiraichi requested a review from mruberry September 8, 2021 12:19
@ysiraichi
Copy link
Collaborator Author

@mruberry this is a friendly reminder. Do you have some time to take a look at this PR?

:meth:`torch.Tensor.expand`, are easier to read and are therefore more
advisable to use.
.. warning::
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @ysiraichi, thanks for the ping and sorry to make you wait for a review. Now let's talk about as_strided and empty_strided and use this opportunity to correct and modernize their documentation.

First, I don't think the documentation for this function is correct, and that's part of what has made this PR hard to review. Let's look at an example program:

t = torch.tensor((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))[::2]
# t = tensor([1, 3, 5, 7, 9])
v = torch.as_strided(t, size=[2], stride=[1])
# v = tensor([1, 2])

In what sense is v a view of t? It contains a value not found in t!

And when we look at the implementation of as_strided this makes sense:

Tensor as_strided_tensorimpl(const Tensor& self, IntArrayRef size, IntArrayRef stride, optional<int64_t> storage_offset_) {

because it seems like as_strided creates a view of :attr:`input`'s storage, not a view of :attr:`input`. So first we need to explain that.

Now let's talk about warning. It's trying to tell us two things:

  • creating an "overlapped" tensor where multiple indices refer to the same element is a bad idea
  • you probably shouldn't use this function

This seems like a strange warning to me, so let's dig into its history: #22842 (comment). Seems like there were concerns about exposing as_strided at all, which is reasonable, but I think we can do a better job with this warning.

What would you think of changing the warning to this, instead:

"Prefer using other view functions, like :meth:`torch.Tensor.expand`, to setting a view's strides manually with `as_strided`, as this function's behavior depends on the implementation of a tensor's storage. The constructed view of the storage must only refer to elements within the storage or a runtime error will be thrown, and if the view is "overlapped" (with multiple indices referring to the same element in memory) its behavior is undefined."

This more clearly communicates why this function shouldn't be used -- because storage is an implementation detail and relying on it is inherently risky and strange for users. This also simplifies the other warnings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That sounds way better! Thanks, @mruberry. I will change it right away!

r"""
empty_strided(size, stride, *, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) -> Tensor
Returns a tensor filled with uninitialized data. The shape and strides of the tensor is
Copy link
Collaborator

Choose a reason for hiding this comment

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

In light of the above discussion we should carefully update the documentation of empty_strided, too.

Let's start with the first paragraph:

"Creates a tensor with the specified :attr:`size` and :attr:`stride` and filled with undefined data."

This modernizes the language, preserves this PR's fix, and doesn't include implementation details like how much memory is allocated.

I don't know if a note about about how much memory an implementation of this function could use is interesting to users. So maybe we could drop it?

Now let's talk about the warning for this function. This warning is intended to say "don't create an overlapped tensor", and we can do that more easily (following the above):

"If the constructed tensor is "overlapped" (with multiple indices referring to the same element in memory) its behavior is undefined."

We don't declare "overlapped" behaviors as having undefined behavior in a note (yet), but we probably should.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. The amount of allocated memory doesn't sound that useful here.

ysiraichi added a commit that referenced this pull request Sep 24, 2021
ysiraichi added a commit that referenced this pull request Sep 27, 2021
@ysiraichi
Copy link
Collaborator Author

@mruberry I have addressed your changes and rebased this PR. Do you have some time to take a look at this it?

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.

Cool! Thanks @ysiraichi!

@mruberry
Copy link
Collaborator

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

@facebook-github-bot facebook-github-bot deleted the gh/ysiraichi/28/head branch October 4, 2021 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.empty_strided(size, stride) not really equivalent to torch.empty(size).as_strided(size, stride)

5 participants