TensorTonic
Study Plans
Study Plan
Score a KL-Regularized Policy Batch
Language ModelsReinforcement LearningLoss Functions
Medium

Score a batch of sampled responses with a KL penalty. For response i and token t, mitm_{it}mit​ marks a selected response token, ℓitπ\ell^\pi_{it}ℓitπ​ and ℓitref\ell^{\mathrm{ref}}_{it}ℓitref​ are its policy and reference log probabilities, rir_iri​ is its reward, and β\betaβ is the penalty coefficient. Compute

D^i=∑tmit(ℓitπ−ℓitref)\widehat D_i=\sum_t m_{it}(\ell^\pi_{it}-\ell^{\mathrm{ref}}_{it})Di​=t∑​mit​(ℓitπ​−ℓitref​) r~i=ri−βD^i\widetilde r_i=r_i-\beta\widehat D_iri​=ri​−βDi​ J=1B∑ir~i.J=\frac{1}{B}\sum_i\widetilde r_i.J=B1​i∑​ri​.

Here, D^i\widehat D_iDi​ is the sampled sequence divergence, r~i\widetilde r_iri​ its penalized reward, B the batch size, and J the mean objective. This sampled divergence may be negative; do not clamp it or divide by response length. Masked positions contribute zero. An empty batch has objective zero.

Return a Python dictionary of float32 PyTorch tensors: objective is scalar; penalized_rewards and sampled_divergences each have shape (B,). All outputs remain on the input device.

Loading visualization...

Examples

Input: policy_log_probs = [[-1,-2]], reference_log_probs = [[-1.5,-2.5]], rewards = [3], response_mask = [[true,true]], beta = 0.5

Output: {"objective":2.5,"penalized_rewards":[2.5],"sampled_divergences":[1]}

Explanation: The two selected differences sum to one, so the reward receives a penalty of 0.5.

Input: policy_log_probs = [[-1,-2,-3],[-0.5,-0.5,-0.5]], reference_log_probs = [[-1.2,-2.1,-3.4],[-0.7,-0.4,-0.2]], rewards = [1,-1], response_mask = [[true,true,false],[true,true,true]], beta = 0.2

Output: {"objective":-0.01,"penalized_rewards":[0.94,-0.96],"sampled_divergences":[0.3,-0.2]}

Input: policy_log_probs = [[-1,-2]], reference_log_probs = [[-3,-4]], rewards = [-2], response_mask = [[false,false]], beta = 3

Output: {"objective":-2,"penalized_rewards":[-2],"sampled_divergences":[0]}

Hint 1

torch.where can exclude masked entries before reduction.

Hint 2

Tensor.sum reduces a chosen axis.

Hint 3

A PyTorch scalar tensor has shape ().

Requirements

  • Use PyTorch with float32 arithmetic and outputs.
  • Respect the supplied response mask and penalty coefficient.
  • Do not modify input tensors.

Constraints

  • Policy and reference tensors share shape (batch, tokens); rewards has shape (batch,).
  • Floating inputs are supplied as float32 on one device.
  • The Boolean response mask matches the log-probability shape and device.
  • Floating inputs and beta are finite; beta is nonnegative.
Try Similar Problems
Implement the DPO LossMediumCompare Sequence and Token Length NormalizationMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasyImplement Symmetric CLIP Contrastive LossMediumNormalize GRPO Group AdvantagesMediumImplement the SigLIP Pairwise LossMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMediumPPO Clipped Surrogate ObjectiveMediumDQN Loss with Target NetworkMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Score a KL-Regularized Policy Batch

Language ModelsReinforcement LearningLoss Functions
Medium

Score a batch of sampled responses with a KL penalty. For response i and token t, mitm_{it}mit​ marks a selected response token, ℓitπ\ell^\pi_{it}ℓitπ​ and ℓitref\ell^{\mathrm{ref}}_{it}ℓitref​ are its policy and reference log probabilities, rir_iri​ is its reward, and β\betaβ is the penalty coefficient. Compute

D^i=∑tmit(ℓitπ−ℓitref)\widehat D_i=\sum_t m_{it}(\ell^\pi_{it}-\ell^{\mathrm{ref}}_{it})Di​=t∑​mit​(ℓitπ​−ℓitref​) r~i=ri−βD^i\widetilde r_i=r_i-\beta\widehat D_iri​=ri​−βDi​ J=1B∑ir~i.J=\frac{1}{B}\sum_i\widetilde r_i.J=B1​i∑​ri​.

Here, D^i\widehat D_iDi​ is the sampled sequence divergence, r~i\widetilde r_iri​ its penalized reward, B the batch size, and J the mean objective. This sampled divergence may be negative; do not clamp it or divide by response length. Masked positions contribute zero. An empty batch has objective zero.

Return a Python dictionary of float32 PyTorch tensors: objective is scalar; penalized_rewards and sampled_divergences each have shape (B,). All outputs remain on the input device.

Loading visualization...

Examples

Input: policy_log_probs = [[-1,-2]], reference_log_probs = [[-1.5,-2.5]], rewards = [3], response_mask = [[true,true]], beta = 0.5

Output: {"objective":2.5,"penalized_rewards":[2.5],"sampled_divergences":[1]}

Explanation: The two selected differences sum to one, so the reward receives a penalty of 0.5.

Input: policy_log_probs = [[-1,-2,-3],[-0.5,-0.5,-0.5]], reference_log_probs = [[-1.2,-2.1,-3.4],[-0.7,-0.4,-0.2]], rewards = [1,-1], response_mask = [[true,true,false],[true,true,true]], beta = 0.2

Output: {"objective":-0.01,"penalized_rewards":[0.94,-0.96],"sampled_divergences":[0.3,-0.2]}

Input: policy_log_probs = [[-1,-2]], reference_log_probs = [[-3,-4]], rewards = [-2], response_mask = [[false,false]], beta = 3

Output: {"objective":-2,"penalized_rewards":[-2],"sampled_divergences":[0]}

Hint 1

torch.where can exclude masked entries before reduction.

Hint 2

Tensor.sum reduces a chosen axis.

Hint 3

A PyTorch scalar tensor has shape ().

Requirements

  • Use PyTorch with float32 arithmetic and outputs.
  • Respect the supplied response mask and penalty coefficient.
  • Do not modify input tensors.

Constraints

  • Policy and reference tensors share shape (batch, tokens); rewards has shape (batch,).
  • Floating inputs are supplied as float32 on one device.
  • The Boolean response mask matches the log-probability shape and device.
  • Floating inputs and beta are finite; beta is nonnegative.
Try Similar Problems
Implement the DPO LossMediumCompare Sequence and Token Length NormalizationMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasyImplement Symmetric CLIP Contrastive LossMediumNormalize GRPO Group AdvantagesMediumImplement the SigLIP Pairwise LossMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMediumPPO Clipped Surrogate ObjectiveMediumDQN Loss with Target NetworkMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.