Skip to content

override Flux transformer - #639

Merged
Nerogar merged 16 commits into
Nerogar:masterfrom
dxqb:override_transformer
Apr 21, 2025
Merged

Nerogar merged 16 commits into
Nerogar:masterfrom
dxqb:override_transformer

Conversation

@dxqb

@dxqb dxqb commented Jan 12, 2025 •

Copy link
Copy Markdown
Collaborator

People have started to use and share Flux .safetensors files that only contain the Flux transformer, not the entire model pipeline. The entire pipeline is almost twice the size of the transformer only.

Examples:

  • finetunes on CivitAI, to offer a smaller download size of around 11GB instead of 20
  • I have myself recently tried to train based on Flux merged with a LoRA. But this Comfy workflow to merge a LoRA only outputs the transformer: https://civitai.com/models/982277/flux-lora-merge

Using this PR, you can load the model pipeline from black-forest-labs/FLUX.1-dev, but override the transformer from a safetensors file.

Comment thread modules/ui/ModelTab.py Resolved
@dxqb

dxqb commented Jan 12, 2025

Copy link
Copy Markdown
Collaborator Author

Note: This PR only fully works once diffusers have fixed this issue huggingface/diffusers#10540 by merging this PR huggingface/diffusers#10541.

@dxqb dxqb mentioned this pull request Jan 12, 2025
@dxqb
dxqb marked this pull request as draft January 21, 2025 18:30
@O-J1

O-J1 commented Feb 9, 2025

Copy link
Copy Markdown
Collaborator

Note: This PR only fully works once diffusers have fixed this issue huggingface/diffusers#10540 by merging this PR huggingface/diffusers#10541.

Looks like it was merged? huggingface/diffusers#10545

@dxqb

dxqb commented Feb 9, 2025

Copy link
Copy Markdown
Collaborator Author

Looks like it was merged? huggingface/diffusers#10545

yes, but other open issues above before this PR can be merged

@dxqb

dxqb commented Mar 29, 2025

Copy link
Copy Markdown
Collaborator Author

Finished
As a side effect, this PR cuts RAM usage in half for loading Flux from single files, even without using a transformer overide, by avoiding loading the transformer in float32

@dxqb
dxqb marked this pull request as ready for review March 29, 2025 07:51
dxqb added a commit to dxqb/OneTrainer that referenced this pull request Apr 5, 2025
transformer = FluxTransformer2DModel.from_single_file(
transformer_model_name,
#avoid loading the transformer in float32:
torch_dtype = torch.bfloat16 if weight_dtypes.prior.torch_dtype() is None else weight_dtypes.prior.torch_dtype()

@Nerogar Nerogar Apr 11, 2025 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could now quantize the weights twice if the source is not in bf16 format. For example, if weight_dtypes.prior is set to fp8, and the source is in fp32, then this will first convert to bf16 (losing precision). Then the actual quantization to fp8 is done.

@dxqb dxqb Apr 11, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is model-specific code and Flux was published in bf16, so I don't think we have to worry about precision loss from fp32.
the alternative is mentioned here, #639 (comment), to let the user choose a load dtype.

The current behaviour is to load a fp8 source to fp32 and load a bf16 source to bf16. Loading fp8 sources needs 48 GB vram just for that.
new behaviour is to load both to bf16.

@Nerogar Nerogar Apr 12, 2025 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that argument is only true for the base flux model released by BFL. Fine tunes can be distributed in any other precision. There was some discussion about a possible solution in the diffusers library, but it didn't really reach any conclusion huggingface/diffusers#10679

A load d type could work, but I can almost guarantee you that there will be a lot of questions about that setting

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave it as proposed in the PR.
It's not ideal, but an improvement for all current use cases, which is fp8 and bf16 transformer files. Precision loss can happen only if you had a fp32 transformer file and wanted to load it in fp8, and what's the point of that? If you specifically created an fp32 transformer for some reason on your own (because none exist AFAIK), you would load it in fp32 and not in fp8

@Nerogar
Nerogar merged commit abaf638 into Nerogar:master Apr 21, 2025
@dxqb
dxqb deleted the override_transformer branch April 23, 2025 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants