Skip to content

[Refactor] Model-family spec registry for export: MoE expert naming#1828

Draft
h-guo18 wants to merge 13 commits into
mainfrom
haoguo/export-modelinglib
Draft

[Refactor] Model-family spec registry for export: MoE expert naming#1828
h-guo18 wants to merge 13 commits into
mainfrom
haoguo/export-modelinglib

Conversation

@h-guo18

@h-guo18 h-guo18 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Refactor / code health

Adds a declarative per-model-family registry, modelopt/torch/export/modeling/, and
migrates the first data category: MoE expert linear naming. Each family is one
small spec file (families/qwen.py, deepseek.py, mixtral.py, dbrx.py,
gptoss.py, nemotron.py, gemma.py); importing the package registers all specs.

Two HF-path consumers now read the registry instead of branching on class names:

  • layer_utils.get_expert_linear_names — resolves spec.expert_linear_names first;
    the legacy if/elif chain is preserved verbatim as fallback, so unmatched families
    are byte-for-byte unaffected.
  • layer_utils.get_experts_list — reads spec.expert_linear_names gated by
    spec.has_iterable_experts, which reproduces the legacy "supported 4 families,
    else NotImplementedError" behavior.

Scope note: per MODEL_SPECIFIC_REFACTOR.md, this series targets the unified HF
path only
. The TRT-LLM checkpoint path keeps its branches (to be moved out as-is
on a separate track); the Megatron path already follows a per-family registry
pattern and is untouched.

Relationship to #1939 (why a second registry?)

The two registries are deliberate, orthogonal layers — not duplication:

Example: when exporting a QuantMixtralSparseMoeBlock, #1939 matches it to the
shared iterable-experts handler (the same handler serves Qwen/DeepSeek/Gemma4);
inside, get_expert_linear_names resolves the mixtral spec and returns
("w1", "w2", "w3"). One handler serves many families, and one family spec will
serve many handlers — a many-to-many relation indexed from both sides. Folding one
registry into the other would either turn declarative data into per-family
functions, or turn behavior into spec fields. Sharing the matcher machinery between
the two is planned for P4 (see the doc).

Usage

# Adding a MoE model family is a small declarative file; no engine edits.
# modelopt/torch/export/modeling/families/<family>.py
register(
    ModelSpec(
        name="qwen_moe",
        moe_block_names=("Qwen3MoeSparseMoeBlock",),
        expert_linear_names=("gate_proj", "down_proj", "up_proj"),
        has_iterable_experts=True,
    )
)

Testing

  • New tests/unit/torch/export/test_modeling_specs.py: registry matching
    (substring / quantized-class-name / unmatched→None), fallback preservation, and
    get_experts_list grouping + unsupported-family rejection.
  • Existing tests/unit/torch/export/test_layer_utils.py expert-naming tests pass
    unchanged through the new spec path (equivalence check).
  • pytest tests/unit/torch/export/ — 116 passed (test_export_diffusers.py
    skipped: pre-existing CUDA/glibc collection error unrelated to this change).
  • Docs build clean with --fail-on-warning (fixes the previous build-docs failure).

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅ (fallback-first; behavior-preserving)
  • If you copied code from any other sources or added a new PIP dependency: N/A
  • Did you write any new necessary tests?: ✅ (test_modeling_specs.py)
  • Did you update Changelog?: N/A (internal refactor, no public API change)

Additional Information

Follow-ups (see MODEL_SPECIFIC_REFACTOR.md): P2 PQS_FUSE_MODULE_MAPPING
spec.pqs_fuse_rules; P3 is_moe name list → spec.moe_block_names; P4 handlers
consume specs directly + unify matcher machinery with #1939's registry.

@copy-pr-bot

copy-pr-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@h-guo18 h-guo18 changed the title [Refactor] Extract model specific logics in export [Refactor] Extract model specific logics in export lib Jun 25, 2026
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c82bb9cf-8702-4bd1-a47b-6cd03fdfa45e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch haoguo/export-modelinglib

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.69072% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.43%. Comparing base (f479e78) to head (fae15c9).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
modelopt/torch/export/layer_utils.py 85.36% 6 Missing ⚠️
modelopt/torch/export/unified_export_hf.py 28.57% 5 Missing ⚠️
modelopt/torch/export/plugins/vllm_fakequant_hf.py 25.00% 3 Missing ⚠️
modelopt/torch/export/quant_utils.py 40.00% 3 Missing ⚠️
modelopt/torch/export/hf_export_handlers.py 0.00% 2 Missing ⚠️
modelopt/modeling/registry.py 98.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1828      +/-   ##
==========================================
- Coverage   77.87%   74.43%   -3.44%     
==========================================
  Files         522      544      +22     
  Lines       58452    61841    +3389     
==========================================
+ Hits        45522    46034     +512     
- Misses      12930    15807    +2877     
Flag Coverage Δ
unit 55.13% <89.69%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread modelopt/modeling/base.py Outdated


@dataclass
class ModelSpec:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trtllm export path is deprecated.

RelativeAttentionTableConfig,
RgLruConfig,
)
from .model_config_utils import pad_weights

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HF export only.

Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
@h-guo18 h-guo18 force-pushed the haoguo/export-modelinglib branch from 8ddd26c to e2a32bb Compare July 14, 2026 22:49
@github-actions

github-actions Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://NVIDIA.github.io/Model-Optimizer/pr-preview/pr-1828/

Built to branch gh-pages at 2026-07-16 20:46 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@h-guo18 h-guo18 changed the title [Refactor] Extract model specific logics in export lib [Refactor] Model-family spec registry for export: MoE expert naming Jul 14, 2026
h-guo18 added 9 commits July 14, 2026 23:45
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
@@ -0,0 +1,120 @@
# Export: Model-Specific Logic Refactor — Action Plan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file meant to be checked in?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it will be removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is still messy. Need some cleaning before ready for review

h-guo18 added 3 commits July 16, 2026 20:41
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Signed-off-by: h-guo18 <67671475+h-guo18@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants