Skip to content

[OMNIML-5105] Fix torch import error to remove circular dependency & move Nemotron configs #1606

Merged
jenchen13 merged 6 commits into
mainfrom
jennifchen/fix_import
Jun 23, 2026
Merged

[OMNIML-5105] Fix torch import error to remove circular dependency & move Nemotron configs #1606
jenchen13 merged 6 commits into
mainfrom
jennifchen/fix_import

Conversation

@jenchen13

@jenchen13 jenchen13 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Bug fix
when running Megatron-LM modelopt example generate.py a circular import causes an error. the cause was because it import modelopt.torch.quantization which imports modelopt/torch -->

modelopt/torch/__init__.py:26. The chain is: import modelopt.torch.opt → runs modelopt/torch/__init__.py (parent package) → which pulls in distill
  → distill/mode.py:25 → back into opt before opt.utils is bound.

Usage

# Add a code snippet demonstrating how to use this

Testing

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.).

  • Is this change backward compatible?: ✅ / ❌ / N/A
  • 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?: ✅ / ❌ / N/A
  • Did you update Changelog?: ✅ / ❌ / N/A
  • Did you get Claude approval on this PR?: ✅ / ❌ / N/A

Additional Information

Summary by CodeRabbit

  • Chores

    • Improved package initialization stability to prevent issues during model compilation.
    • Updated PTQ example/workflow configurations to use the nvfp4-mse and nvfp4-max-calib quantization variants, including matching export and vLLM checkpoint/output paths.
  • Documentation

    • Refreshed PTQ recipe and changelog references to the new recipe YAML locations.
    • Minor comment wording/capitalization updates in Nemotron-3 Super/A12B PTQ recipe files.

@jenchen13 jenchen13 requested review from a team as code owners June 2, 2026 17:17
@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Reorders modelopt.torch subpackage imports to load opt before dependent modules and updates Nemotron PTQ recipe paths, small recipe header comments, changelog entries, and launcher example configs to reference nvfp4-mse / nvfp4-max-calib.

Changes

Torch Package Import Ordering

Layer / File(s) Summary
Torch import ordering with circular-dependency avoidance
modelopt/torch/__init__.py
Subpackage imports reordered with opt loaded first, wrapped in isort directives and comments explaining the ordering to prevent circular-import failures during torch.compile() registration.

Nemotron PTQ recipes and launcher examples

Layer / File(s) Summary
Changelog and recipe header edits
CHANGELOG.rst, modelopt_recipes/.../ptq/nvfp4-max-calib.yaml, modelopt_recipes/.../ptq/nvfp4-mse.yaml
CHANGELOG updated to new huggingface/.../ptq/ recipe paths; two PTQ recipe header comments reworded (documentation-only).
Launcher example: Nemotron-3-Super-120B PTQ smoke pipeline
tools/launcher/examples/nvidia/.../NVIDIA-Nemotron-3-Super-120B-A12B-BF16/megatron_lm_ptq.yaml
Pipeline note and QUANT_CFG env values updated to use nvfp4-mse; vLLM task now loads/saves *_nvfp4-mse exported checkpoints.
Launcher example: Nemotron-3-Ultra-550B PTQ pipeline
tools/launcher/examples/nvidia/.../NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16/megatron_lm_ptq.yaml
Pipeline note and QUANT_CFG env values updated to use nvfp4-max-calib; vLLM task now loads/saves nvfp4-max-calib exported checkpoints.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • realAsma
🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: fixing a torch circular import dependency and moving Nemotron configurations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns ✅ Passed PR introduces no security anti-patterns: only reorders imports, updates docs, and modifies YAML configs with no new security-sensitive code patterns.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jennifchen/fix_import

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-06-23 18:21 UTC

@coderabbitai coderabbitai Bot left a comment

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.

🧹 Nitpick comments (1)
modelopt/torch/__init__.py (1)

26-40: ⚡ Quick win

Make the package exports explicit here.

This import-order fix looks fine, but modelopt.torch still relies on implicit package exports. Please add an explicit __all__ for the intended public subpackages here, and if this package is meant to re-export submodule symbols, align the __init__.py with the repo’s from .module import * pattern as well.

♻️ Minimal direction
 """Model optimization and deployment subpackage for torch."""
 
+__all__ = [
+    "opt",
+    "distill",
+    "nas",
+    "peft",
+    "prune",
+    "quantization",
+    "sparsity",
+    "speculative",
+    "utils",
+]
+
 import importlib
 import warnings as _warnings

As per coding guidelines, **/*.py: "Define the public API with __all__ at the top of each Python module and re-export submodules in __init__.py files using from .module import * to keep the public API explicit and make star-imports safe".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt/torch/__init__.py` around lines 26 - 40, Add an explicit public API
to modelopt.torch by defining __all__ in this __init__.py listing the exported
subpackages (e.g. "opt", "distill", "nas", "peft", "prune", "quantization",
"sparsity", "speculative", "utils"), and if any of these subpackages are
intended to re-export symbols at package-level follow the repo pattern by adding
corresponding from .<module> import * lines for those modules (keep the
import-order fix intact and ensure __all__ reflects exactly the names you
re-export).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@modelopt/torch/__init__.py`:
- Around line 26-40: Add an explicit public API to modelopt.torch by defining
__all__ in this __init__.py listing the exported subpackages (e.g. "opt",
"distill", "nas", "peft", "prune", "quantization", "sparsity", "speculative",
"utils"), and if any of these subpackages are intended to re-export symbols at
package-level follow the repo pattern by adding corresponding from .<module>
import * lines for those modules (keep the import-order fix intact and ensure
__all__ reflects exactly the names you re-export).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 30f55e52-9764-4583-9c50-4a144a1491ca

📥 Commits

Reviewing files that changed from the base of the PR and between 8f96832 and 5353089.

📒 Files selected for processing (3)
  • modelopt/torch/__init__.py
  • modelopt_recipes/huggingface/Nemotron-3-Super-120B-A12B/super-nvfp4-max-calib.yaml
  • modelopt_recipes/huggingface/Nemotron-3-Super-120B-A12B/super-nvfp4.yaml

@codecov

codecov Bot commented Jun 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.73%. Comparing base (c3b913b) to head (422a8fe).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1606       +/-   ##
===========================================
+ Coverage   62.88%   76.73%   +13.84%     
===========================================
  Files         511      511               
  Lines       56634    56634               
===========================================
+ Hits        35615    43456     +7841     
+ Misses      21019    13178     -7841     
Flag Coverage Δ
examples 42.10% <ø> (+4.09%) ⬆️
gpu 57.87% <ø> (+37.30%) ⬆️
regression 14.71% <ø> (+0.04%) ⬆️
unit 54.63% <ø> (ø)

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.

@jenchen13 jenchen13 enabled auto-merge (squash) June 2, 2026 17:38
@@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Mirrors the published nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4 hf_quant_config.json:
# but with ONE major difference: use max calibration instead of MSE
# Approximately mirrors the published nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4 hf_quant_config.json:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why do we not just publish the same MSE based recipe?

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.

that's AutoQuant derived, very specific on certain layers and not generalizable

@@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let's move into huggingface/nemotron_h/ptq/

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 is model specific, I would like to keep the model specific folder

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

sorry, I actually mean huggingface/nemotron_h/Nemotron-3-Super-120B-A12B/ptq/

Nemotron-3-Super-120B-A12B is of nemotron_h model type

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.

that will make the recipe name super long .. I don't understand why we need a huggingface/ folder when we could just store models in top level modelopt_recipes so that the recipe would look like HuggingFace structure e.g. meta-llama/llama-3.1-8b

we also only have PTQ recipes right now so adding the ptq/ is redundant

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree with Jenny. It should just be exactly the same as the hugging face hub id. For example,

modelopt_recipes/nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-BF16/nvfp4_max_calib_hf_ptq.yaml

@jenchen13 jenchen13 force-pushed the jennifchen/fix_import branch from 5353089 to 2b147c5 Compare June 11, 2026 16:06
@jenchen13 jenchen13 requested a review from ajrasane June 11, 2026 16:11
@jenchen13 jenchen13 changed the title Fix torch import error to remove circular dependency & move Nemotron configs [OMNIML-5105] Fix torch import error to remove circular dependency & move Nemotron configs Jun 12, 2026
and Move Nemotron recipes to Hugging Face paths

Signed-off-by: Jennifer Chen <jennifchen@nvidia.com>
Signed-off-by: Jennifer Chen <jennifchen@nvidia.com>
@jenchen13 jenchen13 force-pushed the jennifchen/fix_import branch from 2b147c5 to 0f25f65 Compare June 12, 2026 16:22
Signed-off-by: Jennifer Chen <jennifchen@nvidia.com>

@coderabbitai coderabbitai Bot left a comment

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.

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@tools/launcher/examples/nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16/megatron_lm_ptq.yaml`:
- Around line 31-32: MLM_MODEL_CFG currently points to
nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16 while QUANT_CFG references
huggingface/models/nvidia/Nemotron-3-Super-120B-A12B/ptq/nvfp4-max-calib which
is a recipe mismatch; either update QUANT_CFG to an Ultra-specific recipe when
available or add an inline comment next to QUANT_CFG clarifying that the
Ultra-550B reuses the Super-120B nvfp4-max-calib recipe intentionally (reference
MLM_MODEL_CFG and QUANT_CFG and the nvfp4-max-calib recipe name in the comment).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 198d214d-ba62-44cf-88f1-6c55ecb78776

📥 Commits

Reviewing files that changed from the base of the PR and between 2b147c5 and 0f25f65.

📒 Files selected for processing (7)
  • CHANGELOG.rst
  • modelopt/torch/__init__.py
  • modelopt_recipes/huggingface/models/nvidia/Nemotron-3-Nano-4B/ptq/nvfp4_w4a16.yaml
  • modelopt_recipes/huggingface/models/nvidia/Nemotron-3-Super-120B-A12B/ptq/nvfp4-max-calib.yaml
  • modelopt_recipes/huggingface/models/nvidia/Nemotron-3-Super-120B-A12B/ptq/nvfp4-mse.yaml
  • tools/launcher/examples/nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-BF16/megatron_lm_ptq.yaml
  • tools/launcher/examples/nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-BF16/megatron_lm_ptq.yaml
✅ Files skipped from review due to trivial changes (3)
  • modelopt_recipes/huggingface/models/nvidia/Nemotron-3-Super-120B-A12B/ptq/nvfp4-max-calib.yaml
  • CHANGELOG.rst
  • modelopt_recipes/huggingface/models/nvidia/Nemotron-3-Super-120B-A12B/ptq/nvfp4-mse.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • modelopt/torch/init.py

@shengliangxu shengliangxu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@jenchen13 jenchen13 requested a review from a team as a code owner June 23, 2026 16:52
Signed-off-by: Jennifer Chen <jennifchen@nvidia.com>
@jenchen13 jenchen13 merged commit 28b5e26 into main Jun 23, 2026
52 checks passed
@jenchen13 jenchen13 deleted the jennifchen/fix_import branch June 23, 2026 18:21
cjluo-nv added a commit that referenced this pull request Jul 14, 2026
…tests (#1970)

### What does this PR do?

**Type of change:** documentation (+ new tests)

`modelopt_recipes/ptq.md` is meant to track every shipped PTQ recipe,
but recent recipe PRs did not update it. This PR brings the doc back in
sync and adds unit tests so it can't drift again.

**Doc sync — general recipes:**
- Add `nvfp4_experts_only_input_scale1-kv_fp8_cast` (#1947) to the
shipped-recipes table (now 20 recipes) and document the `input_scale1`
variant (constant amax 2688 → exported NVFP4 `input_scale == 1.0`,
expert activations uncalibrated).
- Add a pointer that the PTQ recipes' `quantize` sections also drive
QAT/QAD, with the LSQ / Dual-LSQ QAD recipes under `general/qad/`
(#1884).

**Doc sync — model-specific recipes:**
- Add `gemma4` (algorithm override, #1690), `diffusion_gemma` (extra
`*self_conditioning*` exclusion, #1707), `vit` (FP8 with attention BMM
quantizers for Torch-TRT, #1569), and the qwen3_5 / qwen3_5_moe
`w4a16_nvfp4_mse-fp8_attn-kv_fp8_cast` twins (#1620).
- Rewrite the checkpoint-mirror section for the
`huggingface/models/nvidia/` tier: Super recipe rename
(`super-nvfp4.yaml` → `nvfp4-mse.yaml` / `nvfp4-max-calib.yaml`),
Nano-4B GGUF Q4_K_M mirror (#1327/#1606), Ultra-550B `nvfp4-4o6`
(#1684); fix stale `models/<checkpoint>` paths.

**Enforcement — new `tests/unit/recipe/test_recipe_docs.py`:**
1. Every `general/ptq/*.yaml` stem must appear (backticked) in `ptq.md`.
2. Every recipe row in the shipped-recipes table must exist on disk
(catches renames/removals).
3. The "All N `general/ptq/` recipes" count must match the file count.
4. Every model folder under `huggingface/` containing `ptq/*.yaml` must
be mentioned in the doc.

### Usage

```bash
pytest tests/unit/recipe/test_recipe_docs.py -q
```

### Testing

- `pytest tests/unit/recipe/ -q` → 218 passed (includes the 4 new
tests).
- Verified enforcement bites: against the pre-update `ptq.md`, 3 of the
4 new tests fail with actionable messages.
- `pre-commit run --files modelopt_recipes/ptq.md
tests/unit/recipe/test_recipe_docs.py` → all hooks pass.

### 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?: ✅ (docs + tests only; no runtime
code changed)
- 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?: ✅ —
`tests/unit/recipe/test_recipe_docs.py`
- Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?:
N/A (documentation/test change)
- Did you get Claude approval on this PR?: ❌ (pending)

### Additional Information

Follow-up to the recipe guide added in #1662. The doc-consistency tests
could also be wired into `tools/precommit/check_modelopt_recipes.py` for
commit-time feedback if reviewers prefer both layers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Documentation**
* Added the `nvfp4_experts_only_input_scale1-kv_fp8_cast` recipe to the
general PTQ catalog.
* Documented the new `input_scale1` calibration behavior and related
quantization/caching details.
* Expanded and reworked PTQ documentation, including updated navigation,
“Beyond PTQ” guidance, new/updated model-specific recipes, and expanded
checkpoint mirror entries.
  * Updated the general PTQ shipped-recipe count to 20.

* **Tests**
* Added automated consistency checks between on-disk shipped recipe YAML
files and the PTQ documentation (coverage, existence, and counts).
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Chenjie Luo <chenjiel@nvidia.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.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.

5 participants