That’s quite strange, as the second approach is the correct one.
It’s usually not recommended to call the forward method directly, as it would skip all registered hooks.
However, could it be the case that you are using forward hooks, which might somehow delete the gradients?
If not, could you post the model definition, so that we could debug the issue?