Skip to content

[prototype] Expert Parallel - #714

Closed
tianyu-l wants to merge 2 commits into
gh/tianyu-l/21/basefrom
gh/tianyu-l/21/head
Closed

tianyu-l wants to merge 2 commits into
gh/tianyu-l/21/basefrom
gh/tianyu-l/21/head

Conversation

@tianyu-l

@tianyu-l tianyu-l commented Dec 3, 2024 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

The expert-choice MoE implementation is mostly from torchtune: meta-pytorch/torchtune#1902
The PR requires (ad hoc) changes to pytorch: pytorch/pytorch#141937

Issue tracking:

  • [dp2ep] how to apply FSDP only to the non-MoE modules? (for now need to comment out fully_shard)
  • [dp2ep] shard_dim_alltoall not robust (especially during backward with more than 1D)
  • [tp2ep] backward efficiency may not optimized (e.g. right now aten.scatter.src only supports replicate sharding prop)
  • [tp2ep] using DTensor (e.g. in "tp2ep"), the backward aten.scatter.src requires _allow_implicit_replication (maybe because in backward some tensor is not generated as DTensor)
  • some other issues tracked in ad hoc changes to unblock Expert Parallel prototype pytorch#141937
  • torch.compile fails on torch.topk

Haven't worked on

  • softmax scoring instead of sigmoid (can be done similarly, would incur extra communications)
  • part of DP (e.g. CP) to EP

Not considering

  • shared expert overlapping
  • token-choice MoE

[ghstack-poisoned]
tianyu-l added a commit that referenced this pull request Dec 3, 2024
ghstack-source-id: b4d3f46
Pull Request resolved: #714
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 3, 2024
@tianyu-l
tianyu-l marked this pull request as draft December 3, 2024 04:10
The expert-choice MoE implementation is mostly from torchtune: meta-pytorch/torchtune#1902
The PR requires (ad hoc) changes to pytorch: pytorch/pytorch#141937

Issue tracking:
- [ ] [dp2ep] how to apply FSDP only to the non-MoE modules?
- [ ] [dp2ep] `shard_dim_alltoall` not robust (especially during backward with more than 1D)
- [ ] [tp2ep] backward efficiency may not optimized (e.g. right now `aten.scatter.src` only supports [replicate](https://github.com/pytorch/pytorch/blob/main/torch/distributed/tensor/_ops/_tensor_ops.py#L368) sharding prop)
- [ ] [tp2ep] using DTensor (e.g. in "tp2ep"), the backward `aten.scatter.src` requires `_allow_implicit_replication` (maybe because in backward some tensor is not generated as DTensor)
- [ ] some other issues tracked in pytorch/pytorch#141937
- [ ] `torch.compile` fails on `torch.topk`

Haven't worked on
- softmax scoring instead of sigmoid (can be done similarly, would incur extra communications)
- part of DP (e.g. CP) to EP

Not considering
- shared expert overlapping
- token-choice MoE

[ghstack-poisoned]
tianyu-l added a commit that referenced this pull request Dec 3, 2024
ghstack-source-id: d03719e
Pull Request resolved: #714
if self.use_sigmoid:
scores = torch.sigmoid(scores.to(torch.float32)).to(x.dtype)
else:
scores = F.softmax(scores.to(torch.float32), dim=0).to(x.dtype)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment here - it's not clear imo if the optimal order is softmax and then topk, or topk and then softmax.
It does not change the routing, but it changes the weights.
In my nanogpt MoE it has the option to toggle the ordering.

@tianyu-l

tianyu-l commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

somehow I lost access to this ghstack, moving to #725 instead

@tianyu-l tianyu-l closed this Dec 9, 2024
@tianyu-l
tianyu-l deleted the gh/tianyu-l/21/head branch December 18, 2024 00:43
shuhuayu added a commit that referenced this pull request Nov 11, 2026
## Why
The Qwen3.8-Flash-Next QSA attention (#5782) will use `gather_attn`'s
grouped-KV / separate-V Triton kernels. They first appear in
attention-gym v0.0.19, after attention-gym #678, #701, #702, #713, #714,
#715 and #703. torchtitan pins `26beb363`, which predates all of these.
This PR only bumps the pin; the Flash-Next change stays in #5782.

## What changed (all mechanical)
| | Before | After |
| --- | --- | --- |
| attn-gym pin | `26beb363` (#704 merge) | `0d0197fb` (v0.0.19 tag, #703
merge) |

- `.ci/docker/requirements.txt` (root `requirements.txt` symlinks to
it): new SHA. The TODO now says to switch to the PyPI release once
v0.0.19 is published there (PyPI's newest is 0.0.18). #704, which the
old TODO waited on, is already included in both pins.
- `torchtitan/models/common/attn_gym.py` (install hint) and
`tests/unit_tests/cpu/models/common/test_optional_imports.py` (2
asserts): same SHA.

## Why v0.0.19 rather than attention-gym `main`
`26beb363..0d0197fb` changes only `attn_gym/sparse/gather_attn` (plus
its tests and docs). I diffed the two installed `attn_gym` trees to
confirm this. GDN, KDA, short_conv, the indexer, MLA and the CuTe
backend are byte-identical, so qwen3_5, qwen3_8, kimi and the
deepseek_v4 indexer can't change. `main` would also pull in the
warpdrive rewrite and the indexer/delta-H ports (~28 `_delta_rule`
files).

## Numerics
The only `gather_attn` consumer is
`torchtitan/models/deepseek_v4/attention.py`. Old vs new pin, 10 steps,
seed 42, deterministic, loss and grad_norm read from TensorBoard via
`scripts/loss_compare.py`, on GB300:

| Config | GPUs | Result |
| --- | --- | --- |
| `deepseek_v4_debugmodel_fsdp4_ep4` (DistMuon) | 4 | bitwise identical*
|
| `deepseek_v4_debugmodel_fsdp2_tp2_ep2_pp2` with dp_shard=1 (host has 4
GPUs) | 4 | bitwise identical* |

\* With `gather_attn`'s Triton tuners pinned to the same candidate in
both runs. Without pinning, deepseek_v4 isn't run-to-run reproducible on
**either** pin when the autotune cache is cold. `_FWD_TMA` and
`_BWD_DLOCAL_KV_TMA` choose `num_warps` 4 vs 8 by timing, and the two
give different bits. Ranks and cold-cache runs can pick different
winners, and torch's deterministic flag doesn't control this. This
already happens on main. I filed it upstream as
meta-pytorch/attention-gym#777, with a single-GPU repro. Because of it,
this PR adds no deepseek_v4 golden; see follow-ups.

## Testing
- `pre-commit run --all-files` passes, including `pyrefly-check`.
- `pytest tests/unit_tests/cpu/models/common` plus the deepseek_v4,
qwen3_5, qwen3_8 and kimi_k3 CPU tests: 416 passed. The same 9 tests
fail with both the old and new pin on this aarch64 host
(`test_lora_forward` and 8 qwen3_5 tests). The one I inspected,
`test_lora_forward`, fails with "torch.compile on current platform is
not supported for CPU".

## Known issues / follow-ups
- deepseek_v4 golden numerics: add once attention-gym#777 lets us fix
the tile config. The golden then has to be exported on CI's A10G
runners, not on GB300.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants