Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import torch
|
| 2 |
from diffusers import DiffusionPipeline
|
| 3 |
import spaces
|
| 4 |
-
from spaces.zero.torch.aoti import ZeroGPUCompiledModel, ZeroGPUWeights
|
| 5 |
from time import perf_counter
|
| 6 |
import argparse
|
| 7 |
|
|
@@ -10,8 +10,8 @@ CKPT_ID = "black-forest-labs/Flux.1-Dev"
|
|
| 10 |
def get_pipe_kwargs():
|
| 11 |
return {
|
| 12 |
"prompt": "A cat holding a sign that says hello world",
|
| 13 |
-
"height":
|
| 14 |
-
"width":
|
| 15 |
"guidance_scale": 3.5,
|
| 16 |
"num_inference_steps": 50,
|
| 17 |
"max_sequence_length": 512,
|
|
@@ -21,7 +21,7 @@ def get_pipe_kwargs():
|
|
| 21 |
def load_pipeline():
|
| 22 |
pipe = DiffusionPipeline.from_pretrained(
|
| 23 |
CKPT_ID,
|
| 24 |
-
torch_dtype=torch.float32, # CPU
|
| 25 |
device_map="cpu"
|
| 26 |
)
|
| 27 |
pipe.set_progress_bar_config(disable=True)
|
|
@@ -30,36 +30,32 @@ def load_pipeline():
|
|
| 30 |
@torch.no_grad()
|
| 31 |
def aot_compile_load(pipe, regional=False):
|
| 32 |
prompt = "example prompt"
|
| 33 |
-
|
| 34 |
torch.compiler.reset()
|
| 35 |
with torch._inductor.utils.fresh_inductor_cache():
|
| 36 |
if regional:
|
| 37 |
-
# Compile
|
| 38 |
-
for
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
exported = torch.export.export(block, args=call.args, kwargs=call.kwargs)
|
| 51 |
-
compiled = spaces.aoti_compile(exported)
|
| 52 |
-
weights = ZeroGPUWeights(block.state_dict())
|
| 53 |
-
compiled_block = ZeroGPUCompiledModel(compiled.archive_file, weights)
|
| 54 |
-
block.forward = compiled_block
|
| 55 |
else:
|
| 56 |
-
# Compile the whole transformer
|
| 57 |
-
with
|
| 58 |
pipe(prompt=prompt)
|
| 59 |
exported = torch.export.export(pipe.transformer, args=call.args, kwargs=call.kwargs)
|
| 60 |
-
compiled =
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
return pipe
|
| 64 |
|
| 65 |
def measure_compile_time(pipe, regional=False):
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import DiffusionPipeline
|
| 3 |
import spaces
|
| 4 |
+
from spaces.zero.torch.aoti import ZeroGPUCompiledModel, ZeroGPUWeights, aoti_capture, aoti_compile, aoti_apply
|
| 5 |
from time import perf_counter
|
| 6 |
import argparse
|
| 7 |
|
|
|
|
| 10 |
def get_pipe_kwargs():
|
| 11 |
return {
|
| 12 |
"prompt": "A cat holding a sign that says hello world",
|
| 13 |
+
"height": 512, # reduce memory usage
|
| 14 |
+
"width": 512,
|
| 15 |
"guidance_scale": 3.5,
|
| 16 |
"num_inference_steps": 50,
|
| 17 |
"max_sequence_length": 512,
|
|
|
|
| 21 |
def load_pipeline():
|
| 22 |
pipe = DiffusionPipeline.from_pretrained(
|
| 23 |
CKPT_ID,
|
| 24 |
+
torch_dtype=torch.float32, # CPU-only
|
| 25 |
device_map="cpu"
|
| 26 |
)
|
| 27 |
pipe.set_progress_bar_config(disable=True)
|
|
|
|
| 30 |
@torch.no_grad()
|
| 31 |
def aot_compile_load(pipe, regional=False):
|
| 32 |
prompt = "example prompt"
|
| 33 |
+
|
| 34 |
torch.compiler.reset()
|
| 35 |
with torch._inductor.utils.fresh_inductor_cache():
|
| 36 |
if regional:
|
| 37 |
+
# Compile transformer blocks **one at a time** to save memory
|
| 38 |
+
for block_list in [pipe.transformer.transformer_blocks, pipe.transformer.single_transformer_blocks]:
|
| 39 |
+
for i, block in enumerate(block_list):
|
| 40 |
+
with aoti_capture(block) as call:
|
| 41 |
+
pipe(prompt=prompt)
|
| 42 |
+
exported = torch.export.export(block, args=call.args, kwargs=call.kwargs)
|
| 43 |
+
compiled = aoti_compile(exported)
|
| 44 |
+
weights = ZeroGPUWeights(block.state_dict())
|
| 45 |
+
compiled_block = ZeroGPUCompiledModel(compiled.archive_file, weights)
|
| 46 |
+
block.forward = compiled_block # replace forward with compiled block
|
| 47 |
+
# Free memory
|
| 48 |
+
del exported, compiled, weights, compiled_block, call
|
| 49 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
+
# Compile the whole transformer at once
|
| 52 |
+
with aoti_capture(pipe.transformer) as call:
|
| 53 |
pipe(prompt=prompt)
|
| 54 |
exported = torch.export.export(pipe.transformer, args=call.args, kwargs=call.kwargs)
|
| 55 |
+
compiled = aoti_compile(exported)
|
| 56 |
+
aoti_apply(compiled, pipe.transformer)
|
| 57 |
+
del exported, compiled, call
|
| 58 |
+
|
| 59 |
return pipe
|
| 60 |
|
| 61 |
def measure_compile_time(pipe, regional=False):
|