Nekochu commited on
Commit
449e82f
·
1 Parent(s): 7720399

triple-fallback artifact export + triton cache

Browse files

After compile, tries in order:
1. AOTInductor aoti_compile_and_package → .pt2 (best)
2. torch.compiler.save_cache_artifacts → mega-cache .bin (PyTorch 2.7+)
3. In-memory only (fallback)
Also: TRITON_CACHE_DIR set for kernel persistence
Download button finds all artifact types

Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -52,9 +52,10 @@ try:
52
  del _torch
53
  except ImportError:
54
  pass
55
- # Persist torch.compile inductor cache across ZeroGPU sessions
56
  _inductor_cache = os.path.join(os.path.expanduser("~"), ".cache", "corridorkey", "inductor")
57
  os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR", _inductor_cache)
 
58
 
59
  # Workaround: Gradio cache_examples bug with None outputs.
60
  _original_read_from_flag = gr.components.Component.read_from_flag
@@ -380,17 +381,29 @@ def _load_greenformer(img_size):
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
@@ -1027,7 +1040,7 @@ with gr.Blocks(title="CorridorKey") as demo:
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
 
 
52
  del _torch
53
  except ImportError:
54
  pass
55
+ # Persist compilation caches across ZeroGPU sessions
56
  _inductor_cache = os.path.join(os.path.expanduser("~"), ".cache", "corridorkey", "inductor")
57
  os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR", _inductor_cache)
58
+ os.environ.setdefault("TRITON_CACHE_DIR", os.path.join(os.path.expanduser("~"), ".cache", "corridorkey", "triton"))
59
 
60
  # Workaround: Gradio cache_examples bug with None outputs.
61
  _original_read_from_flag = gr.components.Component.read_from_flag
 
381
  time.time() - t_compile, e)
382
  torch.cuda.empty_cache()
383
 
384
+ # Export compiled artifacts for future pre-compiled loading
385
  pt2_path = f"/tmp/corridorkey_{img_size}.pt2"
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
+ # Method 1: AOTInductor .pt2 (best — standalone compiled artifact)
389
  try:
 
 
390
  exported = torch.export.export(unwrapped, (dummy_export,))
391
+ torch._inductor.aoti_compile_and_package(exported, package_path=pt2_path)
392
+ logger.info("AOTInductor .pt2 exported: %s (%.1f MB)",
393
  pt2_path, os.path.getsize(pt2_path) / 1024 / 1024)
394
  except Exception as e:
395
+ logger.info("AOTInductor export failed (%s), trying mega-cache...", type(e).__name__)
396
+ # Method 2: Portable mega-cache (PyTorch 2.7+)
397
+ try:
398
+ artifact_bytes, cache_info = torch.compiler.save_cache_artifacts()
399
+ cache_path = f"/tmp/corridorkey_{img_size}_cache.bin"
400
+ with open(cache_path, "wb") as f:
401
+ f.write(artifact_bytes)
402
+ logger.info("Mega-cache saved: %s (%.1f MB, %s)",
403
+ cache_path, len(artifact_bytes) / 1024 / 1024, cache_info)
404
+ except Exception as e2:
405
+ logger.info("Mega-cache also failed (%s) — triton kernels cached in memory only",
406
+ type(e2).__name__)
407
 
408
  logger.info("GreenFormer loaded on CUDA (img_size=%d)", img_size)
409
  return model
 
1040
  pt2_btn = gr.Button("Check for .pt2", size="sm")
1041
  def _get_pt2():
1042
  import glob
1043
+ files = glob.glob("/tmp/corridorkey_*.*")
1044
  return files if files else None
1045
  pt2_btn.click(fn=_get_pt2, outputs=[pt2_download])
1046