-
Notifications
You must be signed in to change notification settings - Fork 307
Remove _moe_count_expert_calib_tokens flag; tie token counting to moe_calib_experts_ratio #1062
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
Merged
+35
−46
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -444,16 +444,15 @@ class _QuantSparseMoe(QuantModule): | |
|
|
||
| Supports ``layer_sync_moe_local_experts_amax`` to sync input quantizer amax across experts. | ||
|
|
||
| Optionally supports two config-driven features (disabled by default): | ||
| Optionally supports config-driven features (disabled by default): | ||
| - ``_moe_calib_experts_ratio``: force-forward tokens to more experts during calibration. | ||
| - ``_moe_count_expert_calib_tokens``: count tokens routed to each expert during calibration. | ||
| When set to a value > 0, also enables token counting per expert. | ||
|
|
||
| When both are disabled, forward is a direct pass-through with zero overhead. | ||
| When disabled, forward is a direct pass-through with zero overhead. | ||
| """ | ||
|
Comment on lines
+447
to
452
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring inaccuracy: token counting is not enabled when ratio == 1.0 Line 449 states "When set to a value > 0, also enables token counting per expert," but line 512 only enables counting when 📝 Suggested docstring fix Optionally supports config-driven features (disabled by default):
- ``_moe_calib_experts_ratio``: force-forward tokens to more experts during calibration.
- When set to a value > 0, also enables token counting per expert.
+ When set to a value in (0, 1), also enables token counting per expert.
+ At ratio == 1.0, all experts are calibrated so counting is skipped.🤖 Prompt for AI Agents |
||
|
|
||
| def _setup(self): | ||
| self._moe_calib_experts_ratio = None | ||
| self._moe_count_expert_calib_tokens = False | ||
| self._token_counting_initialized = False | ||
|
|
||
| def _init_token_counting(self): | ||
|
|
@@ -501,24 +500,18 @@ def _gate_forward_hook(self, module, input, output): | |
| self.expert_token_count += counts.to(self.expert_token_count.device) | ||
|
|
||
| def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: | ||
| if not self._moe_calib_experts_ratio and not self._moe_count_expert_calib_tokens: | ||
| if self._moe_calib_experts_ratio is None: | ||
| return super().forward(hidden_states) | ||
|
|
||
| if self._moe_count_expert_calib_tokens and not self._token_counting_initialized: | ||
| self._init_token_counting() | ||
|
|
||
| is_calib = any(getattr(m, "_if_calib", False) for m in self.experts.modules()) | ||
| self._count_expert_tokens = is_calib and self._moe_count_expert_calib_tokens | ||
|
|
||
| # If any of the experts are in calibration mode, we will forward all tokens to | ||
| # self._moe_calib_experts_ratio % of the experts to improve the calibration coverage. | ||
| # This is used only for calibration, we need to re-calculate the actual outputs again using | ||
| # the original top_k | ||
| if is_calib and self._moe_calib_experts_ratio: | ||
| self._count_expert_tokens = True | ||
| assert 0 < self._moe_calib_experts_ratio <= 1, ( | ||
| "moe_calib_experts_ratio must be between 0 and 1" | ||
| ) | ||
|
|
||
| # During calibration, forward all tokens to a larger fraction of experts to improve | ||
| # calibration coverage, then re-run with the original top_k for actual outputs. | ||
| if is_calib: | ||
| # Skip counting when all experts are calibrated (ratio == 1.0). | ||
| self._count_expert_tokens = self._moe_calib_experts_ratio < 1.0 | ||
| if self._count_expert_tokens and not self._token_counting_initialized: | ||
| self._init_token_counting() | ||
| if TRANSFORMERS_VERSION_GE_5_0: | ||
| assert hasattr(self, "gate") and hasattr(self.gate, "top_k") | ||
| original_top_k = self.gate.top_k | ||
|
|
@@ -559,7 +552,12 @@ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: | |
| return output | ||
|
|
||
| def layer_sync_moe_local_experts_amax(self): | ||
| """Sync input_quantizer amax across experts so all share the same amax per quantizer.""" | ||
| """Sync input_quantizer amax across experts so all share the same amax per quantizer. | ||
|
|
||
| Skipped when _moe_calib_experts_ratio is set, as each expert is calibrated independently. | ||
| """ | ||
| if self._moe_calib_experts_ratio is not None: | ||
| return | ||
| sync_moe_expert_amax(self.experts) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.