Spaces:
Runtime error
Runtime error
Commit
·
c82ab6a
0
Parent(s):
init
Browse files- README.md +13 -0
- app.py +160 -0
- assets/ref_man1.jpg +0 -0
- assets/ref_man2.jpg +0 -0
- assets/ref_man3.jpg +0 -0
- assets/ref_woman1.jpg +0 -0
- assets/ref_woman2.jpg +0 -0
- assets/ref_woman3.jpg +0 -0
- config.json +15 -0
- projection.py +161 -0
- requirements.txt +10 -0
- transformer_flux_custom.py +942 -0
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Flux Consistancy V2
|
| 3 |
+
emoji: 👁
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.23.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
short_description: Generate images with texts and reference images
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import spaces
|
| 5 |
+
import safetensors
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from loguru import logger
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
from huggingface_hub import hf_hub_download, login
|
| 11 |
+
from diffusers import FluxPipeline, FluxTransformer2DModel
|
| 12 |
+
|
| 13 |
+
from projection import ImageEncoder
|
| 14 |
+
from transformer_flux_custom import FluxTransformer2DModel as FluxTransformer2DModelWithIP
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
model_config = './config.json'
|
| 18 |
+
pretrained_model_name = 'black-forest-labs/FLUX.1-dev'
|
| 19 |
+
adapter_path = 'model.safetensors'
|
| 20 |
+
adapter_repo_id = "ashen0209/Flux-Consistancy-v2"
|
| 21 |
+
|
| 22 |
+
conditioner_base_model = 'eva02_large_patch14_448.mim_in22k_ft_in1k'
|
| 23 |
+
conditioner_layer_num = 12
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
output_dim = 4096
|
| 26 |
+
logger.info(f"pretrained_model_name: {pretrained_model_name}, adapter_repo_id: {adapter_repo_id}, adapter_path: {adapter_path}, conditioner_layer: {conditioner_layer_num}, output_dim {output_dim}, device: {device}")
|
| 27 |
+
|
| 28 |
+
logger.info("init model")
|
| 29 |
+
model = FluxTransformer2DModelWithIP.from_config(model_config, torch_dtype=torch.bfloat16) # type: ignore
|
| 30 |
+
logger.info("load model")
|
| 31 |
+
copy = FluxTransformer2DModel.from_pretrained(pretrained_model_name, subfolder='transformer', torch_dtype=torch.bfloat16)
|
| 32 |
+
model.load_state_dict(copy.state_dict(), strict=False)
|
| 33 |
+
del copy
|
| 34 |
+
|
| 35 |
+
logger.info("load proj")
|
| 36 |
+
extra_embedder = ImageEncoder(output_dim, layer_num=conditioner_layer_num, seq_len=2, device=device, base_model=conditioner_base_model, use_pyramid=True).to(device=device, dtype=torch.bfloat16)
|
| 37 |
+
|
| 38 |
+
logger.info("load pipe")
|
| 39 |
+
pipe = FluxPipeline.from_pretrained(pretrained_model_name, transformer=model, torch_dtype=torch.bfloat16)
|
| 40 |
+
pipe.to(dtype=torch.bfloat16, device=device)
|
| 41 |
+
|
| 42 |
+
logger.info("download adapter")
|
| 43 |
+
login(token=os.environ['HF_TOKEN'])
|
| 44 |
+
file_path = hf_hub_download(repo_id=adapter_repo_id, filename=adapter_path)
|
| 45 |
+
|
| 46 |
+
logger.info("load adapter")
|
| 47 |
+
state_dict = safetensors.torch.load_file(file_path)
|
| 48 |
+
state_dict = {'.'.join(k.split('.')[1:]): state_dict[k] for k in state_dict.keys()}
|
| 49 |
+
diff = model.load_state_dict(state_dict, strict=False)
|
| 50 |
+
diff = extra_embedder.load_state_dict(state_dict, strict=False)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
IMAGE_PROCESS_TRANSFORM = transforms.Compose([
|
| 54 |
+
transforms.Resize((448, 448)),
|
| 55 |
+
transforms.ToTensor(),
|
| 56 |
+
transforms.Normalize(mean=[0.4815, 0.4578, 0.4082], std=[0.2686, 0.2613, 0.276])
|
| 57 |
+
])
|
| 58 |
+
|
| 59 |
+
@spaces.GPU
|
| 60 |
+
def generate_image(ref_image, ref_image2=None, prompt="", height=512, width=512, num_steps=25, guidance_scale=3.5, seed=0, ip_scale=1.0):
|
| 61 |
+
print(f"ref_image: {ref_image.size if ref_image is not None else None}, "
|
| 62 |
+
f"ref_image2: {ref_image2.size if ref_image2 is not None else None}, "
|
| 63 |
+
f"prompt: {prompt}, height: {height}, width: {width}, num_steps: {num_steps}, guidance_scale: {guidance_scale}, ip_scale: {ip_scale}")
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
image_refs = map(torch.stack, [
|
| 66 |
+
[IMAGE_PROCESS_TRANSFORM(i) for i in [ref_image, ref_image2] if i is not None]
|
| 67 |
+
])
|
| 68 |
+
image_refs = [i.to(dtype=torch.bfloat16, device='cuda') for i in image_refs]
|
| 69 |
+
prompt_embeds, pooled_prompt_embeds, txt_ids = pipe.encode_prompt(prompt, prompt)
|
| 70 |
+
visual_prompt_embeds = extra_embedder(image_refs)
|
| 71 |
+
prompt_embeds_with_ref = torch.cat([prompt_embeds, visual_prompt_embeds], dim=1)
|
| 72 |
+
pipe.transformer.ip_scale = ip_scale
|
| 73 |
+
image = pipe(
|
| 74 |
+
prompt_embeds=prompt_embeds_with_ref,
|
| 75 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 76 |
+
# negative_prompt_embeds=negative_prompt_embeds,
|
| 77 |
+
# negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
| 78 |
+
height=height,
|
| 79 |
+
width=width,
|
| 80 |
+
num_inference_steps=num_steps,
|
| 81 |
+
guidance_scale=guidance_scale,
|
| 82 |
+
).images[0]
|
| 83 |
+
return image
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
examples = [
|
| 88 |
+
["assets/ref_woman1.jpg", None, "A photo of the woman dancing in the desert, blue sky, cinematic studio photography of high-fidelity subject, natural lightning, insanely detailed and intricate.", 512, 768],
|
| 89 |
+
["assets/ref_man1.jpg", "assets/ref_woman1.jpg", "The man and woman are standing in a sunlit meadow. The man is taking photos of the woman as she poses with the bouquet", 512, 768],
|
| 90 |
+
["assets/ref_man2.jpg", "assets/ref_woman2.jpg", "The man and woman are standing next to a motorcycle on a deserted road. The woman is pointing at the map, while the man looks confused but intrigued.", 512, 768],
|
| 91 |
+
["assets/ref_man3.jpg", "assets/ref_woman3.jpg", "The man and woman are at a glamorous ballroom dance. The man is offering the woman a glass of champagne, while she fans herself gracefully.", 512, 768],
|
| 92 |
+
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
with gr.Blocks() as demo:
|
| 96 |
+
# Top-level inputs that are always visible
|
| 97 |
+
with gr.Row():
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
## Character Consistancy Image Generation based on Flux
|
| 100 |
+
- The model can be downloaded at https://huggingface.co/ashen0209/Flux-Consistancy-v2
|
| 101 |
+
- The model is good at generating consistent images of human characters, capable of multi-subjects generation especisally on realistic scenes
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column():
|
| 106 |
+
with gr.Row():
|
| 107 |
+
ref_image = gr.Image(type="pil", label="Upload Reference Subject Image", width=300)
|
| 108 |
+
ref_image2 = gr.Image(type="pil", label="[Optional] complement image or additional image from different category", width=200)
|
| 109 |
+
description = gr.Textbox(lines=2, placeholder="Describe the desired contents", label="Description Text")
|
| 110 |
+
generate_btn = gr.Button("Generate Image")
|
| 111 |
+
|
| 112 |
+
# Advanced options hidden inside an accordion (click to expand)
|
| 113 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 114 |
+
height_slider = gr.Slider(minimum=256, maximum=1024, value=512, step=64, label="Height")
|
| 115 |
+
width_slider = gr.Slider(minimum=256, maximum=1024, value=512, step=64, label="Width")
|
| 116 |
+
steps_slider = gr.Slider(minimum=20, maximum=50, value=25, step=1, label="Number of Steps")
|
| 117 |
+
guidance_slider = gr.Slider(minimum=1.0, maximum=8.0, value=3.5, step=0.1, label="Guidance Scale")
|
| 118 |
+
ref_scale_slider = gr.Slider(minimum=0.0, maximum=2.0, value=1.0, step=0.1, label="Reference Image Scale")
|
| 119 |
+
|
| 120 |
+
with gr.Column():
|
| 121 |
+
output = gr.Image(type="pil", label="Generated Image", )
|
| 122 |
+
# with gr.Row():
|
| 123 |
+
# with gr.Group():
|
| 124 |
+
# with gr.Row(equal_height=True):
|
| 125 |
+
# with gr.Column(scale=1, min_width=50, ):
|
| 126 |
+
# randomize_checkbox = gr.Checkbox(label="Randomize Seed", value=True)
|
| 127 |
+
# with gr.Column(scale=3, min_width=100):
|
| 128 |
+
# seed_io = gr.Number(label="Seed (if not randomizing)", value=0, interactive=True, )
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
gr.Examples(
|
| 132 |
+
label='Click on following examples to load and try',
|
| 133 |
+
examples=examples,
|
| 134 |
+
inputs=[ref_image, ref_image2, description, height_slider, width_slider],
|
| 135 |
+
fn=generate_image,
|
| 136 |
+
outputs=output,
|
| 137 |
+
# example_labels=['Reference Subject', 'Additional Reference', 'Prompt', 'Height', 'Width'],
|
| 138 |
+
cache_examples=True,
|
| 139 |
+
cache_mode='lazy'
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Row():
|
| 143 |
+
gr.Markdown("""
|
| 144 |
+
### Tips:
|
| 145 |
+
- Images with human subjects tend to perform better than other categories.
|
| 146 |
+
- Images where the subject occupies most of the frame with a clean, uncluttered background yield improved results.
|
| 147 |
+
- Including multiple subjects of the same category may cause blending issues.
|
| 148 |
+
""")
|
| 149 |
+
# When the button is clicked, pass all inputs to generate_image
|
| 150 |
+
generate_btn.click(
|
| 151 |
+
fn=generate_image,
|
| 152 |
+
inputs=[ref_image, ref_image2, description, height_slider, width_slider, steps_slider, guidance_slider, ref_scale_slider],
|
| 153 |
+
outputs=output,
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
if __name__ == "__main__":
|
| 159 |
+
demo.launch()
|
| 160 |
+
|
assets/ref_man1.jpg
ADDED
|
assets/ref_man2.jpg
ADDED
|
assets/ref_man3.jpg
ADDED
|
assets/ref_woman1.jpg
ADDED
|
assets/ref_woman2.jpg
ADDED
|
assets/ref_woman3.jpg
ADDED
|
config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "FluxTransformer2DModel",
|
| 3 |
+
"_diffusers_version": "0.30.0.dev0",
|
| 4 |
+
"_name_or_path": "../checkpoints/flux-dev/transformer",
|
| 5 |
+
"attention_head_dim": 128,
|
| 6 |
+
"guidance_embeds": true,
|
| 7 |
+
"in_channels": 64,
|
| 8 |
+
"joint_attention_dim": 4096,
|
| 9 |
+
"num_attention_heads": 24,
|
| 10 |
+
"num_layers": 19,
|
| 11 |
+
"num_single_layers": 38,
|
| 12 |
+
"patch_size": 1,
|
| 13 |
+
"pooled_projection_dim": 768
|
| 14 |
+
}
|
| 15 |
+
|
projection.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
from loguru import logger
|
| 5 |
+
# from prodigyopt import Prodigy
|
| 6 |
+
from torch.utils.checkpoint import checkpoint
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
# from sbp.nn.model_paths import MODEL_PATHS
|
| 9 |
+
# # from sbp.nn.torch.models.qformer import ModifiedQFormer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ImageEncoder(nn.Module):
|
| 13 |
+
|
| 14 |
+
def __init__(self, output_dim, base_model='eva02_base_patch14_224.mim_in22k', layer_num=6, seq_len=3, device='cpu', use_pe=False, use_pyramid=False, use_global_feature=False, use_qformer_dim=False):
|
| 15 |
+
super().__init__()
|
| 16 |
+
self.output_dim = output_dim
|
| 17 |
+
import timm
|
| 18 |
+
# paths = {
|
| 19 |
+
# 'eva02_large_patch14_448.mim_in22k_ft_in1k': MODEL_PATHS.EVA02_LARGE_448_MIM_IN22K,
|
| 20 |
+
# 'eva02_base_patch14_224.mim_in22k': MODEL_PATHS.EVA02_BASE_224_MIM_IN22K,
|
| 21 |
+
# }
|
| 22 |
+
if base_model == 'eva02_base_patch14_224.mim_in22k':
|
| 23 |
+
self.img_seq = 257
|
| 24 |
+
elif base_model == 'eva02_large_patch14_448.mim_in22k_ft_in1k':
|
| 25 |
+
self.img_seq = 1025
|
| 26 |
+
elif base_model == 'siglip2':
|
| 27 |
+
self.img_seq = 1024
|
| 28 |
+
else:
|
| 29 |
+
raise ValueError(f" unknown {base_model}, supported: {list(paths.keys())}")
|
| 30 |
+
# self.base_model = timm.create_model(base_model, pretrained=True, pretrained_cfg_overlay={'file': paths[base_model], 'custom_load': False})
|
| 31 |
+
self.base_model = timm.create_model(base_model, pretrained=False)
|
| 32 |
+
del self.base_model.norm, self.base_model.fc_norm, self.base_model.head, self.base_model.head_drop
|
| 33 |
+
del self.base_model.blocks[layer_num:]
|
| 34 |
+
dim_mult = 3 if use_pyramid else 1
|
| 35 |
+
image_output_dim = self.base_model.num_features * dim_mult
|
| 36 |
+
self.seq_len = seq_len
|
| 37 |
+
self.device = device
|
| 38 |
+
self.use_pe = use_pe
|
| 39 |
+
self.use_pyramid = use_pyramid
|
| 40 |
+
self.use_global_feature = use_global_feature
|
| 41 |
+
self.use_qformer = use_qformer_dim > 0
|
| 42 |
+
if self.use_pe:
|
| 43 |
+
self.pe = torch.zeros([1, self.seq_len * self.img_seq, self.output_dim], device=self.device, dtype=torch.bfloat16)
|
| 44 |
+
for i in range(self.seq_len):
|
| 45 |
+
self.pe[:, i * self.img_seq: (i + 1) * self.img_seq, i::self.seq_len] = 0.05
|
| 46 |
+
if self.use_qformer:
|
| 47 |
+
logger.info("image projection use qformer ...")
|
| 48 |
+
self.qformer = ModifiedQFormer(
|
| 49 |
+
input_dim=image_output_dim,
|
| 50 |
+
hidden_dim=use_qformer_dim,
|
| 51 |
+
num_heads=12,
|
| 52 |
+
num_layers=6,
|
| 53 |
+
output_dim=output_dim,
|
| 54 |
+
num_queries=512,
|
| 55 |
+
use_self_attention=False
|
| 56 |
+
).cuda()
|
| 57 |
+
else:
|
| 58 |
+
self.project = nn.Linear(image_output_dim, output_dim)
|
| 59 |
+
self.final_norm = nn.LayerNorm(output_dim)
|
| 60 |
+
|
| 61 |
+
def apply_feature_pyramid(self, original_tokens, original_grid_size=32, downsample = [1, 4, 32]):
|
| 62 |
+
B, seq_len, D = original_tokens[0].shape
|
| 63 |
+
H = W = original_grid_size
|
| 64 |
+
|
| 65 |
+
token_lst = []
|
| 66 |
+
for i, tokens in enumerate(original_tokens):
|
| 67 |
+
downsample_size = downsample[i]
|
| 68 |
+
if downsample_size == 0:
|
| 69 |
+
pass
|
| 70 |
+
elif downsample_size == 1:
|
| 71 |
+
token_lst.append(tokens)
|
| 72 |
+
else:
|
| 73 |
+
head, tokens = torch.split(tokens, [1, 1024], dim=1)
|
| 74 |
+
tokens_2d = tokens.view(B, H, W, D).permute(0, 3, 1, 2) # Reshape tokens to 2D grid (B, D, H, W)
|
| 75 |
+
pooled = F.avg_pool2d(tokens_2d, kernel_size=downsample_size, stride=downsample_size) # (B, D, 32//ds, 32//ds)
|
| 76 |
+
up = F.interpolate(pooled, size=(H, W), mode='nearest') # (B, D, 32, 32)
|
| 77 |
+
up = up.permute(0, 2, 3, 1).reshape(B, seq_len - 1, D)
|
| 78 |
+
up = torch.cat([head, up], dim=1)
|
| 79 |
+
token_lst.append(up / downsample_size ** 0.5)
|
| 80 |
+
combined_tokens = torch.cat(token_lst, dim=2)
|
| 81 |
+
return combined_tokens
|
| 82 |
+
|
| 83 |
+
def apply_global_feature(self, original_tokens, original_grid_size=32, pool_size=4):
|
| 84 |
+
B, seq_len, D = original_tokens.shape
|
| 85 |
+
H = W = original_grid_size
|
| 86 |
+
|
| 87 |
+
tokens_2d = original_tokens.view(B, H, W, D).permute(0, 3, 1, 2)
|
| 88 |
+
pooled = F.avg_pool2d(tokens_2d, kernel_size=pool_size, stride=pool_size) # (B, D, 8, 8)
|
| 89 |
+
pooled = pooled.permute((0, 2, 3, 1)).reshape((B, seq_len // pool_size // pool_size, D))
|
| 90 |
+
return pooled
|
| 91 |
+
|
| 92 |
+
def forward(self, image_list):
|
| 93 |
+
splits = [len(lst) for lst in image_list]
|
| 94 |
+
if sum(splits) == 0:
|
| 95 |
+
return torch.zeros([len(splits), self.seq_len * self.img_seq, self.output_dim], device=self.device, dtype=torch.bfloat16)
|
| 96 |
+
x = torch.concat(image_list, dim=0).to(device=self.device, dtype=torch.bfloat16)
|
| 97 |
+
x = self.base_model.patch_embed(x)
|
| 98 |
+
x, rot_pos_embed = self.base_model._pos_embed(x)
|
| 99 |
+
intermediates = []
|
| 100 |
+
for i, blk in enumerate(self.base_model.blocks):
|
| 101 |
+
x = blk(x, rope=rot_pos_embed)
|
| 102 |
+
if i in [11]:
|
| 103 |
+
intermediates.append(x)
|
| 104 |
+
intermediates.append(x)
|
| 105 |
+
if self.use_pyramid:
|
| 106 |
+
x = self.apply_feature_pyramid(intermediates + [x])
|
| 107 |
+
elif self.use_global_feature:
|
| 108 |
+
x = self.apply_global_feature(x)
|
| 109 |
+
|
| 110 |
+
if self.use_qformer:
|
| 111 |
+
x = self.qformer(x)
|
| 112 |
+
else:
|
| 113 |
+
x = self.project(x)
|
| 114 |
+
x = self.final_norm(x)
|
| 115 |
+
|
| 116 |
+
b, seq_len, c= x.shape
|
| 117 |
+
split_patches = torch.split(x, splits, dim=0)
|
| 118 |
+
split_patches = [nn.functional.pad(sample, (0, 0, 0, 0, 0, self.seq_len - len(sample))) for sample in split_patches]
|
| 119 |
+
x = torch.stack(split_patches, dim=0)
|
| 120 |
+
x = x.reshape((len(splits), self.seq_len * seq_len, c))
|
| 121 |
+
if self.use_pe:
|
| 122 |
+
x = x + self.pe
|
| 123 |
+
return x
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class ImageEncoderWithSiglip(nn.Module):
|
| 127 |
+
|
| 128 |
+
def __init__(self, output_dim, base_model="siglip2-so400m-patch16-512", layer_num=6, seq_len=3, device='cpu', use_pe=False):
|
| 129 |
+
super().__init__()
|
| 130 |
+
self.output_dim = output_dim
|
| 131 |
+
ckpt = {
|
| 132 |
+
'siglip-so400m-patch14-384': MODEL_PATHS.SIGLIP_SO400M_384,
|
| 133 |
+
'siglip2-so400m-patch16-512': MODEL_PATHS.SIGLIP2_SO400M_512
|
| 134 |
+
}[base_model]
|
| 135 |
+
image_classifier = pipeline(model=ckpt, task="zero-shot-image-classification", device='cpu')
|
| 136 |
+
logger.info(f"using {layer_num} / {len(image_classifier.model.vision_model.encoder.layers)} layers of {base_model} ... ")
|
| 137 |
+
del image_classifier.model.vision_model.encoder.layers[layer_num:]
|
| 138 |
+
num_features = image_classifier.model.vision_model.post_layernorm.normalized_shape[0]
|
| 139 |
+
self.base_model = image_classifier.model.vision_model
|
| 140 |
+
self.project = nn.Linear(num_features, output_dim)
|
| 141 |
+
self.final_norm = nn.LayerNorm(output_dim)
|
| 142 |
+
self.seq_len = seq_len
|
| 143 |
+
self.device = device
|
| 144 |
+
self.use_pe = use_pe
|
| 145 |
+
|
| 146 |
+
def forward(self, image_list):
|
| 147 |
+
splits = [len(lst) for lst in image_list]
|
| 148 |
+
if sum(splits) == 0:
|
| 149 |
+
return torch.zeros([len(splits), self.seq_len * self.img_seq, self.output_dim], device=self.device, dtype=torch.bfloat16)
|
| 150 |
+
x = torch.concat(image_list, dim=0).to(device=self.device, dtype=torch.bfloat16)
|
| 151 |
+
x = self.base_model(x).last_hidden_state
|
| 152 |
+
x = self.project(x)
|
| 153 |
+
x = self.final_norm(x)
|
| 154 |
+
b, seq_len, c= x.shape
|
| 155 |
+
split_patches = torch.split(x, splits, dim=0)
|
| 156 |
+
split_patches = [nn.functional.pad(sample, (0, 0, 0, 0, 0, self.seq_len - len(sample))) for sample in split_patches]
|
| 157 |
+
x = torch.stack(split_patches, dim=0)
|
| 158 |
+
x = x.reshape((len(splits), self.seq_len * seq_len, c))
|
| 159 |
+
if self.use_pe:
|
| 160 |
+
x = x + self.pe
|
| 161 |
+
return x
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
diffusers
|
| 3 |
+
invisible_watermark
|
| 4 |
+
torch
|
| 5 |
+
transformers
|
| 6 |
+
torchvision
|
| 7 |
+
timm
|
| 8 |
+
loguru
|
| 9 |
+
einops
|
| 10 |
+
sentencepiece
|
transformer_flux_custom.py
ADDED
|
@@ -0,0 +1,942 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Black Forest Labs, The HuggingFace Team and The InstantX Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
from typing import Any, Dict, Optional, Tuple, Union
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn as nn
|
| 21 |
+
import torch.nn.functional as F
|
| 22 |
+
from einops import rearrange
|
| 23 |
+
|
| 24 |
+
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 25 |
+
from diffusers.loaders import FromOriginalModelMixin, PeftAdapterMixin
|
| 26 |
+
from diffusers.models.attention import FeedForward
|
| 27 |
+
from diffusers.models.attention_processor import (
|
| 28 |
+
Attention,
|
| 29 |
+
AttentionProcessor,
|
| 30 |
+
FluxAttnProcessor2_0,
|
| 31 |
+
FluxAttnProcessor2_0_NPU,
|
| 32 |
+
FusedFluxAttnProcessor2_0,
|
| 33 |
+
)
|
| 34 |
+
from diffusers.models.modeling_utils import ModelMixin
|
| 35 |
+
from diffusers.models.normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle
|
| 36 |
+
from diffusers.utils import USE_PEFT_BACKEND, is_torch_version, logging, scale_lora_layers, unscale_lora_layers
|
| 37 |
+
from diffusers.utils.import_utils import is_torch_npu_available
|
| 38 |
+
from diffusers.utils.torch_utils import maybe_allow_in_graph
|
| 39 |
+
from diffusers.models.embeddings import CombinedTimestepGuidanceTextProjEmbeddings, CombinedTimestepTextProjEmbeddings, FluxPosEmbed
|
| 40 |
+
from diffusers.models.modeling_outputs import Transformer2DModelOutput
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class FluxIPAttnProcessor2_0:
|
| 47 |
+
"""Attention processor used typically in processing the SD3-like self-attention projections."""
|
| 48 |
+
|
| 49 |
+
def __init__(self):
|
| 50 |
+
if not hasattr(F, "scaled_dot_product_attention"):
|
| 51 |
+
raise ImportError("FluxAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
|
| 52 |
+
|
| 53 |
+
def __call__(
|
| 54 |
+
self,
|
| 55 |
+
attn: Attention,
|
| 56 |
+
hidden_states: torch.FloatTensor,
|
| 57 |
+
encoder_hidden_states: torch.FloatTensor = None,
|
| 58 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 59 |
+
image_rotary_emb: Optional[torch.Tensor] = None,
|
| 60 |
+
) -> torch.FloatTensor:
|
| 61 |
+
batch_size, _, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
|
| 62 |
+
|
| 63 |
+
# `sample` projections.
|
| 64 |
+
query = attn.to_q(hidden_states)
|
| 65 |
+
key = attn.to_k(hidden_states)
|
| 66 |
+
value = attn.to_v(hidden_states)
|
| 67 |
+
|
| 68 |
+
inner_dim = key.shape[-1]
|
| 69 |
+
head_dim = inner_dim // attn.heads
|
| 70 |
+
|
| 71 |
+
query = img_q = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 72 |
+
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 73 |
+
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 74 |
+
|
| 75 |
+
if attn.norm_q is not None:
|
| 76 |
+
query = attn.norm_q(query)
|
| 77 |
+
if attn.norm_k is not None:
|
| 78 |
+
key = attn.norm_k(key)
|
| 79 |
+
|
| 80 |
+
# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states`
|
| 81 |
+
if encoder_hidden_states is not None:
|
| 82 |
+
# `context` projections.
|
| 83 |
+
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
|
| 84 |
+
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
|
| 85 |
+
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
|
| 86 |
+
|
| 87 |
+
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
|
| 88 |
+
batch_size, -1, attn.heads, head_dim
|
| 89 |
+
).transpose(1, 2)
|
| 90 |
+
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
|
| 91 |
+
batch_size, -1, attn.heads, head_dim
|
| 92 |
+
).transpose(1, 2)
|
| 93 |
+
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view(
|
| 94 |
+
batch_size, -1, attn.heads, head_dim
|
| 95 |
+
).transpose(1, 2)
|
| 96 |
+
|
| 97 |
+
if attn.norm_added_q is not None:
|
| 98 |
+
encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
|
| 99 |
+
if attn.norm_added_k is not None:
|
| 100 |
+
encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)
|
| 101 |
+
|
| 102 |
+
# attention
|
| 103 |
+
query = torch.cat([encoder_hidden_states_query_proj, query], dim=2)
|
| 104 |
+
key = torch.cat([encoder_hidden_states_key_proj, key], dim=2)
|
| 105 |
+
value = torch.cat([encoder_hidden_states_value_proj, value], dim=2)
|
| 106 |
+
|
| 107 |
+
if image_rotary_emb is not None:
|
| 108 |
+
from diffusers.models.embeddings import apply_rotary_emb
|
| 109 |
+
query = apply_rotary_emb(query, image_rotary_emb)
|
| 110 |
+
key = apply_rotary_emb(key, image_rotary_emb)
|
| 111 |
+
|
| 112 |
+
hidden_states = F.scaled_dot_product_attention(
|
| 113 |
+
query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
|
| 114 |
+
)
|
| 115 |
+
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
|
| 116 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 117 |
+
|
| 118 |
+
if encoder_hidden_states is not None:
|
| 119 |
+
encoder_hidden_states, hidden_states = (
|
| 120 |
+
hidden_states[:, : encoder_hidden_states.shape[1]],
|
| 121 |
+
hidden_states[:, encoder_hidden_states.shape[1] :],
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# linear proj
|
| 125 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 126 |
+
# dropout
|
| 127 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 128 |
+
|
| 129 |
+
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
|
| 130 |
+
|
| 131 |
+
return hidden_states, encoder_hidden_states, img_q
|
| 132 |
+
else:
|
| 133 |
+
return hidden_states, img_q
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
@maybe_allow_in_graph
|
| 137 |
+
class FluxSingleTransformerBlock(nn.Module):
|
| 138 |
+
r"""
|
| 139 |
+
A Transformer block following the MMDiT architecture, introduced in Stable Diffusion 3.
|
| 140 |
+
|
| 141 |
+
Reference: https://arxiv.org/abs/2403.03206
|
| 142 |
+
|
| 143 |
+
Parameters:
|
| 144 |
+
dim (`int`): The number of channels in the input and output.
|
| 145 |
+
num_attention_heads (`int`): The number of heads to use for multi-head attention.
|
| 146 |
+
attention_head_dim (`int`): The number of channels in each head.
|
| 147 |
+
context_pre_only (`bool`): Boolean to determine if we should add some blocks associated with the
|
| 148 |
+
processing of `context` conditions.
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
def __init__(self, dim, num_attention_heads, attention_head_dim, mlp_ratio=4.0):
|
| 152 |
+
super().__init__()
|
| 153 |
+
self.mlp_hidden_dim = int(dim * mlp_ratio)
|
| 154 |
+
|
| 155 |
+
self.norm = AdaLayerNormZeroSingle(dim)
|
| 156 |
+
self.proj_mlp = nn.Linear(dim, self.mlp_hidden_dim)
|
| 157 |
+
self.act_mlp = nn.GELU(approximate="tanh")
|
| 158 |
+
self.proj_out = nn.Linear(dim + self.mlp_hidden_dim, dim)
|
| 159 |
+
|
| 160 |
+
if is_torch_npu_available():
|
| 161 |
+
processor = FluxAttnProcessor2_0_NPU()
|
| 162 |
+
else:
|
| 163 |
+
processor = FluxAttnProcessor2_0()
|
| 164 |
+
self.attn = Attention(
|
| 165 |
+
query_dim=dim,
|
| 166 |
+
cross_attention_dim=None,
|
| 167 |
+
dim_head=attention_head_dim,
|
| 168 |
+
heads=num_attention_heads,
|
| 169 |
+
out_dim=dim,
|
| 170 |
+
bias=True,
|
| 171 |
+
processor=processor,
|
| 172 |
+
qk_norm="rms_norm",
|
| 173 |
+
eps=1e-6,
|
| 174 |
+
pre_only=True,
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
def forward(
|
| 178 |
+
self,
|
| 179 |
+
hidden_states: torch.FloatTensor,
|
| 180 |
+
temb: torch.FloatTensor,
|
| 181 |
+
image_rotary_emb=None,
|
| 182 |
+
joint_attention_kwargs=None,
|
| 183 |
+
):
|
| 184 |
+
residual = hidden_states
|
| 185 |
+
norm_hidden_states, gate = self.norm(hidden_states, emb=temb)
|
| 186 |
+
mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states))
|
| 187 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 188 |
+
attn_output = self.attn(
|
| 189 |
+
hidden_states=norm_hidden_states,
|
| 190 |
+
image_rotary_emb=image_rotary_emb,
|
| 191 |
+
**joint_attention_kwargs,
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2)
|
| 195 |
+
gate = gate.unsqueeze(1)
|
| 196 |
+
hidden_states = gate * self.proj_out(hidden_states)
|
| 197 |
+
hidden_states = residual + hidden_states
|
| 198 |
+
if hidden_states.dtype == torch.float16:
|
| 199 |
+
hidden_states = hidden_states.clip(-65504, 65504)
|
| 200 |
+
|
| 201 |
+
return hidden_states
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
@maybe_allow_in_graph
|
| 205 |
+
class FluxTransformerBlock(nn.Module):
|
| 206 |
+
r"""
|
| 207 |
+
A Transformer block following the MMDiT architecture, introduced in Stable Diffusion 3.
|
| 208 |
+
|
| 209 |
+
Reference: https://arxiv.org/abs/2403.03206
|
| 210 |
+
|
| 211 |
+
Parameters:
|
| 212 |
+
dim (`int`): The number of channels in the input and output.
|
| 213 |
+
num_attention_heads (`int`): The number of heads to use for multi-head attention.
|
| 214 |
+
attention_head_dim (`int`): The number of channels in each head.
|
| 215 |
+
context_pre_only (`bool`): Boolean to determine if we should add some blocks associated with the
|
| 216 |
+
processing of `context` conditions.
|
| 217 |
+
"""
|
| 218 |
+
|
| 219 |
+
def __init__(self, dim, num_attention_heads, attention_head_dim, qk_norm="rms_norm", eps=1e-6):
|
| 220 |
+
super().__init__()
|
| 221 |
+
|
| 222 |
+
self.norm1 = AdaLayerNormZero(dim)
|
| 223 |
+
|
| 224 |
+
self.norm1_context = AdaLayerNormZero(dim)
|
| 225 |
+
|
| 226 |
+
if hasattr(F, "scaled_dot_product_attention"):
|
| 227 |
+
processor = FluxAttnProcessor2_0()
|
| 228 |
+
else:
|
| 229 |
+
raise ValueError(
|
| 230 |
+
"The current PyTorch version does not support the `scaled_dot_product_attention` function."
|
| 231 |
+
)
|
| 232 |
+
self.attn = Attention(
|
| 233 |
+
query_dim=dim,
|
| 234 |
+
cross_attention_dim=None,
|
| 235 |
+
added_kv_proj_dim=dim,
|
| 236 |
+
dim_head=attention_head_dim,
|
| 237 |
+
heads=num_attention_heads,
|
| 238 |
+
out_dim=dim,
|
| 239 |
+
context_pre_only=False,
|
| 240 |
+
bias=True,
|
| 241 |
+
processor=processor,
|
| 242 |
+
qk_norm=qk_norm,
|
| 243 |
+
eps=eps,
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
self.norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 247 |
+
self.ff = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 248 |
+
|
| 249 |
+
self.norm2_context = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 250 |
+
self.ff_context = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 251 |
+
|
| 252 |
+
# let chunk size default to None
|
| 253 |
+
self._chunk_size = None
|
| 254 |
+
self._chunk_dim = 0
|
| 255 |
+
|
| 256 |
+
def forward(
|
| 257 |
+
self,
|
| 258 |
+
hidden_states: torch.FloatTensor,
|
| 259 |
+
encoder_hidden_states: torch.FloatTensor,
|
| 260 |
+
temb: torch.FloatTensor,
|
| 261 |
+
image_rotary_emb=None,
|
| 262 |
+
joint_attention_kwargs=None,
|
| 263 |
+
):
|
| 264 |
+
norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(hidden_states, emb=temb)
|
| 265 |
+
|
| 266 |
+
norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_context(
|
| 267 |
+
encoder_hidden_states, emb=temb
|
| 268 |
+
)
|
| 269 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 270 |
+
# Attention.
|
| 271 |
+
attn_output, context_attn_output = self.attn(
|
| 272 |
+
hidden_states=norm_hidden_states,
|
| 273 |
+
encoder_hidden_states=norm_encoder_hidden_states,
|
| 274 |
+
image_rotary_emb=image_rotary_emb,
|
| 275 |
+
**joint_attention_kwargs,
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
# Process attention outputs for the `hidden_states`.
|
| 279 |
+
attn_output = gate_msa.unsqueeze(1) * attn_output
|
| 280 |
+
hidden_states = hidden_states + attn_output
|
| 281 |
+
|
| 282 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 283 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
|
| 284 |
+
|
| 285 |
+
ff_output = self.ff(norm_hidden_states)
|
| 286 |
+
ff_output = gate_mlp.unsqueeze(1) * ff_output
|
| 287 |
+
|
| 288 |
+
hidden_states = hidden_states + ff_output
|
| 289 |
+
|
| 290 |
+
# Process attention outputs for the `encoder_hidden_states`.
|
| 291 |
+
|
| 292 |
+
context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output
|
| 293 |
+
encoder_hidden_states = encoder_hidden_states + context_attn_output
|
| 294 |
+
|
| 295 |
+
norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states)
|
| 296 |
+
norm_encoder_hidden_states = norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None]
|
| 297 |
+
|
| 298 |
+
context_ff_output = self.ff_context(norm_encoder_hidden_states)
|
| 299 |
+
encoder_hidden_states = encoder_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output
|
| 300 |
+
if encoder_hidden_states.dtype == torch.float16:
|
| 301 |
+
encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
|
| 302 |
+
|
| 303 |
+
return encoder_hidden_states, hidden_states
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
@maybe_allow_in_graph
|
| 307 |
+
class FluxTransformerIPBlock(nn.Module):
|
| 308 |
+
def __init__(self, dim, num_attention_heads, attention_head_dim, qk_norm="rms_norm", eps=1e-6, ip_dim=3072):
|
| 309 |
+
super().__init__()
|
| 310 |
+
|
| 311 |
+
self.norm1 = AdaLayerNormZero(dim)
|
| 312 |
+
|
| 313 |
+
self.norm1_context = AdaLayerNormZero(dim)
|
| 314 |
+
|
| 315 |
+
if hasattr(F, "scaled_dot_product_attention"):
|
| 316 |
+
processor = FluxIPAttnProcessor2_0()
|
| 317 |
+
else:
|
| 318 |
+
raise ValueError(
|
| 319 |
+
"The current PyTorch version does not support the `scaled_dot_product_attention` function."
|
| 320 |
+
)
|
| 321 |
+
self.attn = Attention(
|
| 322 |
+
query_dim=dim,
|
| 323 |
+
cross_attention_dim=None,
|
| 324 |
+
added_kv_proj_dim=dim,
|
| 325 |
+
dim_head=attention_head_dim,
|
| 326 |
+
heads=num_attention_heads,
|
| 327 |
+
out_dim=dim,
|
| 328 |
+
context_pre_only=False,
|
| 329 |
+
bias=True,
|
| 330 |
+
processor=processor,
|
| 331 |
+
qk_norm=qk_norm,
|
| 332 |
+
eps=eps,
|
| 333 |
+
)
|
| 334 |
+
self.ip_k_proj = nn.Linear(ip_dim, num_attention_heads * attention_head_dim, bias=True)
|
| 335 |
+
self.ip_v_proj = nn.Linear(ip_dim, num_attention_heads * attention_head_dim, bias=True)
|
| 336 |
+
self.ip_dim = ip_dim
|
| 337 |
+
self.num_heads = num_attention_heads
|
| 338 |
+
self.head_dim = attention_head_dim
|
| 339 |
+
nn.init.zeros_(self.ip_v_proj.weight)
|
| 340 |
+
nn.init.zeros_(self.ip_v_proj.bias)
|
| 341 |
+
|
| 342 |
+
self.norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 343 |
+
self.ff = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 344 |
+
|
| 345 |
+
self.norm2_context = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 346 |
+
self.ff_context = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 347 |
+
|
| 348 |
+
# let chunk size default to None
|
| 349 |
+
self._chunk_size = None
|
| 350 |
+
self._chunk_dim = 0
|
| 351 |
+
|
| 352 |
+
def forward(
|
| 353 |
+
self,
|
| 354 |
+
hidden_states: torch.FloatTensor,
|
| 355 |
+
encoder_hidden_states: torch.FloatTensor,
|
| 356 |
+
temb: torch.FloatTensor,
|
| 357 |
+
image_rotary_emb=None,
|
| 358 |
+
joint_attention_kwargs=None,
|
| 359 |
+
image_proj=None,
|
| 360 |
+
ip_scale = 1.0,
|
| 361 |
+
return_mask = False
|
| 362 |
+
):
|
| 363 |
+
norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(hidden_states, emb=temb)
|
| 364 |
+
|
| 365 |
+
norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_context(
|
| 366 |
+
encoder_hidden_states, emb=temb
|
| 367 |
+
)
|
| 368 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 369 |
+
# Attention.
|
| 370 |
+
attn_output, context_attn_output, img_q = self.attn(
|
| 371 |
+
hidden_states=norm_hidden_states,
|
| 372 |
+
encoder_hidden_states=norm_encoder_hidden_states,
|
| 373 |
+
image_rotary_emb=image_rotary_emb,
|
| 374 |
+
**joint_attention_kwargs,
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
# Process attention outputs for the `hidden_states`.
|
| 378 |
+
attn_output = gate_msa.unsqueeze(1) * attn_output
|
| 379 |
+
hidden_states = hidden_states + attn_output
|
| 380 |
+
|
| 381 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 382 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
|
| 383 |
+
|
| 384 |
+
ff_output = self.ff(norm_hidden_states)
|
| 385 |
+
ff_output = gate_mlp.unsqueeze(1) * ff_output
|
| 386 |
+
|
| 387 |
+
hidden_states = hidden_states + ff_output
|
| 388 |
+
|
| 389 |
+
# Process attention outputs for the `encoder_hidden_states`.
|
| 390 |
+
|
| 391 |
+
context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output
|
| 392 |
+
encoder_hidden_states = encoder_hidden_states + context_attn_output
|
| 393 |
+
|
| 394 |
+
norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states)
|
| 395 |
+
norm_encoder_hidden_states = norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None]
|
| 396 |
+
|
| 397 |
+
context_ff_output = self.ff_context(norm_encoder_hidden_states)
|
| 398 |
+
encoder_hidden_states = encoder_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output
|
| 399 |
+
if encoder_hidden_states.dtype == torch.float16:
|
| 400 |
+
encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
|
| 401 |
+
|
| 402 |
+
if image_proj is not None and ip_scale != 0:
|
| 403 |
+
ip_q = img_q
|
| 404 |
+
# image_proj = encoder_hidden_states[:, -512:, :]
|
| 405 |
+
# print("image_proj:", image_proj.shape, "encoder_hidden_states:", encoder_hidden_states.shape)
|
| 406 |
+
ip_k = self.ip_k_proj(image_proj)
|
| 407 |
+
ip_v = self.ip_v_proj(image_proj)
|
| 408 |
+
ip_k = rearrange(ip_k, 'B L (H D) -> B H L D', H=self.num_heads, D=self.head_dim)
|
| 409 |
+
ip_v = rearrange(ip_v, 'B L (H D) -> B H L D', H=self.num_heads, D=self.head_dim)
|
| 410 |
+
# print("qkv shape:", ip_q.shape, ip_k.shape, ip_v.shape)
|
| 411 |
+
ip_attention = F.scaled_dot_product_attention(ip_q, ip_k, ip_v, attn_mask=None, dropout_p=0.0, is_causal=False)
|
| 412 |
+
ip_attention = rearrange(ip_attention, 'B H L D -> B L (H D)', H=self.num_heads, D=self.head_dim)
|
| 413 |
+
hidden_states = hidden_states + ip_scale * ip_attention
|
| 414 |
+
|
| 415 |
+
if return_mask and image_proj is not None:
|
| 416 |
+
query_mask = compute_attention_mask_on_query(ip_q, ip_k)
|
| 417 |
+
return encoder_hidden_states, hidden_states, query_mask
|
| 418 |
+
else:
|
| 419 |
+
return encoder_hidden_states, hidden_states
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def compute_attention_mask_on_query(q, k, split=2):
|
| 423 |
+
L, S = q.size(-2), k.size(-2)
|
| 424 |
+
splits = [S//split for _ in range(split)]
|
| 425 |
+
scale_factor = 1 / math.sqrt(q.size(-1))
|
| 426 |
+
attn_weight = q @ k.transpose(-2, -1) * scale_factor
|
| 427 |
+
|
| 428 |
+
attn_weight = torch.softmax(attn_weight, dim=(1, 3)).mean(dim-1) # bhqk -> bq
|
| 429 |
+
return attn_weight
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
@maybe_allow_in_graph
|
| 435 |
+
class FluxSingleTransformerIPBlock(nn.Module):
|
| 436 |
+
r"""
|
| 437 |
+
A Transformer block following the MMDiT architecture, introduced in Stable Diffusion 3.
|
| 438 |
+
|
| 439 |
+
Reference: https://arxiv.org/abs/2403.03206
|
| 440 |
+
|
| 441 |
+
Parameters:
|
| 442 |
+
dim (`int`): The number of channels in the input and output.
|
| 443 |
+
num_attention_heads (`int`): The number of heads to use for multi-head attention.
|
| 444 |
+
attention_head_dim (`int`): The number of channels in each head.
|
| 445 |
+
context_pre_only (`bool`): Boolean to determine if we should add some blocks associated with the
|
| 446 |
+
processing of `context` conditions.
|
| 447 |
+
"""
|
| 448 |
+
|
| 449 |
+
def __init__(self, dim, num_attention_heads, attention_head_dim, mlp_ratio=4.0, ip_dim=4096):
|
| 450 |
+
super().__init__()
|
| 451 |
+
self.mlp_hidden_dim = int(dim * mlp_ratio)
|
| 452 |
+
|
| 453 |
+
self.norm = AdaLayerNormZeroSingle(dim)
|
| 454 |
+
self.proj_mlp = nn.Linear(dim, self.mlp_hidden_dim)
|
| 455 |
+
self.act_mlp = nn.GELU(approximate="tanh")
|
| 456 |
+
self.proj_out = nn.Linear(dim + self.mlp_hidden_dim, dim)
|
| 457 |
+
|
| 458 |
+
if is_torch_npu_available():
|
| 459 |
+
processor = FluxAttnProcessor2_0_NPU()
|
| 460 |
+
else:
|
| 461 |
+
processor = FluxIPAttnProcessor2_0()
|
| 462 |
+
self.attn = Attention(
|
| 463 |
+
query_dim=dim,
|
| 464 |
+
cross_attention_dim=None,
|
| 465 |
+
dim_head=attention_head_dim,
|
| 466 |
+
heads=num_attention_heads,
|
| 467 |
+
out_dim=dim,
|
| 468 |
+
bias=True,
|
| 469 |
+
processor=processor,
|
| 470 |
+
qk_norm="rms_norm",
|
| 471 |
+
eps=1e-6,
|
| 472 |
+
pre_only=True,
|
| 473 |
+
)
|
| 474 |
+
self.ip_k_proj = nn.Linear(ip_dim, num_attention_heads * attention_head_dim, bias=True)
|
| 475 |
+
self.ip_v_proj = nn.Linear(ip_dim, num_attention_heads * attention_head_dim, bias=True)
|
| 476 |
+
nn.init.zeros_(self.ip_v_proj.weight)
|
| 477 |
+
nn.init.zeros_(self.ip_v_proj.bias)
|
| 478 |
+
self.ip_dim = ip_dim
|
| 479 |
+
self.num_heads = num_attention_heads
|
| 480 |
+
self.head_dim = attention_head_dim
|
| 481 |
+
|
| 482 |
+
def forward(
|
| 483 |
+
self,
|
| 484 |
+
hidden_states: torch.FloatTensor,
|
| 485 |
+
temb: torch.FloatTensor,
|
| 486 |
+
image_rotary_emb=None,
|
| 487 |
+
joint_attention_kwargs=None,
|
| 488 |
+
image_proj=None,
|
| 489 |
+
ip_scale=1.0,
|
| 490 |
+
return_mask=False
|
| 491 |
+
):
|
| 492 |
+
residual = hidden_states
|
| 493 |
+
norm_hidden_states, gate = self.norm(hidden_states, emb=temb)
|
| 494 |
+
mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states))
|
| 495 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 496 |
+
attn_output, img_q = self.attn(
|
| 497 |
+
hidden_states=norm_hidden_states,
|
| 498 |
+
image_rotary_emb=image_rotary_emb,
|
| 499 |
+
**joint_attention_kwargs,
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2)
|
| 503 |
+
gate = gate.unsqueeze(1)
|
| 504 |
+
hidden_states = gate * self.proj_out(hidden_states)
|
| 505 |
+
hidden_states = residual + hidden_states
|
| 506 |
+
if hidden_states.dtype == torch.float16:
|
| 507 |
+
hidden_states = hidden_states.clip(-65504, 65504)
|
| 508 |
+
|
| 509 |
+
ip_q = img_q
|
| 510 |
+
if image_proj is not None and ip_scale != 0:
|
| 511 |
+
# image_proj = encoder_hidden_states[:, -512:, :]
|
| 512 |
+
ip_k = self.ip_k_proj(image_proj)
|
| 513 |
+
ip_v = self.ip_v_proj(image_proj)
|
| 514 |
+
ip_k = rearrange(ip_k, 'B L (H D) -> B H L D', H=self.num_heads, D=self.head_dim)
|
| 515 |
+
ip_v = rearrange(ip_v, 'B L (H D) -> B H L D', H=self.num_heads, D=self.head_dim)
|
| 516 |
+
# print("qkv shape:", ip_q.shape, ip_k.shape, ip_v.shape)
|
| 517 |
+
ip_attention = F.scaled_dot_product_attention(ip_q, ip_k, ip_v, attn_mask=None, dropout_p=0.0, is_causal=False)
|
| 518 |
+
ip_attention = rearrange(ip_attention, 'B H L D -> B L (H D)', H=self.num_heads, D=self.head_dim)
|
| 519 |
+
hidden_states = hidden_states + ip_scale * ip_attention
|
| 520 |
+
if return_mask and image_proj is not None:
|
| 521 |
+
query_mask = compute_attention_mask_on_query(ip_q, ip_k)
|
| 522 |
+
return hidden_states, query_mask
|
| 523 |
+
else:
|
| 524 |
+
return hidden_states
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
class FluxTransformer2DModel(ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin):
|
| 528 |
+
"""
|
| 529 |
+
The Transformer model introduced in Flux.
|
| 530 |
+
|
| 531 |
+
Reference: https://blackforestlabs.ai/announcing-black-forest-labs/
|
| 532 |
+
|
| 533 |
+
Parameters:
|
| 534 |
+
patch_size (`int`): Patch size to turn the input data into small patches.
|
| 535 |
+
in_channels (`int`, *optional*, defaults to 16): The number of channels in the input.
|
| 536 |
+
num_layers (`int`, *optional*, defaults to 18): The number of layers of MMDiT blocks to use.
|
| 537 |
+
num_single_layers (`int`, *optional*, defaults to 18): The number of layers of single DiT blocks to use.
|
| 538 |
+
attention_head_dim (`int`, *optional*, defaults to 64): The number of channels in each head.
|
| 539 |
+
num_attention_heads (`int`, *optional*, defaults to 18): The number of heads to use for multi-head attention.
|
| 540 |
+
joint_attention_dim (`int`, *optional*): The number of `encoder_hidden_states` dimensions to use.
|
| 541 |
+
pooled_projection_dim (`int`): Number of dimensions to use when projecting the `pooled_projections`.
|
| 542 |
+
guidance_embeds (`bool`, defaults to False): Whether to use guidance embeddings.
|
| 543 |
+
"""
|
| 544 |
+
|
| 545 |
+
_supports_gradient_checkpointing = True
|
| 546 |
+
_no_split_modules = ["FluxTransformerBlock", "FluxSingleTransformerBlock"]
|
| 547 |
+
|
| 548 |
+
@register_to_config
|
| 549 |
+
def __init__(
|
| 550 |
+
self,
|
| 551 |
+
patch_size: int = 1,
|
| 552 |
+
in_channels: int = 64,
|
| 553 |
+
out_channels: Optional[int] = None,
|
| 554 |
+
num_layers: int = 19,
|
| 555 |
+
num_single_layers: int = 38,
|
| 556 |
+
attention_head_dim: int = 128,
|
| 557 |
+
num_attention_heads: int = 24,
|
| 558 |
+
joint_attention_dim: int = 4096,
|
| 559 |
+
pooled_projection_dim: int = 768,
|
| 560 |
+
guidance_embeds: bool = False,
|
| 561 |
+
axes_dims_rope: Tuple[int] = (16, 56, 56),
|
| 562 |
+
ip_dim: int = 4096
|
| 563 |
+
):
|
| 564 |
+
super().__init__()
|
| 565 |
+
self.out_channels = out_channels or in_channels
|
| 566 |
+
self.inner_dim = self.config.num_attention_heads * self.config.attention_head_dim
|
| 567 |
+
self.ip_scale = 1.0
|
| 568 |
+
self.addition_seq_len = 512
|
| 569 |
+
|
| 570 |
+
self.pos_embed = FluxPosEmbed(theta=10000, axes_dim=axes_dims_rope)
|
| 571 |
+
|
| 572 |
+
text_time_guidance_cls = (
|
| 573 |
+
CombinedTimestepGuidanceTextProjEmbeddings if guidance_embeds else CombinedTimestepTextProjEmbeddings
|
| 574 |
+
)
|
| 575 |
+
self.time_text_embed = text_time_guidance_cls(
|
| 576 |
+
embedding_dim=self.inner_dim, pooled_projection_dim=self.config.pooled_projection_dim
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
self.context_embedder = nn.Linear(self.config.joint_attention_dim, self.inner_dim)
|
| 580 |
+
self.x_embedder = nn.Linear(self.config.in_channels, self.inner_dim)
|
| 581 |
+
|
| 582 |
+
self.transformer_blocks = nn.ModuleList(
|
| 583 |
+
[
|
| 584 |
+
FluxTransformerIPBlock(
|
| 585 |
+
dim=self.inner_dim,
|
| 586 |
+
num_attention_heads=self.config.num_attention_heads,
|
| 587 |
+
attention_head_dim=self.config.attention_head_dim,
|
| 588 |
+
ip_dim=ip_dim
|
| 589 |
+
)
|
| 590 |
+
for i in range(self.config.num_layers)
|
| 591 |
+
]
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
self.single_transformer_blocks = nn.ModuleList(
|
| 595 |
+
[
|
| 596 |
+
FluxSingleTransformerIPBlock(
|
| 597 |
+
dim=self.inner_dim,
|
| 598 |
+
num_attention_heads=self.config.num_attention_heads,
|
| 599 |
+
attention_head_dim=self.config.attention_head_dim,
|
| 600 |
+
ip_dim=ip_dim
|
| 601 |
+
)
|
| 602 |
+
for i in range(self.config.num_single_layers)
|
| 603 |
+
]
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
self.norm_out = AdaLayerNormContinuous(self.inner_dim, self.inner_dim, elementwise_affine=False, eps=1e-6)
|
| 607 |
+
self.proj_out = nn.Linear(self.inner_dim, patch_size * patch_size * self.out_channels, bias=True)
|
| 608 |
+
|
| 609 |
+
self.gradient_checkpointing = False
|
| 610 |
+
|
| 611 |
+
@property
|
| 612 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.attn_processors
|
| 613 |
+
def attn_processors(self) -> Dict[str, AttentionProcessor]:
|
| 614 |
+
r"""
|
| 615 |
+
Returns:
|
| 616 |
+
`dict` of attention processors: A dictionary containing all attention processors used in the model with
|
| 617 |
+
indexed by its weight name.
|
| 618 |
+
"""
|
| 619 |
+
# set recursively
|
| 620 |
+
processors = {}
|
| 621 |
+
|
| 622 |
+
def fn_recursive_add_processors(name: str, module: torch.nn.Module, processors: Dict[str, AttentionProcessor]):
|
| 623 |
+
if hasattr(module, "get_processor"):
|
| 624 |
+
processors[f"{name}.processor"] = module.get_processor()
|
| 625 |
+
|
| 626 |
+
for sub_name, child in module.named_children():
|
| 627 |
+
fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)
|
| 628 |
+
|
| 629 |
+
return processors
|
| 630 |
+
|
| 631 |
+
for name, module in self.named_children():
|
| 632 |
+
fn_recursive_add_processors(name, module, processors)
|
| 633 |
+
|
| 634 |
+
return processors
|
| 635 |
+
|
| 636 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor
|
| 637 |
+
def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
|
| 638 |
+
r"""
|
| 639 |
+
Sets the attention processor to use to compute attention.
|
| 640 |
+
|
| 641 |
+
Parameters:
|
| 642 |
+
processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
|
| 643 |
+
The instantiated processor class or a dictionary of processor classes that will be set as the processor
|
| 644 |
+
for **all** `Attention` layers.
|
| 645 |
+
|
| 646 |
+
If `processor` is a dict, the key needs to define the path to the corresponding cross attention
|
| 647 |
+
processor. This is strongly recommended when setting trainable attention processors.
|
| 648 |
+
|
| 649 |
+
"""
|
| 650 |
+
count = len(self.attn_processors.keys())
|
| 651 |
+
|
| 652 |
+
if isinstance(processor, dict) and len(processor) != count:
|
| 653 |
+
raise ValueError(
|
| 654 |
+
f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
|
| 655 |
+
f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
|
| 656 |
+
)
|
| 657 |
+
|
| 658 |
+
def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
|
| 659 |
+
if hasattr(module, "set_processor"):
|
| 660 |
+
if not isinstance(processor, dict):
|
| 661 |
+
module.set_processor(processor)
|
| 662 |
+
else:
|
| 663 |
+
module.set_processor(processor.pop(f"{name}.processor"))
|
| 664 |
+
|
| 665 |
+
for sub_name, child in module.named_children():
|
| 666 |
+
fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
|
| 667 |
+
|
| 668 |
+
for name, module in self.named_children():
|
| 669 |
+
fn_recursive_attn_processor(name, module, processor)
|
| 670 |
+
|
| 671 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.fuse_qkv_projections with FusedAttnProcessor2_0->FusedFluxAttnProcessor2_0
|
| 672 |
+
def fuse_qkv_projections(self):
|
| 673 |
+
"""
|
| 674 |
+
Enables fused QKV projections. For self-attention modules, all projection matrices (i.e., query, key, value)
|
| 675 |
+
are fused. For cross-attention modules, key and value projection matrices are fused.
|
| 676 |
+
|
| 677 |
+
<Tip warning={true}>
|
| 678 |
+
|
| 679 |
+
This API is 🧪 experimental.
|
| 680 |
+
|
| 681 |
+
</Tip>
|
| 682 |
+
"""
|
| 683 |
+
self.original_attn_processors = None
|
| 684 |
+
|
| 685 |
+
for _, attn_processor in self.attn_processors.items():
|
| 686 |
+
if "Added" in str(attn_processor.__class__.__name__):
|
| 687 |
+
raise ValueError("`fuse_qkv_projections()` is not supported for models having added KV projections.")
|
| 688 |
+
|
| 689 |
+
self.original_attn_processors = self.attn_processors
|
| 690 |
+
|
| 691 |
+
for module in self.modules():
|
| 692 |
+
if isinstance(module, Attention):
|
| 693 |
+
module.fuse_projections(fuse=True)
|
| 694 |
+
|
| 695 |
+
self.set_attn_processor(FusedFluxAttnProcessor2_0())
|
| 696 |
+
|
| 697 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.unfuse_qkv_projections
|
| 698 |
+
def unfuse_qkv_projections(self):
|
| 699 |
+
"""Disables the fused QKV projection if enabled.
|
| 700 |
+
|
| 701 |
+
<Tip warning={true}>
|
| 702 |
+
|
| 703 |
+
This API is 🧪 experimental.
|
| 704 |
+
|
| 705 |
+
</Tip>
|
| 706 |
+
|
| 707 |
+
"""
|
| 708 |
+
if self.original_attn_processors is not None:
|
| 709 |
+
self.set_attn_processor(self.original_attn_processors)
|
| 710 |
+
|
| 711 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
| 712 |
+
if hasattr(module, "gradient_checkpointing"):
|
| 713 |
+
module.gradient_checkpointing = value
|
| 714 |
+
|
| 715 |
+
def forward(
|
| 716 |
+
self,
|
| 717 |
+
hidden_states: torch.Tensor,
|
| 718 |
+
encoder_hidden_states: torch.Tensor = None,
|
| 719 |
+
image_proj_hidden_states: torch.Tensor = None,
|
| 720 |
+
pooled_projections: torch.Tensor = None,
|
| 721 |
+
timestep: torch.LongTensor = None,
|
| 722 |
+
img_ids: torch.Tensor = None,
|
| 723 |
+
txt_ids: torch.Tensor = None,
|
| 724 |
+
guidance: torch.Tensor = None,
|
| 725 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 726 |
+
controlnet_block_samples=None,
|
| 727 |
+
controlnet_single_block_samples=None,
|
| 728 |
+
return_dict: bool = True,
|
| 729 |
+
controlnet_blocks_repeat: bool = False,
|
| 730 |
+
ip_scale: int = 1.0,
|
| 731 |
+
return_mask: bool = False
|
| 732 |
+
) -> Union[torch.FloatTensor, Transformer2DModelOutput]:
|
| 733 |
+
"""
|
| 734 |
+
The [`FluxTransformer2DModel`] forward method.
|
| 735 |
+
|
| 736 |
+
Args:
|
| 737 |
+
hidden_states (`torch.FloatTensor` of shape `(batch size, channel, height, width)`):
|
| 738 |
+
Input `hidden_states`.
|
| 739 |
+
encoder_hidden_states (`torch.FloatTensor` of shape `(batch size, sequence_len, embed_dims)`):
|
| 740 |
+
Conditional embeddings (embeddings computed from the input conditions such as prompts) to use.
|
| 741 |
+
pooled_projections (`torch.FloatTensor` of shape `(batch_size, projection_dim)`): Embeddings projected
|
| 742 |
+
from the embeddings of input conditions.
|
| 743 |
+
timestep ( `torch.LongTensor`):
|
| 744 |
+
Used to indicate denoising step.
|
| 745 |
+
block_controlnet_hidden_states: (`list` of `torch.Tensor`):
|
| 746 |
+
A list of tensors that if specified are added to the residuals of transformer blocks.
|
| 747 |
+
joint_attention_kwargs (`dict`, *optional*):
|
| 748 |
+
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
| 749 |
+
`self.processor` in
|
| 750 |
+
[diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
| 751 |
+
return_dict (`bool`, *optional*, defaults to `True`):
|
| 752 |
+
Whether or not to return a [`~models.transformer_2d.Transformer2DModelOutput`] instead of a plain
|
| 753 |
+
tuple.
|
| 754 |
+
|
| 755 |
+
Returns:
|
| 756 |
+
If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a
|
| 757 |
+
`tuple` where the first element is the sample tensor.
|
| 758 |
+
"""
|
| 759 |
+
if joint_attention_kwargs is not None:
|
| 760 |
+
joint_attention_kwargs = joint_attention_kwargs.copy()
|
| 761 |
+
lora_scale = joint_attention_kwargs.pop("scale", 1.0)
|
| 762 |
+
else:
|
| 763 |
+
lora_scale = 1.0
|
| 764 |
+
|
| 765 |
+
if USE_PEFT_BACKEND:
|
| 766 |
+
# weight the lora layers by setting `lora_scale` for each PEFT layer
|
| 767 |
+
scale_lora_layers(self, lora_scale)
|
| 768 |
+
else:
|
| 769 |
+
if joint_attention_kwargs is not None and joint_attention_kwargs.get("scale", None) is not None:
|
| 770 |
+
logger.warning(
|
| 771 |
+
"Passing `scale` via `joint_attention_kwargs` when not using the PEFT backend is ineffective."
|
| 772 |
+
)
|
| 773 |
+
|
| 774 |
+
hidden_states = self.x_embedder(hidden_states)
|
| 775 |
+
|
| 776 |
+
timestep = timestep.to(hidden_states.dtype) * 1000
|
| 777 |
+
if guidance is not None:
|
| 778 |
+
guidance = guidance.to(hidden_states.dtype) * 1000
|
| 779 |
+
else:
|
| 780 |
+
guidance = None
|
| 781 |
+
|
| 782 |
+
temb = (
|
| 783 |
+
self.time_text_embed(timestep, pooled_projections)
|
| 784 |
+
if guidance is None
|
| 785 |
+
else self.time_text_embed(timestep, guidance, pooled_projections)
|
| 786 |
+
)
|
| 787 |
+
_, _s, _ = encoder_hidden_states.shape
|
| 788 |
+
if _s > 2048:
|
| 789 |
+
_im_len = -2050
|
| 790 |
+
elif _s > 1024:
|
| 791 |
+
_im_len = -1025
|
| 792 |
+
elif _s > 512:
|
| 793 |
+
_im_len = -514
|
| 794 |
+
else:
|
| 795 |
+
_im_len = 0
|
| 796 |
+
|
| 797 |
+
if _im_len != 0:
|
| 798 |
+
image_proj = encoder_hidden_states[:, _im_len:, :]
|
| 799 |
+
encoder_hidden_states = self.context_embedder(encoder_hidden_states[:, :_im_len, :])
|
| 800 |
+
txt_ids = txt_ids[:_im_len, :]
|
| 801 |
+
else:
|
| 802 |
+
image_proj = image_proj_hidden_states
|
| 803 |
+
encoder_hidden_states = self.context_embedder(encoder_hidden_states)
|
| 804 |
+
|
| 805 |
+
if txt_ids.ndim == 3:
|
| 806 |
+
logger.warning(
|
| 807 |
+
"Passing `txt_ids` 3d torch.Tensor is deprecated."
|
| 808 |
+
"Please remove the batch dimension and pass it as a 2d torch Tensor"
|
| 809 |
+
)
|
| 810 |
+
txt_ids = txt_ids[0]
|
| 811 |
+
if img_ids.ndim == 3:
|
| 812 |
+
logger.warning(
|
| 813 |
+
"Passing `img_ids` 3d torch.Tensor is deprecated."
|
| 814 |
+
"Please remove the batch dimension and pass it as a 2d torch Tensor"
|
| 815 |
+
)
|
| 816 |
+
img_ids = img_ids[0]
|
| 817 |
+
|
| 818 |
+
ids = torch.cat((txt_ids, img_ids), dim=0)
|
| 819 |
+
# print("id shape:", txt_ids.shape, img_ids.shape, ids.shape)
|
| 820 |
+
image_rotary_emb = self.pos_embed(ids)
|
| 821 |
+
|
| 822 |
+
masks = []
|
| 823 |
+
ip_scale = ip_scale or self.ip_scale
|
| 824 |
+
for index_block, block in enumerate(self.transformer_blocks):
|
| 825 |
+
if torch.is_grad_enabled() and self.gradient_checkpointing:
|
| 826 |
+
|
| 827 |
+
def create_custom_forward(module, return_dict=None):
|
| 828 |
+
def custom_forward(*inputs):
|
| 829 |
+
if return_dict is not None:
|
| 830 |
+
return module(*inputs, return_dict=return_dict)
|
| 831 |
+
else:
|
| 832 |
+
return module(*inputs)
|
| 833 |
+
|
| 834 |
+
return custom_forward
|
| 835 |
+
|
| 836 |
+
ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
|
| 837 |
+
results = torch.utils.checkpoint.checkpoint(
|
| 838 |
+
create_custom_forward(block),
|
| 839 |
+
hidden_states,
|
| 840 |
+
encoder_hidden_states,
|
| 841 |
+
temb,
|
| 842 |
+
image_rotary_emb,
|
| 843 |
+
joint_attention_kwargs,
|
| 844 |
+
image_proj,
|
| 845 |
+
ip_scale,
|
| 846 |
+
return_mask,
|
| 847 |
+
**ckpt_kwargs
|
| 848 |
+
)
|
| 849 |
+
else:
|
| 850 |
+
results = block(
|
| 851 |
+
hidden_states=hidden_states,
|
| 852 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 853 |
+
temb=temb,
|
| 854 |
+
image_rotary_emb=image_rotary_emb,
|
| 855 |
+
joint_attention_kwargs=joint_attention_kwargs,
|
| 856 |
+
image_proj=image_proj,
|
| 857 |
+
ip_scale=ip_scale,
|
| 858 |
+
return_mask=return_mask
|
| 859 |
+
)
|
| 860 |
+
if return_mask:
|
| 861 |
+
encoder_hidden_states, hidden_states, query_mask = results
|
| 862 |
+
masks.append(query_mask)
|
| 863 |
+
else:
|
| 864 |
+
encoder_hidden_states, hidden_states = results
|
| 865 |
+
|
| 866 |
+
# controlnet residual
|
| 867 |
+
if controlnet_block_samples is not None:
|
| 868 |
+
interval_control = len(self.transformer_blocks) / len(controlnet_block_samples)
|
| 869 |
+
interval_control = int(np.ceil(interval_control))
|
| 870 |
+
# For Xlabs ControlNet.
|
| 871 |
+
if controlnet_blocks_repeat:
|
| 872 |
+
hidden_states = (
|
| 873 |
+
hidden_states + controlnet_block_samples[index_block % len(controlnet_block_samples)]
|
| 874 |
+
)
|
| 875 |
+
else:
|
| 876 |
+
hidden_states = hidden_states + controlnet_block_samples[index_block // interval_control]
|
| 877 |
+
|
| 878 |
+
hidden_states = torch.cat([encoder_hidden_states, hidden_states], dim=1)
|
| 879 |
+
|
| 880 |
+
for index_block, block in enumerate(self.single_transformer_blocks):
|
| 881 |
+
if torch.is_grad_enabled() and self.gradient_checkpointing:
|
| 882 |
+
|
| 883 |
+
def create_custom_forward(module, return_dict=None):
|
| 884 |
+
def custom_forward(*inputs):
|
| 885 |
+
if return_dict is not None:
|
| 886 |
+
return module(*inputs, return_dict=return_dict)
|
| 887 |
+
else:
|
| 888 |
+
return module(*inputs)
|
| 889 |
+
|
| 890 |
+
return custom_forward
|
| 891 |
+
|
| 892 |
+
ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
|
| 893 |
+
results = torch.utils.checkpoint.checkpoint(
|
| 894 |
+
create_custom_forward(block),
|
| 895 |
+
hidden_states,
|
| 896 |
+
temb,
|
| 897 |
+
image_rotary_emb,
|
| 898 |
+
joint_attention_kwargs,
|
| 899 |
+
image_proj,
|
| 900 |
+
ip_scale,
|
| 901 |
+
return_mask,
|
| 902 |
+
**ckpt_kwargs,
|
| 903 |
+
)
|
| 904 |
+
|
| 905 |
+
else:
|
| 906 |
+
results = block(
|
| 907 |
+
hidden_states=hidden_states,
|
| 908 |
+
temb=temb,
|
| 909 |
+
image_rotary_emb=image_rotary_emb,
|
| 910 |
+
joint_attention_kwargs=joint_attention_kwargs,
|
| 911 |
+
image_proj=image_proj,
|
| 912 |
+
ip_scale=ip_scale,
|
| 913 |
+
return_mask=return_mask
|
| 914 |
+
)
|
| 915 |
+
if return_mask:
|
| 916 |
+
hidden_states, query_mask = results
|
| 917 |
+
masks.append(query_mask)
|
| 918 |
+
else:
|
| 919 |
+
hidden_states = results
|
| 920 |
+
|
| 921 |
+
# controlnet residual
|
| 922 |
+
if controlnet_single_block_samples is not None:
|
| 923 |
+
interval_control = len(self.single_transformer_blocks) / len(controlnet_single_block_samples)
|
| 924 |
+
interval_control = int(np.ceil(interval_control))
|
| 925 |
+
hidden_states[:, encoder_hidden_states.shape[1] :, ...] = (
|
| 926 |
+
hidden_states[:, encoder_hidden_states.shape[1] :, ...]
|
| 927 |
+
+ controlnet_single_block_samples[index_block // interval_control]
|
| 928 |
+
)
|
| 929 |
+
|
| 930 |
+
hidden_states = hidden_states[:, encoder_hidden_states.shape[1] :, ...]
|
| 931 |
+
|
| 932 |
+
hidden_states = self.norm_out(hidden_states, temb)
|
| 933 |
+
output = self.proj_out(hidden_states)
|
| 934 |
+
|
| 935 |
+
if USE_PEFT_BACKEND:
|
| 936 |
+
# remove `lora_scale` from each PEFT layer
|
| 937 |
+
unscale_lora_layers(self, lora_scale)
|
| 938 |
+
|
| 939 |
+
if not return_dict:
|
| 940 |
+
return (output,)
|
| 941 |
+
|
| 942 |
+
return Transformer2DModelOutput(sample=output)
|