-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Core] Add AuraFlow #8796
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
[Core] Add AuraFlow #8796
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
0ebbd30
add lavender flow transformer
sayakpaul 939d990
progress.
sayakpaul f08baf3
progress
sayakpaul 005a8f6
progress
sayakpaul e238d56
move out the attention processor.
sayakpaul 570c258
finish implementation of pipeline
sayakpaul b881190
default neg promot
sayakpaul b8237b2
up
sayakpaul 89eea61
fixes
sayakpaul 2c97d04
up
sayakpaul a50e1ff
up for pr
sayakpaul b0d29b2
fix copies
sayakpaul ae037cf
Merge branch 'main' into lavender-flow
sayakpaul ad6cb66
move fp32 layer norm to normalization
sayakpaul 8ae6be7
minor fixes
sayakpaul 47ff911
remove boolean flag and resort to norm_type
sayakpaul 10ed96f
eliminate added_qk_norm
sayakpaul 3d9265e
add added_proj_bias
sayakpaul 84708c4
lavender flow -> aura flow
sayakpaul 4bdea0d
Fix the `added_proj_bias` default value (#8800)
sayakpaul bcbc972
Merge branch 'main' into lavender-flow
sayakpaul 89fad69
remnant aura flow renaming
sayakpaul e73442f
make it possible to reuse prompt embeds.
sayakpaul dccc682
rename to auraflow
sayakpaul f23151b
[lavender-flow] use flow match euler scheduler (#8799)
yiyixuxu d9a01f4
resolve conflicts
sayakpaul 8984d23
more feedback.
sayakpaul a281547
context_norm_type fix
sayakpaul 8830bf1
fix circular import
sayakpaul 4334f72
fix conversion
sayakpaul b1dc5ec
add fast tests for pipeline
sayakpaul 942377d
fix test file name
sayakpaul f8a08b5
fix test path
sayakpaul e9832f9
spacxing brtween initialization
sayakpaul 2c87250
style
sayakpaul 1b3e620
add test for the transformer model.
sayakpaul 66ff7f5
Merge branch 'main' into lavender-flow
sayakpaul ed33913
remove context_norm_type
sayakpaul 6531e54
remove ada continuous.
sayakpaul 0f721ac
address yiyi
sayakpaul 95708dc
Merge branch 'main' into lavender-flow
yiyixuxu 15d3198
style
yiyixuxu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import argparse | ||
|
||
import torch | ||
from huggingface_hub import hf_hub_download | ||
|
||
from diffusers.models.transformers.auraflow_transformer_2d import AuraFlowTransformer2DModel | ||
|
||
|
||
def load_original_state_dict(args): | ||
model_pt = hf_hub_download(repo_id=args.original_state_dict_repo_id, filename="aura_diffusion_pytorch_model.bin") | ||
state_dict = torch.load(model_pt, map_location="cpu") | ||
return state_dict | ||
|
||
|
||
def calculate_layers(state_dict_keys, key_prefix): | ||
dit_layers = set() | ||
for k in state_dict_keys: | ||
if key_prefix in k: | ||
dit_layers.add(int(k.split(".")[2])) | ||
print(f"{key_prefix}: {len(dit_layers)}") | ||
return len(dit_layers) | ||
|
||
|
||
# similar to SD3 but only for the last norm layer | ||
def swap_scale_shift(weight, dim): | ||
shift, scale = weight.chunk(2, dim=0) | ||
new_weight = torch.cat([scale, shift], dim=0) | ||
return new_weight | ||
|
||
|
||
def convert_transformer(state_dict): | ||
converted_state_dict = {} | ||
state_dict_keys = list(state_dict.keys()) | ||
|
||
converted_state_dict["register_tokens"] = state_dict.pop("model.register_tokens") | ||
converted_state_dict["pos_embed.pos_embed"] = state_dict.pop("model.positional_encoding") | ||
converted_state_dict["pos_embed.proj.weight"] = state_dict.pop("model.init_x_linear.weight") | ||
converted_state_dict["pos_embed.proj.bias"] = state_dict.pop("model.init_x_linear.bias") | ||
|
||
converted_state_dict["time_step_proj.linear_1.weight"] = state_dict.pop("model.t_embedder.mlp.0.weight") | ||
converted_state_dict["time_step_proj.linear_1.bias"] = state_dict.pop("model.t_embedder.mlp.0.bias") | ||
converted_state_dict["time_step_proj.linear_2.weight"] = state_dict.pop("model.t_embedder.mlp.2.weight") | ||
converted_state_dict["time_step_proj.linear_2.bias"] = state_dict.pop("model.t_embedder.mlp.2.bias") | ||
|
||
converted_state_dict["context_embedder.weight"] = state_dict.pop("model.cond_seq_linear.weight") | ||
|
||
mmdit_layers = calculate_layers(state_dict_keys, key_prefix="double_layers") | ||
single_dit_layers = calculate_layers(state_dict_keys, key_prefix="single_layers") | ||
|
||
# MMDiT blocks 🎸. | ||
for i in range(mmdit_layers): | ||
# feed-forward | ||
path_mapping = {"mlpX": "ff", "mlpC": "ff_context"} | ||
weight_mapping = {"c_fc1": "linear_1", "c_fc2": "linear_2", "c_proj": "out_projection"} | ||
for orig_k, diffuser_k in path_mapping.items(): | ||
for k, v in weight_mapping.items(): | ||
converted_state_dict[f"joint_transformer_blocks.{i}.{diffuser_k}.{v}.weight"] = state_dict.pop( | ||
f"model.double_layers.{i}.{orig_k}.{k}.weight" | ||
) | ||
|
||
# norms | ||
path_mapping = {"modX": "norm1", "modC": "norm1_context"} | ||
for orig_k, diffuser_k in path_mapping.items(): | ||
converted_state_dict[f"joint_transformer_blocks.{i}.{diffuser_k}.linear.weight"] = state_dict.pop( | ||
f"model.double_layers.{i}.{orig_k}.1.weight" | ||
) | ||
|
||
# attns | ||
x_attn_mapping = {"w2q": "to_q", "w2k": "to_k", "w2v": "to_v", "w2o": "to_out.0"} | ||
context_attn_mapping = {"w1q": "add_q_proj", "w1k": "add_k_proj", "w1v": "add_v_proj", "w1o": "to_add_out"} | ||
for attn_mapping in [x_attn_mapping, context_attn_mapping]: | ||
for k, v in attn_mapping.items(): | ||
converted_state_dict[f"joint_transformer_blocks.{i}.attn.{v}.weight"] = state_dict.pop( | ||
f"model.double_layers.{i}.attn.{k}.weight" | ||
) | ||
|
||
# Single-DiT blocks. | ||
for i in range(single_dit_layers): | ||
# feed-forward | ||
mapping = {"c_fc1": "linear_1", "c_fc2": "linear_2", "c_proj": "out_projection"} | ||
for k, v in mapping.items(): | ||
converted_state_dict[f"single_transformer_blocks.{i}.ff.{v}.weight"] = state_dict.pop( | ||
f"model.single_layers.{i}.mlp.{k}.weight" | ||
) | ||
|
||
# norms | ||
converted_state_dict[f"single_transformer_blocks.{i}.norm1.linear.weight"] = state_dict.pop( | ||
f"model.single_layers.{i}.modCX.1.weight" | ||
) | ||
|
||
# attns | ||
x_attn_mapping = {"w1q": "to_q", "w1k": "to_k", "w1v": "to_v", "w1o": "to_out.0"} | ||
for k, v in x_attn_mapping.items(): | ||
converted_state_dict[f"single_transformer_blocks.{i}.attn.{v}.weight"] = state_dict.pop( | ||
f"model.single_layers.{i}.attn.{k}.weight" | ||
) | ||
|
||
# Final blocks. | ||
converted_state_dict["proj_out.weight"] = state_dict.pop("model.final_linear.weight") | ||
converted_state_dict["norm_out.linear.weight"] = swap_scale_shift(state_dict.pop("model.modF.1.weight"), dim=None) | ||
|
||
return converted_state_dict | ||
|
||
|
||
@torch.no_grad() | ||
def populate_state_dict(args): | ||
original_state_dict = load_original_state_dict(args) | ||
state_dict_keys = list(original_state_dict.keys()) | ||
mmdit_layers = calculate_layers(state_dict_keys, key_prefix="double_layers") | ||
single_dit_layers = calculate_layers(state_dict_keys, key_prefix="single_layers") | ||
|
||
converted_state_dict = convert_transformer(original_state_dict) | ||
model_diffusers = AuraFlowTransformer2DModel( | ||
num_mmdit_layers=mmdit_layers, num_single_dit_layers=single_dit_layers | ||
) | ||
model_diffusers.load_state_dict(converted_state_dict, strict=True) | ||
|
||
return model_diffusers | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--original_state_dict_repo_id", default="AuraDiffusion/auradiffusion-v0.1a0", type=str) | ||
parser.add_argument("--dump_path", default="aura-flow", type=str) | ||
parser.add_argument("--hub_id", default=None, type=str) | ||
args = parser.parse_args() | ||
|
||
model_diffusers = populate_state_dict(args) | ||
model_diffusers.save_pretrained(args.dump_path) | ||
if args.hub_id is not None: | ||
model_diffusers.push_to_hub(args.hub_id) |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.