Error when updating policy networks: Trying to backward through the graph a second time

AVS_Origami: <p>Hello, I am using Pytorch to implement a transformer encoder based on GTrXL and train it with reinforcement learning. I am able to successfully complete a single forward and backward pass, but after the second forward pass when I go to backward again, I run into this error:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
Exception raised from unpack at ../torch/csrc/autograd/saved_variable.cpp:134 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) + 0xb0 (0x739c9cda57f0 in /opt/libtorch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 0x84 (0x739c9cd4e092 in /opt/libtorch/lib/libc10.so)
frame #2: torch::autograd::SavedVariable::unpack(std::shared_ptr&lt;torch::autograd::Node&gt;) const + 0x1392 (0x739c82691d92 in /opt/libtorch/lib/libtorch_cpu.so)
frame #3: torch::autograd::generated::MmBackward0::apply(std::vector&lt;at::Tensor, std::allocator&lt;at::Tensor&gt; &gt;&amp;&amp;) + 0xc5 (0x739c817ebb25 in /opt/libtorch/lib/libtorch_cpu.so)
frame #4: &lt;unknown function&gt; + 0x584d2eb (0x739c8264d2eb in /opt/libtorch/lib/libtorch_cpu.so)
frame #5: torch::autograd::Engine::evaluate_function(std::shared_ptr&lt;torch::autograd::GraphTask&gt;&amp;, torch::autograd::Node*, torch::autograd::InputBuffer&amp;, std::shared_ptr&lt;torch::autograd::ReadyQueue&gt; const&amp;) + 0x14c2 (0x739c826478c2 in /opt/libtorch/lib/libtorch_cpu.so)
frame #6: torch::autograd::Engine::thread_main(std::shared_ptr&lt;torch::autograd::GraphTask&gt; const&amp;) + 0x650 (0x739c82648510 in /opt/libtorch/lib/libtorch_cpu.so)
frame #7: torch::autograd::Engine::thread_init(int, std::shared_ptr&lt;torch::autograd::ReadyQueue&gt; const&amp;, bool) + 0x194 (0x739c82642134 in /opt/libtorch/lib/libtorch_cpu.so)
frame #8: &lt;unknown function&gt; + 0xe1c34 (0x739c1f0e1c34 in /usr/lib/libstdc++.so.6)
frame #9: &lt;unknown function&gt; + 0x9439d (0x739c9a78639d in /usr/lib/libc.so.6)
frame #10: &lt;unknown function&gt; + 0x11949c (0x739c9a80b49c in /usr/lib/libc.so.6)
</code></pre>
<p>And here is the code for the backward pass:</p>
<pre><code class="lang-auto">void Agent::update_policy(float tau, float gamma) {
    // Sample a batch of experiences from memory.
    auto [states, actions, rewards, next_states, terms] = this-&gt;mem.sample(32);
    
    // Determine the target q-values.
    torch::Tensor next_actions = std::get&lt;0&gt;(this-&gt;proto(next_states));
    torch::Tensor next_q_vals = this-&gt;critic_tgt.forward(next_states, next_actions);
    torch::Tensor target_q_vals = rewards.unsqueeze(-1) + gamma * (1.0 - terms) * next_q_vals;
    
    // Update critic networks.
    this-&gt;critic.zero_grad();
    torch::Tensor q_vals = this-&gt;critic.forward(states, actions);
    torch::Tensor value_loss = torch::nn::functional::mse_loss(q_vals, target_q_vals);

    // The GTrXL encoder is shared between two linear heads (one for the actor
    // and one for the critic). So, we backward the loss and step the optimizers
    // for both the critic and the GTrXL encoder.
    value_loss.backward();
    this-&gt;opt_critic.step();
    this-&gt;opt_tfxl.step();

    // Update actor networks.
    this-&gt;actor.zero_grad();
    auto [_, _, policy_loss] = this-&gt;forward(states);
    policy_loss = -policy_loss.mean();

    // Just like before, since the GTrXL has two heads, we backward the loss,
    // then step the optimizers for both the actor and the GTrXL encoder. This
    // way, the GTrXL encoder has been updated with the gradients from both the
    // actor and critic networks and will eventually be able to provide outputs
    // that are optimal for either head.
    policy_loss.backward();
    this-&gt;opt_actor.step();
    this-&gt;opt_tfxl.step();

    // Update target networks.
    track(this-&gt;actor_tgt, this-&gt;actor, tau);
    track(this-&gt;critic_tgt, this-&gt;critic, tau);
}
</code></pre>
<p>Based on my research into this error, it looks like this usually occurs because some tensor is getting stored and used for the backward pass again. So, my question is this:</p>
<ol>
<li>If the error is here in the <code>update_policy</code> function, what is causing it?</li>
<li>If the error is somewhere else, how can I track down the tensor that is causing problems?</li>
</ol>
<p>I know that the error message suggests setting <code>retain_graph=true</code>. However, I have looked up this behavior and confirmed that I do not want to do this.</p>

AVS_Origami: <p>Bumping this topic. If anyone has suggestions that would be much appreciated.</p>

soulitzer: <blockquote>
<p>it looks like this usually occurs because some tensor is getting stored and used for the backward pass again</p>
</blockquote>
<p>Yes, if you are backwarding multiple times over the same graph, and use the same</p>
<blockquote>
<p>However, I have looked up this behavior and confirmed that I do not want to do this.</p>
</blockquote>
<p>It sounds like want to make sure that the two backwards operate on two disjoint graphs?</p>
<blockquote>
<p>How can I track down the tensor that is causing problems?</p>
</blockquote>
<p>You can use TORCH_LOGS=“+autograd” to log the backward pass, or enable<br>
<a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.detect_anomaly" class="inline-onebox" rel="noopener nofollow ugc">Automatic differentiation package - torch.autograd — PyTorch 2.5 documentation</a>, which can give you a stack trace of where in the forward the error from backward corresponds to.</p>