Repository navigation
[OMNIML-6533] Import a model's PTQ modules by its model type when the model is used - #3542
shengliangxu wants to merge 3 commits into
Conversation
|
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. |
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: truePre-merge checks:
|
| Check name | Status | Explanation |
|---|---|---|
| Security Anti-Patterns | The security anti-patterns check finds no violation in the PR's changes. I diffed the local checkout (HEAD 9878cef) against origin/main. The diff has 14 files and 278 added lines, which matches the PR… |
Full details: Security Anti-Patterns
-
Explanation
The security anti-patterns check finds no violation in the PR's changes. I diffed the local checkout (HEAD 9878cef) against origin/main. The diff has 14 files and 278 added lines, which matches the PR's described modules. The repository is a shallow clone, so the diff was not confirmed against the PR's stacked base branch. Checks against the six criteria, on added and removed lines: 1.
torch.load/weights_only: no matches. 2.numpy.load/np.load/allow_pickle: no matches. 3.trust_remote_code: no matches. 4.eval(/exec(: no matches. The one dynamic-import path isimportlib.util.find_specandimportlib.import_moduleinmodelopt/torch/models/__init__.py(import_model_modules). It builds a module name fromconfig.model_typeand prefixes it with themodelopt.torch.modelspackage. It is not eval or exec, and the name must passisidentifier(). This does not match criterion 4 as written. 5.# nosec: no matches. No removed lines contain security markers. 6.pyproject.toml/requirements*.txt: not changed, so no new dependencies. The added tests useimportlib.import_module("vllm.models.deepseek_v4.attention")only as a fixed string. No new eval, exec, or pickle-based loading appears.
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
b24e4d3 to
13ac8c7
Compare
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3542 +/- ##
=======================================
Coverage 82.31% 82.31%
=======================================
Files 730 731 +1
Lines 82224 82260 +36
=======================================
+ Hits 67683 67715 +32
- Misses 14541 14545 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
13ac8c7 to
f568324
Compare
f568324 to
b33ede0
Compare
b33ede0 to
c20cdc1
Compare
…ode into modelopt/torch/models (#3368) ### What does this PR do? Type of change: Refactor Part 2 of 2 of a stack that leaves the Hugging Face and vLLM quantization plugins with only generic support. [1/2] #3367 has merged, and this PR now targets main. The vLLM plugins held the wrappers for three named models next to the generic vLLM support. They move to their model packages in `modelopt/torch/models`, as `modeling_ptq_vllm.py` next to the transformers `modeling_ptq.py`. No behavior change: registrations and quantized results are the same. - **DeepSeek-V4** (`models/deepseek_v4/modeling_ptq_vllm.py`): the indexer wrapper, from `vllm_indexer.py`, and DeepSeek-V4's Mega-MoE registration, from `vllm_mega_moe.py`. - **GLM-5.3-Flash** (`models/glm5_next/modeling_ptq_vllm.py`, a new package): the k-pool indexer with its K-cache writers and DeepGEMM scorer, from `vllm_indexer.py`. Like `kimi_k3`, `glm5_next` has no `specs.py`: it holds only vLLM modeling, and a spec would make claims about the transformers model that nothing here checks. - **Kimi-K3** (`models/kimi_k3/modeling_ptq_vllm.py`): the MLP fake-quant preparation, from `vllm.py`, and Kimi-K3's Mega-MoE registration, from `vllm_mega_moe.py`. The preparation used to run inside the vLLM plugin's attention-preparation callback; it is now a `CUSTOM_MODEL_PLUGINS` callback of its own. The two touch disjoint modules, so their order does not matter. `_VLLMParallelLinear` reads a generic marker, `_fake_quant_supported_by_model`, instead of a Kimi-named one. - The generic indexer base class and its FP8 and scorer helpers, the Mega-MoE wrapper and the layer scope stay in the plugins. - `plugins/__init__.py` imports every `modeling_ptq_vllm` from an explicit list after the generic vLLM modules, inside the `import_plugin("vllm")` block, each under its own guard, so a model whose module fails to import loses only its own wrappers. DeepSeek-V4's module registers its indexer before its Mega-MoE import, which main kept in a later plugin module. DeepSeek-V4's and Kimi-K3's optional Mega-MoE imports only warn on an unexpected error, so both modules always complete: DeepSeek-V4's stays importable with its indexer registered, and Kimi-K3's MLP preparation is registered even if vLLM's Kimi module only imports later. The follow-up #3510 replaces this list, and the HF plugin's, by finding the modules through the import system; #3542 then imports only a model's own, when the model is used. - GLM reads the scorer-kwargs registry through `vllm_indexer`, so it and the indexer base class read one binding even when the registry is rebound, as tests do. The moved code is verbatim apart from the callback, the marker and the registry read. Tests in `tests/gpu_vllm` patch the moved names in their new modules. <details><summary>Moved names (old module → new module)</summary> | Name | From `modelopt/torch/quantization/plugins/` | To `modelopt/torch/models/` | |---|---|---| | `_QuantVLLMDeepseekV4Indexer` | `vllm_indexer.py` | `deepseek_v4/modeling_ptq_vllm.py` | | `_QuantVLLMGlm5NextIndexer`, `_DEEP_GEMM_SCORERS`, `_GLM5NEXT_INDEXER_OP`, `_GLM5NEXT_QUERY_KERNEL`, `_KPOOL_CACHE_WRITERS`, `_bind`, `_call_deep_gemm_scorer`, `_glm5next_indexers`, `_import_glm5next_indexer`, `_kpool_decode_written`, `_kpool_prefill_written`, `_run_scorer`, `_scorer_deep_gemm`, `_signature`, `_write_k_cache` | `vllm_indexer.py` | `glm5_next/modeling_ptq_vllm.py` | | `_KimiGemmRsArFakeQuantGuard`, `_KimiGemmRsFakeQuantGuard`, `_kimi_down_proj_has_enabled_quantizers`, `_kimi_mlp_bf16_linear_supports_fake_quant`, `_prepare_kimi_mlp_fake_quant` | `vllm.py` | `kimi_k3/modeling_ptq_vllm.py` | | `prepare_kimi_mlps_on_the_fly` (new callback) | | `kimi_k3/modeling_ptq_vllm.py` | </details> ### The stack [1/2] has merged. Each PR builds and carries its own tests. | PR | What | Added source lines | |---|---|---| | [1/2] #3367 (merged) | Hugging Face plugin: T5's KV-cache attention wrapper, the Nemotron-H check | 73 (31 moved, 14 license header) | | [2/2] (this PR) | vLLM plugins: the DeepSeek-V4, GLM-5.3-Flash and Kimi-K3 wrappers | 572 (about 470 moved, 56 license headers) | | Follow-up #3510, stacked on this PR | Both plugins: find the models' PTQ modules through the import system instead of listing model types | 30 | | Follow-up #3542, stacked on #3510 | Both plugins: import a model's PTQ modules by its model type when the model is used | 109 (14 license headers) | Lines are counted with the PR-size command in the repository's `CLAUDE.md`: added Python source lines, without blank lines, tests, docs and config. Until 2026-11-21 this PR also carried the three commits now in #3510 and #3542. They moved there unchanged, so the move is again the same two patches as at `f04698ff43e`, which Claude approved and the GB200 runs below tested. On 2026-11-23 two commits answered cjluo-nv's review: `add71b82993` guards each model module's import and registers DeepSeek-V4's indexer first, and `bf970e5541d` tests Kimi-K3's MLP preparation through conversion. Its follow-up reviews brought `f2a837a6c21`, which keeps DeepSeek-V4's module importable when its Mega-MoE import fails; `3aff43a3ef0`, which registers Kimi-K3's MLP preparation in that case too; `ba58bf16406` and `51f5f7388ea`, which check GLM-5.3-Flash's native indexer registration independently of the module's own lookup; and `c26d765667a`, which hands vLLM's Kimi module back in the Kimi-K3 retry test instead of re-importing it into a circular import inside vLLM. **Why this PR keeps the eager import list.** It changes where the named-model vLLM wrappers live, not when they are registered: the plugin still imports all three when modelopt is imported, as main does, so the registrations stay identical to main's (checked below) and the move can be reviewed as a relocation. Importing a model's modules on demand, by its model type, as `models/hf.py` already does for its loading and calibration hooks, changes behavior in ways that need their own review and tests, and each came up while that change was part of this PR: a decoder-layer lookup before conversion, which loading with FSDP2 does, must import the model's module first; a module quantized without its model's config no longer gets its wrappers unless its module is imported; and a model type that holds another type's classes needs a package that imports the shared one. #3510 first replaces the list with discovery through the import system, still importing every module, and #3542 then imports a model's own modules when it is used. ### Usage N/A: no new or changed API. ### Testing | Check | Result | |---|---| | pre-commit, every hook including mypy and bandit, over `main..HEAD` | clean | | What `import modelopt.torch.quantization` registers (the quant module registry and `CUSTOM_MODEL_PLUGINS`), vLLM 0.22.1 on CPU, [1/2] vs this PR | identical (DeepSeek-V4's indexer and Mega-MoE entries), plus the new Kimi-K3 callback; importing any one model module first gives the same result | | CPU, `tests/gpu_vllm/torch/quantization/test_vllm_{indexer_quant,dynamic_modules,mega_moe}.py`, vLLM 0.22.1 | the same outcome per test id as on [1/2], plus the new test, which passes: 45 passed, 47 skipped, and 14 failed and 6 errors on both for lack of a GPU (no CUDA device, a missing TE/cuBLAS symbol). The passing tests run the moved DeepSeek-V4 indexer's q/k re-quantization and its refusals, and GLM's query, K-cache and scorer-kwargs paths | | Revert checks | dropping `glm5_next` from the import list fails `test_vllm_plugin_imports_every_model_package`, naming it; having GLM read the scorer-kwargs registry by name fails `test_registered_quantizers_turn_their_config_into_scorer_kwargs`; one guard for the whole list fails the Kimi-K3 case of `test_failed_vllm_model_import_keeps_other_registrations`, and DeepSeek-V4's old import order its DeepSeek-V4 case; dropping Kimi-K3's callback registration fails `test_kimi_gemm_rs_guard_tracks_converted_down_projection` (on CPU, against stand-ins for vLLM's Kimi-K3 modules); a DeepSeek-V4 module that aborts on a Mega-MoE error fails that case's later direct import with `already registered!`; a Kimi-K3 module that skips its MLP preparation after a failed startup import fails the Kimi-K3 retry case; `test_vllm_plugin_registers_native_glm5next_indexer` fails with GLM-5.3-Flash's registration removed, its key renamed or the module's lookup returning `None` (the last two checks against stand-ins) | | CPU, `tests/unit/torch/quantization/plugins`, `tests/unit/torch/models` and `tests/gpu/torch/models/test_modeling_ptq_registration.py`, transformers 5.9 | 752 passed, 16 skipped, as on [1/2] | | GPU on GB200, [1/2] vs this PR: all of `tests/gpu_vllm` at 1 and 2 GPUs under a vLLM 0.29.1rc1 nightly, plus the touched test files under vLLM 0.28.0 and a GLM-5.3-Flash build | no test changes outcome, apart from 3 timeout flips outside the moved code; details below | | CI at `5b9ae6972f0`, the same two patches on an earlier main: every job including the GPU ones; on CI's vLLM 0.30.0 with 1 GPU, `tests/gpu_vllm` at [1/2]'s head vs that one | green; 217 vs 218 passed, 8 skipped on both: per test id, the only change is the new test, which passes | | CPU at `bca69f2a7e9` vs main `b735732ef59`: the three vLLM test files under vLLM 0.22.1, without the 11 end-to-end engine tests, whose setup never returns without a GPU; and under transformers 5.4.0, `tests/unit/torch/quantization/plugins`, `tests/unit/torch/models`, the keep-fused export tests and the registration test | the same outcome per test id, plus the new test, which passes: vLLM 47 vs 46 passed, 42 skipped and the same 12 failed on both (`No CUDA GPUs are available`); transformers 680 passed and 34 skipped on both | | CI at `bca69f2a7e9`, on main `b735732ef59` | green, every job including the GPU ones. `tests/gpu_vllm` under vLLM 0.30.0 gives 218 passed and 8 skipped, as at `5b9ae6972f0`, and the GPU job 1930 passed and 175 skipped. Per test id against `d9c2c6328d0`, the head before #3510's commits moved out, no shared test changes outcome; the only other differences are the two tests #3510 took along, back to the versions they had replaced, which pass, and main's 20 new tests | | CPU at `bf970e5541d` vs `bca69f2a7e9`: the same suites, plus the Kimi tests against stand-ins for vLLM's Kimi-K3 modules, which vLLM 0.22.1 lacks | the same outcome per test id, plus the two new isolation cases, which pass: vLLM 49 passed, 42 skipped and the same 12 failed; transformers 680 passed and 34 skipped; the Kimi tests 12 passed and 1 skipped (the legacy GEMM-RS test, which the stand-ins do not model) | | CI at `bf970e5541d` | the GPU jobs green: the GPU job 1930 passed and 175 skipped, as at `bca69f2a7e9`; `tests/gpu_vllm` under vLLM 0.30.0 220 passed and 8 skipped, the 218 before plus the two new isolation cases, with the Kimi test through `mtq.replace_quant_module` passing against the real Kimi-K3 classes; Megatron 480 passed and TRT-LLM 43 passed. Unit tests, code quality, the docs and codecov are green. Two example jobs failed on pytest timeouts that hit other PRs' runs the same day: `test_llama_distill` (over 300 s), and in `hf_ptq` `test_ptq_t5[fp8...]` (over 300 s) and `test_qwen_vl[fp8]` (over 420 s) | | CI at `f2a837a6c21` | the GPU jobs green: `tests/gpu_vllm` 220 passed and 8 skipped, the DeepSeek-V4 re-import case passing against vLLM 0.30.0's real classes; `hf_ptq`'s `test_qwen_vl[fp8]` timed out again (over 420 s) | | CI at `ba58bf16406` | `tests/gpu_vllm` 220 passed and 1 failed: the new Kimi-K3 retry case, whose re-import of vLLM's Kimi module hit a circular import inside vLLM; `c26d765667a` fixes the test | | CPU at this head vs `bf970e5541d`: the vLLM test files, plus stand-ins for vLLM's Kimi-K3 module and GLM-5.3-Flash's native indexer, which vLLM 0.22.1 lacks | the same outcome per test id, plus the new Kimi-K3 retry case and GLM-5.3-Flash test, which skip without them: vLLM 49 passed, 44 skipped and the same 12 failed; against the stand-ins, both pass | | CI at this head | `tests/gpu_vllm` under vLLM 0.30.0: 222 passed and 8 skipped, the 220 before plus two that ran and passed against vLLM's real classes: the Kimi-K3 retry case of `test_failed_vllm_model_import_keeps_other_registrations`, and `test_vllm_plugin_registers_native_glm5next_indexer`, which found GLM-5.3-Flash's native indexer and checked its wrapper and config key. Every other job passed too: the GPU job 1930 passed and 175 skipped, as at `bca69f2a7e9`, the TRT-LLM and Megatron GPU jobs, the examples including `hf_ptq`, the unit tests and codecov/project | The earlier CPU rows and the GB200 runs predate the rebases onto main since; `git range-diff` shows both patches unchanged by each. **GPU runs** on GB200, [1/2] (`b85e4b6fa8e`) vs this PR (`f04698ff43e`), compared per test id. This cluster has no build of CI's vLLM 0.30.0, so they use three builds that between them contain every vLLM model path the moved code handles: | vLLM build | GPUs | Tests | [1/2] | This PR | Outcome changes | |---|---|---|---|---|---| | 0.29.1rc1 nightly | 1 | all of `tests/gpu_vllm` | 210 passed, 6 skipped, 6 failed, 3 errors | 211 passed, 6 skipped, 6 failed, 3 errors | none besides the new test | | 0.29.1rc1 nightly | 2 | all of `tests/gpu_vllm` | 212 passed, 4 skipped, 6 failed, 3 errors | 214 passed, 4 skipped, 5 failed, 3 errors | besides the new test, 3 tests outside the moved code that hit the 60 s per-test timeout: two only on [1/2], one only here | | 0.28.0 | 1 | the three touched test files | 101 passed, 9 skipped, 1 error | 102 passed, 9 skipped, 1 error | none besides the new test | | GLM-5.3-Flash build | 1 | the three touched test files | 100 passed, 9 skipped, 1 failed, 1 error | 101 passed, 9 skipped, 1 failed, 1 error | none besides the new test | - The failures and errors hit the same tests with the same error on both sides, and these builds are not the vLLM 0.30.0 that CI uses. The errors: 60 s test timeouts and vLLM engine-startup timeouts on the nightly; vLLM rejecting DeepSeek-V4's fp8_ds_mla KV-cache layout with the default KV-cache dtype on 0.28.0 and the GLM build; on the nightly, its Kimi-K3 GEMM-RS-AR path calling an `apply` method that ModelOpt's guard lacks; and on the GLM build, its end-to-end indexer test, whose indexer the ModelOpt plugins don't support. - What the runs cover: DeepSeek-V4's 9 tests ran in every build, with none skipped. GLM-5.3-Flash's tests ran in all three builds, and the nightly and the GLM build each carry one of its two module layouts. Kimi-K3's ran in all three: 0.28.0 takes its legacy GEMM-RS branch, the other two the newer one. In every build the DeepGEMM scorer-numerics test and the `MLAAttention` test skip, since no build has what they need. - The new `test_vllm_plugin_imports_every_model_package` passed in all four runs. - CI's GPU jobs, which skip while a PR targets another PR's branch, ran once this PR targeted main. On CI's runner the moved code's tests all pass except 5 that skip on [1/2]'s head too: Kimi-K3's legacy GEMM-RS test, a path vLLM 0.30.0 no longer takes; the 3 end-to-end indexer tests, which need an SM90 or SM100 GPU (CI's runner is an RTX PRO 6000); and the DeepGEMM scorer-numerics test. ### Before your PR is "*Ready for review*" Commits are signed (`git commit -s -S`). - Is this change backward compatible?: ✅ (every moved name is private; code that imported one from a plugin module now imports it from its model package) - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A - Did you write any new necessary tests?: ✅ (`test_vllm_plugin_imports_every_model_package` in `tests/gpu_vllm/torch/quantization/test_vllm_dynamic_modules.py`: importing modelopt in a fresh interpreter imports every `models/*/modeling_ptq_vllm.py`, where `import_plugin` would otherwise hide a failed import; `test_failed_vllm_model_import_keeps_other_registrations`: a model module that fails to import, GLM's or DeepSeek-V4's Mega-MoE one, leaves Kimi-K3's callback and DeepSeek-V4's indexer registered, DeepSeek-V4's module importable, and Kimi-K3's MLPs prepared once vLLM's Kimi module imports; `test_vllm_plugin_registers_native_glm5next_indexer` checks GLM-5.3-Flash's native indexer and its config key wherever the vLLM build has a supported layout) - Did you update Changelog?: N/A (internal refactor) - Did you get Claude approval on this PR?: ❌ not yet for this head: Claude approved the move at `f04698ff43e`, before the review-fix commits ### Additional Information Tracked by OMNIML-6533, for ModelOpt v0.50.0. Follow-ups: #3510, then #3542. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Documentation** * Clarified model-type-based loading for Transformers and vLLM, and documented model package naming and legacy Falcon types. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Shengliang Xu <[email protected]>
c20cdc1 to
9878cef
Compare
The quantization plugins imported every model package's modeling_ptq (transformers) and modeling_ptq_vllm (vLLM) when modelopt was imported, though a run needs only its own model's. Now nothing model-specific is imported up front. Each framework plugin names its per-model module in MODEL_PTQ_MODULES, and register_custom_model_plugins_on_the_fly, which every conversion runs first, imports those of the model being converted. import_model_modules(model, name) reads the model's types (the root config.model_type, then those of the configs nested in it, such as a VLM's language model), maps each to its package in snake case, and imports <package>/<name>.py where the import system finds it. A model's own module that fails to import now fails its conversion, rather than being skipped with a warning when modelopt is imported. Keyed by type, a module no longer covers a model of another type that holds its classes, so those types get a package whose modeling_ptq.py imports the shared one, as step3p7 does for step3p5: llama4_text, and Falcon's original remote-code types RefinedWeb and RefinedWebModel. A module quantized on its own, with no config, gets a model's wrappers only once that model's modeling_ptq is imported; the keep-fused export tests now import Nemotron-H's. The homogeneous HF decoder discoverer becomes a fallback, tried after every model's own, since a model's discoverer is now registered after it. The registration test checks in a fresh interpreter that importing modelopt imports none of these modules, that converting a Falcon model imports Falcon's and uses its wrapper, and the alias, the snake-case and the nested-config cases; the vLLM test checks that converting a model of each vLLM package's type imports its modeling_ptq_vllm. Since modelopt no longer imports the model modules, one model's failing import cannot keep another's from loading; the isolation test keeps only its DeepSeek-V4 case, which imports that module directly and checks that a failing Mega-MoE import leaves the indexer registered. Signed-off-by: Shengliang Xu <[email protected]>
A model's own decoder discoverer comes with its modeling_ptq, which the previous commit imports at conversion. Loading a model with FSDP2 asks for its decoder layers earlier: fsdp2_wrap and model_load_utils's wrap boundaries call LayerActivationCollector.get_decoder_layers before the model is converted. For a remote-code Nemotron-H, whose layers sit at backbone.layers where the generic HF discoverer does not look, that returned None and fsdp2_wrap refused the model. get_decoder_layers now imports the model's own PTQ modules first, through the same import_model_ptq_modules that conversion uses. fsdp2_utils still imports nothing from transformers: the lookup imports the plugin registry lazily, and a modeling_ptq only for a model whose type has one. The registration test finds a remote-code-style Nemotron-H's layers before any conversion; it fails there without the new import step. Signed-off-by: Shengliang Xu <[email protected]>
Importing a model's PTQ modules by its model type came with two packages, refined_web and refined_web_model, so that Falcon's original remote-code checkpoints (RWForCausalLM, model types RefinedWeb and RefinedWebModel) still loaded Falcon's modeling_ptq. Those checkpoints cannot be quantized anyway: their RWModel.forward calls PreTrainedModel.get_head_mask, which transformers no longer has (checked at 5.5.4 and 5.18.0, the ends of the supported range), so calibration and HF export, which both run the model, fail there, as they do on main. TII's own Falcon checkpoints now report model type falcon, which falcon/ handles directly; only 2023 fine-tunes and test fixtures still report the old types. Drop the two packages and their test cases, and say in falcon/specs.py why those types have none. Signed-off-by: Shengliang Xu <[email protected]>
…instead of listing model types (#3510) ### What does this PR do? Type of change: Refactor **Problem:** The Hugging Face and vLLM quantization plugins import each named model's PTQ module (`modelopt/torch/models/<model_type>/modeling_ptq.py`, and `modeling_ptq_vllm.py` from #3368) from a hard-coded list of model types. So the core library names models, and each new model needs an entry in a plugin's list. **Fix:** `modelopt.torch.models.import_model_modules(name)` imports `<package>/<name>.py` from every model package that has one, in name order, and both plugins call it instead of their lists. It imports the same modules in the same order as the lists did, so every registration stays the same; a new model's module needs no list entry, and the plugins name no model. It finds the modules through the import system (`pkgutil.iter_modules`, then `importlib.util.find_spec`), so a zipped or bytecode-only install loads them too, and guards each import on its own, so a module that fails to import loses only its model's wrappers. Follow-up to #3368, which has merged; this PR now targets main. Its first commit was #3368's third until 2026-11-21, and the second answers the review's question on finding the modules. #3542, stacked on this PR, then imports only the modules of the model being used. It adds 30 source lines (counted with the PR-size command in the repository's `CLAUDE.md`). ### Modules touched - `modelopt.torch.models`: it holds the per-model packages, so the helper that finds their modules (`import_model_modules`) belongs here. - `modelopt.torch.quantization`: both plugins call that helper instead of naming model types. ### Usage A model's PTQ wrappers go in `modelopt/torch/models/<model_type>/modeling_ptq.py` (`modeling_ptq_vllm.py` for vLLM), with no list to update. The HF plugin runs: ```python from modelopt.torch.models import import_model_modules import_model_modules("modeling_ptq") # imports every models/*/modeling_ptq.py, in name order ``` ### Testing The first commit was tested as `2a18aca6203`, on #3368's head at the time (`5b9ae6972f0`), then moved here unchanged (`git range-diff` showed the same patch) and rebased onto main `b735732ef59`. On 2026-11-23 it took over #3368's per-model import guard, and a second commit moved the lookup from files on disk to the import system. | Check | Result | |---|---| | pre-commit, every hook including mypy and bandit, over `main..HEAD` | clean | | What is registered at `2a18aca6203` vs `5b9ae6972f0` (the lists): the quant module registry, `CUSTOM_MODEL_PLUGINS`, the attention wrappers and the decoder discoverers in the order they are tried, under vLLM 0.22.1 (HF and vLLM plugins) and transformers 5.18 and 5.5.4 | identical, in the same order; also with each of the 10 model modules imported first, in a fresh interpreter | | `tests/unit/torch/quantization/plugins`, `tests/unit/torch/models` and the registration test under transformers 5.18, and the three vLLM test files under vLLM 0.22.1, on CPU, the same two heads | the same outcome per test id | | Revert check: a lookup that skips some packages | the HF registration test fails naming the skipped `gpt_oss`, and the vLLM test `glm5_next` | | CI at `2a18aca6203`, which then targeted main | green, every job including the GPU ones: the GPU job 1875 passed, and `tests/gpu_vllm` under vLLM 0.30.0 218 passed and 8 skipped, as at `5b9ae6972f0` | | CPU at `b36784c7609` vs #3368's head then: the three vLLM test files under vLLM 0.22.1, without the 11 end-to-end engine tests, whose setup never returns without a GPU; and under transformers 5.4.0, `tests/unit/torch/quantization/plugins`, `tests/unit/torch/models`, the keep-fused export tests and the registration test; and all of `tests/unit/torch/quantization` vs main | the same outcome per test id: vLLM 47 passed, 42 skipped and the same 12 failed (`No CUDA GPUs are available`); transformers 680 passed and 34 skipped; `tests/unit/torch/quantization` 1,984 passed, 1 skipped and 1 failed, as on main, where `test_hybrid_linear_attention_cache_round_trips` fails the same way for want of a newer transformers | | CI at `b36784c7609` | green, every job that runs on a stacked PR: code quality, the docs build, the CPU unit tests (`linux`, `windows`, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs skip, and their `*-pr-required-check` jobs fail on purpose, as noted below the table | | Install kinds: the `modeling_ptq` modules the HF plugin loads from a normal tree, a bytecode-only `modelopt/torch/models`, a zipped `modelopt`, and a zipped `modelopt` whose `modelopt/torch/models` holds only bytecode, file check (`4ca3976bd2c`) vs the import system (this head) | 7, 0, 0 and 0 vs 7, 7, 7 and 7 | | Lookup cost on NFS, median of 5 fresh processes | 34 ms for the import system vs 0.7 ms for the file check, once per plugin import | | CPU at `96f34d54685` vs `b36784c7609`: the same suites, plus the Kimi tests against stand-ins for vLLM's Kimi-K3 modules, which vLLM 0.22.1 lacks | the same outcome per test id, plus #3368's two new isolation cases, which pass: vLLM 49 passed, 42 skipped and the same 12 failed; transformers 680 passed and 34 skipped; `tests/unit/torch/quantization` 1,984 passed, 1 skipped and 1 failed, as before; the Kimi tests 12 passed and 1 skipped | | CI at `96f34d54685` | green, every job that runs on a stacked PR: code quality, the docs build, the CPU unit tests (`linux`, `windows`, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs), codecov/project and CodeRabbit, which approved. The GPU, example and regression jobs skip, and their `*-pr-required-check` jobs fail on purpose, as noted below the table; two cancelled entries are a duplicate run the push also started | | CPU at `05a0a87eb50`, rebased onto #3368's DeepSeek-V4 fix, vs `96f34d54685`: the vLLM test files | the same outcome per test id: vLLM 49 passed, 42 skipped and the same 12 failed | | CI at `05a0a87eb50` | green, every job that runs on a stacked PR: code quality, the docs build, the CPU unit tests (`linux`, `windows`, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs skip, and their `*-pr-required-check` jobs fail on purpose, as noted below the table; two cancelled entries are a duplicate run the push also started | | CPU at `a1e285388ea`, rebased onto #3368's Kimi-K3 fix and GLM-5.3-Flash check, vs `05a0a87eb50`: the vLLM test files, plus stand-ins for vLLM's Kimi-K3 module and GLM-5.3-Flash's native indexer, which vLLM 0.22.1 lacks | the same outcome per test id, plus the new Kimi-K3 case, which skips without Kimi-K3: vLLM 49 passed, 43 skipped and the same 12 failed; against the stand-ins, the Kimi-K3 retry case and the GLM-5.3-Flash registry check pass | | CI at `a1e285388ea` | green, every job that runs on a stacked PR: code quality, the docs build, the CPU unit tests (`linux`, `windows`, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project, and CodeRabbit approved. The GPU, example and regression jobs skip, and their `*-pr-required-check` jobs fail on purpose, as noted below the table; two cancelled entries are a duplicate run the push also started | | CPU at `ec4f172d55b`, rebased onto #3368's GLM-5.3-Flash test and Kimi-K3 retry fix, vs `a1e285388ea`: the vLLM test files, plus stand-ins for vLLM's Kimi-K3 module and GLM-5.3-Flash's native indexer, which vLLM 0.22.1 lacks | the same outcome per test id, plus the new GLM-5.3-Flash test, which skips without a native indexer: vLLM 49 passed, 44 skipped and the same 12 failed; against the stand-ins, it and the Kimi-K3 retry case pass | | CI at `ec4f172d55b`, while stacked on #3368 | green, every job that runs on a stacked PR: code quality, the docs build, the CPU unit tests (`linux`, `windows`, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project, and CodeRabbit approved. The GPU, example and regression jobs skip, and their `*-pr-required-check` jobs fail on purpose, as noted below the table; two cancelled entries are a duplicate run the push also started | | CI at this head, rebased by GitHub after #3368 merged, with the same patches (`git range-diff`) onto main `1e6408b8307` | green, every job, the GPU ones included now that this PR targets main: the GPU job 1930 passed and 175 skipped, `tests/gpu_vllm` under vLLM 0.30.0 222 passed and 8 skipped, as at #3368's merged head, Megatron 480 passed and TRT-LLM 43 passed; the examples, unit tests, docs and codecov/project passed too | Until #3368 merged, this PR targeted #3368's branch, so CI skipped its GPU, example and regression jobs, and their `*-pr-required-check` jobs failed on purpose. ### Before your PR is "*Ready for review*" Commits are signed (`git commit -s -S`). - Is this change backward compatible?: ✅ (the same modules, imported in the same order; a module that fails to import now loses only its model's wrappers) - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A - Did you write any new necessary tests?: ✅ (the HF registration test now checks, in a fresh interpreter, that importing the plugin imports every `models/*/modeling_ptq.py`, as the vLLM test already does for every `modeling_ptq_vllm.py`) - Did you update Changelog?: N/A (internal refactor) - Did you get Claude approval on this PR?: ❌ not yet (Claude approved #3368 at `f04698ff43e`, before these commits) ### Additional Information Follow-up to #3368, followed by #3542. Tracked by OMNIML-6533, for ModelOpt v0.50.0. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Documentation** * Updated guidance to describe automatic discovery and retain the recommendation to keep model package initialization focused on specifications. * **Tests** * Updated checks and descriptions to cover discovery of model-specific modules by the plugins. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Shengliang Xu <[email protected]>
9878cef to
937d6a5
Compare
What does this PR do?
Type of change: Refactor
Problem: The Hugging Face and vLLM quantization plugins import every named model's PTQ module (
modelopt/torch/models/<model_type>/modeling_ptq.py, andmodeling_ptq_vllm.pyfrom #3368) when modelopt is imported: #3510 finds them by file name and imports them all. So importing modelopt imports every model's PTQ code whether the model is used or not: with vLLM 0.22.1 installed,import modelopt.torch.quantizationtakes 51.5 s on CPU, against 39.4 s without them.Fix: Quantization imports a model's own PTQ modules when it first needs them: when the model is converted (quantize or restore), and when its decoder layers are looked up, which loading with FSDP2 does before any conversion. The modules are found from the model's config, by its model types, so neither plugin names a model.
MODEL_PTQ_MODULESinplugins/custom.py(modeling_ptqfor transformers,modeling_ptq_vllmfor vLLM).import_model_ptq_modules(model)imports the model's own, and runs first inregister_custom_model_plugins_on_the_fly, which every conversion starts with, and inLayerActivationCollector.get_decoder_layers.modelopt.torch.models.import_model_modules(model, name)reads the model's types (the rootconfig.model_type, then those of the configs nested in it, such as a VLM's language model), maps each to its package in snake case, and imports<package>/<name>.pywhere the import system finds it, as [OMNIML-6533] Find the models' PTQ modules through the import system instead of listing model types #3510's lookup does. A model's own module that fails to import fails the step that needed it, the conversion or the decoder-layer lookup, where [OMNIML-6533] Find the models' PTQ modules through the import system instead of listing model types #3510 skips it with a warning when modelopt is imported.modeling_ptq.pyimports the shared one, asstep3p7already does forstep3p5: herellama4_text(Llama4ForCausalLM). Falcon's original remote-code types (RefinedWeb,RefinedWebModel) get none, so they no longer load Falcon's module. They cannot be quantized on main either: their code callsPreTrainedModel.get_head_maskin every forward pass, which transformers no longer has (checked at 5.5.4 and 5.18.0), and TII's own Falcon checkpoints now report model typefalcon.modeling_ptqis imported. The keep-fused export tests quantize Nemotron-H's experts that way, so they now import it.Follow-up to #3510 and stacked on it: its first two commits were the top of #3368 until 2026-11-21, then of #3510 until 2026-11-23. Merge it after #3510. It adds 109 source lines, 14 of them license headers (counted with the PR-size command in the repository's
CLAUDE.md).Modules touched
modelopt.torch.models: it holds the per-model packages, so the lookup from a model's types to its packages (import_model_modules(model, name)) and the package for a type that reuses another type's modules (llama4_text) belong here.modelopt.torch.quantization: the plugins stop importing every model's modules, and the two places that need a model's registrations, conversion (plugins/custom.py) and the decoder-layer lookup (utils/layerwise_calib.py), import its modules first.Usage
Whole models need nothing new: converting one imports its own PTQ modules. To quantize a model-specific module on its own, without a model config, import its model's module first:
Testing
These commits were tested as the top of #3368, then moved unchanged (
git range-diffshows every patch the same) to #3510 and on to this PR, and rebased with them onto mainb735732ef59. On 2026-11-23 they were restacked onto #3510's review fixes: the per-type lookup now usesimportlib.util.find_spec, and #3368's isolation test keeps only its DeepSeek-V4 case here, since modelopt no longer imports any model's module up front. After #3368's DeepSeek-V4 fix that case checks that the module completes, with its indexer registered, when its Mega-MoE import fails. Below,2a18aca6203is #3510's commit, which imports every model's modules with modelopt, the same modules as the lists;0082ff42933andd9c2c6328d0are this PR's head before the splits, on an earlier main and on mainca8e34a63c4.main..HEAD0082ff42933andd9c2c6328d0, as the top of #3368, which then targeted maintests/gpu_vllmunder vLLM 0.30.0 218 passed and 8 skipped at both2a18aca6203(imported with modelopt) vs0082ff42933(imported by converting a config-only model of each type): the quant module registry,CUSTOM_MODEL_PLUGINS, the attention wrappers and the decoder discoverers in the order they are tried, under vLLM 0.22.1 (HF and vLLM plugins, without peft, whose import fails in that environment) and transformers 5.18 and 5.5.4tests/uniton CPU under transformers 5.18, the same two headstests/gpu_vllmunder a vLLM 0.29.1rc1 nightly, and the three touched vLLM test files under vLLM 0.28.0 and a GLM-5.3-Flash build2a18aca6203's side under 0.28.0, in a test outside this change. The replaced vLLM test passes on both sides; the new registration test passes at0082ff42933and fails against2a18aca6203's code, at its first checkself.configto the top-level HF config, whose types (deepseek_v4,glm5_next,kimi_k3) name their packages; GLM-5.3-Flash is absent from 0.28.0deepseek_v4) or from the decoder-layer lookup (a remote-code Nemotron-H'sbackbone.layersare not found), reading only the root model type (the nested-config case), droppingllama4_text's module (its case), registering the HF discoverer as an ordinary entry (the order check), and importing a model module with modelopt (both tests' up-front checks)import modelopt.torch.quantizationon CPU, median of 5 warm runs, without and with this PR's change (2a18aca6203vs0082ff42933)15f6b139e1bvs #3368's head: the three vLLM test files under vLLM 0.22.1, without the 11 end-to-end engine tests, whose setup never returns without a GPU; and under transformers 5.4.0,tests/unit/torch/quantization/plugins,tests/unit/torch/models, the keep-fused export tests and the registration test; and all oftests/unit/torch/quantizationvs mainNo CUDA GPUs are available); transformers 680 passed and 34 skipped on both;tests/unit/torch/quantization1,984 passed, 1 skipped and 1 failed, as on main, wheretest_hybrid_linear_attention_cache_round_tripsfails the same way for want of a newer transformersb24e4d3695a, which drops the RefinedWeb packages: the same suites vs15f6b139e1bb24e4d3695a, as #3510's head and again as this PR's first headlinux,windows, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs skip, as noted below the table13ac8c7f558vsb24e4d3695a: the same suites, plus the Kimi tests against stand-ins for vLLM's Kimi-K3 modules, which vLLM 0.22.1 lackstest_failed_mega_moe_import_keeps_deepseek_v4_indexer, which passes: vLLM 48 passed, 42 skipped and the same 12 failed; transformers 680 passed and 34 skipped;tests/unit/torch/quantization1,984 passed, 1 skipped and 1 failed, as before; the Kimi tests 11 passed and 1 skipped13ac8c7f558linux,windows, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs do not run while this PR is stacked, as noted below the table; two cancelled entries are a duplicate run the push also startedf568324d775vs13ac8c7f558: the vLLM test filesf568324d775linux,windows, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs do not run while this PR is stacked, as noted below the table; two cancelled entries are a duplicate run the push also startedc20cdc145f1vsf568324d775: the vLLM test files, plus a stand-in for GLM-5.3-Flash's native indexer, which vLLM 0.22.1 lackstest_vllm_plugin_registers_native_glm5next_indexer, which skips without a native indexer: vLLM 48 passed, 43 skipped and the same 12 failed; against the stand-in, the GLM-5.3-Flash test passesc20cdc145f1linux,windows, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/project. The GPU, example and regression jobs do not run while this PR is stacked, as noted below the table; two cancelled entries are a duplicate run the push also startedgit range-diff)linux,windows, the 8 multi-version jobs, the minimum-transformers one included, and the torch-only and onnx-only installs) and codecov/projectCI's GPU, example and regression jobs skip while this PR targets #3510's branch, and their
*-pr-required-checkjobs fail on purpose until it targets main; they run once #3510 merges (#3368 has).Before your PR is "Ready for review"
Commits are signed (
git commit -s -S).modeling_ptqimported first (see Usage).CONTRIBUTING.md: N/Atest_modeling_ptq_is_imported_on_conversionchecks that importing modelopt imports nomodeling_ptq, that a remote-code Nemotron-H's decoder layers are found before any conversion, that converting a Falcon model imports Falcon's and uses its wrapper, and the alias and nested-config cases;test_vllm_model_packages_are_imported_on_conversionchecks that converting a model of each vLLM package's type imports itsmodeling_ptq_vllm;test_vllm_plugin_registers_native_glm5next_indexerchecks, after converting a GLM-5.3-Flash model, that its native indexer and config key are registered wherever the vLLM build has a supported layout;test_failed_mega_moe_import_keeps_deepseek_v4_indexerchecks that DeepSeek-V4's module, imported directly, completes with its indexer registered when its Mega-MoE import fails)f04698ff43e, before these commits)Additional Information
Follow-up to #3510. Tracked by OMNIML-6533, for ModelOpt v0.50.0.
Known limitation, postponed: the lookup sees only the types in a model's config tree. A model that holds classes another type's module wraps, without that type in its config, needs a package whose
modeling_ptq.pyimports the shared one, asllama4_textdoes. None of today's modules needs more (checked against transformers 5.5.4 and 5.18.0 and vLLM 0.30.0); also taking types from the classes a model holds would close the gap.