I believe the issue is that the backward pass uses additional memory by computing the gradients.
Detaching the tensor would lower the memory usage, but would also break the training, so no you should not detach the output/loss, if you want to train the model.