It seems your code tries to train the model using some trainer class, which isn’t defined in your code snippet.
If you freeze all parameters or disable the gradient calculation via torch.set_grad_enabled(False), you won’t be able to calculate the gradients with a backward call.
Could you recheck your code and see, where the trainer comes from?