When I changed the code to the first section it worked fine.

This is quite surprising indeed. They should give the same result.
I would check first that this is not about the .eval() mode that is set properly. It has a large impact on the result for models with batchnorm layers.