hmm, wouldn’t it be something like:
for first_grad in first_grads:
  for out in outs:
     hessian_terms.append(...)

If you did:
for first_grad1 in first_grads:
  for first_grad2 in first_grads:

You didn’t use the dloss_out1 to compute dloss_out2, so it wouldn’t be part of your graph and you would probably error there.