RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.LongTensor [2750]] is at version 2; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compu

Hi everyone. I keep getting this runtime error every time I run the code about inplace operation. However, the operation is slicing a tensor in this case and since the tensor adj is not leaf slicing should not affect backprop but it does. I cant figure out why this happens.

If anyone can help me that would be great
Thank you

def convert_adj_to_coo_batch(new_node, new_edge):
#     batch = Batch(x=new_node, adj=new_edge)
    adj = new_edge.clone()
    offset, row, col = (adj > 0).nonzero().t()
    **edge_weight = adj[offset, row, col]**
    row += offset * new_node.shape[1]
    col += offset * new_node.shape[1]
    edge_index = torch.stack([row, col], dim=0)
    x = new_node.view(new_node.shape[0] * new_node.shape[1], new_node.shape[2])
    return x, edge_weight, edge_index

The error is in edge_weight. The code where I do an inplace operation however is here

def introduce_interventions(input_gnn):
    index = torch.randint(1, input_gnn.shape[1], (1,))
    input_gnn[:, index , :] = 0
    input_gnn_cl = input_gnn.clone()
    return input_gnn_cl

This function is called before the erroneous function.

Hi AD!


    row = row + offset * new_node.shape[1]
    col = col + offset * new_node.shape[1]

+= is also an inplace operation for pytorch tensors.

(For pytorch tensors, t += u and t = t + u are not synonymous.)


K. Frank

Thank you so much @KFrank. It worked perfectly!