Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -27,63 +27,34 @@ pipe = JoyImageEditPlusPipeline.from_pretrained(
|
|
| 27 |
)
|
| 28 |
pipe.enable_model_cpu_offload()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
# transformer
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
prompt="a photo",
|
| 41 |
-
negative_prompt="",
|
| 42 |
-
height=512,
|
| 43 |
-
width=512,
|
| 44 |
-
num_inference_steps=1,
|
| 45 |
-
guidance_scale=1.0,
|
| 46 |
-
generator=torch.Generator(device="cpu").manual_seed(0),
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def compile_transformer_blocks():
|
| 52 |
-
"""Ahead-of-time compile the repeated transformer block and apply it.
|
| 53 |
-
|
| 54 |
-
The transformer is a stack of identical ``JoyImageEditPlusTransformerBlock``
|
| 55 |
-
modules (``pipe.transformer.double_blocks``). We capture one block's real
|
| 56 |
-
inputs from a 1-step run, export + AoTI-compile that single block graph, and
|
| 57 |
-
swap the compiled forward into every block. Weights stay runtime inputs, so
|
| 58 |
-
one compiled graph serves all layers. This removes eager per-step overhead
|
| 59 |
-
and materially speeds up inference. torch.compile (JIT) is unsupported on
|
| 60 |
-
ZeroGPU, so this ahead-of-time path is the supported optimization.
|
| 61 |
-
"""
|
| 62 |
-
blocks = pipe.transformer.double_blocks
|
| 63 |
-
block = blocks[0]
|
| 64 |
-
|
| 65 |
-
with spaces.aoti_capture(block) as call:
|
| 66 |
-
_warmup_run()
|
| 67 |
-
|
| 68 |
-
exported = torch.export.export(
|
| 69 |
-
block,
|
| 70 |
-
args=call.args,
|
| 71 |
-
kwargs=call.kwargs,
|
| 72 |
-
strict=False,
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
try:
|
| 83 |
-
_n = compile_transformer_blocks()
|
| 84 |
-
print(f"AoTI: compiled and applied to {_n} transformer blocks")
|
| 85 |
-
except Exception as e: # noqa: BLE001 -- keep the Space running if AoTI fails
|
| 86 |
-
print(f"AoTI compile failed ({e!r}); running eager")
|
| 87 |
|
| 88 |
|
| 89 |
@spaces.GPU(duration=300)
|
|
|
|
| 27 |
)
|
| 28 |
pipe.enable_model_cpu_offload()
|
| 29 |
|
| 30 |
+
# AoTI: load the pre-compiled repeated transformer block graph from the Hub and
|
| 31 |
+
# apply it to every ``JoyImageEditPlusTransformerBlock``
|
| 32 |
+
# (``pipe.transformer.double_blocks``, 40 of them). The graph is compiled
|
| 33 |
+
# offline by the one-shot Space multimodalart/joyai-image-edit-plus-aoti-export
|
| 34 |
+
# and published to the public dataset below as ``package/root/package.pt2``.
|
| 35 |
+
# Weights stay runtime inputs, so the single compiled graph serves all blocks —
|
| 36 |
+
# and no torch.compile happens in this live demo process. Falls back to eager
|
| 37 |
+
# execution if the artifact can't be downloaded/applied.
|
| 38 |
+
AOTI_DATASET_REPO = "multimodalart/joyai-image-edit-plus-aoti-pt2"
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
from huggingface_hub import snapshot_download
|
| 42 |
|
| 43 |
+
_aoti_dir = snapshot_download(
|
| 44 |
+
repo_id=AOTI_DATASET_REPO,
|
| 45 |
+
repo_type="dataset",
|
| 46 |
+
allow_patterns="package/*",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
+
spaces.aoti_load_from_package_dir(
|
| 49 |
+
pipe.transformer.double_blocks,
|
| 50 |
+
f"{_aoti_dir}/package",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
+
print(
|
| 53 |
+
f"AoTI: loaded compiled block graph from {AOTI_DATASET_REPO} and applied "
|
| 54 |
+
f"to {len(pipe.transformer.double_blocks)} transformer blocks"
|
| 55 |
+
)
|
| 56 |
+
except Exception as e: # noqa: BLE001 -- keep the Space running if AoTI load fails
|
| 57 |
+
print(f"AoTI load failed ({e!r}); running eager")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
@spaces.GPU(duration=300)
|