Error in loss.backward() When Using Python API in C++ Frontend

tttsss-01: <p>I’m trying to train my model using the C++ frontend API. In the process of handling the training dataset, I used the Python API to invoke my Python script for data preprocessing and loading. However, when I reach the <code>loss.backward();</code>, an error occurs. The error message is as follows:</p>
<pre data-code-wrap="shell"><code class="lang-plaintext">terminate called after throwing an instance of 'c10::Error'
  what():  The autograd engine was called while holding the GIL. If you are using the C++ API, the autograd engine is an expensive operation that does not require the GIL to be held so you should release it with 'pybind11::gil_scoped_release no_gil;'. If you are not using the C++ API, please report a bug to the pytorch team.
Exception raised from execute at ../torch/csrc/autograd/python_engine.cpp:131 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x57 (0x7ffff7f4b4d7 in /home/stone/libtorch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 0x68 (0x7ffff7f15434 in /home/stone/libtorch/lib/libc10.so)
frame #2: torch::autograd::python::PythonEngine::execute(std::vector&lt;torch::autograd::Edge, std::allocator&lt;torch::autograd::Edge&gt; &gt; const&amp;, std::vector&lt;at::Tensor, std::allocator&lt;at::Tensor&gt; &gt; const&amp;, bool, bool, bool, std::vector&lt;torch::autograd::Edge, std::allocator&lt;torch::autograd::Edge&gt; &gt; const&amp;) + 0x8f (0x7fff4dbdb89f in /home/stone/.local/lib/python3.10/site-packages/torch/lib/libtorch_python.so)
frame #3: &lt;unknown function&gt; + 0x4abdbc1 (0x7fffe2e7abc1 in /home/stone/libtorch/lib/libtorch_cpu.so)
frame #4: torch::autograd::backward(std::vector&lt;at::Tensor, std::allocator&lt;at::Tensor&gt; &gt; const&amp;, std::vector&lt;at::Tensor, std::allocator&lt;at::Tensor&gt; &gt; const&amp;, c10::optional&lt;bool&gt;, bool, std::vector&lt;at::Tensor, std::allocator&lt;at::Tensor&gt; &gt; const&amp;) + 0x5c (0x7fffe2e7ca1c in /home/stone/libtorch/lib/libtorch_cpu.so)
frame #5: &lt;unknown function&gt; + 0x4b1429e (0x7fffe2ed129e in /home/stone/libtorch/lib/libtorch_cpu.so)
frame #6: at::Tensor::_backward(c10::ArrayRef&lt;at::Tensor&gt;, c10::optional&lt;at::Tensor&gt; const&amp;, c10::optional&lt;bool&gt;, bool) const + 0x48 (0x7fffdff59a98 in /home/stone/libtorch/lib/libtorch_cpu.so)
frame #7: at::Tensor::backward(at::Tensor const&amp;, c10::optional&lt;bool&gt;, bool, c10::optional&lt;c10::ArrayRef&lt;at::Tensor&gt; &gt;) const + 0x144 (0x5555555e42ac in /home/stone/projects/gru/build/train)
frame #8: main + 0x8e9 (0x5555555e1c47 in /home/stone/projects/gru/build/train)
frame #9: &lt;unknown function&gt; + 0x29d90 (0x7fff895d7d90 in /lib/x86_64-linux-gnu/libc.so.6)
frame #10: __libc_start_main + 0x80 (0x7fff895d7e40 in /lib/x86_64-linux-gnu/libc.so.6)
frame #11: _start + 0x25 (0x5555555e1265 in /home/stone/projects/gru/build/train)
</code></pre>
<p>I don’t get this error if I don’t use the Python API.<br>
According to the error message, I tried using <code>pybind11::gil_scoped_release no_gil;</code>, but I received the following error: <code>namespace 'pybind11' has no member 'gil_scoped_release'.</code></p>

ptrblck: <p>Maybe includes are missing? Did you try to use:</p>
<pre><code class="lang-auto">#include &lt;pybind11/pybind11.h&gt;
#include &lt;pybind11/functional.h&gt;
#include &lt;torch/csrc/utils/pybind.h&gt;
</code></pre>

tttsss-01: <p>After adding includes, I got new error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/f/7f0d9840a68a757a7ea954018a71e6468f57cb5c.png" data-download-href="https://discuss.pytorch.org/uploads/default/7f0d9840a68a757a7ea954018a71e6468f57cb5c" title="3"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/f/7f0d9840a68a757a7ea954018a71e6468f57cb5c.png" alt="3" data-base62-sha1="i7XLrWUnCf0vmsTF5yZYtYLqZze" width="690" height="397" data-dominant-color="272423"><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">3</span><span class="informations">934×538 29.6 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>

tttsss-01: <aside class="quote no-group quote-modified" data-username="tttsss-01" data-post="3" data-topic="182801">
<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/tttsss-01/48/61591_2.png" class="avatar"> tttsss-01:</div>
<blockquote>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/f/7f0d9840a68a757a7ea954018a71e6468f57cb5c.png" data-download-href="https://discuss.pytorch.org/uploads/default/7f0d9840a68a757a7ea954018a71e6468f57cb5c" title="3"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/f/7f0d9840a68a757a7ea954018a71e6468f57cb5c.png" alt="3" data-base62-sha1="i7XLrWUnCf0vmsTF5yZYtYLqZze" width="690" height="397" data-dominant-color="272423"><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">3</span><span class="informations">934×538 29.6 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>
<p>3934×538 29.6 KB</p>
</blockquote>
</aside>
<p>I got another error.</p>

ptrblck: <p>You would need to debug the segfault via <code>gdb</code> to narrow down where this memory access violation comes from.</p>

TiStar: <p>Any updates on this? I’m facing the same error.<br>
Thanks in advance.</p>