-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[None][fix] Refactoring to avoid circular import when importing torch models #6720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rakib-hasan
merged 10 commits into
NVIDIA:main
from
rakib-hasan:fix/circular_import_with_torch_models
Aug 11, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d7a24c8
refactoring to avoid circular import when importing torch models
rakib-hasan 585b81c
Merge branch 'main' into fix/circular_import_with_torch_models
rakib-hasan 8a53d9d
addressing review comments
rakib-hasan e37359d
Merge branch 'main' into fix/circular_import_with_torch_models
rakib-hasan 631d037
Merge branch 'main' into fix/circular_import_with_torch_models
rakib-hasan 692960c
fixing a tensorrt lib loading issue
rakib-hasan c22a283
Merge branch 'main' into fix/circular_import_with_torch_models
rakib-hasan 219ecd8
fixing a typo introduced in PR #6616
rakib-hasan 79697c2
Merge branch 'main' into fix/circular_import_with_torch_models
rakib-hasan e276bac
applying coderabbit suggestion
rakib-hasan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
rakib-hasan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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 dataclasses import dataclass, field | ||
from typing import Dict, List, Optional | ||
|
||
from ._utils import DictConversion | ||
|
||
|
||
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_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", | ||
} | ||
|
||
|
||
def use_lora( | ||
model, | ||
lora_config: "LoraConfig", | ||
trtllm_modules_to_hf_modules: Optional[Dict[str, str]] = None, | ||
): | ||
"""Use LoRA with the given model and configuration. | ||
|
||
This function is a wrapper that delegates to the appropriate loading function | ||
based on the LoRA checkpoint source. | ||
""" | ||
if lora_config.lora_ckpt_source == "nemo": | ||
from .lora_manager import load_nemo_lora | ||
load_nemo_lora(model, lora_config) | ||
elif lora_config.lora_ckpt_source == "hf": | ||
from .lora_manager import load_hf_lora | ||
load_hf_lora(model, lora_config, trtllm_modules_to_hf_modules) | ||
else: | ||
rakib-hasan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise ValueError( | ||
f"Unsupported lora_ckpt_source: {lora_config.lora_ckpt_source}") | ||
|
||
|
||
@dataclass | ||
class LoraConfig(DictConversion): | ||
lora_dir: List[str] = field(default_factory=list) | ||
lora_ckpt_source: str = "hf" | ||
max_lora_rank: int = 64 | ||
lora_target_modules: List[str] = field(default_factory=list) | ||
trtllm_modules_to_hf_modules: Dict[str, str] = field(default_factory=dict) | ||
max_loras: Optional[int] = None | ||
max_cpu_loras: Optional[int] = None | ||
|
||
def __post_init__(self): | ||
assert self.lora_ckpt_source in [ | ||
"hf", "nemo" | ||
], (f"lora_ckpt_source must be one of 'hf' or 'nemo', got {self.lora_ckpt_source}" | ||
) | ||
|
||
rakib-hasan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@property | ||
def missing_qkv_modules(self) -> List[str]: | ||
return get_missing_qkv_modules_from_lora_modules( | ||
self.lora_target_modules) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.