Compare sequence-summed and token-averaged policy-gradient contributions. For sequence i and token t, let Ai be its advantage, ℓit its token log probability, mit the Boolean selection mask, and ni its selected-token count. The two contributions are
gisum=Ait∑mitℓit giavg={gisum/ni,0,ni>0ni=0.Compare their total absolute magnitudes using
R=∑i∣giavg∣∑i∣gisum∣.Here, R is the magnitude ratio and the sums run over sequences. Use ratio zero when its denominator is zero. A fully masked sequence contributes zero to both vectors.
Return a Python dictionary of PyTorch tensors with sequence_summed and token_averaged of shape (batch,), and magnitude_ratio as a scalar. All outputs preserve the log-probability dtype and device.
Input: token_log_probs = [[-1,-2],[-3,-4]], advantages = [2,-1], token_mask = [[true,true],[true,true]]
Output: {"sequence_summed":[-6,7],"token_averaged":[-3,3.5],"magnitude_ratio":2}
Explanation: Both rows have two active tokens, so each averaged contribution is half its summed contribution.
Input: token_log_probs = [[-2,-5,-8]], advantages = [3], token_mask = [[true,false,false]]
Output: {"sequence_summed":[-6],"token_averaged":[-6],"magnitude_ratio":1}
Input: token_log_probs = [[-1,-2],[-3,-4]], advantages = [2,-1], token_mask = [[false,false],[false,false]]
Output: {"sequence_summed":[0,0],"token_averaged":[0,0],"magnitude_ratio":0}
Tensor.sum reduces a selected axis.
torch.where selects a value for rows with no active tokens.
Tensor.abs prevents signed sequence contributions from canceling during aggregation.
Accepts: array
Accepts: array
Accepts: array
Compare sequence-summed and token-averaged policy-gradient contributions. For sequence i and token t, let Ai be its advantage, ℓit its token log probability, mit the Boolean selection mask, and ni its selected-token count. The two contributions are
gisum=Ait∑mitℓit giavg={gisum/ni,0,ni>0ni=0.Compare their total absolute magnitudes using
R=∑i∣giavg∣∑i∣gisum∣.Here, R is the magnitude ratio and the sums run over sequences. Use ratio zero when its denominator is zero. A fully masked sequence contributes zero to both vectors.
Return a Python dictionary of PyTorch tensors with sequence_summed and token_averaged of shape (batch,), and magnitude_ratio as a scalar. All outputs preserve the log-probability dtype and device.
Input: token_log_probs = [[-1,-2],[-3,-4]], advantages = [2,-1], token_mask = [[true,true],[true,true]]
Output: {"sequence_summed":[-6,7],"token_averaged":[-3,3.5],"magnitude_ratio":2}
Explanation: Both rows have two active tokens, so each averaged contribution is half its summed contribution.
Input: token_log_probs = [[-2,-5,-8]], advantages = [3], token_mask = [[true,false,false]]
Output: {"sequence_summed":[-6],"token_averaged":[-6],"magnitude_ratio":1}
Input: token_log_probs = [[-1,-2],[-3,-4]], advantages = [2,-1], token_mask = [[false,false],[false,false]]
Output: {"sequence_summed":[0,0],"token_averaged":[0,0],"magnitude_ratio":0}
Tensor.sum reduces a selected axis.
torch.where selects a value for rows with no active tokens.
Tensor.abs prevents signed sequence contributions from canceling during aggregation.
Accepts: array
Accepts: array
Accepts: array