Skip to content

MBridge VLM distillation / QAD support#1938

Open
kevalmorabia97 wants to merge 4 commits into
mainfrom
kmorabia/vlm-distill
Open

MBridge VLM distillation / QAD support#1938
kevalmorabia97 wants to merge 4 commits into
mainfrom
kmorabia/vlm-distill

Conversation

@kevalmorabia97

@kevalmorabia97 kevalmorabia97 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

What

Adds VLM (e.g. Qwen3.5-VL, Gemma3-VL) knowledge-distillation / QAD support to the Megatron-Bridge examples:

  • distill.py distills only the language model submodule (vision tower + projector untouched), reusing the LLM training path.
  • New export_distilled_megatron_to_hf.py converts a distilled Megatron checkpoint (any iteration) to HF. Required especially for VLM distilled ckpt as it only has LM weights so we need to initialize full VLM, swap LLM weights then save to HF
  • Renames export.pyexport_quantized_megatron_to_hf.py.

Related upstream Megatron-Bridge PRs to be available in nemo:26.08 container:

Testing

  • Validated end-to-end on nemo:26.06 (distill → separate HF export; LLM + VLM, incl. TP→TP/PP reshard). test_distill_vlm runs the export script as a CI e2e step.
  • Many CICD tests for wide coverage of all mbridge scripts

Summary by CodeRabbit

Summary

  • New Features

    • Added a dedicated HuggingFace exporter for distilled Megatron checkpoints, with distinct LLM vs VLM conversion flows.
  • Bug Fixes

    • Improved Megatron-Bridge distillation/export consistency, including safer handling of VLMs and targeted submodule distillation.
  • Documentation

    • Updated Megatron-Bridge READMEs and tutorials to reference the new quantized and distilled export scripts and revised CLI guidance.
  • Tests

    • Expanded distillation, QAD, and quantization/export tests to cover LLM/VLM variants, with conditional skipping for unsupported MoE setups.

@copy-pr-bot

copy-pr-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

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

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Jul 7, 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

Adds VLM-aware Megatron-Bridge distillation, a distilled-checkpoint Hugging Face exporter, conditional checkpoint state restoration, updated quantized export workflows, hybrid-attention pruning support, and expanded tests and documentation.

Changes

Megatron-Bridge distillation and export flow

Layer / File(s) Summary
Distillation provider wrapper
examples/megatron_bridge/_distillation_provider.py
Defers KD conversion until after checkpoint weight loading and supports restricting conversion to a selected submodule.
Checkpoint restore and bridge plugin wiring
modelopt/torch/utils/plugins/mbridge.py, modelopt/torch/nas/plugins/__init__.py, examples/megatron_bridge/prune_minitron.py
Adds conditional ModelOpt state restoration, removes the Qwen3.5 MoE mapping patch, and moves NAS plugin registration to the pruning entrypoint.
VLM-aware distillation and export
examples/megatron_bridge/distill.py
Targets the language model in VLMs, restores student weights before conversion, and uses dedicated LLM and VLM Hugging Face export helpers.
Distilled checkpoint exporter
examples/megatron_bridge/export_distilled_megatron_to_hf.py
Adds separate LLM and VLM conversion paths for distilled Megatron checkpoints.
Distillation, QAD, and MoE validation
tests/examples/megatron_bridge/*, tests/_test_utils/examples/megatron_bridge.py
Adds VLM distillation coverage, parameterizes QAD tests, validates exported artifacts, and gates unsupported Qwen3.5-MoE cases.
Export workflow documentation
CHANGELOG.rst, examples/megatron_bridge/README.md, examples/megatron_bridge/quantize.py, examples/megatron_bridge/tutorials/*
Updates exporter names, conversion commands, VLM guidance, and distilled-checkpoint instructions.

Hybrid-attention pruning support

Layer / File(s) Summary
Per-layer attention pattern handling
modelopt/torch/nas/plugins/megatron_model_stats.py, tests/gpu_megatron/torch/nas/plugins/test_megatron_model_stats.py
Supports periodic and explicit per-layer linear_attention_freq patterns in parameter counting and verifies equivalent results.
Depth-pruning state management
modelopt/torch/prune/plugins/mcore_minitron.py
Slices per-layer configuration during depth pruning and temporarily prunes and restores candidate models during architecture scoring.
Hybrid-attention pruning validation
tests/gpu_megatron/torch/prune/plugins/test_mcore_gpt_minitron_pruning.py
Validates layer counts and sliced attention patterns for Qwen3.5-like hybrid pruning.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant distill.py
  participant run_provider
  participant convert_to_distillation_provider
  participant save_vlm_to_hf
  participant export_llm_to_hf

  distill.py->>run_provider: Load student checkpoint weights
  run_provider->>convert_to_distillation_provider: Run deferred KD conversion
  convert_to_distillation_provider-->>distill.py: Return distilled model
  distill.py->>save_vlm_to_hf: Export distilled VLM
  distill.py->>export_llm_to_hf: Export distilled LLM
Loading

Possibly related PRs

Suggested reviewers: danielkorzekwa, chenhanyu, j-rausch, jenchen13

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: Megatron-Bridge VLM distillation and QAD support.
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 No listed anti-patterns were found in the changed modelopt/examples Python files; trust_remote_code is caller-controlled and defaults false, and there are no torch.load/np.load/eval/exec/nosec addi...
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch kmorabia/vlm-distill

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

@kevalmorabia97 kevalmorabia97 changed the title feat: VLM distillation and HF export for Megatron-Bridge examples MBridge VLM distillation / QAD support Jul 7, 2026
@kevalmorabia97 kevalmorabia97 marked this pull request as ready for review July 7, 2026 17:08
@kevalmorabia97 kevalmorabia97 requested review from a team as code owners July 7, 2026 17:08
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

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

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

Comment thread examples/megatron_bridge/_distillation_provider.py
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 5.45455% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.81%. Comparing base (7ed19b2) to head (3ea6f6b).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
modelopt/torch/prune/plugins/mcore_minitron.py 2.12% 46 Missing ⚠️
modelopt/torch/nas/plugins/megatron_model_stats.py 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1938      +/-   ##
==========================================
- Coverage   77.92%   73.81%   -4.11%     
==========================================
  Files         519      522       +3     
  Lines       58217    59487    +1270     
==========================================
- Hits        45366    43913    -1453     
- Misses      12851    15574    +2723     
Flag Coverage Δ
unit 55.38% <1.81%> (-0.09%) ⬇️

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.

@kevalmorabia97

Copy link
Copy Markdown
Collaborator Author

/claude review

@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: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/examples/megatron_bridge/test_quantize_export.py (1)

15-83: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Keep the VLM export coverage test_qad.py only reaches export_quantized_megatron_to_hf.py on the non-VLM path; the VLM branch returns before export, so it does not replace the removed case.

🤖 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 `@tests/examples/megatron_bridge/test_quantize_export.py` around lines 15 - 83,
The VLM export path still needs direct coverage because the QAD flow exits
before reaching export_quantized_megatron_to_hf.py, so this test should keep
validating the quantize-and-export bridge for the VLM-specific Megatron
checkpoint. Preserve the existing quantize/export steps in
test_quantize_and_export, using the same helpers like create_tiny_qwen3_dir,
extend_cmd_parts, and run_example_command, so the removed coverage is not lost.
🧹 Nitpick comments (1)
tests/examples/megatron_bridge/test_prune_minitron.py (1)

26-40: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Duplicate helper across test files.

_qwen35_moe_bridge_supported() is duplicated verbatim in tests/examples/megatron_bridge/test_qad.py. Consider moving it to a shared location (e.g. a local conftest.py in tests/examples/megatron_bridge/) to avoid drift between the two copies.

♻️ Suggested consolidation
# tests/examples/megatron_bridge/conftest.py
from megatron.bridge.models.conversion import model_bridge


def qwen35_moe_bridge_supported() -> bool:
    """Whether MBridge supports Qwen3.5-MoE per-expert weight assembly, i.e. nemo:26.08+."""
    try:
        return hasattr(model_bridge, "_fuse_per_expert_hf_weight")
    except Exception:
        return False

Then import qwen35_moe_bridge_supported from the shared module in both test files.

🤖 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 `@tests/examples/megatron_bridge/test_prune_minitron.py` around lines 26 - 40,
The helper _qwen35_moe_bridge_supported() is duplicated in multiple Megatron
bridge test files, so consolidate it into a shared test fixture/module such as a
local conftest.py under tests/examples/megatron_bridge/. Move the logic that
checks model_bridge for _fuse_per_expert_hf_weight into a single shared function
(for example qwen35_moe_bridge_supported), then import and use that shared
helper from both test_prune_minitron.py and test_qad.py to keep the behavior in
sync.
🤖 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 `@examples/megatron_bridge/distill.py`:
- Around line 377-391: In the VLM export path inside distill.py, the call to
save_vlm_to_hf is using the wrong source HF identifier for config/tokenizer
lookup. Update the export block under if args.hf_export_path and is_vlm so it
passes args.student_hf_path instead of args.student_hf_model, matching the
standalone export_distilled_megatron_to_hf flow and the intended VLM source path
semantics.

In `@examples/megatron_bridge/quantize.py`:
- Around line 417-420: The post-save message in quantize.py is too generic
because it tells all users to convert with export_quantized_megatron_to_hf.py
even though VLM checkpoints remain Megatron-only. Update the print_rank_0 hint
near the quantized model save path to branch on is_vlm (or explicitly mention
the VLM exception) so non-VLM users see the conversion guidance while VLM users
are warned that the conversion path is unsupported.

---

Outside diff comments:
In `@tests/examples/megatron_bridge/test_quantize_export.py`:
- Around line 15-83: The VLM export path still needs direct coverage because the
QAD flow exits before reaching export_quantized_megatron_to_hf.py, so this test
should keep validating the quantize-and-export bridge for the VLM-specific
Megatron checkpoint. Preserve the existing quantize/export steps in
test_quantize_and_export, using the same helpers like create_tiny_qwen3_dir,
extend_cmd_parts, and run_example_command, so the removed coverage is not lost.

---

Nitpick comments:
In `@tests/examples/megatron_bridge/test_prune_minitron.py`:
- Around line 26-40: The helper _qwen35_moe_bridge_supported() is duplicated in
multiple Megatron bridge test files, so consolidate it into a shared test
fixture/module such as a local conftest.py under
tests/examples/megatron_bridge/. Move the logic that checks model_bridge for
_fuse_per_expert_hf_weight into a single shared function (for example
qwen35_moe_bridge_supported), then import and use that shared helper from both
test_prune_minitron.py and test_qad.py to keep the behavior in sync.
🪄 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: a418a2b7-0f1b-4330-a397-723d252f5670

📥 Commits

Reviewing files that changed from the base of the PR and between d290839 and f423708.

📒 Files selected for processing (14)
  • CHANGELOG.rst
  • examples/megatron_bridge/README.md
  • examples/megatron_bridge/_distillation_provider.py
  • examples/megatron_bridge/distill.py
  • examples/megatron_bridge/export_distilled_megatron_to_hf.py
  • examples/megatron_bridge/export_quantized_megatron_to_hf.py
  • examples/megatron_bridge/quantize.py
  • examples/megatron_bridge/tutorials/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16/README.md
  • examples/megatron_bridge/tutorials/README.md
  • modelopt/torch/utils/plugins/mbridge.py
  • tests/examples/megatron_bridge/test_distill.py
  • tests/examples/megatron_bridge/test_prune_minitron.py
  • tests/examples/megatron_bridge/test_qad.py
  • tests/examples/megatron_bridge/test_quantize_export.py

Comment thread examples/megatron_bridge/distill.py
Comment thread examples/megatron_bridge/quantize.py Outdated
Comment thread examples/megatron_bridge/distill.py

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude review passed — no blocking issues found. LGTM

Scope: Reviewed all 14 changed files, prioritizing new/modified source (_distillation_provider.py, distill.py, export_distilled_megatron_to_hf.py, quantize.py, modelopt/torch/utils/plugins/mbridge.py), then tests and README/CHANGELOG. Examples + tests PR plus one backward-compatible signature change (load_modelopt_megatron_checkpoint gains restore_modelopt_state: bool = True; default preserves prior behavior).

Findings: CRITICAL: 0 · IMPORTANT: 0 · SUGGESTION: 1 (posted inline) — the inline VLM export branch in distill.py (--hf_export_path + VLM) is not exercised by CI; test_distill_vlm validates only the standalone export_distilled_megatron_to_hf.py path.

Verified: New public imports resolve (copy_hf_ckpt_remote_code, warn_rank_0/print_args). Removed _patch_qwen35_moe_sequential_expert_mappings and the deleted export.py name have no remaining references in examples/ or docs/. The load_modelopt_megatron_checkpoint change is additive and the new restore_modelopt_state=False call site is correct. dist.cleanup() is guarded so the double-cleanup is safe. The teacher-mirroring __setattr__ foot-gun in _distillation_provider.py is handled via object.__setattr__.

Already flagged by CodeRabbit (not duplicated; worth confirming before merge): inline VLM export passes student_hf_model instead of student_hf_path to save_vlm_to_hf; quantize.py post-save message points all users at the export script though VLM quantized export is unsupported; duplicated _qwen35_moe_bridge_supported() helper across two test files.

Risk: Low. Example/test-scoped with documented temporary WARs (nemo:26.08 container) and one backward-compatible plugin signature change. No mode-registration, config-schema, or checkpoint-state-dict changes affecting existing optimized checkpoints.

@kevalmorabia97

Copy link
Copy Markdown
Collaborator Author

Addressed review comments in 623b6c9:

  • @coderabbitai VLM export template arg (distill.py) — fixed: passes --student_hf_path.
  • @coderabbitai quantize.py post-save hint — fixed: scoped on is_vlm (VLMs note that quantized-HF export is unsupported).
  • @coderabbitai duplicated _qwen35_moe_bridge_supported() — moved to tests/_test_utils/examples/megatron_bridge.py and imported in both test_qad.py and test_prune_minitron.py.
  • @coderabbitai "keep VLM export coverage" (test_quantize_export.py) — skipping: there's no quantized-VLM export path to cover (it's unsupported, and export_quantized_megatron_to_hf.py is LLM-only). The meaningful part of the removed test — PTQ of a VLM producing a Megatron checkpoint — is still covered by test_qad.py's gemma3vl case (Step 1 runs quantize.py on the VLM and asserts modelopt_state).
  • @AAnoosheh / @kevalmorabia97: confirmed — the provider lives upstream in feat(distill): defer KD conversion to a pre-wrap hook and support submodule distillation for VLMs NVIDIA-NeMo/Megatron-Bridge#4707; _distillation_provider.py is a temporary 26.06 vendored copy, removed once nemo:26.08 ships it.

All example-test changes collect cleanly; the code fixes are message/arg-only (no behavior change for the default --student_hf_model == --student_hf_path case).

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor
🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
gh pr view 1938 --repo NVIDIA/Model-Optimizer --json commits --jq '.commits[] | select(.oid | startswith("623b6c9")) | .oid'
gh api repos/NVIDIA/Model-Optimizer/commits/623b6c9 --jq '.files[].filename' 2>/dev/null || true

Length of output: 647


@kevalmorabia97 Confirmed — commit 623b6c9 touches exactly the files you described: examples/megatron_bridge/distill.py, examples/megatron_bridge/quantize.py, tests/_test_utils/examples/megatron_bridge.py, tests/examples/megatron_bridge/test_prune_minitron.py, and tests/examples/megatron_bridge/test_qad.py. This lines up with:

  • distill.py — VLM export template arg fix (--student_hf_path).
  • quantize.py — post-save hint now scoped on is_vlm.
  • _qwen35_moe_bridge_supported() deduplicated into tests/_test_utils/examples/megatron_bridge.py and imported by both test_qad.py and test_prune_minitron.py.

test_quantize_export.py wasn't touched in this commit, consistent with your rationale for skipping that item — agreed, since export_quantized_megatron_to_hf.py is LLM-only and quantized-VLM export isn't supported, the PTQ-of-VLM coverage via test_qad.py's gemma3vl case is sufficient.

Thanks also for confirming the _distillation_provider.py vendoring situation tied to NVIDIA-NeMo/Megatron-Bridge#4707.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@kevalmorabia97 kevalmorabia97 force-pushed the kmorabia/vlm-distill branch from 623b6c9 to 3b7106a Compare July 9, 2026 15:45
kevalmorabia97 and others added 3 commits July 13, 2026 06:29
- distill.py: distill only the language_model submodule of a VLM (vision tower
  / projector untouched); reuses the LLM training path for everything else.
- Add export_distilled_megatron_to_hf.py to convert a distilled Megatron
  checkpoint (any iteration) to HF -- LLM/Puzzletron via export_ckpt, VLM via
  in-memory reassembly (vision from HF + distilled LM from the checkpoint).
- Rename export.py -> export_quantized_megatron_to_hf.py; distill.py's final
  export reuses the shared export helpers.
- mbridge: load_modelopt_megatron_checkpoint gains restore_modelopt_state
  (weights-only reload of a full-precision distilled student, no teacher).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
…helper

- distill.py: VLM HF export uses --student_hf_path (the VLM config/tokenizer
  source), not --student_hf_model (which is unused for VLMs and only coincides
  via its default).
- quantize.py: only point non-VLM checkpoints at export_quantized_megatron_to_hf.py;
  quantized-VLM HF export is unsupported, so say so for VLMs.
- Move the duplicated _qwen35_moe_bridge_supported() helper into
  tests/_test_utils/examples/megatron_bridge.py.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
@kevalmorabia97 kevalmorabia97 requested a review from a team as a code owner July 13, 2026 13:29
@nfasfous

Copy link
Copy Markdown

Tested on Cosmos-Reason2-2B:

  • Pruned Model from 28 to 24 dec blocks -> baseline to start KD experiments
  • KD unpruned VLM teacher to pruned VLM student with mbridge then applied PTQ to student down to (a) FP8 and (b) NVFP4
  • Same setup for QAD with (a) FP8 and (b) NVFP4
  • Eval on MMStar dataset and compare to baseline BF16 KD'd student model

Note: KD/QAD only applied only for short run of 50 iters

Results:
image
image

Key takeaways: particularly for NVFP4, QAD recovers drop in performance. FP8 less impacted overall, difference to BF16 is noise.

Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.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 `@modelopt/torch/prune/plugins/mcore_minitron.py`:
- Around line 423-454: Update _temporarily_pruned so the original all_layers
list and layer numbers are restored onto model.decoder.layers before calling
sample(model, sample_func=max). Then sample the reattached full layer list to
restore max-subnet state, followed by restoring per-layer patterns and hybrid
configuration as currently handled.
🪄 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: 5cfd7fbc-1d97-4c0e-93b6-c1ff5b3e4b8c

📥 Commits

Reviewing files that changed from the base of the PR and between 1fee603 and 3ea6f6b.

📒 Files selected for processing (6)
  • examples/megatron_bridge/README.md
  • examples/megatron_bridge/prune_minitron.py
  • modelopt/torch/nas/plugins/megatron_model_stats.py
  • modelopt/torch/prune/plugins/mcore_minitron.py
  • tests/gpu_megatron/torch/nas/plugins/test_megatron_model_stats.py
  • tests/gpu_megatron/torch/prune/plugins/test_mcore_gpt_minitron_pruning.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/megatron_bridge/README.md

Comment on lines +423 to +454
@contextmanager
def _temporarily_pruned(self, export_config: dict):
"""Prune to ``export_config`` for the duration of the block, then restore the max subnet.

Used to score a candidate subnet without permanently mutating the model. ``_prune`` drops
layers and slices per-layer patterns (config lists + hybrid string) in place; ``sample(max)``
only restores hparam-controlled state, so those must be snapshotted and restored here — else
the mutations compound across candidates and corrupt the final export.
"""
model = self.model
all_layers = model.decoder.layers
start_layer_number = all_layers[0].layer_number
hybrid_key = _get_hybrid_pattern_key(model)
saved_patterns = {
attr: getattr(model.config, attr)
for attr in _PER_LAYER_CONFIG_PATTERNS
if isinstance(getattr(model.config, attr, None), (list, tuple))
}
saved_hybrid = getattr(model, hybrid_key) if hybrid_key else None
try:
self._prune(export_config)
yield
finally:
# reset to max subnet, revert dropped layers, and restore in-place-sliced patterns
sample(model, sample_func=max)
for offset, layer in enumerate(all_layers):
layer.layer_number = start_layer_number + offset
model.decoder.layers = all_layers
for attr, pattern in saved_patterns.items():
setattr(model.config, attr, pattern)
if hybrid_key:
setattr(model, hybrid_key, saved_hybrid)

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Look for tied/shared hparam mechanisms for per-layer width dims in Megatron NAS plugins
rg -nP -C4 'class .*Hparam' modelopt/torch/nas/plugins/megatron.py modelopt/torch/opt/dynamic.py
rg -nP -C3 '\btied\b|shared.*hparam|_hp_registry' modelopt/torch/nas/plugins/megatron.py modelopt/torch/opt/dynamic.py

Repository: NVIDIA/Model-Optimizer

Length of output: 1826


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== megatron.py around shared hparam setup =="
sed -n '1210,1285p' modelopt/torch/nas/plugins/megatron.py

echo
echo "== dynamic.py hparam registration/shared semantics =="
rg -n -C4 'shared hidden_size|set the shared hidden_size hparam|_register_hparam|def get_hparam|def _register_hparam|share' modelopt/torch/opt/dynamic.py modelopt/torch/nas/plugins/megatron.py

echo
echo "== dynamic.py hparam-related class/method definitions =="
ast-grep outline modelopt/torch/opt/dynamic.py --view expanded | sed -n '1,240p'

Repository: NVIDIA/Model-Optimizer

Length of output: 34413


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== search for sample(max) behavior and layer traversal =="
rg -n -C4 'def sample\(|sample_func=max|named_modules\(|decoder.layers' modelopt/torch/opt/dynamic.py modelopt/torch/prune/plugins/mcore_minitron.py

echo
echo "== inspect _temporarily_pruned surrounding code =="
sed -n '380,470p' modelopt/torch/prune/plugins/mcore_minitron.py

Repository: NVIDIA/Model-Optimizer

Length of output: 16457


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== dynamic.py sample/export semantics =="
rg -n -C6 'def sample\(|sample_func|max|def export\(' modelopt/torch/opt/dynamic.py

echo
echo "== dynamic.py around sample implementation =="
sed -n '320,420p' modelopt/torch/opt/dynamic.py

echo
echo "== dynamic.py around export implementation =="
sed -n '510,580p' modelopt/torch/opt/dynamic.py

Repository: NVIDIA/Model-Optimizer

Length of output: 11040


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect hparam classes in dynamic.py =="
rg -n -C4 'class .*Hparam|class TracedHp|class .*Hp' modelopt/torch/opt/dynamic.py modelopt/torch/nas/plugins/megatron.py

echo
echo "== inspect _register_hparam and named_hparams traversal =="
sed -n '378,420p' modelopt/torch/opt/dynamic.py
sed -n '1160,1205p' modelopt/torch/opt/dynamic.py

Repository: NVIDIA/Model-Optimizer

Length of output: 7759


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read only the sample helper in dynamic.py if present, and the Hparam classes.
rg -n -C8 'def sample\(|sample_func=max|class TracedHp|class Hparam|def named_hparams\(' modelopt/torch/opt/dynamic.py

Repository: NVIDIA/Model-Optimizer

Length of output: 1661


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== sample helper in dynamic.py =="
rg -n -C12 'def sample\(|sample_func=max' modelopt/torch/opt/dynamic.py

echo
echo "== Hparam classes in dynamic.py =="
rg -n -C8 'class Hparam|class TracedHp|class .*Hp' modelopt/torch/opt/dynamic.py

Repository: NVIDIA/Model-Optimizer

Length of output: 194


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate sample() definitions and callers =="
rg -n -C4 'def sample\(' modelopt
echo
echo "== locate sample(model, sample_func=max) imports =="
rg -n -C4 'sample\(model, sample_func=max\)|from .* import sample|import .*sample' modelopt/torch/prune/plugins/mcore_minitron.py modelopt

Repository: NVIDIA/Model-Optimizer

Length of output: 10977


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect top of mcore_minitron.py for sample import =="
sed -n '1,120p' modelopt/torch/prune/plugins/mcore_minitron.py

Repository: NVIDIA/Model-Optimizer

Length of output: 4372


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect modelopt/torch/nas/utils.py sample implementation =="
sed -n '1,240p' modelopt/torch/nas/utils.py

echo
echo "== inspect selection semantics in modelopt/torch/nas/search_space.py =="
sed -n '1,180p' modelopt/torch/nas/search_space.py

Repository: NVIDIA/Model-Optimizer

Length of output: 16576


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== inspect _DynamicMLP and _DynamicMoELayer setup =="
sed -n '316,390p' modelopt/torch/nas/plugins/megatron.py
sed -n '730,855p' modelopt/torch/nas/plugins/megatron.py

Repository: NVIDIA/Model-Optimizer

Length of output: 9762


Restore the full layer list before sample(max)
sample(model, sample_func=max) only walks reachable modules, so the detached layers in all_layers keep their pruned per-layer widths and get reattached in a stale state. Swap the restore order so model.decoder.layers is set back first, then sample the max subnet.

🤖 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/prune/plugins/mcore_minitron.py` around lines 423 - 454,
Update _temporarily_pruned so the original all_layers list and layer numbers are
restored onto model.decoder.layers before calling sample(model,
sample_func=max). Then sample the reattached full layer list to restore
max-subnet state, followed by restoring per-layer patterns and hybrid
configuration as currently handled.

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.

3 participants