Skip to content

tpu ep refactor: run unchunked EP as chunked EP with one chunk - #247

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_1029966623
Nov 26, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_1029966623

Conversation

@copybara-service

Copy link
Copy Markdown

tpu ep refactor: run unchunked EP as chunked EP with one chunk

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.

`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
copybara-service Bot merged commit 567f0f5 into main Nov 26, 2026
2 checks passed
@copybara-service
copybara-service Bot deleted the test_1029966623 branch November 26, 2026 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant