Repository navigation
Speed up the hf_ptq example tests - #2727
Merged
Merged
Conversation
modelopt.torch.utils.network imported deepspeed.runtime.engine at module load to register DeepSpeedEngine as a wrapper, and make_deepspeed_compatible imported deepspeed's ZeROOrderedDict on every mtq.quantize call. Importing deepspeed takes ~5s, so every process that imports modelopt paid for it, used or not. A DeepSpeedEngine or ZeROOrderedDict can only exist once its module is imported, so look the class up in sys.modules when it is needed. This cuts hf_ptq.py startup from 18.4s to 13.8s, and the TRT-LLM deploy script's from 20.6s to 17.4s. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Chenjie Luo <[email protected]>
- Run single-GPU TRT-LLM deploys' executor in-process (TLLM_WORKER_USE_SINGLE_PROCESS=1). By default the LLM API spawns an MPI worker that imports TRT-LLM again: ~20s per TinyLlama deploy and ~68s per Qwen3-VL multimodal deploy, with identical generated outputs. - Collect example-test coverage with sys.monitoring (COVERAGE_CORE=sysmon). The default C tracer slowed a TinyLlama fp8 + deploy run from 65s to 103s; sysmon takes 70s and records the same lines. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Chenjie Luo <[email protected]>
Contributor
Contributor
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2727 +/- ##
==========================================
+ Coverage 71.67% 78.21% +6.53%
==========================================
Files 644 644
Lines 71470 71477 +7
==========================================
+ Hits 51225 55903 +4678
+ Misses 20245 15574 -4671
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:
|
meenchen
reviewed
Oct 9, 2026
meenchen
left a comment
Contributor
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.
Approving: the three speedups look correct, the deepspeed change keeps the same behaviour, and the new unit tests cover both lookup paths.
No action needed:
_supported_wrappers()returns the sameSUPPORTED_WRAPPERSdict, so the in-place registration inmcore_dist_checkpointing.py(SUPPORTED_WRAPPERS[Float16Module] = "module") still works. The test patches a copy of the dict, so the global isn't polluted.- Engines and ZeRO-3 dicts only exist after their deepspeed modules are imported, so looking them up in
sys.modulesmatches the old import-or-skip behaviour. Nothing else in the repo importsDeepSpeedEnginefromnetwork.py. - Passing
env=torun_example_commandturns off the in-process runner. That runner is only registered by the megatron_bridge conftest, so hf_ptq and llm_eval are unaffected. - The TP>1 nightly cases weren't run. The PR says
TLLM_WORKER_USE_SINGLE_PROCESSonly affects TP1 executors in TRT-LLM, but I couldn't check that against the TRT-LLM source. Watch the next 2-GPU nightly. - Tests were read, not run. No existing tests were changed. The new test file's header matches the standard NVIDIA header.
- Overlap: this touches
tests/_test_utils/examples/run_command.pyand the shared example runner workflow, which other open hf_ptq PRs also use (#2586 path move, #2596). Expect small rebases, though the PR says it's independent of #2596.
Complex PR: 1 existing test file modified or removed. Looping in a human for approval.
cjluo-nv
added a commit
that referenced
this pull request
Oct 13, 2026
…2771) ### What does this PR do? Type of change: Test infrastructure / CI time Follow-up to #2727. A TinyLlama `hf_ptq` example test spends most of its time on single-threaded Python startup and export while the GPU sits idle: only ~15s of a ~65s fp8 + deploy test is GPU work. PR runs of `trtllm (hf_ptq)` now run the tests in 4 pytest-xdist workers on the runner's one GPU. A shared/exclusive file lock (`tests/_test_utils/examples/gpu_lock.py`) controls which tests may share it: - **TinyLlama tests (`shared_gpu`) share the GPU.** Each gets a fixed calibration batch size equal to its calibration size, which is what the probe already chose. Without one, `hf_ptq.py` probes for a batch size by running a forward pass sized to fill all free GPU memory. - **T5, Mixtral and Qwen3-VL (`exclusive_gpu`) run alone.** The Qwen3-VL deploy (TRT-LLM's `quickstart_multimodal.py`) reserves 60% of free GPU memory for KV cache; measured at 60 GiB on a 96 GiB GPU. - **A waiting exclusive test holds back new shared tests**, so it starts as soon as the tests already on the GPU finish, not after all of them. - **The lock is taken in fixture setup.** `timeout_func_only` keeps the wait outside the per-test timeout. It is a no-op without xdist, and `flock` releases it if a worker dies. Each test also gets its own `ROOT_SAVE_PATH`. `huggingface_example.sh` names a checkpoint directory from the model and format alone. Runs that differ only in KV format, TP or PP therefore shared one directory, and the later run found `config.json` and skipped PTQ. Concurrently, they would race. `-n 4` applies to `hf_ptq` PR runs only; the 2-GPU nightly stays serial. `pytest-xdist` is declared in `dev-test`; CI only had it so far because the TRT-LLM image ships it. `uv.lock` adds only its entries. A full `uv lock` with a different uv version also reorders unrelated markers, so I kept only the hunks it adds for `pytest-xdist` and `execnet`. The weekly bump will normalize the rest. ### Usage N/A (CI/test infrastructure) ### Testing - **Full `tests/examples/hf_ptq` lane, `-n 4`, one RTX PRO 6000** in `nvcr.io/nvidia/tensorrt-llm/release:1.3.0rc20` with CI's coverage settings: 233 passed, 0 failed, 10:25. The Qwen3-VL tests calibrated on `cnn_dailymail` locally, because there was no `HF_TOKEN` for the gated default dataset. - **Lock event log from that run:** no test overlapped an exclusive one. Each Qwen3-VL test acquired the GPU as soon as the TinyLlama tests already running finished, and at most 4 tests held it at once. - **Caveat on that run:** another user's vLLM job (60–75 GB) shared the GPU throughout. The run passing shows it tolerates having only ~20–35 GB free. But its memory peaks can't be attributed to these tests, and its time was measured under contention. - **Earlier clean run on an idle GPU,** with every test sharing (before the exclusive lock existed): 871s serial → 312s with `-n 4` (845s → 301s when pinned to 8 CPU cores). 4 TinyLlama workers peaked at ~19 GB of GPU memory and ~40 GB of host RAM (28 GB serial). Covered lines were within 0.25%. - **`tests/examples/hf_ptq/test_gpu_lock.py`** covers shared overlap, exclusive isolation, and a waiting exclusive holder queuing new shared ones. Each test fails against a deliberately broken lock: no turnstile, always shared, or always exclusive. - **pre-commit** on the changed files. - **Not run:** - a clean serial vs `-n 4` comparison with the Qwen3-VL deploys running; this PR's CI run will be the real measurement; - the 2-GPU nightly. ### Before your PR is "*Ready for review*" - Is this change backward compatible?: ✅ - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: ✅ (`pytest-xdist`, MIT, test-only) - Did you write any new necessary tests?: ✅ - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: N/A (CI/test infrastructure) - Did you get Claude approval on this PR?: ❌ (not yet run) ### Additional Information Effects to be aware of: - **Nightly:** the per-test `ROOT_SAVE_PATH` means the two 2-GPU `test_ptq_llama` cases now actually run PTQ and deploy on 2 GPUs. Until now they silently reused the 1-GPU checkpoints. The nightly gets a few minutes slower and could surface real 2-GPU failures. - **Disk:** checkpoints move from `examples/hf_ptq/` to pytest's temp directory, about the same total (~20 GB). - **Host RAM:** 4 workers peaked around 40 GB in the clean run. I don't know the runner's RAM. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Some pull-request test runs now execute in parallel, while other test runs retain their existing behavior. * GPU-based example tests now coordinate access so compatible tests can run together while tests requiring exclusive access run separately. * Added coverage for GPU access coordination and updated quantization tests to use the appropriate access mode. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Chenjie Luo <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
shengliangxu
added a commit
that referenced
this pull request
Oct 17, 2026
Python 3.12 defaults to coverage's C tracer, which slows every line of Python that runs under it. Every lane starts coverage in each spawned worker and subprocess through COVERAGE_PROCESS_START, so the import-heavy start-ups pay for it: the 76 s Megatron worker-pool start, the 22 s hf_ptq processes of the unified-export tests, the puzzletron spawns, the vLLM engine core. COVERAGE_CORE=sysmon records the same lines through sys.monitoring at a fraction of the overhead. Measured (the first three here, the last by #2727 on its hf_ptq example lane): - serial tests/unit run on 4 CPUs: 572 s without coverage, 825 s with the C tracer, 640 s with sysmon; - `import modelopt.torch.quantization.extensions` with this repo's coverage config: 17.1 s native, 23.6 s with the C tracer, 18.3 s with sysmon, and the child process of a spawn writes its data file too, with the same 362 files and 13,821 executed lines; - one TinyLlama fp8 plus TRT-LLM deploy test: 64.8 s without coverage, 103.3 s with the C tracer, 70.5 s with sysmon, identical covered lines. Applies to the unit `linux` job (Python 3.12), gpu_tests.yml and regression_tests.yml. The example runner is left to #2727, which makes the same change there. sysmon needs Python 3.12 or newer; on an older interpreter coverage warns and keeps its default core, so a lane that turns out to run an older Python is slower, not broken. The gpu and gpu_megatron logs show 3.12. Testing: actionlint is clean on all workflows. Not yet run on a real CI runner; the first run should be compared against the previous coverage.xml. Signed-off-by: Shengliang Xu <[email protected]>
shengliangxu
added a commit
that referenced
this pull request
Oct 21, 2026
### What does this PR do? Type of change: CI (test runtime) Sets `COVERAGE_CORE=sysmon` on the unit `linux` job, `gpu_tests.yml` and `regression_tests.yml`. Python 3.12 defaults to coverage's C tracer, which slows every line of Python run under it, and every lane starts coverage in each spawned worker and subprocess through `COVERAGE_PROCESS_START`, so import-heavy start-ups pay for it (the 76 s Megatron pool start, the 22 s hf_ptq processes of the unified-export tests, the puzzletron spawns, the vLLM engine core). sysmon records the same lines through `sys.monitoring`. The example runner is left to #2727, which makes the same change there. On a Python older than 3.12 coverage warns and keeps its default core, so a lane that turned out to run an older Python would be slower, not broken (the gpu and gpu_megatron logs show 3.12). ### Time improvement (measured in CI) pytest time of each lane in the run of #2845 (the base of this PR) and in this PR's run, one run each: | lane | #2845 | this PR | change | |---|---|---|---| | gpu | 2,093 s | 1,289 s | -38% | | gpu_megatron | 2,240 s | 1,212 s | -46% | | gpu_vllm v0.20.0 | 204 s | 170 s | -17% | | gpu_vllm v0.30.0 | 426 s | 328 s | -23% | | regression | 366 s | 268 s | -27% | | unit `linux` (xdist) | 479 s | 305 s | -36% | Workflow wall time: GPU tests 59.2 -> 39.6 min, Regression 21.3 -> 15.2 min. - **How much of that is this PR.** Two single runs of the same code differ by 15-30% on their own: the eight example lanes that run over 100 s, which this PR does not touch, moved by -4% to -31% (llm_sparsity +38%) between the same two runs. The deterministic part is the Megatron worker-pool start, the setup of the first test of each gpu_megatron module: 75-77 s in each of 14 modules before (all setups of 12 s or more add up to 1,365 s) and 22-27 s after (575 s), which is 790 s of the lane's 1,028 s. Locally the unit suite takes 825 s with the C tracer, 640 s with sysmon and 572 s without coverage (4 CPUs, serial). - **A `coverage.xml`-only report change was dropped from this PR.** The runs above are of its earlier head, which also had it. It showed no CI effect: the pytest summary line came 1-4 s after the last test in the #2845 runs, which still wrote the html report, and 0-3 s in this PR's runs (median 1.1 s over the 82 lane runs of the series), so the table is the sysmon core's. It would only have saved developers' local runs (about 25 s per pytest session). Method: every PR of the series has its own CI run, so "before" is the run of the PR(s) below that lack the change and "after" the runs that carry it. Times come from the job logs (`--durations`, or the gap between consecutive result lines). Where runner speed matters, the tests that no PR of the series touches scale each run, and the raw and the scaled medians are both shown. One test varies by 15-30% from run to run on its own, so smaller differences are not evidence. ### Usage Nothing user-facing. ### Testing Measured on this tree (tests/unit on 4 CPUs serial: 572 s without coverage, 825 s with the C tracer, 640 s with sysmon; `import modelopt.torch.quantization.extensions`: 17.1 s native, 23.6 s with the C tracer, 18.3 s with sysmon, with the same 362 files and 13,821 executed lines in the spawned child's data file; one TinyLlama fp8 plus TRT-LLM deploy test: 64.8 s without coverage, 103.3 s with the C tracer, 70.5 s with sysmon, identical covered lines). Pre-commit and actionlint pass. The CI runs of the series are the check on real runners (time section above). ### Before your PR is "*Ready for review*" Make sure you read and follow [Contributor guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md) and your commits are signed (`git commit -s -S`). Make sure you read and follow the [Security Best Practices](https://github.com/NVIDIA/Model-Optimizer/blob/main/SECURITY.md#security-coding-practices-for-contributors) (e.g. avoiding hardcoded `trust_remote_code=True`, `torch.load(..., weights_only=False)`, `pickle`, etc.). - Is this change backward compatible?: N/A (CI and tests) - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: no new dependency - Did you write any new necessary tests?: N/A (CI configuration) - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: N/A (CI and tests) - Did you get Claude approval on this PR?: N/A (optional, not run) ### Additional Information Slice 2 of 11 of the GPU-CI speed-up work, split out of the reference draft #2730 (not for merge; it holds the whole series with its measurements). Slices 5, 8 and 10 (#2849, #2852, #2854) target main and merge independently; the other open slices form a stack and merge bottom-up. Each PR stands on its own and passes CI without the later ones. 1. #2845 [OMNIML-6178] [1/11] Run the unit tests in parallel with pytest-xdist (merged) 2. #2846 [OMNIML-6178] [2/11] Use the sysmon coverage core in CI **(this PR)** 3. #2847 [OMNIML-6179] [3/11] Run manual-marked tests on nightly and on-demand GPU runs 4. #2848 [OMNIML-6181] [4/11] Give gpu_megatron's slow cold starts more time 5. #2849 [OMNIML-6179] [5/11] Make the ONNX tests cheaper: CPU-only ones to the unit lane, small models (targets main) 6. #2850 [OMNIML-6179] [6/11] Use local tokenizers and calibration text in GPU tests, shrink puzzletron data 7. #2851 [OMNIML-6180] [7/11] Trim the llm_eval, llm_qat, EAGLE and megatron_bridge example tests 8. #2852 [OMNIML-6180] [8/11] Trim the DFlash regression test (targets main) 9. #2853 [OMNIML-6180] [9/11] Trim the torch_onnx example lane 10. #2854 [OMNIML-6181] [10/11] Make the distributed worker pool fail fast when a worker dies (targets main) 11. #2855 [OMNIML-6181] [11/11] Share one Megatron worker pool across the gpu_megatron modules <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Updated coverage collection for GPU, regression, and Linux unit-test runs to use Python’s monitoring-based coverage core on supported Python 3.12+ environments. * This configuration reduces overhead during coverage-enabled test execution, particularly for import-heavy worker processes. * No changes to application functionality or public interfaces are included. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Shengliang Xu <[email protected]>
kevalmorabia97
added a commit
that referenced
this pull request
Oct 28, 2026
### What does this PR do? Type of change: Bug fix (release branch only) `modelopt/torch/utils/network.py` wrapped the optional `deepspeed` import in a bare `except:`, which also swallowed `SystemExit` and `KeyboardInterrupt` during `import modelopt` (SonarQube `python:S5754` on `release/0.48.0`). It now catches `Exception`. `ImportError` alone is not enough: deepspeed can fail at import with other errors (e.g. CUDA/op-builder checks), and those should keep falling back to "no deepspeed" rather than break `import modelopt`. `main` no longer has this code: #2727 replaced the import with a `sys.modules` lookup as part of a larger CI speed-up PR, so this is a minimal release-only equivalent instead of a cherry-pick. ### Testing - `tests/unit/torch/utils/test_network.py`: 50 passed. - `import modelopt.torch.utils.network` with deepspeed installed still registers `DeepSpeedEngine`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved compatibility when DeepSpeed cannot be imported. The application now handles import errors and continues with DeepSpeed unavailable, rather than allowing those errors to interrupt the fallback behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Keval Morabia <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Type of change: Test infrastructure / CI time, plus an import-time fix in modelopt
trtllm (hf_ptq)is the slowest example job. I profiled it in the CI image (TRT-LLM 1.3.0rc20, one RTX PRO 6000). A TinyLlama fp8 test with the TRT-LLM deploy step does about 15s of real work. The rest is Python startup in three processes and coverage tracing. This PR makes three changes:sys.monitoring(COVERAGE_CORE=sysmonin_example_tests_runner.yml). CI starts coverage in every example subprocess throughCOVERAGE_PROCESS_START. On Python 3.12, coverage defaults to its C tracer, which slows the fake-quantgenerateand the export paths. The same TinyLlama fp8 + deploy run takes 64.8s with no coverage, 103.3s with the C tracer, and 70.5s with sysmon. Both runs cover the same lines: 17,424 inhf_ptq.pyand 14,534 inrun_tensorrt_llm.py, with none differing. Every example image ships Python 3.12, which sysmon needs.TLLM_WORKER_USE_SINGLE_PROCESS=1inrun_hf_ptq_command). For TP1, the LLM API otherwise spawns an MPI worker that imports TRT-LLM again. The TinyLlama deploy step drops from 51s to 31s, and the Qwen3-VL multimodal test from 184s to 116s. Generated outputs are identical. TP>1 deploys (the nightly multi-GPU cases) still spawn workers. The llm_eval tests share this helper, so their TP1 deploys benefit too.modelopt/torch/utils/network.pyimporteddeepspeed.runtime.engineat module load, andmake_deepspeed_compatibleimported deepspeed'sZeROOrderedDicton everymtq.quantize. ADeepSpeedEngineorZeROOrderedDictcan only exist once its module is imported, so both now look the class up insys.modules.hf_ptq.pystartup drops from 18.4s to 13.8s, andrun_tensorrt_llm.pyfrom 20.6s to 17.4s. Every process that imports modelopt saves this time.Results
I ran the full
tests/examples/hf_ptqlane in the CI image with CI's coverage settings on one GPU, on top of #2596:The same tests took about twice as long per test on the CI runners as on this machine. The absolute savings in CI should therefore be larger.
Usage
N/A (CI/test infrastructure; no API change)
Testing
tests/examples/hf_ptqlane, before and after (as above). 217 tests passed in both runs. The twotest_qwen_vlcases failed in both, because the container had noHF_TOKENfor the gated default calibration dataset. I ran the same VLM fp8 command withcnn_dailymail, with and without the flag. Both passed with identical generated text.test_qwen3_eval_fp8's command (MMLU, lm_eval, simple_eval viatrtllm-serve) throughrun_hf_ptq_commandwithcnn_dailymailcalibration, for the same gated-dataset reason. It passes.tests/unit/torch/utils/test_network.pyandtests/unit/torch/quantization/plugins/test_transformers_deepspeed.py.tests/unit/torch/{utils,opt,quantization/plugins,distill}pass serially in the CI image. Under-n 16, a different handful of tests fails on each run, on bothmainand this branch.Before your PR is "Ready for review"
CONTRIBUTING.md: N/AAdditional Information
This PR is independent of #2596, which trims the hf_ptq test cases. The two touch different files and can merge in either order.
Considered and not done:
max_position_embeddingsmakes the TRT-LLM deploy reserve 9.5GB instead of 2.5GB.calib_batch_sizewould need pinning.🤖 Generated with Claude Code
Summary by CodeRabbit
sysmontracer with Python 3.12 or later.