If I’m not mistaken, the target tensor would have to be one-hot encoded, not the prediction tensor, which seems to break the computation graph.
To do so, you could use F.one_hot. Also, this post 4 provides an implementation in case you get stuck.
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.