Thank you for the code.
Could you also please provide some driver code which does the training, and using which you can observe the no-gradient thing?
The issue could be with the way that the training is driven, and without your code which does this part, it is not clear (at least, to me) how to debug this.