Skip to content

[Flux] Refactor argparser to follow new practice, Fix dtype and txt_in layer shape issue - #1153

Merged
wwwjn merged 2 commits into
mainfrom
flux-fix
Apr 29, 2025
Merged

wwwjn merged 2 commits into
mainfrom
flux-fix

Conversation

@wwwjn

@wwwjn wwwjn commented Apr 28, 2025 •

Copy link
Copy Markdown
Contributor

What changed?

  1. Refactor Flux arg parser to follow [PoC] Typed JobConfig  #767 new argparse practice
  2. Explicitly change dtype for flux model to avoid [Flux] dtype mismatch if FSDP is not used #1137 (when FSDP is not enabled)
  3. Fix txt_in layer shape issue by changing the config of schnell and dev model [Flux] Wrong setting of text encoding linear layer #1146

dtype management in Flux Training

Without FSDP (fsdp dim = 1) :

  • Encoder's dtype = float32, Flux model's dtype = float32

With FSDP enabled:

  • Encoder's dtype = bfloat16, Text and Image encodings' dtype=bfloat16
  • Flux Model's dtype: sharded params are in fp32, reduce-scatter is in fp32, so gradient updates happen in fp32. dtype when calculation=bfloat16

cc @CarlosGomes98

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 28, 2025
@wwwjn
wwwjn requested review from fegin and tianyu-l April 28, 2025 22:56

@fegin fegin left a comment

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.

LGTM in general, one comment of the model conversion. I feel next time it is good to have the refactor part as a separate PR from the bug fix PRs.

Comment thread torchtitan/experiments/flux/train.py Outdated
assert len(self.model_parts) == 1
model = self.model_parts[0]
# explicitely convert flux model to be Bfloat16 no matter FSDP is applied or not
model = self.model_parts[0].to(self._dtype)

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 we should move this to __init__(), we don't want to do the conversion every iteration if the model is fixed to an certain dtype.

@tianyu-l tianyu-l left a comment

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.

we need to be a bit careful on the dtype conversion -- I left a comment

model_parts = self.model_parts
assert len(self.model_parts) == 1
model = self.model_parts[0]
# explicitely convert flux model to be Bfloat16 no matter FSDP is applied or not

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.

With FSDP, it is not the same before vs. after this PR.

  • before: sharded params are in fp32, reduce-scatter is in fp32, so gradient updates happen in fp32
  • after: sharded params are converted to bf16, reduce-scatter is in fp32, I would guess gradient updates happen in fp16 after a dtype conversion

How about we do the following?
explicitly convert dtype of encoder to training.mixed_precision_param if fsdp is used

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.

Hmm, what's the actual dtype the model is going to use without any parallelisms? If it is bf16, then we should always bf16 even if FSDP is applied.

@wwwjn wwwjn Apr 29, 2025 •

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.

With FSDP: Tianyu's plan (before this PR version) is the most accurate version.

  • before: sharded params are in fp32, reduce-scatter is in fp32, so gradient updates happen in fp32
  • Encoder dtypes: bfloat16

Without any parallelism, I guess the actual / most-accurate dtype should be float32.

@tianyu-l tianyu-l linked an issue Apr 29, 2025 that may be closed by this pull request
@wwwjn
wwwjn merged commit 00b4a47 into main Apr 29, 2025
@wwwjn
wwwjn deleted the flux-fix branch July 2, 2025 21:25
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
…n layer shape issue (pytorch#1153)

## What changed?
1. Refactor Flux arg parser to follow pytorch#767 new argparse practice
2. Explicitly change dtype for flux model to avoid pytorch#1137 (when FSDP is
not enabled)
3. Fix txt_in layer shape issue by changing the config of schnell and
dev model pytorch#1146


## dtype management in Flux Training 

Without FSDP (fsdp dim = 1) :
- Encoder's dtype = float32, Flux model's dtype = float32

With FSDP enabled:
- Encoder's dtype = bfloat16, Text and Image encodings' dtype=bfloat16
- Flux Model's dtype: sharded params are in fp32, reduce-scatter is in
fp32, so gradient updates happen in fp32. dtype when
calculation=bfloat16

cc @CarlosGomes98
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
…n layer shape issue (pytorch#1153)

## What changed?
1. Refactor Flux arg parser to follow pytorch#767 new argparse practice
2. Explicitly change dtype for flux model to avoid pytorch#1137 (when FSDP is
not enabled)
3. Fix txt_in layer shape issue by changing the config of schnell and
dev model pytorch#1146


## dtype management in Flux Training 

Without FSDP (fsdp dim = 1) :
- Encoder's dtype = float32, Flux model's dtype = float32

With FSDP enabled:
- Encoder's dtype = bfloat16, Text and Image encodings' dtype=bfloat16
- Flux Model's dtype: sharded params are in fp32, reduce-scatter is in
fp32, so gradient updates happen in fp32. dtype when
calculation=bfloat16

cc @CarlosGomes98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

[Flux] dtype mismatch if FSDP is not used

4 participants