# Possible bug? Linear layer does not throw error on incorrect input size when on GPU

**URL:** <https://discuss.pytorch.org/t/possible-bug-linear-layer-does-not-throw-error-on-incorrect-input-size-when-on-gpu/132432>\
**Category:** Uncategorized\
**Created:** [September 21, 2021, 12:04am UTC](https://discuss.pytorch.org/t/possible-bug-linear-layer-does-not-throw-error-on-incorrect-input-size-when-on-gpu/132432 "2021-09-21T00:04:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ada](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ada/32/32203_2.png) [@Ada](https://discuss.pytorch.org/u/Ada)\
**Post date:** [September 21, 2021, 12:04am UTC](https://discuss.pytorch.org/t/possible-bug-linear-layer-does-not-throw-error-on-incorrect-input-size-when-on-gpu/132432/1 "2021-09-21T00:04:12Z")

</div>

My network threw an error during backprop because layer shapes did not match, and I wondered how/why the network was able to do the forward pass without throwing an error. After investigating, I’ve discovered some weird behavior.

If the model is on the CPU, all is well:

```auto
model = nn.Linear(100,1)
x = torch.randn(1,200)
model(x)

```

throws `RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x200 and 100x1)`, as expected.

However, if I put it on the GPU:

```auto
device = 'cuda:0'
model.to(device)
model(x.to(device))

```

it happily computes without throwing an error.

Is this expected behavior?

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [September 21, 2021, 2:01am UTC](https://discuss.pytorch.org/t/possible-bug-linear-layer-does-not-throw-error-on-incorrect-input-size-when-on-gpu/132432/2 "2021-09-21T02:01:18Z")

</div>

Hi Ada!

> [@Ada](#):
>
> throws `RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x200 and 100x1)`, as expected.
> 
> However, if I put it on the GPU:  
> …  
> it happily computes without throwing an error.

This appears to be a known bug that has recently been fixed. See  
this thread:

> [@Pytorch is allowing incorrect matrix multiplication when using cuda](https://discuss.pytorch.org/t/pytorch-is-allowing-incorrect-matrix-multiplication-when-using-cuda/130664):
>
> Hey everyone, I have a minor issue and I am not sure if it is a bug or I am simply not understanding something. But I noticed that in my project I was getting some strange results for certain configurations and error messages not popping up when expected. To boil it down, I am allowed to multiply matrices of incompatible sizes so long as it takes place on the gpu. When running the following: input = torch.randn(5, 10) network = nn.Linear(3, 3) output = network(input) ## This should fail I …

Best.

K. Frank

---

<div class="post-metadata">

**Author:** ![Ada](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ada/32/32203_2.png) [@Ada](https://discuss.pytorch.org/u/Ada)\
**Post date:** [September 21, 2021, 8:42pm UTC](https://discuss.pytorch.org/t/possible-bug-linear-layer-does-not-throw-error-on-incorrect-input-size-when-on-gpu/132432/3 "2021-09-21T20:42:51Z")

</div>

Thanks for linking that post! I had not succeeded in choosing the right keywords to find it!
