Delete aoti.py
Browse files
aoti.py
DELETED
|
@@ -1,99 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
AOTI (Ahead-of-Time Compilation) loading utilities for ZeroGPU
|
| 3 |
-
Based on official HuggingFace zerogpu-aoti implementation
|
| 4 |
-
Author: cbensimon (HF Staff)
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from typing import cast
|
| 8 |
-
import torch
|
| 9 |
-
from huggingface_hub import hf_hub_download
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
from spaces.zero.torch.aoti import ZeroGPUCompiledModel, ZeroGPUWeights
|
| 13 |
-
from torch._functorch._aot_autograd.subclass_parametrization import unwrap_tensor_subclass_parameters
|
| 14 |
-
AOTI_AVAILABLE = True
|
| 15 |
-
except ImportError:
|
| 16 |
-
AOTI_AVAILABLE = False
|
| 17 |
-
ZeroGPUCompiledModel = None
|
| 18 |
-
ZeroGPUWeights = None
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def _shallow_clone_module(module: torch.nn.Module) -> torch.nn.Module:
|
| 22 |
-
"""Create a shallow clone of a PyTorch module."""
|
| 23 |
-
clone = object.__new__(module.__class__)
|
| 24 |
-
clone.__dict__ = module.__dict__.copy()
|
| 25 |
-
clone._parameters = module._parameters.copy()
|
| 26 |
-
clone._buffers = module._buffers.copy()
|
| 27 |
-
clone._modules = {
|
| 28 |
-
k: _shallow_clone_module(v)
|
| 29 |
-
for k, v in module._modules.items()
|
| 30 |
-
if v is not None
|
| 31 |
-
}
|
| 32 |
-
return clone
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def aoti_blocks_load(module: torch.nn.Module, repo_id: str, variant: str | None = None):
|
| 36 |
-
"""
|
| 37 |
-
Load AOTI compiled blocks for a transformer module.
|
| 38 |
-
|
| 39 |
-
Args:
|
| 40 |
-
module: The transformer module to optimize
|
| 41 |
-
repo_id: HuggingFace repo containing AOTI files (e.g., 'zerogpu-aoti/Wan2')
|
| 42 |
-
variant: Variant name (e.g., 'fp8da' for FP8 dynamic activation)
|
| 43 |
-
|
| 44 |
-
Raises:
|
| 45 |
-
ImportError: If AOTI dependencies are not available
|
| 46 |
-
Exception: If AOTI files cannot be downloaded or loaded
|
| 47 |
-
"""
|
| 48 |
-
if not AOTI_AVAILABLE:
|
| 49 |
-
raise ImportError(
|
| 50 |
-
"AOTI is not available. Please upgrade 'spaces' package:\n"
|
| 51 |
-
"pip install --upgrade spaces"
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# Get repeated blocks from module
|
| 55 |
-
if hasattr(module, '_repeated_blocks'):
|
| 56 |
-
repeated_blocks = cast(list[str], module._repeated_blocks)
|
| 57 |
-
else:
|
| 58 |
-
# Fallback: assume WanTransformerBlock for Wan models
|
| 59 |
-
print("⚠ Module doesn't have _repeated_blocks, using default 'WanTransformerBlock'")
|
| 60 |
-
repeated_blocks = ['WanTransformerBlock']
|
| 61 |
-
|
| 62 |
-
# Download AOTI files for each block type
|
| 63 |
-
aoti_files = {}
|
| 64 |
-
for name in repeated_blocks:
|
| 65 |
-
try:
|
| 66 |
-
subfolder = name if variant is None else f'{name}.{variant}'
|
| 67 |
-
aoti_file = hf_hub_download(
|
| 68 |
-
repo_id=repo_id,
|
| 69 |
-
filename='package.pt2',
|
| 70 |
-
subfolder=subfolder,
|
| 71 |
-
)
|
| 72 |
-
aoti_files[name] = aoti_file
|
| 73 |
-
print(f" ✓ Downloaded AOTI for {name}")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
print(f" ✗ Failed to download AOTI for {name}: {e}")
|
| 76 |
-
raise
|
| 77 |
-
|
| 78 |
-
# Replace forward methods with compiled versions
|
| 79 |
-
for block_name, aoti_file in aoti_files.items():
|
| 80 |
-
replaced_count = 0
|
| 81 |
-
for block in module.modules():
|
| 82 |
-
if block.__class__.__name__ == block_name:
|
| 83 |
-
# Clone and unwrap
|
| 84 |
-
block_ = _shallow_clone_module(block)
|
| 85 |
-
unwrap_tensor_subclass_parameters(block_)
|
| 86 |
-
|
| 87 |
-
# Extract weights
|
| 88 |
-
weights = ZeroGPUWeights(block_.state_dict())
|
| 89 |
-
|
| 90 |
-
# Replace forward with compiled version
|
| 91 |
-
block.forward = ZeroGPUCompiledModel(aoti_file, weights)
|
| 92 |
-
replaced_count += 1
|
| 93 |
-
|
| 94 |
-
print(f" ✓ Replaced {replaced_count} blocks of type {block_name}")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def check_aoti_available() -> bool:
|
| 98 |
-
"""Check if AOTI is available in the current environment."""
|
| 99 |
-
return AOTI_AVAILABLE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|