|
| 1 | +# Copyright 2025 Bytedance Ltd. and/or its affiliates |
| 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 dataclass, field |
| 16 | +from typing import Any, Optional |
| 17 | + |
| 18 | +from omegaconf import MISSING |
| 19 | +from transformers import AutoConfig |
| 20 | + |
| 21 | +from verl.base_config import BaseConfig |
| 22 | +from verl.utils import hf_processor, hf_tokenizer |
| 23 | +from verl.utils.fs import copy_to_local |
| 24 | +from verl.utils.model import get_generation_config, update_model_config |
| 25 | + |
| 26 | +__all__ = ["HFModelConfig"] |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class HFModelConfig(BaseConfig): |
| 31 | + # note that we separate model_path, model_config_path and tokenizer_path in case they are different |
| 32 | + _mutable_fields = { |
| 33 | + "hf_config_path", |
| 34 | + "tokenizer_path", |
| 35 | + "hf_config", |
| 36 | + "generation_config", |
| 37 | + "tokenizer", |
| 38 | + "processor", |
| 39 | + "local_path", |
| 40 | + } |
| 41 | + |
| 42 | + path: str = MISSING |
| 43 | + local_path: Optional[str] = None |
| 44 | + hf_config_path: Optional[str] = None |
| 45 | + tokenizer_path: Optional[str] = None |
| 46 | + |
| 47 | + hf_config: Any = None |
| 48 | + generation_config: Any = None |
| 49 | + tokenizer: Any = None |
| 50 | + processor: Any = None |
| 51 | + |
| 52 | + # whether to use shared memory |
| 53 | + use_shm: bool = False |
| 54 | + trust_remote_code: bool = False |
| 55 | + |
| 56 | + # custom chat template for the model |
| 57 | + custom_chat_template: Optional[str] = None |
| 58 | + |
| 59 | + external_lib: Optional[str] = None |
| 60 | + |
| 61 | + override_config: dict = field(default_factory=dict) |
| 62 | + |
| 63 | + enable_gradient_checkpointing: bool = True |
| 64 | + enable_activation_offload: bool = False |
| 65 | + |
| 66 | + use_remove_padding: bool = False |
| 67 | + |
| 68 | + # lora related. We may setup a separate config later |
| 69 | + lora_rank: int = 0 |
| 70 | + lora_alpha: int = 16 |
| 71 | + target_modules: Optional[str] = "all-linear" |
| 72 | + |
| 73 | + exclude_modules: Optional[str] = None |
| 74 | + use_liger: bool = False |
| 75 | + |
| 76 | + use_fused_kernels: bool = False |
| 77 | + fused_kernel_options: dict = field(default_factory=dict) |
| 78 | + |
| 79 | + def __post_init__(self): |
| 80 | + if self.hf_config_path is None: |
| 81 | + self.hf_config_path = self.path |
| 82 | + if self.tokenizer_path is None: |
| 83 | + self.tokenizer_path = self.path |
| 84 | + |
| 85 | + # constuct tokenizer |
| 86 | + self.local_path = copy_to_local(self.path, use_shm=self.use_shm) |
| 87 | + self.tokenizer = hf_tokenizer(self.local_path, trust_remote_code=self.trust_remote_code) |
| 88 | + self.processor = hf_processor(self.local_path, trust_remote_code=self.trust_remote_code) |
| 89 | + |
| 90 | + self.generation_config = get_generation_config(self.hf_config_path, trust_remote_code=self.trust_remote_code) |
| 91 | + |
| 92 | + # constuct hf_config |
| 93 | + attn_implementation = self.override_config.get("attn_implementation", "flash_attention_2") |
| 94 | + self.hf_config = AutoConfig.from_pretrained( |
| 95 | + self.hf_config_path, trust_remote_code=self.trust_remote_code, attn_implementation=attn_implementation |
| 96 | + ) |
| 97 | + |
| 98 | + override_config_kwargs = { |
| 99 | + "bos_token_id": self.tokenizer.bos_token_id, |
| 100 | + "eos_token_id": self.tokenizer.eos_token_id, |
| 101 | + "pad_token_id": self.tokenizer.pad_token_id, |
| 102 | + } |
| 103 | + override_config_kwargs.update(self.override_config) |
| 104 | + update_model_config(self.hf_config, override_config_kwargs=override_config_kwargs) |
| 105 | + |
| 106 | + # per model patch |
| 107 | + if getattr(self.hf_config, "model_type", None) == "kimi_vl": |
| 108 | + self.hf_config.text_config.topk_method = "greedy" |
| 109 | + |
| 110 | + def get_processor(self): |
| 111 | + return self.processor if self.processor is not None else self.tokenizer |
0 commit comments