The code is difficult to understand with abbreviated var names and commented code. But in general, you should use Variable(tensor, volatile=True) when testing. Upon seeing volatile inputs, backward graph won’t be constructed. So it will save a lot of memory. Also, it is worth noting that torch.cuda.empty_cache() is never solution to OOM. It doesn’t increase the amount of free memory you can use.