Spaces:
Running on Zero
Running on Zero
export .pt2 after compile + download button
Browse filesAfter first inference, tries torch.export to save compiled model as .pt2.
UI: Accordion "Compiled Model Artifacts" with download button.
Flow: run inference at 1024 → check for .pt2 → download
app.py
CHANGED
|
@@ -372,13 +372,26 @@ def _load_greenformer(img_size):
|
|
| 372 |
with torch.inference_mode():
|
| 373 |
compiled(dummy)
|
| 374 |
model = compiled
|
|
|
|
| 375 |
logger.info("torch.compile(max-autotune) succeeded in %.1fs — triton + CUDA graphs active",
|
| 376 |
-
|
| 377 |
except Exception as e:
|
| 378 |
logger.warning("torch.compile() failed (%.1fs), using eager mode: %s",
|
| 379 |
time.time() - t_compile, e)
|
| 380 |
torch.cuda.empty_cache()
|
| 381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
logger.info("GreenFormer loaded on CUDA (img_size=%d)", img_size)
|
| 383 |
return model
|
| 384 |
|
|
@@ -1008,6 +1021,15 @@ with gr.Blocks(title="CorridorKey") as demo:
|
|
| 1008 |
matte_video = gr.Video(label="Alpha Matte")
|
| 1009 |
download_zip = gr.File(label="Download Full Package (Comp + FG + Matte + Processed)")
|
| 1010 |
status_text = gr.Textbox(label="Status", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1011 |
|
| 1012 |
gr.Examples(
|
| 1013 |
examples=[
|
|
|
|
| 372 |
with torch.inference_mode():
|
| 373 |
compiled(dummy)
|
| 374 |
model = compiled
|
| 375 |
+
compile_time = time.time() - t_compile
|
| 376 |
logger.info("torch.compile(max-autotune) succeeded in %.1fs — triton + CUDA graphs active",
|
| 377 |
+
compile_time)
|
| 378 |
except Exception as e:
|
| 379 |
logger.warning("torch.compile() failed (%.1fs), using eager mode: %s",
|
| 380 |
time.time() - t_compile, e)
|
| 381 |
torch.cuda.empty_cache()
|
| 382 |
|
| 383 |
+
# Export compiled model as .pt2 for future pre-compiled loading
|
| 384 |
+
pt2_path = f"/tmp/corridorkey_{img_size}.pt2"
|
| 385 |
+
try:
|
| 386 |
+
unwrapped = model._orig_mod if hasattr(model, '_orig_mod') else model
|
| 387 |
+
dummy_export = torch.zeros(1, 4, img_size, img_size, dtype=torch.float16, device="cuda")
|
| 388 |
+
exported = torch.export.export(unwrapped, (dummy_export,))
|
| 389 |
+
torch.export.save(exported, pt2_path)
|
| 390 |
+
logger.info("Exported .pt2 artifact: %s (%.1f MB)",
|
| 391 |
+
pt2_path, os.path.getsize(pt2_path) / 1024 / 1024)
|
| 392 |
+
except Exception as e:
|
| 393 |
+
logger.info("torch.export skipped (model not fully traceable): %s", type(e).__name__)
|
| 394 |
+
|
| 395 |
logger.info("GreenFormer loaded on CUDA (img_size=%d)", img_size)
|
| 396 |
return model
|
| 397 |
|
|
|
|
| 1021 |
matte_video = gr.Video(label="Alpha Matte")
|
| 1022 |
download_zip = gr.File(label="Download Full Package (Comp + FG + Matte + Processed)")
|
| 1023 |
status_text = gr.Textbox(label="Status", interactive=False)
|
| 1024 |
+
with gr.Accordion("Compiled Model Artifacts", open=False):
|
| 1025 |
+
gr.Markdown("After first inference, compiled .pt2 artifacts are generated for pre-loaded triton speed.")
|
| 1026 |
+
pt2_download = gr.File(label="Download .pt2", interactive=False)
|
| 1027 |
+
pt2_btn = gr.Button("Check for .pt2", size="sm")
|
| 1028 |
+
def _get_pt2():
|
| 1029 |
+
import glob
|
| 1030 |
+
files = glob.glob("/tmp/corridorkey_*.pt2")
|
| 1031 |
+
return files if files else None
|
| 1032 |
+
pt2_btn.click(fn=_get_pt2, outputs=[pt2_download])
|
| 1033 |
|
| 1034 |
gr.Examples(
|
| 1035 |
examples=[
|