Repository navigation
[spmd_types] VarlenAttention: remove hardcoded spmd_types - #3937
Conversation
Summary: Remove topology-specific assumptions at the opaque variable-length attention boundary so its outputs preserve the actual packed-query distribution. This aligns variable-length attention with the query-derived typing used by other attention backends while retaining the existing context-parallel limitation. Test Plan: Ran one training step of Llama 3 VarlenAttention with four-way FSDP, the spmd_types backend, and SPMD typechecking enabled; training completed with loss 8.17858. Ran one training step of GPT-OSS VarlenAttention with four-way FSDP/expert parallelism, the spmd_types backend, and SPMD typechecking enabled; training completed with loss 8.53292, covering the auxiliary LSE path.
| assert isinstance(result, torch.Tensor) | ||
| if get_spmd_backend() == "spmd_types" and spmd.is_type_checking(): | ||
| # exclude CP from typecheck as varlen + CP is not yet supported. | ||
| spmd.assert_type(result, spmd.V, spmd.PartitionSpec("dp", "tp", None)) |
There was a problem hiding this comment.
Is the problem that some parallelism may not be used and if so assert_type would raise?
@pianpwk If spmd_types ought to suppor the case where axis has degree 1, would it not be reasonable to support when an axis is missing? From programming perspective, why users need to be so careful.
There was a problem hiding this comment.
I am not sure if this is what you are asking, but the typecheck still passes even if you use spmd.PartitionSepc(("dp, "cp"), "tp", None)) when CP is not used.
There was a problem hiding this comment.
The problem is that this kernel is used for both decoder and encoder, where encoder has a different sharding type. We don't do CP for encoder.
When CP dimension is enabled, we will get an assert from encoder. That's the lesson from FlexAttention -- we shouldn't hardcode the sharding for attention kernel.
There was a problem hiding this comment.
I think I understood what's going on.
Are you saying for encoder we use CP axis, but perform replicate operations on it?
- Is this documented anywhere? The code could look confusing without.
- Do we have to waste compute on it? Can we use this axis to do more DP / other useful stuff?
There was a problem hiding this comment.
Are you saying for encoder we use CP axis, but perform replicate operations on it?
Yes.
Is this documented anywhere? The code could look confusing without.
We can document the attention kernels. The output should be the same sharding type as query. This part looks okay to me.
Do we have to waste compute on it? Can we use this axis to do more DP / other useful stuff?
It depends.
So the root cause is that we are using one SPMD DeviceMesh for both decoder and encoder. So the attention kernel cannot assume CP axis is always sharded. You can use different meshes for encoder and decoder, which is what you said, using more DP degrees to cover CP. But you will need a collective to transit from LargerDP (encoder) to DP + CP (decoder). This worths to try. But I don't know the benefit of it. And that should be orthogonal to this fix.
In `VarlenAtttention`, remove hard coded `spmd_types` of `varlen_attn` output. Similar to pytorch#3924 (see [comment](pytorch#3924 (review))). Tested by running `llama3_debugmodel_varlen_attn` and `gpt_oss_debugmodel` with `--parallelism.spmd_backend spmd_types ` and `--debug.spmd_typechecking`.
Absorbs 4 upstream commits (checkpoint load/resume clarify pytorch#3732, override kwargs from CLI+config pytorch#3894, [spmd_types] VarlenAttention un-hardcode pytorch#3937, VLLMGenerator readline hang patch pytorch#3950). No conflicts; spmd_types regression verified -- GQA MoE TP+EP+typecheck (12.48->6.82) and DSA GLM-5 TP=2 (12.53->6.82) both clean.
In
VarlenAtttention, remove hard codedspmd_typesofvarlen_attnoutput.Similar to #3924 (see comment).
Tested by running
llama3_debugmodel_varlen_attnandgpt_oss_debugmodelwith--parallelism.spmd_backend spmd_typesand--debug.spmd_typechecking.