-
Notifications
You must be signed in to change notification settings - Fork 580
Add flag-controlled sliced MLA projections #4630
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
dandragona
wants to merge
7
commits into
main
Choose a base branch
from
sliced-mla-projections
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33ffda6
Add flag-controlled sliced MLA projections
dandragona f65d64f
Add unit test for sliced MLA projections
dandragona 3375ac0
Document use_sliced_mla_projections flag
dandragona 52d94b0
Address codecov comments: improve test coverage
dandragona 0e53e1a
Address NuojCheng review comments: check gradients and avoid assertions
dandragona 3f987b8
Address RissyRan review comments on sliced MLA projections
dandragona 6ae6470
Fix code_quality linter errors for sliced MLA projections
dandragona 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -917,17 +917,30 @@ def mla_query_projection( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.q_lora_rank == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q = self.query(inputs_q, out_sharding=query_sharding) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_nope, q_pe = jnp.split(q, [self.qk_nope_head_dim], axis=-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # LoRA path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q = self.wq_a(inputs_q, out_sharding=wqa_out_sharding) # [B, L, q_lora_rank] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q = checkpoint_name(low_rank_q, "query_wa_proj") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q = self.q_norm(low_rank_q) # RMSNorm on low rank | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q = checkpoint_name(low_rank_q, "mla_q") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q = self.wq_b(low_rank_q, out_sharding=query_sharding) # [B, L, n_heads, qk_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.config.use_sliced_mla_proj and self.wq_b.quant is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_nope = self.wq_b( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out_sharding=query_sharding, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slice_bounds=(0, self.qk_nope_head_dim), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) # [B, L, n_heads, qk_nope_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_pe = self.wq_b( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_q, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out_sharding=query_sharding, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slice_bounds=(self.qk_nope_head_dim, self.qk_head_dim), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) # [B, L, n_heads, qk_rope_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q = self.wq_b(low_rank_q, out_sharding=query_sharding) # [B, L, n_heads, qk_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_nope, q_pe = jnp.split(q, [self.qk_nope_head_dim], axis=-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Partial RoPE: Split into non-positional and rotary parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # last dimension: qk_nope_head_dim, qk_rope_head_dim | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_nope, q_pe = jnp.split(q, [self.qk_nope_head_dim], axis=-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_nope = self._maybe_shard_with_logical(q_nope, query_logical_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_pe = self.apply_rotary_embedding(q_pe, inputs_positions=inputs_positions) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q_pe = self._maybe_shard_with_logical(q_pe, query_logical_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -951,10 +964,24 @@ def mla_get_key_value(self, low_rank_main, key_rope, model_mode): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value_logical_name = self.value_axis_names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wkva_out_sharding = create_sharding(self.mesh, key_logical_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kv_out = self.wkv_b(low_rank_main, out_sharding=wkva_out_sharding) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Split kv_out into key_nope and value parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_nope, value = jnp.split(kv_out, [self.qk_nope_head_dim], axis=-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.config.use_sliced_mla_proj and self.wkv_b.quant is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_nope = self.wkv_b( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_main, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out_sharding=wkva_out_sharding, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slice_bounds=(0, self.qk_nope_head_dim), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) # [B, L, n_heads, qk_nope_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value = self.wkv_b( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| low_rank_main, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out_sharding=wkva_out_sharding, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slice_bounds=( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.qk_nope_head_dim, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.qk_nope_head_dim + self.v_head_dim, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) # [B, L, n_heads, v_head_dim] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
966
to
+980
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.
🟡 For the `value` sliced projection of `self.wkv_b`, the code uses `wkva_out_sharding` (derived from `key_logical_name`). Although key and value currently share the same logical layout configuration, using the specific `value_logical_name` to construct `wkvb_out_sharding` is more idiomatically correct, prevents future-proofing bugs if key and value shardings diverge, and aligns with the explicit intention of `mla_get_key_value` where both logical names are fetched.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kv_out = self.wkv_b(low_rank_main, out_sharding=wkva_out_sharding) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Split kv_out into key_nope and value parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_nope, value = jnp.split(kv_out, [self.qk_nope_head_dim], axis=-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_rope = jnp.broadcast_to(key_rope, (key_nope.shape[0], key_nope.shape[1], self.num_query_heads, key_rope.shape[3])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_nope = self._maybe_shard_with_logical(key_nope, key_logical_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_rope = self._maybe_shard_with_logical(key_rope, key_logical_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|---|---|---|
|
|
@@ -2093,6 +2093,108 @@ def test_indexer_autoregression(self, prefill_len, target_len): | |
| self.assertEqual(mla_full_this_idx.shape, mla_idx.shape) | ||
| self.assertTrue(jax.numpy.allclose(mla_full_this_idx, mla_idx, rtol=2e-02, atol=2e-02, equal_nan=False)) | ||
|
|
||
| def test_sliced_mla_projections(self): | ||
| config_arguments = self.config_arguments.copy() | ||
|
|
||
| # Enable sliced projections for one config | ||
| config_arguments_sliced = config_arguments.copy() | ||
| config_arguments_sliced["use_sliced_mla_proj"] = True | ||
|
|
||
| cfg_normal, mla_normal = self.init_mla(config_arguments, rope_type="default") | ||
| _, mla_sliced = self.init_mla(config_arguments_sliced, rope_type="default") | ||
|
|
||
| # Sync weights | ||
| nnx.update(mla_sliced, nnx.state(mla_normal)) | ||
|
|
||
| # Test TRAIN mode with gradient comparison | ||
| lnx, decoder_segment_ids, decoder_positions = self.get_structured_data(cfg_normal, cfg_normal.dtype) | ||
|
|
||
| def loss_fn(model, x): | ||
| out, _ = model( | ||
| x, | ||
| x, | ||
| decoder_segment_ids=decoder_segment_ids, | ||
| inputs_positions=decoder_positions, | ||
| deterministic=True, | ||
| model_mode=MODEL_MODE_TRAIN, | ||
| ) | ||
| return jnp.mean(out.astype(jnp.float32) ** 2), out | ||
|
|
||
| (loss_normal, out_normal_train), (grad_model_normal, grad_x_normal) = nnx.value_and_grad( | ||
| loss_fn, argnums=(0, 1), has_aux=True | ||
| )(mla_normal, lnx) | ||
|
|
||
| (loss_sliced, out_sliced_train), (grad_model_sliced, grad_x_sliced) = nnx.value_and_grad( | ||
| loss_fn, argnums=(0, 1), has_aux=True | ||
| )(mla_sliced, lnx) | ||
|
|
||
| self.assertTrue(jnp.allclose(loss_normal, loss_sliced, rtol=1e-05, atol=1e-05, equal_nan=False)) | ||
| self.assertTrue(jnp.allclose(out_normal_train, out_sliced_train, rtol=1e-05, atol=1e-05, equal_nan=False)) | ||
|
Collaborator
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. for training we want to compare gradients to protect backward pass correctness |
||
| self.assertTrue(jnp.allclose(grad_x_normal, grad_x_sliced, rtol=1e-05, atol=1e-05, equal_nan=False)) | ||
|
|
||
| grad_model_close = jax.tree_util.tree_map( | ||
| lambda x, y: jnp.allclose(x, y, rtol=1e-05, atol=1e-05, equal_nan=False), | ||
| grad_model_normal, | ||
| grad_model_sliced, | ||
| ) | ||
| self.assertTrue(jax.tree_util.tree_all(grad_model_close)) | ||
|
|
||
| # Test PREFILL mode followed by AUTOREGRESSIVE mode to test caching | ||
| prefill_length = cfg_normal.max_prefill_predict_length | ||
| decode_total_length = cfg_normal.max_target_length | ||
|
|
||
| # Re-initialize to ensure clean cache | ||
| cfg_normal, mla_normal = self.init_mla(config_arguments, rope_type="default") | ||
| _, mla_sliced = self.init_mla(config_arguments_sliced, rope_type="default") | ||
| nnx.update(mla_sliced, nnx.state(mla_normal)) | ||
|
|
||
| lnx_prefill = lnx[:, 0:prefill_length, :] | ||
| decoder_segment_ids_prefill = decoder_segment_ids[:, 0:prefill_length] | ||
| decoder_positions_prefill = decoder_positions[:, 0:prefill_length] | ||
|
|
||
| out_normal_prefill, _ = mla_normal( | ||
| lnx_prefill, | ||
| lnx_prefill, | ||
| decoder_segment_ids=decoder_segment_ids_prefill, | ||
| inputs_positions=decoder_positions_prefill, | ||
| deterministic=True, | ||
| model_mode=MODEL_MODE_PREFILL, | ||
| ) | ||
|
|
||
| out_sliced_prefill, _ = mla_sliced( | ||
| lnx_prefill, | ||
| lnx_prefill, | ||
| decoder_segment_ids=decoder_segment_ids_prefill, | ||
| inputs_positions=decoder_positions_prefill, | ||
| deterministic=True, | ||
| model_mode=MODEL_MODE_PREFILL, | ||
| ) | ||
|
|
||
| self.assertTrue(jnp.allclose(out_normal_prefill, out_sliced_prefill, rtol=1e-05, atol=1e-05, equal_nan=False)) | ||
|
|
||
| # Run autoregressive steps | ||
| for idx in range(prefill_length, decode_total_length): | ||
| lnx_idx = lnx[:, idx : idx + 1, :] | ||
| decoder_positions_idx = decoder_positions[:, idx : idx + 1] | ||
|
|
||
| out_normal_idx, _ = mla_normal( | ||
| lnx_idx, | ||
| lnx_idx, | ||
| inputs_positions=decoder_positions_idx, | ||
| deterministic=True, | ||
| model_mode=MODEL_MODE_AUTOREGRESSIVE, | ||
| ) | ||
|
|
||
| out_sliced_idx, _ = mla_sliced( | ||
| lnx_idx, | ||
| lnx_idx, | ||
| inputs_positions=decoder_positions_idx, | ||
| deterministic=True, | ||
| model_mode=MODEL_MODE_AUTOREGRESSIVE, | ||
| ) | ||
|
|
||
| self.assertTrue(jnp.allclose(out_normal_idx, out_sliced_idx, rtol=1e-05, atol=1e-05, equal_nan=False)) | ||
|
|
||
| def test_projection_initialization(self): | ||
| """Tests that MLA and Attention layers initialize the correct projection weights.""" | ||
| # 1. Initialize a standard Attention layer for comparison | ||
|
|
||
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
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.
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.