RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time. Help appreciated!

hjyxxxxxl: <p>I am trying to calculate the mutual information between the hidden layers’ output and input and output using the following code:</p>
<h1></h1>
<p>def InfoNCE(X, Y, batch_size=256, num_epochs=200, dev=torch.device(“cpu”), model=None, rg=True):<br>
A = torch.tensor([float(batch_size)] * batch_size).reshape(batch_size, 1)#.cuda()<br>
if not model:<br>
model = nn.Sequential(<br>
nn.Linear(X.shape[1]+Y.shape[1], 16),<br>
nn.ReLU(),<br>
nn.Linear(16, 8),<br>
nn.ReLU(),<br>
nn.Linear(8, 1),<br>
)</p>
<pre><code># Move data to device
X     = X.to(dev)
Y     = Y.to(dev) + torch.randn_like(Y) * 1e-4
model = model.to(dev)

opt   = optim.SGD(model.parameters(), lr=0.03, momentum=0.9)
td    = TensorDataset(X, Y)

result = []  
for epoch in range(num_epochs):            
    for x, y in DataLoader(td, batch_size, shuffle=True, drop_last=True):            
        opt.zero_grad()
        
        top    = model(torch.cat([x, y], 1)).flatten()
        xiyj   = torch.cat([x.repeat_interleave(batch_size,dim=0),y.repeat(batch_size,1)], 1)    
        bottom = torch.logsumexp(model(xiyj).reshape(batch_size,batch_size), 1) - A.log()
        
        loss   = -(top - bottom).mean()
        
        result.append(-loss.item())
        
        loss.backward(retain_graph=rg)
        opt.step()
        r = torch.tensor(result[-20:]).mean()
#plt.plot(result)
print(r)              
return r
</code></pre>
<p>InfoNCE(dataset.x, layer_2_log[1])</p>
<p>I tried setting retain_graph both true and false after reading some posts, it still gives the runtime error no matter what.</p>
<p>Any help would be appreciated!</p>

albanD: <p>Hi,</p>
<p>Which version of pytorch are you using? Some autograd fix have been added to master last week that might be related. Do you see the same behavior if you use nightly builds?<br>
If you still do, could you write a small script that I could run to reproduce the error?</p>

hjyxxxxxl: <p>Thank you so much for answering!</p>
<ol>
<li>3.6.9</li>
<li>How to check if I have nightly build? I am running in an environment that I created using conda.</li>
<li><a href="https://drive.google.com/file/d/1MvO3DYE_W0pJfcnBb7Yosfp1M2943lil/view?usp=sharing" rel="nofollow noopener">https://drive.google.com/file/d/1MvO3DYE_W0pJfcnBb7Yosfp1M2943lil/view?usp=sharing</a></li>
</ol>

albanD: <p>Hi, you can check your version by printing <code>torch.__version__</code> and report it here.</p>
<p>For your code sample, what are the size of the objects in the dataset? Could you replace the <code>p.load()</code>with just a big <code>torch.rand(your_size)</code> so that I can run it without your dataset?</p>

hjyxxxxxl: <ol>
<li>1.2.0</li>
<li>Sorry that I forgot to share the dataset. Here you go: <a href="https://drive.google.com/file/d/1lMHCbVqdPFI21hsy8Rqd_oB1nHVdEsn5/view?usp=sharing" rel="nofollow noopener">https://drive.google.com/file/d/1lMHCbVqdPFI21hsy8Rqd_oB1nHVdEsn5/view?usp=sharing</a>
</li>
</ol>

albanD: <ol>
<li>Can you try and install the pytorch preview build from <a href="https://pytorch.org/get-started/locally/" rel="nofollow noopener">this</a> page and check if it still happens?</li>
<li>thanks, I asked for random inputs because that way we don’t have to download dataset and move them around on our machines to reproduce <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"> and it’s much simpler for us.</li>
</ol>

hjyxxxxxl: <ol start="2">
<li>My bad. The system is not allowing me to reply with a share line cuz they think it is promotional. Can you comment line 10 and 11 and change line 13 and 14 to:</li>
</ol>
<p>x_train = torch.rand([5198,22])<span class="hashtag">#d</span>[‘mushrooms’][0][0:5198]<br>
y_train = torch.rand([5198,1])<span class="hashtag">#d</span>[‘mushrooms’][1][0:5198].view(5198,-1)</p>

hjyxxxxxl: <ol>
<li>I tried the preview. It still happens.</li>
</ol>

albanD: <p>Ho, looking at the code in a nice editor made me realise that you pass <code>layer_2_log</code> to your second training stage. But that Tensor has some history already from the first training stage. Is that expected that this will backward in the first part of the model as well? If no, you should add a <code>.detach()</code> (when saving it in the first training loop or when passing it to the InfoNCE function) not to keep it’s history.</p>

albanD: 

hjyxxxxxl: <p>I see what you are talking about! Thank you so much!</p>