# Autograd failed after add max\_pool1d layer

**URL:** <https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812>\
**Category:** autograd\
**Created:** [April 8, 2020, 7:13am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812 "2020-04-08T07:13:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nanachi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@Nanachi](https://discuss.pytorch.org/u/Nanachi)\
**Post date:** [April 8, 2020, 7:13am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/1 "2020-04-08T07:13:02Z")

</div>

### Update, the problem is solved.

if you encounter with the same issue, try using .contiguous() as following example from @ptrblck

> [@ptrblck](#):
>
> x= F.max\_pool1d(x.transpose(1,2).contiguous(), kernel\_size=x.size()[1])

Hi all,  
I did slight change on a nn by adding a max\_pool1d and get error traceback

```auto
Traceback (most recent call last):
  File "../../learn/training.py", line 212, in train
    loss.backward()
  File "C:\Users\Veid\Anaconda3\envs\caml\lib\site-packages\torch\tensor.py", line 195, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "C:\Users\Veid\Anaconda3\envs\caml\lib\site-packages\torch\autograd\ __init__.py", line 99, in backward
    allow_unreachable=True) # allow_unreachable flag
RuntimeError: fractional_max_pool2d_backward_out_cuda failed with error code 0

```

How can I correct this right?  
I’m new with torch so I apologize in advance if this is a stupid question.

The original code snippet that works successfully

```python
        #x shape is torch.Size([8, k, 400]) where k is an unfixed number
        #U.weight shape is torch.Size([50, 400])
        alpha = F.softmax(self.U.weight.matmul(x.transpose(1,2)), dim=2)
        #alpha shape is torch.Size([8, 50, k])
        m = alpha.matmul(x)
        #m shape is torch.Size([8, 50, 400])
        #final.weight shape is torch.Size([50, 400])
        y = self.final.weight.mul(m).sum(dim=2).add(self.final.bias)
        #y shape is torch.Size([8, 50])

```

The code snippet after changing that fails to autograd

```auto
        #x shape is torch.Size([8, k, 400]) where k is an unfixed number, 8 is the batch size
        #U.weight shape is torch.Size([50, 400])
        x= F.max_pool1d(x.transpose(1,2), kernel_size=x.size()[1])
        #after max pooling, x shape is torch.Size([8, 400, 1])
        alpha = self.U.weight.mul(x.transpose(1,2)) 
        #alpha shape is torch.Size([8, 50, 400])
        #final.weight shape is torch.Size([50, 400])
        y = self.final.weight.mul(alpha).sum(dim=2).add(self.final.bias)
        #y shape is torch.Size([8, 50])

```

Here is the runable code that I extracted related Variable defination and loss computation part. But the bug can not be reproduced in this  
setting. ☹

```auto
import torch.nn as nn
import torch
import torch.nn.functional as F
from torch.nn.init import xavier_uniform_
from torch.autograd import Variable
target=Variable(torch.randn(8,50))
U = nn.Linear(400, 50)
xavier_uniform_(U.weight)
final = nn.Linear(400, 50)
xavier_uniform_(final.weight)
x = Variable(torch.randn(8,123,400))
'''
#The code snippet that works successfully
alpha = F.softmax(U.weight.matmul(x.transpose(1,2)), dim=2)
m = alpha.matmul(x)
y = final.weight.mul(m).sum(dim=2).add(final.bias)
'''
'''
#The code snippet that fails to autograd
x= F.max_pool1d(x.transpose(1,2), kernel_size=x.size()[1])
alpha = U.weight.mul(x.transpose(1,2)) 
y = final.weight.mul(alpha).sum(dim=2).add(final.bias)
'''
yhat= y
loss = F.binary_cross_entropy_with_logits(yhat, target)
loss.backward()
print(y.size())

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 8, 2020, 7:51am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/2 "2020-04-08T07:51:41Z")

</div>

Could you pose an executable code snippet by wrapping it into three backticks ```, please? 🙂  
This would make debugging much easier.

---

<div class="post-metadata">

**Author:** ![Nanachi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@Nanachi](https://discuss.pytorch.org/u/Nanachi)\
**Post date:** [April 8, 2020, 10:21am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/3 "2020-04-08T10:21:37Z")

</div>

Thanks for your reply!  
I have updated the code snippet.  
I also tried to use a runable test code to reproduce the bug but failed.  
I also updated the test code, I hope this could be help.  
Thanks again for your kind help 🙂

---

<div class="post-metadata">

**Author:** ![braindotai](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/braindotai/32/61373_2.png) [@braindotai](https://discuss.pytorch.org/u/braindotai)\
**Post date:** [April 8, 2020, 10:30am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/4 "2020-04-08T10:30:59Z")

</div>

you can use this key ` which is located left to “1”. So if you want to provide some code then just add it like-  
 ![Annotation 2020-04-08 155857](https://discuss.pytorch.org/uploads/default/original/3X/5/0/50bde2362411e827e02d4d5dcca7041ee3c86a9b.png)  
Try it, this would make your code live 😉

---

<div class="post-metadata">

**Author:** ![Nanachi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@Nanachi](https://discuss.pytorch.org/u/Nanachi)\
**Post date:** [April 8, 2020, 10:37am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/5 "2020-04-08T10:37:55Z")

</div>

Thanks for your kind help!  
It looks much better now 😊

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 8, 2020, 9:59pm UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/6 "2020-04-08T21:59:52Z")

</div>

Thanks for the code snippet.  
Since it’s not reproducible with the last code, could you try to use:

```python
x= F.max_pool1d(x.transpose(1,2).contiguous(), kernel_size=x.size()[1])

```

and rerun the script, please?

Also, which PyTorch version are you using?  
Make sure to update to the latest stable release, as `Variables` are deprecated since `0.4`.

---

<div class="post-metadata">

**Author:** ![Nanachi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@Nanachi](https://discuss.pytorch.org/u/Nanachi)\
**Post date:** [April 9, 2020, 7:02am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/7 "2020-04-09T07:02:21Z")

</div>

Thank you! It worked.  
I am using PyTorch1.4.0  
Seems the bug is coming from misuse of max\_pool1d with Variables?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 9, 2020, 7:46am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/8 "2020-04-09T07:46:20Z")

</div>

I think it’s coming from passing a non-contiguous tensor to the pooling layer, which was recently fixed, so it should work using the nightly binaries.

---

<div class="post-metadata">

**Author:** ![Nanachi](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@Nanachi](https://discuss.pytorch.org/u/Nanachi)\
**Post date:** [April 9, 2020, 10:54am UTC](https://discuss.pytorch.org/t/autograd-failed-after-add-max-pool1d-layer/75812/9 "2020-04-09T10:54:58Z")

</div>

I got it. Thanks again for your kind help! 😃
