Spaces:
Runtime error
Runtime error
loader Hub + delete duplicate
Browse files- optimization.py +158 -172
optimization.py
CHANGED
|
@@ -1,173 +1,159 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
from
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
from optimization_utils import
|
| 18 |
-
from optimization_utils import
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
from optimization_utils import ZeroGPUCompiledModel
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
P = ParamSpec('P')
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
'
|
| 44 |
-
'
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
if
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
)
|
| 87 |
-
"""
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
pipeline
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
pipeline.
|
| 123 |
-
pipeline.
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
compiled_transformer_1, compiled_transformer_2 = load_compiled_transformers_from_hub(
|
| 160 |
-
repo_id=precompiled_repo
|
| 161 |
-
)
|
| 162 |
-
else:
|
| 163 |
-
compiled_transformer_1, compiled_transformer_2 = compile_transformer()
|
| 164 |
-
|
| 165 |
-
# expose for downloads
|
| 166 |
-
COMPILED_TRANSFORMER_1 = compiled_transformer_1
|
| 167 |
-
COMPILED_TRANSFORMER_2 = compiled_transformer_2
|
| 168 |
-
|
| 169 |
-
pipeline.transformer.forward = compiled_transformer_1
|
| 170 |
-
drain_module_parameters(pipeline.transformer)
|
| 171 |
-
|
| 172 |
-
pipeline.transformer_2.forward = compiled_transformer_2
|
| 173 |
drain_module_parameters(pipeline.transformer_2)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
from typing import Any
|
| 5 |
+
from typing import Callable
|
| 6 |
+
from typing import ParamSpec
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import spaces
|
| 10 |
+
import torch
|
| 11 |
+
from torch.utils._pytree import tree_map_only
|
| 12 |
+
from torchao.quantization import quantize_
|
| 13 |
+
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
|
| 14 |
+
from torchao.quantization import Int8WeightOnlyConfig
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
|
| 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 |
+
LATENT_FRAMES_DIM = torch.export.Dim('num_latent_frames', min=8, max=81)
|
| 27 |
+
LATENT_PATCHED_HEIGHT_DIM = torch.export.Dim('latent_patched_height', min=30, max=52)
|
| 28 |
+
LATENT_PATCHED_WIDTH_DIM = torch.export.Dim('latent_patched_width', min=30, max=52)
|
| 29 |
+
|
| 30 |
+
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 31 |
+
'hidden_states': {
|
| 32 |
+
2: LATENT_FRAMES_DIM,
|
| 33 |
+
3: 2 * LATENT_PATCHED_HEIGHT_DIM,
|
| 34 |
+
4: 2 * LATENT_PATCHED_WIDTH_DIM,
|
| 35 |
+
},
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
INDUCTOR_CONFIGS = {
|
| 39 |
+
'conv_1x1_as_mm': True,
|
| 40 |
+
'epilogue_fusion': False,
|
| 41 |
+
'coordinate_descent_tuning': True,
|
| 42 |
+
'coordinate_descent_check_all_directions': True,
|
| 43 |
+
'max_autotune': True,
|
| 44 |
+
'triton.cudagraphs': True,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _strtobool(v: str | None, default: bool = True) -> bool:
|
| 49 |
+
if v is None:
|
| 50 |
+
return default
|
| 51 |
+
return v.strip().lower() in ("1", "true", "yes", "y", "on")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _load_compiled_pt(path: str):
|
| 55 |
+
"""
|
| 56 |
+
Load either:
|
| 57 |
+
- a serialized dict produced by to_serializable_dict() (format zerogpu_aoti_v1), or
|
| 58 |
+
- an old-style pickled ZeroGPUCompiledModel.
|
| 59 |
+
"""
|
| 60 |
+
obj = torch.load(path, map_location="cpu", weights_only=False)
|
| 61 |
+
|
| 62 |
+
# New format: dict payload
|
| 63 |
+
if isinstance(obj, dict) and obj.get("format") == "zerogpu_aoti_v1":
|
| 64 |
+
return zerogpu_compiled_from_serializable_dict(obj)
|
| 65 |
+
|
| 66 |
+
# Old format: direct object
|
| 67 |
+
if isinstance(obj, ZeroGPUCompiledModel):
|
| 68 |
+
return obj
|
| 69 |
+
|
| 70 |
+
raise ValueError(
|
| 71 |
+
f"Unsupported compiled transformer file format at {path}. "
|
| 72 |
+
f"Got type={type(obj)} keys={list(obj.keys()) if isinstance(obj, dict) else None}"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def load_compiled_transformers_from_hub(
|
| 77 |
+
repo_id: str,
|
| 78 |
+
filename_1: str = "compiled_transformer_1.pt",
|
| 79 |
+
filename_2: str = "compiled_transformer_2.pt",
|
| 80 |
+
):
|
| 81 |
+
"""
|
| 82 |
+
Charge les artefacts précompilés depuis le Hub.
|
| 83 |
+
|
| 84 |
+
IMPORTANT:
|
| 85 |
+
Les fichiers attendus sont ceux que tu exportes via to_serializable_dict()
|
| 86 |
+
(format 'zerogpu_aoti_v1') OU un pickle direct de ZeroGPUCompiledModel.
|
| 87 |
+
"""
|
| 88 |
+
path_1 = hf_hub_download(repo_id=repo_id, filename=filename_1)
|
| 89 |
+
path_2 = hf_hub_download(repo_id=repo_id, filename=filename_2)
|
| 90 |
+
|
| 91 |
+
compiled_1 = _load_compiled_pt(path_1)
|
| 92 |
+
compiled_2 = _load_compiled_pt(path_2)
|
| 93 |
+
return compiled_1, compiled_2
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 97 |
+
@spaces.GPU(duration=1500)
|
| 98 |
+
def compile_transformer():
|
| 99 |
+
pipeline.load_lora_weights(
|
| 100 |
+
"Kijai/WanVideo_comfy",
|
| 101 |
+
weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
|
| 102 |
+
adapter_name="lightx2v",
|
| 103 |
+
)
|
| 104 |
+
kwargs_lora = {"load_into_transformer_2": True}
|
| 105 |
+
pipeline.load_lora_weights(
|
| 106 |
+
"Kijai/WanVideo_comfy",
|
| 107 |
+
weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
|
| 108 |
+
adapter_name="lightx2v_2",
|
| 109 |
+
**kwargs_lora,
|
| 110 |
+
)
|
| 111 |
+
pipeline.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1.0, 1.0])
|
| 112 |
+
pipeline.fuse_lora(adapter_names=["lightx2v"], lora_scale=3.0, components=["transformer"])
|
| 113 |
+
pipeline.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1.0, components=["transformer_2"])
|
| 114 |
+
pipeline.unload_lora_weights()
|
| 115 |
+
|
| 116 |
+
with capture_component_call(pipeline, "transformer") as call:
|
| 117 |
+
pipeline(*args, **kwargs)
|
| 118 |
+
|
| 119 |
+
dynamic_shapes = tree_map_only((torch.Tensor, bool), lambda t: None, call.kwargs)
|
| 120 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 121 |
+
|
| 122 |
+
quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 123 |
+
quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())
|
| 124 |
+
|
| 125 |
+
exported_1 = torch.export.export(
|
| 126 |
+
mod=pipeline.transformer,
|
| 127 |
+
args=call.args,
|
| 128 |
+
kwargs=call.kwargs,
|
| 129 |
+
dynamic_shapes=dynamic_shapes,
|
| 130 |
+
)
|
| 131 |
+
exported_2 = torch.export.export(
|
| 132 |
+
mod=pipeline.transformer_2,
|
| 133 |
+
args=call.args,
|
| 134 |
+
kwargs=call.kwargs,
|
| 135 |
+
dynamic_shapes=dynamic_shapes,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
compiled_1 = aoti_compile(exported_1, INDUCTOR_CONFIGS)
|
| 139 |
+
compiled_2 = aoti_compile(exported_2, INDUCTOR_CONFIGS)
|
| 140 |
+
return compiled_1, compiled_2
|
| 141 |
+
|
| 142 |
+
# Text encoder quant (inchangé)
|
| 143 |
+
quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
|
| 144 |
+
|
| 145 |
+
use_precompiled = True
|
| 146 |
+
precompiled_repo = os.getenv("WAN_PRECOMPILED_REPO", "Fabrice-TIERCELIN/Wan_2.2_compiled")
|
| 147 |
+
|
| 148 |
+
if use_precompiled:
|
| 149 |
+
compiled_transformer_1, compiled_transformer_2 = load_compiled_transformers_from_hub(
|
| 150 |
+
repo_id=precompiled_repo
|
| 151 |
+
)
|
| 152 |
+
else:
|
| 153 |
+
compiled_transformer_1, compiled_transformer_2 = compile_transformer()
|
| 154 |
+
|
| 155 |
+
pipeline.transformer.forward = compiled_transformer_1
|
| 156 |
+
drain_module_parameters(pipeline.transformer)
|
| 157 |
+
|
| 158 |
+
pipeline.transformer_2.forward = compiled_transformer_2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
drain_module_parameters(pipeline.transformer_2)
|