In the first part you create a tensor of the log_probs. I think at this point you create a tensor with empty history since your returns have no history as well. In the second part you have (probably) saved the log_probs externally as tensor (including the gradient path). This is why you do not have a very deep gradient history in your first snippet, I suppose.