Init.
Browse files- .gitattributes +35 -35
- README.md +10 -13
- app.py +202 -0
- models/attention_processor.py +124 -0
- models/calligrapher.py +115 -0
- models/projection_models.py +71 -0
- models/transformer_flux_inpainting.py +624 -0
- pipeline_calligrapher.py +972 -0
- requirements.txt +10 -0
- utils.py +125 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Calligrapher
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: 'Calligrapher: Freestyle Text Image Customization'
|
| 3 |
+
emoji: 🖌️
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio demo for text customization with Calligrapher (the reference is uploaded by the user).
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import torch
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
import spaces
|
| 13 |
+
from huggingface_hub import snapshot_download
|
| 14 |
+
from pipeline_calligrapher import CalligrapherPipeline
|
| 15 |
+
from models.calligrapher import Calligrapher
|
| 16 |
+
from models.transformer_flux_inpainting import FluxTransformer2DModel
|
| 17 |
+
from utils import process_gradio_source, get_bbox_from_mask, crop_image_from_bb, \
|
| 18 |
+
resize_img_and_pad, generate_context_reference_image
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Function of loading pre-trained models.
|
| 22 |
+
def load_models():
|
| 23 |
+
snapshot_download(
|
| 24 |
+
repo_id="Calligrapher2025/Calligrapher",
|
| 25 |
+
allow_patterns="calligrapher.bin",
|
| 26 |
+
local_dir="./",
|
| 27 |
+
)
|
| 28 |
+
print("calligrapher.bin successfully downloaded!")
|
| 29 |
+
transformer = FluxTransformer2DModel.from_pretrained("black-forest-labs/FLUX.1-Fill-dev",
|
| 30 |
+
subfolder="transformer",
|
| 31 |
+
torch_dtype=torch.bfloat16)
|
| 32 |
+
pipe = CalligrapherPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev",
|
| 33 |
+
transformer=transformer,
|
| 34 |
+
torch_dtype=torch.bfloat16).to("cuda")
|
| 35 |
+
model = Calligrapher(pipe,
|
| 36 |
+
image_encoder_path="google/siglip-so400m-patch14-384",
|
| 37 |
+
calligrapher_path="calligrapher.bin",
|
| 38 |
+
device="cuda", num_tokens=128)
|
| 39 |
+
return model
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Init models.
|
| 43 |
+
model = load_models()
|
| 44 |
+
print('Model loaded!')
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@spaces.GPU()
|
| 48 |
+
def process_and_generate(editor_component, reference_image, prompt, height, width,
|
| 49 |
+
scale, steps=50, seed=42, use_context=True, num_images=1):
|
| 50 |
+
print('Begin processing!')
|
| 51 |
+
# Get source, mask, and cropped images from gr.ImageEditor.
|
| 52 |
+
source_image, mask_image, cropped_image = process_gradio_source(editor_component)
|
| 53 |
+
|
| 54 |
+
# Resize source and mask.
|
| 55 |
+
source_image = source_image.resize((width, height))
|
| 56 |
+
mask_image = mask_image.resize((width, height), Image.NEAREST)
|
| 57 |
+
mask_np = np.array(mask_image)
|
| 58 |
+
mask_np[mask_np > 0] = 255
|
| 59 |
+
mask_image = Image.fromarray(mask_np.astype(np.uint8))
|
| 60 |
+
|
| 61 |
+
if reference_image is None:
|
| 62 |
+
# If self-inpaint (no input ref): (1) get bounding box from the mask and (2) perform cropping to get the ref image.
|
| 63 |
+
tl, br = get_bbox_from_mask(mask_image)
|
| 64 |
+
# Convert irregularly shaped masks into rectangles.
|
| 65 |
+
reference_image = crop_image_from_bb(source_image, tl, br)
|
| 66 |
+
# Raw reference image before resizing.
|
| 67 |
+
reference_image_to_encoder = resize_img_and_pad(reference_image, target_size=(512, 512))
|
| 68 |
+
|
| 69 |
+
if use_context:
|
| 70 |
+
reference_context = generate_context_reference_image(reference_image, width)
|
| 71 |
+
# Concat the context on the top of the input masked image in the pixel space.
|
| 72 |
+
source_with_context = Image.new(source_image.mode, (width, reference_context.size[1] + height))
|
| 73 |
+
source_with_context.paste(reference_context, (0, 0))
|
| 74 |
+
source_with_context.paste(source_image, (0, reference_context.size[1]))
|
| 75 |
+
# Concat the zero mask on the top of the mask image.
|
| 76 |
+
mask_with_context = Image.new(mask_image.mode,
|
| 77 |
+
(mask_image.size[0],
|
| 78 |
+
reference_context.size[1] + mask_image.size[0]),
|
| 79 |
+
color=0)
|
| 80 |
+
mask_with_context.paste(mask_image, (0, reference_context.size[1]))
|
| 81 |
+
|
| 82 |
+
source_image = source_with_context
|
| 83 |
+
mask_image = mask_with_context
|
| 84 |
+
|
| 85 |
+
all_generated_images = []
|
| 86 |
+
for i in range(num_images):
|
| 87 |
+
res = model.generate(
|
| 88 |
+
image=source_image,
|
| 89 |
+
mask_image=mask_image,
|
| 90 |
+
ref_image=reference_image_to_encoder,
|
| 91 |
+
prompt=prompt,
|
| 92 |
+
scale=scale,
|
| 93 |
+
num_inference_steps=steps,
|
| 94 |
+
width=source_image.size[0],
|
| 95 |
+
height=source_image.size[1],
|
| 96 |
+
seed=seed + i,
|
| 97 |
+
)[0]
|
| 98 |
+
if use_context:
|
| 99 |
+
res_vis = res.crop((0, reference_context.size[1], res.width, res.height)) # remove context
|
| 100 |
+
mask_vis = mask_image.crop(
|
| 101 |
+
(0, reference_context.size[1], mask_image.width, mask_image.height)) # remove context mask
|
| 102 |
+
else:
|
| 103 |
+
res_vis = res
|
| 104 |
+
mask_vis = mask_image
|
| 105 |
+
all_generated_images.append((res_vis, f"Generating {i + 1} (Seed: {seed + i})"))
|
| 106 |
+
|
| 107 |
+
return mask_vis, reference_image_to_encoder, all_generated_images
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Main gradio codes.
|
| 111 |
+
with gr.Blocks(theme="default", css=".image-editor img {max-width: 70%; height: 70%;}") as demo:
|
| 112 |
+
gr.Markdown(
|
| 113 |
+
"""
|
| 114 |
+
# 🖌️ Calligrapher: Freestyle Text Image Customization    [[Code]](https://github.com/Calligrapher2025/Calligrapher) [[Project Page]](https://calligrapher2025.github.io/Calligrapher/)
|
| 115 |
+
### Consider giving a star to the [project](https://github.com/Calligrapher2025/Calligrapher) if you find it useful!
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
with gr.Column(scale=3):
|
| 121 |
+
gr.Markdown("### 🎨 Image Editing Panel")
|
| 122 |
+
editor_component = gr.ImageEditor(
|
| 123 |
+
label="Upload or Draw",
|
| 124 |
+
type="pil",
|
| 125 |
+
brush=gr.Brush(colors=["#FFFFFF"], default_size=30, color_mode="fixed"),
|
| 126 |
+
layers=True,
|
| 127 |
+
interactive=True,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
gr.Markdown("### 📤 Output Result")
|
| 131 |
+
gallery = gr.Gallery(label="🖼️ Result Gallery")
|
| 132 |
+
gr.Markdown(
|
| 133 |
+
"""<br>
|
| 134 |
+
|
| 135 |
+
### ✨User Tips:
|
| 136 |
+
|
| 137 |
+
1. **Speed vs Quality Trade-off.** Use fewer steps (e.g., 10-step which takes ~4s/image on a single A6000 GPU) for faster generation, but quality may be lower.
|
| 138 |
+
|
| 139 |
+
2. **Inpaint Position Freedom.** Inpainting positions are flexible - they don't necessarily need to match the original text locations in the input image.
|
| 140 |
+
|
| 141 |
+
3. **Iterative Editing.** Drag outputs from the gallery to the Image Editing Panel (clean the Editing Panel first) for quick refinements.
|
| 142 |
+
|
| 143 |
+
4. **Mask Optimization.** Adjust mask size/aspect ratio to match your desired content. The model tends to fill the masks, and harmonizes the generation with background in terms of color and lighting.
|
| 144 |
+
|
| 145 |
+
5. **Reference Image Tip.** White-background references improve style consistency - the encoder also considers background context of the given reference image.
|
| 146 |
+
|
| 147 |
+
6. **Resolution Balance.** Very high-resolution generation sometimes triggers spelling errors. 512/768px are recommended considering the model is trained under the resolution of 512.
|
| 148 |
+
"""
|
| 149 |
+
)
|
| 150 |
+
with gr.Column(scale=1):
|
| 151 |
+
gr.Markdown("### ⚙️Settings")
|
| 152 |
+
reference_image = gr.Image(
|
| 153 |
+
label="🧩 Reference Image (skip this if self-reference)",
|
| 154 |
+
sources=["upload"],
|
| 155 |
+
type="pil",
|
| 156 |
+
)
|
| 157 |
+
prompt = gr.Textbox(
|
| 158 |
+
label="📝 Prompt",
|
| 159 |
+
placeholder="The text is 'Image'...",
|
| 160 |
+
value="The text is 'Image'."
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
with gr.Accordion("🔧 Additional Settings", open=True):
|
| 164 |
+
with gr.Row():
|
| 165 |
+
height = gr.Number(label="Height", value=512, precision=0)
|
| 166 |
+
width = gr.Number(label="Width", value=512, precision=0)
|
| 167 |
+
scale = gr.Slider(0.0, 2.0, 1.0, step=0.1, value=1.0, label="🎚️ Strength")
|
| 168 |
+
steps = gr.Slider(1, 100, 50, step=1, label="🔁 Steps")
|
| 169 |
+
with gr.Row():
|
| 170 |
+
seed = gr.Number(label="🎲 Seed", value=56, precision=0)
|
| 171 |
+
use_context = gr.Checkbox(value=True, label="🔍 Use Context", interactive=True)
|
| 172 |
+
num_images = gr.Slider(1, 16, 2, step=1, label="🖼️ Sample Amount")
|
| 173 |
+
|
| 174 |
+
run_btn = gr.Button("🚀 Run", variant="primary")
|
| 175 |
+
|
| 176 |
+
mask_output = gr.Image(label="🟩 Mask Demo")
|
| 177 |
+
reference_demo = gr.Image(label="🧩 Reference Demo")
|
| 178 |
+
|
| 179 |
+
# Run button event.
|
| 180 |
+
run_btn.click(
|
| 181 |
+
fn=process_and_generate,
|
| 182 |
+
inputs=[
|
| 183 |
+
editor_component,
|
| 184 |
+
reference_image,
|
| 185 |
+
prompt,
|
| 186 |
+
height,
|
| 187 |
+
width,
|
| 188 |
+
scale,
|
| 189 |
+
steps,
|
| 190 |
+
seed,
|
| 191 |
+
use_context,
|
| 192 |
+
num_images
|
| 193 |
+
],
|
| 194 |
+
outputs=[
|
| 195 |
+
mask_output,
|
| 196 |
+
reference_demo,
|
| 197 |
+
gallery
|
| 198 |
+
]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
if __name__ == "__main__":
|
| 202 |
+
demo.launch()
|
models/attention_processor.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from diffusers.models.normalization import RMSNorm
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class FluxAttnProcessor(nn.Module):
|
| 9 |
+
def __init__(self, hidden_size, cross_attention_dim=None, scale=1.0, num_tokens=4):
|
| 10 |
+
super().__init__()
|
| 11 |
+
|
| 12 |
+
self.hidden_size = hidden_size
|
| 13 |
+
self.cross_attention_dim = cross_attention_dim
|
| 14 |
+
self.scale = scale
|
| 15 |
+
self.num_tokens = num_tokens
|
| 16 |
+
|
| 17 |
+
self.to_k_ip = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
|
| 18 |
+
self.to_v_ip = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
|
| 19 |
+
|
| 20 |
+
self.norm_added_k = RMSNorm(128, eps=1e-5, elementwise_affine=False)
|
| 21 |
+
|
| 22 |
+
def __call__(
|
| 23 |
+
self,
|
| 24 |
+
attn,
|
| 25 |
+
hidden_states: torch.FloatTensor,
|
| 26 |
+
image_emb: torch.FloatTensor,
|
| 27 |
+
encoder_hidden_states: torch.FloatTensor = None,
|
| 28 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 29 |
+
image_rotary_emb: Optional[torch.Tensor] = None,
|
| 30 |
+
) -> torch.FloatTensor:
|
| 31 |
+
batch_size, _, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
|
| 32 |
+
|
| 33 |
+
query = attn.to_q(hidden_states)
|
| 34 |
+
key = attn.to_k(hidden_states)
|
| 35 |
+
value = attn.to_v(hidden_states)
|
| 36 |
+
|
| 37 |
+
inner_dim = key.shape[-1]
|
| 38 |
+
head_dim = inner_dim // attn.heads
|
| 39 |
+
|
| 40 |
+
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 41 |
+
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 42 |
+
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 43 |
+
|
| 44 |
+
if attn.norm_q is not None:
|
| 45 |
+
query = attn.norm_q(query)
|
| 46 |
+
if attn.norm_k is not None:
|
| 47 |
+
key = attn.norm_k(key)
|
| 48 |
+
|
| 49 |
+
if image_emb is not None:
|
| 50 |
+
ip_hidden_states = image_emb
|
| 51 |
+
ip_hidden_states_key_proj = self.to_k_ip(ip_hidden_states)
|
| 52 |
+
ip_hidden_states_value_proj = self.to_v_ip(ip_hidden_states)
|
| 53 |
+
|
| 54 |
+
ip_hidden_states_key_proj = ip_hidden_states_key_proj.view(
|
| 55 |
+
batch_size, -1, attn.heads, head_dim
|
| 56 |
+
).transpose(1, 2)
|
| 57 |
+
ip_hidden_states_value_proj = ip_hidden_states_value_proj.view(
|
| 58 |
+
batch_size, -1, attn.heads, head_dim
|
| 59 |
+
).transpose(1, 2)
|
| 60 |
+
|
| 61 |
+
ip_hidden_states_key_proj = self.norm_added_k(ip_hidden_states_key_proj)
|
| 62 |
+
|
| 63 |
+
ip_hidden_states = F.scaled_dot_product_attention(query,
|
| 64 |
+
ip_hidden_states_key_proj,
|
| 65 |
+
ip_hidden_states_value_proj,
|
| 66 |
+
dropout_p=0.0, is_causal=False)
|
| 67 |
+
|
| 68 |
+
ip_hidden_states = ip_hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
|
| 69 |
+
ip_hidden_states = ip_hidden_states.to(query.dtype)
|
| 70 |
+
|
| 71 |
+
if encoder_hidden_states is not None:
|
| 72 |
+
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
|
| 73 |
+
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
|
| 74 |
+
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
|
| 75 |
+
|
| 76 |
+
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
|
| 77 |
+
batch_size, -1, attn.heads, head_dim
|
| 78 |
+
).transpose(1, 2)
|
| 79 |
+
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
|
| 80 |
+
batch_size, -1, attn.heads, head_dim
|
| 81 |
+
).transpose(1, 2)
|
| 82 |
+
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view(
|
| 83 |
+
batch_size, -1, attn.heads, head_dim
|
| 84 |
+
).transpose(1, 2)
|
| 85 |
+
|
| 86 |
+
if attn.norm_added_q is not None:
|
| 87 |
+
encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
|
| 88 |
+
if attn.norm_added_k is not None:
|
| 89 |
+
encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)
|
| 90 |
+
|
| 91 |
+
query = torch.cat([encoder_hidden_states_query_proj, query], dim=2)
|
| 92 |
+
key = torch.cat([encoder_hidden_states_key_proj, key], dim=2)
|
| 93 |
+
value = torch.cat([encoder_hidden_states_value_proj, value], dim=2)
|
| 94 |
+
|
| 95 |
+
if image_rotary_emb is not None:
|
| 96 |
+
from diffusers.models.embeddings import apply_rotary_emb
|
| 97 |
+
|
| 98 |
+
query = apply_rotary_emb(query, image_rotary_emb)
|
| 99 |
+
key = apply_rotary_emb(key, image_rotary_emb)
|
| 100 |
+
|
| 101 |
+
hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False)
|
| 102 |
+
|
| 103 |
+
hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
|
| 104 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 105 |
+
|
| 106 |
+
if encoder_hidden_states is not None:
|
| 107 |
+
|
| 108 |
+
encoder_hidden_states, hidden_states = (
|
| 109 |
+
hidden_states[:, : encoder_hidden_states.shape[1]],
|
| 110 |
+
hidden_states[:, encoder_hidden_states.shape[1]:],
|
| 111 |
+
)
|
| 112 |
+
if image_emb is not None:
|
| 113 |
+
hidden_states = hidden_states + self.scale * ip_hidden_states
|
| 114 |
+
|
| 115 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 116 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 117 |
+
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
|
| 118 |
+
|
| 119 |
+
return hidden_states, encoder_hidden_states
|
| 120 |
+
else:
|
| 121 |
+
if image_emb is not None:
|
| 122 |
+
hidden_states = hidden_states + self.scale * ip_hidden_states
|
| 123 |
+
|
| 124 |
+
return hidden_states
|
models/calligrapher.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from transformers import AutoProcessor, SiglipVisionModel
|
| 5 |
+
from models.projection_models import MLPProjModel, QFormerProjModel
|
| 6 |
+
from models.attention_processor import FluxAttnProcessor
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Calligrapher:
|
| 10 |
+
def __init__(self, sd_pipe, image_encoder_path, calligrapher_path, device, num_tokens):
|
| 11 |
+
self.device = device
|
| 12 |
+
self.image_encoder_path = image_encoder_path
|
| 13 |
+
self.calligrapher_path = calligrapher_path
|
| 14 |
+
self.num_tokens = num_tokens
|
| 15 |
+
|
| 16 |
+
self.pipe = sd_pipe.to(self.device)
|
| 17 |
+
self.set_attn_adapter()
|
| 18 |
+
|
| 19 |
+
self.image_encoder = SiglipVisionModel.from_pretrained(image_encoder_path).to(self.device, dtype=torch.bfloat16)
|
| 20 |
+
self.clip_image_processor = AutoProcessor.from_pretrained(self.image_encoder_path)
|
| 21 |
+
self.image_proj_mlp, self.image_proj_qformer = self.init_proj()
|
| 22 |
+
|
| 23 |
+
self.load_models()
|
| 24 |
+
|
| 25 |
+
def init_proj(self):
|
| 26 |
+
image_proj_mlp = MLPProjModel(
|
| 27 |
+
cross_attention_dim=self.pipe.transformer.config.joint_attention_dim,
|
| 28 |
+
id_embeddings_dim=1152,
|
| 29 |
+
num_tokens=self.num_tokens,
|
| 30 |
+
).to(self.device, dtype=torch.bfloat16)
|
| 31 |
+
|
| 32 |
+
image_proj_qformer = QFormerProjModel(
|
| 33 |
+
cross_attention_dim=4096,
|
| 34 |
+
id_embeddings_dim=1152,
|
| 35 |
+
num_tokens=self.num_tokens,
|
| 36 |
+
num_heads=8,
|
| 37 |
+
num_query_tokens=32
|
| 38 |
+
).to(self.device, dtype=torch.bfloat16)
|
| 39 |
+
return image_proj_mlp, image_proj_qformer
|
| 40 |
+
|
| 41 |
+
def set_attn_adapter(self):
|
| 42 |
+
transformer = self.pipe.transformer
|
| 43 |
+
attn_procs = {}
|
| 44 |
+
for name in transformer.attn_processors.keys():
|
| 45 |
+
if name.startswith("transformer_blocks.") or name.startswith("single_transformer_blocks"):
|
| 46 |
+
attn_procs[name] = FluxAttnProcessor(
|
| 47 |
+
hidden_size=transformer.config.num_attention_heads * transformer.config.attention_head_dim,
|
| 48 |
+
cross_attention_dim=transformer.config.joint_attention_dim,
|
| 49 |
+
num_tokens=self.num_tokens,
|
| 50 |
+
).to(self.device, dtype=torch.bfloat16)
|
| 51 |
+
else:
|
| 52 |
+
attn_procs[name] = transformer.attn_processors[name]
|
| 53 |
+
transformer.set_attn_processor(attn_procs)
|
| 54 |
+
|
| 55 |
+
def load_models(self):
|
| 56 |
+
state_dict = torch.load(self.calligrapher_path, map_location="cpu")
|
| 57 |
+
self.image_proj_mlp.load_state_dict(state_dict["image_proj_mlp"], strict=True)
|
| 58 |
+
self.image_proj_qformer.load_state_dict(state_dict["image_proj_qformer"], strict=True)
|
| 59 |
+
target_layers = torch.nn.ModuleList(self.pipe.transformer.attn_processors.values())
|
| 60 |
+
target_layers.load_state_dict(state_dict["attn_adapter"], strict=False)
|
| 61 |
+
|
| 62 |
+
@torch.inference_mode()
|
| 63 |
+
def get_image_embeds(self, pil_image=None, clip_image_embeds=None):
|
| 64 |
+
if pil_image is not None:
|
| 65 |
+
if isinstance(pil_image, Image.Image):
|
| 66 |
+
pil_image = [pil_image]
|
| 67 |
+
clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values
|
| 68 |
+
clip_image_embeds = self.image_encoder(
|
| 69 |
+
clip_image.to(self.device, dtype=self.image_encoder.dtype)).pooler_output
|
| 70 |
+
clip_image_embeds = clip_image_embeds.to(dtype=torch.bfloat16)
|
| 71 |
+
else:
|
| 72 |
+
clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.bfloat16)
|
| 73 |
+
image_prompt_embeds = self.image_proj_mlp(clip_image_embeds) \
|
| 74 |
+
+ self.image_proj_qformer(clip_image_embeds)
|
| 75 |
+
return image_prompt_embeds
|
| 76 |
+
|
| 77 |
+
def set_scale(self, scale):
|
| 78 |
+
for attn_processor in self.pipe.transformer.attn_processors.values():
|
| 79 |
+
if isinstance(attn_processor, FluxAttnProcessor):
|
| 80 |
+
attn_processor.scale = scale
|
| 81 |
+
|
| 82 |
+
def generate(
|
| 83 |
+
self,
|
| 84 |
+
image=None,
|
| 85 |
+
mask_image=None,
|
| 86 |
+
ref_image=None,
|
| 87 |
+
clip_image_embeds=None,
|
| 88 |
+
prompt=None,
|
| 89 |
+
scale=1.0,
|
| 90 |
+
seed=None,
|
| 91 |
+
num_inference_steps=30,
|
| 92 |
+
**kwargs,
|
| 93 |
+
):
|
| 94 |
+
self.set_scale(scale)
|
| 95 |
+
|
| 96 |
+
image_prompt_embeds = self.get_image_embeds(
|
| 97 |
+
pil_image=ref_image, clip_image_embeds=clip_image_embeds
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if seed is None:
|
| 101 |
+
generator = None
|
| 102 |
+
else:
|
| 103 |
+
generator = torch.Generator(self.device).manual_seed(seed)
|
| 104 |
+
|
| 105 |
+
images = self.pipe(
|
| 106 |
+
image=image,
|
| 107 |
+
mask_image=mask_image,
|
| 108 |
+
prompt=prompt,
|
| 109 |
+
image_emb=image_prompt_embeds,
|
| 110 |
+
num_inference_steps=num_inference_steps,
|
| 111 |
+
generator=generator,
|
| 112 |
+
**kwargs,
|
| 113 |
+
).images
|
| 114 |
+
|
| 115 |
+
return images
|
models/projection_models.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class QFormerProjModel(nn.Module):
|
| 6 |
+
def __init__(self,
|
| 7 |
+
cross_attention_dim=4096,
|
| 8 |
+
id_embeddings_dim=1152,
|
| 9 |
+
num_tokens=128,
|
| 10 |
+
num_heads=8,
|
| 11 |
+
num_query_tokens=32):
|
| 12 |
+
super().__init__()
|
| 13 |
+
self.cross_attention_dim = cross_attention_dim
|
| 14 |
+
self.num_tokens = num_tokens
|
| 15 |
+
|
| 16 |
+
self.query_embeds = nn.Parameter(torch.randn(num_tokens, cross_attention_dim))
|
| 17 |
+
|
| 18 |
+
self.id_proj = nn.Sequential(
|
| 19 |
+
nn.Linear(id_embeddings_dim, id_embeddings_dim * 2),
|
| 20 |
+
nn.GELU(),
|
| 21 |
+
nn.Linear(id_embeddings_dim * 2, cross_attention_dim * num_query_tokens)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
self.cross_attn = nn.MultiheadAttention(
|
| 25 |
+
embed_dim=cross_attention_dim,
|
| 26 |
+
num_heads=num_heads,
|
| 27 |
+
batch_first=True
|
| 28 |
+
)
|
| 29 |
+
self.cross_attn_norm = nn.LayerNorm(cross_attention_dim)
|
| 30 |
+
|
| 31 |
+
self.norm = nn.LayerNorm(cross_attention_dim)
|
| 32 |
+
|
| 33 |
+
def forward(self, id_embeds):
|
| 34 |
+
batch_size = id_embeds.size(0)
|
| 35 |
+
|
| 36 |
+
projected = self.id_proj(id_embeds)
|
| 37 |
+
kv = projected.view(batch_size, -1, self.cross_attention_dim)
|
| 38 |
+
|
| 39 |
+
queries = self.query_embeds.unsqueeze(0).expand(batch_size, -1, -1)
|
| 40 |
+
|
| 41 |
+
attn_output, _ = self.cross_attn(
|
| 42 |
+
query=queries,
|
| 43 |
+
key=kv,
|
| 44 |
+
value=kv
|
| 45 |
+
)
|
| 46 |
+
attn_output = self.cross_attn_norm(attn_output + queries)
|
| 47 |
+
|
| 48 |
+
return self.norm(attn_output)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class MLPProjModel(torch.nn.Module):
|
| 52 |
+
def __init__(self,
|
| 53 |
+
cross_attention_dim=768,
|
| 54 |
+
id_embeddings_dim=512,
|
| 55 |
+
num_tokens=4):
|
| 56 |
+
super().__init__()
|
| 57 |
+
self.cross_attention_dim = cross_attention_dim
|
| 58 |
+
self.num_tokens = num_tokens
|
| 59 |
+
|
| 60 |
+
self.proj = torch.nn.Sequential(
|
| 61 |
+
torch.nn.Linear(id_embeddings_dim, id_embeddings_dim * 2),
|
| 62 |
+
torch.nn.GELU(),
|
| 63 |
+
torch.nn.Linear(id_embeddings_dim * 2, cross_attention_dim * num_tokens),
|
| 64 |
+
)
|
| 65 |
+
self.norm = torch.nn.LayerNorm(cross_attention_dim)
|
| 66 |
+
|
| 67 |
+
def forward(self, id_embeds):
|
| 68 |
+
x = self.proj(id_embeds)
|
| 69 |
+
x = x.reshape(-1, self.num_tokens, self.cross_attention_dim)
|
| 70 |
+
x = self.norm(x)
|
| 71 |
+
return x
|
models/transformer_flux_inpainting.py
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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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 |
+
|
| 23 |
+
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 24 |
+
from diffusers.loaders import FluxTransformer2DLoadersMixin, FromOriginalModelMixin, PeftAdapterMixin
|
| 25 |
+
from diffusers.models.attention import FeedForward
|
| 26 |
+
from diffusers.models.attention_processor import (
|
| 27 |
+
Attention,
|
| 28 |
+
AttentionProcessor,
|
| 29 |
+
FluxAttnProcessor2_0,
|
| 30 |
+
FluxAttnProcessor2_0_NPU,
|
| 31 |
+
FusedFluxAttnProcessor2_0,
|
| 32 |
+
)
|
| 33 |
+
from diffusers.models.modeling_utils import ModelMixin
|
| 34 |
+
from diffusers.models.normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle
|
| 35 |
+
from diffusers.utils import USE_PEFT_BACKEND, is_torch_version, logging, scale_lora_layers, unscale_lora_layers
|
| 36 |
+
from diffusers.utils.import_utils import is_torch_npu_available
|
| 37 |
+
from diffusers.utils.torch_utils import maybe_allow_in_graph
|
| 38 |
+
from diffusers.models.embeddings import CombinedTimestepGuidanceTextProjEmbeddings, CombinedTimestepTextProjEmbeddings, FluxPosEmbed
|
| 39 |
+
from diffusers.models.modeling_outputs import Transformer2DModelOutput
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@maybe_allow_in_graph
|
| 46 |
+
class FluxSingleTransformerBlock(nn.Module):
|
| 47 |
+
r"""
|
| 48 |
+
A Transformer block following the MMDiT architecture, introduced in Stable Diffusion 3.
|
| 49 |
+
|
| 50 |
+
Reference: https://arxiv.org/abs/2403.03206
|
| 51 |
+
|
| 52 |
+
Parameters:
|
| 53 |
+
dim (`int`): The number of channels in the input and output.
|
| 54 |
+
num_attention_heads (`int`): The number of heads to use for multi-head attention.
|
| 55 |
+
attention_head_dim (`int`): The number of channels in each head.
|
| 56 |
+
context_pre_only (`bool`): Boolean to determine if we should add some blocks associated with the
|
| 57 |
+
processing of `context` conditions.
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
def __init__(self, dim, num_attention_heads, attention_head_dim, mlp_ratio=4.0):
|
| 61 |
+
super().__init__()
|
| 62 |
+
self.mlp_hidden_dim = int(dim * mlp_ratio)
|
| 63 |
+
|
| 64 |
+
self.norm = AdaLayerNormZeroSingle(dim)
|
| 65 |
+
self.proj_mlp = nn.Linear(dim, self.mlp_hidden_dim)
|
| 66 |
+
self.act_mlp = nn.GELU(approximate="tanh")
|
| 67 |
+
self.proj_out = nn.Linear(dim + self.mlp_hidden_dim, dim)
|
| 68 |
+
|
| 69 |
+
if is_torch_npu_available():
|
| 70 |
+
processor = FluxAttnProcessor2_0_NPU()
|
| 71 |
+
else:
|
| 72 |
+
processor = FluxAttnProcessor2_0()
|
| 73 |
+
self.attn = Attention(
|
| 74 |
+
query_dim=dim,
|
| 75 |
+
cross_attention_dim=None,
|
| 76 |
+
dim_head=attention_head_dim,
|
| 77 |
+
heads=num_attention_heads,
|
| 78 |
+
out_dim=dim,
|
| 79 |
+
bias=True,
|
| 80 |
+
processor=processor,
|
| 81 |
+
qk_norm="rms_norm",
|
| 82 |
+
eps=1e-6,
|
| 83 |
+
pre_only=True,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def forward(
|
| 87 |
+
self,
|
| 88 |
+
hidden_states: torch.Tensor,
|
| 89 |
+
temb: torch.Tensor,
|
| 90 |
+
image_emb=None,
|
| 91 |
+
image_rotary_emb: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
| 92 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 93 |
+
) -> torch.Tensor:
|
| 94 |
+
residual = hidden_states
|
| 95 |
+
norm_hidden_states, gate = self.norm(hidden_states, emb=temb)
|
| 96 |
+
mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states))
|
| 97 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 98 |
+
attn_output = self.attn(
|
| 99 |
+
hidden_states=norm_hidden_states,
|
| 100 |
+
image_rotary_emb=image_rotary_emb,
|
| 101 |
+
image_emb=image_emb,
|
| 102 |
+
**joint_attention_kwargs,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2)
|
| 106 |
+
gate = gate.unsqueeze(1)
|
| 107 |
+
hidden_states = gate * self.proj_out(hidden_states)
|
| 108 |
+
hidden_states = residual + hidden_states
|
| 109 |
+
if hidden_states.dtype == torch.float16:
|
| 110 |
+
hidden_states = hidden_states.clip(-65504, 65504)
|
| 111 |
+
|
| 112 |
+
return hidden_states
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
@maybe_allow_in_graph
|
| 116 |
+
class FluxTransformerBlock(nn.Module):
|
| 117 |
+
r"""
|
| 118 |
+
A Transformer block following the MMDiT architecture, introduced in Stable Diffusion 3.
|
| 119 |
+
|
| 120 |
+
Reference: https://arxiv.org/abs/2403.03206
|
| 121 |
+
|
| 122 |
+
Args:
|
| 123 |
+
dim (`int`):
|
| 124 |
+
The embedding dimension of the block.
|
| 125 |
+
num_attention_heads (`int`):
|
| 126 |
+
The number of attention heads to use.
|
| 127 |
+
attention_head_dim (`int`):
|
| 128 |
+
The number of dimensions to use for each attention head.
|
| 129 |
+
qk_norm (`str`, defaults to `"rms_norm"`):
|
| 130 |
+
The normalization to use for the query and key tensors.
|
| 131 |
+
eps (`float`, defaults to `1e-6`):
|
| 132 |
+
The epsilon value to use for the normalization.
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
def __init__(
|
| 136 |
+
self, dim: int, num_attention_heads: int, attention_head_dim: int, qk_norm: str = "rms_norm", eps: float = 1e-6
|
| 137 |
+
):
|
| 138 |
+
super().__init__()
|
| 139 |
+
|
| 140 |
+
self.norm1 = AdaLayerNormZero(dim)
|
| 141 |
+
|
| 142 |
+
self.norm1_context = AdaLayerNormZero(dim)
|
| 143 |
+
|
| 144 |
+
if hasattr(F, "scaled_dot_product_attention"):
|
| 145 |
+
processor = FluxAttnProcessor2_0()
|
| 146 |
+
else:
|
| 147 |
+
raise ValueError(
|
| 148 |
+
"The current PyTorch version does not support the `scaled_dot_product_attention` function."
|
| 149 |
+
)
|
| 150 |
+
self.attn = Attention(
|
| 151 |
+
query_dim=dim,
|
| 152 |
+
cross_attention_dim=None,
|
| 153 |
+
added_kv_proj_dim=dim,
|
| 154 |
+
dim_head=attention_head_dim,
|
| 155 |
+
heads=num_attention_heads,
|
| 156 |
+
out_dim=dim,
|
| 157 |
+
context_pre_only=False,
|
| 158 |
+
bias=True,
|
| 159 |
+
processor=processor,
|
| 160 |
+
qk_norm=qk_norm,
|
| 161 |
+
eps=eps,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
self.norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 165 |
+
self.ff = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 166 |
+
|
| 167 |
+
self.norm2_context = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
|
| 168 |
+
self.ff_context = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
|
| 169 |
+
|
| 170 |
+
# let chunk size default to None
|
| 171 |
+
self._chunk_size = None
|
| 172 |
+
self._chunk_dim = 0
|
| 173 |
+
|
| 174 |
+
def forward(
|
| 175 |
+
self,
|
| 176 |
+
hidden_states: torch.Tensor,
|
| 177 |
+
encoder_hidden_states: torch.Tensor,
|
| 178 |
+
temb: torch.Tensor,
|
| 179 |
+
image_emb=None,
|
| 180 |
+
image_rotary_emb: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
| 181 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 182 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 183 |
+
norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(hidden_states, emb=temb)
|
| 184 |
+
|
| 185 |
+
norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_context(
|
| 186 |
+
encoder_hidden_states, emb=temb
|
| 187 |
+
)
|
| 188 |
+
joint_attention_kwargs = joint_attention_kwargs or {}
|
| 189 |
+
# Attention.
|
| 190 |
+
attention_outputs = self.attn(
|
| 191 |
+
hidden_states=norm_hidden_states,
|
| 192 |
+
encoder_hidden_states=norm_encoder_hidden_states,
|
| 193 |
+
image_rotary_emb=image_rotary_emb,
|
| 194 |
+
image_emb=image_emb,
|
| 195 |
+
**joint_attention_kwargs,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
if len(attention_outputs) == 2:
|
| 199 |
+
attn_output, context_attn_output = attention_outputs
|
| 200 |
+
elif len(attention_outputs) == 3:
|
| 201 |
+
attn_output, context_attn_output, ip_attn_output = attention_outputs
|
| 202 |
+
|
| 203 |
+
# Process attention outputs for the `hidden_states`.
|
| 204 |
+
attn_output = gate_msa.unsqueeze(1) * attn_output
|
| 205 |
+
hidden_states = hidden_states + attn_output
|
| 206 |
+
|
| 207 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 208 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
|
| 209 |
+
|
| 210 |
+
ff_output = self.ff(norm_hidden_states)
|
| 211 |
+
ff_output = gate_mlp.unsqueeze(1) * ff_output
|
| 212 |
+
|
| 213 |
+
hidden_states = hidden_states + ff_output
|
| 214 |
+
if len(attention_outputs) == 3:
|
| 215 |
+
hidden_states = hidden_states + ip_attn_output
|
| 216 |
+
|
| 217 |
+
# Process attention outputs for the `encoder_hidden_states`.
|
| 218 |
+
|
| 219 |
+
context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output
|
| 220 |
+
encoder_hidden_states = encoder_hidden_states + context_attn_output
|
| 221 |
+
|
| 222 |
+
norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states)
|
| 223 |
+
norm_encoder_hidden_states = norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None]
|
| 224 |
+
|
| 225 |
+
context_ff_output = self.ff_context(norm_encoder_hidden_states)
|
| 226 |
+
encoder_hidden_states = encoder_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output
|
| 227 |
+
if encoder_hidden_states.dtype == torch.float16:
|
| 228 |
+
encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
|
| 229 |
+
|
| 230 |
+
return encoder_hidden_states, hidden_states
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
class FluxTransformer2DModel(
|
| 234 |
+
ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, FluxTransformer2DLoadersMixin
|
| 235 |
+
):
|
| 236 |
+
"""
|
| 237 |
+
The Transformer model introduced in Flux.
|
| 238 |
+
|
| 239 |
+
Reference: https://blackforestlabs.ai/announcing-black-forest-labs/
|
| 240 |
+
|
| 241 |
+
Args:
|
| 242 |
+
patch_size (`int`, defaults to `1`):
|
| 243 |
+
Patch size to turn the input data into small patches.
|
| 244 |
+
in_channels (`int`, defaults to `64`):
|
| 245 |
+
The number of channels in the input.
|
| 246 |
+
out_channels (`int`, *optional*, defaults to `None`):
|
| 247 |
+
The number of channels in the output. If not specified, it defaults to `in_channels`.
|
| 248 |
+
num_layers (`int`, defaults to `19`):
|
| 249 |
+
The number of layers of dual stream DiT blocks to use.
|
| 250 |
+
num_single_layers (`int`, defaults to `38`):
|
| 251 |
+
The number of layers of single stream DiT blocks to use.
|
| 252 |
+
attention_head_dim (`int`, defaults to `128`):
|
| 253 |
+
The number of dimensions to use for each attention head.
|
| 254 |
+
num_attention_heads (`int`, defaults to `24`):
|
| 255 |
+
The number of attention heads to use.
|
| 256 |
+
joint_attention_dim (`int`, defaults to `4096`):
|
| 257 |
+
The number of dimensions to use for the joint attention (embedding/channel dimension of
|
| 258 |
+
`encoder_hidden_states`).
|
| 259 |
+
pooled_projection_dim (`int`, defaults to `768`):
|
| 260 |
+
The number of dimensions to use for the pooled projection.
|
| 261 |
+
guidance_embeds (`bool`, defaults to `False`):
|
| 262 |
+
Whether to use guidance embeddings for guidance-distilled variant of the model.
|
| 263 |
+
axes_dims_rope (`Tuple[int]`, defaults to `(16, 56, 56)`):
|
| 264 |
+
The dimensions to use for the rotary positional embeddings.
|
| 265 |
+
"""
|
| 266 |
+
|
| 267 |
+
_supports_gradient_checkpointing = True
|
| 268 |
+
_no_split_modules = ["FluxTransformerBlock", "FluxSingleTransformerBlock"]
|
| 269 |
+
|
| 270 |
+
@register_to_config
|
| 271 |
+
def __init__(
|
| 272 |
+
self,
|
| 273 |
+
patch_size: int = 1,
|
| 274 |
+
in_channels: int = 64,
|
| 275 |
+
out_channels: Optional[int] = None,
|
| 276 |
+
num_layers: int = 19,
|
| 277 |
+
num_single_layers: int = 38,
|
| 278 |
+
attention_head_dim: int = 128,
|
| 279 |
+
num_attention_heads: int = 24,
|
| 280 |
+
joint_attention_dim: int = 4096,
|
| 281 |
+
pooled_projection_dim: int = 768,
|
| 282 |
+
guidance_embeds: bool = False,
|
| 283 |
+
axes_dims_rope: Tuple[int] = (16, 56, 56),
|
| 284 |
+
):
|
| 285 |
+
super().__init__()
|
| 286 |
+
self.out_channels = out_channels or in_channels
|
| 287 |
+
self.inner_dim = num_attention_heads * attention_head_dim
|
| 288 |
+
|
| 289 |
+
self.pos_embed = FluxPosEmbed(theta=10000, axes_dim=axes_dims_rope)
|
| 290 |
+
|
| 291 |
+
text_time_guidance_cls = (
|
| 292 |
+
CombinedTimestepGuidanceTextProjEmbeddings if guidance_embeds else CombinedTimestepTextProjEmbeddings
|
| 293 |
+
)
|
| 294 |
+
self.time_text_embed = text_time_guidance_cls(
|
| 295 |
+
embedding_dim=self.inner_dim, pooled_projection_dim=pooled_projection_dim
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
self.context_embedder = nn.Linear(joint_attention_dim, self.inner_dim)
|
| 299 |
+
self.x_embedder = nn.Linear(in_channels, self.inner_dim)
|
| 300 |
+
|
| 301 |
+
self.transformer_blocks = nn.ModuleList(
|
| 302 |
+
[
|
| 303 |
+
FluxTransformerBlock(
|
| 304 |
+
dim=self.inner_dim,
|
| 305 |
+
num_attention_heads=num_attention_heads,
|
| 306 |
+
attention_head_dim=attention_head_dim,
|
| 307 |
+
)
|
| 308 |
+
for _ in range(num_layers)
|
| 309 |
+
]
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
self.single_transformer_blocks = nn.ModuleList(
|
| 313 |
+
[
|
| 314 |
+
FluxSingleTransformerBlock(
|
| 315 |
+
dim=self.inner_dim,
|
| 316 |
+
num_attention_heads=num_attention_heads,
|
| 317 |
+
attention_head_dim=attention_head_dim,
|
| 318 |
+
)
|
| 319 |
+
for _ in range(num_single_layers)
|
| 320 |
+
]
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
self.norm_out = AdaLayerNormContinuous(self.inner_dim, self.inner_dim, elementwise_affine=False, eps=1e-6)
|
| 324 |
+
self.proj_out = nn.Linear(self.inner_dim, patch_size * patch_size * self.out_channels, bias=True)
|
| 325 |
+
|
| 326 |
+
self.gradient_checkpointing = False
|
| 327 |
+
|
| 328 |
+
@property
|
| 329 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.attn_processors
|
| 330 |
+
def attn_processors(self) -> Dict[str, AttentionProcessor]:
|
| 331 |
+
r"""
|
| 332 |
+
Returns:
|
| 333 |
+
`dict` of attention processors: A dictionary containing all attention processors used in the model with
|
| 334 |
+
indexed by its weight name.
|
| 335 |
+
"""
|
| 336 |
+
# set recursively
|
| 337 |
+
processors = {}
|
| 338 |
+
|
| 339 |
+
def fn_recursive_add_processors(name: str, module: torch.nn.Module, processors: Dict[str, AttentionProcessor]):
|
| 340 |
+
if hasattr(module, "get_processor"):
|
| 341 |
+
processors[f"{name}.processor"] = module.get_processor()
|
| 342 |
+
|
| 343 |
+
for sub_name, child in module.named_children():
|
| 344 |
+
fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)
|
| 345 |
+
|
| 346 |
+
return processors
|
| 347 |
+
|
| 348 |
+
for name, module in self.named_children():
|
| 349 |
+
fn_recursive_add_processors(name, module, processors)
|
| 350 |
+
|
| 351 |
+
return processors
|
| 352 |
+
|
| 353 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor
|
| 354 |
+
def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
|
| 355 |
+
r"""
|
| 356 |
+
Sets the attention processor to use to compute attention.
|
| 357 |
+
|
| 358 |
+
Parameters:
|
| 359 |
+
processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
|
| 360 |
+
The instantiated processor class or a dictionary of processor classes that will be set as the processor
|
| 361 |
+
for **all** `Attention` layers.
|
| 362 |
+
|
| 363 |
+
If `processor` is a dict, the key needs to define the path to the corresponding cross attention
|
| 364 |
+
processor. This is strongly recommended when setting trainable attention processors.
|
| 365 |
+
|
| 366 |
+
"""
|
| 367 |
+
count = len(self.attn_processors.keys())
|
| 368 |
+
|
| 369 |
+
if isinstance(processor, dict) and len(processor) != count:
|
| 370 |
+
raise ValueError(
|
| 371 |
+
f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
|
| 372 |
+
f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
|
| 376 |
+
if hasattr(module, "set_processor"):
|
| 377 |
+
if not isinstance(processor, dict):
|
| 378 |
+
module.set_processor(processor)
|
| 379 |
+
else:
|
| 380 |
+
module.set_processor(processor.pop(f"{name}.processor"))
|
| 381 |
+
|
| 382 |
+
for sub_name, child in module.named_children():
|
| 383 |
+
fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
|
| 384 |
+
|
| 385 |
+
for name, module in self.named_children():
|
| 386 |
+
fn_recursive_attn_processor(name, module, processor)
|
| 387 |
+
|
| 388 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.fuse_qkv_projections with FusedAttnProcessor2_0->FusedFluxAttnProcessor2_0
|
| 389 |
+
def fuse_qkv_projections(self):
|
| 390 |
+
"""
|
| 391 |
+
Enables fused QKV projections. For self-attention modules, all projection matrices (i.e., query, key, value)
|
| 392 |
+
are fused. For cross-attention modules, key and value projection matrices are fused.
|
| 393 |
+
|
| 394 |
+
<Tip warning={true}>
|
| 395 |
+
|
| 396 |
+
This API is 🧪 experimental.
|
| 397 |
+
|
| 398 |
+
</Tip>
|
| 399 |
+
"""
|
| 400 |
+
self.original_attn_processors = None
|
| 401 |
+
|
| 402 |
+
for _, attn_processor in self.attn_processors.items():
|
| 403 |
+
if "Added" in str(attn_processor.__class__.__name__):
|
| 404 |
+
raise ValueError("`fuse_qkv_projections()` is not supported for models having added KV projections.")
|
| 405 |
+
|
| 406 |
+
self.original_attn_processors = self.attn_processors
|
| 407 |
+
|
| 408 |
+
for module in self.modules():
|
| 409 |
+
if isinstance(module, Attention):
|
| 410 |
+
module.fuse_projections(fuse=True)
|
| 411 |
+
|
| 412 |
+
self.set_attn_processor(FusedFluxAttnProcessor2_0())
|
| 413 |
+
|
| 414 |
+
# Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.unfuse_qkv_projections
|
| 415 |
+
def unfuse_qkv_projections(self):
|
| 416 |
+
"""Disables the fused QKV projection if enabled.
|
| 417 |
+
|
| 418 |
+
<Tip warning={true}>
|
| 419 |
+
|
| 420 |
+
This API is 🧪 experimental.
|
| 421 |
+
|
| 422 |
+
</Tip>
|
| 423 |
+
|
| 424 |
+
"""
|
| 425 |
+
if self.original_attn_processors is not None:
|
| 426 |
+
self.set_attn_processor(self.original_attn_processors)
|
| 427 |
+
|
| 428 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
| 429 |
+
if hasattr(module, "gradient_checkpointing"):
|
| 430 |
+
module.gradient_checkpointing = value
|
| 431 |
+
|
| 432 |
+
def forward(
|
| 433 |
+
self,
|
| 434 |
+
hidden_states: torch.Tensor,
|
| 435 |
+
encoder_hidden_states: torch.Tensor = None,
|
| 436 |
+
image_emb: torch.FloatTensor = None,
|
| 437 |
+
pooled_projections: torch.Tensor = None,
|
| 438 |
+
timestep: torch.LongTensor = None,
|
| 439 |
+
img_ids: torch.Tensor = None,
|
| 440 |
+
txt_ids: torch.Tensor = None,
|
| 441 |
+
guidance: torch.Tensor = None,
|
| 442 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 443 |
+
controlnet_block_samples=None,
|
| 444 |
+
controlnet_single_block_samples=None,
|
| 445 |
+
return_dict: bool = True,
|
| 446 |
+
controlnet_blocks_repeat: bool = False,
|
| 447 |
+
) -> Union[torch.Tensor, Transformer2DModelOutput]:
|
| 448 |
+
"""
|
| 449 |
+
The [`FluxTransformer2DModel`] forward method.
|
| 450 |
+
|
| 451 |
+
Args:
|
| 452 |
+
hidden_states (`torch.Tensor` of shape `(batch_size, image_sequence_length, in_channels)`):
|
| 453 |
+
Input `hidden_states`.
|
| 454 |
+
encoder_hidden_states (`torch.Tensor` of shape `(batch_size, text_sequence_length, joint_attention_dim)`):
|
| 455 |
+
Conditional embeddings (embeddings computed from the input conditions such as prompts) to use.
|
| 456 |
+
pooled_projections (`torch.Tensor` of shape `(batch_size, projection_dim)`): Embeddings projected
|
| 457 |
+
from the embeddings of input conditions.
|
| 458 |
+
timestep ( `torch.LongTensor`):
|
| 459 |
+
Used to indicate denoising step.
|
| 460 |
+
block_controlnet_hidden_states: (`list` of `torch.Tensor`):
|
| 461 |
+
A list of tensors that if specified are added to the residuals of transformer blocks.
|
| 462 |
+
joint_attention_kwargs (`dict`, *optional*):
|
| 463 |
+
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
| 464 |
+
`self.processor` in
|
| 465 |
+
[diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
| 466 |
+
return_dict (`bool`, *optional*, defaults to `True`):
|
| 467 |
+
Whether or not to return a [`~models.transformer_2d.Transformer2DModelOutput`] instead of a plain
|
| 468 |
+
tuple.
|
| 469 |
+
|
| 470 |
+
Returns:
|
| 471 |
+
If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a
|
| 472 |
+
`tuple` where the first element is the sample tensor.
|
| 473 |
+
"""
|
| 474 |
+
|
| 475 |
+
if joint_attention_kwargs is not None:
|
| 476 |
+
joint_attention_kwargs = joint_attention_kwargs.copy()
|
| 477 |
+
lora_scale = joint_attention_kwargs.pop("scale", 1.0)
|
| 478 |
+
else:
|
| 479 |
+
lora_scale = 1.0
|
| 480 |
+
|
| 481 |
+
if USE_PEFT_BACKEND:
|
| 482 |
+
# weight the lora layers by setting `lora_scale` for each PEFT layer
|
| 483 |
+
scale_lora_layers(self, lora_scale)
|
| 484 |
+
else:
|
| 485 |
+
if joint_attention_kwargs is not None and joint_attention_kwargs.get("scale", None) is not None:
|
| 486 |
+
logger.warning(
|
| 487 |
+
"Passing `scale` via `joint_attention_kwargs` when not using the PEFT backend is ineffective."
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
hidden_states = self.x_embedder(hidden_states)
|
| 491 |
+
|
| 492 |
+
timestep = timestep.to(hidden_states.dtype) * 1000
|
| 493 |
+
if guidance is not None:
|
| 494 |
+
guidance = guidance.to(hidden_states.dtype) * 1000
|
| 495 |
+
else:
|
| 496 |
+
guidance = None
|
| 497 |
+
|
| 498 |
+
temb = (
|
| 499 |
+
self.time_text_embed(timestep, pooled_projections)
|
| 500 |
+
if guidance is None
|
| 501 |
+
else self.time_text_embed(timestep, guidance, pooled_projections)
|
| 502 |
+
)
|
| 503 |
+
encoder_hidden_states = self.context_embedder(encoder_hidden_states)
|
| 504 |
+
|
| 505 |
+
if txt_ids.ndim == 3:
|
| 506 |
+
logger.warning(
|
| 507 |
+
"Passing `txt_ids` 3d torch.Tensor is deprecated."
|
| 508 |
+
"Please remove the batch dimension and pass it as a 2d torch Tensor"
|
| 509 |
+
)
|
| 510 |
+
txt_ids = txt_ids[0]
|
| 511 |
+
if img_ids.ndim == 3:
|
| 512 |
+
logger.warning(
|
| 513 |
+
"Passing `img_ids` 3d torch.Tensor is deprecated."
|
| 514 |
+
"Please remove the batch dimension and pass it as a 2d torch Tensor"
|
| 515 |
+
)
|
| 516 |
+
img_ids = img_ids[0]
|
| 517 |
+
|
| 518 |
+
ids = torch.cat((txt_ids, img_ids), dim=0)
|
| 519 |
+
image_rotary_emb = self.pos_embed(ids)
|
| 520 |
+
|
| 521 |
+
if joint_attention_kwargs is not None and "ip_adapter_image_embeds" in joint_attention_kwargs:
|
| 522 |
+
ip_adapter_image_embeds = joint_attention_kwargs.pop("ip_adapter_image_embeds")
|
| 523 |
+
ip_hidden_states = self.encoder_hid_proj(ip_adapter_image_embeds)
|
| 524 |
+
joint_attention_kwargs.update({"ip_hidden_states": ip_hidden_states})
|
| 525 |
+
|
| 526 |
+
for index_block, block in enumerate(self.transformer_blocks):
|
| 527 |
+
if torch.is_grad_enabled() and self.gradient_checkpointing:
|
| 528 |
+
|
| 529 |
+
def create_custom_forward(module, return_dict=None):
|
| 530 |
+
def custom_forward(*inputs):
|
| 531 |
+
if return_dict is not None:
|
| 532 |
+
return module(*inputs, return_dict=return_dict)
|
| 533 |
+
else:
|
| 534 |
+
return module(*inputs)
|
| 535 |
+
|
| 536 |
+
return custom_forward
|
| 537 |
+
|
| 538 |
+
ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
|
| 539 |
+
encoder_hidden_states, hidden_states = torch.utils.checkpoint.checkpoint(
|
| 540 |
+
create_custom_forward(block),
|
| 541 |
+
hidden_states,
|
| 542 |
+
encoder_hidden_states,
|
| 543 |
+
temb,
|
| 544 |
+
image_emb,
|
| 545 |
+
image_rotary_emb,
|
| 546 |
+
**ckpt_kwargs,
|
| 547 |
+
)
|
| 548 |
+
|
| 549 |
+
else:
|
| 550 |
+
encoder_hidden_states, hidden_states = block(
|
| 551 |
+
hidden_states=hidden_states,
|
| 552 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 553 |
+
temb=temb,
|
| 554 |
+
image_emb=image_emb,
|
| 555 |
+
image_rotary_emb=image_rotary_emb,
|
| 556 |
+
joint_attention_kwargs=joint_attention_kwargs,
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
+
# controlnet residual
|
| 560 |
+
if controlnet_block_samples is not None:
|
| 561 |
+
interval_control = len(self.transformer_blocks) / len(controlnet_block_samples)
|
| 562 |
+
interval_control = int(np.ceil(interval_control))
|
| 563 |
+
# For Xlabs ControlNet.
|
| 564 |
+
if controlnet_blocks_repeat:
|
| 565 |
+
hidden_states = (
|
| 566 |
+
hidden_states + controlnet_block_samples[index_block % len(controlnet_block_samples)]
|
| 567 |
+
)
|
| 568 |
+
else:
|
| 569 |
+
hidden_states = hidden_states + controlnet_block_samples[index_block // interval_control]
|
| 570 |
+
hidden_states = torch.cat([encoder_hidden_states, hidden_states], dim=1)
|
| 571 |
+
|
| 572 |
+
for index_block, block in enumerate(self.single_transformer_blocks):
|
| 573 |
+
if torch.is_grad_enabled() and self.gradient_checkpointing:
|
| 574 |
+
|
| 575 |
+
def create_custom_forward(module, return_dict=None):
|
| 576 |
+
def custom_forward(*inputs):
|
| 577 |
+
if return_dict is not None:
|
| 578 |
+
return module(*inputs, return_dict=return_dict)
|
| 579 |
+
else:
|
| 580 |
+
return module(*inputs)
|
| 581 |
+
|
| 582 |
+
return custom_forward
|
| 583 |
+
|
| 584 |
+
ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
|
| 585 |
+
hidden_states = torch.utils.checkpoint.checkpoint(
|
| 586 |
+
create_custom_forward(block),
|
| 587 |
+
hidden_states,
|
| 588 |
+
temb,
|
| 589 |
+
image_emb,
|
| 590 |
+
image_rotary_emb,
|
| 591 |
+
**ckpt_kwargs,
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
else:
|
| 595 |
+
hidden_states = block(
|
| 596 |
+
hidden_states=hidden_states,
|
| 597 |
+
temb=temb,
|
| 598 |
+
image_emb=image_emb,
|
| 599 |
+
image_rotary_emb=image_rotary_emb,
|
| 600 |
+
joint_attention_kwargs=joint_attention_kwargs,
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
# controlnet residual
|
| 604 |
+
if controlnet_single_block_samples is not None:
|
| 605 |
+
interval_control = len(self.single_transformer_blocks) / len(controlnet_single_block_samples)
|
| 606 |
+
interval_control = int(np.ceil(interval_control))
|
| 607 |
+
hidden_states[:, encoder_hidden_states.shape[1] :, ...] = (
|
| 608 |
+
hidden_states[:, encoder_hidden_states.shape[1] :, ...]
|
| 609 |
+
+ controlnet_single_block_samples[index_block // interval_control]
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
hidden_states = hidden_states[:, encoder_hidden_states.shape[1] :, ...]
|
| 613 |
+
|
| 614 |
+
hidden_states = self.norm_out(hidden_states, temb)
|
| 615 |
+
output = self.proj_out(hidden_states)
|
| 616 |
+
|
| 617 |
+
if USE_PEFT_BACKEND:
|
| 618 |
+
# remove `lora_scale` from each PEFT layer
|
| 619 |
+
unscale_lora_layers(self, lora_scale)
|
| 620 |
+
|
| 621 |
+
if not return_dict:
|
| 622 |
+
return (output,)
|
| 623 |
+
|
| 624 |
+
return Transformer2DModelOutput(sample=output)
|
pipeline_calligrapher.py
ADDED
|
@@ -0,0 +1,972 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Black Forest Labs and The HuggingFace 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 |
+
import inspect
|
| 16 |
+
from typing import Any, Callable, Dict, List, Optional, Union
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import torch
|
| 20 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
| 21 |
+
|
| 22 |
+
from diffusers.image_processor import VaeImageProcessor
|
| 23 |
+
from diffusers.loaders import FluxLoraLoaderMixin, FromSingleFileMixin, TextualInversionLoaderMixin
|
| 24 |
+
from diffusers.models.autoencoders import AutoencoderKL
|
| 25 |
+
from models.transformer_flux_inpainting import FluxTransformer2DModel
|
| 26 |
+
from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
|
| 27 |
+
from diffusers.utils import (
|
| 28 |
+
USE_PEFT_BACKEND,
|
| 29 |
+
is_torch_xla_available,
|
| 30 |
+
logging,
|
| 31 |
+
replace_example_docstring,
|
| 32 |
+
scale_lora_layers,
|
| 33 |
+
unscale_lora_layers,
|
| 34 |
+
)
|
| 35 |
+
from diffusers.utils.torch_utils import randn_tensor
|
| 36 |
+
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
|
| 37 |
+
from diffusers.pipelines.flux.pipeline_output import FluxPipelineOutput
|
| 38 |
+
|
| 39 |
+
if is_torch_xla_available():
|
| 40 |
+
import torch_xla.core.xla_model as xm
|
| 41 |
+
|
| 42 |
+
XLA_AVAILABLE = True
|
| 43 |
+
else:
|
| 44 |
+
XLA_AVAILABLE = False
|
| 45 |
+
|
| 46 |
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
| 47 |
+
|
| 48 |
+
EXAMPLE_DOC_STRING = """
|
| 49 |
+
Examples:
|
| 50 |
+
```py
|
| 51 |
+
>>> import torch
|
| 52 |
+
>>> from diffusers import FluxFillPipeline
|
| 53 |
+
>>> from diffusers.utils import load_image
|
| 54 |
+
|
| 55 |
+
>>> image = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/cup.png")
|
| 56 |
+
>>> mask = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/cup_mask.png")
|
| 57 |
+
|
| 58 |
+
>>> pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16)
|
| 59 |
+
>>> pipe.enable_model_cpu_offload() # save some VRAM by offloading the model to CPU
|
| 60 |
+
|
| 61 |
+
>>> image = pipe(
|
| 62 |
+
... prompt="a white paper cup",
|
| 63 |
+
... image=image,
|
| 64 |
+
... mask_image=mask,
|
| 65 |
+
... height=1632,
|
| 66 |
+
... width=1232,
|
| 67 |
+
... guidance_scale=30,
|
| 68 |
+
... num_inference_steps=50,
|
| 69 |
+
... max_sequence_length=512,
|
| 70 |
+
... generator=torch.Generator("cpu").manual_seed(0),
|
| 71 |
+
... ).images[0]
|
| 72 |
+
>>> image.save("flux_fill.png")
|
| 73 |
+
```
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.calculate_shift
|
| 78 |
+
def calculate_shift(
|
| 79 |
+
image_seq_len,
|
| 80 |
+
base_seq_len: int = 256,
|
| 81 |
+
max_seq_len: int = 4096,
|
| 82 |
+
base_shift: float = 0.5,
|
| 83 |
+
max_shift: float = 1.16,
|
| 84 |
+
):
|
| 85 |
+
m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
|
| 86 |
+
b = base_shift - m * base_seq_len
|
| 87 |
+
mu = image_seq_len * m + b
|
| 88 |
+
return mu
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
| 92 |
+
def retrieve_timesteps(
|
| 93 |
+
scheduler,
|
| 94 |
+
num_inference_steps: Optional[int] = None,
|
| 95 |
+
device: Optional[Union[str, torch.device]] = None,
|
| 96 |
+
timesteps: Optional[List[int]] = None,
|
| 97 |
+
sigmas: Optional[List[float]] = None,
|
| 98 |
+
**kwargs,
|
| 99 |
+
):
|
| 100 |
+
r"""
|
| 101 |
+
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
| 102 |
+
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
| 103 |
+
|
| 104 |
+
Args:
|
| 105 |
+
scheduler (`SchedulerMixin`):
|
| 106 |
+
The scheduler to get timesteps from.
|
| 107 |
+
num_inference_steps (`int`):
|
| 108 |
+
The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
|
| 109 |
+
must be `None`.
|
| 110 |
+
device (`str` or `torch.device`, *optional*):
|
| 111 |
+
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
| 112 |
+
timesteps (`List[int]`, *optional*):
|
| 113 |
+
Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
|
| 114 |
+
`num_inference_steps` and `sigmas` must be `None`.
|
| 115 |
+
sigmas (`List[float]`, *optional*):
|
| 116 |
+
Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
|
| 117 |
+
`num_inference_steps` and `timesteps` must be `None`.
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
| 121 |
+
second element is the number of inference steps.
|
| 122 |
+
"""
|
| 123 |
+
if timesteps is not None and sigmas is not None:
|
| 124 |
+
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
| 125 |
+
if timesteps is not None:
|
| 126 |
+
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
| 127 |
+
if not accepts_timesteps:
|
| 128 |
+
raise ValueError(
|
| 129 |
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
| 130 |
+
f" timestep schedules. Please check whether you are using the correct scheduler."
|
| 131 |
+
)
|
| 132 |
+
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
| 133 |
+
timesteps = scheduler.timesteps
|
| 134 |
+
num_inference_steps = len(timesteps)
|
| 135 |
+
elif sigmas is not None:
|
| 136 |
+
accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
| 137 |
+
if not accept_sigmas:
|
| 138 |
+
raise ValueError(
|
| 139 |
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
| 140 |
+
f" sigmas schedules. Please check whether you are using the correct scheduler."
|
| 141 |
+
)
|
| 142 |
+
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
| 143 |
+
timesteps = scheduler.timesteps
|
| 144 |
+
num_inference_steps = len(timesteps)
|
| 145 |
+
else:
|
| 146 |
+
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
| 147 |
+
timesteps = scheduler.timesteps
|
| 148 |
+
return timesteps, num_inference_steps
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
|
| 152 |
+
def retrieve_latents(
|
| 153 |
+
encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
|
| 154 |
+
):
|
| 155 |
+
if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
|
| 156 |
+
return encoder_output.latent_dist.sample(generator)
|
| 157 |
+
elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
|
| 158 |
+
return encoder_output.latent_dist.mode()
|
| 159 |
+
elif hasattr(encoder_output, "latents"):
|
| 160 |
+
return encoder_output.latents
|
| 161 |
+
else:
|
| 162 |
+
raise AttributeError("Could not access latents of provided encoder_output")
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
class CalligrapherPipeline(
|
| 166 |
+
DiffusionPipeline,
|
| 167 |
+
FluxLoraLoaderMixin,
|
| 168 |
+
FromSingleFileMixin,
|
| 169 |
+
TextualInversionLoaderMixin,
|
| 170 |
+
):
|
| 171 |
+
r"""
|
| 172 |
+
The Flux Fill pipeline for image inpainting/outpainting.
|
| 173 |
+
|
| 174 |
+
Reference: https://blackforestlabs.ai/flux-1-tools/
|
| 175 |
+
|
| 176 |
+
Args:
|
| 177 |
+
transformer ([`FluxTransformer2DModel`]):
|
| 178 |
+
Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
|
| 179 |
+
scheduler ([`FlowMatchEulerDiscreteScheduler`]):
|
| 180 |
+
A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
|
| 181 |
+
vae ([`AutoencoderKL`]):
|
| 182 |
+
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
|
| 183 |
+
text_encoder ([`CLIPTextModel`]):
|
| 184 |
+
[CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically
|
| 185 |
+
the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant.
|
| 186 |
+
text_encoder_2 ([`T5EncoderModel`]):
|
| 187 |
+
[T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically
|
| 188 |
+
the [google/t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl) variant.
|
| 189 |
+
tokenizer (`CLIPTokenizer`):
|
| 190 |
+
Tokenizer of class
|
| 191 |
+
[CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
|
| 192 |
+
tokenizer_2 (`T5TokenizerFast`):
|
| 193 |
+
Second Tokenizer of class
|
| 194 |
+
[T5TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5TokenizerFast).
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
model_cpu_offload_seq = "text_encoder->text_encoder_2->transformer->vae"
|
| 198 |
+
_optional_components = []
|
| 199 |
+
_callback_tensor_inputs = ["latents", "prompt_embeds"]
|
| 200 |
+
|
| 201 |
+
def __init__(
|
| 202 |
+
self,
|
| 203 |
+
scheduler: FlowMatchEulerDiscreteScheduler,
|
| 204 |
+
vae: AutoencoderKL,
|
| 205 |
+
text_encoder: CLIPTextModel,
|
| 206 |
+
tokenizer: CLIPTokenizer,
|
| 207 |
+
text_encoder_2: T5EncoderModel,
|
| 208 |
+
tokenizer_2: T5TokenizerFast,
|
| 209 |
+
transformer: FluxTransformer2DModel,
|
| 210 |
+
):
|
| 211 |
+
super().__init__()
|
| 212 |
+
|
| 213 |
+
print('Using scheduler: ', scheduler)
|
| 214 |
+
|
| 215 |
+
self.register_modules(
|
| 216 |
+
vae=vae,
|
| 217 |
+
text_encoder=text_encoder,
|
| 218 |
+
text_encoder_2=text_encoder_2,
|
| 219 |
+
tokenizer=tokenizer,
|
| 220 |
+
tokenizer_2=tokenizer_2,
|
| 221 |
+
transformer=transformer,
|
| 222 |
+
scheduler=scheduler,
|
| 223 |
+
)
|
| 224 |
+
self.vae_scale_factor = (
|
| 225 |
+
2 ** (len(self.vae.config.block_out_channels) - 1) if hasattr(self, "vae") and self.vae is not None else 8
|
| 226 |
+
)
|
| 227 |
+
# Flux latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible
|
| 228 |
+
# by the patch size. So the vae scale factor is multiplied by the patch size to account for this
|
| 229 |
+
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2)
|
| 230 |
+
self.mask_processor = VaeImageProcessor(
|
| 231 |
+
vae_scale_factor=self.vae_scale_factor * 2,
|
| 232 |
+
vae_latent_channels=self.vae.config.latent_channels,
|
| 233 |
+
do_normalize=False,
|
| 234 |
+
do_binarize=True,
|
| 235 |
+
do_convert_grayscale=True,
|
| 236 |
+
)
|
| 237 |
+
self.tokenizer_max_length = (
|
| 238 |
+
self.tokenizer.model_max_length if hasattr(self, "tokenizer") and self.tokenizer is not None else 77
|
| 239 |
+
)
|
| 240 |
+
self.default_sample_size = 128
|
| 241 |
+
|
| 242 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline._get_t5_prompt_embeds
|
| 243 |
+
def _get_t5_prompt_embeds(
|
| 244 |
+
self,
|
| 245 |
+
prompt: Union[str, List[str]] = None,
|
| 246 |
+
num_images_per_prompt: int = 1,
|
| 247 |
+
max_sequence_length: int = 512,
|
| 248 |
+
device: Optional[torch.device] = None,
|
| 249 |
+
dtype: Optional[torch.dtype] = None,
|
| 250 |
+
):
|
| 251 |
+
device = device or self._execution_device
|
| 252 |
+
dtype = dtype or self.text_encoder.dtype
|
| 253 |
+
|
| 254 |
+
prompt = [prompt] if isinstance(prompt, str) else prompt
|
| 255 |
+
batch_size = len(prompt)
|
| 256 |
+
|
| 257 |
+
if isinstance(self, TextualInversionLoaderMixin):
|
| 258 |
+
prompt = self.maybe_convert_prompt(prompt, self.tokenizer_2)
|
| 259 |
+
|
| 260 |
+
text_inputs = self.tokenizer_2(
|
| 261 |
+
prompt,
|
| 262 |
+
padding="max_length",
|
| 263 |
+
max_length=max_sequence_length,
|
| 264 |
+
truncation=True,
|
| 265 |
+
return_length=False,
|
| 266 |
+
return_overflowing_tokens=False,
|
| 267 |
+
return_tensors="pt",
|
| 268 |
+
)
|
| 269 |
+
text_input_ids = text_inputs.input_ids
|
| 270 |
+
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids
|
| 271 |
+
|
| 272 |
+
if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
|
| 273 |
+
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1: -1])
|
| 274 |
+
logger.warning(
|
| 275 |
+
"The following part of your input was truncated because `max_sequence_length` is set to "
|
| 276 |
+
f" {max_sequence_length} tokens: {removed_text}"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
prompt_embeds = self.text_encoder_2(text_input_ids.to(device), output_hidden_states=False)[0]
|
| 280 |
+
|
| 281 |
+
dtype = self.text_encoder_2.dtype
|
| 282 |
+
prompt_embeds = prompt_embeds.to(dtype=dtype, device=device)
|
| 283 |
+
|
| 284 |
+
_, seq_len, _ = prompt_embeds.shape
|
| 285 |
+
|
| 286 |
+
# duplicate text embeddings and attention mask for each generation per prompt, using mps friendly method
|
| 287 |
+
prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
|
| 288 |
+
prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
|
| 289 |
+
|
| 290 |
+
return prompt_embeds
|
| 291 |
+
|
| 292 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline._get_clip_prompt_embeds
|
| 293 |
+
def _get_clip_prompt_embeds(
|
| 294 |
+
self,
|
| 295 |
+
prompt: Union[str, List[str]],
|
| 296 |
+
num_images_per_prompt: int = 1,
|
| 297 |
+
device: Optional[torch.device] = None,
|
| 298 |
+
):
|
| 299 |
+
device = device or self._execution_device
|
| 300 |
+
|
| 301 |
+
prompt = [prompt] if isinstance(prompt, str) else prompt
|
| 302 |
+
batch_size = len(prompt)
|
| 303 |
+
|
| 304 |
+
if isinstance(self, TextualInversionLoaderMixin):
|
| 305 |
+
prompt = self.maybe_convert_prompt(prompt, self.tokenizer)
|
| 306 |
+
|
| 307 |
+
text_inputs = self.tokenizer(
|
| 308 |
+
prompt,
|
| 309 |
+
padding="max_length",
|
| 310 |
+
max_length=self.tokenizer_max_length,
|
| 311 |
+
truncation=True,
|
| 312 |
+
return_overflowing_tokens=False,
|
| 313 |
+
return_length=False,
|
| 314 |
+
return_tensors="pt",
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
text_input_ids = text_inputs.input_ids
|
| 318 |
+
untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
|
| 319 |
+
if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
|
| 320 |
+
removed_text = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1: -1])
|
| 321 |
+
logger.warning(
|
| 322 |
+
"The following part of your input was truncated because CLIP can only handle sequences up to"
|
| 323 |
+
f" {self.tokenizer_max_length} tokens: {removed_text}"
|
| 324 |
+
)
|
| 325 |
+
prompt_embeds = self.text_encoder(text_input_ids.to(device), output_hidden_states=False)
|
| 326 |
+
|
| 327 |
+
# Use pooled output of CLIPTextModel
|
| 328 |
+
prompt_embeds = prompt_embeds.pooler_output
|
| 329 |
+
prompt_embeds = prompt_embeds.to(dtype=self.text_encoder.dtype, device=device)
|
| 330 |
+
|
| 331 |
+
# duplicate text embeddings for each generation per prompt, using mps friendly method
|
| 332 |
+
prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt)
|
| 333 |
+
prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, -1)
|
| 334 |
+
|
| 335 |
+
return prompt_embeds
|
| 336 |
+
|
| 337 |
+
def prepare_mask_latents(
|
| 338 |
+
self,
|
| 339 |
+
mask,
|
| 340 |
+
masked_image,
|
| 341 |
+
batch_size,
|
| 342 |
+
num_channels_latents,
|
| 343 |
+
num_images_per_prompt,
|
| 344 |
+
height,
|
| 345 |
+
width,
|
| 346 |
+
dtype,
|
| 347 |
+
device,
|
| 348 |
+
generator
|
| 349 |
+
):
|
| 350 |
+
# 1. calculate the height and width of the latents
|
| 351 |
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
| 352 |
+
# latent height and width to be divisible by 2.
|
| 353 |
+
height = 2 * (int(height) // (self.vae_scale_factor * 2))
|
| 354 |
+
width = 2 * (int(width) // (self.vae_scale_factor * 2))
|
| 355 |
+
# 2. encode the masked image
|
| 356 |
+
if masked_image.shape[1] == num_channels_latents:
|
| 357 |
+
masked_image_latents = masked_image
|
| 358 |
+
else:
|
| 359 |
+
masked_image_latents = retrieve_latents(self.vae.encode(masked_image), generator=generator)
|
| 360 |
+
|
| 361 |
+
masked_image_latents = (masked_image_latents - self.vae.config.shift_factor) * self.vae.config.scaling_factor
|
| 362 |
+
masked_image_latents = masked_image_latents.to(device=device, dtype=dtype)
|
| 363 |
+
|
| 364 |
+
# 3. duplicate mask and masked_image_latents for each generation per prompt, using mps friendly method
|
| 365 |
+
batch_size = batch_size * num_images_per_prompt
|
| 366 |
+
if mask.shape[0] < batch_size:
|
| 367 |
+
if not batch_size % mask.shape[0] == 0:
|
| 368 |
+
raise ValueError(
|
| 369 |
+
"The passed mask and the required batch size don't match. Masks are supposed to be duplicated to"
|
| 370 |
+
f" a total batch size of {batch_size}, but {mask.shape[0]} masks were passed. Make sure the number"
|
| 371 |
+
" of masks that you pass is divisible by the total requested batch size."
|
| 372 |
+
)
|
| 373 |
+
mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1)
|
| 374 |
+
if masked_image_latents.shape[0] < batch_size:
|
| 375 |
+
if not batch_size % masked_image_latents.shape[0] == 0:
|
| 376 |
+
raise ValueError(
|
| 377 |
+
"The passed images and the required batch size don't match. Images are supposed to be duplicated"
|
| 378 |
+
f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed."
|
| 379 |
+
" Make sure the number of images that you pass is divisible by the total requested batch size."
|
| 380 |
+
)
|
| 381 |
+
masked_image_latents = masked_image_latents.repeat(batch_size // masked_image_latents.shape[0], 1, 1, 1)
|
| 382 |
+
|
| 383 |
+
# 4. pack the masked_image_latents
|
| 384 |
+
# batch_size, num_channels_latents, height, width -> batch_size, height//2 * width//2 , num_channels_latents*4
|
| 385 |
+
masked_image_latents = self._pack_latents(
|
| 386 |
+
masked_image_latents,
|
| 387 |
+
batch_size,
|
| 388 |
+
num_channels_latents,
|
| 389 |
+
height,
|
| 390 |
+
width,
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
# 5.resize mask to latents shape we we concatenate the mask to the latents
|
| 394 |
+
mask = mask[:, 0, :, :] # batch_size, 8 * height, 8 * width (mask has not been 8x compressed)
|
| 395 |
+
mask = mask.view(
|
| 396 |
+
batch_size, height, self.vae_scale_factor, width, self.vae_scale_factor
|
| 397 |
+
) # batch_size, height, 8, width, 8
|
| 398 |
+
mask = mask.permute(0, 2, 4, 1, 3) # batch_size, 8, 8, height, width
|
| 399 |
+
mask = mask.reshape(
|
| 400 |
+
batch_size, self.vae_scale_factor * self.vae_scale_factor, height, width
|
| 401 |
+
) # batch_size, 8*8, height, width
|
| 402 |
+
|
| 403 |
+
# 6. pack the mask:
|
| 404 |
+
# batch_size, 64, height, width -> batch_size, height//2 * width//2 , 64*2*2
|
| 405 |
+
mask = self._pack_latents(
|
| 406 |
+
mask,
|
| 407 |
+
batch_size,
|
| 408 |
+
self.vae_scale_factor * self.vae_scale_factor,
|
| 409 |
+
height,
|
| 410 |
+
width,
|
| 411 |
+
)
|
| 412 |
+
mask = mask.to(device=device, dtype=dtype)
|
| 413 |
+
|
| 414 |
+
return mask, masked_image_latents
|
| 415 |
+
|
| 416 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline.encode_prompt
|
| 417 |
+
def encode_prompt(
|
| 418 |
+
self,
|
| 419 |
+
prompt: Union[str, List[str]],
|
| 420 |
+
prompt_2: Union[str, List[str]],
|
| 421 |
+
device: Optional[torch.device] = None,
|
| 422 |
+
num_images_per_prompt: int = 1,
|
| 423 |
+
prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 424 |
+
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 425 |
+
max_sequence_length: int = 512,
|
| 426 |
+
lora_scale: Optional[float] = None,
|
| 427 |
+
):
|
| 428 |
+
r"""
|
| 429 |
+
|
| 430 |
+
Args:
|
| 431 |
+
prompt (`str` or `List[str]`, *optional*):
|
| 432 |
+
prompt to be encoded
|
| 433 |
+
prompt_2 (`str` or `List[str]`, *optional*):
|
| 434 |
+
The prompt or prompts to be sent to the `tokenizer_2` and `text_encoder_2`. If not defined, `prompt` is
|
| 435 |
+
used in all text-encoders
|
| 436 |
+
device: (`torch.device`):
|
| 437 |
+
torch device
|
| 438 |
+
num_images_per_prompt (`int`):
|
| 439 |
+
number of images that should be generated per prompt
|
| 440 |
+
prompt_embeds (`torch.FloatTensor`, *optional*):
|
| 441 |
+
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
| 442 |
+
provided, text embeddings will be generated from `prompt` input argument.
|
| 443 |
+
pooled_prompt_embeds (`torch.FloatTensor`, *optional*):
|
| 444 |
+
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting.
|
| 445 |
+
If not provided, pooled text embeddings will be generated from `prompt` input argument.
|
| 446 |
+
lora_scale (`float`, *optional*):
|
| 447 |
+
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded.
|
| 448 |
+
"""
|
| 449 |
+
device = device or self._execution_device
|
| 450 |
+
|
| 451 |
+
# set lora scale so that monkey patched LoRA
|
| 452 |
+
# function of text encoder can correctly access it
|
| 453 |
+
if lora_scale is not None and isinstance(self, FluxLoraLoaderMixin):
|
| 454 |
+
self._lora_scale = lora_scale
|
| 455 |
+
|
| 456 |
+
# dynamically adjust the LoRA scale
|
| 457 |
+
if self.text_encoder is not None and USE_PEFT_BACKEND:
|
| 458 |
+
scale_lora_layers(self.text_encoder, lora_scale)
|
| 459 |
+
if self.text_encoder_2 is not None and USE_PEFT_BACKEND:
|
| 460 |
+
scale_lora_layers(self.text_encoder_2, lora_scale)
|
| 461 |
+
|
| 462 |
+
prompt = [prompt] if isinstance(prompt, str) else prompt
|
| 463 |
+
|
| 464 |
+
if prompt_embeds is None:
|
| 465 |
+
prompt_2 = prompt_2 or prompt
|
| 466 |
+
prompt_2 = [prompt_2] if isinstance(prompt_2, str) else prompt_2
|
| 467 |
+
|
| 468 |
+
# We only use the pooled prompt output from the CLIPTextModel
|
| 469 |
+
pooled_prompt_embeds = self._get_clip_prompt_embeds(
|
| 470 |
+
prompt=prompt,
|
| 471 |
+
device=device,
|
| 472 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 473 |
+
)
|
| 474 |
+
prompt_embeds = self._get_t5_prompt_embeds(
|
| 475 |
+
prompt=prompt_2,
|
| 476 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 477 |
+
max_sequence_length=max_sequence_length,
|
| 478 |
+
device=device,
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
+
if self.text_encoder is not None:
|
| 482 |
+
if isinstance(self, FluxLoraLoaderMixin) and USE_PEFT_BACKEND:
|
| 483 |
+
# Retrieve the original scale by scaling back the LoRA layers
|
| 484 |
+
unscale_lora_layers(self.text_encoder, lora_scale)
|
| 485 |
+
|
| 486 |
+
if self.text_encoder_2 is not None:
|
| 487 |
+
if isinstance(self, FluxLoraLoaderMixin) and USE_PEFT_BACKEND:
|
| 488 |
+
# Retrieve the original scale by scaling back the LoRA layers
|
| 489 |
+
unscale_lora_layers(self.text_encoder_2, lora_scale)
|
| 490 |
+
|
| 491 |
+
dtype = self.text_encoder.dtype if self.text_encoder is not None else self.transformer.dtype
|
| 492 |
+
text_ids = torch.zeros(prompt_embeds.shape[1], 3).to(device=device, dtype=dtype)
|
| 493 |
+
|
| 494 |
+
return prompt_embeds, pooled_prompt_embeds, text_ids
|
| 495 |
+
|
| 496 |
+
def check_inputs(
|
| 497 |
+
self,
|
| 498 |
+
prompt,
|
| 499 |
+
prompt_2,
|
| 500 |
+
height,
|
| 501 |
+
width,
|
| 502 |
+
prompt_embeds=None,
|
| 503 |
+
pooled_prompt_embeds=None,
|
| 504 |
+
callback_on_step_end_tensor_inputs=None,
|
| 505 |
+
max_sequence_length=None,
|
| 506 |
+
image=None,
|
| 507 |
+
mask_image=None,
|
| 508 |
+
masked_image_latents=None,
|
| 509 |
+
):
|
| 510 |
+
if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0:
|
| 511 |
+
logger.warning(
|
| 512 |
+
f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly"
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
+
if callback_on_step_end_tensor_inputs is not None and not all(
|
| 516 |
+
k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
|
| 517 |
+
):
|
| 518 |
+
raise ValueError(
|
| 519 |
+
f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
|
| 520 |
+
)
|
| 521 |
+
|
| 522 |
+
if prompt is not None and prompt_embeds is not None:
|
| 523 |
+
raise ValueError(
|
| 524 |
+
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
| 525 |
+
" only forward one of the two."
|
| 526 |
+
)
|
| 527 |
+
elif prompt_2 is not None and prompt_embeds is not None:
|
| 528 |
+
raise ValueError(
|
| 529 |
+
f"Cannot forward both `prompt_2`: {prompt_2} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
|
| 530 |
+
" only forward one of the two."
|
| 531 |
+
)
|
| 532 |
+
elif prompt is None and prompt_embeds is None:
|
| 533 |
+
raise ValueError(
|
| 534 |
+
"Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
|
| 535 |
+
)
|
| 536 |
+
elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
|
| 537 |
+
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
|
| 538 |
+
elif prompt_2 is not None and (not isinstance(prompt_2, str) and not isinstance(prompt_2, list)):
|
| 539 |
+
raise ValueError(f"`prompt_2` has to be of type `str` or `list` but is {type(prompt_2)}")
|
| 540 |
+
|
| 541 |
+
if prompt_embeds is not None and pooled_prompt_embeds is None:
|
| 542 |
+
raise ValueError(
|
| 543 |
+
"If `prompt_embeds` are provided, `pooled_prompt_embeds` also have to be passed. Make sure to generate `pooled_prompt_embeds` from the same text encoder that was used to generate `prompt_embeds`."
|
| 544 |
+
)
|
| 545 |
+
|
| 546 |
+
if max_sequence_length is not None and max_sequence_length > 512:
|
| 547 |
+
raise ValueError(f"`max_sequence_length` cannot be greater than 512 but is {max_sequence_length}")
|
| 548 |
+
|
| 549 |
+
if image is not None and masked_image_latents is not None:
|
| 550 |
+
raise ValueError(
|
| 551 |
+
"Please provide either `image` or `masked_image_latents`, `masked_image_latents` should not be passed."
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
if image is not None and mask_image is None:
|
| 555 |
+
raise ValueError("Please provide `mask_image` when passing `image`.")
|
| 556 |
+
|
| 557 |
+
@staticmethod
|
| 558 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline._prepare_latent_image_ids
|
| 559 |
+
def _prepare_latent_image_ids(batch_size, height, width, device, dtype):
|
| 560 |
+
latent_image_ids = torch.zeros(height, width, 3)
|
| 561 |
+
latent_image_ids[..., 1] = latent_image_ids[..., 1] + torch.arange(height)[:, None]
|
| 562 |
+
latent_image_ids[..., 2] = latent_image_ids[..., 2] + torch.arange(width)[None, :]
|
| 563 |
+
|
| 564 |
+
latent_image_id_height, latent_image_id_width, latent_image_id_channels = latent_image_ids.shape
|
| 565 |
+
|
| 566 |
+
latent_image_ids = latent_image_ids.reshape(
|
| 567 |
+
latent_image_id_height * latent_image_id_width, latent_image_id_channels
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
return latent_image_ids.to(device=device, dtype=dtype)
|
| 571 |
+
|
| 572 |
+
@staticmethod
|
| 573 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline._pack_latents
|
| 574 |
+
def _pack_latents(latents, batch_size, num_channels_latents, height, width):
|
| 575 |
+
latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2)
|
| 576 |
+
latents = latents.permute(0, 2, 4, 1, 3, 5)
|
| 577 |
+
latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4)
|
| 578 |
+
|
| 579 |
+
return latents
|
| 580 |
+
|
| 581 |
+
@staticmethod
|
| 582 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline._unpack_latents
|
| 583 |
+
def _unpack_latents(latents, height, width, vae_scale_factor):
|
| 584 |
+
batch_size, num_patches, channels = latents.shape
|
| 585 |
+
|
| 586 |
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
| 587 |
+
# latent height and width to be divisible by 2.
|
| 588 |
+
height = 2 * (int(height) // (vae_scale_factor * 2))
|
| 589 |
+
width = 2 * (int(width) // (vae_scale_factor * 2))
|
| 590 |
+
|
| 591 |
+
latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2)
|
| 592 |
+
latents = latents.permute(0, 3, 1, 4, 2, 5)
|
| 593 |
+
|
| 594 |
+
latents = latents.reshape(batch_size, channels // (2 * 2), height, width)
|
| 595 |
+
|
| 596 |
+
return latents
|
| 597 |
+
|
| 598 |
+
def enable_vae_slicing(self):
|
| 599 |
+
r"""
|
| 600 |
+
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
|
| 601 |
+
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
|
| 602 |
+
"""
|
| 603 |
+
self.vae.enable_slicing()
|
| 604 |
+
|
| 605 |
+
def disable_vae_slicing(self):
|
| 606 |
+
r"""
|
| 607 |
+
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
|
| 608 |
+
computing decoding in one step.
|
| 609 |
+
"""
|
| 610 |
+
self.vae.disable_slicing()
|
| 611 |
+
|
| 612 |
+
def enable_vae_tiling(self):
|
| 613 |
+
r"""
|
| 614 |
+
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
|
| 615 |
+
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
|
| 616 |
+
processing larger images.
|
| 617 |
+
"""
|
| 618 |
+
self.vae.enable_tiling()
|
| 619 |
+
|
| 620 |
+
def disable_vae_tiling(self):
|
| 621 |
+
r"""
|
| 622 |
+
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
|
| 623 |
+
computing decoding in one step.
|
| 624 |
+
"""
|
| 625 |
+
self.vae.disable_tiling()
|
| 626 |
+
|
| 627 |
+
# Copied from diffusers.pipelines.flux.pipeline_flux.FluxPipeline.prepare_latents
|
| 628 |
+
def prepare_latents(
|
| 629 |
+
self,
|
| 630 |
+
batch_size,
|
| 631 |
+
num_channels_latents,
|
| 632 |
+
height,
|
| 633 |
+
width,
|
| 634 |
+
dtype,
|
| 635 |
+
device,
|
| 636 |
+
generator,
|
| 637 |
+
latents=None,
|
| 638 |
+
):
|
| 639 |
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
| 640 |
+
# latent height and width to be divisible by 2.
|
| 641 |
+
height = 2 * (int(height) // (self.vae_scale_factor * 2))
|
| 642 |
+
width = 2 * (int(width) // (self.vae_scale_factor * 2))
|
| 643 |
+
|
| 644 |
+
shape = (batch_size, num_channels_latents, height, width)
|
| 645 |
+
|
| 646 |
+
if latents is not None:
|
| 647 |
+
latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
|
| 648 |
+
return latents.to(device=device, dtype=dtype), latent_image_ids
|
| 649 |
+
|
| 650 |
+
if isinstance(generator, list) and len(generator) != batch_size:
|
| 651 |
+
raise ValueError(
|
| 652 |
+
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
| 653 |
+
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
| 654 |
+
)
|
| 655 |
+
|
| 656 |
+
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
| 657 |
+
latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width)
|
| 658 |
+
|
| 659 |
+
latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
|
| 660 |
+
|
| 661 |
+
return latents, latent_image_ids
|
| 662 |
+
|
| 663 |
+
@property
|
| 664 |
+
def guidance_scale(self):
|
| 665 |
+
return self._guidance_scale
|
| 666 |
+
|
| 667 |
+
@property
|
| 668 |
+
def joint_attention_kwargs(self):
|
| 669 |
+
return self._joint_attention_kwargs
|
| 670 |
+
|
| 671 |
+
@property
|
| 672 |
+
def num_timesteps(self):
|
| 673 |
+
return self._num_timesteps
|
| 674 |
+
|
| 675 |
+
@property
|
| 676 |
+
def interrupt(self):
|
| 677 |
+
return self._interrupt
|
| 678 |
+
|
| 679 |
+
@torch.no_grad()
|
| 680 |
+
@replace_example_docstring(EXAMPLE_DOC_STRING)
|
| 681 |
+
def __call__(
|
| 682 |
+
self,
|
| 683 |
+
prompt: Union[str, List[str]] = None,
|
| 684 |
+
prompt_2: Optional[Union[str, List[str]]] = None,
|
| 685 |
+
image: Optional[torch.FloatTensor] = None,
|
| 686 |
+
mask_image: Optional[torch.FloatTensor] = None,
|
| 687 |
+
masked_image_latents: Optional[torch.FloatTensor] = None,
|
| 688 |
+
height: Optional[int] = None,
|
| 689 |
+
width: Optional[int] = None,
|
| 690 |
+
num_inference_steps: int = 50,
|
| 691 |
+
sigmas: Optional[List[float]] = None,
|
| 692 |
+
guidance_scale: float = 30.0,
|
| 693 |
+
num_images_per_prompt: Optional[int] = 1,
|
| 694 |
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
| 695 |
+
latents: Optional[torch.FloatTensor] = None,
|
| 696 |
+
prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 697 |
+
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 698 |
+
image_emb: Optional[torch.FloatTensor] = None,
|
| 699 |
+
output_type: Optional[str] = "pil",
|
| 700 |
+
return_dict: bool = True,
|
| 701 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 702 |
+
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
| 703 |
+
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
| 704 |
+
max_sequence_length: int = 512,
|
| 705 |
+
):
|
| 706 |
+
r"""
|
| 707 |
+
Function invoked when calling the pipeline for generation.
|
| 708 |
+
|
| 709 |
+
Args:
|
| 710 |
+
prompt (`str` or `List[str]`, *optional*):
|
| 711 |
+
The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
|
| 712 |
+
instead.
|
| 713 |
+
prompt_2 (`str` or `List[str]`, *optional*):
|
| 714 |
+
The prompt or prompts to be sent to `tokenizer_2` and `text_encoder_2`. If not defined, `prompt` is
|
| 715 |
+
will be used instead
|
| 716 |
+
image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
|
| 717 |
+
`Image`, numpy array or tensor representing an image batch to be used as the starting point. For both
|
| 718 |
+
numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list
|
| 719 |
+
or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a
|
| 720 |
+
list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)`.
|
| 721 |
+
mask_image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
|
| 722 |
+
`Image`, numpy array or tensor representing an image batch to mask `image`. White pixels in the mask
|
| 723 |
+
are repainted while black pixels are preserved. If `mask_image` is a PIL image, it is converted to a
|
| 724 |
+
single channel (luminance) before use. If it's a numpy array or pytorch tensor, it should contain one
|
| 725 |
+
color channel (L) instead of 3, so the expected shape for pytorch tensor would be `(B, 1, H, W)`, `(B,
|
| 726 |
+
H, W)`, `(1, H, W)`, `(H, W)`. And for numpy array would be for `(B, H, W, 1)`, `(B, H, W)`, `(H, W,
|
| 727 |
+
1)`, or `(H, W)`.
|
| 728 |
+
mask_image_latent (`torch.Tensor`, `List[torch.Tensor]`):
|
| 729 |
+
`Tensor` representing an image batch to mask `image` generated by VAE. If not provided, the mask
|
| 730 |
+
latents tensor will ge generated by `mask_image`.
|
| 731 |
+
use_context (`bool`):
|
| 732 |
+
Whether to use the context image to guide the customization.
|
| 733 |
+
height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
| 734 |
+
The height in pixels of the generated image. This is set to 1024 by default for the best results.
|
| 735 |
+
width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
| 736 |
+
The width in pixels of the generated image. This is set to 1024 by default for the best results.
|
| 737 |
+
num_inference_steps (`int`, *optional*, defaults to 50):
|
| 738 |
+
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
|
| 739 |
+
expense of slower inference.
|
| 740 |
+
sigmas (`List[float]`, *optional*):
|
| 741 |
+
Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
|
| 742 |
+
their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
|
| 743 |
+
will be used.
|
| 744 |
+
guidance_scale (`float`, *optional*, defaults to 7.0):
|
| 745 |
+
Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
|
| 746 |
+
`guidance_scale` is defined as `w` of equation 2. of [Imagen
|
| 747 |
+
Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
|
| 748 |
+
1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
|
| 749 |
+
usually at the expense of lower image quality.
|
| 750 |
+
num_images_per_prompt (`int`, *optional*, defaults to 1):
|
| 751 |
+
The number of images to generate per prompt.
|
| 752 |
+
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
|
| 753 |
+
One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
|
| 754 |
+
to make generation deterministic.
|
| 755 |
+
latents (`torch.FloatTensor`, *optional*):
|
| 756 |
+
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
|
| 757 |
+
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
|
| 758 |
+
tensor will ge generated by sampling using the supplied random `generator`.
|
| 759 |
+
prompt_embeds (`torch.FloatTensor`, *optional*):
|
| 760 |
+
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
| 761 |
+
provided, text embeddings will be generated from `prompt` input argument.
|
| 762 |
+
pooled_prompt_embeds (`torch.FloatTensor`, *optional*):
|
| 763 |
+
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting.
|
| 764 |
+
If not provided, pooled text embeddings will be generated from `prompt` input argument.
|
| 765 |
+
output_type (`str`, *optional*, defaults to `"pil"`):
|
| 766 |
+
The output format of the generate image. Choose between
|
| 767 |
+
[PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
|
| 768 |
+
return_dict (`bool`, *optional*, defaults to `True`):
|
| 769 |
+
Whether or not to return a [`~pipelines.flux.FluxPipelineOutput`] instead of a plain tuple.
|
| 770 |
+
joint_attention_kwargs (`dict`, *optional*):
|
| 771 |
+
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
| 772 |
+
`self.processor` in
|
| 773 |
+
[diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
| 774 |
+
callback_on_step_end (`Callable`, *optional*):
|
| 775 |
+
A function that calls at the end of each denoising steps during the inference. The function is called
|
| 776 |
+
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
|
| 777 |
+
callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
|
| 778 |
+
`callback_on_step_end_tensor_inputs`.
|
| 779 |
+
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
| 780 |
+
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
| 781 |
+
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
| 782 |
+
`._callback_tensor_inputs` attribute of your pipeline class.
|
| 783 |
+
max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`.
|
| 784 |
+
|
| 785 |
+
Examples:
|
| 786 |
+
|
| 787 |
+
Returns:
|
| 788 |
+
[`~pipelines.flux.FluxPipelineOutput`] or `tuple`: [`~pipelines.flux.FluxPipelineOutput`] if `return_dict`
|
| 789 |
+
is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated
|
| 790 |
+
images.
|
| 791 |
+
"""
|
| 792 |
+
|
| 793 |
+
height = height or self.default_sample_size * self.vae_scale_factor
|
| 794 |
+
width = width or self.default_sample_size * self.vae_scale_factor
|
| 795 |
+
|
| 796 |
+
# 1. Check inputs. Raise error if not correct
|
| 797 |
+
self.check_inputs(
|
| 798 |
+
prompt,
|
| 799 |
+
prompt_2,
|
| 800 |
+
height,
|
| 801 |
+
width,
|
| 802 |
+
prompt_embeds=prompt_embeds,
|
| 803 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 804 |
+
callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
|
| 805 |
+
max_sequence_length=max_sequence_length,
|
| 806 |
+
image=image,
|
| 807 |
+
mask_image=mask_image,
|
| 808 |
+
masked_image_latents=masked_image_latents,
|
| 809 |
+
)
|
| 810 |
+
|
| 811 |
+
self._guidance_scale = guidance_scale
|
| 812 |
+
self._joint_attention_kwargs = joint_attention_kwargs
|
| 813 |
+
self._interrupt = False
|
| 814 |
+
|
| 815 |
+
# 2. Define call parameters
|
| 816 |
+
if prompt is not None and isinstance(prompt, str):
|
| 817 |
+
batch_size = 1
|
| 818 |
+
elif prompt is not None and isinstance(prompt, list):
|
| 819 |
+
batch_size = len(prompt)
|
| 820 |
+
else:
|
| 821 |
+
batch_size = prompt_embeds.shape[0]
|
| 822 |
+
|
| 823 |
+
device = self._execution_device
|
| 824 |
+
|
| 825 |
+
# 3. Prepare prompt embeddings
|
| 826 |
+
lora_scale = (
|
| 827 |
+
self.joint_attention_kwargs.get("scale", None) if self.joint_attention_kwargs is not None else None
|
| 828 |
+
)
|
| 829 |
+
(
|
| 830 |
+
prompt_embeds,
|
| 831 |
+
pooled_prompt_embeds,
|
| 832 |
+
text_ids,
|
| 833 |
+
) = self.encode_prompt(
|
| 834 |
+
prompt=prompt,
|
| 835 |
+
prompt_2=prompt_2,
|
| 836 |
+
prompt_embeds=prompt_embeds,
|
| 837 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 838 |
+
device=device,
|
| 839 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 840 |
+
max_sequence_length=max_sequence_length,
|
| 841 |
+
lora_scale=lora_scale,
|
| 842 |
+
)
|
| 843 |
+
|
| 844 |
+
# 4. Prepare latent variables
|
| 845 |
+
num_channels_latents = self.vae.config.latent_channels
|
| 846 |
+
|
| 847 |
+
latents, latent_image_ids = self.prepare_latents(
|
| 848 |
+
batch_size * num_images_per_prompt,
|
| 849 |
+
num_channels_latents,
|
| 850 |
+
height,
|
| 851 |
+
width,
|
| 852 |
+
prompt_embeds.dtype,
|
| 853 |
+
device,
|
| 854 |
+
generator,
|
| 855 |
+
latents,
|
| 856 |
+
)
|
| 857 |
+
|
| 858 |
+
# 5. Prepare mask and masked image latents
|
| 859 |
+
if masked_image_latents is not None:
|
| 860 |
+
masked_image_latents = masked_image_latents.to(latents.device)
|
| 861 |
+
else:
|
| 862 |
+
image = self.image_processor.preprocess(image, height=height, width=width)
|
| 863 |
+
mask_image = self.mask_processor.preprocess(mask_image, height=height, width=width)
|
| 864 |
+
|
| 865 |
+
masked_image = image * (1 - mask_image)
|
| 866 |
+
masked_image = masked_image.to(device=device, dtype=prompt_embeds.dtype)
|
| 867 |
+
|
| 868 |
+
height, width = image.shape[-2:]
|
| 869 |
+
mask, masked_image_latents \
|
| 870 |
+
= self.prepare_mask_latents(
|
| 871 |
+
mask_image,
|
| 872 |
+
masked_image,
|
| 873 |
+
batch_size,
|
| 874 |
+
num_channels_latents,
|
| 875 |
+
num_images_per_prompt,
|
| 876 |
+
height,
|
| 877 |
+
width,
|
| 878 |
+
prompt_embeds.dtype,
|
| 879 |
+
device,
|
| 880 |
+
generator
|
| 881 |
+
)
|
| 882 |
+
masked_image_latents = torch.cat((masked_image_latents, mask), dim=-1)
|
| 883 |
+
|
| 884 |
+
# 6. Prepare timesteps
|
| 885 |
+
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas
|
| 886 |
+
image_seq_len = latents.shape[1]
|
| 887 |
+
mu = calculate_shift(
|
| 888 |
+
image_seq_len,
|
| 889 |
+
self.scheduler.config.base_image_seq_len,
|
| 890 |
+
self.scheduler.config.max_image_seq_len,
|
| 891 |
+
self.scheduler.config.base_shift,
|
| 892 |
+
self.scheduler.config.max_shift,
|
| 893 |
+
)
|
| 894 |
+
timesteps, num_inference_steps = retrieve_timesteps(
|
| 895 |
+
self.scheduler,
|
| 896 |
+
num_inference_steps,
|
| 897 |
+
device,
|
| 898 |
+
sigmas=sigmas,
|
| 899 |
+
mu=mu,
|
| 900 |
+
)
|
| 901 |
+
num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
|
| 902 |
+
self._num_timesteps = len(timesteps)
|
| 903 |
+
|
| 904 |
+
# handle guidance
|
| 905 |
+
if self.transformer.config.guidance_embeds:
|
| 906 |
+
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32)
|
| 907 |
+
guidance = guidance.expand(latents.shape[0])
|
| 908 |
+
else:
|
| 909 |
+
guidance = None
|
| 910 |
+
|
| 911 |
+
# 7. Denoising loop
|
| 912 |
+
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
| 913 |
+
for i, t in enumerate(timesteps):
|
| 914 |
+
if self.interrupt:
|
| 915 |
+
continue
|
| 916 |
+
cond_latents = masked_image_latents
|
| 917 |
+
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
| 918 |
+
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
| 919 |
+
noise_pred = self.transformer(
|
| 920 |
+
hidden_states=torch.cat((latents, cond_latents), dim=2),
|
| 921 |
+
timestep=timestep / 1000,
|
| 922 |
+
guidance=guidance,
|
| 923 |
+
pooled_projections=pooled_prompt_embeds,
|
| 924 |
+
encoder_hidden_states=prompt_embeds,
|
| 925 |
+
image_emb=image_emb,
|
| 926 |
+
txt_ids=text_ids,
|
| 927 |
+
img_ids=latent_image_ids,
|
| 928 |
+
joint_attention_kwargs=self.joint_attention_kwargs,
|
| 929 |
+
return_dict=False,
|
| 930 |
+
)[0]
|
| 931 |
+
|
| 932 |
+
# compute the previous noisy sample x_t -> x_t-1
|
| 933 |
+
latents_dtype = latents.dtype
|
| 934 |
+
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
| 935 |
+
|
| 936 |
+
if latents.dtype != latents_dtype:
|
| 937 |
+
if torch.backends.mps.is_available():
|
| 938 |
+
# some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
|
| 939 |
+
latents = latents.to(latents_dtype)
|
| 940 |
+
|
| 941 |
+
if callback_on_step_end is not None:
|
| 942 |
+
callback_kwargs = {}
|
| 943 |
+
for k in callback_on_step_end_tensor_inputs:
|
| 944 |
+
callback_kwargs[k] = locals()[k]
|
| 945 |
+
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
| 946 |
+
|
| 947 |
+
latents = callback_outputs.pop("latents", latents)
|
| 948 |
+
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
| 949 |
+
|
| 950 |
+
# call the callback, if provided
|
| 951 |
+
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
| 952 |
+
progress_bar.update()
|
| 953 |
+
|
| 954 |
+
if XLA_AVAILABLE:
|
| 955 |
+
xm.mark_step()
|
| 956 |
+
|
| 957 |
+
# 8. Post-process the image
|
| 958 |
+
if output_type == "latent":
|
| 959 |
+
image = latents
|
| 960 |
+
else:
|
| 961 |
+
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
| 962 |
+
latents = (latents / self.vae.config.scaling_factor) + self.vae.config.shift_factor
|
| 963 |
+
image = self.vae.decode(latents, return_dict=False)[0]
|
| 964 |
+
image = self.image_processor.postprocess(image, output_type=output_type)
|
| 965 |
+
|
| 966 |
+
# Offload all models
|
| 967 |
+
self.maybe_free_model_hooks()
|
| 968 |
+
|
| 969 |
+
if not return_dict:
|
| 970 |
+
return (image,)
|
| 971 |
+
|
| 972 |
+
return FluxPipelineOutput(images=image)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
diffusers==0.33.0
|
| 4 |
+
transformers==4.49.0
|
| 5 |
+
Pillow==11.0.0
|
| 6 |
+
numpy==1.26.4
|
| 7 |
+
opencv-python==4.11.0.86
|
| 8 |
+
accelerate==0.30.1
|
| 9 |
+
protobuf==5.28.3
|
| 10 |
+
sentencepiece==0.2.0
|
utils.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Helper functions mainly for data preparation.
|
| 3 |
+
"""
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Extracting the image and mask from gr.ImageEditor.
|
| 10 |
+
def process_gradio_source(editor_data):
|
| 11 |
+
original_image = editor_data["background"].convert("RGB")
|
| 12 |
+
layers = editor_data.get("layers", [])
|
| 13 |
+
|
| 14 |
+
full_mask = Image.new("L", original_image.size, 0)
|
| 15 |
+
cropped_region = original_image.copy()
|
| 16 |
+
|
| 17 |
+
if not layers:
|
| 18 |
+
return full_mask, cropped_region, original_image
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
layer_image = layers[0]
|
| 22 |
+
layer_pos = (0, 0)
|
| 23 |
+
|
| 24 |
+
if layer_image.mode != "RGBA":
|
| 25 |
+
layer_image = layer_image.convert("RGBA")
|
| 26 |
+
|
| 27 |
+
alpha_channel = layer_image.split()[-1]
|
| 28 |
+
|
| 29 |
+
full_mask = Image.new("L", original_image.size, 0)
|
| 30 |
+
full_mask.paste(alpha_channel, layer_pos)
|
| 31 |
+
full_mask = full_mask.point(lambda p: 255 if p > 128 else 0)
|
| 32 |
+
|
| 33 |
+
original_np = np.array(original_image)
|
| 34 |
+
mask_np = np.array(full_mask)
|
| 35 |
+
|
| 36 |
+
mask_bool = mask_np > 0
|
| 37 |
+
cropped_array = np.zeros_like(original_np)
|
| 38 |
+
cropped_array[mask_bool] = original_np[mask_bool]
|
| 39 |
+
|
| 40 |
+
cropped_region = Image.fromarray(cropped_array)
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Raise error: {str(e)}")
|
| 44 |
+
|
| 45 |
+
return original_image, full_mask, cropped_region
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Get bounding box from the mask.
|
| 49 |
+
def get_bbox_from_mask(mask_image):
|
| 50 |
+
mask_array = np.array(mask_image)
|
| 51 |
+
if mask_array.ndim == 3:
|
| 52 |
+
mask_array = cv2.cvtColor(mask_array, cv2.COLOR_RGB2GRAY)
|
| 53 |
+
_, binary_mask = cv2.threshold(mask_array, 127, 255, cv2.THRESH_BINARY)
|
| 54 |
+
|
| 55 |
+
contours, _ = cv2.findContours(mask_array, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 56 |
+
if not contours:
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
largest_contour = max(contours, key=cv2.contourArea)
|
| 60 |
+
x, y, w, h = cv2.boundingRect(largest_contour)
|
| 61 |
+
return (x, y), (x + w, y + h)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Crop image from the bounding box coord.
|
| 65 |
+
def crop_image_from_bb(original_image, top_left, bottom_right):
|
| 66 |
+
x1, y1 = map(int, top_left)
|
| 67 |
+
x2, y2 = map(int, bottom_right)
|
| 68 |
+
if not (0 <= x1 < x2 <= original_image.width and 0 <= y1 < y2 <= original_image.height):
|
| 69 |
+
raise ValueError("Invalid bounding box coordinates")
|
| 70 |
+
crop_box = (x1, y1, x2, y2)
|
| 71 |
+
cropped_image = original_image.crop(crop_box)
|
| 72 |
+
return cropped_image
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Resize image to the target size with zero paddings.
|
| 76 |
+
def resize_img_and_pad(input_image, target_size):
|
| 77 |
+
cropped_width, cropped_height = input_image.size
|
| 78 |
+
target_width, target_height = target_size
|
| 79 |
+
scale = min(target_width / cropped_width, target_height / cropped_height)
|
| 80 |
+
new_width = int(cropped_width * scale)
|
| 81 |
+
new_height = int(cropped_height * scale)
|
| 82 |
+
|
| 83 |
+
resized_image = input_image.resize((new_width, new_height), Image.BILINEAR)
|
| 84 |
+
|
| 85 |
+
padded_image = Image.new("RGB", target_size, (0, 0, 0))
|
| 86 |
+
|
| 87 |
+
left_padding = (target_width - new_width) // 2
|
| 88 |
+
top_padding = (target_height - new_height) // 2
|
| 89 |
+
padded_image.paste(resized_image, (left_padding, top_padding))
|
| 90 |
+
|
| 91 |
+
return padded_image
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def pil_to_np(pil_img):
|
| 95 |
+
if pil_img.mode == 'RGBA':
|
| 96 |
+
rgb = pil_img.convert('RGB')
|
| 97 |
+
alpha = pil_img.split()[3]
|
| 98 |
+
img = np.array(rgb)
|
| 99 |
+
alpha = np.array(alpha)
|
| 100 |
+
else:
|
| 101 |
+
img = np.array(pil_img.convert('RGB'))
|
| 102 |
+
return img
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# Helper function considering the VAE compression factor.
|
| 106 |
+
def nearest_multiple_of_16(ref_height):
|
| 107 |
+
lower = (ref_height // 16) * 16
|
| 108 |
+
upper = ((ref_height + 15) // 16) * 16
|
| 109 |
+
if ref_height - lower <= upper - ref_height:
|
| 110 |
+
return lower
|
| 111 |
+
else:
|
| 112 |
+
return upper
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Resize to generate the reference context.
|
| 116 |
+
def generate_context_reference_image(reference_image_pil, img_width=512):
|
| 117 |
+
reference_image_rgb = pil_to_np(reference_image_pil)
|
| 118 |
+
ref_height, ref_width = reference_image_rgb.shape[0], reference_image_rgb.shape[1]
|
| 119 |
+
|
| 120 |
+
ref_height = int((img_width / ref_width) * ref_height)
|
| 121 |
+
ref_height = nearest_multiple_of_16(ref_height)
|
| 122 |
+
|
| 123 |
+
reference_context = cv2.resize(reference_image_rgb, (img_width, ref_height))
|
| 124 |
+
reference_new_pil = Image.fromarray(reference_context)
|
| 125 |
+
return reference_new_pil
|