Repository navigation
[spmd_types] FLUX enablement - #3823
Conversation
[ghstack-poisoned]
| qkv_bias: bool = False | ||
| inner_attention: ScaledDotProductAttention.Config = field( | ||
| default_factory=ScaledDotProductAttention.Config | ||
| ) |
There was a problem hiding this comment.
this is so the sharding config can be patched i believe
|
|
||
|
|
||
| def annotate_dp_cp_params_as_r(model: nn.Module, parallel_dims: ParallelDims) -> None: | ||
| # TODO(pianpwk): Infer these from the active SPMD mesh instead. |
There was a problem hiding this comment.
R@DP-axis inference works with partial annotations, but not yet with empty tensors. working to add that in pytorch
| batch_type = { | ||
| DP: spmd.S(0), | ||
| CP: spmd.R, | ||
| } |
There was a problem hiding this comment.
seems flux multimodal inputs are evenly sharded?
There was a problem hiding this comment.
please verify that numerics doesn't change with DP & CP involved
There was a problem hiding this comment.
I needed to patch this to do simple KV allgather instead of the pytorch ring-attention context, but yes with that we get bitwise:
torchtitan/torchtitan/distributed/context_parallel.py
Lines 80 to 99 in c2a3293
was going to ask if you wanted that added, or an explicit ring-attention impl in titan
There was a problem hiding this comment.
Let's pretend there's no ring-attention impl.
For the all-gather one, is SDPA supported
- for full bidirectional masking, if q (sharded) and kv (replicate) don't have the same length, would it just work? I think this is doable, but I'm not sure if sdpa supports that.
- for full causal, would sharded q and replicate kv just work? I couldn't imagine how indices are aligned.
There was a problem hiding this comment.
if q (sharded) and kv (replicate) don't have the same length, would it just work?
Based on doc notation it seems supported... https://docs.pytorch.org/docs/2.12/generated/torch.nn.functional.scaled_dot_product_attention.html
for full causal, would sharded q and replicate kv just work? I couldn't imagine how indices are aligned.
is this concerning FLUX or something else? The callsites in FLUX should all be no-mask right, while other models are on Flex/VarLen I think?
There was a problem hiding this comment.
concerning something that doesn't exist in torchtitan
- if someone uses torchtitan to experiment with causal sdpa with all-gather CP, would they get an error?
- some graph trainer tests are still on SDPA I believe.
There was a problem hiding this comment.
cc @fegin , i'm not sure. I guess we could easily introduce a apply_cp_kv_allgather_to_forward
There was a problem hiding this comment.
Full causal won't work today without some mask/bias work. This only works for Flux because it doesn't use causal masks.
|
|
||
| q, k = apply_rope(q, k, pe) | ||
| attn = self.inner_attention(q, k, v) | ||
| attn = self.inner_attention(q, k, v, is_causal=False) |
There was a problem hiding this comment.
SelfAttention has this flag=False. I'm not sure why SingleStreamBlock and DoubleStreamBlock don't set it to False. Could you check BFL's official FLUX.1 implementation to confirm?
There was a problem hiding this comment.
yes, seems both blocks call attention: https://github.com/black-forest-labs/flux/blob/802fb4713906133fcbd0d8dc5351620ca4773036/src/flux/modules/layers.py#L194
which calls F.SDPA: https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py#L6
which defaults is_causal=False: https://docs.pytorch.org/docs/2.12/generated/torch.nn.functional.scaled_dot_product_attention.html
|
|
||
| q, k = apply_rope(q, k, pe) | ||
| attn = self.inner_attention(q, k, v) | ||
| attn = self.inner_attention(q, k, v, is_causal=False) |
There was a problem hiding this comment.
SelfAttention has this flag=False. I'm not sure why SingleStreamBlock and DoubleStreamBlock don't set it to False. Could you check BFL's official FLUX.1 implementation to confirm?
| ["cp", "dp_shard", "dp_replicate"] | ||
| if config.parallelism.spmd_backend == "spmd_types" | ||
| else ["fsdp", "dp_replicate"] |
There was a problem hiding this comment.
is there anything particular with the order? (it looks inner -> outer mesh ordered, not sure if matters)
There was a problem hiding this comment.
I think the seed offset logic here wants innermost-dim first?
torchtitan/torchtitan/distributed/utils.py
Lines 245 to 259 in 0052a38
not 100% sure
There was a problem hiding this comment.
The order doesn't matter. This is used to guarantee that we will get different seed among these dimenions.
| batch_type = { | ||
| DP: spmd.S(0), | ||
| CP: spmd.R, | ||
| } |
There was a problem hiding this comment.
please verify that numerics doesn't change with DP & CP involved
| from torchtitan.models.common.attention import ScaledDotProductAttention | ||
| from torchtitan.models.common.nn_modules import GELU, LayerNorm, Linear, RMSNorm, SiLU | ||
| from torchtitan.models.flux.model.model import ( | ||
| local_concat_text_image_attention_states, |
There was a problem hiding this comment.
why not defining this function in this file? actually in the same forward where this function is used
| return combined[:, :text_seq_len], combined[:, text_seq_len:] | ||
|
|
||
|
|
||
| @spmd.local_map( |
There was a problem hiding this comment.
if using local_map, we should annotate in_types
However, according to our previous discussion, this could be an ad hoc local context (instead of introducing a function). Either way is fine to me.
| return torch.cat((text, image), dim=1) | ||
|
|
||
|
|
||
| @spmd.local_map( |
| ), | ||
| out_types=spmd.PartitionSpec("dp", "cp", None, None), | ||
| ) | ||
| def local_concat_text_image_attention_states( |
There was a problem hiding this comment.
can we move this within the function where it's used?
| ), | ||
| out_types=spmd.PartitionSpec("dp", "cp", None), | ||
| ) | ||
| def local_concat_text_image(text: torch.Tensor, image: torch.Tensor) -> torch.Tensor: |
| @@ -58,11 +60,16 @@ def __init__(self, config: Config): | |||
| # Set random seed, and maybe enable deterministic mode | |||
| # (mainly for debugging, expect perf loss). | |||
| # For Flux model, we need distinct seed across FSDP ranks to ensure we randomly dropout prompts info in dataloader | |||
There was a problem hiding this comment.
See this comment:
given CP shards data after dataloading (this is TRUE, right??), I don't think we need to put CP in this list, regardless of spmd_types or DTensor.
So technically it should be [dp_shard, dp_replicate]
| dp_mesh_dims = None | ||
| assert dp_mesh is not None | ||
| if parallelism.spmd_backend == "spmd_types": | ||
| annotate_dp_cp_params_as_r(model, parallel_dims) |
There was a problem hiding this comment.
wait, these should happen in set_sharding?
There was a problem hiding this comment.
where is this function? if you mean ShardingConfig-based, it's a bit plain to annotate everything as R@DP/CP
| set_flux_inner_attention_local_map(block_cfg.inner_attention) | ||
|
|
||
|
|
||
| def annotate_dp_cp_params_as_r(model: nn.Module, parallel_dims: ParallelDims) -> None: |
There was a problem hiding this comment.
Did you and Ed finally decided not to infer this automatically inside FSDP?
There was a problem hiding this comment.
No, we added it, but it only supports the partially-annotated case (TP is on, but not DP/CP), this is the totally unannotated case (param empty, need to look up spmd.current_mesh).
Started trying to extend this in pytorch
| ), | ||
| out_types=spmd.PartitionSpec("dp", "cp", None, None), | ||
| ) | ||
| def local_concat_text_image_attention_states( |
There was a problem hiding this comment.
Does @spmd.local_map not support annotating an inline embedded function?
If you have to put it as a separate function
| def local_concat_text_image_attention_states( | |
| def _local_concat_text_image_attention_states( |
| ), | ||
| out_types=spmd.PartitionSpec("dp", "cp", None), | ||
| ) | ||
| def local_concat_text_image(text: Tensor, image: Tensor) -> Tensor: |
There was a problem hiding this comment.
| def local_concat_text_image(text: Tensor, image: Tensor) -> Tensor: | |
| def _local_concat_text_image(text: Tensor, image: Tensor) -> Tensor: |
There was a problem hiding this comment.
btw could you answer my question?
Does
@spmd.local_mapnot support annotating an inline embedded function?
There was a problem hiding this comment.
oops sorry, dropped a change. should be fine
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * __->__ pytorch#3823
Stack from ghstack (oldest at bottom):