Do you mean I could directly apply nn.Softmax() to get what I want after I multiply raw by one_hots?

From what I understand of your code yes.
The only point of the for loop compared to a full Softmax() is not to modify the values that you don’t want to compute the softmax for right? If so, you can actually change these values to anything given that you set them to 0 later.
Also using a regular softmax will remove the inplace assignment raw[i] = xxx and so you won’t need the clone() anymore.