I’m not particularly sure of the math here. But shouldn’t the following get you the required matrix?  You’re trying to compute gradient(y, x) as per docs 52. Doing y.backward() will store gradient(w, x) as required by gradient descent. The problem if you try that directly is that it throws an error that it can’t be computed implicitly. As a hack, you can do  y[i].backward(retain_graph=True) and accumulate the required gradient in w1.grad over each function.
>>> x = np.arange(1,3,1)
>>> x = torch.from_numpy(x).reshape(len(x),1)
>>> x = x.float()
>>> x.requires_grad = True
>>> 
>>> w1 = torch.randn((2,2), requires_grad = True)
>>> y = w1@x
>>> 
>>> # Compute gradients of w1 . x
... 
>>> y[0].backward(retain_graph=True)
>>> w1.grad
tensor([[1., 2.],
        [0., 0.]])
>>> y[1].backward(retain_graph=True)
>>> w1.grad
tensor([[1., 2.],
        [1., 2.]])