RuntimeError: assigned grad has data of a different size

sswwhh: <p>Hi!</p>
<p>After the update of pytorch from 1.9 → 1.10, I get an error like the screenshot, which used to work until last Friday. Can somebody help me with this?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/7/b76f7ace64eca41fd562bc4d2306c06b3c3c8555.png" data-download-href="https://discuss.pytorch.org/uploads/default/b76f7ace64eca41fd562bc4d2306c06b3c3c8555" title="Screen Shot 2021-12-06 at 4.36.41 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/7/b76f7ace64eca41fd562bc4d2306c06b3c3c8555_2_600x500.png" alt="Screen Shot 2021-12-06 at 4.36.41 PM" data-base62-sha1="qaKdIzBxHAp0Vis5bKuC1oaPR53" width="600" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/7/b76f7ace64eca41fd562bc4d2306c06b3c3c8555_2_600x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/7/b76f7ace64eca41fd562bc4d2306c06b3c3c8555_2_900x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/7/b76f7ace64eca41fd562bc4d2306c06b3c3c8555.png 2x" data-dominant-color="F6F8F8"><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 2021-12-06 at 4.36.41 PM</span><span class="informations">1010×841 144 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></p>

ptrblck: <p>Based on the screenshot it seems you are manually assigning <code>dy.unsqueeze(0).clone()</code> to <code>param.grad</code>, which is causing the error. Check the shape of <code>param</code>. <code>param.grad</code>, and <code>dy.unsqueeze(0)</code> before the assignment and make sure all have the same shape.</p>

sswwhh: <p>hi, this used to work until last Friday before the pytorch 1.10 was updated. What I am doing here is assigning <code>dy</code> as <code>param.grad</code> in LBFGS inside a closure. I am curious why it started not working after the new pytorch version.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/7/47b6e8281a0f3872cd87a047588a87ce6c456dca.png" data-download-href="https://discuss.pytorch.org/uploads/default/47b6e8281a0f3872cd87a047588a87ce6c456dca" title="Screen Shot 2021-12-07 at 12.41.51 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47b6e8281a0f3872cd87a047588a87ce6c456dca_2_690x465.png" alt="Screen Shot 2021-12-07 at 12.41.51 AM" data-base62-sha1="aepM92HzOaiLFEqZDXO83MxaErw" width="690" height="465" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47b6e8281a0f3872cd87a047588a87ce6c456dca_2_690x465.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/7/47b6e8281a0f3872cd87a047588a87ce6c456dca_2_1035x697.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/7/47b6e8281a0f3872cd87a047588a87ce6c456dca.png 2x" data-dominant-color="373F48"><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 2021-12-07 at 12.41.51 AM</span><span class="informations">1044×704 75.8 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></p>

ptrblck: <p>I don’t know why the code started to fail in a newer release, but would still recommend to check the mentioned shape and based on this track down why these shapes are wrong now and what might have changed (maybe a method is returning a different shape now or using another default).<br>
You could also check for deprecation warnings in the older (working) release and see if you were ignoring any shape changes.</p>

sswwhh: <p>Here is the result below, but one thing I notice is if I run this is gpu it works, and if I run this in cpu, it doesn’t. Do you have any suggestions?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/4/74465c8c9bb43f1e013cf368a3503bdb7ed905c0.png" data-download-href="https://discuss.pytorch.org/uploads/default/74465c8c9bb43f1e013cf368a3503bdb7ed905c0" title="Screen Shot 2021-12-07 at 9.36.38 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/4/74465c8c9bb43f1e013cf368a3503bdb7ed905c0_2_690x454.png" alt="Screen Shot 2021-12-07 at 9.36.38 AM" data-base62-sha1="gAC8oBQfKRcM1pn7yv0AcADKMes" width="690" height="454" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/4/74465c8c9bb43f1e013cf368a3503bdb7ed905c0_2_690x454.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/4/74465c8c9bb43f1e013cf368a3503bdb7ed905c0_2_1035x681.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/4/74465c8c9bb43f1e013cf368a3503bdb7ed905c0_2_1380x908.png 2x" data-dominant-color="F6F5F4"><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 2021-12-07 at 9.36.38 AM</span><span class="informations">1384×912 106 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></p>

ptrblck: <p>Thanks for the update. The output shows different shapes of the gradient and parameter so could you explain what the gradient would represent and how it should be applied on the parameter?<br>
I don’t know why the GPU run isn’t failing, but assume it should and might be missing a check.</p>
<p>EDIT: I just checked this wrong assignment and it’s failing for CPU and CUDA tensors in my setup, so the check seems to be OK in a recent master build.</p>

sswwhh: <p>Thanks, It will be actually very nice for me to show you the whole code very quickly to show you the overall picture by sharing screen call…</p>

sswwhh: <p>There was a similar issue here too, but I am having trouble understanding this</p>
<aside class="quote quote-modified" data-post="1" data-topic="95373">
  <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/siladittya_manna/48/26984_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/runtimeerror-assigned-grad-has-data-of-a-different-type/95373">RuntimeError: assigned grad has data of a different type</a> <a class="badge-category__wrapper " href="/c/autograd/7"><span data-category-id="7" data-drop-close="true" class="badge-category " title="A category of posts relating to the autograd engine itself."><span class="badge-category__name">autograd</span></span></a>
  </div>
  <blockquote>
    I am trying to assign a ndarray of zeros to the grad of the model parameters, but I am getting the error in the title. 
I was just trying to see if I can assign a numpy array to the param.grad. But in my project I won’t be assigning zeros actually. 
for param in model.parameters():
            #print(param.grad)
            param.grad = torch.from_numpy(np.zeros(param.grad.shape)).type(param.grad.dtype)

I am trying to do this step after loss.backward() step. 
I want to know what is causing this…
  </blockquote>
</aside>