[RL] Add an nvfp4 online input scale mode#18012
[RL] Add an nvfp4 online input scale mode#18012zianglih wants to merge 17 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @zianglih, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to NVFP4 quantization by implementing an "online input scale mode." This new feature allows the system to dynamically calculate and apply input scaling factors during model execution, adapting to the characteristics of the input data. The primary goal is to improve the accuracy and efficiency of NVFP4 operations, particularly within Mixture of Experts (MoE) layers. The changes are carefully integrated across the codebase, including environment variable control, core scaling logic, and optimizations for CUDA graph compatibility, ensuring a robust and performant solution. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces an online input scaling mode for nvfp4 quantization. This is a significant feature that touches multiple parts of the quantization and MoE layers. The implementation correctly adds the new environment variable and integrates the online scaling logic where needed. However, there are several instances of code duplication for the online scaling logic across different files and within the same file. Refactoring this duplicated code into shared helper functions would greatly improve the maintainability and readability of the codebase. Additionally, some of the new logic could be simplified for better clarity.
|
@zianglih Could you fix the merge conflicts? |
|
@b8zhong thanks for reviewing. Let me take a look! |
|
Very interested in this as well! |
|
Hi @b8zhong @BitPhinix , I'll clean up this PR this week. |
3c26712 to
b75a96e
Compare
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Hi @b8zhong , I have refactored the implementation. Could you review again? Thanks! |
|
Most NVIDIA ci passed, only 1 irrelevant failure. |
|
This change looks very beneficial for enabling NVFP4 online quantizer. Having the compute_input_scale_and_inv function makes it much easier to reuse to add a weight quantization path. |
|
Hi @b8zhong , do you plan to merge the PR? Thanks |
|
@zianglih No need to merge in main anymore. I'll rerun until NV CI passes |
|
Hi @b8zhong , there is 1 failed nv ci. Could you rerun? Thanks! |
Head branch was pushed to by a user without write access
|
Resolved a minor merge conflict, no functional changes. Failed nv ci is irrelevant to this PR. |
Two features for NVFP4 RL training with miles: 1. post_process_weights endpoint: Re-runs process_weights_after_loading on NVFP4 layers after in-place weight updates, handling padding and shuffling for GEMM kernels. In RL mode (enable_memory_saver), original MoE weights are preserved for reloading instead of being deleted. 2. Online input scale (PR sgl-project#18012 by zianglih): Dynamically computes NVFP4 input_scale from per-batch activation amax at inference time, instead of using stale checkpoint-derived scales. Essential for RL where model weights change every training step. Enabled via SGLANG_NVFP4_ONLINE_INPUT_SCALE=1. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two features for NVFP4 RL training with miles: 1. post_process_weights endpoint: Re-runs process_weights_after_loading on NVFP4 layers after in-place weight updates, handling padding and shuffling for GEMM kernels. In RL mode (enable_memory_saver), original MoE weights are preserved for reloading instead of being deleted. 2. Online input scale (PR sgl-project#18012 by zianglih): Dynamically computes NVFP4 input_scale from per-batch activation amax at inference time, instead of using stale checkpoint-derived scales. Essential for RL where model weights change every training step. Enabled via SGLANG_NVFP4_ONLINE_INPUT_SCALE=1. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Closing this PR, a better implementation is #22918 . |
Motivation
@HumansAnd
Miles NVFP4 RL: radixark/miles#546
NVFP4 quantization recipe has a two-level micro-block scaling strategy for both input activations and model weight. For inference efficiency, the 2nd level fp32 input scale is usually calibrated and statically stored in model checkpoint.
However, for RL, the model is under constant training and the dynamic range of input activation changes dynamically. One option is to compute the scale on the fly.
Alternatively, this mode might be useful for better nvfp4 quantization quality at the cost of some performance overhead.
Modifications
Accuracy Tests
Serving existing nvfp4 checkpoint
Qwen3-30B-A3B
DeepSeek-R1-0528
Serving PTQ checkpoint
PTQ checkpoints converted from radixark/miles#536
Qwen3-235B-A22B
Qwen3-30B-A3B-Instruct-2507
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci