In python, you can use c.retain_grad() for this to be populated for non-leafs.
Not sure if it is available on the cpp API yet? cc @yf225
Otherwise, you can use autograd::grad and provide the inputs you want gradient for, where you can specify any Tensor that requires gradients (no need for them to be leafs).