Skip to content

[spmd_types] VarlenAttention: remove hardcoded spmd_types - #3937

Merged
fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:varlen-spmd
Jul 21, 2026
Merged

fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:varlen-spmd

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

In VarlenAtttention, remove hard coded spmd_types of varlen_attn output.
Similar to #3924 (see comment).

Tested by running llama3_debugmodel_varlen_attn and gpt_oss_debugmodel with --parallelism.spmd_backend spmd_types and --debug.spmd_typechecking.

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 17, 2026
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))

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.

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.

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.

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.

@fegin fegin Jul 20, 2026 •

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.

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.

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.

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?

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.

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.

@fegin
fegin merged commit 5059f32 into pytorch:main Jul 21, 2026
23 of 25 checks passed
saforem2 pushed a commit to saforem2/torchtitan that referenced this pull request Jul 21, 2026
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`.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jul 21, 2026
mreso added a commit to mreso/torchtitan that referenced this pull request Jul 21, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants