Skip to content

feat: Adding quantization aware training support with Model-Optimizer - #1756

Merged
terrykong merged 78 commits into
mainfrom
mxin/qarl-3
May 5, 2026
Merged

terrykong merged 78 commits into
mainfrom
mxin/qarl-3

Conversation

@mxinO

@mxinO mxinO commented Jan 10, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Add quantization aware (QA) training, e.g. QA-GRPO, QA-On policy distillation.

Usage

Run a QARL by enabling simulated quantization with modelopt, adding the following config,

++policy.generation.quant_cfg="NVFP4_DEFAULT_CFG" \
++policy.quant_cfg="NVFP4_DEFAULT_CFG" \

Other quantization formats are available, but currently only tensor-wise quantization format are supported.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...
  • vllm updated import path, modelopt needs update vllm plugin
  • Moldeopt needs tranformers > 5.0 support

@github-actions

Copy link
Copy Markdown

⚠️ File Consistency Check

Check based on commit: 4f14851 (PR #1756 from mxin/qarl-3)

⚠️ DTensor Policy Worker Synchronization Warning

The file nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py was modified in this PR, but nemo_rl/models/policy/workers/dtensor_policy_worker.py was not updated.

Why this matters:
These files contain related DTensor policy worker implementations that should be kept synchronized to ensure consistency across different versions.

Action required:

  • Please review if the changes in nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py should also be applied to nemo_rl/models/policy/workers/dtensor_policy_worker.py
  • Update nemo_rl/models/policy/workers/dtensor_policy_worker.py if necessary to maintain consistency
  • If the files are intentionally different, please add a comment in the PR explaining why

Files to check:

  • Modified: nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py
  • Not modified: nemo_rl/models/policy/workers/dtensor_policy_worker.py

This check ensures that related file implementations remain synchronized across the codebase. If you believe this warning is incorrect or the files should intentionally differ, please add a comment explaining the reasoning.

mxinO added 4 commits January 13, 2026 23:04
Signed-off-by: Meng Xin <[email protected]>
Signed-off-by: Meng Xin <[email protected]>
Signed-off-by: Meng Xin <[email protected]>
@guyueh1
guyueh1 self-requested a review January 14, 2026 17:28

@realAsma realAsma 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.

@mxinO Is it possible to consolidate all ModelOpt related modifications to one folder (following the patch based integration ModelOpt adopted For MCore, Huggingface etc..)?

@copy-pr-bot

copy-pr-bot Bot commented Mar 5, 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.

@github-actions

github-actions Bot commented Mar 6, 2026

Copy link
Copy Markdown

⚠️ File Consistency Check

Check based on commit: ca00c02 (PR #1756 from mxin/qarl-3)

⚠️ DTensor Policy Worker Synchronization Warning

The file nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py was modified in this PR, but nemo_rl/models/policy/workers/dtensor_policy_worker.py was not updated.

Why this matters:
These files contain related DTensor policy worker implementations that should be kept synchronized to ensure consistency across different versions.

Action required:

  • Please review if the changes in nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py should also be applied to nemo_rl/models/policy/workers/dtensor_policy_worker.py
  • Update nemo_rl/models/policy/workers/dtensor_policy_worker.py if necessary to maintain consistency
  • If the files are intentionally different, please add a comment in the PR explaining why

Files to check:

  • Modified: nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py
  • Not modified: nemo_rl/models/policy/workers/dtensor_policy_worker.py

This check ensures that related file implementations remain synchronized across the codebase. If you believe this warning is incorrect or the files should intentionally differ, please add a comment explaining the reasoning.

Signed-off-by: Meng Xin <[email protected]>
@github-actions

Copy link
Copy Markdown

⚠️ File Consistency Check

Check based on commit: df851ce (PR #1756 from mxin/qarl-3)

⚠️ DTensor Policy Worker Synchronization Warning

The file nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py was modified in this PR, but nemo_rl/models/policy/workers/dtensor_policy_worker.py was not updated.

Why this matters:
These files contain related DTensor policy worker implementations that should be kept synchronized to ensure consistency across different versions.

Action required:

  • Please review if the changes in nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py should also be applied to nemo_rl/models/policy/workers/dtensor_policy_worker.py
  • Update nemo_rl/models/policy/workers/dtensor_policy_worker.py if necessary to maintain consistency
  • If the files are intentionally different, please add a comment in the PR explaining why

Files to check:

  • Modified: nemo_rl/models/policy/workers/dtensor_policy_worker_v2.py
  • Not modified: nemo_rl/models/policy/workers/dtensor_policy_worker.py

This check ensures that related file implementations remain synchronized across the codebase. If you believe this warning is incorrect or the files should intentionally differ, please add a comment explaining the reasoning.

@mxinO

mxinO commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor Author

one folder (following the patch based integration ModelOpt adopted For MCore, Huggingface etc..)?

The core changes can be gathered to folder, some minor changes to RL code are still needed. Just restructured.

@realAsma

Copy link
Copy Markdown

@mxinO
Can we move/unify some of the vllm patching with what we already have in ModelOpt?

Also Ccing Kinjal @kinjalpatel27

@mxinO

mxinO commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

@mxinO Can we move/unify some of the vllm patching with what we already have in ModelOpt?

Also Ccing Kinjal @kinjalpatel27

The modules support is using modelopt existing ones, the worker patch is somewhat different, it doesn't require calibration because the amax will come from the the policy.
The idea of amax auto loading (HF -> vllm) might can also be added to modelopt.

mxinO added 2 commits March 13, 2026 23:15
mxinO added 4 commits April 30, 2026 20:18
Add tests/functional/qa_distillation_megatron.sh that runs 1 step of
on-policy QA-Distillation (W4A4 / NVFP4_DEFAULT_CFG, the verified-
converging recipe per docs/guides/quantization-aware-rl.md) on
Qwen/Qwen3-0.6B with TP=2 PP=1 CP=1, saves a quantized Megatron
checkpoint, then invokes the Megatron-Bridge export script
(3rdparty/Megatron-Bridge-workspace/Megatron-Bridge/examples/quantization/export.py)
to convert the checkpoint into the HuggingFace safetensors layout.
The test asserts the export directory ends up with config.json and
at least one .safetensors file.

Per PR review feedback, the modelopt -> Megatron-Bridge -> HF export
pipeline is brittle and benefits from regular regression coverage in
addition to the (long-running) nightly distillation test. Wire the
new test into tests/functional/L1_Functional_Tests_GPU.sh in the
full-mode lane next to distillation_megatron.sh.

QAD uses self-distillation (policy and teacher are the same HF model),
which is the convention from the qa_distillation_math_megatron.yaml
example header — explained in an inline comment so future readers
don't second-guess it against the vanilla distillation test.

Signed-off-by: Meng Xin <[email protected]>
The bridge quantization export script needs two env tweaks that aren't
obvious from its own help:

- PYTHONPATH must expose Megatron-LM's `megatron.training` because the
  bridge script imports it at module load but `--extra mcore` only
  packages megatron.core from the workspace.
- `--tp 1` is required at export time (modelopt currently does not
  support TP>1 there). Training at TP>1 is fine — the bridge re-shards
  on load via mp_overrides. `--pp` may be >1 for large models with
  `--nproc_per_node` matching `--pp`.

Update the QARL guide's Exporting Checkpoints section accordingly and
align tests/functional/qa_distillation_megatron.sh: train at TP=2 to
exercise the cross-TP load path, export at --tp 1 --pp 1.

Signed-off-by: Meng Xin <[email protected]>
The L1_Functional_Tests_GPU.sh post-frozen-env block lists tests in
alphabetical order. The original add of qa_distillation_megatron.sh
clustered it next to distillation_megatron.sh; move it to its proper
slot between prorlv2.sh and rm.sh.

Signed-off-by: Meng Xin <[email protected]>
@mxinO

mxinO commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for your prompt response to my comments! Overall, I think the PR is in a great state. After discussing internally, there are two more things we'd like to see before merging:

  1. Update the Dockerfile to include the modelopt dependency so the that modelopt doesn't need to be installed on the fly every time
  2. Add a functional test (to tests/functional) that runs one step of QARL followed by running the export script. We often find export scripts to be quite brittle, so this will ensure that the export is being checked regularly

Thanks @ashors1 , I've addressed both. For the exporting, the script is totally managed in MBridge, not in Nemo-RL, but I still added a test, but if any issue is raised, the fix should be in MBridge side.

@mxinO
mxinO requested a review from ashors1 May 1, 2026 09:08
@mxinO

mxinO commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

@mxinO mxinO added CI:L2 Run doctests, unit tests, functional tests, and convergence tests CI:L1 Run doctests, unit tests, and functional tests and removed CI:L2 Run doctests, unit tests, functional tests, and convergence tests labels May 1, 2026
The pyrefly project-includes whitelist requires that any source file
with zero type errors be listed explicitly. The QARL subtree added
four empty package markers under nemo_rl/modelopt/models/* which
type-check clean and were tripping pyrefly's "zero errors but not in
project-includes" guard. Add them in alphabetical order alongside
nemo_rl/modelopt/__init__.py.

Signed-off-by: Meng Xin <[email protected]>
@mxinO

mxinO commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

@ashors1

ashors1 commented May 1, 2026

Copy link
Copy Markdown
Contributor

Thank you @mxinO! Could you fix the failing lint check when you get a chance? I think uv run ruff check --fix . && uv run ruff format should do the trick

mxinO added 2 commits May 1, 2026 20:41
Add the four QARL-subtree source files that pyrefly type-checks clean
to the project-includes whitelist:

  - nemo_rl/modelopt/models/generation/vllm_quant_worker.py
  - nemo_rl/modelopt/models/policy/workers/dtensor_quant_policy_worker.py
  - nemo_rl/modelopt/models/policy/workers/dtensor_quant_policy_worker_v2.py
  - nemo_rl/modelopt/registry.py

The remaining modelopt files (vllm_quant_backend.py, vllm_quant_patch.py,
megatron_quant_policy_worker.py, models/policy/workers/utils.py,
modelopt/utils.py) still have type errors and stay out of the whitelist
until those are addressed.

Signed-off-by: Meng Xin <[email protected]>
@mxinO

mxinO commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

@terrykong
terrykong enabled auto-merge (squash) May 5, 2026 03:12
@terrykong
terrykong merged commit cc75cad into main May 5, 2026
28 of 29 checks passed
@terrykong
terrykong deleted the mxin/qarl-3 branch May 5, 2026 03:22

This branch was previously deployed

1 inactive deployment
nemo-ci — 3573d582 Deployed May 2, 2026 by copy-pr-bot[bot] via L1_Functional_Tests_GPU #11149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants