-
Notifications
You must be signed in to change notification settings - Fork 200
[MoE] Add conditional expert calibration #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @dichn, 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 significantly enhances the flexibility of Mixture-of-Experts (MoE) model calibration by introducing configurable control over expert execution and output contribution. My changes allow for more precise calibration strategies, enabling scenarios where all experts are evaluated regardless of routing, or where expert activations are computed without affecting the final model output.
Highlights
- New Configuration Class: I've introduced a new
CalibrationConfig
class inllmcompressor/modeling/config.py
to centralize and manage parameters for Mixture-of-Experts (MoE) calibration. This class includesmoe_calibrate_all_experts
andmoe_calibrate_gated_acts
boolean flags, with validation to prevent unsupported configurations. - Conditional Expert Execution: I've implemented conditional expert execution logic within the MoE layers for DeepseekV3, Llama4, and Qwen3 models. This allows for flexible control during calibration:
moe_calibrate_all_experts
ensures all experts run their forward pass, whilemoe_calibrate_gated_acts
determines if their outputs contribute to the final hidden state. - Integration with Model Preparation: The
replace_modules_for_calibration
andmoe_calibration_context
functions inllmcompressor/modeling/prepare.py
have been updated to accept and pass the new calibration configuration. This ensures that the desired calibration behavior is applied when MoE modules are replaced or temporarily modified. - Unit Test Coverage: I've added new unit tests for DeepseekV3, Llama4, and Qwen3 MoE layers to verify that all experts are triggered when
moe_calibrate_all_experts
is enabled andmoe_calibrate_gated_acts
is disabled, ensuring the intended calibration behavior.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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 .gemini/
folder in the base of the repository. Detailed instructions can be found here.
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 or fill out our survey to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces conditional expert calibration for Mixture-of-Experts (MoE) models, controlled by a new CalibrationConfig
. The changes allow for more flexible calibration setups. A critical issue was identified where the moe_calibrate_gated_acts
flag was not correctly implemented in the model forward passes, leading to incorrect behavior when set to False
. A suggestion was also made to improve the clarity of an error message.
@kylesayrs |
Re-pushed for
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR! Do you mind listing how you’ve tested the updated examples?
Change Purpose: - Improve MoE calibration support by adding configuration-based expert execution Change Details: - Create class `CalibrationConfig` to standalone llmcompressor/modeling/config.py - Add conditional expert execution based on: - `moe_calibrate_all_experts`: If True, all experts run for every token; If False, only routed experts are run - `moe_calibrate_gated_acts`: If True, routed experts contribute final output; If False, expert activations are computed but excluded from the final output - Add unit test to verify all experts are triggered during MoE calibration
Re-pushed for adding missing
Note on the skipped test
CC: @dsikka @kylesayrs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR, it looks great! I had a couple suggestions:
- Documenting the flags in the class itself, so it's clearer to future users
- Few naming suggestions (nits)
I'm running the llama4 example at the moment, will update here if it passes!
|
||
|
||
class CalibrationConfig(BaseModel): | ||
moe_calibrate_all_experts: bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add more information in this config class around what these flags do for future readers, so it's clear which flag should be set for which mode?
I was thinking something like:
| all_experts | gated_acts | Behavior |
|-------------|------------|------------------------------------------------------------------------|
| True | True | All experts run, routed experts contribute to output (current default) |
| True | False | All experts run for calibration, but outputs ignored |
| False | True | Only routed experts run and contribute (standard inference) |
| False | False | Invalid configuration (raises error) |
from pydantic import BaseModel, model_validator | ||
|
||
|
||
class CalibrationConfig(BaseModel): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: What do you think about renaming to MoECalibrationConfig?
|
||
class CalibrationConfig(BaseModel): | ||
moe_calibrate_all_experts: bool | ||
moe_calibrate_gated_acts: bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Consider renaming to something like use_gated_outputs
since The name suggests it's about "calibrating
gated activations" but it actually controls whether expert outputs contribute to the final result.
Update: The llama4 test failed for me locally with:
I will take a look in sometime! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will the user configure these arguments?
Currently, for qwen3, we pass in calibrate_moe_context=True for nvfp4:
https://github.com/vllm-project/llm-compressor/blob/main/examples/quantization_w4a4_fp4/qwen_30b_a3b.py#L70
This allows us to temporarily update the moe blocks with the blocks defined in modeling/qwen3_moe.py - is the plan to keep this argument?
As noted by @kylesayrs, this PR aligns with a spec change that drops |
Change Purpose:
Change Details:
CalibrationConfig
to standalone llmcompressor/modeling/config.pymoe_calibrate_all_experts
: If True, all experts run for every token; If False, only routed experts are run -moe_calibrate_gated_acts
: If True, routed experts contribute final output; If False, expert activations are computed but excluded from the final output