One of the variables needed for gradient computation has been modified by an inplace operation (backward pass is performed twice)

Srushti_Patil: <p>I am running the code snippet</p>
<pre><code class="lang-auto"># Define torch NN module


class Net(Module):
    def __init__(self, qnn):
        super().__init__()
        self.conv1 = Conv2d(1, 2, kernel_size=5)
        self.conv2 = Conv2d(2, 16, kernel_size=5)
        self.dropout = Dropout2d()
        self.fc1 = Linear(256, 64)
        self.fc2 = Linear(64, 2)  # 2-dimensional input to QNN
        self.qnn = TorchConnector(qnn)  # Apply torch connector, weights chosen
        # uniformly at random from interval [-1,1].
        #self.fc3 = Linear(1, 1)  # 1-dimensional output from QNN

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2)
        x = self.dropout(x)
        x = x.view(x.shape[0], -1)
        x = F.relu(self.fc1(x))
        x = self.fc2(x).clone()
        x = self.qnn(x)  # apply QNN
        #x = self.fc3(x)
        return cat((x, 1 - x), -1)


model4 = Net(qnn4)

# Define model, optimizer, and loss function
optimizer = optim.Adam(list(model4.parameters())[:-1], lr=0.001)


minimizer_args = dict(method='COBYLA', options={'disp':True, 'maxiter':5}, jac = False)
optimizer_qnn = MinimizeWrapper(list(model4.parameters()), minimizer_args)

loss_fun = CrossEntropyLoss()
# Start training
epochs = 10  # Set number of epochs
loss_list = []  # Store loss history
model4.train()  # Set model to training mode

for epoch in range(epochs):
    total_loss = []
    print('qnn params', list(model4.qnn.parameters()))
    with torch.autograd.set_detect_anomaly(True): 
        for batch_idx, (data, target) in enumerate(train_loader):

            output = model4(data) 
            loss = loss_fun(output, target)
            
            
            
            
            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()
        
            total_loss.append(loss.item()) 
        def closure():
            optimizer_qnn.zero_grad()
            loss = loss_fun(output, target)  
            loss.backward()
            print(loss.item()) 
            return loss 
        optimizer_qnn.step(closure)   
    print('qnn params after', list(model4.qnn.parameters())) 
    loss_list.append(sum(total_loss) / len(total_loss))
    print("Training [{:.0f}%]\tLoss: {:.4f}".format(100.0 * (epoch + 1) / epochs, loss_list[-1]))
</code></pre>
<p>and I ran the anomaly detection and am getting the in-place operation error at <code>x = self.fc2(x).clone()</code> this line. I added <code>clone()</code> to see if the error is rectified, but nothing changed. Can somebody help me?</p>
<p>I am also posting the traceback below:</p>
<pre><code class="lang-auto">
C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\autograd\__init__.py:200: UserWarning: Error detected in AddmmBackward0. Traceback of forward call that caused the error:
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel_launcher.py", line 17, in &lt;module&gt;
    app.launch_new_instance()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\traitlets\config\application.py", line 1043, in launch_instance
    app.start()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\kernelapp.py", line 725, in start
    self.io_loop.start()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\tornado\platform\asyncio.py", line 195, in start
    self.asyncio_loop.run_forever()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 1859, in _run_once
    handle._run()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\kernelbase.py", line 513, in dispatch_queue
    await self.process_one()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\kernelbase.py", line 502, in process_one
    await dispatch(*args)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\kernelbase.py", line 409, in dispatch_shell
    await result
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\kernelbase.py", line 729, in execute_request
    reply_content = await reply_content
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\ipkernel.py", line 422, in do_execute
    res = shell.run_cell(
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ipykernel\zmqshell.py", line 540, in run_cell
    return super().run_cell(*args, **kwargs)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\interactiveshell.py", line 3009, in run_cell
    result = self._run_cell(
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\interactiveshell.py", line 3064, in _run_cell
    result = runner(coro)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
    coro.send(None)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\interactiveshell.py", line 3269, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\interactiveshell.py", line 3448, in run_ast_nodes
    if await self.run_code(code, result, async_=asy):
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\IPython\core\interactiveshell.py", line 3508, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "C:\Users\Srushti\AppData\Local\Temp\ipykernel_68744\2922959437.py", line 20, in &lt;module&gt;
    output = model4(data)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Srushti\AppData\Local\Temp\ipykernel_68744\1791620977.py", line 24, in forward
    x = self.fc2(x).clone()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\nn\modules\linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
 (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\autograd\python_anomaly_mode.cpp:119.)
</code></pre>

KFrank: <p>Hi Srushti!</p>
<aside class="quote no-group" data-username="Srushti_Patil" data-post="1" data-topic="187533" data-full="true">
<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/srushti_patil/48/63631_2.png" class="avatar"> Srushti_Patil:</div>
<blockquote>
<pre><code class="lang-auto">    with torch.autograd.set_detect_anomaly(True): 
        for batch_idx, (data, target) in enumerate(train_loader):

            output = model4(data) 
            loss = loss_fun(output, target)
            
            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()
        
            total_loss.append(loss.item()) 
        def closure():
            optimizer_qnn.zero_grad()
            loss = loss_fun(output, target)  
            loss.backward()
            print(loss.item()) 
            return loss 
        optimizer_qnn.step(closure)   
</code></pre>
</blockquote>
</aside>
<p>Your immediate problem is likely here.  You are calling <code>loss.backward()</code><br>
with <code>retain_graph = True</code>.  First, you should think carefully about whether<br>
you need <code>retain_graph = True</code>, and if so, why.</p>
<p>In the last iteration of your <code>enumerate (train_loader)</code> loop, you build a<br>
computation graph that connects <code>output</code> to the parameters of <code>model4</code>.  This<br>
graph is preserved.  In general, <code>optimizer.step()</code> modifies the parameters<br>
of your model inplace.  When <code>closure()</code> is then executed, it calls <code>backward()</code><br>
on <code>loss_fun (output, target)</code>, which will backpropagate through the graph<br>
that connects <code>output</code> to the parameters of <code>model4</code>.  But those parameters<br>
have been modified inplace, causing the error.</p>
<aside class="quote no-group">
<blockquote>
<pre><code class="lang-auto">  File "C:\Users\Srushti\AppData\Local\Temp\ipykernel_68744\1791620977.py", line 24, in forward
    x = self.fc2(x).clone()
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Srushti\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\torch\nn\modules\linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
 (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\autograd\python_anomaly_mode.cpp:119.)
</code></pre>
</blockquote>
</aside>
<p>The forward-call traceback generated by <code>set_detect_anomaly (True)</code> is<br>
complaining about the call to <code>fc2 (x)</code> in your <code>model4</code>.  This agrees with the<br>
above analysis in that that <code>fc2.weight</code> has been modified inplace by the call<br>
to <code>optimizer.step()</code>.</p>
<p>To fix this, you will need to think through the logic of your use case.  Does<br>
<code>optimizer_qnn.step (closure)</code> need gradients of <code>output</code> with respect<br>
to the “regular” parameters of <code>model4</code>, such as <code>fc2</code>?  If so, would it be practical<br>
to rebuild the <code>model4</code>–<code>output</code> graph after calling <code>optimizer.step()</code>, perhaps<br>
inside of <code>closure()</code>?</p>
<p>For some examples that show how to debug and fix inplace-modification errors,<br>
see this post:</p>
<aside class="quote quote-modified" data-post="7" data-topic="171826">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-64-1-which-is-output-0-of-asstridedbackward0-is-at-version-3-expected-version-2-instead-hint-the-backtrace-further-a/171826/7">"RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64, 1]], which is output 0 of AsStridedBackward0, is at version 3; expected version 2 instead. Hint: the backtrace further a</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>
    Hi Fahmyadan and Sangyoon! 

Here are some suggestions about how to track down (and maybe fix) 
inplace-modification errors.  Note that an inplace modification in the forward 
pass is not necessarily* an error – it depends on whether and how the tensor 
that was modified is used in the backward pass.  Note that inplace operations 
can be useful for saving memory – if you replace an innocent inplace operation 
with an out-of-place equivalent, your training will use more memory (and, to a 
minor e…
  </blockquote>
</aside>

<p>Good luck!</p>
<p>K. Frank</p>