This records all _prob tensor “versions”, the framework doesn’t know that writes are non-overlapping and intermediate states are discardable. Then you have a long chain of autograd nodes to be processed during backward().
Workaround is to use torch.gather to set all values at once. Another option is to rewrite this with torch.where & torch.cat, similarly avoiding assignments to a buffer.