Gradient computation has been modified by an Inplace. operation. Warning: Error detected in GatherBackward

Hi. I am getting the following error when running the code:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.LongTensor [14, 1, 768]] is at version 2; expected version 1 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!

Here is the stack trace:

File “/content/drive/MyDrive/NLP projects/Emotion-cause-pair/temp-reccon/end2end/e2e_model.py”, line 351, in train
end_positions=end_positions,
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “/content/drive/MyDrive/NLP projects/Emotion-cause-pair/temp-reccon/end2end/e2e_model.py”, line 111, in forward
end_logits = self.end(out, start_positions=start_positions, p_mask=p_mask)
File “/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py”, line 722, in _call_impl
result = self.forward(*input, **kwargs)
File “/content/drive/MyDrive/NLP projects/Emotion-cause-pair/temp-reccon/end2end/beam.py”, line 69, in forward
start_states = hidden_states.gather(-2, st_pos) # shape (bs, 1, hs)

I have narrowed down to the portion of code that is giving this error, but I am not sure what needs to be changed to correct this. Here is the code snippet responsible for this error:

slen, hs = hidden_states.shape[-2:]
st_pos = start_positions[:, None, None].expand(-1, -1, hs)
start_states = hidden_states.gather(-2, st_pos)
start_states = start_states.expand(-1, slen, -1)

Can someone please help me overcome this error. Thanks.