detach() on a tensor will, as the name suggests, detach the tensor from the computation graph, so that the gradients will be stopped at this point. All operations performed before the detach call will not get any gradients from operations performed on this tensor.
If you are detaching manually somewhere in the model, this might explain, why the output doesn’t require gradients.
The increase in memory is expected, as the backward pass needs intermediate tensors to properly calculate the gradients.
If you are running out of memory, try to lower the batch size or use torch.utils.checkpoint to trade compute for memory.