image engine: FLUX 3 Image Klein
Browse files- .gitattributes +1 -35
- README.md +9 -7
- app.py +234 -0
- configs/base/transformer/config.json +21 -0
- configs/distilled/transformer/config.json +23 -0
- diffusers-0.41.0.dev0-py3-none-any.whl +3 -0
- requirements.txt +6 -0
- vae_convert.py +153 -0
.gitattributes
CHANGED
|
@@ -1,35 +1 @@
|
|
| 1 |
-
*.
|
| 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 |
+
*.whl filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.30.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MV Studio Image
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.30.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
python_version: '3.12'
|
| 10 |
+
startup_duration_timeout: 1h
|
| 11 |
+
short_description: FLUX 3 Image Klein keyframes + album art (private engine)
|
| 12 |
---
|
| 13 |
|
| 14 |
+
MV Studio image engine. FLUX 3 Image Klein (distilled), up to 10 reference images.
|
| 15 |
+
Model loading adapted from black-forest-labs/flux-3-image-klein. FLUX Kommunity License applies to the weights.
|
app.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MV Studio image engine: FLUX 3 Image Klein (distilled) on ZeroGPU.
|
| 2 |
+
|
| 3 |
+
Keyframes for music-video shots and album art. Takes a final caption (the studio upsamples it with the
|
| 4 |
+
brain engine first, using the FLUX upsampler's own system prompts) plus up to 10 reference images, so a
|
| 5 |
+
character's front/left/right/back photos lock identity. Model loading follows black-forest-labs/flux-3-image-klein.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import glob
|
| 9 |
+
import os
|
| 10 |
+
import subprocess
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
+
_wheel = glob.glob(os.path.join(HERE, "diffusers-*.whl"))
|
| 15 |
+
if _wheel:
|
| 16 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-deps", "--no-cache-dir", _wheel[0]])
|
| 17 |
+
|
| 18 |
+
import spaces # noqa: E402 (must come before torch)
|
| 19 |
+
import json # noqa: E402
|
| 20 |
+
import random # noqa: E402
|
| 21 |
+
import tempfile # noqa: E402
|
| 22 |
+
import time # noqa: E402
|
| 23 |
+
from pathlib import Path # noqa: E402
|
| 24 |
+
|
| 25 |
+
import gradio as gr # noqa: E402
|
| 26 |
+
import numpy as np # noqa: E402
|
| 27 |
+
import safetensors.torch # noqa: E402
|
| 28 |
+
import torch # noqa: E402
|
| 29 |
+
from huggingface_hub import hf_hub_download, snapshot_download # noqa: E402
|
| 30 |
+
from PIL import Image # noqa: E402
|
| 31 |
+
from spaces.config import Config as SPACES_CONFIG # noqa: E402
|
| 32 |
+
from transformers import AutoProcessor, Qwen3VLForConditionalGeneration # noqa: E402
|
| 33 |
+
|
| 34 |
+
from diffusers import AutoencoderKLFlux2, Flux3Transformer2DModel # noqa: E402
|
| 35 |
+
from diffusers.modular_pipelines import Flux3DistilledAutoBlocks # noqa: E402
|
| 36 |
+
from vae_convert import convert_flux2_vae_checkpoint_to_diffusers # noqa: E402
|
| 37 |
+
|
| 38 |
+
dtype = torch.bfloat16
|
| 39 |
+
token = os.environ.get("HF_TOKEN")
|
| 40 |
+
REPO = os.environ.get("FLUX_REPO", "black-forest-labs/flux-3-image-klein")
|
| 41 |
+
|
| 42 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 43 |
+
MAX_IMAGE_SIZE = 4096
|
| 44 |
+
MAX_REF_PIXELS = 1024 * 1024
|
| 45 |
+
MAX_REFS = 10
|
| 46 |
+
RESOLUTIONS = {"1K": 1024, "2K": 2048, "4K": 4096}
|
| 47 |
+
ASPECTS = {"1:1": 1.0, "4:3": 4 / 3, "3:4": 3 / 4, "3:2": 3 / 2, "2:3": 2 / 3, "16:9": 16 / 9, "9:16": 9 / 16}
|
| 48 |
+
|
| 49 |
+
print("[boot] text encoder…", flush=True)
|
| 50 |
+
text_encoder = Qwen3VLForConditionalGeneration.from_pretrained(REPO, subfolder="text_encoder", dtype=dtype, token=token)
|
| 51 |
+
processor = AutoProcessor.from_pretrained(REPO, subfolder="text_encoder", token=token)
|
| 52 |
+
|
| 53 |
+
print("[boot] VAE…", flush=True)
|
| 54 |
+
vae = AutoencoderKLFlux2()
|
| 55 |
+
_ae = safetensors.torch.load_file(hf_hub_download(REPO, "ae.safetensors", token=token))
|
| 56 |
+
vae.load_state_dict(convert_flux2_vae_checkpoint_to_diffusers(_ae, vae.config), strict=True)
|
| 57 |
+
vae.to(torch.float32).eval()
|
| 58 |
+
# Decode in 2048 px tiles above 2048 px: a 4K decode in one piece runs out of memory on ZeroGPU.
|
| 59 |
+
vae.use_tiling = True
|
| 60 |
+
vae.tile_sample_min_size = 2048
|
| 61 |
+
vae.tile_latent_min_size = 2048 // (2 ** (len(vae.config.block_out_channels) - 1))
|
| 62 |
+
del _ae
|
| 63 |
+
|
| 64 |
+
print("[boot] distilled transformer…", flush=True)
|
| 65 |
+
transformer = Flux3Transformer2DModel.from_single_file(
|
| 66 |
+
f"https://huggingface.co/{REPO}/blob/main/model.safetensors",
|
| 67 |
+
config=os.path.join(HERE, "configs", "distilled"),
|
| 68 |
+
subfolder="transformer",
|
| 69 |
+
torch_dtype=dtype,
|
| 70 |
+
token=token,
|
| 71 |
+
)
|
| 72 |
+
for name in Flux3Transformer2DModel._keep_in_fp32_modules:
|
| 73 |
+
for module_name, module in transformer.named_modules():
|
| 74 |
+
if module_name.split(".")[-1] == name:
|
| 75 |
+
module.to(torch.float32)
|
| 76 |
+
|
| 77 |
+
pipe = Flux3DistilledAutoBlocks().init_pipeline()
|
| 78 |
+
pipe.update_components(text_encoder=text_encoder, tokenizer=processor, vae=vae, transformer=transformer)
|
| 79 |
+
pipe.to("cuda")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _load_aoti():
|
| 83 |
+
"""Ahead-of-time compiled blocks from the official Space, when this torch matches; eager otherwise."""
|
| 84 |
+
from spaces.zero.torch.aoti import aoti_load_from_package_dir
|
| 85 |
+
|
| 86 |
+
repo = "diffusers-internal-dev/f3-small-image-distilled-aoti-sm120"
|
| 87 |
+
config = json.loads(Path(hf_hub_download(repo, "config.json", token=token)).read_text())
|
| 88 |
+
if config.get("torch") != torch.__version__:
|
| 89 |
+
raise RuntimeError(f"built for torch {config.get('torch')}, runtime has {torch.__version__}")
|
| 90 |
+
package_dir = Path(snapshot_download(repo, allow_patterns="package/*", token=token)) / "package"
|
| 91 |
+
|
| 92 |
+
def blocks(name):
|
| 93 |
+
stack = transformer.get_submodule(name)
|
| 94 |
+
return list(stack) if isinstance(stack, torch.nn.ModuleList) else [stack]
|
| 95 |
+
|
| 96 |
+
names = list(config["stacks"])
|
| 97 |
+
for name in names:
|
| 98 |
+
for block in blocks(name):
|
| 99 |
+
block.attn.norm_q.to(torch.bfloat16)
|
| 100 |
+
block.attn.norm_k.to(torch.bfloat16)
|
| 101 |
+
eager = {name: [block.forward for block in blocks(name)] for name in names}
|
| 102 |
+
aoti_load_from_package_dir(transformer, package_dir)
|
| 103 |
+
for name in names:
|
| 104 |
+
for block, eager_forward in zip(blocks(name), eager[name]):
|
| 105 |
+
compiled = block.forward
|
| 106 |
+
|
| 107 |
+
def forward(*args, _c=compiled, _e=eager_forward, **kwargs):
|
| 108 |
+
try:
|
| 109 |
+
return _c(*args, **kwargs)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"[aoti] eager fallback: {e!r}", flush=True)
|
| 112 |
+
return _e(*args, **kwargs)
|
| 113 |
+
|
| 114 |
+
block.forward = forward
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
_load_aoti()
|
| 119 |
+
print("[boot] AOTI loaded", flush=True)
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"[boot] AOTI unavailable, running eager: {e!r}", flush=True)
|
| 122 |
+
print("[boot] ready", flush=True)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def dims(aspect: str, resolution: str) -> tuple[int, int]:
|
| 126 |
+
ratio = ASPECTS.get(aspect, 1.0)
|
| 127 |
+
side = RESOLUTIONS.get(resolution, 2048)
|
| 128 |
+
h = (side * side / ratio) ** 0.5
|
| 129 |
+
w = h * ratio
|
| 130 |
+
scale = min(1.0, MAX_IMAGE_SIZE / max(w, h))
|
| 131 |
+
return (max(256, round(w * scale / 16) * 16), max(256, round(h * scale / 16) * 16))
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def fit_reference(img: Image.Image) -> Image.Image:
|
| 135 |
+
img = img.convert("RGB")
|
| 136 |
+
pixels = img.width * img.height
|
| 137 |
+
if pixels <= MAX_REF_PIXELS:
|
| 138 |
+
return img
|
| 139 |
+
scale = (MAX_REF_PIXELS / pixels) ** 0.5
|
| 140 |
+
return img.resize((max(16, round(img.width * scale)), max(16, round(img.height * scale))), Image.LANCZOS)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def load_refs(files) -> list[Image.Image]:
|
| 144 |
+
paths = [f if isinstance(f, str) else getattr(f, "name", None) or f.get("path") for f in (files or [])]
|
| 145 |
+
return [fit_reference(Image.open(p)) for p in paths[:MAX_REFS] if p]
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def _tokens(width, height, refs):
|
| 149 |
+
tokens = width * height / 256 + 512
|
| 150 |
+
for img in refs:
|
| 151 |
+
tokens += min(img.width * img.height, MAX_REF_PIXELS) / 256
|
| 152 |
+
return tokens
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# GPU-second estimates from the official Space's timings (large = half RTX Pro 6000, xlarge = full, 2x quota).
|
| 156 |
+
def _duration_large(caption, refs, width, height, seed, steps):
|
| 157 |
+
return int(min(max(15 + steps * 0.5 * (_tokens(width, height, refs) / 4608) ** 1.5, 20), 300))
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def _duration_xlarge(caption, refs, width, height, seed, steps):
|
| 161 |
+
return int(min(max(20 + 1.3 * steps * 8.4 * (_tokens(width, height, refs) / 66048) ** 1.5, 30), 300))
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def _run(caption, refs, width, height, seed, steps):
|
| 165 |
+
t0 = time.time()
|
| 166 |
+
kwargs = dict(prompt=caption, height=height, width=width, num_inference_steps=steps,
|
| 167 |
+
generator=torch.Generator().manual_seed(seed), output="images")
|
| 168 |
+
if refs:
|
| 169 |
+
kwargs["image"] = refs
|
| 170 |
+
image = pipe(**kwargs)[0]
|
| 171 |
+
print(f"[timing] {width}x{height} refs={len(refs)} steps={steps} gen_s={time.time() - t0:.1f} "
|
| 172 |
+
f"peak_gb={torch.cuda.max_memory_allocated() / 2**30:.1f}", flush=True)
|
| 173 |
+
return image
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@spaces.GPU(duration=_duration_large)
|
| 177 |
+
def _gpu_large(caption, refs, width, height, seed, steps):
|
| 178 |
+
return _run(caption, refs, width, height, seed, steps)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
@spaces.GPU(duration=_duration_xlarge, size="xlarge")
|
| 182 |
+
def _gpu_xlarge(caption, refs, width, height, seed, steps):
|
| 183 |
+
return _run(caption, refs, width, height, seed, steps)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def generate(caption: str, references=None, aspect: str = "1:1", resolution: str = "2K",
|
| 187 |
+
seed: int = -1, steps: int = 16):
|
| 188 |
+
"""Generate one image from a full scene caption and optional reference images (identity lock).
|
| 189 |
+
|
| 190 |
+
Args:
|
| 191 |
+
caption: the full, already-upsampled scene caption. Refer to references as "the first image", ...
|
| 192 |
+
references: up to 10 image files (e.g. a character's front, left, right and back photos).
|
| 193 |
+
aspect: one of 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16.
|
| 194 |
+
resolution: 1K, 2K or 4K (4K runs on a full GPU and costs 2x quota).
|
| 195 |
+
seed: -1 for random.
|
| 196 |
+
steps: denoising steps (distilled checkpoint is trained for 4-16).
|
| 197 |
+
|
| 198 |
+
Returns:
|
| 199 |
+
(image file path, seed used)
|
| 200 |
+
"""
|
| 201 |
+
if not (caption or "").strip():
|
| 202 |
+
raise gr.Error("Caption is empty.")
|
| 203 |
+
refs = load_refs(references)
|
| 204 |
+
width, height = dims(aspect, resolution)
|
| 205 |
+
seed = random.randint(0, MAX_SEED) if int(seed) < 0 else int(seed)
|
| 206 |
+
steps = max(1, min(int(steps), 16))
|
| 207 |
+
gpu = _gpu_large
|
| 208 |
+
if SPACES_CONFIG.zero_gpu and _duration_large(caption, refs, width, height, seed, steps) >= 300:
|
| 209 |
+
gpu = _gpu_xlarge
|
| 210 |
+
image = gpu(caption.strip(), refs, width, height, seed, steps)
|
| 211 |
+
out = Path(tempfile.mkdtemp()) / f"img_{seed}.png"
|
| 212 |
+
image.save(out)
|
| 213 |
+
return str(out), seed
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
with gr.Blocks(title="MV Studio · image engine") as demo:
|
| 217 |
+
gr.Markdown("### MV Studio image engine · FLUX 3 Image Klein\nBackend for the studio. Captions should be full scene descriptions.")
|
| 218 |
+
with gr.Row():
|
| 219 |
+
with gr.Column():
|
| 220 |
+
caption = gr.Textbox(label="Caption", lines=5)
|
| 221 |
+
references = gr.File(label="Reference images (≤10)", file_count="multiple", file_types=["image"])
|
| 222 |
+
with gr.Row():
|
| 223 |
+
aspect = gr.Dropdown(list(ASPECTS), value="1:1", label="Aspect")
|
| 224 |
+
resolution = gr.Radio(list(RESOLUTIONS), value="2K", label="Resolution")
|
| 225 |
+
with gr.Row():
|
| 226 |
+
seed = gr.Number(value=-1, precision=0, label="Seed (-1 random)")
|
| 227 |
+
steps = gr.Slider(4, 16, value=16, step=1, label="Steps")
|
| 228 |
+
run = gr.Button("Generate", variant="primary")
|
| 229 |
+
with gr.Column():
|
| 230 |
+
out_image = gr.Image(label="Result", type="filepath")
|
| 231 |
+
out_seed = gr.Number(label="Seed used", precision=0)
|
| 232 |
+
run.click(generate, [caption, references, aspect, resolution, seed, steps], [out_image, out_seed], api_name="generate")
|
| 233 |
+
|
| 234 |
+
demo.queue(max_size=16).launch(show_error=True)
|
configs/base/transformer/config.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "Flux3Transformer2DModel",
|
| 3 |
+
"_diffusers_version": "0.41.0.dev0",
|
| 4 |
+
"attention_head_dim": 128,
|
| 5 |
+
"axes_dims_rope": [
|
| 6 |
+
32,
|
| 7 |
+
32,
|
| 8 |
+
32,
|
| 9 |
+
32
|
| 10 |
+
],
|
| 11 |
+
"cond_in_channels": 128,
|
| 12 |
+
"eps": 1e-06,
|
| 13 |
+
"in_channels": 128,
|
| 14 |
+
"joint_attention_dim": 20480,
|
| 15 |
+
"mlp_ratio": 3.0,
|
| 16 |
+
"num_attention_heads": 24,
|
| 17 |
+
"num_layers": 5,
|
| 18 |
+
"num_single_layers": 28,
|
| 19 |
+
"rope_theta": 10000,
|
| 20 |
+
"timestep_channels": 256
|
| 21 |
+
}
|
configs/distilled/transformer/config.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "Flux3Transformer2DModel",
|
| 3 |
+
"_diffusers_version": "0.41.0.dev0",
|
| 4 |
+
"_name_or_path": "/raid/yiyi/flux3-image-distilled-modular",
|
| 5 |
+
"attention_head_dim": 128,
|
| 6 |
+
"axes_dims_rope": [
|
| 7 |
+
32,
|
| 8 |
+
32,
|
| 9 |
+
32,
|
| 10 |
+
32
|
| 11 |
+
],
|
| 12 |
+
"cond_in_channels": 128,
|
| 13 |
+
"eps": 1e-06,
|
| 14 |
+
"flow_map": true,
|
| 15 |
+
"in_channels": 128,
|
| 16 |
+
"joint_attention_dim": 20480,
|
| 17 |
+
"mlp_ratio": 3.0,
|
| 18 |
+
"num_attention_heads": 24,
|
| 19 |
+
"num_layers": 5,
|
| 20 |
+
"num_single_layers": 28,
|
| 21 |
+
"rope_theta": 10000,
|
| 22 |
+
"timestep_channels": 256
|
| 23 |
+
}
|
diffusers-0.41.0.dev0-py3-none-any.whl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cf85a6a723cd72cb7d7ce2f4210a450a69926675402e0652f29ca9175ad19992
|
| 3 |
+
size 6014799
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.11.0
|
| 2 |
+
torchvision==0.26.0
|
| 3 |
+
transformers
|
| 4 |
+
accelerate
|
| 5 |
+
safetensors
|
| 6 |
+
importlib_metadata
|
vae_convert.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
DIFFUSERS_VAE_TO_FLUX2_MAPPING = {
|
| 4 |
+
"encoder.conv_in.weight": "encoder.conv_in.weight",
|
| 5 |
+
"encoder.conv_in.bias": "encoder.conv_in.bias",
|
| 6 |
+
"encoder.conv_out.weight": "encoder.conv_out.weight",
|
| 7 |
+
"encoder.conv_out.bias": "encoder.conv_out.bias",
|
| 8 |
+
"encoder.conv_norm_out.weight": "encoder.norm_out.weight",
|
| 9 |
+
"encoder.conv_norm_out.bias": "encoder.norm_out.bias",
|
| 10 |
+
"decoder.conv_in.weight": "decoder.conv_in.weight",
|
| 11 |
+
"decoder.conv_in.bias": "decoder.conv_in.bias",
|
| 12 |
+
"decoder.conv_out.weight": "decoder.conv_out.weight",
|
| 13 |
+
"decoder.conv_out.bias": "decoder.conv_out.bias",
|
| 14 |
+
"decoder.conv_norm_out.weight": "decoder.norm_out.weight",
|
| 15 |
+
"decoder.conv_norm_out.bias": "decoder.norm_out.bias",
|
| 16 |
+
"quant_conv.weight": "encoder.quant_conv.weight",
|
| 17 |
+
"quant_conv.bias": "encoder.quant_conv.bias",
|
| 18 |
+
"post_quant_conv.weight": "decoder.post_quant_conv.weight",
|
| 19 |
+
"post_quant_conv.bias": "decoder.post_quant_conv.bias",
|
| 20 |
+
"bn.running_mean": "bn.running_mean",
|
| 21 |
+
"bn.running_var": "bn.running_var",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def conv_attn_to_linear(checkpoint):
|
| 26 |
+
keys = list(checkpoint.keys())
|
| 27 |
+
attn_keys = ["query.weight", "key.weight", "value.weight"]
|
| 28 |
+
for key in keys:
|
| 29 |
+
if ".".join(key.split(".")[-2:]) in attn_keys:
|
| 30 |
+
if checkpoint[key].ndim > 2:
|
| 31 |
+
checkpoint[key] = checkpoint[key][:, :, 0, 0]
|
| 32 |
+
elif "proj_attn.weight" in key:
|
| 33 |
+
if checkpoint[key].ndim > 2:
|
| 34 |
+
checkpoint[key] = checkpoint[key][:, :, 0]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def update_vae_resnet_ldm_to_diffusers(keys, new_checkpoint, checkpoint, mapping):
|
| 38 |
+
for ldm_key in keys:
|
| 39 |
+
diffusers_key = ldm_key.replace(mapping["old"], mapping["new"]).replace("nin_shortcut", "conv_shortcut")
|
| 40 |
+
new_checkpoint[diffusers_key] = checkpoint.get(ldm_key)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def update_vae_attentions_ldm_to_diffusers(keys, new_checkpoint, checkpoint, mapping):
|
| 44 |
+
for ldm_key in keys:
|
| 45 |
+
diffusers_key = (
|
| 46 |
+
ldm_key.replace(mapping["old"], mapping["new"])
|
| 47 |
+
.replace("norm.weight", "group_norm.weight")
|
| 48 |
+
.replace("norm.bias", "group_norm.bias")
|
| 49 |
+
.replace("q.weight", "to_q.weight")
|
| 50 |
+
.replace("q.bias", "to_q.bias")
|
| 51 |
+
.replace("k.weight", "to_k.weight")
|
| 52 |
+
.replace("k.bias", "to_k.bias")
|
| 53 |
+
.replace("v.weight", "to_v.weight")
|
| 54 |
+
.replace("v.bias", "to_v.bias")
|
| 55 |
+
.replace("proj_out.weight", "to_out.0.weight")
|
| 56 |
+
.replace("proj_out.bias", "to_out.0.bias")
|
| 57 |
+
)
|
| 58 |
+
new_checkpoint[diffusers_key] = checkpoint.get(ldm_key)
|
| 59 |
+
|
| 60 |
+
shape = new_checkpoint[diffusers_key].shape
|
| 61 |
+
|
| 62 |
+
if len(shape) == 3:
|
| 63 |
+
new_checkpoint[diffusers_key] = new_checkpoint[diffusers_key][:, :, 0]
|
| 64 |
+
elif len(shape) == 4:
|
| 65 |
+
new_checkpoint[diffusers_key] = new_checkpoint[diffusers_key][:, :, 0, 0]
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def convert_flux2_vae_checkpoint_to_diffusers(vae_state_dict, config):
|
| 69 |
+
new_checkpoint = {}
|
| 70 |
+
for diffusers_key, ldm_key in DIFFUSERS_VAE_TO_FLUX2_MAPPING.items():
|
| 71 |
+
if ldm_key not in vae_state_dict:
|
| 72 |
+
continue
|
| 73 |
+
new_checkpoint[diffusers_key] = vae_state_dict[ldm_key]
|
| 74 |
+
|
| 75 |
+
num_down_blocks = len(config["down_block_types"])
|
| 76 |
+
down_blocks = {
|
| 77 |
+
layer_id: [key for key in vae_state_dict if f"down.{layer_id}" in key] for layer_id in range(num_down_blocks)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
for i in range(num_down_blocks):
|
| 81 |
+
resnets = [key for key in down_blocks[i] if f"down.{i}" in key and f"down.{i}.downsample" not in key]
|
| 82 |
+
update_vae_resnet_ldm_to_diffusers(
|
| 83 |
+
resnets,
|
| 84 |
+
new_checkpoint,
|
| 85 |
+
vae_state_dict,
|
| 86 |
+
mapping={"old": f"down.{i}.block", "new": f"down_blocks.{i}.resnets"},
|
| 87 |
+
)
|
| 88 |
+
if f"encoder.down.{i}.downsample.conv.weight" in vae_state_dict:
|
| 89 |
+
new_checkpoint[f"encoder.down_blocks.{i}.downsamplers.0.conv.weight"] = vae_state_dict.get(
|
| 90 |
+
f"encoder.down.{i}.downsample.conv.weight"
|
| 91 |
+
)
|
| 92 |
+
new_checkpoint[f"encoder.down_blocks.{i}.downsamplers.0.conv.bias"] = vae_state_dict.get(
|
| 93 |
+
f"encoder.down.{i}.downsample.conv.bias"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
mid_resnets = [key for key in vae_state_dict if "encoder.mid.block" in key]
|
| 97 |
+
num_mid_res_blocks = 2
|
| 98 |
+
for i in range(1, num_mid_res_blocks + 1):
|
| 99 |
+
resnets = [key for key in mid_resnets if f"encoder.mid.block_{i}" in key]
|
| 100 |
+
update_vae_resnet_ldm_to_diffusers(
|
| 101 |
+
resnets,
|
| 102 |
+
new_checkpoint,
|
| 103 |
+
vae_state_dict,
|
| 104 |
+
mapping={"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"},
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
mid_attentions = [key for key in vae_state_dict if "encoder.mid.attn" in key]
|
| 108 |
+
update_vae_attentions_ldm_to_diffusers(
|
| 109 |
+
mid_attentions, new_checkpoint, vae_state_dict, mapping={"old": "mid.attn_1", "new": "mid_block.attentions.0"}
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
num_up_blocks = len(config["up_block_types"])
|
| 113 |
+
up_blocks = {
|
| 114 |
+
layer_id: [key for key in vae_state_dict if f"up.{layer_id}" in key] for layer_id in range(num_up_blocks)
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
for i in range(num_up_blocks):
|
| 118 |
+
block_id = num_up_blocks - 1 - i
|
| 119 |
+
resnets = [
|
| 120 |
+
key for key in up_blocks[block_id] if f"up.{block_id}" in key and f"up.{block_id}.upsample" not in key
|
| 121 |
+
]
|
| 122 |
+
update_vae_resnet_ldm_to_diffusers(
|
| 123 |
+
resnets,
|
| 124 |
+
new_checkpoint,
|
| 125 |
+
vae_state_dict,
|
| 126 |
+
mapping={"old": f"up.{block_id}.block", "new": f"up_blocks.{i}.resnets"},
|
| 127 |
+
)
|
| 128 |
+
if f"decoder.up.{block_id}.upsample.conv.weight" in vae_state_dict:
|
| 129 |
+
new_checkpoint[f"decoder.up_blocks.{i}.upsamplers.0.conv.weight"] = vae_state_dict[
|
| 130 |
+
f"decoder.up.{block_id}.upsample.conv.weight"
|
| 131 |
+
]
|
| 132 |
+
new_checkpoint[f"decoder.up_blocks.{i}.upsamplers.0.conv.bias"] = vae_state_dict[
|
| 133 |
+
f"decoder.up.{block_id}.upsample.conv.bias"
|
| 134 |
+
]
|
| 135 |
+
|
| 136 |
+
mid_resnets = [key for key in vae_state_dict if "decoder.mid.block" in key]
|
| 137 |
+
num_mid_res_blocks = 2
|
| 138 |
+
for i in range(1, num_mid_res_blocks + 1):
|
| 139 |
+
resnets = [key for key in mid_resnets if f"decoder.mid.block_{i}" in key]
|
| 140 |
+
update_vae_resnet_ldm_to_diffusers(
|
| 141 |
+
resnets,
|
| 142 |
+
new_checkpoint,
|
| 143 |
+
vae_state_dict,
|
| 144 |
+
mapping={"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"},
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
mid_attentions = [key for key in vae_state_dict if "decoder.mid.attn" in key]
|
| 148 |
+
update_vae_attentions_ldm_to_diffusers(
|
| 149 |
+
mid_attentions, new_checkpoint, vae_state_dict, mapping={"old": "mid.attn_1", "new": "mid_block.attentions.0"}
|
| 150 |
+
)
|
| 151 |
+
conv_attn_to_linear(new_checkpoint)
|
| 152 |
+
|
| 153 |
+
return new_checkpoint
|