Hi,
This is because you modify the input of your Function inplace here and since it’s a Tensor that you created and that requires gradients (we call these leaf Tensors), this is not allowed.
You can add at the beginning of your function raw = raw.clone() to solve that problem.
Also, since you set to 0 all the values that should be to 0 at the end of your function, I think it will be more efficient to apply a full softmax instead of the for loop.