Spaces:
Running
on
Zero
Running
on
Zero
Fix AoT (#1)
Browse files- Fix AoT (35ee5b0267f6846d0c86392c55dd3bf67deb512c)
- Update app.py (152019b5b68dd7404d072590a79c42c29bd14814)
Co-authored-by: Charles Bensimon <[email protected]>
- app.py +12 -14
- optimization.py +46 -29
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 2 |
import subprocess
|
| 3 |
import sys
|
| 4 |
import io
|
| 5 |
-
from kernels import get_kernel
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
| 8 |
import random
|
|
@@ -38,14 +37,11 @@ def remote_text_encoder(prompts):
|
|
| 38 |
return prompt_embeds
|
| 39 |
|
| 40 |
# Load model
|
| 41 |
-
fa3_kernel = get_kernel("kernels-community/flash-attn3", revision="fake-ops-return-probs")
|
| 42 |
-
|
| 43 |
repo_id = "black-forest-labs/FLUX.2-dev"
|
| 44 |
|
| 45 |
dit = Flux2Transformer2DModel.from_pretrained(
|
| 46 |
repo_id,
|
| 47 |
subfolder="transformer",
|
| 48 |
-
attn_implementation=fa3_kernel,
|
| 49 |
torch_dtype=torch.bfloat16
|
| 50 |
)
|
| 51 |
|
|
@@ -56,16 +52,18 @@ pipe = Flux2Pipeline.from_pretrained(
|
|
| 56 |
torch_dtype=torch.bfloat16
|
| 57 |
)
|
| 58 |
pipe.to("cuda")
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
@spaces.GPU(duration=180)
|
| 71 |
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)):
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import sys
|
| 4 |
import io
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import random
|
|
|
|
| 37 |
return prompt_embeds
|
| 38 |
|
| 39 |
# Load model
|
|
|
|
|
|
|
| 40 |
repo_id = "black-forest-labs/FLUX.2-dev"
|
| 41 |
|
| 42 |
dit = Flux2Transformer2DModel.from_pretrained(
|
| 43 |
repo_id,
|
| 44 |
subfolder="transformer",
|
|
|
|
| 45 |
torch_dtype=torch.bfloat16
|
| 46 |
)
|
| 47 |
|
|
|
|
| 52 |
torch_dtype=torch.bfloat16
|
| 53 |
)
|
| 54 |
pipe.to("cuda")
|
| 55 |
+
|
| 56 |
+
pipe.transformer.set_attention_backend("_flash_3_hub")
|
| 57 |
+
|
| 58 |
+
optimize_pipeline_(
|
| 59 |
+
pipe,
|
| 60 |
+
image=[Image.new("RGB", (1024, 1024))],
|
| 61 |
+
prompt_embeds = remote_text_encoder("prompt").to("cuda"),
|
| 62 |
+
guidance_scale=2.5,
|
| 63 |
+
width=1024,
|
| 64 |
+
height=1024,
|
| 65 |
+
num_inference_steps=1
|
| 66 |
+
)
|
| 67 |
|
| 68 |
@spaces.GPU(duration=180)
|
| 69 |
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)):
|
optimization.py
CHANGED
|
@@ -6,25 +6,32 @@ 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 |
-
|
| 14 |
-
TRANSFORMER_TEXT_SEQ_LENGTH_DIM = torch.export.Dim('text_seq_length', min=64, max=512)
|
| 15 |
|
| 16 |
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 17 |
-
'
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
},
|
| 20 |
-
'
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
},
|
| 29 |
}
|
| 30 |
|
|
@@ -33,28 +40,38 @@ INDUCTOR_CONFIGS = {
|
|
| 33 |
'epilogue_fusion': False,
|
| 34 |
'coordinate_descent_tuning': True,
|
| 35 |
'coordinate_descent_check_all_directions': True,
|
| 36 |
-
|
| 37 |
-
|
| 38 |
}
|
| 39 |
|
| 40 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
pipeline(*args, **kwargs)
|
| 47 |
|
| 48 |
dynamic_shapes = tree_map(lambda t: None, call.kwargs)
|
| 49 |
-
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from typing import ParamSpec
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
+
from spaces.zero.torch.aoti import ZeroGPUCompiledModel
|
| 10 |
+
from spaces.zero.torch.aoti import ZeroGPUWeights
|
| 11 |
from torch.utils._pytree import tree_map
|
| 12 |
|
| 13 |
P = ParamSpec('P')
|
| 14 |
|
| 15 |
+
TRANSFORMER_IMAGE_DIM = torch.export.Dim('image_seq_length', min=4096, max=16384) # min: 0 images, max: 3 (1024x1024) images
|
|
|
|
| 16 |
|
| 17 |
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 18 |
+
'double': {
|
| 19 |
+
'hidden_states': {
|
| 20 |
+
1: TRANSFORMER_IMAGE_DIM,
|
| 21 |
+
},
|
| 22 |
+
'image_rotary_emb': (
|
| 23 |
+
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 24 |
+
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 25 |
+
),
|
| 26 |
},
|
| 27 |
+
'single': {
|
| 28 |
+
'hidden_states': {
|
| 29 |
+
1: TRANSFORMER_IMAGE_DIM + 512,
|
| 30 |
+
},
|
| 31 |
+
'image_rotary_emb': (
|
| 32 |
+
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 33 |
+
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 34 |
+
),
|
| 35 |
},
|
| 36 |
}
|
| 37 |
|
|
|
|
| 40 |
'epilogue_fusion': False,
|
| 41 |
'coordinate_descent_tuning': True,
|
| 42 |
'coordinate_descent_check_all_directions': True,
|
| 43 |
+
'max_autotune': True,
|
| 44 |
+
'triton.cudagraphs': True,
|
| 45 |
}
|
| 46 |
|
| 47 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 48 |
|
| 49 |
+
blocks = {
|
| 50 |
+
'double': pipeline.transformer.transformer_blocks,
|
| 51 |
+
'single': pipeline.transformer.single_transformer_blocks,
|
| 52 |
+
}
|
| 53 |
|
| 54 |
+
@spaces.GPU(duration=1200)
|
| 55 |
+
def compile_block(blocks_kind: str):
|
| 56 |
+
block = blocks[blocks_kind][0]
|
| 57 |
+
with spaces.aoti_capture(block) as call:
|
| 58 |
pipeline(*args, **kwargs)
|
| 59 |
|
| 60 |
dynamic_shapes = tree_map(lambda t: None, call.kwargs)
|
| 61 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES[blocks_kind]
|
| 62 |
+
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
exported = torch.export.export(
|
| 65 |
+
mod=block,
|
| 66 |
+
args=call.args,
|
| 67 |
+
kwargs=call.kwargs,
|
| 68 |
+
dynamic_shapes=dynamic_shapes,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return spaces.aoti_compile(exported, INDUCTOR_CONFIGS).archive_file
|
| 72 |
+
|
| 73 |
+
for blocks_kind in ('double', 'single'):
|
| 74 |
+
archive_file = compile_block(blocks_kind)
|
| 75 |
+
for block in blocks[blocks_kind]:
|
| 76 |
+
weights = ZeroGPUWeights(block.state_dict())
|
| 77 |
+
block.forward = ZeroGPUCompiledModel(archive_file, weights)
|