I am trying to calculate the gradient (d(loss)/dj). But I get grad is None
class model(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(256, 2)
        
    def forward(self, j, labels):
        e = self.fc(j)
        print(labels.shape)
        print(e.shape)

        j.requires_grad = True
        
        with torch.enable_grad():
            loss = F.cross_entropy(e, labels, reduction='sum')
            j.retain_grad()
            loss.backward()
            grad = j.grad.detach()
            
        return grad