one of the variables needed for gradient computation has been modified by an inplace operation : can't find inplace operation

I have this code below and I can't find the inplace operation that prevents the gradient from computing.

for epoch in range(nepoch):
    model.train()
    scheduler.step()

    for batch1 in loader1:
        torch.ones(len(batch1[0]), dtype=torch.float)
        x, label = batch1
        x = x1.to('cuda', non_blocking=True)
        optimizer.zero_grad()
        pred = model(x)
        pred = pred.squeeze() if pred.ndimension() &gt; 1 else pred
        label = (label.float()).cuda(cuda0)
        weights = torch.ones(len(label))
        loss_fun = torch.nn.BCEWithLogitsLoss(weight=weights.cuda(cuda0))
        score = loss_fun(pred, label)
        label = np.array(np.round(label.cpu().detach())).astype(bool)
        pred = np.array(pred.cpu().detach()&gt;0).astype(bool)
        torch.autograd.set_detect_anomaly(True)

        score.backward()
        optimizer.step()

At the end I have this error that pops up:
Warning: Error detected in MulBackward0. Traceback of forward call        that caused the error:
File &quot;train.py&quot;, line 98, in &lt;module&gt;
  pred = model(x)
File &quot;/home/anatole2/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 550, in __call__
  result = self.forward(*input, **kwargs)
File &quot;/home/anatole2/miniconda3/lib/python3.7/site-packages/torch/nn/modules/container.py&quot;, line 100, in forward
  input = module(input)
File &quot;/home/anatole2/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 550, in __call__
  result = self.forward(*input, **kwargs)
File &quot;/home/anatole2/best/PCEN_pytorch.py&quot;, line 30, in forward
  filtered[i] = filtered[i] + (1-exp(self.log_s)) * filtered[i-1]
(print_stack at /pytorch/torch/csrc/autograd/python_anomaly_mode.cpp:60)
Traceback (most recent call last):
File &quot;train.py&quot;, line 116, in &lt;module&gt;
  score.backward()
File &quot;/home/anatole2/miniconda3/lib/python3.7/site-packages/torch/tensor.py&quot;, line 198, in backward
  torch.autograd.backward(self, gradient, retain_graph, create_graph)
File &quot;/home/anatole2/miniconda3/lib/python3.7/site-packages/torch/autograd/__init__.py&quot;, line 100, in backward
  allow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [16, 1, 80]], which is output 0 of SelectBackward, is at version 378; expected version 377 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

If you could help me that'd be great !

The in-place operation seems to be on this line:
File &quot;/home/anatole2/best/PCEN_pytorch.py&quot;, line 30, in forward
  filtered[i] = filtered[i] + (1-exp(self.log_s)) * filtered[i-1]

Note that it is using the value from filtered[i] and then storing the result in filtered[i].  This is what in-place means; the new value overwrites the old one.
To fix it, you'd need to do something like this:
filtered_new = torch.zeros_like(filtered)
...
filtered_new[i] = filtered[i] + (1-exp(self.log_s)) * filtered[i-1]

The part that makes this a bit complicated is that this seems to be inside a loop (I assume i is the loop counter) and it probably uses the values from the previous pass through the loop.  The modified version is not in-place, but probably won't produce the same results as the original either.  So you may have to do something like this:
filtered_new[i] = filtered[i] + (1-exp(self.log_s)) * filtered_new[i-1]

It's impossible to solve this without seeing more code around this, but basically - look around, and replace any operation which changes existing tensors with an operation which creates new tensors to store the results of the calculation.
