about your original error message.
it is expected.
x[0, :2] is unreachable from the original graph. every time you do indexing on x you get a new Variable
with history independent of all other indexing ops.
One way to keep your use-case efficient, and make this work is:
first index x, and then compute the loss only on those two elements.
For example:
x_ = x[0, :2]
loss = 0.5*((x_ - y) **2).sum()
grad = torch.autograd.grad(loss, x_, create_graph=True)[0]