MPSNDArray Error: buffer is not large enough

astroboylrx: <p>The code below exits with this error:<br>
<code>/AppleInternal/Library/BuildRoots/.../Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 1024 bytes</code></p>
<pre><code class="lang-python">import torch
mps = torch.device("mps")

size = 16
A = torch.rand(size, device=mps)
F = torch.rand(size, size, size, device=mps)
print(A@F)
</code></pre>
<p>What happened? Thanks a lot in advance.</p>

philipturner: <p>That’s a bug that the PyTorch team needs to fix. Metal throws a fatal error if the descriptor doesn’t match the allocated buffer’s size. <a class="mention" href="/u/alband">@albanD</a> does the backend compile with Metal API validation on? I can’t remember whether that would trigger such an assertion failure, or if MPS’s validation mechanisms are strictly independent of the underlying Metal framework.</p>

albanD: <p>Hi,<br>
Thanks for the report. That does look like a bug on our end!<br>
Could you open an issue on github with this code sample so that we can easily track the fix for this?</p>

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

      <a href="https://github.com/pytorch/pytorch/issues/77851" 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/77851" target="_blank" rel="noopener nofollow ugc">MPSNDArray allocated with wrong size</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-05-19" data-time="14:27:56" data-timezone="UTC">02:27PM - 19 May 22 UTC</span>
      </div>


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

    <div class="labels">
    </div>
  </div>
</div>

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

The following code:
```python
import torch
mps = torc<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">h.device("mps")

size = 16
A = torch.rand(size, device=mps)
F = torch.rand(size, size, size, device=mps)
print(A@F)
```

Produces the following error:
```
/AppleInternal/Library/BuildRoots/.../Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 1024 bytes
```

### Versions

The PyTorch Forums thread linked below should help steer discussion on this:

https://discuss.pytorch.org/t/mpsndarray-error-buffer-is-not-large-enough/152030</span></p>
  </div>

  </article>

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

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


astroboylrx: <p>Many thanks for creating the issue.</p>

Marie_Jonsson: <p>I have the error.<br>
/AppleInternal/Library/BuildRoots/8d3bda53-8d9c-11ec-abd7-fa6a1964e34e/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Types/MPSNDArray.mm:782: failed assertion `[MPSNDArray, initWithBuffer:descriptor:] Error: buffer is not large enough. Must be 32768 bytes<br>
’<br>
I use Macbook Pro M1 (Monterey), PyCharm, homebrew, miniforge3. It starts after the last line of code in script. Everything else runs as it should.</p>
<p>Last line of code:<br>
train(bert_classifier, train_dataloader, val_dataloader, epochs=2, evaluation=True)</p>

philipturner: <p>From now on, could we keep discussion on the GitHub issue instead of posting on this thread?</p>

Marie_Jonsson: <p>Ok, I were not aware of that pytorch used GitHub for issues. Regards Marie</p>