Trainer for the Group Relative Policy Optimization (GRPO) method. This algorithm was initially proposed in the paper [DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models](https://huggingface.co/papers/2402.03300). Example: ```python from datasets
| 68 | RewardFunc = Union[str, PreTrainedModel, Callable[[list, list], list[float]]] |
| 69 | |
| 70 | class AsyncRLGRPOTrainer(Trainer): |
| 71 | """ |
| 72 | Trainer for the Group Relative Policy Optimization (GRPO) method. This algorithm was initially proposed in the |
| 73 | paper [DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models](https://huggingface.co/papers/2402.03300). |
| 74 | |
| 75 | Example: |
| 76 | |
| 77 | ```python |
| 78 | from datasets import load_dataset |
| 79 | from trl import GRPOTrainer |
| 80 | |
| 81 | dataset = load_dataset("trl-lib/tldr", split="train") |
| 82 | |
| 83 | trainer = GRPOTrainer( |
| 84 | model="Qwen/Qwen2-0.5B-Instruct", |
| 85 | reward_funcs="weqweasdas/RM-Gemma-2B", |
| 86 | train_dataset=dataset, |
| 87 | ) |
| 88 | |
| 89 | trainer.train() |
| 90 | ``` |
| 91 | |
| 92 | Args: |
| 93 | model (`Union[str, PreTrainedModel]`): |
| 94 | Model to be trained. Can be either: |
| 95 | |
| 96 | - A string, being the *model id* of a pretrained model hosted inside a model repo on huggingface.co, or |
| 97 | a path to a *directory* containing model weights saved using |
| 98 | [`~transformers.PreTrainedModel.save_pretrained`], e.g., `'./my_model_directory/'`. The model is |
| 99 | loaded using [`~transformers.AutoModelForCausalLM.from_pretrained`] with the keywork arguments |
| 100 | in `args.model_init_kwargs`. |
| 101 | - A [`~transformers.PreTrainedModel`] object. Only causal language models are supported. |
| 102 | reward_funcs (`Union[RewardFunc, list[RewardFunc]]`): |
| 103 | Reward functions to be used for computing the rewards. To compute the rewards, we call all the reward |
| 104 | functions with the prompts and completions and sum the rewards. Can be either: |
| 105 | |
| 106 | - A single reward function, such as: |
| 107 | - A string: The *model ID* of a pretrained model hosted inside a model repo on huggingface.co, or a |
| 108 | path to a *directory* containing model weights saved using |
| 109 | [`~transformers.PreTrainedModel.save_pretrained`], e.g., `'./my_model_directory/'`. The model is loaded |
| 110 | using [`~transformers.AutoModelForSequenceClassification.from_pretrained`] with `num_labels=1` and the |
| 111 | keyword arguments in `args.model_init_kwargs`. |
| 112 | - A [`~transformers.PreTrainedModel`] object: Only sequence classification models are supported. |
| 113 | - A custom reward function: The function is provided with the prompts and the generated completions, |
| 114 | plus any additional columns in the dataset. It should return a list of rewards. For more details, see |
| 115 | [Using a custom reward function](#using-a-custom-reward-function). |
| 116 | - A list of reward functions, where each item can independently be any of the above types. Mixing different |
| 117 | types within the list (e.g., a string model ID and a custom reward function) is allowed. |
| 118 | args ([`GRPOConfig`], *optional*, defaults to `None`): |
| 119 | Configuration for this trainer. If `None`, a default configuration is used. |
| 120 | train_dataset ([`~datasets.Dataset`] or [`~datasets.IterableDataset`]): |
| 121 | Dataset to use for training. It must include a column `"prompt"`. Any additional columns in the dataset is |
| 122 | ignored. The format of the samples can be either: |
| 123 | |
| 124 | - [Standard](dataset_formats#standard): Each sample contains plain text. |
| 125 | - [Conversational](dataset_formats#conversational): Each sample contains structured messages (e.g., role |
| 126 | and content). |
| 127 | eval_dataset ([`~datasets.Dataset`], [`~datasets.IterableDataset`] or `dict[str, Union[Dataset, IterableDataset]]`): |