Hi,
Using with torch.no_grad() disables gradient calculation. So, the reason why it uses less memory is that it’s not storing any Tensors that are needed to calculate gradients of your loss. Also, because you don’t store anything for the backward pass, the evaluation of your network is quicker (and use less memory).
You can read more about it in the docs! no_grad — PyTorch 1.9.0 documentation 1