|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from typing import Dict, List, Optional |
| 18 | + |
| 19 | +from ._utils import DictConversion |
| 20 | + |
| 21 | + |
| 22 | +def get_missing_qkv_modules_from_lora_modules( |
| 23 | + lora_target_modules: List[str]) -> List[str]: |
| 24 | + """Get missing QKV modules from LoRA target modules. |
| 25 | +
|
| 26 | + In current design, q_lora_params, k_lora_params and v_lora_params should be all enabled or |
| 27 | + all disabled at the same time. However, some lora checkpoints (e.g. BART) only contain two of them, |
| 28 | + so we use zero tensor to fill the missing ones. |
| 29 | + """ |
| 30 | + missing_qkv_modules = [] |
| 31 | + if any(x in lora_target_modules for x in ["attn_q", "attn_k", "attn_v"]): |
| 32 | + for lora_module in ["attn_q", "attn_k", "attn_v"]: |
| 33 | + if lora_module not in lora_target_modules: |
| 34 | + missing_qkv_modules.append(lora_module) |
| 35 | + if any(x in lora_target_modules |
| 36 | + for x in ["cross_attn_q", "cross_attn_k", "cross_attn_v"]): |
| 37 | + for lora_module in ["cross_attn_q", "cross_attn_k", "cross_attn_v"]: |
| 38 | + if lora_module not in lora_target_modules: |
| 39 | + missing_qkv_modules.append(lora_module) |
| 40 | + return missing_qkv_modules |
| 41 | + |
| 42 | + |
| 43 | +def get_default_trtllm_modules_to_hf_modules(): |
| 44 | + """Get default mapping from TensorRT-LLM module names to HuggingFace module names.""" |
| 45 | + return { |
| 46 | + "attn_q": "q_proj", |
| 47 | + "attn_k": "k_proj", |
| 48 | + "attn_v": "v_proj", |
| 49 | + "attn_dense": "o_proj", |
| 50 | + "mlp_h_to_4h": "gate_proj", |
| 51 | + "mlp_4h_to_h": "down_proj", |
| 52 | + "mlp_gate": "up_proj", |
| 53 | + "mlp_gate_up": "gate_up_proj", |
| 54 | + "moe_h_to_4h": "w1", |
| 55 | + "moe_4h_to_h": "w2", |
| 56 | + "moe_gate": "w3", |
| 57 | + "moe_router": "gate", |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +def use_lora( |
| 62 | + model, |
| 63 | + lora_config: "LoraConfig", |
| 64 | + trtllm_modules_to_hf_modules: Optional[Dict[str, str]] = None, |
| 65 | +): |
| 66 | + """Use LoRA with the given model and configuration. |
| 67 | +
|
| 68 | + This function is a wrapper that delegates to the appropriate loading function |
| 69 | + based on the LoRA checkpoint source. |
| 70 | + """ |
| 71 | + if lora_config.lora_ckpt_source == "nemo": |
| 72 | + from .lora_manager import load_nemo_lora |
| 73 | + load_nemo_lora(model, lora_config) |
| 74 | + elif lora_config.lora_ckpt_source == "hf": |
| 75 | + from .lora_manager import load_hf_lora |
| 76 | + load_hf_lora(model, lora_config, trtllm_modules_to_hf_modules) |
| 77 | + else: |
| 78 | + raise ValueError( |
| 79 | + f"Unsupported lora_ckpt_source: {lora_config.lora_ckpt_source}") |
| 80 | + |
| 81 | + |
| 82 | +@dataclass |
| 83 | +class LoraConfig(DictConversion): |
| 84 | + lora_dir: List[str] = field(default_factory=list) |
| 85 | + lora_ckpt_source: str = "hf" |
| 86 | + max_lora_rank: int = 64 |
| 87 | + lora_target_modules: List[str] = field(default_factory=list) |
| 88 | + trtllm_modules_to_hf_modules: Dict[str, str] = field(default_factory=dict) |
| 89 | + max_loras: Optional[int] = None |
| 90 | + max_cpu_loras: Optional[int] = None |
| 91 | + swap_gate_up_proj_lora_b_weight: bool = True |
| 92 | + |
| 93 | + def __post_init__(self): |
| 94 | + assert self.lora_ckpt_source in [ |
| 95 | + "hf", "nemo" |
| 96 | + ], (f"lora_ckpt_source must be one of 'hf' or 'nemo', got {self.lora_ckpt_source}" |
| 97 | + ) |
| 98 | + |
| 99 | + @property |
| 100 | + def missing_qkv_modules(self) -> List[str]: |
| 101 | + return get_missing_qkv_modules_from_lora_modules( |
| 102 | + self.lora_target_modules) |
0 commit comments