I’m not sure if the code treats gr_a and gr_b right, since it seems they are constants for the backward pass.
At least playing around with the code, I saw the same error as @ElleryL explained, which indicates these tensors are not necessary for the gradient computation.
I didn’t understand all code samples, so sorry, if this misses the point, but is this basically, what you are trying to do (@ElleryL):
x = torch.tensor([1.], requires_grad=False)
w = torch.tensor([2.], requires_grad=True)
y = torch.tensor([1.])

for epoch in range(10):
    w.grad = None
    loss_w = ((x*w - y)**2).mean()
    print('Loss w: ', loss_w)
    loss_w.backward()
    print('Grad w: ', w.grad)
    
    w_grad = w.grad.clone()
    w_grad.requires_grad_(True)
    
    loss_w_grad = (w_grad**2).mean()
    loss_w_grad.backward()
    print('Loss w_grad: ', loss_w_grad)
    print('Grad w_grad: ', w_grad.grad)
    
    # update with w.grad.grad
    w.data.sub_(0.1 * (w.grad.data - w_grad.grad.data*0.1))
    print('W: ', w.data)