RuntimeError when using Pytorch

Tianming_Qu: <p>RuntimeError: dst.nbytes() &gt;= (dst.storage_offset() * dst.element_size()) INTERNAL ASSERT FAILED at “/Users/runner/work/pytorch/pytorch/pytorch/aten/src/ATen/native/mps/operations/Copy.mm”:130, please report a bug to PyTorch.</p>
<p>Dear Developers,</p>
<p>I got an error like this, could you give me some instructions on why this happens and how I can solve it?</p>
<p>Best,<br>
Tianming</p>

eqy: <p>In my experience the MPS backend is still relatively new and has some bugs that need to be worked out. Could you send a small code snippet that reproduces this issue on your end?</p>

Tianming_Qu: <p>Sure, here is the code:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/f/df07965ab38f9809587db2f51eeb6b8c19ebda55.png" data-download-href="https://discuss.pytorch.org/uploads/default/df07965ab38f9809587db2f51eeb6b8c19ebda55" title="Screen Shot 2022-12-08 at 9.04.06 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/df07965ab38f9809587db2f51eeb6b8c19ebda55_2_690x219.png" alt="Screen Shot 2022-12-08 at 9.04.06 AM" data-base62-sha1="vP0JmxNshAYe7prtbbVIcd842Dr" width="690" height="219" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/df07965ab38f9809587db2f51eeb6b8c19ebda55_2_690x219.png, https://discuss.pytorch.org/uploads/default/original/3X/d/f/df07965ab38f9809587db2f51eeb6b8c19ebda55.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/f/df07965ab38f9809587db2f51eeb6b8c19ebda55.png 2x" data-dominant-color="EFF0EE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2022-12-08 at 9.04.06 AM</span><span class="informations">1005×319 125 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
And here is the Gaussian Fxn I defined:</p>
<p>def gaussian(Sj1,Slj1,Sj2,Slj2):</p>
<pre><code>sigma1 = torch.tensor(0.5)
sigma2 = torch.tensor(0.5)
Kb = torch.tensor(0.001987204)
T = torch.tensor(300)
h0 = torch.tensor(0.0001)
g = torch.tensor(0)
C1 = torch.tensor(0)
C2 = torch.tensor(0)
for i in range((torch.where(Slj2 == Sj2)[0][0] - 5),(torch.where(Slj2 == Sj2)[0][0] + 6)):
    if i &lt; 0:
        C2 = i + 1000
    elif i &gt; 999:
        C2 = i - 1000
    else:
        C2 = i

    for j in range((torch.where(Slj1 == Sj1)[0][0] - 5),(torch.where(Slj2 == Sj2)[0][0] + 6)):
        if j &lt; 0:
            C1 = j + 1000
        elif j &gt; 999:
            C1 = j -1000
        else:
            C1 = j 
        
        g = g + count[C2,C1] * h0 * torch.exp( (-(Sj1 - Slj1[C1]) ** 2 / (2 * sigma1 ** 2)) + (-(Sj2 - Slj2[C2]) ** 2 / (2 * sigma2 ** 2)) )        

F[torch.where(Slj2 == Sj2)[0][0]][torch.where(Slj1 == Sj1)[0][0]] = torch.exp(-g / (Kb * T))
#print(F[Slj1.index(Sj1)][Slj2.index(Sj2)])
return F[torch.where(Slj2 == Sj2)[0][0]][torch.where(Slj1 == Sj1)[0][0]]
</code></pre>

J_Johnson: <p>Seems to me like you might not be using <code>torch.where</code> correctly. It needs 2 more parameters, x and y. See the docs here:</p>
<p><a href="https://pytorch.org/docs/stable/generated/torch.where.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.where.html</a></p>

Tianming_Qu: <p>oh, I really thought torch.where is the same as np.where</p>

J_Johnson: <p>Corrected. I see it can be used as <code>torch.nonzero(condition, as_tuple=True)</code>.</p>

J_Johnson: <p>This should probably be raised on the Github page with a minimalist example(i.e. remove as much code as you can to still reproduce the error).</p>

Tianming_Qu: <aside class="quote no-group" data-username="J_Johnson" data-post="6" data-topic="167796">
<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/j_johnson/48/55494_2.png" class="avatar"> J_Johnson:</div>
<blockquote>
<p>torch.nonzero(condition, as_tuple=True)</p>
</blockquote>
</aside>
<p>emmm, I still have the same issue after I change torch.where to torch.nonzero <img src="https://discuss.pytorch.org/images/emoji/apple/melting_face.png?v=12" title=":melting_face:" class="emoji" alt=":melting_face:" loading="lazy" width="20" height="20"></p>

J_Johnson: <p>Yes, I meant I stood corrected.</p>