Source and weight input channels mismatch

HurstSi: <p>I set the device to ‘mps’<br>
but it shows:</p>
<pre><code class="lang-auto">/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShadersGraph/mpsgraph/MetalPerformanceShadersGraph/Runtimes/MPSRuntime/Operations/GPUConv2DOps.mm:214: failed assertion `Source and weight input channels mismatch'
</code></pre>
<p>and then the program quit.</p>
<p>it also continuous showed me that:</p>
<pre><code class="lang-auto">anaconda3/envs/Unet/lib/python3.8/site-packages/torch/amp/autocast_mode.py:198: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling
  warnings.warn('User provided device_type of \'cuda\', but CUDA is not available. Disabling')

</code></pre>
<p>What’s wrong with this. How can I solve it. Thank you.</p>

HurstSi: <p>/AppleInternal/Library/BuildRoots/b6051351-c030-11ec-96e9-3e7866fcf3a1/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShadersGraph/mpsgraph/MetalPerformanceShadersGraph/Runtimes/MPSRuntime/Operations/GPUConv2DOps.mm:214: failed assertion `Source and weight input channels mismatch’</p>

albanD: <p>Hi,</p>
<p>Are you using AMP? It does not support mps right now I’m afraid (We didn’t had time to work on that yet).<br>
Also the channel mismatch error seems unrelated to mps. Do you get the same thing if you run on CPU?</p>

HurstSi: <p>Yes, I’m using the amp. Really thankful for your contribution.<br>
Maybe there’s something with my code, I didn’t get the mismatch error when I used the device “cpu”</p>

albanD: <p>Can you try to run without AMP on MPS and see if that works?</p>

JeremyM: <p>I am getting this same error. As far as I’m aware I’m not using AMP. I do not get the same error if I run on CPU, everything runs fine until I use MPS with 2D convolution.</p>

JeremyM: <p>I was able to write a minimal reproducer.</p>
<pre data-code-wrap="import"><code class="lang-nohighlight">import numpy as np
device = torch.device("mps")

conv = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3).to(device)

data = torch.tensor(np.random.uniform(size=(1, 10, 10, 1)), dtype=torch.float32).to(device)
optimizer = torch.optim.Adam(conv.parameters(), lr=0.1)
x = data.permute(0, 3, 1, 2)
out = torch.sum(conv(x))
loss = torch.nn.MSELoss()(out, torch.zeros_like(out))
optimizer.zero_grad()
out.backward()
optimizer.step()
</code></pre>
<p>my output is</p>
<pre><code class="lang-auto">$ python3 reproducer.py
/AppleInternal/Library/BuildRoots/560148d7-a559-11ec-8c96-4add460b61a6/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShadersGraph/mpsgraph/MetalPerformanceShadersGraph/Runtimes/MPSRuntime/Operations/GPUConv2DOps.mm:214: failed assertion `Source and weight input channels mismatch'
zsh: abort      python3 reproducer.py
</code></pre>
<p>If I switch to the “cpu” device it runs without error. My torch versions are</p>
<p>torch                           1.13.0.dev20220826<br>
torchaudio                      0.13.0.dev20220826<br>
torchvision                     0.14.0.dev20220826</p>

zhaoBowen612: <aside class="quote no-group" data-username="JeremyM" data-post="7" data-topic="152559">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jeremym/48/45404_2.png" class="avatar"> JeremyM:</div>
<blockquote>
<p>M</p>
</blockquote>
</aside>
<p>You forgot to import torch.<br>
I can also reproduce this issue. Could you please issue this in the repository of torch in Github?</p>

JeremyM: <aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/84206">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/84206" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/84206" target="_blank" rel="noopener nofollow ugc">source and weight input channels mismatch when using Conv2D on mps</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-08-29" data-time="17:27:26" data-timezone="UTC">05:27PM - 29 Aug 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/mcgibbon" target="_blank" rel="noopener nofollow ugc">
          <img alt="mcgibbon" src="https://avatars.githubusercontent.com/u/12307589?v=4" class="onebox-avatar-inline" width="20" height="20">
          mcgibbon
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: convolution
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: mps
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

When using 2D convolution on an M1 macbook with the mp<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">s device, python crashes. This can be reproduced with:

```python
import numpy as np
import torch
device = torch.device("mps")

conv = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3).to(device)

data = torch.tensor(np.random.uniform(size=(1, 10, 10, 1)), dtype=torch.float32).to(device)
optimizer = torch.optim.Adam(conv.parameters(), lr=0.1)
x = data.permute(0, 3, 1, 2)
out = torch.sum(conv(x))
loss = torch.nn.MSELoss()(out, torch.zeros_like(out))
optimizer.zero_grad()
out.backward()
optimizer.step()
```

Which on my machine outputs:
```bash
$ python3 reproducer.py
/AppleInternal/Library/BuildRoots/560148d7-a559-11ec-8c96-4add460b61a6/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShadersGraph/mpsgraph/MetalPerformanceShadersGraph/Runtimes/MPSRuntime/Operations/GPUConv2DOps.mm:214: failed assertion `Source and weight input channels mismatch'
zsh: abort      python3 reproducer.py
```

If I run the example above with the "cpu" device, it exits without error.

### Versions

```bash
$ python3 collect_env.py
Collecting environment information...
PyTorch version: 1.13.0.dev20220826
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (arm64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2)
CMake version: version 3.23.1
Libc version: N/A

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:14)  [Clang 12.0.1 ] (64-bit runtime)
Python platform: macOS-12.3.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] torch==1.13.0.dev20220826
[pip3] torchaudio==0.13.0.dev20220826
[pip3] torchvision==0.14.0.dev20220826
[conda] numpy                     1.21.5           py38hb29071a_0    conda-forge
[conda] torch                     1.13.0.dev20220826          pypi_0    pypi
[conda] torchaudio                0.13.0.dev20220826          pypi_0    pypi
[conda] torchvision               0.14.0.dev20220826          pypi_0    pypi
```

cc @kulinseth @albanD</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


Suryalok1: <p>I am also seeing the same issue.<br>
It seems like the issue is with permute operation.</p>
<p>Try the below:</p>
<p>import numpy as np<br>
device = torch.device(“mps”)</p>
<p>conv = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3).to(device)</p>
<p>data = torch.tensor(np.random.uniform(size=(1, 1, 10, 10)), dtype=torch.float32).to(device)</p>
<p>optimizer = torch.optim.Adam(conv.parameters(), lr=0.1)<br>
x = data<br>
out = torch.sum(conv(x))<br>
loss = torch.nn.MSELoss()(out, torch.zeros_like(out))<br>
optimizer.zero_grad()<br>
out.backward()<br>
optimizer.step()</p>