Repository navigation
[MoE][7/n] Keep 3D tensors through MoE, flatten inside GroupedExperts - #3423
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
Move the `.view(-1, dim)` flatten from `MoE.forward()` into `GroupedExperts.forward()` and `GptOssGroupedExperts.forward()`, so that the router and shared experts operate on 3D `(bs, slen, dim)` tensors throughout. This fixes the `_router_gate_config` for full_dtensor mode: previously, `MoE.forward()` called `.view(-1, dim)` on a 3D DTensor with `Shard(1)`, creating a `StridedShard` that the gate config could not express. With this change the gate sees 3D input and can use `Shard(1)` (slen dim) directly. Changes: - `GroupedExperts.forward`: flatten x, top_scores, selected_experts_indices to 2D at entry; return 2D output from combine (no `.view(input_shape)` — would break when sp_size > 1 since combine expands the token count). - `GptOssGroupedExperts.forward`: same flatten-at-entry pattern, remove `.view(input_shape)`. - `TokenChoiceTopKRouter`: use `dim=-1` everywhere instead of hardcoded dim indices; `unflatten(-1, ...)` and `flatten(-2)` for node-limited routing; works with any number of leading dims. - `MoE.forward`: keep 3D x, pass to router/experts/shared_experts, `return out.reshape(bs, slen, dim)` at the end. - `moe_sharding.py`: update shared expert configs from `Shard(1)` to `Shard(2)` (feature dim of 3D); router gate EP config from `Shard(0)` to `Shard(1)` (slen dim of 3D). ghstack-source-id: b179269 Pull-Request: #3423
Move the `.view(-1, dim)` flatten from `MoE.forward()` into `GroupedExperts.forward()` and `GptOssGroupedExperts.forward()`, so that the router and shared experts operate on 3D `(bs, slen, dim)` tensors throughout. This fixes the `_router_gate_config` for full_dtensor mode: previously, `MoE.forward()` called `.view(-1, dim)` on a 3D DTensor with `Shard(1)`, creating a `StridedShard` that the gate config could not express. With this change the gate sees 3D input and can use `Shard(1)` (slen dim) directly. Changes: - `GroupedExperts.forward`: flatten x, top_scores, selected_experts_indices to 2D at entry; return 2D output from combine (no `.view(input_shape)` — would break when sp_size > 1 since combine expands the token count). - `GptOssGroupedExperts.forward`: same flatten-at-entry pattern, remove `.view(input_shape)`. - `TokenChoiceTopKRouter`: use `dim=-1` everywhere instead of hardcoded dim indices; `unflatten(-1, ...)` and `flatten(-2)` for node-limited routing; works with any number of leading dims. - `MoE.forward`: keep 3D x, pass to router/experts/shared_experts, `return out.reshape(bs, slen, dim)` at the end. - `moe_sharding.py`: update shared expert configs from `Shard(1)` to `Shard(2)` (feature dim of 3D); router gate EP config from `Shard(0)` to `Shard(1)` (slen dim of 3D). ghstack-source-id: 196aad1 Pull-Request: #3423
Move the `.view(-1, dim)` flatten from `MoE.forward()` into `GroupedExperts.forward()` and `GptOssGroupedExperts.forward()`, so that the router and shared experts operate on 3D `(bs, slen, dim)` tensors throughout. This fixes the `_router_gate_config` for full_dtensor mode: previously, `MoE.forward()` called `.view(-1, dim)` on a 3D DTensor with `Shard(1)`, creating a `StridedShard` that the gate config could not express. With this change the gate sees 3D input and can use `Shard(1)` (slen dim) directly. Changes: - `GroupedExperts.forward`: flatten x, top_scores, selected_experts_indices to 2D at entry; return 2D output from combine (no `.view(input_shape)` — would break when sp_size > 1 since combine expands the token count). - `GptOssGroupedExperts.forward`: same flatten-at-entry pattern, remove `.view(input_shape)`. - `TokenChoiceTopKRouter`: use `dim=-1` everywhere instead of hardcoded dim indices; `unflatten(-1, ...)` and `flatten(-2)` for node-limited routing; works with any number of leading dims. - `MoE.forward`: keep 3D x, pass to router/experts/shared_experts, `return out.reshape(bs, slen, dim)` at the end. - `moe_sharding.py`: update shared expert configs from `Shard(1)` to `Shard(2)` (feature dim of 3D); router gate EP config from `Shard(0)` to `Shard(1)` (slen dim of 3D). ghstack-source-id: 4d20406 Pull-Request: #3423
[ghstack-poisoned]
Move the `.view(-1, dim)` flatten from `MoE.forward()` into `GroupedExperts.forward()` and `GptOssGroupedExperts.forward()`, so that the router and shared experts operate on 3D `(bs, slen, dim)` tensors throughout. This fixes the `_router_gate_config` for full_dtensor mode: previously, `MoE.forward()` called `.view(-1, dim)` on a 3D DTensor with `Shard(1)`, creating a `StridedShard` that the gate config could not express. With this change the gate sees 3D input and can use `Shard(1)` (slen dim) directly. Changes: - `GroupedExperts.forward`: flatten x, top_scores, selected_experts_indices to 2D at entry; return 2D output from combine (no `.view(input_shape)` — would break when sp_size > 1 since combine expands the token count). - `GptOssGroupedExperts.forward`: same flatten-at-entry pattern, remove `.view(input_shape)`. - `TokenChoiceTopKRouter`: use `dim=-1` everywhere instead of hardcoded dim indices; `unflatten(-1, ...)` and `flatten(-2)` for node-limited routing; works with any number of leading dims. - `MoE.forward`: keep 3D x, pass to router/experts/shared_experts, `return out.reshape(bs, slen, dim)` at the end. - `moe_sharding.py`: update shared expert configs from `Shard(1)` to `Shard(2)` (feature dim of 3D); router gate EP config from `Shard(0)` to `Shard(1)` (slen dim of 3D). ghstack-source-id: c3d90f5 Pull-Request: #3423
Move `.view(-1, dim)` from `MoE.forward()` into `GroupedExperts.forward()` / `GptOssGroupedExperts.forward()`, so the router and shared experts see 3D `(bs, slen, dim)` tensors. This avoids `StridedShard` when `.view(-1, dim)` is called on a `Shard(1)` 3D DTensor at the MoE boundary. - `GroupedExperts.forward` / `GptOssGroupedExperts.forward`: flatten to 2D at entry - `TokenChoiceTopKRouter`: use `dim=-1` and relative dim ops to handle any number of leading dims - `moe_sharding.py`: update placements for 3D shapes (`Shard(1)` for slen, `Shard(2)` for feature dim) ghstack-source-id: 9154b16 Pull-Request: #3423
|
@claude review |
|
Claude finished @acisseJZhong's task in 2m 26s —— View job Code Review: [MoE] Keep 3D tensors through MoE, flatten inside GroupedExperts
Overall: Clean PR that correctly moves the flatten from the MoE boundary into GroupedExperts, avoiding Minor Issues
What looks good
|
tianyu-l
left a comment
There was a problem hiding this comment.
please address comments
| x = x.reshape(-1, x.size(-1)) | ||
| top_scores = top_scores.reshape(-1, top_scores.size(-1)) | ||
| selected_experts_indices = selected_experts_indices.reshape( | ||
| -1, selected_experts_indices.size(-1) | ||
| ) |
There was a problem hiding this comment.
can we use view instead of reshape, if things are already contiguous
| ) | ||
|
|
||
| # top scores shape (bs*slen, top_k) | ||
| # top scores shape (*, top_k) |
There was a problem hiding this comment.
can we be explicit and use (bs, slen, top_k)
| x = x.reshape(-1, x.size(-1)) | ||
| top_scores = top_scores.reshape(-1, top_scores.size(-1)) | ||
| selected_experts_indices = selected_experts_indices.reshape( | ||
| -1, selected_experts_indices.size(-1) | ||
| ) |
| """ | ||
| n_tokens = scores.size(0) | ||
| leading_shape = scores.shape[:-1] | ||
| n_tokens = scores[..., 0].numel() |
There was a problem hiding this comment.
Let's assume the dimension is (bs, slen, _) and use them explicitly.
|
@claude review this PR, be concise. |
|
Claude finished @acisseJZhong's task in 48s —— View job Code Review
Overall: Clean, correct change. Moving the flatten into Issues to address (from reviewer comments)
Remaining reviewer requestThe reviewer asked to "assume the dimension is Looks good
|
… routing Merged 7 upstream commits (19c567f..af33f76). Documents which ones needed ezpz replays: - PR pytorch#3398 (Module subclass refactor): 3 import paths replayed in b052f29 — pure import-path swap, class API unchanged. - PR pytorch#3146 (deterministic MoE routing): inherits transitively; this is the upstream fix for the _histc_xpu non-determinism blocker we hit on 2026-05-21. --debug.deterministic on MoE+XPU should now work. - PR pytorch#3423 (MoE [7/n] 3D tensors): inherits transitively; doesn't touch deepseek_v3 callsites. - PR pytorch#3105 (FSDP symm_mem): skipped — ezpz has its own apply_fsdp and symm_mem is an optional optimization XPU CCL likely doesn't support. - PRs pytorch#3331/pytorch#3369/pytorch#3361: graph_trainer-only no-ops. Captures two action items: smoke-test before next production push, and re-try --debug.deterministic on MoE+XPU.
… fix is incomplete Three smokes on my own 2N alloc (12467455): - agpt_2b clean (24.34 GiB, ~5,900 TPS, ~22% MFU) — matches the 2026-05-22 baseline exactly. PR pytorch#3398 nn_modules import-path replay (b052f29) didn't perturb agpt. - moe_2b_ep at the new LBS=2 default clean (27.09 GiB, ~3,200 TPS, 9.4% MFU, loss 12.93→6.12) — numerically identical to the 2026-05-22 baseline (27.08 GiB, ~3,200 TPS, 9.4% MFU, loss 6.15). PR pytorch#3423 (MoE [7/n] 3D tensors) inherits cleanly via the dispatcher path. - moe_2b_ep with --debug.deterministic STILL FAILS with the same _histc_xpu error from 2026-05-21. PR pytorch#3146 was advertised to fix this but the merged diff only contains the aten.topk.default save -list addition — the promised histc → bincount swap in common/moe.py:262 is missing from the commit. Verified via GitHub API. Adds smoke report at docs/experiments/moe/sunspot/20260527-smoke-n2-40th-sync.md, links from the moe experiments README, updates the 40th-sync entry in upstream-sync.md, and the journal. Open action item: file upstream issue on pytorch/torchtitan for the incomplete PR pytorch#3146.
Stack from ghstack (oldest at bottom):
Summary
Address #3386 (comment)
Move
.view(-1, dim)fromMoE.forward()intoGroupedExperts.forward()/GptOssGroupedExperts.forward(), so the router and shared experts see 3D(bs, slen, dim)tensors. This avoidsStridedShardwhen.view(-1, dim)is called on aShard(1)3D DTensor at the MoE boundary.GroupedExperts.forward/GptOssGroupedExperts.forward: flatten to 2D at entryTokenChoiceTopKRouter: usedim=-1and relative dim ops to handle any number of leading dimsmoe_sharding.py: update placements for 3D shapes (Shard(1)for slen,Shard(2)for feature dim)Test