-
Notifications
You must be signed in to change notification settings - Fork 589
Build the Qwen3-Next shared expert only when shared_experts > 0 #4930
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
Open
WandLZhang
wants to merge
3
commits into
AI-Hypercomputer:main
Choose a base branch
from
WandLZhang:qwen3next-shared-expert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−25
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """The Qwen3-Next sparse MoE block builds a shared expert only when asked.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from flax import nnx | ||
| import jax | ||
| from jax.sharding import Mesh | ||
|
|
||
| from maxtext.configs import pyconfig | ||
| from maxtext.models.qwen3 import Qwen3NextSparseMoeBlock | ||
| from maxtext.utils import maxtext_utils | ||
| from tests.utils.test_helpers import get_test_config_path | ||
|
|
||
|
|
||
| def _count_params(module) -> int: | ||
| return sum(x.size for x in jax.tree.leaves(nnx.state(module, nnx.Param))) | ||
|
|
||
|
|
||
| class Qwen3NextSharedExpertTest(unittest.TestCase): | ||
| """`shared_experts` defaults to 0, so building the expert unconditionally put a | ||
| second full-size MLP in every layer of a dense configuration.""" | ||
|
|
||
| def _block(self, shared_experts: int) -> Qwen3NextSparseMoeBlock: | ||
| config = pyconfig.initialize( | ||
| [None, get_test_config_path()], | ||
| run_name="qwen3_next_shared_expert_test", | ||
| enable_checkpointing=False, | ||
| override_model_config=True, | ||
| model_name="qwen3-next-80b-a3b", | ||
| dtype="bfloat16", | ||
| weight_dtype="bfloat16", | ||
| megablox=False, | ||
| sparse_matmul=False, | ||
| max_target_length=8, | ||
| per_device_batch_size=1, | ||
| base_emb_dim=8, | ||
| base_moe_mlp_dim=16, | ||
| base_num_decoder_layers=1, | ||
| num_experts=2, | ||
| num_experts_per_tok=1, | ||
| shared_experts=shared_experts, | ||
| ) | ||
| mesh = Mesh(maxtext_utils.create_device_mesh(config), config.mesh_axes) | ||
| return Qwen3NextSparseMoeBlock(config=config, mesh=mesh, rngs=nnx.Rngs(params=jax.random.PRNGKey(0))) | ||
|
|
||
| def test_no_shared_expert_when_zero(self): | ||
| block = self._block(shared_experts=0) | ||
| self.assertFalse(block.use_shared_expert) | ||
| self.assertIsNone(block.shared_expert) | ||
| self.assertIsNone(block.shared_expert_gate) | ||
|
|
||
| def test_shared_expert_when_one(self): | ||
| block = self._block(shared_experts=1) | ||
| self.assertTrue(block.use_shared_expert) | ||
| self.assertIsNotNone(block.shared_expert) | ||
| self.assertIsNotNone(block.shared_expert_gate) | ||
|
|
||
| def test_the_only_difference_is_the_shared_expert(self): | ||
| """The parameter count with the flag off is short by the shared expert and | ||
| its gate, and by nothing else.""" | ||
| with_expert = self._block(shared_experts=1) | ||
| without = self._block(shared_experts=0) | ||
| expert_params = _count_params(with_expert.shared_expert) + _count_params(with_expert.shared_expert_gate) | ||
| self.assertGreater(expert_params, 0) | ||
| self.assertEqual(_count_params(with_expert) - _count_params(without), expert_params) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
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.
Conditionally defining instance attributes can lead to
AttributeErrors if they are accessed elsewhere, and it often triggers warnings or errors in static type checkers like Pytype or MyPy. It is a best practice to always initialize all instance attributes in__init__.Consider explicitly initializing
self.shared_expertandself.shared_expert_gatetoNonewhenself.use_shared_expertisFalse.