Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/twinkle/loss/grpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,16 @@ def __init__(self, loss_agg_mode: Literal['token-mean', 'seq-mean-token-mean'] =
f'Expected one of {sorted(self._LOSS_AGG_MODES)}.')
self.loss_agg_mode = loss_agg_mode

def micro_batch_scale(self, inputs, indices):
if self.loss_agg_mode != 'token-mean':
return super().micro_batch_scale(inputs, indices)
import torch
token_counts = [
self._resolve_loss_mask(model_input, torch.atleast_2d(torch.as_tensor(model_input['labels']))).sum().item()
for model_input in inputs
]
return sum(token_counts[index] for index in indices) / max(sum(token_counts), 1)

def _aggregate_loss(
self,
per_token_loss: 'torch.Tensor',
Expand Down