If you are calling detach() on the output of F.log_softmax, the computation graph will be cut at this place, so that Autograd won’t be able to calculate the gradients for the former part of the graph.
You might need to reduce num_batch, if you are running out of memory.