You can avoid most of these problems by wrapping it in an object that subclasses nn.Module.  There’s a reason most neural networks subclass nn.Module.  Try this version
import torch
from torch import nn
class foo(nn.Module):
    def __init__(self):
        super(foo,self).__init__()
        self.x = nn.Parameter(torch.ones(2,10)) #requires_grad automatically True
        self.z = nn.Parameter(torch.ones(1,10))
bar=foo().cuda()
y = (4 * bar.x * bar.x + 10 * bar.z).mean()
y.backward()
print('x  grad :', bar.x.grad)
print('z  grad:', bar.z.grad)
print('y  grad:', y.grad)