if you are doing this for efficiency reasons, then dont. the way the autograd works, it will end up computing the full gradient for x anyways. Instead, get gradient wrt x, and narrow down to the first two elements.
Coming to the specific error, I’ll get back to you on this tomorrow.