TensorTonic
Study Plans
Study Plan
Build an Assistant-Only SFT Loss Mask
Language ModelsLoss Functions
Easy

Build next-token training labels that supervise only assistant content. At source position t, the target is the token at position t + 1. Include that target only when both positions are attended and the target role equals assistant_role. Roles describe individual tokens; the source role does not determine eligibility.

Every other label is -100, including the final position. The Boolean loss mask is true exactly where a target is included. Return a Python dictionary with labels, a PyTorch tensor matching the input token shape and dtype, and loss_mask, a Boolean PyTorch tensor of the same shape. Both outputs stay on the input device.

Loading visualization...

Examples

Input: input_ids = [[10,11,20,21,2]], role_ids = [[-1,0,-1,1,-1]], attention_mask = [[true,true,true,true,true]], assistant_role = 1

Output: {"labels":[[-100,-100,21,-100,-100]],"loss_mask":[[false,false,true,false,false]]}

Explanation: Position two predicts token 21, the only next token marked as attended assistant content.

Input: input_ids = [[1,10,11,12]], role_ids = [[-1,0,0,-1]], attention_mask = [[true,true,true,true]], assistant_role = 1

Output: {"labels":[[-100,-100,-100,-100]],"loss_mask":[[false,false,false,false]]}

Input: input_ids = [[1,2,3]], role_ids = [[-1,1,1]], attention_mask = [[true,true,true]], assistant_role = 1

Output: {"labels":[[2,3,-100]],"loss_mask":[[true,true,false]]}

Hint 1

torch.full_like creates an output with the input shape and dtype.

Hint 2

Adjacent sequence slices align positions with their next-token targets.

Hint 3

torch.where selects values using a Boolean tensor.

Requirements

  • Use PyTorch with the supplied token roles and attention mask.
  • Ignore targets from other roles and any transition touching an unattended position.
  • Do not modify input tensors.

Constraints

  • All three tensors share shape (batch, sequence) and device.
  • Token and role tensors are supplied as int64; the attention mask is Boolean.
  • assistant_role is a Python integer.
  • Empty sequences and noncontiguous tensors are valid.
Try Similar Problems
Implement the DPO LossMediumScore a KL-Regularized Policy BatchMediumImplement Symmetric CLIP Contrastive LossMediumCompare Sequence and Token Length NormalizationMediumImplement the SigLIP Pairwise LossMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardOrthogonalize a Muon Momentum UpdateEasyMasked Language ModelingEasyDice LossEasyLog Loss (Binary Cross-Entropy)EasyNext Sentence PredictionEasy

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Build an Assistant-Only SFT Loss Mask

Language ModelsLoss Functions
Easy

Build next-token training labels that supervise only assistant content. At source position t, the target is the token at position t + 1. Include that target only when both positions are attended and the target role equals assistant_role. Roles describe individual tokens; the source role does not determine eligibility.

Every other label is -100, including the final position. The Boolean loss mask is true exactly where a target is included. Return a Python dictionary with labels, a PyTorch tensor matching the input token shape and dtype, and loss_mask, a Boolean PyTorch tensor of the same shape. Both outputs stay on the input device.

Loading visualization...

Examples

Input: input_ids = [[10,11,20,21,2]], role_ids = [[-1,0,-1,1,-1]], attention_mask = [[true,true,true,true,true]], assistant_role = 1

Output: {"labels":[[-100,-100,21,-100,-100]],"loss_mask":[[false,false,true,false,false]]}

Explanation: Position two predicts token 21, the only next token marked as attended assistant content.

Input: input_ids = [[1,10,11,12]], role_ids = [[-1,0,0,-1]], attention_mask = [[true,true,true,true]], assistant_role = 1

Output: {"labels":[[-100,-100,-100,-100]],"loss_mask":[[false,false,false,false]]}

Input: input_ids = [[1,2,3]], role_ids = [[-1,1,1]], attention_mask = [[true,true,true]], assistant_role = 1

Output: {"labels":[[2,3,-100]],"loss_mask":[[true,true,false]]}

Hint 1

torch.full_like creates an output with the input shape and dtype.

Hint 2

Adjacent sequence slices align positions with their next-token targets.

Hint 3

torch.where selects values using a Boolean tensor.

Requirements

  • Use PyTorch with the supplied token roles and attention mask.
  • Ignore targets from other roles and any transition touching an unattended position.
  • Do not modify input tensors.

Constraints

  • All three tensors share shape (batch, sequence) and device.
  • Token and role tensors are supplied as int64; the attention mask is Boolean.
  • assistant_role is a Python integer.
  • Empty sequences and noncontiguous tensors are valid.
Try Similar Problems
Implement the DPO LossMediumScore a KL-Regularized Policy BatchMediumImplement Symmetric CLIP Contrastive LossMediumCompare Sequence and Token Length NormalizationMediumImplement the SigLIP Pairwise LossMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardOrthogonalize a Muon Momentum UpdateEasyMasked Language ModelingEasyDice LossEasyLog Loss (Binary Cross-Entropy)EasyNext Sentence PredictionEasy

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.