-
Notifications
You must be signed in to change notification settings - Fork 487
[Refactor] Extract model specific logics in export lib #1828
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
Draft
h-guo18
wants to merge
8
commits into
main
Choose a base branch
from
haoguo/export-modelinglib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0fbbbf9
export modeling lib
h-guo18 2d30cb9
moe
h-guo18 87a8815
activation function
h-guo18 86e3966
move force_share_embedding_table
h-guo18 9d54011
move mlp keywords
h-guo18 e2ab51c
move PQS_FUSE_MODULE_MAPPING
h-guo18 cd74be8
clean up comments
h-guo18 8ddd26c
add hook
h-guo18 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,47 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 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. | ||
|
|
||
| """Model-family export descriptors. | ||
|
|
||
| Holds per-model export data organized by model family. The export code resolves a | ||
| ``ModelSpec`` via the registry lookups and reads its fields; an unmatched lookup | ||
| returns ``None`` so callers fall back to their default behavior. | ||
| """ | ||
|
|
||
| from . import families | ||
| from .base import ModelSpec | ||
| from .context import ExportContext | ||
| from .hooks import NULL_HOOKS, ModelHooks | ||
| from .registry import ( | ||
| iter_pqs_fuse_rules, | ||
| match_by_architecture, | ||
| match_by_decoder_type, | ||
| match_mlp_block, | ||
| match_moe_block, | ||
| register, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "NULL_HOOKS", | ||
| "ExportContext", | ||
| "ModelHooks", | ||
| "ModelSpec", | ||
| "iter_pqs_fuse_rules", | ||
| "match_by_architecture", | ||
| "match_by_decoder_type", | ||
| "match_mlp_block", | ||
| "match_moe_block", | ||
| "register", | ||
| ] |
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,95 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 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. | ||
|
|
||
| """Per-model-family export descriptor. | ||
|
|
||
| A ``ModelSpec`` declares how one model family differs from the generic export path, | ||
| so the export code can read these values instead of branching on model names. Each | ||
| spec holds per-model data only, not export logic. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .hooks import ModelHooks | ||
|
|
||
| __all__ = ["ModelSpec"] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ModelSpec: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trtllm export path is deprecated. |
||
| """Per-model-family export data. | ||
|
|
||
| A spec is resolved from a model (or one of its sub-modules) via any of its matching | ||
| keys: | ||
| architectures: exact HuggingFace architecture class names (e.g. "Qwen3MoeForCausalLM"). | ||
| decoder_types: ModelOpt ``decoder_type`` strings (e.g. "bloom", "phi3"). Exact match. | ||
| moe_block_names: MoE block class-name substrings, matched case-insensitively | ||
| (e.g. "Qwen3MoeSparseMoeBlock"). | ||
| mlp_block_names: MLP module class names, matched by exact equality | ||
| (e.g. "ArcticMLP", "Phi3SmallMLP"). | ||
|
|
||
| Per-model fields: | ||
| expert_linear_names: expert linear projection names for this family's MoE block, | ||
| e.g. ("gate_proj", "down_proj", "up_proj"). ``None`` if not applicable. | ||
| has_iterable_experts: True when experts are stored as per-expert iterable | ||
| sub-modules (Mixtral, Qwen MoE, NemotronH, Gemma4) and can be grouped by | ||
| ``get_experts_list``. False for stacked or fused layouts (DBRX, GptOss). | ||
| forced_activation: activation that overrides MLP activation detection | ||
| (e.g. Bloom/GLM → "gelu", Phi3 → "swiglu"). ``None`` for no override. | ||
| force_share_embedding_table: True for families that share the embedding/output | ||
| table (Gemma/Gemma2/Gemma3); still gated by a weight-equality check. | ||
| mlp_keyword_roles: overrides mapping a child-module name to its MLP role, e.g. | ||
| {"up_proj": "fc"} (MPT/Phi3Small) or {"w1": "fc", "w2": "proj", "w3": "gate"} | ||
| (Arctic/InternLM2). Each keyword is removed from the default role sets and | ||
| added to its target role. ``None`` for no override. | ||
| pqs_fuse_rules: AWQ pre_quant_scale fusion rules, each a | ||
| ``(module_class_substrings, fuse_into, fuse_from)`` triple: for a module whose | ||
| class name contains one of the substrings, the pre_quant_scale on ``fuse_from`` | ||
| is folded into ``fuse_into`` (e.g. attention o_proj → v_proj, MLP down_proj → | ||
| up_proj). | ||
| hooks: optional :class:`ModelHooks` for behavioral seams that no value can express | ||
| (custom config-slot placement, module-tree unwrap, MoE building). ``None`` uses | ||
| the engine defaults. | ||
| """ | ||
|
|
||
| name: str | ||
|
|
||
| # Matching keys. | ||
| architectures: tuple[str, ...] = () | ||
| decoder_types: tuple[str, ...] = () | ||
| moe_block_names: tuple[str, ...] = () | ||
| mlp_block_names: tuple[str, ...] = () | ||
|
|
||
| # MoE expert layout. | ||
| expert_linear_names: tuple[str, ...] | None = None | ||
| has_iterable_experts: bool = False | ||
|
|
||
| # MLP and activation. | ||
| forced_activation: str | None = None | ||
| mlp_keyword_roles: dict[str, str] | None = None | ||
|
|
||
| # Embedding. | ||
| force_share_embedding_table: bool = False | ||
|
|
||
| # AWQ pre_quant_scale fusion. | ||
| pqs_fuse_rules: tuple[tuple[tuple[str, ...], str, str], ...] = () | ||
|
|
||
| # Behavioral overrides for structural export seams (None = use the engine default). | ||
| hooks: "ModelHooks | None" = None | ||
|
|
||
| # Free-form extension slot for future per-model fields. | ||
| _extra: dict = field(default_factory=dict, repr=False) | ||
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,32 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 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. | ||
|
|
||
| """Context passed from the export engine into per-model hooks. | ||
|
|
||
| The engine fills this with the builder callables a hook may need and passes it in, so | ||
| hooks never import the engine modules directly (keeping ``modeling/`` free of cycles). | ||
| """ | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| __all__ = ["ExportContext"] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExportContext: | ||
| """Builders and metadata available to ``ModelHooks`` methods.""" | ||
|
|
||
| build_layernorm_config: Callable |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HF export only.