Repository navigation
Fix import modelopt.torch without the [hf] extra - #2671
kevalmorabia97 merged 3 commits into
Conversation
modelopt.torch failed to import on a base `pip install nvidia-modelopt`: utils/dataset_utils.py imported `requests` and opt/plugins/huggingface.py imported `huggingface_hub` at module scope, but neither is a base dependency. Both modules are imported unconditionally by modelopt.torch, so the import failed with ModuleNotFoundError even though the docs state the base install covers modelopt.torch. Import each one inside the only function that uses it, next to the other optional Hugging Face imports there, and add a test that imports modelopt.torch with the [hf] extra's modules unavailable. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: LinCanNerd <[email protected]>
|
@AAnoosheh When you have a moment, could you run |
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review (gpt-6.1-sol) — DM the bot to share feedback.
Approve: deferring optional dependency imports fixes base-install loading without changing the affected feature logic, and the subprocess test covers the regression.
No action needed.
|
@LinCanNerd can you fix merge conflicts? |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2671 +/- ##
==========================================
+ Coverage 76.93% 78.87% +1.93%
==========================================
Files 658 658
Lines 73815 73815
==========================================
+ Hits 56791 58222 +1431
+ Misses 17024 15593 -1431
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:
|
…se-install-imports Signed-off-by: LinCanNerd <[email protected]> # Conflicts: # CHANGELOG.rst
Signed-off-by: Keval Morabia <[email protected]>
|
/ok to test 43eaad9 |
### What does this PR do? Type of change: new tests #2671 made `import modelopt.torch` work on a base install and added `tests/unit/torch/test_import.py` to keep it that way. This extends that test: in the same subprocess, with the `[hf]` extra's modules still made unavailable, it also quantizes a small model (`mtq.INT8_DEFAULT_CFG`, max calibration). The import alone doesn't cover this. In 0.47.0, even with #2671 applied, `mtq.quantize` on a base install fails: ``` File ".../modelopt/torch/quantization/model_calib.py", line 99, in _collect_grouped_linears from modelopt.torch.export.layer_utils import _GATE_UP_PAIRS File ".../modelopt/torch/export/__init__.py", line 28, in <module> from .unified_export_megatron import * File ".../modelopt/torch/export/unified_export_megatron.py", line 32, in <module> from huggingface_hub import get_safetensors_metadata, hf_hub_download ModuleNotFoundError: No module named 'huggingface_hub' ``` Max calibration checks that fused sibling linears share one weight `global_amax`, and since #1605 (0.46.0) it looked up the gate/up projection names in `modelopt.torch.export`. #1828 replaced that lookup with `modelopt.torch.models.list_all_possible`, so main and `release/0.48.0` quantize fine. No test would catch it coming back: every CI environment has `huggingface_hub`, if only through `timm` from `dev-test`. The quantize step goes into the existing subprocess rather than a second one, following CONTRIBUTING's rule for unit tests that spawn processes. On a shared x86 builder, over 5 interleaved runs, the subprocess used a median 4.37 s of CPU time both with and without the quantize step; the difference is within the run-to-run spread (4.1 to 4.7 s), so nearly all of it is the import. ### Usage N/A ### Testing `tests/unit/torch/test_import.py`, on main at efda42c, in an environment with `huggingface_hub` installed (through `timm`): - With this PR: passes. - The same, with `import modelopt.torch.export` added back to `_collect_grouped_linears`: fails at the quantize step, with `ModuleNotFoundError: import of huggingface_hub halted; None in sys.modules`. - 0.47.0 with #2671 and this test: fails the same way, at the `_GATE_UP_PAIRS` import. - `pre-commit run` with this repository's configuration (ruff 0.15.20, mypy 2.1.0) passes on the changed file. Earlier, without the test, in environments that really lack the `[hf]` extra (modelopt and its required dependencies only): INT8 quantization of a small model, in one process and in two gloo ranks, fails on 0.47.0 with #2671, and passes on main (then 3788881) and on `release/0.48.0` with #2671. ### 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`: N/A - Did you write any new necessary tests?: ✅ - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: N/A - Did you get Claude approval on this PR?: N/A ### Additional Information Found while packaging 0.47.0 for Nixpkgs (NixOS/nixpkgs#577198). `release/0.48.0` doesn't have `test_import.py` yet, so this belongs with #2671's cherry-pick, if it's wanted there. **AI disclosure:** this change, the testing described above and this description were produced with Claude Code (Claude Opus 5.5). 🤖 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 * **Tests** * Expanded PyTorch import coverage to verify that a small model can be quantized with calibration when optional Hugging Face modules are unavailable. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Connor Baker <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
#2928 #2885 #2908 #2929 #2950 #2972 #2877 #2951 #2816 #3005 #3012 #2971 #3015 #3006 #3026 #2988 #3028 #1860 #3030 #2612 #3033 #2653 #2599 #2655 #2618 #2868 #3032 #3027 #2869 #2984 #2975 #2345 #3020 #2997 (#3072) ## Cherry-picked PRs - #2817 - #2671 - #2892 - #2876 - #2910 - #2911 - #2919 - #2904 - #2937 - #2928 - #2885 - #2908 - #2929 - #2950 - #2972 - #2877 - #2951 - #2816 - #3005 - #3012 - #2971 - #3015 - #3006 - #3026 - #2988 - #3028 - #1860 - #3030 - #2612 - #3033 - #2653 - #2599 - #2655 - #2618 - #2868 - #3032 - #3027 - #2869 - #2984 - #2975 - #2345 - #3020 - #2997 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Diffusion exports now offer more control over attention-quantizer metadata and preserve sparse-attention settings in a format compatible with downstream tooling. * Model loading better handles select bundled model implementations, and Docker guidance now covers writable CUDA-extension cache locations. * **Bug Fixes** * Improved reliability for model export and checkpoint saving, quantization across devices, ONNX processing, and calibration. * Temporary TensorRT and autotuning artifacts now use private directories by default. * **Documentation** * Updated installation guidance, compatibility notes, and links; corrected typos across guides and examples. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Chad Voegele <[email protected]> Signed-off-by: Rohan Joshi <[email protected]> Signed-off-by: LinCanNerd <[email protected]> Signed-off-by: Keval Morabia <[email protected]> Signed-off-by: weimingc <[email protected]> Signed-off-by: Noey Yang <[email protected]> Signed-off-by: Kai Xu <[email protected]> Signed-off-by: Zhehao Hu <[email protected]> Signed-off-by: noeyy-mino <[email protected]> Signed-off-by: joe-nv <[email protected]> Signed-off-by: yingguo-trt <[email protected]> Signed-off-by: Shengliang Xu <[email protected]> Signed-off-by: Chenjie Luo <[email protected]> Signed-off-by: anmaurya001 <[email protected]> Signed-off-by: Connor Baker <[email protected]> Signed-off-by: wiederse <[email protected]> Signed-off-by: Vu Dinh <[email protected]> Signed-off-by: Pratik Gandhi <[email protected]> Signed-off-by: Michael Rusu <[email protected]> Signed-off-by: Michael-Rusu <[email protected]> Signed-off-by: namgyu-youn <[email protected]> Signed-off-by: Sylvester Kaczmarek <[email protected]> Signed-off-by: Javier de Jesus <[email protected]> Signed-off-by: javierdejesusda <[email protected]> Signed-off-by: Zhiyu Cheng <[email protected]> Signed-off-by: Juhi Mittal <[email protected]> Signed-off-by: Dmitry Rantovov <[email protected]> Signed-off-by: h-guo18 <[email protected]> Co-authored-by: Chad Voegele <[email protected]> Co-authored-by: LinCanNerd <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Keval Morabia <[email protected]> Co-authored-by: Wei-Ming Chen <[email protected]> Co-authored-by: noeyy-mino <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: kaix-nv <[email protected]> Co-authored-by: Zhehao Hu <[email protected]> Co-authored-by: joe <[email protected]> Co-authored-by: yingguo-trt <[email protected]> Co-authored-by: Shengliang Xu <[email protected]> Co-authored-by: Chenjie Luo <[email protected]> Co-authored-by: anmaurya001 <[email protected]> Co-authored-by: Connor Baker <[email protected]> Co-authored-by: Jiao Mingyang <[email protected]> Co-authored-by: Vu Dinh <[email protected]> Co-authored-by: Pratik Gandhi <[email protected]> Co-authored-by: Michael R <[email protected]> Co-authored-by: Michael-Rusu <[email protected]> Co-authored-by: namgyu-youn <[email protected]> Co-authored-by: Sylvester Kaczmarek <[email protected]> Co-authored-by: Javier De Jesus <[email protected]> Co-authored-by: Zhiyu <[email protected]> Co-authored-by: Juhi Mittal <[email protected]> Co-authored-by: Dmitry Rantovov <[email protected]> Co-authored-by: h-guo18 <[email protected]>
What does this PR do?
Type of change: Bug fix
import modelopt.torchfails on a base install (pip install nvidia-modelopt, no extras), although the installation docs state that the base install provides themodelopt.torchrequirements. Reproduced with the released 0.47.0 in a fresh venv containing onlytorchandnvidia-modelopt==0.47.0:Root cause: two modules that
modelopt.torchalways imports have module-scope imports of packages that are not base dependencies:modelopt/torch/utils/dataset_utils.pyimportsrequests(since Allow basename of dataset paths to match registered names #997). It is only used indownload_hf_dataset_as_jsonl, which already importsdatasetsandhuggingface_hublocally.requestsis not declared anywhere; it was only present transitively.modelopt/torch/opt/plugins/huggingface.pyimportshuggingface_hub(since Add experimental support for transformers>=5.0 + min torch 2.8 #975).modelopt/torch/opt/__init__.pyimports this plugin underimport_plugin("huggingface"), butopt/plugins/__init__.pyimports it unconditionally, andunified_export_hf.pyimports from it directly. It is only used in_get_modelopt_state_path.The fix moves each import into the only function that uses it. CI did not catch this because every test environment installs extras that bring both packages in, so the new
tests/unit/torch/test_import.pyimportsmodelopt.torchin a subprocess with the[hf]extra's modules (plusrequests) made unavailable.Out of scope:
modelopt.torch.export(importshuggingface_hubdeliberately viautils/plugins/hf_checkpoint_utils.py, #2427) andmodelopt.torch.puzzletron(has its own[puzzletron]extra) still need their optional dependencies.Usage
No API changes.
Testing
torchand this branch installed (pip install -e ., base dependencies only):import modelopt.torchandmodelopt.torch.{opt,quantization,sparsity,distill,nas,prune,speculative,peft,trace,utils,fastgen,models}all import; before the change every one failed with theModuleNotFoundErrorabove.tests/unit/torch/test_import.pyfails onmain(import of requests halted; None in sys.modules) and passes with this change.pytest tests/unit/torch/utils/test_dataset_utils.py tests/unit/torch/opt tests/unit/torch/test_import.py: 122 passed (withdatasetsinstalled,test_dataset_utils.py: 70 passed).pre-commit runon the changed files: all hooks pass.Found while setting up ModelOpt with pip-only dependencies on a Jetson AGX Thor.
Before your PR is "Ready for review"
Make sure you read and follow Contributor guidelines and your commits are signed (
git commit -s -S).Make sure you read and follow the Security Best Practices (e.g. avoiding hardcoded
trust_remote_code=True,torch.load(..., weights_only=False),pickle, etc.).CONTRIBUTING.md: N/AAdditional Information
🤖 Generated with Claude Code
Summary by CodeRabbit
modelopt.torchfrom importing when optional Hugging Face dependencies were not installed. The package can now be imported without those dependencies. Features that need them, such as downloading datasets or checking the Hugging Face model cache, still require the relevant optional dependencies.