RuntimeError: input is not contiguous
When i reshape a tensor (if the rank is changed), i get the following error. <SCODE>x = ...  # Tensor of shape (100, 20)
x.view(-1)   # expect a tensor of shape (2000)

RuntimeError: input is not contiguous
<ECODE> What does ‘contiguous’ mean and why does this error occur?
yunjey
apaszke
Can you tell when we get this error? I am facing this problem but not always. I am just wondering why this is happening occasionally?
wasiahmad
parth126
colesbury
A classical way to obtain this bug is to use transposition. If you do <SCODE>x = torch.Tensor(5,2)
y = x.t()
<ECODE> Then, the storage of y is still the same than the one of x. You can check: <SCODE>x.fill_(0)
 0  0
 0  0
 0  0
 0  0
 0  0
[torch.FloatTensor of size 5x2]

y
 0  0  0  0  0
 0  0  0  0  0
[torch.FloatTensor of size 2x5]
<ECODE> <SCODE>y.view(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: input is not contiguous at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensor.c:231
<ECODE>
alexis-jacq
Thanks. That makes complete sense now.
parth126
Why is it not good idea to add automatic  tensor.contiguous() to transpose function?
ducha-aiki
I guess, because it saves a lot of memory to keep one storage for each tensor, while you don’t apply any reshaping transformation.
alexis-jacq
So, in the following case z=y.contiguous().view(-1) , will the gradients of some error with respect to z backpropgate to x?.
zakaria_laskar
colesbury
thanks for your clear and useful reply ~~~
alphadl
<SCODE>        xn_lstm = torch.cat((loss_prep, err_prep, grad_prep), 1).unsqueeze(0) # [n_learner_params, 6]
        # normal lstm([loss, grad_prep, train_err]) = lstm(xn)
        n_learner_params = xn_lstm.size(1)
        (lstmh, lstmc) = hs[0] # previous hx from first (standard) lstm i.e. lstm_hx = (lstmh, lstmc) = hs[0]
        if lstmh.size(1) != xn_lstm.size(1): # only true when prev lstm_hx is equal to decoder/controllers hx
            # make sure that h, c from decoder/controller has the right size to go into the meta-optimizer
            expand_size = torch.Size([1,n_learner_params,self.lstm.hidden_size])
            lstmh, lstmc = lstmh.squeeze(0).expand(expand_size), lstmc.squeeze(0).expand(expand_size)
        lstm_out, (lstmh, lstmc) = self.lstm(input=xn_lstm, hx=(lstmh, lstmc))
<ECODE> error: <SCODE>RuntimeError: rnn: hx is not contiguous
<ECODE>
Brando_Miranda