|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. 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 | + |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from paddleformers.trainer import TrainingArguments |
| 20 | +from paddleformers.trainer.trainer_utils import IntervalStrategy |
| 21 | +from paddleformers.trainer.utils.doc import add_start_docstrings |
| 22 | +from paddleformers.transformers.configuration_utils import llmmetaclass |
| 23 | +from paddleformers.trl import DataConfig |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +@llmmetaclass |
| 28 | +@add_start_docstrings(TrainingArguments.__doc__) |
| 29 | +class DPOTrainingArguments(TrainingArguments): |
| 30 | + """DPOTrainingArguments""" |
| 31 | + |
| 32 | + unified_checkpoint: bool = field( |
| 33 | + default=True, |
| 34 | + metadata={"help": "Enable fused linear grad add strategy."}, |
| 35 | + ) |
| 36 | + unified_checkpoint_config: Optional[str] = field( |
| 37 | + default="", |
| 38 | + metadata={"help": "Configs to unify hybrid parallel checkpoint.\n"}, |
| 39 | + ) |
| 40 | + autotuner_benchmark: bool = field( |
| 41 | + default=False, |
| 42 | + metadata={"help": "Whether to run benchmark by autotuner. True for from_scratch."}, |
| 43 | + ) |
| 44 | + benchmark: bool = field( |
| 45 | + default=False, |
| 46 | + metadata={"help": "Whether to run benchmark by autotuner. True for from_scratch."}, |
| 47 | + ) |
| 48 | + use_intermediate_api: bool = field( |
| 49 | + default=False, |
| 50 | + metadata={"help": "Flag indicating whether to use the intermediate API for model."}, |
| 51 | + ) |
| 52 | + num_hidden_layers: int = field(default=2, metadata={"help": "The number of hidden layers in the network model."}) |
| 53 | + |
| 54 | + def __post_init__(self): |
| 55 | + super().__post_init__() |
| 56 | + if self.autotuner_benchmark: |
| 57 | + self.num_train_epochs = 1 |
| 58 | + self.max_steps = 5 |
| 59 | + self.do_train = True |
| 60 | + self.do_export = False |
| 61 | + self.do_predict = False |
| 62 | + self.do_eval = False |
| 63 | + self.overwrite_output_dir = True |
| 64 | + self.load_best_model_at_end = False |
| 65 | + self.report_to = [] |
| 66 | + self.save_strategy = IntervalStrategy.NO |
| 67 | + self.evaluation_strategy = IntervalStrategy.NO |
| 68 | + if not self.disable_tqdm: |
| 69 | + self.logging_steps = 1 |
| 70 | + self.logging_strategy = IntervalStrategy.STEPS |
| 71 | + if self.benchmark: |
| 72 | + self.do_train = True |
| 73 | + self.do_export = False |
| 74 | + self.do_predict = False |
| 75 | + self.do_eval = False |
| 76 | + self.overwrite_output_dir = True |
| 77 | + self.load_best_model_at_end = False |
| 78 | + self.save_strategy = IntervalStrategy.NO |
| 79 | + self.evaluation_strategy = IntervalStrategy.NO |
| 80 | + if not self.disable_tqdm: |
| 81 | + self.logging_steps = 1 |
| 82 | + self.logging_strategy = IntervalStrategy.STEPS |
| 83 | + if self.max_steps > 0: |
| 84 | + self.num_train_epochs = 1 |
| 85 | + |
| 86 | + |
| 87 | +@dataclass |
| 88 | +class DPOConfig: |
| 89 | + """DPOConfig""" |
| 90 | + |
| 91 | + beta: float = field(default=0.1, metadata={"help": "the beta parameter for DPO loss"}) |
| 92 | + simpo_gamma: float = field(default=0.5, metadata={"help": "the gamma parameter for SimPO loss"}) |
| 93 | + label_smoothing: float = field(default=0.0, metadata={"help": "label_smoothing ratio"}) |
| 94 | + loss_type: str = field(default="sigmoid", metadata={"help": "DPO loss type"}) |
| 95 | + pref_loss_ratio: float = field(default=1.0, metadata={"help": "DPO loss ratio"}) |
| 96 | + sft_loss_ratio: float = field(default=0.0, metadata={"help": "SFT loss ratio"}) |
| 97 | + dpop_lambda: float = field(default=50, metadata={"help": "dpop_lambda"}) |
| 98 | + ref_model_update_steps: int = field(default=-1, metadata={"help": "Update ref model state dict "}) |
| 99 | + reference_free: bool = field(default=False, metadata={"help": "No reference model."}) |
| 100 | + lora: bool = field(default=False, metadata={"help": "Use LoRA model."}) |
| 101 | + |
| 102 | + |
| 103 | +@dataclass |
| 104 | +class DPODataArgument(DataConfig): |
| 105 | + """DataArgument""" |
| 106 | + |
| 107 | + max_seq_len: int = field(default=4096, metadata={"help": "Maximum sequence length."}) |
| 108 | + max_prompt_len: int = field(default=2048, metadata={"help": "Maximum prompt length."}) |
| 109 | + num_samples_each_epoch: int = field(default=6000000, metadata={"help": "Number of sample per training epoch."}) |
| 110 | + buffer_size: int = field(default=1000, metadata={"help": "Preloading buffer capacity."}) |
| 111 | + mask_out_eos_token: bool = field(default=True, metadata={"help": "EOS loss masking."}) |
| 112 | + |
| 113 | + |
| 114 | +@dataclass |
| 115 | +class DPOModelArgument: |
| 116 | + """ModelArgument""" |
| 117 | + |
| 118 | + model_name_or_path: str = field( |
| 119 | + default=None, metadata={"help": "Pretrained model name or path to local directory."} |
| 120 | + ) |
| 121 | + tokenizer_name_or_path: Optional[str] = field( |
| 122 | + default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} |
| 123 | + ) |
| 124 | + download_hub: str = field( |
| 125 | + default="aistudio", |
| 126 | + metadata={ |
| 127 | + "help": "The source for model downloading, options include `huggingface`, `aistudio`, `modelscope`, default `aistudio`" |
| 128 | + }, |
| 129 | + ) |
| 130 | + flash_mask: bool = field(default=False, metadata={"help": "Whether to use flash mask in flash attention."}) |
| 131 | + weight_quantize_algo: str = field( |
| 132 | + default=None, |
| 133 | + metadata={"help": "Model weight quantization algorithm including 'nf4'(qlora), 'weight_only_int8'."}, |
| 134 | + ) |
| 135 | + fuse_attention_qkv: bool = field( |
| 136 | + default=None, |
| 137 | + metadata={"help": "whether to fuse attention qkv"}, |
| 138 | + ) |
| 139 | + fuse_attention_ffn: bool = field( |
| 140 | + default=None, |
| 141 | + metadata={"help": "whether to fuse first up and gate proj in mlp block"}, |
| 142 | + ) |
| 143 | + use_sparse_head_and_loss_fn: bool = field( |
| 144 | + default=True, |
| 145 | + metadata={"help": "Whether to use sparse indexing for loss calculation."}, |
| 146 | + ) |
| 147 | + use_fused_head_and_loss_fn: bool = field( |
| 148 | + default=True, |
| 149 | + metadata={"help": "Whether to use fused kernel to calculate lm head and loss."}, |
| 150 | + ) |
| 151 | + use_attn_mask_startend_row_indices: bool = field( |
| 152 | + default=True, |
| 153 | + metadata={"help": "Sparse attention mode."}, |
| 154 | + ) |
| 155 | + |
| 156 | + # LoRA |
| 157 | + lora_rank: int = field(default=8, metadata={"help": "Lora rank."}) |
| 158 | + lora_path: str = field(default=None, metadata={"help": "Initialize lora state dict."}) |
| 159 | + rslora: bool = field(default=False, metadata={"help": "Whether to use RsLoRA"}) |
| 160 | + lora_plus_scale: float = field(default=1.0, metadata={"help": "Lora B scale in LoRA+ technique"}) |
| 161 | + lora_alpha: int = field(default=-1, metadata={"help": "lora_alpha"}) |
| 162 | + rslora_plus: bool = field(default=False, metadata={"help": "Strengthen lora performance"}) |
| 163 | + use_quick_lora: bool = field(default=True, metadata={"help": "quick lora"}) |
0 commit comments