I’m not sure why you are seeing this error. I tried to reproduce it using your code snippet (and removing some steps) which works fine:
D = nn.Linear(1, 1)
G = nn.Linear(1, 1)

optim_D = torch.optim.SGD(D.parameters(), lr=1e-3)
optim_G = torch.optim.SGD(G.parameters(), lr=1e-3)


real_img = torch.randn(1, 1)
latents = torch.randn(1, 1)
# =======================================================================================================
#   (1) Update D network: D_logistic_r1(default)
# =======================================================================================================
# Compute adversarial loss toward discriminator
real_logit = D(real_img)
fake_img = G(latents)
fake_logit = D(fake_img.detach())

d_loss = fake_logit.mean()
d_loss = d_loss + real_logit.mean()

# Update discriminator
optim_D.zero_grad()
d_loss.backward()
optim_D.step()

# =======================================================================================================
#   (2) Update G network: G_logistic_ns_pathreg(default)
# =======================================================================================================
G.zero_grad()
fake_scores_out = D(fake_img)
g_loss = fake_scores_out.mean()

# Update generator
# Note: g_loss.backward(retain_graph=True) is ok, but this version is not I want!
g_loss.backward()
optim_G.step()

That being said, the general training routing works fine.
However, the penalty calculation might still produce this error (which isn’t defined unfortunately, so I had to remove it for my code snippet).