Repository navigation
move padding to inference - #4100
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
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]
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 ```
tianyu-l
left a comment
There was a problem hiding this comment.
looks reasonable! some questions
| if self.use_v2_model_runner: | ||
| raise ValueError( | ||
| "TorchTitan's vLLM integration requires the V1 model runner" | ||
| ) |
There was a problem hiding this comment.
shall we follow up with the vllm side fix so that we can upgrade to v2? @pzhan9
| expert_sequence_parallel_size: int = 1, | ||
| enable_sequence_parallel: bool = False, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
does it run well when it's turned on?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
passing enable_sp from compilation_config feels ... weird, but I guess that's how you work with vllm?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
oh? I didn't see any sp_min_token_num
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