Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
7f4c99b
verified
·
0
Parent(s):
Squashing commit
Browse filesCo-authored-by: linoyts <linoyts@users.noreply.huggingface.co>
- .gitattributes +36 -0
- README.md +12 -0
- app.py +256 -0
- bird.webp +3 -0
- cat_window.webp +0 -0
- optimization.py +60 -0
- person1.webp +0 -0
- requirements.txt +6 -0
- woman1.webp +0 -0
- woman2.webp +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 36 |
+
bird.webp filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: FLUX.2-dev
|
| 3 |
+
emoji: 💻
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.0.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
import io
|
| 5 |
+
from kernels import get_kernel
|
| 6 |
+
|
| 7 |
+
# Clone and install diffusers from private repo
|
| 8 |
+
def setup_diffusers():
|
| 9 |
+
gh_token = os.environ.get("GH_API_KEY")
|
| 10 |
+
if not gh_token:
|
| 11 |
+
raise ValueError("GH_API_KEY environment variable not set")
|
| 12 |
+
|
| 13 |
+
repo_url = f"https://{gh_token}@github.com/apolinario/diffusers-new-model-addition.git"
|
| 14 |
+
repo_dir = "diffusers"
|
| 15 |
+
|
| 16 |
+
# Clone repo if it doesn't exist
|
| 17 |
+
if not os.path.exists(repo_dir):
|
| 18 |
+
print("Cloning diffusers repository...")
|
| 19 |
+
subprocess.run(["git", "clone", repo_url, repo_dir], check=True)
|
| 20 |
+
|
| 21 |
+
# Checkout the branch
|
| 22 |
+
#print("Checking out full-flux2 branch...")
|
| 23 |
+
#subprocess.run(["git", "-C", repo_dir, "checkout", "full-flux2"], check=True)
|
| 24 |
+
|
| 25 |
+
# Install in editable mode
|
| 26 |
+
print("Installing diffusers in editable mode...")
|
| 27 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "-e", repo_dir], check=True)
|
| 28 |
+
|
| 29 |
+
# Add to Python path
|
| 30 |
+
diffusers_src = os.path.abspath(os.path.join(repo_dir, "src"))
|
| 31 |
+
if diffusers_src not in sys.path:
|
| 32 |
+
sys.path.insert(0, diffusers_src)
|
| 33 |
+
|
| 34 |
+
print("Diffusers setup complete!")
|
| 35 |
+
|
| 36 |
+
# Run setup
|
| 37 |
+
setup_diffusers()
|
| 38 |
+
|
| 39 |
+
import gradio as gr
|
| 40 |
+
import numpy as np
|
| 41 |
+
import random
|
| 42 |
+
import spaces
|
| 43 |
+
import torch
|
| 44 |
+
from diffusers import Flux2Pipeline, Flux2Transformer2DModel
|
| 45 |
+
from diffusers import BitsAndBytesConfig as DiffBitsAndBytesConfig
|
| 46 |
+
from optimization import optimize_pipeline_
|
| 47 |
+
import requests
|
| 48 |
+
from PIL import Image
|
| 49 |
+
import json
|
| 50 |
+
|
| 51 |
+
dtype = torch.bfloat16
|
| 52 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 53 |
+
|
| 54 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 55 |
+
MAX_IMAGE_SIZE = 1024
|
| 56 |
+
|
| 57 |
+
def remote_text_encoder(prompts):
|
| 58 |
+
response = requests.post(
|
| 59 |
+
"https://remote-text-encoder-flux-2.huggingface.co/predict",
|
| 60 |
+
json={"prompt": prompts},
|
| 61 |
+
headers={
|
| 62 |
+
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
|
| 63 |
+
"Content-Type": "application/json"
|
| 64 |
+
}
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
assert response.status_code == 200, f"{response.status_code=}"
|
| 68 |
+
|
| 69 |
+
prompt_embeds = torch.load(io.BytesIO(response.content))
|
| 70 |
+
|
| 71 |
+
return prompt_embeds
|
| 72 |
+
|
| 73 |
+
# Load model
|
| 74 |
+
fa3_kernel = get_kernel("kernels-community/flash-attn3", revision="fake-ops-return-probs")
|
| 75 |
+
|
| 76 |
+
repo_id = "diffusers-internal-dev/new-model-image-final-weights"
|
| 77 |
+
dit = Flux2Transformer2DModel.from_pretrained(
|
| 78 |
+
repo_id,
|
| 79 |
+
subfolder="transformer",
|
| 80 |
+
attn_implementation=fa3_kernel,
|
| 81 |
+
torch_dtype=torch.bfloat16
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
pipe = Flux2Pipeline.from_pretrained(
|
| 85 |
+
repo_id,
|
| 86 |
+
text_encoder=None,
|
| 87 |
+
transformer=dit,
|
| 88 |
+
torch_dtype=torch.bfloat16
|
| 89 |
+
)
|
| 90 |
+
pipe.to("cuda")
|
| 91 |
+
# pipe.transformer.compile_repeated_blocks(dynamic=True)
|
| 92 |
+
|
| 93 |
+
#optimize_pipeline_(
|
| 94 |
+
# pipe,
|
| 95 |
+
# prompt_embeds=remote_text_encoder("prompt").to("cuda"),
|
| 96 |
+
# guidance_scale=2.5,
|
| 97 |
+
# width=1024,
|
| 98 |
+
# height=1024,
|
| 99 |
+
# num_inference_steps=1,
|
| 100 |
+
#)
|
| 101 |
+
|
| 102 |
+
@spaces.GPU(duration=180)
|
| 103 |
+
def infer(prompt, input_images, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
|
| 104 |
+
|
| 105 |
+
if randomize_seed:
|
| 106 |
+
seed = random.randint(0, MAX_SEED)
|
| 107 |
+
|
| 108 |
+
# Get prompt embeddings from remote text encoder
|
| 109 |
+
progress(0.1, desc="Encoding prompt...")
|
| 110 |
+
prompt_embeds = remote_text_encoder(prompt).to("cuda")
|
| 111 |
+
|
| 112 |
+
# Prepare image list (convert None or empty gallery to None)
|
| 113 |
+
image_list = None
|
| 114 |
+
if input_images is not None and len(input_images) > 0:
|
| 115 |
+
image_list = []
|
| 116 |
+
for item in input_images:
|
| 117 |
+
image_list.append(item[0])
|
| 118 |
+
|
| 119 |
+
# Generate image
|
| 120 |
+
progress(0.3, desc="Generating image...")
|
| 121 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 122 |
+
image = pipe(
|
| 123 |
+
prompt_embeds=prompt_embeds,
|
| 124 |
+
image=image_list,
|
| 125 |
+
width=width,
|
| 126 |
+
height=height,
|
| 127 |
+
num_inference_steps=num_inference_steps,
|
| 128 |
+
guidance_scale=guidance_scale,
|
| 129 |
+
generator=generator,
|
| 130 |
+
).images[0]
|
| 131 |
+
|
| 132 |
+
return image, seed
|
| 133 |
+
|
| 134 |
+
examples = [
|
| 135 |
+
["Create a vase on a table in living room, the color of the vase is a gradient of color, starting with #02eb3c color and finishing with #edfa3c. The flowers inside the vase have the color #ff0088", None],
|
| 136 |
+
["Photorealistic infographic showing the complete Berlin TV Tower (Fernsehturm) from ground base to antenna tip, full vertical view with entire structure visible including concrete shaft, metallic sphere, and antenna spire. Slight upward perspective angle looking up toward the iconic sphere, perfectly centered on clean white background. Left side labels with thin horizontal connector lines: the text '368m' in extra large bold dark grey numerals (#2D3748) positioned at exactly the antenna tip with 'TOTAL HEIGHT' in small caps below. The text '207m' in extra large bold with 'TELECAFÉ' in small caps below, with connector line touching the sphere precisely at the window level. Right side label with horizontal connector line touching the sphere's equator: the text '32m' in extra large bold dark grey numerals with 'SPHERE DIAMETER' in small caps below. Bottom section arranged in three balanced columns: Left - Large text '986' in extra bold dark grey with 'STEPS' in caps below. Center - 'BERLIN TV TOWER' in bold caps with 'FERNSEHTURM' in lighter weight below. Right - 'INAUGURATED' in bold caps with 'OCTOBER 3, 1969' below. All typography in modern sans-serif font (such as Inter or Helvetica), color #2D3748, clean minimal technical diagram style. Horizontal connector lines are thin, precise, and clearly visible, touching the tower structure at exact corresponding measurement points. Professional architectural elevation drawing aesthetic with dynamic low angle perspective creating sense of height and grandeur, poster-ready infographic design with perfect visual hierarchy.", None],
|
| 137 |
+
["Soaking wet capybara taking shelter under a banana leaf in the rainy jungle, close up photo", None],
|
| 138 |
+
["A kawaii die-cut sticker of a chubby orange cat, featuring big sparkly eyes and a happy smile with paws raised in greeting and a heart-shaped pink nose. The design should have smooth rounded lines with black outlines and soft gradient shading with pink cheeks.", None],
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
examples_images = [
|
| 142 |
+
# ["Replace the top of the person from image 1 with the one from image 2", ["person1.webp", "woman2.webp"]],
|
| 143 |
+
["The person from image 1 is petting the cat from image 2, the bird from image 3 is next to them", ["woman1.webp", "cat_window.webp", "bird.webp"]]
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
css="""
|
| 147 |
+
#col-container {
|
| 148 |
+
margin: 0 auto;
|
| 149 |
+
max-width: 620px;
|
| 150 |
+
}
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
with gr.Blocks() as demo:
|
| 154 |
+
|
| 155 |
+
with gr.Column(elem_id="col-container"):
|
| 156 |
+
gr.Markdown(f"""# FLUX.2 [dev]
|
| 157 |
+
32B param rectified flow transformer guidance-distilled from [FLUX.2 pro](#) [[model](#)], [[blog](#)]
|
| 158 |
+
""")
|
| 159 |
+
|
| 160 |
+
with gr.Accordion("Input image(s) (optional)", open=False):
|
| 161 |
+
input_images = gr.Gallery(
|
| 162 |
+
label="Input Image(s)",
|
| 163 |
+
type="pil",
|
| 164 |
+
columns=3,
|
| 165 |
+
rows=1,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
with gr.Row():
|
| 169 |
+
|
| 170 |
+
prompt = gr.Text(
|
| 171 |
+
label="Prompt",
|
| 172 |
+
show_label=False,
|
| 173 |
+
max_lines=2,
|
| 174 |
+
placeholder="Enter your prompt",
|
| 175 |
+
container=False,
|
| 176 |
+
scale=3
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
run_button = gr.Button("Run", scale=1)
|
| 180 |
+
|
| 181 |
+
result = gr.Image(label="Result", show_label=False)
|
| 182 |
+
|
| 183 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 184 |
+
|
| 185 |
+
seed = gr.Slider(
|
| 186 |
+
label="Seed",
|
| 187 |
+
minimum=0,
|
| 188 |
+
maximum=MAX_SEED,
|
| 189 |
+
step=1,
|
| 190 |
+
value=0,
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 194 |
+
|
| 195 |
+
with gr.Row():
|
| 196 |
+
|
| 197 |
+
width = gr.Slider(
|
| 198 |
+
label="Width",
|
| 199 |
+
minimum=256,
|
| 200 |
+
maximum=MAX_IMAGE_SIZE,
|
| 201 |
+
step=32,
|
| 202 |
+
value=1024,
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
height = gr.Slider(
|
| 206 |
+
label="Height",
|
| 207 |
+
minimum=256,
|
| 208 |
+
maximum=MAX_IMAGE_SIZE,
|
| 209 |
+
step=32,
|
| 210 |
+
value=1024,
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
with gr.Row():
|
| 214 |
+
|
| 215 |
+
num_inference_steps = gr.Slider(
|
| 216 |
+
label="Number of inference steps",
|
| 217 |
+
minimum=1,
|
| 218 |
+
maximum=100,
|
| 219 |
+
step=1,
|
| 220 |
+
value=30,
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
guidance_scale = gr.Slider(
|
| 224 |
+
label="Guidance scale",
|
| 225 |
+
minimum=0.0,
|
| 226 |
+
maximum=10.0,
|
| 227 |
+
step=0.1,
|
| 228 |
+
value=4,
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
gr.Examples(
|
| 232 |
+
examples=examples,
|
| 233 |
+
fn=infer,
|
| 234 |
+
inputs=[prompt, input_images],
|
| 235 |
+
outputs=[result, seed],
|
| 236 |
+
cache_examples=True,
|
| 237 |
+
cache_mode="lazy"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
gr.Examples(
|
| 241 |
+
examples=examples_images,
|
| 242 |
+
fn=infer,
|
| 243 |
+
inputs=[prompt, input_images],
|
| 244 |
+
outputs=[result, seed],
|
| 245 |
+
cache_examples=True,
|
| 246 |
+
cache_mode="lazy"
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
gr.on(
|
| 250 |
+
triggers=[run_button.click, prompt.submit],
|
| 251 |
+
fn=infer,
|
| 252 |
+
inputs=[prompt, input_images, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
|
| 253 |
+
outputs=[result, seed]
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
demo.launch(css=css)
|
bird.webp
ADDED
|
Git LFS Details
|
cat_window.webp
ADDED
|
optimization.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
from typing import Any
|
| 5 |
+
from typing import Callable
|
| 6 |
+
from typing import ParamSpec
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
+
from torch.utils._pytree import tree_map
|
| 10 |
+
|
| 11 |
+
P = ParamSpec('P')
|
| 12 |
+
|
| 13 |
+
TRANSFORMER_IMAGE_SEQ_LENGTH_DIM = torch.export.Dim('image_seq_length', min=64, max=16384)
|
| 14 |
+
TRANSFORMER_TEXT_SEQ_LENGTH_DIM = torch.export.Dim('text_seq_length', min=64, max=512)
|
| 15 |
+
|
| 16 |
+
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 17 |
+
'hidden_states': {
|
| 18 |
+
1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM,
|
| 19 |
+
},
|
| 20 |
+
'encoder_hidden_states': {
|
| 21 |
+
1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM,
|
| 22 |
+
},
|
| 23 |
+
'img_ids': {
|
| 24 |
+
1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM,
|
| 25 |
+
},
|
| 26 |
+
'txt_ids': {
|
| 27 |
+
1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM,
|
| 28 |
+
},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
INDUCTOR_CONFIGS = {
|
| 32 |
+
'conv_1x1_as_mm': True,
|
| 33 |
+
'epilogue_fusion': False,
|
| 34 |
+
'coordinate_descent_tuning': True,
|
| 35 |
+
'coordinate_descent_check_all_directions': True,
|
| 36 |
+
#'max_autotune': True,
|
| 37 |
+
#'triton.cudagraphs': True,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 41 |
+
|
| 42 |
+
@spaces.GPU(duration=1500)
|
| 43 |
+
def compile_transformer():
|
| 44 |
+
|
| 45 |
+
with spaces.aoti_capture(pipeline.transformer) as call:
|
| 46 |
+
pipeline(*args, **kwargs)
|
| 47 |
+
|
| 48 |
+
dynamic_shapes = tree_map(lambda t: None, call.kwargs)
|
| 49 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 50 |
+
|
| 51 |
+
exported = torch.export.export(
|
| 52 |
+
mod=pipeline.transformer,
|
| 53 |
+
args=call.args,
|
| 54 |
+
kwargs=call.kwargs,
|
| 55 |
+
dynamic_shapes=dynamic_shapes,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)
|
| 59 |
+
|
| 60 |
+
spaces.aoti_apply(compile_transformer(), pipeline.transformer)
|
person1.webp
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
accelerate
|
| 3 |
+
safetensors
|
| 4 |
+
bitsandbytes
|
| 5 |
+
torchao
|
| 6 |
+
kernels
|
woman1.webp
ADDED
|
woman2.webp
ADDED
|