The solution is very straightforward. Just fill zeros into the desired slots of the gradient before updating your weights.
a = Variable(torch.randn(3,4), requires_grad=True)
b = torch.mean(a ** 2)

b.backward()

# fill zeros into the first row of grad
b.grad.data[0,:].fill_(0)

# the first row does not change due to zero grad
optimizer.step()