I have been checking the previous implementation and the problem was that some layers had their training mode set to True. So it might be what you expose, that the  set_grad_enabled(True)  3 was set internally. That was one of the things I did not know, whether torch.no_grad() had maximum priority or not