*** RuntimeError: a leaf Variable that requires grad has been used in an in-place operation

I defined a type of loss and encounter *** RuntimeError: a leaf Variable that requires grad has been used in an in-place operation. The loss is defined as follows

def SMLoss(feature):
    co=feature.mm(feature.t())
    l=co.diag().resize(batch_size,1).sqrt()
   S=co.div(l.mm(l.t()))+1
   W=S.resize(le,1)
   P=S.div(S.sum(1).mm(torch.autograd.Variable(torch.ones(1,batch_size)).cuda()))
    VP=P.resize(le,1)
    VPT=torch.cat(P.t().unsqueeze(2),0)
   tmp=VP.mm(VPT.t())
    smloss=torch.autograd.Variable(torch.zeros(le,le).cuda(),requires_grad=True)
   for u in range(0,le):
        for v in range(0,le):
              smloss[u,v]=tmp[u,v]*((W[p[u,v]]-W[q[u,v]])**2)
    return smloss.sum()

in which p and q are index-maps pre-defined.
Can someone give me a help to solve the problem, thx a lot