Nice spot! That said, when producing the grads using .detach() or .clone(), the Variables (e.g. k[0]) are volatile, despite the losses (g and kl) being non-volatile, so it seems like something needs to be done with the grads.
Sticking trust_loss.volatile = False at the end produces RuntimeError: there are no graph nodes that require computing gradients, which seems like further evidence that something needs to be done earlier on in the function.