[megablox] Remove redundant expert-weight transpose copy in unquantized gmm_v2 DLHS - #5123
Open
copybara-service[bot] wants to merge 1 commit into
Open
[megablox] Remove redundant expert-weight transpose copy in unquantized gmm_v2 DLHS#5123copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
copybara-service
Bot
requested review from
RissyRan,
darisoy,
gagika,
igorts-git,
michelle-yooh,
richjames0,
shralex,
shuningjin and
zxhe-sean
as code owners
September 3, 2026 04:45
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…ed gmm_v2 DLHS The tokamax gmm_v2 kernel has no native transpose_rhs support, so the V2 DLHS gradient path (`_dlhs_run_tokamax_v2`) manually materializes `rhs.swapaxes(1, 2)` on every backward step. For the MoE expert weight this is a full-tensor HBM copy of a large `[num_experts, k, n]` array (e.g. `bf16[16, 7168, 2048]`), emitted by XLA as a standalone `copy` with `op_name="rhs"`. The V1 tokamax path already computes the identical DLHS with no transpose: it calls `tokamax.ragged_dot_general` with `DLHS_RAGGED_DOT_DIM_NUMS`, contracting over the `n` dimension directly. This change routes the unquantized gmm_v2 DLHS (quantization_rule is None) through that transpose-free V1 kernel. Quantized paths are unchanged and keep the V2 kernel, because their scale handling (`_bwd_prepare_inputs` / `_dlhs_scale_grad_by_rhs_scale`) is gmm_v2-specific. Verified on TPU with a DeepSeek-scale shape (lhs[m=16384, k=7168], weight[g=16, k=7168, n=2048], bf16): the full expert-weight transpose copy is removed (1 -> 0), gradients are bit-identical (max relative error 0.0), and the full gmm fwd+bwd is ~3% faster. The output is bit-identical, so this is a pure efficiency change with no numerical or convergence impact. PiperOrigin-RevId: 975477390
copybara-service
Bot
force-pushed
the
test_975477390
branch
from
September 3, 2026 05:22
fe19c2f to
d322d47
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
[megablox] Remove redundant expert-weight transpose copy in unquantized gmm_v2 DLHS
The tokamax gmm_v2 kernel has no native transpose_rhs support, so the V2 DLHS
gradient path (
_dlhs_run_tokamax_v2) manually materializesrhs.swapaxes(1, 2)on every backward step. For the MoE expert weight this is a full-tensor HBM copy
of a large
[num_experts, k, n]array (e.g.bf16[16, 7168, 2048]), emitted byXLA as a standalone
copywithop_name="rhs".The V1 tokamax path already computes the identical DLHS with no transpose: it
calls
tokamax.ragged_dot_generalwithDLHS_RAGGED_DOT_DIM_NUMS, contractingover the
ndimension directly. This change routes the unquantized gmm_v2 DLHS(quantization_rule is None) through that transpose-free V1 kernel. Quantized
paths are unchanged and keep the V2 kernel, because their scale handling
(
_bwd_prepare_inputs/_dlhs_scale_grad_by_rhs_scale) is gmm_v2-specific.Verified on TPU with a DeepSeek-scale shape (lhs[m=16384, k=7168],
weight[g=16, k=7168, n=2048], bf16): the full expert-weight transpose copy is
removed (1 -> 0), gradients are bit-identical (max relative error 0.0), and the
full gmm fwd+bwd is ~3% faster. The output is bit-identical, so this is a pure
efficiency change with no numerical or convergence impact.