Repository navigation
tpu ep refactor: run unchunked EP as chunked EP with one chunk - #247
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_1029966623
branch
from
November 26, 2026 06:18
0ffc51c to
ee772fb
Compare
`TpuRoutedExperts._forward_impl` had two EP forwards: the unchunked TPU EP forward (shared with the EP = 1 code) and `_forward_ep_chunked` for `num_token_chunks > 1`. Each had its own `scale_before_down_proj` row-scale handling around the dispatch, experts and combine. Review of #207 asked for one path, since unchunked EP is chunking with C = 1. This merges them before upcoming work (the dropless fallback, in-kernel dW accumulation, scan + EP, two layers per executable) adds more branches to the same forward. Changes, in `components/token_dispatcher.py`: - `_forward_impl` sends every TPU EP transport (`ring_of_experts`, `ragged_all_to_all`, `auto`) with an EP mesh to `_forward_ep(num_chunks=...)`. EP = 1 and upstream's `all_to_all` keep the code they ran before. - `_ep_num_chunks` picks C: `num_token_chunks`, falling back to the largest divisor of the token count (with the same one-time warning), and 1 on a one-rank EP mesh or an empty batch, as before. - `_ep_dispatch_experts_combine` is the one copy of dispatch, row scaling, experts and combine. Both paths call it. - `_forward_ep` with C = 1 calls it once and does nothing else. Only C > 1 does the following, unchanged: the FSDP2 check, unsharding the expert weights once, FP32 cross-chunk dW accumulation (`_ChunkWeight` / `_Fp32GradAccumulator`), the `ep_chunk_idx` / `ep_max_inflight_chunks` tags, per-chunk token counts for ragged, and recording the dropped routes summed over the chunks. At C = 1 the dispatcher records the one count, as before. How C = 1 stays unchanged: - `tests/test_token_dispatcher.py` gains `test_single_chunk_ep_traces_the_unchunked_graph` (CPU, one process, local stand-ins for the EP collectives). It traces the joint forward and backward at EP = 2, C = 1 (Ring-of-Experts with `scale_before_down_proj`) and compares it node for node (op, target, inputs, output shape and dtype) with a straight-line dispatch, experts, combine reference, with no chunk-tagged nodes and the experts unsharding their own weights once. - The test passes on the code before this change. - C > 1 is unchanged: before and after the change, for C in {1, 2, 4}, both transports, row scaling on and off, and dropless or capacity factor 0.75, the joint traces were identical node for node (including the `ep_*` chunk metadata) in all 24 cases. Outputs, input, score and weight gradients, dropped-count record calls and drop stats were bitwise identical too. Behaviour change, unreachable with the TPU dispatchers: under an EP mesh with a TPU transport and `scale_before_down_proj`, dispatch metadata that isn't `TpuEpDispatchMetadata` now raises `EP scale_before_down_proj requires TPU EP dispatch metadata`. Before, C = 1 went to the EP = 1 local reduce for `LocalDispatchMetadata`, and C > 1 raised the same error worded `Chunked EP ...`. `TpuAllToAllTokenDispatcher` always returns TPU EP metadata there. The FP32 chunk dW accumulation without an FP32 weight copy (`_ChunkWeight` / `_Fp32GradAccumulator`, 34a0d8b) has already landed. This change moves that code into `_forward_ep` without changing it. Tests (CPU): `tests/test_token_dispatcher.py`, `tests/test_ep_token_chunking_distributed.py`, `tests/test_roe_ep_dispatcher_distributed.py`, `tests/test_ragged_ep_dispatcher_distributed.py` and `graph_trainer/tests/test_tpu_graph_trainer_distributed.py` passed. The graph-trainer EP cases skip on CPU (torch.compile); they and `qwen3_5/tests/test_ep_parity_distributed.py` run on TPU only. TPU A/B (performance): `qwen35_moe_35b_split_tokamax_ep_benchmark` (TpuGraphTrainer, EP = 4 Ring-of-Experts) on tpu7x-2x2x2, 30 steps, mean step time over steps 10-30. Both arms ran on the same base image, main (34a0d8b) with and without this change. | Chunks | Arm | Mean step time | MFU | Peak memory | Loss at step 30 | |---|---|---|---|---|---| | C = 1 | main (2 runs) | 5.8125 s, 5.8118 s | 9.271%, 9.272% | 65.00 GiB | 2.19970 | | C = 1 | this change (2 runs) | 5.8123 s, 5.8139 s | 9.271%, 9.268% | 65.00 GiB | 2.19970 | | C = 4 | main | 5.8016 s | 9.288% | 59.07 GiB | 2.18803 | | C = 4 | this change | 5.7998 s | 9.291% | 59.07 GiB | 2.18803 | Step time differs by at most 0.03%, within run-to-run noise, and the logged losses and gradient norms match at every logged step. Ragged all-to-all and other models and EP degrees were not benchmarked. PiperOrigin-RevId: 1030434551
copybara-service
Bot
force-pushed
the
test_1029966623
branch
from
November 26, 2026 06:48
ee772fb to
567f0f5
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.
tpu ep refactor: run unchunked EP as chunked EP with one chunk
TpuRoutedExperts._forward_implhad two EP forwards: the unchunked TPU EP forward (shared with the EP = 1 code) and_forward_ep_chunkedfornum_token_chunks > 1. Each had its ownscale_before_down_projrow-scale handling around the dispatch, experts and combine. Review of #207 asked for one path, since unchunked EP is chunking with C = 1. This merges them before upcoming work (the dropless fallback, in-kernel dW accumulation, scan + EP, two layers per executable) adds more branches to the same forward.Changes, in
components/token_dispatcher.py:_forward_implsends every TPU EP transport (ring_of_experts,ragged_all_to_all,auto) with an EP mesh to_forward_ep(num_chunks=...). EP = 1 and upstream'sall_to_allkeep the code they ran before._ep_num_chunkspicks C:num_token_chunks, falling back to the largest divisor of the token count (with the same one-time warning), and 1 on a one-rank EP mesh or an empty batch, as before._ep_dispatch_experts_combineis the one copy of dispatch, row scaling, experts and combine. Both paths call it._forward_epwith C = 1 calls it once and does nothing else. Only C > 1 does the following, unchanged: the FSDP2 check, unsharding the expert weights once, FP32 cross-chunk dW accumulation (_ChunkWeight/_Fp32GradAccumulator), theep_chunk_idx/ep_max_inflight_chunkstags, per-chunk token counts for ragged, and recording the dropped routes summed over the chunks. At C = 1 the dispatcher records the one count, as before.How C = 1 stays unchanged:
tests/test_token_dispatcher.pygainstest_single_chunk_ep_traces_the_unchunked_graph(CPU, one process, local stand-ins for the EP collectives). It traces the joint forward and backward at EP = 2, C = 1 (Ring-of-Experts withscale_before_down_proj) and compares it node for node (op, target, inputs, output shape and dtype) with a straight-line dispatch, experts, combine reference, with no chunk-tagged nodes and the experts unsharding their own weights once.ep_*chunk metadata) in all 24 cases. Outputs, input, score and weight gradients, dropped-count record calls and drop stats were bitwise identical too.Behaviour change, unreachable with the TPU dispatchers: under an EP mesh with a TPU transport and
scale_before_down_proj, dispatch metadata that isn'tTpuEpDispatchMetadatanow raisesEP scale_before_down_proj requires TPU EP dispatch metadata. Before, C = 1 went to the EP = 1 local reduce forLocalDispatchMetadata, and C > 1 raised the same error wordedChunked EP ....TpuAllToAllTokenDispatcheralways returns TPU EP metadata there.The FP32 chunk dW accumulation without an FP32 weight copy (
_ChunkWeight/_Fp32GradAccumulator, 34a0d8b) has already landed. This change moves that code into_forward_epwithout changing it.Tests (CPU):
tests/test_token_dispatcher.py,tests/test_ep_token_chunking_distributed.py,tests/test_roe_ep_dispatcher_distributed.py,tests/test_ragged_ep_dispatcher_distributed.pyandgraph_trainer/tests/test_tpu_graph_trainer_distributed.pypassed. The graph-trainer EP cases skip on CPU (torch.compile); they andqwen3_5/tests/test_ep_parity_distributed.pyrun on TPU only.TPU A/B (performance):
qwen35_moe_35b_split_tokamax_ep_benchmark(TpuGraphTrainer, EP = 4 Ring-of-Experts) on tpu7x-2x2x2, 30 steps, mean step time over steps 10-30. Both arms ran on the same base image, main (34a0d8b) with and without this change.Step time differs by at most 0.03%, within run-to-run noise, and the logged losses and gradient norms match at every logged step. Ragged all-to-all and other models and EP degrees were not benchmarked.