Like @SimonW said if no mismatch occurs, the intermediate values will be used so the gradients w.r.t. W1 will be applied on W2.
I created a small example. Is this, what you would like to do?
x = Variable(torch.ones(1, 4))
w = Variable(torch.ones(4, 1), requires_grad=True)
y = Variable(torch.ones(1, 1))

output = torch.matmul(x, w)

criterion = nn.MSELoss()
optimizer = optim.SGD([w], lr=0.1)

loss = criterion(output, y)
#w.data.fill_(2) # comment / uncomment
loss.backward()

print('w {}grad {}'.format(w.data, w.grad.data))

optimizer.step()
print('w {}grad {}'.format(w.data, w.grad.data))
optimizer.zero_grad()