Sorry for late response, i’ve made a script which is able to reproduce this error.
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F


from torch.autograd import Variable
from torch.autograd import grad


torch.manual_seed(42)
num_classes = 26

class UnaryNet(nn.Module):
    def __init__(self):
        super(UnaryNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, 5)
        self.conv2 = nn.Conv2d(10, 20, 5)

        self.fc1_mean = nn.Linear(5 * 5 * 20, 140)
        self.fc2_mean = nn.Linear(140, num_classes)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, 5 * 5 * 20)
        
        mean = F.relu(self.fc1_mean(x))
        mean = self.fc2_mean(mean)

        return mean

net = UnaryNet()

phi = Variable(torch.Tensor([1.]), requires_grad=True)
theta = net.parameters()

input, target = Variable(torch.randn(5,1,32,32)), Variable(torch.rand(5).long())
criterion = nn.CrossEntropyLoss()

loss1 = criterion(net(input), target) * phi
theta_grad = torch.autograd.grad([loss1], theta, create_graph=True)

loss2 = 0.
for grad in theta_grad: loss2 += grad.pow(2).sum()
phi_grad = torch.autograd.grad([loss2], [phi])