The first code snippet runs fine without retain_graph=True using your code as the base:
model = nn.Linear(1, 1)
data = torch.randn(10, 1)
target = torch.zeros(10).long()
output = model(data)

loss = 0.
for out, tar in zip(output, target):
    loss += F.nll_loss(out.unsqueeze(0), tar.unsqueeze(0))
loss.backward()
print(model.weight.grad)