Skip to content

updates GRPOTrainer compatible with trl 0.17 #3969

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 33 commits into from
Apr 30, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix split
  • Loading branch information
hjh0119 committed Apr 27, 2025
commit 550f06fe017932770fa688aa2166834a64779005
10 changes: 5 additions & 5 deletions swift/trainers/rlhf_trainer/grpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@
OutputsType = List[List[Tuple[List[Dict], str]]]


def _batch_split_tensor_dict(tensor_dict_list: List[Dict[str, Optional[torch.Tensor]]],
num_chunks: int) -> List[List[Dict[str, Optional[torch.Tensor]]]]:
return [hf_split_tensor_dict(tensor_dict, num_chunks) for tensor_dict in tensor_dict_list]


def apply_split_tensor_dict_patch():
from trl.trainer import grpo_trainer

def _batch_split_tensor_dict(tensor_dict_list: List[Dict[str, Optional[torch.Tensor]]],
num_chunks: int) -> List[List[Dict[str, Optional[torch.Tensor]]]]:
return [hf_split_tensor_dict(tensor_dict, num_chunks) for tensor_dict in tensor_dict_list]

if not hasattr(grpo_trainer, '_original_split_tensor_dict'):
grpo_trainer._original_split_tensor_dict = hf_split_tensor_dict
grpo_trainer.split_tensor_dict = _batch_split_tensor_dict
Expand Down Expand Up @@ -863,6 +862,7 @@ def _generate_and_score_completions(self, inputs: InputsType) -> InputsType:
self._log_metrics(batch_encoded_inputs, messages, completions, total_rewards, total_rewards_per_func)

# TODO: Confirm that everything is a tensor.
batch_encoded_inputs.pop('logits_to_keep')
return batch_encoded_inputs

def _score_completions(self, inputs: InputsType) -> Tuple[torch.Tensor, torch.Tensor, List[str]]:
Expand Down
Loading