They should be the same. To debug, just take an example small enough to calculate the gradients by hand and compare against what you are seeing.
For instance, y=wx, set your w=1, and loss y^2/2. Then your gradient is the same as x. Here’s an example of computing two gradient for two different x’s and checking that they match manually computed gradient – Google Colab 1
You can use this to debug your Hessian trace as well, trace is especially easy when everything is a scalar