Hi, I have some problems of getting the gradient when I used the kl_div function.
I tried two versions of the kl_div function, one is the official version: F.kl_div, and the other is implemented by myself. The results of the official kl_div and my kl_div are same but the gradient of the F.kl_div is always 0.
import torch
import torch.nn as nn
import torch.nn.functional as F


def kl_div(x, y):
    _kl = torch.sum(y * (torch.log(y) - x), -1)
    return torch.mean(_kl)


logits = torch.Tensor([[1, 2, 3, 4]])

logits.requires_grad = True

label = torch.Tensor([[0.1, 0.1, 0.7, 0.1]])
label2 = torch.Tensor([[0.1, 0.1, 0.7, 0.1]])
label.requires_grad = True
y_qry = torch.LongTensor([1])

logsoftlogits = F.log_softmax(logits, dim=-1)

case = 0
if case == 0:
    loss = kl_div(logsoftlogits, label)
    #print(loss)
else:
    loss = F.kl_div(logsoftlogits, label, reduction='sum') / logsoftlogits.size()[0]
    #print(loss)
grad = torch.autograd.grad(outputs=loss, inputs=logits, create_graph=True)
logits_q = logits - grad[0]

loss_q = F.cross_entropy(logits_q, y_qry)

labelgrad = torch.autograd.grad(outputs=loss_q, inputs=label, grad_outputs=torch.ones(loss_q.size()))
print(labelgrad)

the official kl_div result:

(tensor([[0., 0., 0., 0.]]),)

my implementation reusult:

(tensor([[-0.2615, -1.1997,  0.1304,  0.1274]]),)