8000 view() after transpose() raises non contiguous error · Issue #764 · pytorch/pytorch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

view() after transpose() raises non contiguous error #764

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

Closed
wkentaro opened this issue Feb 16, 2017 · 4 comments
8000
Closed

view() after transpose() raises non contiguous error #764

wkentaro opened this issue Feb 16, 2017 · 4 comments

Comments

@wkentaro
Copy link
Contributor

I'm running below code, and got error below.
Commenting out y = y.transpose(0, 1) ends up without error.
Is this something expected?

import torch
import torch.nn
from torch.autograd import Variable
import numpy as np

x = Variable(torch.from_numpy(np.random.randn(4, 6, 5)).float(), requires_grad=True)
x = torch.transpose(x, 1, 2)
conv = torch.nn.Conv1d(in_channels=5, out_channels=10, kernel_size=2)
y = conv(x)
y = y.transpose(0, 1)  # this line cause error
y = y.view(-1)
result = torch.sum(y)
result.backward()
Traceback (most recent call last):
  File "spam.py", line 13, in <module>
    y = y.view(-1)
  File "/home/wkentaro/anaconda2/lib/python2.7/site-packages/torch/autograd/variable.py", line 469, in view
    return View(*sizes)(self)
  File "/home/wkentaro/anaconda2/lib/python2.7/site-packages/torch/autograd/_functions/tensor.py", line 96, in forward
    result = i.view(*self.sizes)
RuntimeError: input is not contiguous at /home/wkentaro/Projects/pytorch/src/pytorch/torch/lib/TH/generic/THTensor.c:231

In actual, I'm using .transpose() and .view() to use cross_entropy for (n_batch, channel, height, width) tensor by reshaping it to (n_batch * height * width, channel)

@wkentaro wkentaro changed the title transpose() after view() raises non contiguous error view() after transpose() raises non contiguous error Feb 16, 2017
@fmassa
Copy link
Member
fmassa commented Feb 16, 2017

Yes, this is expected, as view is only supposed to work on contiguous tensors, and transposing a tensor makes it non-contiguous. You can use .contiguous() after transpose to fix your issue

@wkentaro
Copy link
Contributor Author

Thank you for the quick response. It fixes my problem!
I think I'm being familiar to pytorch.

@ducha-aiki
Copy link

@fmassa is there any good reason to add contiguous() into transpose function be default?

@fmassa
Copy link
Member
fmassa commented Jul 25, 2017

@ducha-aiki it is much more efficient to have non-contiguous tensors, as you only need to perform a swap in the sizes/strides, instead of copying the whole memory of the tensor.
The reason why adding a contiguous inside view might not be a good idea is that we would not be guaranteed anymore that the original tensor and the viewed tensor shares the same memory address, which is supposed in the codebase atm.
Not sure which is the best approach, if we would prefer to change view (and maybe break existing code) or add another function which is view+contiguous, and call it something else (like ravel or something)

jjsjann123 pushed a commit to mcarilli/pytorch that referenced this issue Mar 25, 2021
…rch#764)

Predicate inside blockBroadcast rather than enclosing it with a predicate if clause.
KyleCZH pushed a commit to KyleCZH/pytorch that referenced this issue Sep 20, 2021
* update install_miopen.sh for rocm 4.2

* update manywheel/build_all_docker.sh for rocm 4.2

* add rocm4.2 to update_s3_htmls.sh

* fix copy/paste error for rocm in manywheel/build_all_docker.sh

* add rocm 4.2 to build-manywheel-images.yml action
akashveramd pushed a commit to akashveramd/pytorch that referenced this issue Apr 9, 2025
* Add basic setup for precommit

* Update README.md with instructions on installing precommit hooks

---------

Co-authored-by: Illia Silin <98187287+illsilin@users.noreply.github.com>
Co-authored-by: Bartlomiej Wroblewski <bwroblewski10@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0