Allow .view on noncontig tensors when certain conditions are met#4062
Allow .view on noncontig tensors when certain conditions are met#4062soumith merged 1 commit intopytorch:masterfrom
Conversation
|
cc @vadimkantorov . This should satisfy your request, right? Let me know if it is not the case. |
|
It seems so. I'm a surprised this is satisfied because @colesbury suggested a new function Will there be an easy way to check if two shapes are compatible? So that I can call |
|
I can now see there's |
|
Will this PR enable also that example |
|
@vadimkantorov : Yes it enables that example. Thanks for bringing that up! It helped me identify and fix a bug with zero-strided tensors in my previous code :). I just added tests for that case. Hmmm about |
|
The original example in #3653 and Please check with @colesbury for this design and semantics :) |
|
They both don't change numel. In the second example, we have The "contiguous" chunk here is all dimensions, i.e., |
|
Nice! We should still add If you decide to implement |
Implements #3653 .
On a high level, a tensor with
sizecan be viewed as aview_sizeif wesizeinto chunks where each chunk is "contiguous" within itself.view_sizeinto same number of chunks, where each chunk pair has matching "numel", i.e. number of subspace.Copying from the doc change in this PR:
For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions :math:
d, d+1, \dots, d+kthat satisfy the following contiguity-like condition that\forall i = 0, \dots, k-1,stride[i] = stride[i+1] \times size[i+1]Otherwise, :func:
contiguousneeds to be called before the tensor can be viewed.The diagram in the added test case might help understanding this as well.