Onboard the Llama4 decoder to explicit sharding - #5066
Open
NuojCheng wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for explicit sharding in the Llama4 model. Key changes include updating the LLaMA embedding shift logic to use jnp.where under explicit sharding to avoid predicate sharding issues, configuring physical shardings and logical constraints in the Llama4 decoder, and adding comprehensive integration, unit, and AOT compilation tests to verify correctness. There are no review comments, and I have no additional feedback to provide.
NuojCheng
force-pushed
the
chengnuojin-explicit-llama4
branch
from
September 4, 2026 16:35
b67dc5b to
7f95c17
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
NuojCheng
marked this pull request as ready for review
September 4, 2026 18:14
NuojCheng
requested review from
A9isha,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
parambole,
richjames0,
shralex,
shuningjin,
vipannalla and
xibinliu
as code owners
September 4, 2026 18:14
Shuwen-Fang
approved these changes
Sep 4, 2026
NuojCheng
force-pushed
the
chengnuojin-explicit-llama4
branch
2 times, most recently
from
September 4, 2026 23:19
f532626 to
9c453cf
Compare
Llama4DecoderLayer and Llama4ScannableBlock now go through maybe_shard_with_logical instead of nn.with_logical_constraint, cache the NamedShardings for their activation and MLP-intermediate layouts, and pass out_sharding / intermediate_sharding down into the attention, RMSNorm, dense MLP and RoutedAndSharedMoE sublayers. Under ShardMode.AUTO every one of those arguments is ignored, so GSPMD infers the same layouts it does today. LLaMARotaryEmbedding, which Llama4 uses via rope_type=llama3.1, could not run under explicit sharding at all: it built the lax.select predicate by tiling a 1-D mask to the full input shape, producing a replicated [B, S, N, H] operand that sharding-in-types rejects against sharded cases. Broadcasting the same mask through jnp.where selects identical elements without carrying a sharding. The rewrite is gated on shard_mode so ShardMode.AUTO keeps the fusion, and therefore the exact rounding, it has today. Tests: a pyconfig check that decoder_block=llama4 is accepted and that the vision encoder still is not, two AOT compiles (llama4-17b-16e with ZeRO-1 plus gradient accumulation on v5p-256, and llama4-17b-128e at FSDP 64 x expert 8 on v5p-1024), and two TPU integration tests. The integration model uses four layers with a MoE step of 2 so it covers all four Llama4 layer variants at once. Explicit sharding is bit-for-bit with auto under tensor parallelism and within 5e-7 under expert parallelism; explicit + ZeRO-1 + gradient accumulation tracks the unsharded auto baseline to 1e-5.
NuojCheng
force-pushed
the
chengnuojin-explicit-llama4
branch
from
September 4, 2026 23:22
9c453cf to
3d2008c
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.
Description
Onboards the Llama4 decoder to
shard_mode=explicit, following the same pattern as the Qwen3, Mistral and DeepSeek blocks that are already supported.Llama4DecoderLayerandLlama4ScannableBlocknow route their activation constraints throughmaybe_shard_with_logicalinstead ofnn.with_logical_constraint, cache theNamedShardings for the activation and MLP-intermediate layouts in__init__, and threadout_sharding/intermediate_shardinginto the attention,RMSNorm, dense MLP andRoutedAndSharedMoEsublayers. UnderShardMode.AUTOevery one of those arguments is ignored and GSPMD infers exactly the layouts it does today, so this is a no-op for existing runs.RoutedAndSharedMoEalready accepts both sharding arguments (it is shared with DeepSeek), so no MoE changes were needed.The rotary embedding fix
LLaMARotaryEmbedding— which Llama4 reaches viarope_type: "llama3.1"— could not run under explicit sharding at all. It built thelax.selectpredicate by tiling a 1-D parity mask up to the full input shape:That produces a replicated
[B, S, N, H]predicate, and sharding-in-types requires the predicate to match the cases in sharding as well as shape, so the trace fails with:Broadcasting the same 1-D mask through
jnp.whereselects identical elements and carries no sharding of its own. The two formulations are mathematically identical — verified elementwise-equal in the forward pass, in the gradient, and in bf16 — but they fuse differently in XLA, and swapping unconditionally shiftedllama3.1losses by ~1e-5 inShardMode.AUTO. Sincellama3.1is out of scope here, the rewrite is gated onshard_modesoAUTOkeeps byte-identical behaviour; this was confirmed by re-running the auto path against pristinemain.Testing
tests/unit/pyconfig_test.py—decoder_block=llama4is accepted under explicit sharding, anduse_multimodal=Trueis still rejected (only the text stack is onboarded; the Llama4 vision encoder is not).tests/unit/train_compile_test.py— two AOT compiles:llama4-17b-16ewith ZeRO-1 + gradient accumulation onv5p-256, andllama4-17b-128eat FSDP 64 × expert 8 onv5p-1024.tests/integration/train_tests.py— the integration model uses 4 layers withinterleave_moe_layer_step=2, which covers all four Llama4 layer variants in one run: chunked+dense, chunked+MoE, chunked+dense and global (NoPE) + MoE.Measured on a local TPU v7-8, 3 steps of synthetic data:
[8.119888, 8.105545, 8.097639][8.119888, 8.105545, 8.097639][8.119880, 8.105589, 8.097667][8.119880, 8.105585, 8.097659][8.120317, 8.107924, 8.101201][8.120396, 8.107988, 8.101174]The ZeRO-1 test carries
@pytest.mark.skip_on_tpu7xfor consistency with the other ZeRO-1 tests (b/517509898); the numbers above were produced by temporarily lifting that marker, and the marker was restored afterwards.Regression sweep: the pre-existing
qwen3,qwen3_moe,qwen3_custom_moe,mistralandmixtralexplicit-vs-auto parity tests, and the existing AOT explicit/ZeRO-1 compiles, all still pass.pyinkis clean andpylintreports only pre-existingR0917/W0613findings on untouched signatures.Known gaps
RoutedMoE.dense_matmul(sparse_matmul=False) is still not onboarded to explicit sharding — a gap Llama4 shares withqwen3_moeandmixtral.num_experts_per_tok=1), so the top-k dispatch path is not exercised.use_multimodalremains rejected under explicit sharding.Tests
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure (put X in square brackets):