-
Notifications
You must be signed in to change notification settings - Fork 7k
[Single File] Add single file support for Flux Transformer #9083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,7 @@ | |
| "animatediff": "down_blocks.0.motion_modules.0.temporal_transformer.transformer_blocks.0.attention_blocks.1.pos_encoder.pe", | ||
| "animatediff_v2": "mid_block.motion_modules.0.temporal_transformer.norm.bias", | ||
| "animatediff_sdxl_beta": "up_blocks.2.motion_modules.0.temporal_transformer.norm.weight", | ||
| "flux": "double_blocks.0.img_attn.norm.key_norm.scale", | ||
| } | ||
|
|
||
| DIFFUSERS_DEFAULT_PIPELINE_PATHS = { | ||
|
|
@@ -110,6 +111,8 @@ | |
| "animatediff_v2": {"pretrained_model_name_or_path": "guoyww/animatediff-motion-adapter-v1-5-2"}, | ||
| "animatediff_v3": {"pretrained_model_name_or_path": "guoyww/animatediff-motion-adapter-v1-5-3"}, | ||
| "animatediff_sdxl_beta": {"pretrained_model_name_or_path": "guoyww/animatediff-motion-adapter-sdxl-beta"}, | ||
| "flux-dev": {"pretrained_model_name_or_path": "black-forest-labs/FLUX.1-dev"}, | ||
| "flux-schnell": {"pretrained_model_name_or_path": "black-forest-labs/FLUX.1-schnell"}, | ||
| } | ||
|
|
||
| # Use to configure model sample size when original config is provided | ||
|
|
@@ -503,6 +506,11 @@ def infer_diffusers_model_type(checkpoint): | |
| else: | ||
| model_type = "animatediff_v3" | ||
|
|
||
| elif CHECKPOINT_KEY_NAMES["flux"] in checkpoint: | ||
| if "guidance_in.in_layer.bias" in checkpoint: | ||
| model_type = "flux-dev" | ||
| else: | ||
| model_type = "flux-schnell" | ||
| else: | ||
| model_type = "v1" | ||
|
|
||
|
|
@@ -1859,3 +1867,195 @@ def convert_animatediff_checkpoint_to_diffusers(checkpoint, **kwargs): | |
| ] = v | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def convert_flux_transformer_checkpoint_to_diffusers(checkpoint, **kwargs): | ||
| converted_state_dict = {} | ||
|
|
||
| num_layers = list(set(int(k.split(".", 2)[1]) for k in checkpoint if "double_blocks." in k))[-1] + 1 # noqa: C401 | ||
| num_single_layers = list(set(int(k.split(".", 2)[1]) for k in checkpoint if "single_blocks." in k))[-1] + 1 # noqa: C401 | ||
| mlp_ratio = 4.0 | ||
| inner_dim = 3072 | ||
|
|
||
| # in SD3 original implementation of AdaLayerNormContinuous, it split linear projection output into shift, scale; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can leave outside of the function no since SD3 also uses it? |
||
| # while in diffusers it split into scale, shift. Here we swap the linear projection weights in order to be able to use diffusers implementation | ||
| def swap_scale_shift(weight): | ||
| shift, scale = weight.chunk(2, dim=0) | ||
| new_weight = torch.cat([scale, shift], dim=0) | ||
| return new_weight | ||
|
|
||
| ## time_text_embed.timestep_embedder <- time_in | ||
| converted_state_dict["time_text_embed.timestep_embedder.linear_1.weight"] = checkpoint.pop( | ||
| "time_in.in_layer.weight" | ||
| ) | ||
| converted_state_dict["time_text_embed.timestep_embedder.linear_1.bias"] = checkpoint.pop("time_in.in_layer.bias") | ||
| converted_state_dict["time_text_embed.timestep_embedder.linear_2.weight"] = checkpoint.pop( | ||
| "time_in.out_layer.weight" | ||
| ) | ||
| converted_state_dict["time_text_embed.timestep_embedder.linear_2.bias"] = checkpoint.pop("time_in.out_layer.bias") | ||
|
|
||
| ## time_text_embed.text_embedder <- vector_in | ||
| converted_state_dict["time_text_embed.text_embedder.linear_1.weight"] = checkpoint.pop("vector_in.in_layer.weight") | ||
| converted_state_dict["time_text_embed.text_embedder.linear_1.bias"] = checkpoint.pop("vector_in.in_layer.bias") | ||
| converted_state_dict["time_text_embed.text_embedder.linear_2.weight"] = checkpoint.pop( | ||
| "vector_in.out_layer.weight" | ||
| ) | ||
| converted_state_dict["time_text_embed.text_embedder.linear_2.bias"] = checkpoint.pop("vector_in.out_layer.bias") | ||
|
|
||
| # guidance | ||
| has_guidance = any("guidance" in k for k in checkpoint) | ||
| if has_guidance: | ||
| converted_state_dict["time_text_embed.guidance_embedder.linear_1.weight"] = checkpoint.pop( | ||
| "guidance_in.in_layer.weight" | ||
| ) | ||
| converted_state_dict["time_text_embed.guidance_embedder.linear_1.bias"] = checkpoint.pop( | ||
| "guidance_in.in_layer.bias" | ||
| ) | ||
| converted_state_dict["time_text_embed.guidance_embedder.linear_2.weight"] = checkpoint.pop( | ||
| "guidance_in.out_layer.weight" | ||
| ) | ||
| converted_state_dict["time_text_embed.guidance_embedder.linear_2.bias"] = checkpoint.pop( | ||
| "guidance_in.out_layer.bias" | ||
| ) | ||
|
|
||
| # context_embedder | ||
| converted_state_dict["context_embedder.weight"] = checkpoint.pop("txt_in.weight") | ||
| converted_state_dict["context_embedder.bias"] = checkpoint.pop("txt_in.bias") | ||
|
|
||
| # x_embedder | ||
| converted_state_dict["x_embedder.weight"] = checkpoint.pop("img_in.weight") | ||
| converted_state_dict["x_embedder.bias"] = checkpoint.pop("img_in.bias") | ||
|
|
||
| # double transformer blocks | ||
| for i in range(num_layers): | ||
| block_prefix = f"transformer_blocks.{i}." | ||
| # norms. | ||
| ## norm1 | ||
| converted_state_dict[f"{block_prefix}norm1.linear.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_mod.lin.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}norm1.linear.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_mod.lin.bias" | ||
| ) | ||
| ## norm1_context | ||
| converted_state_dict[f"{block_prefix}norm1_context.linear.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mod.lin.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}norm1_context.linear.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mod.lin.bias" | ||
| ) | ||
| # Q, K, V | ||
| sample_q, sample_k, sample_v = torch.chunk(checkpoint.pop(f"double_blocks.{i}.img_attn.qkv.weight"), 3, dim=0) | ||
| context_q, context_k, context_v = torch.chunk( | ||
| checkpoint.pop(f"double_blocks.{i}.txt_attn.qkv.weight"), 3, dim=0 | ||
| ) | ||
| sample_q_bias, sample_k_bias, sample_v_bias = torch.chunk( | ||
| checkpoint.pop(f"double_blocks.{i}.img_attn.qkv.bias"), 3, dim=0 | ||
| ) | ||
| context_q_bias, context_k_bias, context_v_bias = torch.chunk( | ||
| checkpoint.pop(f"double_blocks.{i}.txt_attn.qkv.bias"), 3, dim=0 | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.to_q.weight"] = torch.cat([sample_q]) | ||
| converted_state_dict[f"{block_prefix}attn.to_q.bias"] = torch.cat([sample_q_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.to_k.weight"] = torch.cat([sample_k]) | ||
| converted_state_dict[f"{block_prefix}attn.to_k.bias"] = torch.cat([sample_k_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.to_v.weight"] = torch.cat([sample_v]) | ||
| converted_state_dict[f"{block_prefix}attn.to_v.bias"] = torch.cat([sample_v_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.add_q_proj.weight"] = torch.cat([context_q]) | ||
| converted_state_dict[f"{block_prefix}attn.add_q_proj.bias"] = torch.cat([context_q_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.add_k_proj.weight"] = torch.cat([context_k]) | ||
| converted_state_dict[f"{block_prefix}attn.add_k_proj.bias"] = torch.cat([context_k_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.add_v_proj.weight"] = torch.cat([context_v]) | ||
| converted_state_dict[f"{block_prefix}attn.add_v_proj.bias"] = torch.cat([context_v_bias]) | ||
| # qk_norm | ||
| converted_state_dict[f"{block_prefix}attn.norm_q.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_attn.norm.query_norm.scale" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.norm_k.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_attn.norm.key_norm.scale" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.norm_added_q.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_attn.norm.query_norm.scale" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.norm_added_k.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_attn.norm.key_norm.scale" | ||
| ) | ||
| # ff img_mlp | ||
| converted_state_dict[f"{block_prefix}ff.net.0.proj.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_mlp.0.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}ff.net.0.proj.bias"] = checkpoint.pop(f"double_blocks.{i}.img_mlp.0.bias") | ||
| converted_state_dict[f"{block_prefix}ff.net.2.weight"] = checkpoint.pop(f"double_blocks.{i}.img_mlp.2.weight") | ||
| converted_state_dict[f"{block_prefix}ff.net.2.bias"] = checkpoint.pop(f"double_blocks.{i}.img_mlp.2.bias") | ||
| converted_state_dict[f"{block_prefix}ff_context.net.0.proj.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mlp.0.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}ff_context.net.0.proj.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mlp.0.bias" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}ff_context.net.2.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mlp.2.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}ff_context.net.2.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_mlp.2.bias" | ||
| ) | ||
| # output projections. | ||
| converted_state_dict[f"{block_prefix}attn.to_out.0.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_attn.proj.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.to_out.0.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.img_attn.proj.bias" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.to_add_out.weight"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_attn.proj.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.to_add_out.bias"] = checkpoint.pop( | ||
| f"double_blocks.{i}.txt_attn.proj.bias" | ||
| ) | ||
|
|
||
| # single transfomer blocks | ||
| for i in range(num_single_layers): | ||
| block_prefix = f"single_transformer_blocks.{i}." | ||
| # norm.linear <- single_blocks.0.modulation.lin | ||
| converted_state_dict[f"{block_prefix}norm.linear.weight"] = checkpoint.pop( | ||
| f"single_blocks.{i}.modulation.lin.weight" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}norm.linear.bias"] = checkpoint.pop( | ||
| f"single_blocks.{i}.modulation.lin.bias" | ||
| ) | ||
| # Q, K, V, mlp | ||
| mlp_hidden_dim = int(inner_dim * mlp_ratio) | ||
| split_size = (inner_dim, inner_dim, inner_dim, mlp_hidden_dim) | ||
| q, k, v, mlp = torch.split(checkpoint.pop(f"single_blocks.{i}.linear1.weight"), split_size, dim=0) | ||
| q_bias, k_bias, v_bias, mlp_bias = torch.split( | ||
| checkpoint.pop(f"single_blocks.{i}.linear1.bias"), split_size, dim=0 | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.to_q.weight"] = torch.cat([q]) | ||
| converted_state_dict[f"{block_prefix}attn.to_q.bias"] = torch.cat([q_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.to_k.weight"] = torch.cat([k]) | ||
| converted_state_dict[f"{block_prefix}attn.to_k.bias"] = torch.cat([k_bias]) | ||
| converted_state_dict[f"{block_prefix}attn.to_v.weight"] = torch.cat([v]) | ||
| converted_state_dict[f"{block_prefix}attn.to_v.bias"] = torch.cat([v_bias]) | ||
| converted_state_dict[f"{block_prefix}proj_mlp.weight"] = torch.cat([mlp]) | ||
| converted_state_dict[f"{block_prefix}proj_mlp.bias"] = torch.cat([mlp_bias]) | ||
| # qk norm | ||
| converted_state_dict[f"{block_prefix}attn.norm_q.weight"] = checkpoint.pop( | ||
| f"single_blocks.{i}.norm.query_norm.scale" | ||
| ) | ||
| converted_state_dict[f"{block_prefix}attn.norm_k.weight"] = checkpoint.pop( | ||
| f"single_blocks.{i}.norm.key_norm.scale" | ||
| ) | ||
| # output projections. | ||
| converted_state_dict[f"{block_prefix}proj_out.weight"] = checkpoint.pop(f"single_blocks.{i}.linear2.weight") | ||
| converted_state_dict[f"{block_prefix}proj_out.bias"] = checkpoint.pop(f"single_blocks.{i}.linear2.bias") | ||
|
|
||
| converted_state_dict["proj_out.weight"] = checkpoint.pop("final_layer.linear.weight") | ||
| converted_state_dict["proj_out.bias"] = checkpoint.pop("final_layer.linear.bias") | ||
| converted_state_dict["norm_out.linear.weight"] = swap_scale_shift( | ||
| checkpoint.pop("final_layer.adaLN_modulation.1.weight") | ||
| ) | ||
| converted_state_dict["norm_out.linear.bias"] = swap_scale_shift( | ||
| checkpoint.pop("final_layer.adaLN_modulation.1.bias") | ||
| ) | ||
|
|
||
| return converted_state_dict | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be okay to hardcode these no? They are the same across both models. We can grab
inner_dimfrom the checkpoint, but not sure aboutmlp_ratio?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @yiyixuxu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think okay to hard code. I would just define them as proper constants at the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if they're only applied within the scope of this function, they don't need to exist as constants that can be accessed globally?