|
| 1 | +# Copyright 2020-2025 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import warnings |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +from transformers import TrainingArguments |
| 20 | + |
| 21 | +from ...trainer.grpo_config import GRPOConfig |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class MiniLLMConfig(GRPOConfig): |
| 26 | + """ |
| 27 | + Configuration class for [`MiniLLMTrainer`]. |
| 28 | +
|
| 29 | + This class includes only the parameters that are specific to MiniLLM training. For a full list of training |
| 30 | + arguments, please refer to the [`~transformers.TrainingArguments`] and [`GRPOConfig`] documentation. |
| 31 | +
|
| 32 | + Args: |
| 33 | + temperature (`float`, *optional*, defaults to `0.9`): |
| 34 | + Temperature for sampling. The higher the temperature, the more random the completions. |
| 35 | + lmbda (`float`, *optional*, defaults to `0.5`): |
| 36 | + Lambda parameter that controls the student data fraction (i.e., the proportion of on-policy |
| 37 | + student-generated outputs). |
| 38 | + beta (`float`, *optional*, defaults to `0.5`): |
| 39 | + Interpolation coefficient between `0.0` and `1.0` of the Generalized Jensen-Shannon Divergence loss. When |
| 40 | + beta is `0.0`, the loss is the KL divergence. When beta is `1.0`, the loss is the Inverse KL Divergence. |
| 41 | + max_new_tokens (`int`, *optional*, defaults to `128`): |
| 42 | + Maximum number of tokens to generate per completion. |
| 43 | + teacher_model_name_or_path (`str`, *optional*): |
| 44 | + Model name or path of the teacher model. If `None`, the teacher model will be the same as the model being |
| 45 | + trained. |
| 46 | + teacher_model_init_kwargs (`dict[str, Any]]`, *optional*): |
| 47 | + Keyword arguments to pass to `AutoModelForCausalLM.from_pretrained` when instantiating the teacher model |
| 48 | + from a string. |
| 49 | + disable_dropout (`bool`, *optional*, defaults to `True`): |
| 50 | + Whether to disable dropout in the model. |
| 51 | + seq_kd (`bool`, *optional*, defaults to `False`): |
| 52 | + Seq_kd parameter that controls whether to perform Sequence-Level KD (can be viewed as supervised FT on |
| 53 | + teacher-generated output). |
| 54 | + """ |
| 55 | + |
| 56 | + teacher_model_init_kwargs: dict[str, Any] | None = field( |
| 57 | + default=None, |
| 58 | + metadata={ |
| 59 | + "help": "Keyword arguments to pass to `AutoModelForCausalLM.from_pretrained` when instantiating the " |
| 60 | + "teacher model from a string." |
| 61 | + }, |
| 62 | + ) |
| 63 | + disable_dropout: bool = field( |
| 64 | + default=True, |
| 65 | + metadata={"help": "Whether to disable dropouts in `model`."}, |
| 66 | + ) |
| 67 | + rkl_advantage: bool = field( |
| 68 | + default=True, |
| 69 | + metadata={"help": "Whether to add the reverse KL advantage to the reward advantage."}, |
| 70 | + ) |
| 71 | + single_step_decomposition: bool = field( |
| 72 | + default=True, |
| 73 | + metadata={"help": "Whether to use single-step decomposition for the KL divergence computation."}, |
| 74 | + ) |
| 75 | + kd_temperature: float = field( |
| 76 | + default=1.0, |
| 77 | + metadata={ |
| 78 | + "help": "Temperature for knowledge distillation. Higher temperatures produce softer probability " |
| 79 | + "distributions over classes." |
| 80 | + }, |
| 81 | + ) |
| 82 | + gamma: float = field( |
| 83 | + default=0.0, |
| 84 | + metadata={"help": "Discount factor for future rewards in reinforcement learning."}, |
| 85 | + ) |
| 86 | + length_normalization: bool = field( |
| 87 | + default=True, |
| 88 | + metadata={"help": "Whether to apply length normalization to the rewards."}, |
| 89 | + ) |
| 90 | + |
| 91 | + def __post_init__(self): |
| 92 | + # We do not use the post_init of GRPOConfig because: |
| 93 | + # 1. num_generations can be < 2 in MiniLLMConfig. Scale_rewards must be set to "none" to avoid nan. |
| 94 | + self.bf16 = not (self.fp16) if self.bf16 is None else self.bf16 |
| 95 | + |
| 96 | + TrainingArguments.__post_init__(self) |
| 97 | + |
| 98 | + self.scale_rewards = {True: "group", False: "none"}.get(self.scale_rewards, self.scale_rewards) |
| 99 | + if self.num_generations == 1: |
| 100 | + self.scale_rewards = "none" |
| 101 | + |
| 102 | + num_processes = self.world_size |
| 103 | + # The current default effective batch size |
| 104 | + if self.generation_batch_size is None and self.steps_per_generation is None: |
| 105 | + self.steps_per_generation = self.gradient_accumulation_steps |
| 106 | + self.generation_batch_size = self.per_device_train_batch_size * num_processes * self.steps_per_generation |
| 107 | + elif self.generation_batch_size is not None and self.steps_per_generation is None: |
| 108 | + # Just ensure the value is divisible by the global batch size |
| 109 | + if self.generation_batch_size % (self.per_device_train_batch_size * num_processes) != 0: |
| 110 | + raise ValueError( |
| 111 | + f"generation_batch_size ({self.generation_batch_size}) must be divisible by the global batch size " |
| 112 | + f"({self.per_device_train_batch_size * num_processes})." |
| 113 | + ) |
| 114 | + self.steps_per_generation = self.generation_batch_size // ( |
| 115 | + self.per_device_train_batch_size * num_processes |
| 116 | + ) |
| 117 | + elif self.generation_batch_size is None and self.steps_per_generation is not None: |
| 118 | + self.generation_batch_size = self.per_device_train_batch_size * num_processes * self.steps_per_generation |
| 119 | + else: |
| 120 | + raise ValueError( |
| 121 | + "'generation_batch_size' and 'steps_per_generation' can not be both configured at the same time" |
| 122 | + ) |
| 123 | + |
| 124 | + if self.do_eval and self.eval_strategy != "no": |
| 125 | + # Just ensure the value is divisible by the global batch size |
| 126 | + if (self.per_device_eval_batch_size * num_processes) % self.num_generations != 0: |
| 127 | + raise ValueError( |
| 128 | + f"The global eval batch size ({self.per_device_eval_batch_size} * {num_processes}) must be " |
| 129 | + f"divisible by num_generations ({self.num_generations})." |
| 130 | + ) |
| 131 | + |
| 132 | + # The generation batch must contain full prompt groups (no partials), so it must be divisible by |
| 133 | + # num_generations. |
| 134 | + if self.generation_batch_size % self.num_generations != 0: |
| 135 | + raise ValueError( |
| 136 | + f"generation_batch_size ({self.generation_batch_size}) must be divisible by num_generations " |
| 137 | + f"({self.num_generations})." |
| 138 | + ) |
| 139 | + |
| 140 | + if self.use_liger_loss is not None: |
| 141 | + warnings.warn( |
| 142 | + "The `use_liger_loss` argument is deprecated and will be removed in version 0.28.0. Please use " |
| 143 | + "`use_liger_kernel` instead.", |
| 144 | + FutureWarning, |
| 145 | + stacklevel=2, |
| 146 | + ) |
| 147 | + self.use_liger_kernel = self.use_liger_loss |
| 148 | + |
| 149 | + if self.delta is not None and self.use_liger_kernel: |
| 150 | + raise ValueError("Liger kernel does not support two-sided GRPO loss yet.") |
0 commit comments