cciwon-code-review-cli 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/bin/code-review.js +1 -1
  2. package/lib/chat-mode.js +7 -2
  3. package/package.json +1 -1
  4. package/unsloth_compiled_cache/.locks/.lock.AqlmLoraLinear_peft_forward.py +0 -0
  5. package/unsloth_compiled_cache/.locks/.lock.AwqLoraLinear_peft_forward.py +0 -0
  6. package/unsloth_compiled_cache/.locks/.lock.BatchNorm1d.py +0 -0
  7. package/unsloth_compiled_cache/.locks/.lock.BatchNorm2d.py +0 -0
  8. package/unsloth_compiled_cache/.locks/.lock.BatchNorm3d.py +0 -0
  9. package/unsloth_compiled_cache/.locks/.lock.Conv1d.py +0 -0
  10. package/unsloth_compiled_cache/.locks/.lock.Conv2d.py +0 -0
  11. package/unsloth_compiled_cache/.locks/.lock.Conv3d.py +0 -0
  12. package/unsloth_compiled_cache/.locks/.lock.ConvTranspose1d.py +0 -0
  13. package/unsloth_compiled_cache/.locks/.lock.ConvTranspose2d.py +0 -0
  14. package/unsloth_compiled_cache/.locks/.lock.ConvTranspose3d.py +0 -0
  15. package/unsloth_compiled_cache/.locks/.lock.GPTQLoraLinear_peft_forward.py +0 -0
  16. package/unsloth_compiled_cache/.locks/.lock.GroupNorm.py +0 -0
  17. package/unsloth_compiled_cache/.locks/.lock.LayerNorm.py +0 -0
  18. package/unsloth_compiled_cache/.locks/.lock.Linear4bit_peft_forward.py +0 -0
  19. package/unsloth_compiled_cache/.locks/.lock.Linear8bitLt_peft_forward.py +0 -0
  20. package/unsloth_compiled_cache/.locks/.lock.Linear_peft_forward.py +0 -0
  21. package/unsloth_compiled_cache/.locks/.lock.LoraParallelLinear_peft_forward.py +0 -0
  22. package/unsloth_compiled_cache/.locks/.lock.RMSNorm.py +0 -0
  23. package/unsloth_compiled_cache/.locks/.lock.UnslothBCOTrainer.py +0 -0
  24. package/unsloth_compiled_cache/.locks/.lock.UnslothCPOTrainer.py +0 -0
  25. package/unsloth_compiled_cache/.locks/.lock.UnslothDPOTrainer.py +0 -0
  26. package/unsloth_compiled_cache/.locks/.lock.UnslothGKDTrainer.py +0 -0
  27. package/unsloth_compiled_cache/.locks/.lock.UnslothGRPOTrainer.py +0 -0
  28. package/unsloth_compiled_cache/.locks/.lock.UnslothKTOTrainer.py +0 -0
  29. package/unsloth_compiled_cache/.locks/.lock.UnslothNashMDTrainer.py +0 -0
  30. package/unsloth_compiled_cache/.locks/.lock.UnslothORPOTrainer.py +0 -0
  31. package/unsloth_compiled_cache/.locks/.lock.UnslothOnlineDPOTrainer.py +0 -0
  32. package/unsloth_compiled_cache/.locks/.lock.UnslothPPOTrainer.py +0 -0
  33. package/unsloth_compiled_cache/.locks/.lock.UnslothPRMTrainer.py +0 -0
  34. package/unsloth_compiled_cache/.locks/.lock.UnslothRLOOTrainer.py +0 -0
  35. package/unsloth_compiled_cache/.locks/.lock.UnslothRewardTrainer.py +0 -0
  36. package/unsloth_compiled_cache/.locks/.lock.UnslothSFTTrainer.py +0 -0
  37. package/unsloth_compiled_cache/.locks/.lock.UnslothXPOTrainer.py +0 -0
  38. package/unsloth_compiled_cache/.locks/.lock.unsloth_compiled_module_qwen3_moe.py +0 -0
  39. package/unsloth_compiled_cache/.locks/.lock.unsloth_compiled_module_siglip.py +0 -0
  40. package/unsloth_compiled_cache/AqlmLoraLinear_peft_forward.py +88 -0
  41. package/unsloth_compiled_cache/AwqLoraLinear_peft_forward.py +87 -0
  42. package/unsloth_compiled_cache/BatchNorm1d.py +117 -0
  43. package/unsloth_compiled_cache/BatchNorm2d.py +117 -0
  44. package/unsloth_compiled_cache/BatchNorm3d.py +117 -0
  45. package/unsloth_compiled_cache/Conv1d.py +70 -0
  46. package/unsloth_compiled_cache/Conv2d.py +70 -0
  47. package/unsloth_compiled_cache/Conv3d.py +70 -0
  48. package/unsloth_compiled_cache/ConvTranspose1d.py +97 -0
  49. package/unsloth_compiled_cache/ConvTranspose2d.py +106 -0
  50. package/unsloth_compiled_cache/ConvTranspose3d.py +98 -0
  51. package/unsloth_compiled_cache/GPTQLoraLinear_peft_forward.py +95 -0
  52. package/unsloth_compiled_cache/GroupNorm.py +70 -0
  53. package/unsloth_compiled_cache/LayerNorm.py +72 -0
  54. package/unsloth_compiled_cache/Linear4bit_peft_forward.py +115 -0
  55. package/unsloth_compiled_cache/Linear8bitLt_peft_forward.py +113 -0
  56. package/unsloth_compiled_cache/Linear_peft_forward.py +104 -0
  57. package/unsloth_compiled_cache/LoraParallelLinear_peft_forward.py +91 -0
  58. package/unsloth_compiled_cache/RMSNorm.py +73 -0
  59. package/unsloth_compiled_cache/UnslothBCOTrainer.py +2026 -0
  60. package/unsloth_compiled_cache/UnslothCPOTrainer.py +1806 -0
  61. package/unsloth_compiled_cache/UnslothDPOTrainer.py +2750 -0
  62. package/unsloth_compiled_cache/UnslothGKDTrainer.py +1157 -0
  63. package/unsloth_compiled_cache/UnslothGRPOTrainer.py +3607 -0
  64. package/unsloth_compiled_cache/UnslothKTOTrainer.py +2220 -0
  65. package/unsloth_compiled_cache/UnslothNashMDTrainer.py +1210 -0
  66. package/unsloth_compiled_cache/UnslothORPOTrainer.py +1730 -0
  67. package/unsloth_compiled_cache/UnslothOnlineDPOTrainer.py +2313 -0
  68. package/unsloth_compiled_cache/UnslothPPOTrainer.py +1504 -0
  69. package/unsloth_compiled_cache/UnslothPRMTrainer.py +979 -0
  70. package/unsloth_compiled_cache/UnslothRLOOTrainer.py +2674 -0
  71. package/unsloth_compiled_cache/UnslothRewardTrainer.py +1197 -0
  72. package/unsloth_compiled_cache/UnslothSFTTrainer.py +1416 -0
  73. package/unsloth_compiled_cache/UnslothXPOTrainer.py +1255 -0
  74. package/unsloth_compiled_cache/__pycache__/AqlmLoraLinear_peft_forward.cpython-312.pyc +0 -0
  75. package/unsloth_compiled_cache/__pycache__/AwqLoraLinear_peft_forward.cpython-312.pyc +0 -0
  76. package/unsloth_compiled_cache/__pycache__/BatchNorm1d.cpython-312.pyc +0 -0
  77. package/unsloth_compiled_cache/__pycache__/BatchNorm2d.cpython-312.pyc +0 -0
  78. package/unsloth_compiled_cache/__pycache__/BatchNorm3d.cpython-312.pyc +0 -0
  79. package/unsloth_compiled_cache/__pycache__/Conv1d.cpython-312.pyc +0 -0
  80. package/unsloth_compiled_cache/__pycache__/Conv2d.cpython-312.pyc +0 -0
  81. package/unsloth_compiled_cache/__pycache__/Conv3d.cpython-312.pyc +0 -0
  82. package/unsloth_compiled_cache/__pycache__/ConvTranspose1d.cpython-312.pyc +0 -0
  83. package/unsloth_compiled_cache/__pycache__/ConvTranspose2d.cpython-312.pyc +0 -0
  84. package/unsloth_compiled_cache/__pycache__/ConvTranspose3d.cpython-312.pyc +0 -0
  85. package/unsloth_compiled_cache/__pycache__/GPTQLoraLinear_peft_forward.cpython-312.pyc +0 -0
  86. package/unsloth_compiled_cache/__pycache__/GroupNorm.cpython-312.pyc +0 -0
  87. package/unsloth_compiled_cache/__pycache__/LayerNorm.cpython-312.pyc +0 -0
  88. package/unsloth_compiled_cache/__pycache__/Linear4bit_peft_forward.cpython-312.pyc +0 -0
  89. package/unsloth_compiled_cache/__pycache__/Linear8bitLt_peft_forward.cpython-312.pyc +0 -0
  90. package/unsloth_compiled_cache/__pycache__/Linear_peft_forward.cpython-312.pyc +0 -0
  91. package/unsloth_compiled_cache/__pycache__/LoraParallelLinear_peft_forward.cpython-312.pyc +0 -0
  92. package/unsloth_compiled_cache/__pycache__/RMSNorm.cpython-312.pyc +0 -0
  93. package/unsloth_compiled_cache/__pycache__/UnslothBCOTrainer.cpython-312.pyc +0 -0
  94. package/unsloth_compiled_cache/__pycache__/UnslothCPOTrainer.cpython-312.pyc +0 -0
  95. package/unsloth_compiled_cache/__pycache__/UnslothDPOTrainer.cpython-312.pyc +0 -0
  96. package/unsloth_compiled_cache/__pycache__/UnslothGKDTrainer.cpython-312.pyc +0 -0
  97. package/unsloth_compiled_cache/__pycache__/UnslothGRPOTrainer.cpython-312.pyc +0 -0
  98. package/unsloth_compiled_cache/__pycache__/UnslothKTOTrainer.cpython-312.pyc +0 -0
  99. package/unsloth_compiled_cache/__pycache__/UnslothNashMDTrainer.cpython-312.pyc +0 -0
  100. package/unsloth_compiled_cache/__pycache__/UnslothORPOTrainer.cpython-312.pyc +0 -0
  101. package/unsloth_compiled_cache/__pycache__/UnslothOnlineDPOTrainer.cpython-312.pyc +0 -0
  102. package/unsloth_compiled_cache/__pycache__/UnslothPPOTrainer.cpython-312.pyc +0 -0
  103. package/unsloth_compiled_cache/__pycache__/UnslothPRMTrainer.cpython-312.pyc +0 -0
  104. package/unsloth_compiled_cache/__pycache__/UnslothRLOOTrainer.cpython-312.pyc +0 -0
  105. package/unsloth_compiled_cache/__pycache__/UnslothRewardTrainer.cpython-312.pyc +0 -0
  106. package/unsloth_compiled_cache/__pycache__/UnslothSFTTrainer.cpython-312.pyc +0 -0
  107. package/unsloth_compiled_cache/__pycache__/UnslothXPOTrainer.cpython-312.pyc +0 -0
  108. package/unsloth_compiled_cache/__pycache__/unsloth_compiled_module_qwen3_moe.cpython-312.pyc +0 -0
  109. package/unsloth_compiled_cache/__pycache__/unsloth_compiled_module_siglip.cpython-312.pyc +0 -0
  110. package/unsloth_compiled_cache/unsloth_compiled_module_qwen3_moe.py +726 -0
  111. package/unsloth_compiled_cache/unsloth_compiled_module_siglip.py +534 -0
@@ -0,0 +1,1197 @@
1
+ """
2
+ 2025.12.6
3
+ 2025.12.7
4
+ 4.57.1
5
+ 0.24.0
6
+ __UNSLOTH_VERSIONING__
7
+ """
8
+
9
+ # Unsloth auto generated code
10
+ # Copyright 2023-present Daniel Han-Chen, Michael Han-Chen & the Unsloth team. All rights reserved.
11
+ #
12
+ # This program is free software: you can redistribute it and/or modify
13
+ # it under the terms of the GNU Lesser General Public License as published by
14
+ # the Free Software Foundation, either version 3 of the License, or
15
+ # (at your option) any later version.
16
+ #
17
+ # This program is distributed in the hope that it will be useful,
18
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ # GNU General Public License for more details.
21
+ #
22
+ # You should have received a copy of the GNU Lesser General Public License
23
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
24
+
25
+ from torch import Tensor
26
+ import torch
27
+ import torch.nn as nn
28
+ from torch.nn import functional as F
29
+ from typing import Any, List, Optional, Tuple, Union, Dict, Set, Callable
30
+ from trl.trainer.reward_trainer import (Any, AutoModelForSequenceClassification, AutoTokenizer, BaseTrainer, Callable, DataCollator, DataCollatorForPreference, Dataset, EvalPrediction, IterableDataset, Optional, PartialState, Path, PeftConfig, PreTrainedModel, PreTrainedTokenizerBase, RewardConfig, RewardTrainer, TrainerCallback, Union, clone_chat_template, contextlib, dataclass, defaultdict, disable_dropout_in_model, get_act_offloading_ctx_manager, is_conversational, logger, logging, nn, os, pad, prepare_peft_model, re, remove_none_values, suppress_from_pretrained_warning, torch, transformers, Optional, PreTrainedModel, logger, os, re, torch)
31
+
32
+
33
+ import os
34
+ from typing import *
35
+ from dataclasses import dataclass, field
36
+ from packaging.version import Version
37
+ import torch
38
+ import numpy as np
39
+ from contextlib import nullcontext
40
+ from torch.nn import functional as F
41
+ import inspect
42
+ from transformers import DataCollatorForSeq2Seq, DataCollatorForLanguageModeling as TransformersDataCollatorForLanguageModeling
43
+ from transformers.training_args import ParallelMode
44
+
45
+ # Wrap trainer with padding to right and enable training mode
46
+ # Also patches W&B since multiple runs must use wandb.finish()
47
+ import functools
48
+ from types import MethodType
49
+ def prepare_for_training_mode(f):
50
+ @functools.wraps(f)
51
+ def wrapper(self, *args, **kwargs):
52
+ # Enable training mode
53
+ if hasattr(self, 'model') and hasattr(self.model, "for_training"):
54
+ self.model.for_training()
55
+ output = f(self, *args, **kwargs)
56
+ # Return inference mode
57
+ if hasattr(self, 'model') and hasattr(self.model, "for_inference"):
58
+ self.model.for_inference()
59
+ # Patch W&B to enable logging on future runs, otherwise it'll overwrite the first run
60
+ try:
61
+ import wandb
62
+ wandb.finish()
63
+ except:
64
+ pass
65
+ return output
66
+ return wrapper
67
+ pass
68
+
69
+ torch_compile_options = {
70
+ "epilogue_fusion" : True,
71
+ "max_autotune" : False,
72
+ "shape_padding" : True,
73
+ "trace.enabled" : False,
74
+ "triton.cudagraphs" : False,
75
+ }
76
+
77
+ @torch.compile(dynamic = True, fullgraph = True, options = torch_compile_options,)
78
+ def chunked_selective_log_softmax(logits, index):
79
+ # Split into 4 chunks only
80
+ chunked_logits = torch.chunk(logits.reshape(-1, logits.shape[-1]), chunks = 4, dim = 0)
81
+ chunked_index = torch.chunk(index.reshape(-1), chunks = 4, dim = 0)
82
+ all_per_token_logps = []
83
+ # Below loop does the same as selective_log_softmax(chunk_logits, chunk_index)
84
+ for chunk_logits, chunk_index in zip(chunked_logits, chunked_index):
85
+ chunk_logits = chunk_logits.to(torch.float32)
86
+ selected_logits = torch.gather(chunk_logits, dim = -1, index = chunk_index.unsqueeze(-1)).squeeze(-1)
87
+ logsumexp_values = torch.logsumexp(chunk_logits, dim = -1)
88
+ per_token_logps = selected_logits - logsumexp_values
89
+ all_per_token_logps.append(per_token_logps)
90
+ pass
91
+ all_per_token_logps = torch.concat(all_per_token_logps)
92
+ all_per_token_logps = all_per_token_logps.reshape((logits.shape[0], logits.shape[1]))
93
+ return all_per_token_logps
94
+
95
+ def calculate_pad_tokens_in_prompt(
96
+ input_ids: torch.Tensor,
97
+ logits_to_keep: int,
98
+ pad_token_id: int
99
+ ) -> torch.Tensor:
100
+ """
101
+ Given prompt tensor, it returns all the left padded tokens in that sequence. so [pad, pad, pad, cat] = 3 tokens
102
+ """
103
+ if logits_to_keep >= input_ids.shape[1]:
104
+ raise ValueError("logits_to_keep must be smaller than the sequence length.")
105
+
106
+ prompt_section = input_ids[:, :-logits_to_keep]
107
+
108
+ padding_mask = (prompt_section == pad_token_id)
109
+
110
+ pad_token_counts = padding_mask.sum(dim=1)
111
+
112
+ return pad_token_counts
113
+
114
+ def create_completion_attention_mask(
115
+ completion_input_ids: torch.Tensor,
116
+ left_pad_tokens_per_prompt: torch.Tensor,
117
+ max_left_pad: int,
118
+ pad_token_id: int
119
+ ) -> torch.Tensor:
120
+ """
121
+ Given that we have a sequence, [p,p,p,c,c,c,pad,pad,pad]
122
+
123
+ Where p are extra prompt tokens we got from slicing the torch tensor, c is completion tokens
124
+ and pad are pad tokens, this function would make a completion mask that would 0 out the pad
125
+ and p tokens. so in this example [0,0,0,1,1,1,0,0,0]
126
+ """
127
+ batch_size, completion_len = completion_input_ids.shape
128
+ device = completion_input_ids.device
129
+
130
+ num_tokens_to_mask = max_left_pad - left_pad_tokens_per_prompt
131
+
132
+ indices = torch.arange(completion_len, device=device).unsqueeze(0)
133
+ shift_mask = indices >= num_tokens_to_mask.unsqueeze(1)
134
+
135
+ non_padding_mask = (completion_input_ids != pad_token_id)
136
+
137
+ final_mask = shift_mask & non_padding_mask
138
+
139
+ return final_mask
140
+
141
+ def left_pack_padding(tensor: torch.Tensor, pad_id: int) -> torch.Tensor:
142
+ """
143
+ Moves all padding tokens in each sequence of a batch to the right.
144
+ """
145
+ mask = (tensor != pad_id)
146
+ # Must do stable=True since binary mark is unordered
147
+ sorted_indices = torch.argsort(mask, dim=1, descending=True, stable=True)
148
+ packed_tensor = torch.gather(tensor, 1, sorted_indices)
149
+ return packed_tensor
150
+
151
+ def align_logprobs_with_mask(
152
+ logprob_tensor: torch.Tensor,
153
+ attention_mask: torch.Tensor,
154
+ pad_value: float = 0.0
155
+ ) -> torch.Tensor:
156
+ """
157
+ Aligns a log probability tensor with a given attention mask.
158
+ """
159
+
160
+ device = logprob_tensor.device
161
+ batch_size, logprob_seq_len = logprob_tensor.shape
162
+ mask_seq_len = attention_mask.shape[1]
163
+
164
+ padded_logprobs = torch.full(
165
+ attention_mask.shape,
166
+ fill_value=pad_value,
167
+ dtype=logprob_tensor.dtype,
168
+ device=device
169
+ )
170
+
171
+ left_pad_counts = torch.argmax(attention_mask, dim=1)
172
+
173
+ cols = torch.arange(logprob_seq_len, device=device)
174
+ dest_indices = left_pad_counts.unsqueeze(1) + cols
175
+
176
+ # Create destination row indices
177
+ # Shape: [batch_size, logprob_seq_len]
178
+ row_indices = torch.arange(batch_size, device=device).unsqueeze(1).expand_as(dest_indices)
179
+
180
+ # --- 4. Filter out-of-bounds indices and perform assignment ---
181
+ # Create a mask to identify only the indices that are within the bounds
182
+ # of the target tensor's sequence length.
183
+ valid_mask = dest_indices < mask_seq_len
184
+
185
+ # Use this mask to select only the valid row indices, column indices,
186
+ # and the corresponding values from the logprob tensor.
187
+ # This flattens the selected elements into 1D tensors.
188
+ valid_rows = row_indices[valid_mask]
189
+ valid_cols = dest_indices[valid_mask]
190
+ valid_vals = logprob_tensor[valid_mask]
191
+
192
+ # Place the valid values into their correct positions in the padded tensor
193
+ # using a single, efficient advanced indexing operation.
194
+ padded_logprobs[valid_rows, valid_cols] = valid_vals
195
+
196
+ return padded_logprobs
197
+ @dataclass
198
+ class UnslothRewardConfig(RewardConfig):
199
+ """
200
+
201
+ Configuration class for the [`RewardTrainer`].
202
+
203
+ This class includes only the parameters that are specific to Reward training. For a full list of training
204
+ arguments, please refer to the [`~transformers.TrainingArguments`] documentation. Note that default values in this
205
+ class may differ from those in [`~transformers.TrainingArguments`].
206
+
207
+ Using [`~transformers.HfArgumentParser`] we can turn this class into
208
+ [argparse](https://docs.python.org/3/library/argparse#module-argparse) arguments that can be specified on the
209
+ command line.
210
+
211
+ Parameters:
212
+ > Parameters that control the model
213
+
214
+ model_init_kwargs (`dict[str, Any]`, *optional*):
215
+ Keyword arguments for [`~transformers.AutoModelForCausalLM.from_pretrained`], used when the `model`
216
+ argument of the [`RewardTrainer`] is provided as a string. If you're training a MoE architecture and want
217
+ to include the load balancing/auxilliary loss as a part of the final loss, remember to set
218
+ `output_router_logits=True` in this dictionary.
219
+ chat_template_path (`str`, *optional*):
220
+ If specified, sets the model's chat template. This can either be the path to a tokenizer (local directory
221
+ or Hugging Face Hub model) or a direct path to a Jinja template file. When using a Jinja file, you must
222
+ ensure that any special tokens referenced in the template are added to the tokenizer and that the model's
223
+ embedding layer is resized accordingly.
224
+ disable_dropout (`bool`, *optional*, defaults to `True`):
225
+ Whether to disable dropout in the model.
226
+
227
+ > Parameters that control the data preprocessing
228
+
229
+ dataset_num_proc (`int`, *optional*):
230
+ Number of processes to use for processing the dataset.
231
+ eos_token (`str`, *optional*):
232
+ Token used to indicate the end of a turn or sequence. If `None`, it defaults to
233
+ `processing_class.eos_token`.
234
+ pad_token (`str`, *optional*):
235
+ Token used for padding. If `None`, it defaults to `processing_class.pad_token`, or if that is also `None`,
236
+ it falls back to `processing_class.eos_token`.
237
+ max_length (`int` or `None`, *optional*, defaults to `1024`):
238
+ Maximum length of the tokenized sequence. Samples are filtered out if either chosen or rejected sequence
239
+ exceeds this value. If `None`, no filtering is applied.
240
+ pad_to_multiple_of (`int`, *optional*):
241
+ If set, the sequences will be padded to a multiple of this value.
242
+
243
+ > Parameters that control the training
244
+
245
+ center_rewards_coefficient (`float`, *optional*):
246
+ Coefficient to incentivize the reward model to output mean-zero rewards (proposed by
247
+ https://huggingface.co/papers/2312.09244, Eq. 2). Recommended value: `0.01`.
248
+ activation_offloading (`bool`, *optional*, defaults to `False`):
249
+ Whether to offload the activations to the CPU.
250
+
251
+ """
252
+ vllm_sampling_params: Optional[Any] = field(
253
+ default = None,
254
+ metadata = {'help': 'vLLM SamplingParams'},
255
+ )
256
+ unsloth_num_chunks : Optional[int] = field(
257
+ default = -1,
258
+ metadata = {'help': 'Chunk size to reduce memory usage. -1 is most efficient.'},
259
+ )
260
+ max_seq_length : Optional[int] = field(
261
+ default = None,
262
+ metadata = {'help': 'Maximum sequence length to truncate to.'},
263
+ )
264
+ def __init__(
265
+ self,
266
+ output_dir = None,
267
+ overwrite_output_dir = None,
268
+ do_train = False,
269
+ do_eval = False,
270
+ do_predict = False,
271
+ eval_strategy = 'no',
272
+ prediction_loss_only = False,
273
+ per_device_train_batch_size = 4,
274
+ per_device_eval_batch_size = 4,
275
+ per_gpu_train_batch_size = None,
276
+ per_gpu_eval_batch_size = None,
277
+ gradient_accumulation_steps = 2,
278
+ eval_accumulation_steps = 2,
279
+ eval_delay = 0,
280
+ torch_empty_cache_steps = 250,
281
+ learning_rate = 5e-05,
282
+ weight_decay = 0.01,
283
+ adam_beta1 = 0.9,
284
+ adam_beta2 = 0.999,
285
+ adam_epsilon = 1e-08,
286
+ max_grad_norm = 1.0,
287
+ num_train_epochs = 3.0,
288
+ max_steps = -1,
289
+ lr_scheduler_type = 'linear',
290
+ warmup_ratio = 0.1,
291
+ warmup_steps = 0,
292
+ log_level = 'passive',
293
+ log_level_replica = 'warning',
294
+ log_on_each_node = True,
295
+ logging_dir = None,
296
+ logging_strategy = 'steps',
297
+ logging_first_step = False,
298
+ logging_steps = 1,
299
+ logging_nan_inf_filter = False,
300
+ save_strategy = 'steps',
301
+ save_steps = 500,
302
+ save_total_limit = None,
303
+ save_safetensors = True,
304
+ save_on_each_node = False,
305
+ save_only_model = False,
306
+ restore_callback_states_from_checkpoint = False,
307
+ no_cuda = False,
308
+ use_cpu = False,
309
+ use_mps_device = False,
310
+ seed = 3407,
311
+ data_seed = 3407,
312
+ jit_mode_eval = False,
313
+ bf16 = False,
314
+ fp16 = False,
315
+ fp16_opt_level = 'O1',
316
+ half_precision_backend = 'auto',
317
+ bf16_full_eval = False,
318
+ fp16_full_eval = False,
319
+ tf32 = None,
320
+ local_rank = -1,
321
+ ddp_backend = None,
322
+ tpu_num_cores = None,
323
+ tpu_metrics_debug = False,
324
+ debug = '',
325
+ dataloader_drop_last = False,
326
+ eval_steps = None,
327
+ dataloader_num_workers = 0,
328
+ dataloader_prefetch_factor = None,
329
+ past_index = -1,
330
+ run_name = None,
331
+ disable_tqdm = None,
332
+ remove_unused_columns = True,
333
+ label_names = None,
334
+ load_best_model_at_end = False,
335
+ metric_for_best_model = None,
336
+ greater_is_better = None,
337
+ ignore_data_skip = False,
338
+ fsdp = None,
339
+ fsdp_min_num_params = 0,
340
+ fsdp_config = None,
341
+ fsdp_transformer_layer_cls_to_wrap = None,
342
+ accelerator_config = None,
343
+ parallelism_config = None,
344
+ deepspeed = None,
345
+ label_smoothing_factor = 0.0,
346
+ optim = 'adamw_8bit',
347
+ optim_args = None,
348
+ adafactor = False,
349
+ group_by_length = False,
350
+ length_column_name = 'length',
351
+ report_to = 'none',
352
+ project = 'huggingface',
353
+ trackio_space_id = 'trackio',
354
+ ddp_find_unused_parameters = None,
355
+ ddp_bucket_cap_mb = None,
356
+ ddp_broadcast_buffers = None,
357
+ dataloader_pin_memory = True,
358
+ dataloader_persistent_workers = False,
359
+ skip_memory_metrics = True,
360
+ use_legacy_prediction_loop = False,
361
+ push_to_hub = False,
362
+ resume_from_checkpoint = None,
363
+ hub_model_id = None,
364
+ hub_strategy = 'every_save',
365
+ hub_token = None,
366
+ hub_private_repo = None,
367
+ hub_always_push = False,
368
+ hub_revision = None,
369
+ gradient_checkpointing = True,
370
+ gradient_checkpointing_kwargs = None,
371
+ include_inputs_for_metrics = False,
372
+ eval_do_concat_batches = True,
373
+ fp16_backend = 'auto',
374
+ push_to_hub_model_id = None,
375
+ push_to_hub_organization = None,
376
+ push_to_hub_token = None,
377
+ mp_parameters = '',
378
+ auto_find_batch_size = False,
379
+ full_determinism = False,
380
+ torchdynamo = None,
381
+ ray_scope = 'last',
382
+ ddp_timeout = 1800,
383
+ torch_compile = False,
384
+ torch_compile_backend = None,
385
+ torch_compile_mode = None,
386
+ include_tokens_per_second = False,
387
+ include_num_input_tokens_seen = False,
388
+ neftune_noise_alpha = None,
389
+ optim_target_modules = None,
390
+ batch_eval_metrics = False,
391
+ eval_on_start = False,
392
+ use_liger_kernel = False,
393
+ liger_kernel_config = None,
394
+ eval_use_gather_object = False,
395
+ average_tokens_across_devices = True,
396
+ model_init_kwargs = None,
397
+ chat_template_path = None,
398
+ disable_dropout = True,
399
+ dataset_num_proc = None,
400
+ eos_token = None,
401
+ pad_token = None,
402
+ max_length = 1024,
403
+ pad_to_multiple_of = None,
404
+ center_rewards_coefficient = None,
405
+ activation_offloading = False,
406
+ vllm_sampling_params = None,
407
+ unsloth_num_chunks = -1,
408
+ max_seq_length = None,
409
+ **kwargs,
410
+ ):
411
+ if learning_rate < 1e-7: print(f'Unsloth: Your learning rate of `{learning_rate}` is too small and less than 1e-7! Consider increasing it, otherwise gradient updates will be close to 0!')
412
+ if learning_rate > 1: print(f'Unsloth: Your learning rate of `{learning_rate}` is way too larger > 1! Consider decreasing it to 1e-1, otherwise gradient updates will explode!')
413
+ if output_dir is None and save_strategy == 'steps' and save_steps == 500:
414
+ output_dir = 'unsloth_training_checkpoints'
415
+ save_strategy = 'no'
416
+ if dataset_num_proc is None:
417
+ from multiprocessing import cpu_count
418
+ dataset_num_proc = min(max(cpu_count()+4, 2), 64)
419
+ if os.environ.get('UNSLOTH_ENABLE_FLEX_ATTENTION', '0') == '1':
420
+ from unsloth_zoo.flex_attention import HAS_FLEX_ATTENTION
421
+ if HAS_FLEX_ATTENTION and pad_to_multiple_of is None:
422
+ from unsloth_zoo.flex_attention import FLEX_ATTENTION_BLOCK_SIZE
423
+ pad_to_multiple_of = FLEX_ATTENTION_BLOCK_SIZE
424
+
425
+
426
+ super().__init__(
427
+ output_dir = output_dir,
428
+ overwrite_output_dir = overwrite_output_dir,
429
+ do_train = do_train,
430
+ do_eval = do_eval,
431
+ do_predict = do_predict,
432
+ eval_strategy = eval_strategy,
433
+ prediction_loss_only = prediction_loss_only,
434
+ per_device_train_batch_size = per_device_train_batch_size,
435
+ per_device_eval_batch_size = per_device_eval_batch_size,
436
+ per_gpu_train_batch_size = per_gpu_train_batch_size,
437
+ per_gpu_eval_batch_size = per_gpu_eval_batch_size,
438
+ gradient_accumulation_steps = gradient_accumulation_steps,
439
+ eval_accumulation_steps = eval_accumulation_steps,
440
+ eval_delay = eval_delay,
441
+ torch_empty_cache_steps = torch_empty_cache_steps,
442
+ learning_rate = learning_rate,
443
+ weight_decay = weight_decay,
444
+ adam_beta1 = adam_beta1,
445
+ adam_beta2 = adam_beta2,
446
+ adam_epsilon = adam_epsilon,
447
+ max_grad_norm = max_grad_norm,
448
+ num_train_epochs = num_train_epochs,
449
+ max_steps = max_steps,
450
+ lr_scheduler_type = lr_scheduler_type,
451
+ warmup_ratio = warmup_ratio,
452
+ warmup_steps = warmup_steps,
453
+ log_level = log_level,
454
+ log_level_replica = log_level_replica,
455
+ log_on_each_node = log_on_each_node,
456
+ logging_dir = logging_dir,
457
+ logging_strategy = logging_strategy,
458
+ logging_first_step = logging_first_step,
459
+ logging_steps = logging_steps,
460
+ logging_nan_inf_filter = logging_nan_inf_filter,
461
+ save_strategy = save_strategy,
462
+ save_steps = save_steps,
463
+ save_total_limit = save_total_limit,
464
+ save_safetensors = save_safetensors,
465
+ save_on_each_node = save_on_each_node,
466
+ save_only_model = save_only_model,
467
+ restore_callback_states_from_checkpoint = restore_callback_states_from_checkpoint,
468
+ no_cuda = no_cuda,
469
+ use_cpu = use_cpu,
470
+ use_mps_device = use_mps_device,
471
+ seed = seed,
472
+ data_seed = data_seed,
473
+ jit_mode_eval = jit_mode_eval,
474
+ bf16 = bf16,
475
+ fp16 = fp16,
476
+ fp16_opt_level = fp16_opt_level,
477
+ half_precision_backend = half_precision_backend,
478
+ bf16_full_eval = bf16_full_eval,
479
+ fp16_full_eval = fp16_full_eval,
480
+ tf32 = tf32,
481
+ local_rank = local_rank,
482
+ ddp_backend = ddp_backend,
483
+ tpu_num_cores = tpu_num_cores,
484
+ tpu_metrics_debug = tpu_metrics_debug,
485
+ debug = debug,
486
+ dataloader_drop_last = dataloader_drop_last,
487
+ eval_steps = eval_steps,
488
+ dataloader_num_workers = dataloader_num_workers,
489
+ dataloader_prefetch_factor = dataloader_prefetch_factor,
490
+ past_index = past_index,
491
+ run_name = run_name,
492
+ disable_tqdm = disable_tqdm,
493
+ remove_unused_columns = remove_unused_columns,
494
+ label_names = label_names,
495
+ load_best_model_at_end = load_best_model_at_end,
496
+ metric_for_best_model = metric_for_best_model,
497
+ greater_is_better = greater_is_better,
498
+ ignore_data_skip = ignore_data_skip,
499
+ fsdp = fsdp,
500
+ fsdp_min_num_params = fsdp_min_num_params,
501
+ fsdp_config = fsdp_config,
502
+ fsdp_transformer_layer_cls_to_wrap = fsdp_transformer_layer_cls_to_wrap,
503
+ accelerator_config = accelerator_config,
504
+ parallelism_config = parallelism_config,
505
+ deepspeed = deepspeed,
506
+ label_smoothing_factor = label_smoothing_factor,
507
+ optim = optim,
508
+ optim_args = optim_args,
509
+ adafactor = adafactor,
510
+ group_by_length = group_by_length,
511
+ length_column_name = length_column_name,
512
+ report_to = report_to,
513
+ project = project,
514
+ trackio_space_id = trackio_space_id,
515
+ ddp_find_unused_parameters = ddp_find_unused_parameters,
516
+ ddp_bucket_cap_mb = ddp_bucket_cap_mb,
517
+ ddp_broadcast_buffers = ddp_broadcast_buffers,
518
+ dataloader_pin_memory = dataloader_pin_memory,
519
+ dataloader_persistent_workers = dataloader_persistent_workers,
520
+ skip_memory_metrics = skip_memory_metrics,
521
+ use_legacy_prediction_loop = use_legacy_prediction_loop,
522
+ push_to_hub = push_to_hub,
523
+ resume_from_checkpoint = resume_from_checkpoint,
524
+ hub_model_id = hub_model_id,
525
+ hub_strategy = hub_strategy,
526
+ hub_token = hub_token,
527
+ hub_private_repo = hub_private_repo,
528
+ hub_always_push = hub_always_push,
529
+ hub_revision = hub_revision,
530
+ gradient_checkpointing = gradient_checkpointing,
531
+ gradient_checkpointing_kwargs = gradient_checkpointing_kwargs,
532
+ include_inputs_for_metrics = include_inputs_for_metrics,
533
+ eval_do_concat_batches = eval_do_concat_batches,
534
+ fp16_backend = fp16_backend,
535
+ push_to_hub_model_id = push_to_hub_model_id,
536
+ push_to_hub_organization = push_to_hub_organization,
537
+ push_to_hub_token = push_to_hub_token,
538
+ mp_parameters = mp_parameters,
539
+ auto_find_batch_size = auto_find_batch_size,
540
+ full_determinism = full_determinism,
541
+ torchdynamo = torchdynamo,
542
+ ray_scope = ray_scope,
543
+ ddp_timeout = ddp_timeout,
544
+ torch_compile = torch_compile,
545
+ torch_compile_backend = torch_compile_backend,
546
+ torch_compile_mode = torch_compile_mode,
547
+ include_tokens_per_second = include_tokens_per_second,
548
+ include_num_input_tokens_seen = include_num_input_tokens_seen,
549
+ neftune_noise_alpha = neftune_noise_alpha,
550
+ optim_target_modules = optim_target_modules,
551
+ batch_eval_metrics = batch_eval_metrics,
552
+ eval_on_start = eval_on_start,
553
+ use_liger_kernel = use_liger_kernel,
554
+ liger_kernel_config = liger_kernel_config,
555
+ eval_use_gather_object = eval_use_gather_object,
556
+ average_tokens_across_devices = average_tokens_across_devices,
557
+ model_init_kwargs = model_init_kwargs,
558
+ chat_template_path = chat_template_path,
559
+ disable_dropout = disable_dropout,
560
+ dataset_num_proc = dataset_num_proc,
561
+ eos_token = eos_token,
562
+ pad_token = pad_token,
563
+ max_length = max_length,
564
+ pad_to_multiple_of = pad_to_multiple_of,
565
+ center_rewards_coefficient = center_rewards_coefficient,
566
+ activation_offloading = activation_offloading,**kwargs)
567
+ self.vllm_sampling_params = vllm_sampling_params
568
+ self.unsloth_num_chunks = unsloth_num_chunks
569
+ self.max_seq_length = max_seq_length
570
+ pass
571
+
572
+ class _UnslothRewardTrainer(BaseTrainer):
573
+ """"""
574
+
575
+ _tag_names = ["trl", "reward-trainer"]
576
+ _name = "Reward"
577
+ _template_file = "rm_model_card.md"
578
+
579
+ def __init__(
580
+ self,
581
+ model: Union[str, PreTrainedModel],
582
+ args: Optional[RewardConfig] = None,
583
+ data_collator: Optional[DataCollator] = None,
584
+ train_dataset: Optional[Union[Dataset, IterableDataset]] = None,
585
+ eval_dataset: Optional[Union[Dataset, dict[str, Dataset]]] = None,
586
+ processing_class: Optional[PreTrainedTokenizerBase] = None,
587
+ compute_metrics: Optional[Callable[[EvalPrediction], dict]] = None,
588
+ callbacks: Optional[list[TrainerCallback]] = None,
589
+ optimizers: tuple[Optional[torch.optim.Optimizer], Optional[torch.optim.lr_scheduler.LambdaLR]] = (None, None),
590
+ optimizer_cls_and_kwargs: Optional[tuple[type[torch.optim.Optimizer], dict[str, Any]]] = None,
591
+ preprocess_logits_for_metrics: Optional[Callable[[torch.Tensor, torch.Tensor], torch.Tensor]] = None,
592
+ peft_config: Optional["PeftConfig"] = None,
593
+ ):
594
+ # Args
595
+ if args is None:
596
+ model_name = model if isinstance(model, str) else model.config._name_or_path
597
+ model_name = model_name.split("/")[-1]
598
+ args = RewardConfig(f"{model_name}-Reward")
599
+
600
+ # Model
601
+ model_init_kwargs = args.model_init_kwargs or {}
602
+ if isinstance(model, str):
603
+ model_id = model
604
+ dtype = model_init_kwargs.get("dtype")
605
+ if isinstance(dtype, torch.dtype) or dtype == "auto" or dtype is None:
606
+ pass # dtype is already a torch.dtype or "auto" or None
607
+ elif isinstance(dtype, str) and dtype in ["bfloat16", "float16", "float32"]:
608
+ model_init_kwargs["dtype"] = getattr(torch, dtype)
609
+ else:
610
+ raise ValueError(
611
+ "Invalid `dtype` passed to `RewardConfig`. Expected either 'auto' or a string representing "
612
+ f"a valid `torch.dtype` (e.g., 'float32'), but got {dtype}."
613
+ )
614
+ with suppress_from_pretrained_warning(transformers.modeling_utils.logger):
615
+ model = AutoModelForSequenceClassification.from_pretrained(model_id, num_labels=1, **model_init_kwargs)
616
+ else:
617
+ model_id = model.config._name_or_path
618
+ if args.model_init_kwargs is not None:
619
+ logger.warning(
620
+ "You passed `model_init_kwargs` to the `RewardConfig`, but your model is already instantiated. "
621
+ "The `model_init_kwargs` will be ignored."
622
+ )
623
+
624
+ # Processing class
625
+ if processing_class is None:
626
+ processing_class = AutoTokenizer.from_pretrained(model_id)
627
+
628
+ # Handle pad token for processors or tokenizers
629
+ if args.eos_token is not None:
630
+ eos_token = args.eos_token
631
+ eos_token_id = processing_class.convert_tokens_to_ids(eos_token)
632
+ if eos_token_id is None:
633
+ raise ValueError(
634
+ f"The specified `eos_token` ('{eos_token}') is not found in the vocabulary of the given "
635
+ f"`processing_class` ({processing_class.__class__.__name__}). Ensure that the `eos_token` exists "
636
+ "in the vocabulary before using it as an EOS token."
637
+ )
638
+ processing_class.eos_token_id = eos_token_id
639
+
640
+ if args.chat_template_path is not None:
641
+ if os.path.isfile(args.chat_template_path) and args.chat_template_path.endswith((".jinja", ".j2")):
642
+ with open(args.chat_template_path, encoding="utf-8") as chat_template_file:
643
+ processing_class.chat_template = chat_template_file.read()
644
+ added_tokens = []
645
+ else:
646
+ model, processing_class, added_tokens = clone_chat_template(
647
+ model, processing_class, args.chat_template_path
648
+ )
649
+ else:
650
+ added_tokens = []
651
+
652
+ # PEFT configuration and model wrapping
653
+ if False:
654
+ if added_tokens:
655
+ # Ensure that the added tokens are trainable
656
+ if peft_config.trainable_token_indices is None:
657
+ peft_config.trainable_token_indices = {"embed_tokens": added_tokens}
658
+ elif "embed_tokens" not in peft_config.trainable_token_indices:
659
+ peft_config.trainable_token_indices["embed_tokens"] = added_tokens
660
+ else:
661
+ peft_config.trainable_token_indices["embed_tokens"].extend(added_tokens)
662
+
663
+ # Ensure that the lm_head is trainable
664
+ if peft_config.modules_to_save is None or "lm_head" not in peft_config.modules_to_save:
665
+ logger.warning(
666
+ "Cloning chat template added new tokens to the tokenizer, but 'lm_head' is not in PEFT's "
667
+ "`modules_to_save`. As a result, the model may not learn to generate outputs with these new "
668
+ "tokens, leading to degraded generation quality. To fix this, add "
669
+ "`modules_to_save=['lm_head']` to your PEFT configuration."
670
+ )
671
+
672
+ if peft_config.modules_to_save is None:
673
+ peft_config.modules_to_save = ["lm_head"]
674
+ else:
675
+ peft_config.modules_to_save.append("lm_head")
676
+
677
+ if False:
678
+ model = prepare_peft_model(model, peft_config, args)
679
+
680
+ # Disable dropout in the model
681
+ if args.disable_dropout:
682
+ disable_dropout_in_model(model)
683
+
684
+ # Pad token [needed for SequenceClassification models]
685
+ # If not provided, use the one from the processing class or the eos token if the processing class does not have
686
+ # a pad token.
687
+ pad_token = args.pad_token or processing_class.pad_token or processing_class.eos_token
688
+ pad_token_id = processing_class.convert_tokens_to_ids(pad_token)
689
+ if pad_token_id is None:
690
+ raise ValueError(
691
+ f"The specified `pad_token` ('{pad_token}') is not found in the vocabulary of the given "
692
+ f"`processing_class` ({processing_class.__class__.__name__}). Ensure that the `pad_token` exists "
693
+ "in the vocabulary before using it as a padding token."
694
+ )
695
+ model.config.pad_token_id = pad_token_id
696
+ processing_class.pad_token_id = pad_token_id
697
+
698
+ # Data collator
699
+ if data_collator is None:
700
+ data_collator = DataCollatorForPreference(
701
+ pad_token_id=pad_token_id,
702
+ pad_to_multiple_of=args.pad_to_multiple_of,
703
+ )
704
+
705
+ # Dataset
706
+ train_dataset = self._prepare_dataset(train_dataset, processing_class, args, "train")
707
+ if eval_dataset is not None:
708
+ if isinstance(eval_dataset, dict):
709
+ eval_dataset = {
710
+ key: self._prepare_dataset(dataset, processing_class, args, key)
711
+ for key, dataset in eval_dataset.items()
712
+ }
713
+ else:
714
+ eval_dataset = self._prepare_dataset(eval_dataset, processing_class, args, "eval")
715
+
716
+ # Initialize the metrics
717
+ self._metrics = {"train": defaultdict(list), "eval": defaultdict(list)}
718
+ self._total_train_tokens = 0
719
+
720
+ # Initialize the Trainer. Parent class will handle:
721
+ # - DeepSpeed configuration [through create_accelerator_and_postprocess]
722
+ # - FSDP setup
723
+ # - Distributed training setup
724
+ # - Optimizer and scheduler creation
725
+
726
+ super().__init__(
727
+ model=model,
728
+ args=args,
729
+ data_collator=data_collator,
730
+ train_dataset=train_dataset,
731
+ eval_dataset=eval_dataset,
732
+ processing_class=processing_class,
733
+ compute_metrics=compute_metrics,
734
+ callbacks=callbacks,
735
+ optimizers=optimizers,
736
+ optimizer_cls_and_kwargs=optimizer_cls_and_kwargs,
737
+ preprocess_logits_for_metrics=preprocess_logits_for_metrics,
738
+ )
739
+
740
+ # During evaluation, Trainer calls compute_loss[] only if can_return_loss is True and label_names is empty.
741
+ self.can_return_loss = True
742
+ self.label_names = []
743
+
744
+ # Initialize activation offloading context
745
+ if self.args.activation_offloading:
746
+ self.maybe_activation_offload_context = get_act_offloading_ctx_manager(model=self.model)
747
+ else:
748
+ self.maybe_activation_offload_context = contextlib.nullcontext()
749
+
750
+ # Add tags for models that have been loaded with the correct transformers version
751
+ if hasattr(self.model, "add_model_tags"):
752
+ self.model.add_model_tags(self._tag_names)
753
+
754
+ self.aux_loss_enabled = getattr(model.config, "output_router_logits", False)
755
+
756
+ def _prepare_dataset(
757
+ self,
758
+ dataset: Union[Dataset, IterableDataset],
759
+ processing_class: PreTrainedTokenizerBase,
760
+ args: RewardConfig,
761
+ dataset_name: str,
762
+ ) -> Union[Dataset, IterableDataset]:
763
+ # Tabular backends like Arrow/Parquet insert `None` for mismatched keys in nested structures. Clean them from
764
+ # sampled data.
765
+ if isinstance(dataset, Dataset): # IterableDataset does not support `with_transform`
766
+ dataset = dataset.with_transform(remove_none_values)
767
+
768
+ # If the dataset is already preprocessed (tokenized), skip the processing steps.
769
+ column_names = list(next(iter(dataset)).keys())
770
+ is_processed = "chosen_input_ids" in column_names and "rejected_input_ids" in column_names
771
+
772
+ # Build the kwargs for the `map` function
773
+ map_kwargs = {}
774
+ if isinstance(dataset, Dataset): # IterableDataset does not support num_proc
775
+ map_kwargs["num_proc"] = args.dataset_num_proc
776
+
777
+ with PartialState().main_process_first():
778
+ if not is_processed:
779
+ # Add EOS token to the end of the sequences if needed
780
+ first_example = next(iter(dataset))
781
+ if not is_conversational(first_example):
782
+ if isinstance(dataset, Dataset): # `IterableDataset.map` does not support `desc`
783
+ map_kwargs["desc"] = f"Adding EOS to {dataset_name} dataset"
784
+
785
+ def add_eos(example, eos_token):
786
+ if not example["chosen"].endswith(eos_token):
787
+ example["chosen"] = example["chosen"] + eos_token
788
+ if "rejected" in example and not example["rejected"].endswith(eos_token):
789
+ example["rejected"] = example["rejected"] + eos_token
790
+ return example
791
+
792
+ dataset = dataset.map(
793
+ add_eos,
794
+ fn_kwargs={"eos_token": processing_class.eos_token},
795
+ **map_kwargs,
796
+ )
797
+
798
+ # Tokenize the dataset
799
+ if isinstance(dataset, Dataset): # `IterableDataset.map` does not support `desc`
800
+ map_kwargs["desc"] = f"Tokenizing {dataset_name} dataset"
801
+
802
+ def tokenize_fn(example, processing_class):
803
+ if "prompt" in example: # explicit prompt case
804
+ example["chosen"] = example["prompt"] + example["chosen"]
805
+ example["rejected"] = example["prompt"] + example["rejected"]
806
+
807
+ if is_conversational(example):
808
+ chosen_input_ids = processing_class.apply_chat_template(
809
+ example["chosen"],
810
+ tools=example.get("tools"),
811
+ **example.get("chat_template_kwargs", {}),
812
+ )
813
+ rejected_input_ids = processing_class.apply_chat_template(
814
+ example["rejected"],
815
+ tools=example.get("tools"),
816
+ **example.get("chat_template_kwargs", {}),
817
+ )
818
+ output = {"chosen_input_ids": chosen_input_ids, "rejected_input_ids": rejected_input_ids}
819
+ else:
820
+ output = {
821
+ "chosen_input_ids": processing_class(text=example["chosen"])["input_ids"],
822
+ "rejected_input_ids": processing_class(text=example["rejected"])["input_ids"],
823
+ }
824
+ return output
825
+
826
+ dataset = dataset.map(tokenize_fn, fn_kwargs={"processing_class": processing_class}, **map_kwargs)
827
+
828
+ # Filter samples that are longer than `max_length`
829
+ if args.max_length is not None:
830
+ if isinstance(dataset, Dataset): # `IterableDataset.map` does not support `desc`
831
+ map_kwargs["desc"] = f"Filtering {dataset_name} >{args.max_length} tokens"
832
+ dataset = dataset.filter(
833
+ lambda example: len(example["chosen_input_ids"]) <= args.max_length
834
+ and len(example["rejected_input_ids"]) <= args.max_length,
835
+ **map_kwargs,
836
+ )
837
+
838
+ return dataset
839
+
840
+ def _set_signature_columns_if_needed(self):
841
+ # If `self.args.remove_unused_columns` is True, non-signature columns are removed.
842
+ # By default, this method sets `self._signature_columns` to the model's expected inputs (usually, "input_ids"
843
+ # and "attention_mask").
844
+ if self._signature_columns is None:
845
+ self._signature_columns = ["chosen_input_ids", "rejected_input_ids", "margin"]
846
+
847
+ def compute_loss(
848
+ self,
849
+ model: nn.Module,
850
+ inputs: dict[str, Union[torch.Tensor, Any]],
851
+ return_outputs: bool = False,
852
+ num_items_in_batch: Optional[torch.Tensor] = None,
853
+ ):
854
+ """
855
+ Compute training loss and additionally compute token accuracies
856
+ """
857
+ mode = "train" if self.model.training else "eval"
858
+
859
+ # If not set, defaults from model config and may warn since cache isn't compatible with gradient checkpointing
860
+ inputs["use_cache"] = False
861
+ outputs = model(**inputs)
862
+
863
+ # Split the rewards into chosen and rejected
864
+ rewards_chosen, rewards_rejected = torch.chunk(outputs.logits.squeeze(-1), chunks=2)
865
+
866
+ # Calculate loss, optionally modulate with margin
867
+ if "margin" in inputs:
868
+ loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected - inputs["margin"]).mean()
869
+ else:
870
+ loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected).mean()
871
+
872
+ if self.args.center_rewards_coefficient is not None:
873
+ loss += self.args.center_rewards_coefficient * torch.mean((rewards_chosen + rewards_rejected) ** 2)
874
+
875
+ if mode == "train":
876
+ num_tokens_in_batch = self.accelerator.gather_for_metrics(inputs["attention_mask"].sum()).sum().item()
877
+ self._total_train_tokens += num_tokens_in_batch
878
+ self._metrics[mode]["num_tokens"] = [self._total_train_tokens]
879
+
880
+ # Compute min, mean, max, accuracy and margin
881
+ with torch.no_grad():
882
+ all_rewards = self.accelerator.gather(outputs.logits)
883
+ self._metrics[mode]["min_reward"].append(all_rewards.min().item())
884
+ self._metrics[mode]["mean_reward"].append(all_rewards.mean().item())
885
+ self._metrics[mode]["max_reward"].append(all_rewards.max().item())
886
+
887
+ mean_accuracy = (rewards_chosen > rewards_rejected).float().mean()
888
+ mean_accuracy = self.accelerator.gather_for_metrics(mean_accuracy).mean().item()
889
+ self._metrics[mode]["accuracy"].append(mean_accuracy)
890
+
891
+ mean_margin = (rewards_chosen - rewards_rejected).mean()
892
+ mean_margin = self.accelerator.gather_for_metrics(mean_margin).mean()
893
+ self._metrics[mode]["margin"].append(mean_margin.item())
894
+
895
+ return (loss, outputs) if return_outputs else loss
896
+
897
+ # Override training step to add activation offloading context.
898
+ def training_step(self, *args, **kwargs):
899
+ with self.maybe_activation_offload_context:
900
+ return super().training_step(*args, **kwargs)
901
+
902
+ def log(self, logs: dict[str, float], start_time: Optional[float] = None) -> None:
903
+ mode = "train" if self.model.training else "eval"
904
+ metrics = {key: sum(val) / len(val) for key, val in self._metrics[mode].items()} # average the metrics
905
+
906
+ # This method can be called both in training and evaluation. When called in evaluation, the keys in `logs`
907
+ # start with "eval_". We need to add the prefix "eval_" to the keys in `metrics` to match the format.
908
+ if mode == "eval":
909
+ metrics = {f"eval_{key}": val for key, val in metrics.items()}
910
+
911
+ logs.update(metrics)
912
+ super().log(logs, start_time)
913
+ self._metrics[mode].clear()
914
+
915
+ # Ensure the model card is saved along with the checkpoint
916
+ def _save_checkpoint(self, model, trial):
917
+ if self.args.hub_model_id is None:
918
+ model_name = Path(self.args.output_dir).name
919
+ else:
920
+ model_name = self.args.hub_model_id.split("/")[-1]
921
+ self.create_model_card(model_name=model_name)
922
+ super()._save_checkpoint(model, trial)
923
+ class UnslothRewardTrainer(_UnslothRewardTrainer):
924
+ """
925
+
926
+ Trainer for Outcome-supervised Reward Models (ORM).
927
+
928
+ This class is a wrapper around the [`~transformers.Trainer`] class and inherits all of its attributes and methods.
929
+
930
+ Example:
931
+
932
+ ```python
933
+ from trl import RewardTrainer
934
+ from datasets import load_dataset
935
+
936
+ dataset = load_dataset("trl-lib/ultrafeedback_binarized", split="train")
937
+
938
+ trainer = RewardTrainer(model="Qwen/Qwen2.5-0.5B-Instruct", train_dataset=dataset)
939
+ trainer.train()
940
+ ```
941
+
942
+ Args:
943
+ model (`Union[str, PreTrainedModel]`):
944
+ Model to be trained. Can be either:
945
+
946
+ - A string, being the *model id* of a pretrained model hosted inside a model repo on huggingface.co, or a
947
+ path to a *directory* containing model weights saved using
948
+ [`~transformers.PreTrainedModel.save_pretrained`], e.g., `'./my_model_directory/'`. The model is loaded
949
+ using `AutoModelForSequenceClassification.from_pretrained` with the keyword arguments in
950
+ `args.model_init_kwargs`.
951
+ - A sequence classification [`~transformers.PreTrainedModel`] object.
952
+ args ([`RewardConfig`], *optional*):
953
+ Configuration for this trainer. If `None`, a default configuration is used.
954
+ data_collator ([`~transformers.DataCollator`], *optional*):
955
+ Function to use to form a batch from a list of elements of the processed `train_dataset` or `eval_dataset`.
956
+ Will default to [`~trainer.reward_trainer.DataCollatorForPreference`].
957
+ train_dataset ([`~datasets.Dataset`] or [`~datasets.IterableDataset`]):
958
+ Dataset to use for training. This trainer supports [preference](#preference) type (both implicit and
959
+ explicit prompt). The format of the samples can be either:
960
+
961
+ - [Standard](dataset_formats#standard): Each sample contains plain text.
962
+ - [Conversational](dataset_formats#conversational): Each sample contains structured messages (e.g., role
963
+ and content).
964
+
965
+ The trainer also supports processed datasets (tokenized) as long as they contain an `chosen_input_ids` and
966
+ `rejected_input_ids` fields.
967
+ eval_dataset ([`~datasets.Dataset`], [`~datasets.IterableDataset`] or `dict[str, Union[Dataset, IterableDataset]]`):
968
+ Dataset to use for evaluation. It must meet the same requirements as `train_dataset`.
969
+ processing_class ([`~transformers.PreTrainedTokenizerBase`], *optional*):
970
+ Tokenizer used to process the data. If `None`, the tokenizer is loaded from the model's name with
971
+ [`~transformers.AutoTokenizer.from_pretrained`]. A padding token, `processing_class.pad_token`, must be
972
+ set. If the processing class has not set a padding token, `processing_class.eos_token` will be used as the
973
+ default.
974
+ compute_metrics (`Callable[[EvalPrediction], dict]`, *optional*):
975
+ The function that will be used to compute metrics at evaluation. Must take a
976
+ [`~transformers.EvalPrediction`] and return a dictionary string to metric values. When passing
977
+ [`RewardConfig`] with `batch_eval_metrics` set to `True`, your `compute_metrics` function must take a
978
+ boolean `compute_result` argument. This will be triggered after the last eval batch to signal that the
979
+ function needs to calculate and return the global summary statistics rather than accumulating the
980
+ batch-level statistics.
981
+ callbacks (list of [`~transformers.TrainerCallback`], *optional*):
982
+ List of callbacks to customize the training loop. Will add those to the list of default callbacks detailed
983
+ in [here](https://huggingface.co/docs/transformers/main_classes/callback).
984
+
985
+ If you want to remove one of the default callbacks used, use the [`~transformers.Trainer.remove_callback`]
986
+ method.
987
+ optimizers (`tuple[Optional[torch.optim.Optimizer], Optional[torch.optim.lr_scheduler.LambdaLR]]`, *optional*, defaults to `(None, None)`):
988
+ A tuple containing the optimizer and the scheduler to use. Will default to an instance of `AdamW` on your
989
+ model and a scheduler given by [`~transformers.get_linear_schedule_with_warmup`] controlled by `args`.
990
+ optimizer_cls_and_kwargs (`tuple[Type[torch.optim.Optimizer], Dict[str, Any]]`, *optional*):
991
+ A tuple containing the optimizer class and keyword arguments to use. Overrides `optim` and `optim_args` in
992
+ `args`. Incompatible with the `optimizers` argument.
993
+
994
+ Unlike `optimizers`, this argument avoids the need to place model parameters on the correct devices before
995
+ initializing the Trainer.
996
+ preprocess_logits_for_metrics (`Callable[[torch.Tensor, torch.Tensor], torch.Tensor]`, *optional*):
997
+ A function that preprocess the logits right before caching them at each evaluation step. Must take two
998
+ tensors, the logits and the labels, and return the logits once processed as desired. The modifications made
999
+ by this function will be reflected in the predictions received by `compute_metrics`.
1000
+
1001
+ Note that the labels (second parameter) will be `None` if the dataset does not have them.
1002
+ peft_config ([`~peft.PeftConfig`], *optional*):
1003
+ PEFT configuration used to wrap the model. If `None`, the model is not wrapped. Note that if the loaded
1004
+ model is a causal LM, it's highly recommended to set `modules_to_save=["score"]` in the PEFT configuration
1005
+ to ensure that the reward head is properly trained.
1006
+
1007
+ """
1008
+ def __init__(
1009
+ self,
1010
+ model,
1011
+ args = None,
1012
+ data_collator = None,
1013
+ train_dataset = None,
1014
+ eval_dataset = None,
1015
+ processing_class = None,
1016
+ compute_metrics = None,
1017
+ callbacks = None,
1018
+ optimizer_cls_and_kwargs = None,
1019
+ preprocess_logits_for_metrics = None,
1020
+ peft_config = None,
1021
+ **kwargs
1022
+ ):
1023
+ if args is None: args = UnslothRewardConfig()
1024
+ use_bf16 = getattr(args, 'bf16', False)
1025
+ if type(use_bf16) is not bool: use_bf16 = False
1026
+ use_fp16 = getattr(args, 'fp16', False)
1027
+ if type(use_fp16) is not bool: use_fp16 = False
1028
+ force_float32 = False
1029
+ full_finetuning = os.environ.get('UNSLOTH_ENABLE_FULL_FINETUNING', '0') == '1'
1030
+ if not full_finetuning and (os.environ.get('UNSLOTH_FORCE_FLOAT32', '0') == '1'):
1031
+ print('Unsloth: Switching to float32 training since model cannot work with float16')
1032
+ force_float32 = True
1033
+ mixed_precision_dtype = os.environ.get('UNSLOTH_MIXED_PRECISION', 'float32')
1034
+ dtype = getattr(model.config, 'dtype', None) or getattr(model.config, 'torch_dtype', None)
1035
+ if dtype is None: dtype = model.get_input_embeddings().weight.dtype
1036
+ from unsloth_zoo.utils import _get_dtype
1037
+ dtype = _get_dtype(dtype)
1038
+ float16 = dtype == torch.float16
1039
+ if not force_float32 and (float16 and use_bf16): raise TypeError('Unsloth: Model is in float16 precision but you want to use bfloat16 precision. Set fp16 to `True` and bf16 to `False`')
1040
+ if not force_float32 and (not float16 and use_fp16): raise TypeError('Unsloth: Model is in bfloat16 precision but you want to use float16 precision. Set fp16 to `False` and bf16 to `True`')
1041
+ if force_float32:
1042
+ # Forced float32 training
1043
+ args.fp16 = False
1044
+ args.bf16 = False
1045
+ os.environ['ACCELERATE_MIXED_PRECISION'] = 'no'
1046
+ if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'
1047
+ # args.mixed_precision is a new argument which needs to be set now
1048
+ elif (not use_bf16 and not use_fp16) and mixed_precision_dtype == 'float32':
1049
+ # Mixed precision training
1050
+ args.fp16 = float16
1051
+ args.bf16 = not float16
1052
+ os.environ['ACCELERATE_MIXED_PRECISION'] = 'fp16' if float16 else 'bf16'
1053
+ if hasattr(args, 'mixed_precision'): args.mixed_precision = 'fp16' if float16 else 'bf16'
1054
+ # args.mixed_precision is a new argument which needs to be set now
1055
+ elif mixed_precision_dtype == 'bfloat16':
1056
+ # Both False since bfloat16 full finetuning doesn't do any autocasting.
1057
+ args.fp16 = False
1058
+ args.bf16 = False
1059
+ os.environ['ACCELERATE_MIXED_PRECISION'] = 'no'
1060
+ if hasattr(args, 'mixed_precision'): args.mixed_precision = 'no'
1061
+ # args.mixed_precision is a new argument which needs to be set now
1062
+
1063
+ if getattr(args, 'eval_dataset', None) is not None and getattr(args, 'eval_strategy', 'no') == 'no':
1064
+ args.eval_strategy = 'steps'
1065
+ if getattr(args, 'eval_steps', None) is None: args.eval_steps = 0.1
1066
+ ga_steps = getattr(args, 'gradient_accumulation_steps', None)
1067
+ if ga_steps is not None and ga_steps > 1:
1068
+ from transformers import __version__ as transformers_version
1069
+ if Version(transformers_version) <= Version('4.45.2'):
1070
+ print('**** Unsloth: Please use our fixed gradient_accumulation_steps by updating transformers, TRL and Unsloth!\n'
1071
+ '`pip install --upgrade --no-cache-dir --force-reinstall --no-deps unsloth transformers trl unsloth_zoo`')
1072
+ if getattr(args, 'eval_strategy', 'no') != 'no':
1073
+ eval_bsz = getattr(args, 'per_device_eval_batch_size', 8)
1074
+ if eval_bsz == 8 and args.per_device_train_batch_size < eval_bsz: args.per_device_eval_batch_size = args.per_device_train_batch_size
1075
+ if getattr(args, 'eval_accumulation_steps', None) is None and ga_steps is not None: args.eval_accumulation_steps = ga_steps
1076
+ fp16_full_eval = getattr(args, 'fp16_full_eval', False)
1077
+ if type(fp16_full_eval) is not bool: fp16_full_eval = False
1078
+ bf16_full_eval = getattr(args, 'bf16_full_eval', False)
1079
+ if type(bf16_full_eval) is not bool: bf16_full_eval = False
1080
+ if args.fp16 and bf16_full_eval: args.bf16_full_eval = False; args.fp16_full_eval = True
1081
+ if args.bf16 and fp16_full_eval: args.bf16_full_eval = True; args.fp16_full_eval = False
1082
+ if force_float32:
1083
+ args.bf16_full_eval = False
1084
+ args.fp16_full_eval = False
1085
+ elif os.environ.get('UNSLOTH_MIXED_PRECISION', 'float32') == 'bfloat16':
1086
+ args.bf16_full_eval = True
1087
+ args.fp16_full_eval = False
1088
+ elif not bf16_full_eval and not fp16_full_eval:
1089
+ args.bf16_full_eval = args.bf16
1090
+ args.fp16_full_eval = args.fp16
1091
+ _output_logits = False
1092
+ if locals().get('compute_metrics', None) is not None: _output_logits = True
1093
+ if locals().get('preprocess_logits_for_metrics', None) is not None: _output_logits = True
1094
+ if _output_logits:
1095
+ os.environ['UNSLOTH_RETURN_LOGITS'] = '1'
1096
+ if 'max_seq_length' not in locals() and not hasattr(args, 'max_seq_length'):
1097
+ pass
1098
+ else:
1099
+ model_max_seq_length = getattr(model, 'max_seq_length', None)
1100
+ args_max_seq_length = getattr(args, 'max_seq_length', None)
1101
+ if args_max_seq_length is None and model_max_seq_length is not None:
1102
+ max_seq_length = model.max_seq_length
1103
+ if hasattr(args, 'max_seq_length'): args.max_seq_length = max_seq_length
1104
+ if model is not None and hasattr(model, 'for_training'):
1105
+ model.for_training(use_gradient_checkpointing=getattr(args, 'gradient_checkpointing', True))
1106
+ if 'tokenizer' in locals() and hasattr(tokenizer, 'padding_side'): tokenizer.padding_side = 'right'
1107
+ if 'processing_class' in locals():
1108
+ if hasattr(processing_class, 'padding_side'): processing_class.padding_side = 'right'
1109
+ if hasattr(processing_class, 'tokenizer') and hasattr(processing_class.tokenizer, 'padding_side'): processing_class.tokenizer.padding_side = 'right'
1110
+ __tokenizer = processing_class if 'processing_class' in locals() else tokenizer
1111
+ from unsloth_zoo.vision_utils import UnslothVisionDataCollator
1112
+ if not isinstance(data_collator, UnslothVisionDataCollator):
1113
+ if isinstance(data_collator, DataCollatorForSeq2Seq) and 'labels' not in train_dataset.column_names:
1114
+ data_collator = TransformersDataCollatorForLanguageModeling(
1115
+ __tokenizer,
1116
+ mlm = False,
1117
+ mlm_probability = 0.0,
1118
+ pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),
1119
+ )
1120
+ elif isinstance(data_collator, TransformersDataCollatorForLanguageModeling) and 'labels' in train_dataset.column_names:
1121
+ data_collator = DataCollatorForSeq2Seq(
1122
+ __tokenizer,
1123
+ pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),
1124
+ )
1125
+ else:
1126
+ if hasattr(args, 'remove_unused_columns'): args.remove_unused_columns = False
1127
+ if hasattr(args, 'dataset_text_field'): args.dataset_text_field = ''
1128
+ if hasattr(args, 'dataset_kwargs'): args.dataset_kwargs = {'skip_prepare_dataset': True}
1129
+ if not isinstance(data_collator, UnslothVisionDataCollator):
1130
+ if not hasattr(__tokenizer, 'pad') and hasattr(__tokenizer, 'tokenizer'):
1131
+ if isinstance(data_collator, DataCollatorForSeq2Seq):
1132
+ data_collator = DataCollatorForSeq2Seq(
1133
+ __tokenizer.tokenizer,
1134
+ pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),
1135
+ )
1136
+ else:
1137
+ data_collator = TransformersDataCollatorForLanguageModeling(
1138
+ __tokenizer.tokenizer,
1139
+ mlm = False,
1140
+ mlm_probability = 0.0,
1141
+ pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),
1142
+ )
1143
+ other_metrics = []
1144
+
1145
+ from unsloth_zoo.logging_utils import PatchRLStatistics
1146
+ PatchRLStatistics('reward_trainer', other_metrics)
1147
+
1148
+ # [TODO] Fix up DataParallel multiplying batch sizes
1149
+ # [TODO] DDP works, but DP seems to not work? [TODO]
1150
+ if getattr(args, "parallel_mode", None) == ParallelMode.NOT_DISTRIBUTED and args.n_gpu > 1:
1151
+ if getattr(args, "_n_gpu", 1) != 1:
1152
+ args._n_gpu = 1
1153
+ if "model" in locals() and hasattr(model, "for_training"):
1154
+ model.for_training(use_gradient_checkpointing=getattr(args, 'gradient_checkpointing', True))
1155
+ super().__init__(
1156
+ model = model,
1157
+ args = args,
1158
+ data_collator = data_collator,
1159
+ train_dataset = train_dataset,
1160
+ eval_dataset = eval_dataset,
1161
+ processing_class = processing_class,
1162
+ compute_metrics = compute_metrics,
1163
+ callbacks = callbacks,
1164
+ optimizer_cls_and_kwargs = optimizer_cls_and_kwargs,
1165
+ preprocess_logits_for_metrics = preprocess_logits_for_metrics,
1166
+ peft_config = peft_config,**kwargs)
1167
+ if "model" in locals() and hasattr(model, "for_inference"):
1168
+ model.for_inference()
1169
+ if hasattr(self, 'neftune_hook_handle'):
1170
+ self.neftune_hook_handle.remove()
1171
+ if hasattr(self, 'neftune_hook_handle'): del self.neftune_hook_handle
1172
+ if getattr(args, 'neftune_noise_alpha', None) is not None:
1173
+ model.get_input_embeddings().neftune_noise_alpha = self.neftune_noise_alpha
1174
+ pass
1175
+ if hasattr(self, 'accelerator'):
1176
+ scaler = self.accelerator.scaler
1177
+ current_model = model
1178
+ while hasattr(current_model, 'model'):
1179
+ current_model.accelerator_scaler = scaler
1180
+ current_model = current_model.model
1181
+ current_model.accelerator_scaler = scaler
1182
+ pass
1183
+ if hasattr(self, 'train'):
1184
+ self.train = MethodType(prepare_for_training_mode(self.__class__.train), self)
1185
+ pass
1186
+
1187
+ pass
1188
+
1189
+
1190
+ if hasattr(logger, "addFilter"):
1191
+ import logging
1192
+ class HideLoggingMessage(logging.Filter):
1193
+ def __init__(self, text): self.text = text
1194
+ def filter(self, x): return not (self.text in x.getMessage())
1195
+ pass
1196
+ logger.addFilter(HideLoggingMessage("`use_cache=True`"))
1197
+