Repository navigation
Conversation
…CP hook Qwen3 uses colwise_parallel(use_local_output=False) for wq/wk/wv to keep q/k/v as DTensors through q_norm/k_norm (SequenceParallel). When CP is also enabled, the CP attention hook on inner_attention expects plain tensors, not TP-mesh DTensors, causing an AssertionError. Fix: register a pre-forward hook on inner_attention (when CP is enabled) that calls .to_local() on DTensor args before the CP hook runs. This cleanly separates the TP and CP DTensor domains while preserving the autograd chain. ghstack-source-id: d7bfc4b Pull-Request: #2480
| them here. This hook must be registered before the CP hook. | ||
| """ | ||
| new_args = tuple( | ||
| arg.to_local() if isinstance(arg, DTensor) else arg for arg in args |
There was a problem hiding this comment.
No we shouldn't do this. to_local() should always come with grad_placement, which is hard to make generic.
Maybe you can use local_map explicitly around CP region inside Attention.
| inner_attn, | ||
| # Single output: wrap in tuple so local_map sees Tuple[PlacementType] | ||
| out_placements=(shard_heads,), | ||
| in_placements=(shard_heads, shard_heads, shard_heads), |
There was a problem hiding this comment.
what about placements for other args passed into inner attention? do we need to consider them?
There was a problem hiding this comment.
looks like they're non-Tensor args that local_map will flatten away, while BlockMask/VarlenMetadata isn't supported yet; qwen CP only supports SDPA backend. Adding a note there
There was a problem hiding this comment.
I believe CP with Flex attention will be enabled by @fegin soon. Not sure about what's the gap for CP + varlen attention.
| # boundary. TP's use_local_output=False produces DTensor q/k/v, but CP | ||
| # hooks expect plain tensors. local_map handles this with correct | ||
| # grad_placements for backward pass. | ||
| if parallel_dims.tp_enabled and parallel_dims.cp_enabled: |
There was a problem hiding this comment.
do we need to check if use_local_output=False for TP?
There was a problem hiding this comment.
hmm I can't find a clean way to check this
There was a problem hiding this comment.
ic, maybe we can just assume we always use full DTensor for TP, then we need to land #2149 first?
commented
Mar 4, 2026
|
sorry, please ignore the PR in its current state, everything's from Claude. will put more thought into this |
commented
Mar 5, 2026
commented
Mar 6, 2026
|
@pianpwk Do you wanna refine this PR and we can land it as a workaround before full DTensor mode? |
| from torchtitan.tools.logging import logger | ||
|
|
||
|
|
||
| class _LocalMapInnerAttention(nn.Module): |
There was a problem hiding this comment.
This is also useful for torchtitan + vllm as we have to manually patch vllm's attention kernel now which also requires a plain tensor
There was a problem hiding this comment.
can we move this function outside qwen3 as it is useful in general?
…cal before CP hook" Qwen3 uses colwise_parallel(use_local_output=False) for wq/wk/wv to keep q/k/v as DTensors through q_norm/k_norm (SequenceParallel). When CP is also enabled, the CP attention hook on inner_attention expects plain tensors, not TP-mesh DTensors, causing an AssertionError. Fix: register a pre-forward hook on inner_attention (when CP is enabled) that calls .to_local() on DTensor args before the CP hook runs. This cleanly separates the TP and CP DTensor domains while preserving the autograd chain. [ghstack-poisoned]
| inner_attn, | ||
| # Single output: wrap in tuple so local_map sees Tuple[PlacementType] | ||
| out_placements=(shard_heads,), | ||
| in_placements=(shard_heads, shard_heads, shard_heads), |
There was a problem hiding this comment.
I believe CP with Flex attention will be enabled by @fegin soon. Not sure about what's the gap for CP + varlen attention.
| from torchtitan.tools.logging import logger | ||
|
|
||
|
|
||
| class _LocalMapInnerAttention(nn.Module): |
There was a problem hiding this comment.
can we move this function outside qwen3 as it is useful in general?
| # boundary. TP's use_local_output=False produces DTensor q/k/v, but CP | ||
| # hooks expect plain tensors. local_map handles this with correct | ||
| # grad_placements for backward pass. | ||
| if parallel_dims.tp_enabled and parallel_dims.cp_enabled: |
There was a problem hiding this comment.
ic, maybe we can just assume we always use full DTensor for TP, then we need to land #2149 first?
| for block in model.layers.values(): | ||
| attn = block.attention # pyrefly: ignore [missing-attribute] | ||
| # Workaround: cuDNN SDPA backward has a stride mismatch bug with CP. | ||
| # Exclude cuDNN until PyTorch fix lands. |
There was a problem hiding this comment.
do you know what PR is the fix? can you link it in comment?
There was a problem hiding this comment.
Still an open issue: pytorch/pytorch#176915 (comment)
| SDPBackend.MATH, | ||
| ] | ||
| ) | ||
| attn.inner_attention = ( |
There was a problem hiding this comment.
I think this should be fine? L187 was meant to override this class' backend attribute, which is wrapped over:
torchtitan/torchtitan/models/common/attention.py
Lines 169 to 190 in b0a7d32
There was a problem hiding this comment.
ic, my bad. It was a little bit confusing since we are overriding attn.inner_attention. But indeed the backend attribute for the real attn.inner_attention(L195) should be updated.
|
|
||
|
|
||
| class _LocalMapInnerAttention(nn.Module): | ||
| """Wraps inner_attention with local_map for TP + CP composition. |
There was a problem hiding this comment.
Instead of making it only supports TP+CP, can we make it generic and support arbitrary tensor / dtensor inputs -- e.g. every inner_attention wrapper (flex, varlen, sdpa) should inherit an InnerAttentionWrapper which does this local_map treatment. cc @acisseJZhong this aligns with our idea of using local_map for attention in full DTensor.
There was a problem hiding this comment.
just synced with @fegin we should do this to make local_map work with every attention.
There was a problem hiding this comment.
every inner_attention wrapper (flex, varlen, sdpa)
Does SDPA support Dtensor input already?
There was a problem hiding this comment.
Does SDPA support Dtensor input already?
Yes, so it's not absolutely necessary. But unifying the interface with local_map sounds cleaner in torchtitan.
Stack from ghstack (oldest at bottom):
Fixes #2446 by using local_map region for CP