# SPDX-FileCopyrightText: Copyright (c) 2022-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, List, Literal, Optional
from pydantic import Field
from tensorrt_llm.llmapi.utils import StrictBaseModel
def get_missing_qkv_modules_from_lora_modules(
lora_target_modules: List[str]) -> List[str]:
"""Get missing QKV modules from LoRA target modules.
In current design, q_lora_params, k_lora_params and v_lora_params should be all enabled or
all disabled at the same time. However, some lora checkpoints (e.g. BART) only contain two of them,
so we use zero tensor to fill the missing ones.
"""
missing_qkv_modules = []
if any(x in lora_target_modules for x in ["attn_q", "attn_k", "attn_v"]):
for lora_module in ["attn_q", "attn_k", "attn_v"]:
if lora_module not in lora_target_modules:
missing_qkv_modules.append(lora_module)
if any(x in lora_target_modules
for x in ["cross_attn_q", "cross_attn_k", "cross_attn_v"]):
for lora_module in ["cross_attn_q", "cross_attn_k", "cross_attn_v"]:
if lora_module not in lora_target_modules:
missing_qkv_modules.append(lora_module)
return missing_qkv_modules
def get_default_trtllm_modules_to_hf_modules():
"""Get default mapping from TensorRT-LLM module names to HuggingFace module names."""
return {
"attn_q": "q_proj",
"attn_k": "k_proj",
"attn_v": "v_proj",
"attn_qkv": "qkv_proj",
"attn_dense": "o_proj",
"mlp_h_to_4h": "gate_proj",
"mlp_4h_to_h": "down_proj",
"mlp_gate": "up_proj",
"mlp_gate_up": "gate_up_proj",
"moe_h_to_4h": "w1",
"moe_4h_to_h": "w2",
"moe_gate": "w3",
"moe_router": "gate",
"shared_expert_h_to_4h": "shared_expert.gate_proj",
"shared_expert_4h_to_h": "shared_expert.down_proj",
"shared_expert_gate": "shared_expert.up_proj",
"mamba_in_proj": "in_proj",
"mamba_out_proj": "out_proj",
"moe_latent_fc1": "fc1_latent_proj",
"moe_latent_fc2": "fc2_latent_proj",
}
[docs]
class LoraConfig(StrictBaseModel):
lora_dir: List[str] = Field(
default_factory=list,
description="List of directories containing LoRA adapter checkpoints. "
"For HuggingFace format, expects adapter_model.bin/.safetensors and adapter_config.json. "
"For NeMo format, expects .nemo files. If empty, lora_target_modules must be provided."
)
lora_ckpt_source: Literal["hf", "nemo"] = Field(
default="hf",
description=
"Checkpoint format: 'hf' for HuggingFace PEFT format, 'nemo' for NeMo format. "
"NeMo only supports attn_qkv module.")
max_lora_rank: int = Field(
default=64,
description=
"Maximum LoRA rank across all adapters, used to pre-allocate workspace memory. "
"Set to the actual max rank of your adapters to reduce memory usage.")
lora_target_modules: List[str] = Field(
default_factory=list,
description="TensorRT-LLM module names where LoRA is applied "
"(e.g., ['attn_q', 'attn_k', 'attn_v'], ['attn_qkv'], ['mlp_gate', 'mlp_up']). "
"If empty and lora_dir is provided, inferred from checkpoint.")
trtllm_modules_to_hf_modules: Dict[str, str] = Field(
default_factory=dict,
description=
"Mapping from TensorRT-LLM module names to HuggingFace module names "
"(e.g., {'attn_q': 'q_proj'}). If empty, uses default mappings.")
max_loras: Optional[int] = Field(
default=None,
description="Sizes the device-side LoRA cache for this many adapters at "
"max_lora_rank, taking precedence over "
"peft_cache_config.device_cache_percent when positive. Also sets "
"the number of adapter slots in LoRA CUDA graphs (8 when unset). "
"See the CUDA Graph Adapter Capacity section of the LoRA guide.")
max_cpu_loras: Optional[int] = Field(
default=None,
description=
"Maximum number of LoRA adapters that can be stored in CPU memory. "
"Controls host-side cache for prefetching adapters before moving to GPU."
)
swap_gate_up_proj_lora_b_weight: bool = Field(
default=True,
description=
"Whether to swap gate/up projection order in fused gate_up_proj LoRA B weights. "
"Set to False for models like Phi-4-MM that use a different weight order."
)
overlap_lora_and_base: bool = Field(
default=False,
description=
"Whether to place LoRA operations on a secondary CUDA stream and overlap them "
"with base model computations. Improves latency in memory-bound regimes."
)
cuda_graph_specialize_lora: bool = Field(
default=False,
description=
"Whether to capture separate CUDA graphs for batches with and without "
"active LoRA adapters. Reduces overhead for batches without LoRA at "
"the cost of increased warmup time and CUDA graph metadata.")
@property
def missing_qkv_modules(self) -> List[str]:
return get_missing_qkv_modules_from_lora_modules(
self.lora_target_modules)