Skip to content

[grpo] fix labels pop and peftmodel parameter check #4136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
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
7 changes: 5 additions & 2 deletions swift/trainers/rlhf_trainer/grpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ def _prepare_batch_inputs(self, inputs: InputsType, rewards: torch.Tensor) -> Li
batch_encoded_inputs = to_device(template.data_collator(batch_encoded_inputs), self.model.device)

# Process labels and masks
labels = batch_encoded_inputs['labels']
labels = batch_encoded_inputs.pop('labels')
logits_to_keep = (labels.shape[-1] - (torch.ne(labels, -100).int().argmax(-1))).max().item()
batch_encoded_inputs.update({
'completion_mask':
Expand Down Expand Up @@ -1164,7 +1164,10 @@ def _get_per_token_logps(self, model, inputs):
logits_to_keep = inputs['logits_to_keep']
input_ids = inputs['input_ids']
unwrapped_model = self.accelerator.unwrap_model(model)
parameters = inspect.signature(unwrapped_model.forward).parameters
if is_peft_model(unwrapped_model):
parameters = inspect.signature(unwrapped_model.base_model.model.forward).parameters
else:
parameters = inspect.signature(unwrapped_model.forward).parameters
if not unwrapped_model.model_meta.is_multimodal and 'logits_to_keep' in parameters:
# save memory
return super()._get_per_token_logps(model, input_ids, inputs['attention_mask'], logits_to_keep)
Expand Down
Loading