Spaces:
Runtime error
Runtime error
Format
Browse files- optimization.py +29 -44
optimization.py
CHANGED
|
@@ -17,16 +17,11 @@ from huggingface_hub import hf_hub_download
|
|
| 17 |
from optimization_utils import capture_component_call
|
| 18 |
from optimization_utils import aoti_compile
|
| 19 |
from optimization_utils import drain_module_parameters
|
| 20 |
-
from optimization_utils import zerogpu_compiled_from_serializable_dict
|
| 21 |
from optimization_utils import ZeroGPUCompiledModel
|
| 22 |
|
| 23 |
|
| 24 |
P = ParamSpec('P')
|
| 25 |
|
| 26 |
-
# Expose compiled models so app.py can offer them for download
|
| 27 |
-
COMPILED_TRANSFORMER_1 = None
|
| 28 |
-
COMPILED_TRANSFORMER_2 = None
|
| 29 |
-
|
| 30 |
LATENT_FRAMES_DIM = torch.export.Dim('num_latent_frames', min=8, max=81)
|
| 31 |
LATENT_PATCHED_HEIGHT_DIM = torch.export.Dim('latent_patched_height', min=30, max=52)
|
| 32 |
LATENT_PATCHED_WIDTH_DIM = torch.export.Dim('latent_patched_width', min=30, max=52)
|
|
@@ -55,50 +50,35 @@ def _strtobool(v: str | None, default: bool = True) -> bool:
|
|
| 55 |
return v.strip().lower() in ("1", "true", "yes", "y", "on")
|
| 56 |
|
| 57 |
|
| 58 |
-
def _load_compiled_pt(path: str):
|
| 59 |
-
"""
|
| 60 |
-
Load either:
|
| 61 |
-
- a serialized dict produced by to_serializable_dict() (format zerogpu_aoti_v1), or
|
| 62 |
-
- an old-style pickled ZeroGPUCompiledModel.
|
| 63 |
-
"""
|
| 64 |
-
obj = torch.load(path, map_location="cpu", weights_only=False)
|
| 65 |
-
|
| 66 |
-
# New format: dict payload
|
| 67 |
-
if isinstance(obj, dict) and obj.get("format") == "zerogpu_aoti_v1":
|
| 68 |
-
return zerogpu_compiled_from_serializable_dict(obj)
|
| 69 |
-
|
| 70 |
-
# Old format: direct object
|
| 71 |
-
if isinstance(obj, ZeroGPUCompiledModel):
|
| 72 |
-
return obj
|
| 73 |
-
|
| 74 |
-
raise ValueError(
|
| 75 |
-
f"Unsupported compiled transformer file format at {path}. "
|
| 76 |
-
f"Got type={type(obj)} keys={list(obj.keys()) if isinstance(obj, dict) else None}"
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
def load_compiled_transformers_from_hub(
|
| 81 |
repo_id: str,
|
| 82 |
filename_1: str = "compiled_transformer_1.pt",
|
| 83 |
filename_2: str = "compiled_transformer_2.pt",
|
|
|
|
| 84 |
):
|
| 85 |
"""
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
(format 'zerogpu_aoti_v1') OU un pickle direct de ZeroGPUCompiledModel.
|
| 91 |
"""
|
| 92 |
path_1 = hf_hub_download(repo_id=repo_id, filename=filename_1)
|
| 93 |
path_2 = hf_hub_download(repo_id=repo_id, filename=filename_2)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
return compiled_1, compiled_2
|
| 98 |
|
| 99 |
|
| 100 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
@spaces.GPU(duration=1500)
|
| 104 |
def compile_transformer():
|
|
@@ -145,23 +125,28 @@ def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kw
|
|
| 145 |
compiled_2 = aoti_compile(exported_2, INDUCTOR_CONFIGS)
|
| 146 |
return compiled_1, compiled_2
|
| 147 |
|
| 148 |
-
#
|
| 149 |
quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
|
| 150 |
|
| 151 |
-
use_precompiled =
|
| 152 |
precompiled_repo = os.getenv("WAN_PRECOMPILED_REPO", "Fabrice-TIERCELIN/Wan_2.2_compiled")
|
| 153 |
|
|
|
|
|
|
|
|
|
|
| 154 |
if use_precompiled:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
else:
|
| 159 |
compiled_transformer_1, compiled_transformer_2 = compile_transformer()
|
| 160 |
|
| 161 |
-
# expose for downloads
|
| 162 |
-
COMPILED_TRANSFORMER_1 = compiled_transformer_1
|
| 163 |
-
COMPILED_TRANSFORMER_2 = compiled_transformer_2
|
| 164 |
-
|
| 165 |
pipeline.transformer.forward = compiled_transformer_1
|
| 166 |
drain_module_parameters(pipeline.transformer)
|
| 167 |
|
|
|
|
| 17 |
from optimization_utils import capture_component_call
|
| 18 |
from optimization_utils import aoti_compile
|
| 19 |
from optimization_utils import drain_module_parameters
|
|
|
|
| 20 |
from optimization_utils import ZeroGPUCompiledModel
|
| 21 |
|
| 22 |
|
| 23 |
P = ParamSpec('P')
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
LATENT_FRAMES_DIM = torch.export.Dim('num_latent_frames', min=8, max=81)
|
| 26 |
LATENT_PATCHED_HEIGHT_DIM = torch.export.Dim('latent_patched_height', min=30, max=52)
|
| 27 |
LATENT_PATCHED_WIDTH_DIM = torch.export.Dim('latent_patched_width', min=30, max=52)
|
|
|
|
| 50 |
return v.strip().lower() in ("1", "true", "yes", "y", "on")
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def load_compiled_transformers_from_hub(
|
| 54 |
repo_id: str,
|
| 55 |
filename_1: str = "compiled_transformer_1.pt",
|
| 56 |
filename_2: str = "compiled_transformer_2.pt",
|
| 57 |
+
device: str = "cuda",
|
| 58 |
):
|
| 59 |
"""
|
| 60 |
+
Les fichiers du Hub contiennent un dict (payload) créé via ct.to_serializable_dict()
|
| 61 |
+
sauvegardé par torch.save(). On doit donc:
|
| 62 |
+
1) torch.load() -> payload dict
|
| 63 |
+
2) reconstruire un ZeroGPUCompiledModel avec les constantes sur CUDA
|
|
|
|
| 64 |
"""
|
| 65 |
path_1 = hf_hub_download(repo_id=repo_id, filename=filename_1)
|
| 66 |
path_2 = hf_hub_download(repo_id=repo_id, filename=filename_2)
|
| 67 |
|
| 68 |
+
payload_1 = torch.load(path_1, map_location="cpu", weights_only=False)
|
| 69 |
+
payload_2 = torch.load(path_2, map_location="cpu", weights_only=False)
|
| 70 |
+
|
| 71 |
+
compiled_1 = ZeroGPUCompiledModel.from_serializable_dict(payload_1, device=device)
|
| 72 |
+
compiled_2 = ZeroGPUCompiledModel.from_serializable_dict(payload_2, device=device)
|
| 73 |
return compiled_1, compiled_2
|
| 74 |
|
| 75 |
|
| 76 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 77 |
+
"""
|
| 78 |
+
- Si WAN_USE_PRECOMPILED=1 -> essaie de charger depuis le Hub.
|
| 79 |
+
Si ça échoue (format incompatible, etc.), fallback compilation GPU.
|
| 80 |
+
- Sinon -> compile sur GPU.
|
| 81 |
+
"""
|
| 82 |
|
| 83 |
@spaces.GPU(duration=1500)
|
| 84 |
def compile_transformer():
|
|
|
|
| 125 |
compiled_2 = aoti_compile(exported_2, INDUCTOR_CONFIGS)
|
| 126 |
return compiled_1, compiled_2
|
| 127 |
|
| 128 |
+
# Quantize text encoder
|
| 129 |
quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
|
| 130 |
|
| 131 |
+
use_precompiled = _strtobool(os.getenv("WAN_USE_PRECOMPILED"), default=False)
|
| 132 |
precompiled_repo = os.getenv("WAN_PRECOMPILED_REPO", "Fabrice-TIERCELIN/Wan_2.2_compiled")
|
| 133 |
|
| 134 |
+
compiled_transformer_1 = None
|
| 135 |
+
compiled_transformer_2 = None
|
| 136 |
+
|
| 137 |
if use_precompiled:
|
| 138 |
+
try:
|
| 139 |
+
compiled_transformer_1, compiled_transformer_2 = load_compiled_transformers_from_hub(
|
| 140 |
+
repo_id=precompiled_repo,
|
| 141 |
+
device="cuda",
|
| 142 |
+
)
|
| 143 |
+
print(f"[WAN] Loaded precompiled transformers from {precompiled_repo}")
|
| 144 |
+
except Exception as e:
|
| 145 |
+
print(f"[WAN] Failed to load precompiled transformers, fallback to GPU compile: {e}")
|
| 146 |
+
compiled_transformer_1, compiled_transformer_2 = compile_transformer()
|
| 147 |
else:
|
| 148 |
compiled_transformer_1, compiled_transformer_2 = compile_transformer()
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
pipeline.transformer.forward = compiled_transformer_1
|
| 151 |
drain_module_parameters(pipeline.transformer)
|
| 152 |
|