| 1 | # Copyright 2025 the LlamaFactory team. |
| 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 | from dataclasses import asdict, dataclass, field |
| 16 | from typing import Any |
| 17 | |
| 18 | from transformers import GenerationConfig |
| 19 | |
| 20 | |
| 21 | @dataclass |
| 22 | class GeneratingArguments: |
| 23 | r"""Arguments pertaining to specify the decoding parameters.""" |
| 24 | |
| 25 | do_sample: bool = field( |
| 26 | default=True, |
| 27 | metadata={"help": "Whether or not to use sampling, use greedy decoding otherwise."}, |
| 28 | ) |
| 29 | temperature: float = field( |
| 30 | default=0.95, |
| 31 | metadata={"help": "The value used to modulate the next token probabilities."}, |
| 32 | ) |
| 33 | top_p: float = field( |
| 34 | default=0.7, |
| 35 | metadata={ |
| 36 | "help": ( |
| 37 | "The smallest set of most probable tokens with probabilities that add up to top_p or higher are kept." |
| 38 | ) |
| 39 | }, |
| 40 | ) |
| 41 | top_k: int = field( |
| 42 | default=50, |
| 43 | metadata={"help": "The number of highest probability vocabulary tokens to keep for top-k filtering."}, |
| 44 | ) |
| 45 | num_beams: int = field( |
| 46 | default=1, |
| 47 | metadata={"help": "Number of beams for beam search. 1 means no beam search."}, |
| 48 | ) |
| 49 | max_length: int = field( |
| 50 | default=1024, |
| 51 | metadata={"help": "The maximum length the generated tokens can have. It can be overridden by max_new_tokens."}, |
| 52 | ) |
| 53 | max_new_tokens: int = field( |
| 54 | default=1024, |
| 55 | metadata={"help": "The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt."}, |
| 56 | ) |
| 57 | repetition_penalty: float = field( |
| 58 | default=1.0, |
| 59 | metadata={"help": "The parameter for repetition penalty. 1.0 means no penalty."}, |
| 60 | ) |
| 61 | length_penalty: float = field( |
| 62 | default=1.0, |
| 63 | metadata={"help": "Exponential penalty to the length that is used with beam-based generation."}, |
| 64 | ) |
| 65 | skip_special_tokens: bool = field( |
| 66 | default=True, |
| 67 | metadata={"help": "Whether or not to remove special tokens in the decoding."}, |
| 68 | ) |
| 69 | |
| 70 | def to_dict(self, obey_generation_config: bool = False) -> dict[str, Any]: |
| 71 | args = asdict(self) |
| 72 | if args.get("max_new_tokens", -1) > 0: |
| 73 | args.pop("max_length", None) |
| 74 | else: |
| 75 | args.pop("max_new_tokens", None) |
| 76 | |
| 77 | if obey_generation_config: |
| 78 | generation_config = GenerationConfig() |
| 79 | for key in list(args.keys()): |
| 80 | if not hasattr(generation_config, key): |
| 81 | args.pop(key) |
| 82 | |
| 83 | return args |