Glad you found the source of the problem, it's great to have unit tests that check the correctness of the gradient!