Skip to content

move padding to inference - #4100

Merged
acisseJZhong merged 11 commits into
mainfrom
gh/acisseJZhong/29/head
Aug 11, 2026
Merged

acisseJZhong merged 11 commits into
mainfrom
gh/acisseJZhong/29/head

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary

Move uneven sequence padding out of the common MoE model and into the vLLM inference path, where
uneven decode batches originate. The custom vLLM V1 runner pads dense/expert-SP token counts to the
TP degree and filters invalid expert-SP CUDA graph capture sizes.

This keeps inference-specific padding out of shared model and training code.

Test Plan

CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc-per-node=4 -m pytest -q torchtitan/
experiments/rl/tests/test_generator.py::test_vllm_uneven_decode_tp_padding

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 10, 2026
@acisseJZhong acisseJZhong changed the title move padding to inference [do not review] move padding to inference Aug 10, 2026
[ghstack-poisoned]
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request Aug 10, 2026
ghstack-source-id: fad6380
Pull-Request: #4100
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request Aug 10, 2026
ghstack-source-id: 03eb6de
Pull-Request: #4100
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request Aug 10, 2026
ghstack-source-id: 216f5ee
Pull-Request: #4100
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request Aug 10, 2026
ghstack-source-id: 23e926d
Pull-Request: #4100
@acisseJZhong acisseJZhong changed the title [do not review] move padding to inference move padding to inference Aug 10, 2026
acisseJZhong added a commit that referenced this pull request Aug 11, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4045
* #4067
* #4100
* #4080
* __->__ #3996

## Summary

This updates the MoE EP/SP sharding contract so token dispatchers return
the local sequence shard directly instead of constructing a full
global-length sparse contribution.

The main change is that routed expert outputs are represented as the MoE
local output layout:
- EP on: sequence-sharded layout
- EP off, experts using pure TP: Partial layout

Shared experts are updated to produce the same layout as routed experts
before the routed + shared add. The outer MoE boundary remains
responsible for redistributing the final MoE output to `sp_layout`.

## Changes

- Update standard AllToAll combine to scatter into a local-shard output
buffer instead of a full `sp_size`-expanded buffer.
- Remove the SP global-index remapping path from EP token dispatchers.
- Remove `local_seq_len_after_padding` from the dispatcher combine
interface since local-shard combine no longer needs per-rank-to-global
SP index mapping.
- Keep `num_local_tokens_after_padding` for logical sequence padding, so
AllToAll can size the local padded output without materializing pad
rows.
- Make DeepEP combine stay async for all SP sizes; `MoE.forward()` syncs
before the routed output is read.
- Update MoE sharding annotations so routed and shared expert outputs
match before addition:
  - under EP: sequence-sharded
  - without EP: Partial
- Move shared expert TP redistributions onto the linear layers so
`w1/w3` expose input all-gather and `w2` exposes
Partial-to-output-layout reduction.
- Update the MoE sharding diagram.

## Test plan
loss parity on FSDP4, TP=2, SP on, EP=2, ETP=1: AssertionError:
7.949278831481934 != 7.9492878913879395 : Loss mismatch at step 1:
baseline=7.949278831481934, test=7.9492878913879395

```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='deepseek_v3' --baseline-config='deepseek_v3_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2" --baseline-ngpus=8 --test-module='deepseek_v3' --test-config='deepseek_v3_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2" --test-ngpus=8 --assert-equal --no-seed-checkpoint
```

loss parity on FSDP4, TP=2, SP off, EP=2, ETP=1: [LOSS_COMPARE] Average
difference: 0.000000
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='deepseek_v3' --baseline-config='deepseek_v3_debugmodel' --baseline-options="--parallelism.spmd_backend spmd_types --parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable-sequence-parallel" --baseline-ngpus=8 --test-module='deepseek_v3' --test-config='deepseek_v3_debugmodel' --test-options="--parallelism.spmd_backend spmd_types --parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 2 --parallelism.no-enable-sequence-parallel" --test-ngpus=8 --assert-equal --no-seed-checkpoint
```

loss parity on FSDP4, TP=2, SP on, EP=1, ETP=2: AssertionError:
6.0643310546875 != 6.064278602600098 : Loss mismatch at step 2:
baseline=6.0643310546875, test=6.064278602600098
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='deepseek_v3' --baseline-config='deepseek_v3_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 1" --baseline-ngpus=8 --test-module='deepseek_v3' --test-config='deepseek_v3_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 1" --test-ngpus=8 --assert-equal --no-seed-checkpoint
```

loss parity on FSDP4, TP=2, SP off, EP=1, ETP=2: [LOSS_COMPARE] Average
difference: 0.000000
```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='deepseek_v3' --baseline-config='deepseek_v3_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 1 --parallelism.no-enable-sequence-parallel" --baseline-ngpus=8 --test-module='deepseek_v3' --test-config='deepseek_v3_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 4 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 1 --parallelism.no-enable-sequence-parallel" --test-ngpus=8 --assert-equal --no-seed-checkpoint
```

passed spmd type checking: 
```
NCCL_NVLS_ENABLE=0 NGPU=8 MODULE=deepseek_v3 CONFIG=deepseek_v3_debugmodel ./run_train.sh \
  --parallelism.pipeline_parallel_degree 1 \
  --parallelism.data_parallel_shard_degree 4 \
  --parallelism.tensor_parallel_degree 2 \
  --parallelism.expert_parallel_degree 2 \
  --parallelism.spmd_backend spmd_types \
  --debug.spmd_typechecking \
  --training.steps 10 \
  activation-checkpoint:none
```

Also used a temporary script based on `TestBitwiseParityMoEEP` to run
main
  and the current commit with identical checkpoint weights and uneven
  prompts, then compared all prefill/decode logprobs and generated
  token IDs bit-for-bit.
[ghstack-poisoned]
[ghstack-poisoned]
acisseJZhong added a commit that referenced this pull request Aug 11, 2026
ghstack-source-id: 873adbb
Pull-Request: #4100
acisseJZhong added a commit that referenced this pull request Aug 11, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #4045
* #4067
* #4100
* __->__ #4080


## Summary
this would resolve #3622 

Pad MoE inputs to the expert sequence-parallel degree before routing,
then
trim the output to its original length. This ensures equal TP shards for
uneven decode batches and removes virtual-padding bookkeeping from token
  dispatchers.

Adds a TP=EP=4 vLLM test covering a three-request decode batch and
verifying
  bitwise decode/prefill parity.

  ## Test Plan
```
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc-per-node=4     -m pytest -q     torchtitan/experiments/rl/tests/test_generator.py::test_vllm_uneven_decode_tp_padding
```
acisseJZhong added a commit that referenced this pull request Aug 11, 2026
ghstack-source-id: 873adbb
Pull-Request: #4100

@tianyu-l tianyu-l left a comment

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.

looks reasonable! some questions

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.

pleasing

Comment on lines +33 to +36
if self.use_v2_model_runner:
raise ValueError(
"TorchTitan's vLLM integration requires the V1 model runner"
)

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.

shall we follow up with the vllm side fix so that we can upgrade to v2? @pzhan9

Comment on lines +220 to +221
expert_sequence_parallel_size: int = 1,
enable_sequence_parallel: bool = False,

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.

can we not have default values for these two args? You can move them upwards.

expert_parallel_degree: int = 1
"""Expert parallelism degree for MoE layers. 1 means disabled."""

enable_sequence_parallel: bool = False

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.

does it run well when it's turned on?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes it should work, vllm will be responsible to pad to tp_multiple.


def _pad_for_sequence_parallelism(self, num_scheduled_tokens: int) -> int:
tp_size = self.vllm_config.parallel_config.tensor_parallel_size
enable_dense_sp = self.compilation_config.pass_config.enable_sp and tp_size > 1

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.

passing enable_sp from compilation_config feels ... weird, but I guess that's how you work with vllm?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes the reason is that vllm_config.parallel_config doesn't expose enable_sequence_parallel; dense SP is solely decided by the compile for example https://github.com/vllm-project/vllm/blob/adc1200597c0e8e024173bf38270a660b176642d/vllm/config/vllm.py#L1376, even if it is set to True, vllm will turn it off when
- tensor_parallel_size == 1; or
- the hidden-size/device heuristic cannot produce sp_min_token_num.

To provide explicit control of SP, therefore I need to pass this into/override the compilation config and set sp_min_token_num.

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.

oh? I didn't see any sp_min_token_num

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just updated

[ghstack-poisoned]
@acisseJZhong acisseJZhong added ciflow/h100.8 Trigger H100.8 CI ciflow/rl and removed ciflow/rl labels Aug 11, 2026
@acisseJZhong
acisseJZhong changed the base branch from gh/acisseJZhong/29/base to main August 11, 2026 07:25
@acisseJZhong
acisseJZhong merged commit d94ecb9 into main Aug 11, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/rl ciflow/8gpu 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.

2 participants