Loading a state dict seems to erase grad

Evan_Vogelbaum: <p>Hi Ya’ll,</p>
<p>I am experiencing a somewhat spooky error, in which the simple act of loading a new state dictionary into my model seems to erase the graph tying parameters to an external matrix.</p>
<p>Here is the code, and I will explain below.</p>
<pre><code class="lang-auto">    for grad, (name, param) in zip(recovered, model.named_parameters()):
        updated_param = param - stepsize * grad  # This is 100% dependent on M
        new_state_dict[name] = updated_param

    model.load_state_dict(new_state_dict)
    l = list(model.parameters())
    test = ag.grad(l[0][0, 0], M, allow_unused=True)  # and somehow this is 100% NOT dependent on M
</code></pre>
<p>Essentially I need to do some manual SGD because of other computations I am doing with M (that is a matrix that is modifying gradients). Every gradient (which is in “recovered”) is dependent on M.</p>
<p>Here is the dillema<br>
updated_param <strong>is</strong> dependent on M<br>
for any name, the parameter contained in new_stat_dict[name] <strong>is</strong> dependent on M<br>
but some how, that last test shows that none of the parameters in my model are dependent on M</p>
<p>Any thoughts on what is going on here would be super helpful!</p>

albanD: <p>Hi,</p>
<p>This is “expected” given the how all these functions are supposed to work.<br>
In particular, load_state_dict is 100% breaking the computational graph so you won’t be able to backpropagate through what it does.</p>
<p>The <code>nn.Parameters()</code> that are recovered by <code>model.parameters()</code> are supposed to be special Tensors that are learnt when you use nn tools.<br>
In your case, your try and hack around that by having some <code>nn.Parameters()</code> that you don’t actually learn.<br>
Unfortunately, you cannot do it this way.</p>
<p>A simple way to do this is to create your own nn.Module that inherits from the orignal module you wanted and that has M as an nn.Parameter() but not the original <code>.weight</code> for example. Then inside the forward pass of this module, you will first compute the new <code>weight</code>, update it with a simple <code>self.weight = new_value</code>, and then call the parent’s module forward method <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Evan_Vogelbaum: <p>Hi Alban,</p>
<p>Thanks so much for breaking that down for me; I’m glad its expected and not a bug. My overall goal is for the graph to continue through the update of the weights/biases in my network by the gradient, because I want to use future losses to update my matrix M.</p>
<p>What I am doing is passing data through the model, getting gradient, applying ops to it using M, updating parameters with that transformed gradient, and then repeating. I am hoping that I can differentiate the loss calculated using parameters updated through gradients modified by M in order to update M. Will what you suggest enable me to do that?</p>

albanD: <p>You can definitely do that, the autograd will have no issue with it. The main issue you’re going to encounter is that the <code>nn</code> tools are not built for that <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"><br>
For example, the basic <code>nn.Conv2d()</code> is built to have learnable parameters. So if you don’t want these to be learnable but intermediary results, you’ll have to hack around it.</p>
<p>So the best way to implement this is going to be very dependent on your exact application.<br>
If you have a code sample showing what you try to do, I’ll be happy to take a look at it. My advice for a first draft would be to do a toy example with limited scope that use as few <code>nn</code> tools as possible. And then you can try adding them back one by one, checking if they break your workflow.</p>

Evan_Vogelbaum: <p>Damn, thats good to know. My goal is to still update the params in the net, but via ag.grad(), not through loss.backward()… It sounds like from what you are saying that even that will not work if I try to use nn.</p>
<p>Luckily, the net I am implementing this on is a super simple Feed-Forward MLP, so I will try implementing it by hand via Torch Tensors and multiplication, and post it here (for people having the same issue in the future, I will detail what parts break my code). Out of curiosity, do you think an elementwise like nn.tanh will also break the computational graph, or am I going to need to program in my own non-linearities as well?</p>

albanD: <p>Hi,<br>
Maybe I’ve been a bit negative.<br>
Here is a code sample of how you can do this for linear:</p>
<p>You can use such module as a dropin replacement for Linear in your model and the rest will work as expected! (avoid multithreaded forward in a single copy of this net <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> )</p>
<pre><code class="lang-auto">import torch
from torch import nn


class MyLinear(nn.Linear):
    def __init__(self, in_size, out_size):
        super().__init__(in_size, out_size)
        self.in_size = in_size
        self.out_size = out_size
        # Remove the original weights and bias
        del self.weight
        del self.bias

        # Add our new parameter
        self.M = nn.Parameter(torch.rand(out_size, 1))

    def forward(self, inp):
        # repopulate the original weights with our custom version
        # You can do any differentiable op here
        self.weight = self.M.expand(self.out_size, self.in_size) * 3
        self.weight[1, 1] = 0 # Mask entry 1,1 in in weight to see some difference in the gradients
        self.bias = self.M.squeeze(-1)

        result = super().forward(inp)

        # Avoid side effects in nn.Module
        del self.weight
        del self.bias

        return result


in_size = 10
out_size = 20

my_layer = MyLinear(in_size, out_size)

inp = torch.rand(5, in_size)
out = my_layer(inp)
out.sum().backward()

print(my_layer.M.grad)
</code></pre>
<p>Hope this helps !</p>