Really low accuraccy of 0%

SaraMoosbauer: <p>I’ve tried training a simple mnist classifier on maps, when training the exact same code on the cpu I get an accuracy of 98%, however on mps I get 0.00%<br>
Anyone an Idea why that is?</p>

albanD: <p>That is surprising for sure.<br>
Do you have a small code sample to reproduce this issue by any chance?</p>

pannous: <p>0% is very strange since you should get at least 10% for random guesses.</p>

pannous: <p>I reproduced your issue here:</p><aside class="onebox githubgist" data-onebox-src="https://gist.github.com/pannous/0ab32760e8b7750efe980aebbba2eeb6">
  <header class="source">

      <a href="https://gist.github.com/pannous/0ab32760e8b7750efe980aebbba2eeb6" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/pannous/0ab32760e8b7750efe980aebbba2eeb6" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/pannous/0ab32760e8b7750efe980aebbba2eeb6</a></h4>

  <h5>mnist_trivial.py</h5>
  <pre><code class="Python">#!/usr/local/bin/python3
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms

device = 'mps'

# parameters
learning_rate = 0.01
</code></pre>
    This file has been truncated. <a href="https://gist.github.com/pannous/0ab32760e8b7750efe980aebbba2eeb6" target="_blank" rel="noopener nofollow ugc">show original</a>

<p>
</p>

  </article>

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

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

<p>the bug seems to happen in:<br>
<code>accuracy = correct_prediction.float().mean()</code></p>
<p>since the data suggests a mean different from 0.0<br>
<code>correct_predictions tensor([ True,  True,  True,  ...,  True, False,  True], device='mps:0')</code></p>

pannous: <p>Related to the other .float() bug which <a class="mention" href="/u/alband">@albanD</a> identified yesterday and created a ticket for:</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/77849">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/77849" 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/77849" target="_blank" rel="noopener nofollow ugc">Conversion from int to float dtype is not working on MPS device</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:08:32" data-timezone="UTC">02:08PM - 19 May 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/albanD" target="_blank" rel="noopener nofollow ugc">
          <img alt="albanD" src="https://avatars.githubusercontent.com/u/6359743?v=4" class="onebox-avatar-inline" width="20" height="20">
          albanD
        </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;">
          high priority
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triage review
        </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">Simple repro:

```python
import torch

print(torch.tensor([[0, 0], [0, 1], <span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">[1, 0], [1, 1]]).type(torch.float32))
print(torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]], device="mps").type(torch.float32))
```

This returns:
```
tensor([[0., 0.],
        [0., 1.],
        [1., 0.],
        [1., 1.]])
tensor([[0., 0.],
        [0., 0.],
        [0., 0.],
        [0., 0.]], device='mps:0')
```

cc @ezyang @gchanan @zou3519</span></p>
  </div>

  </article>

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

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

<p>In the meantime it can be solved by using<br>
<code>accuracy = float(correct_prediction.sum())/len(correct_prediction)</code></p>

SaraMoosbauer: <p>Thank you so much, I also found that the bug is happening in the evaluate function as the trainings loss is going down</p>

albanD: <p>We sent a fix for that. The nightly build for tomorrow should have a fix for that.</p>

KMT62: <p>A similar issue is found when executing the sample code here: <a href="https://pytorch.org/tutorials/beginner/basics/quickstart_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Quickstart — PyTorch Tutorials 2.0.1+cu117 documentation</a></p>
<p>Specifically in function test(), line:</p>
<pre><code class="lang-auto">correct += (pred.argmax(1) == y).type(torch.float).sum().item()
</code></pre>
<p>When device = ‘mps’ it always results in 10% accuracy. when device = ‘cpu’, the accuracy is as expected.</p>