Upload VideoEngine.py
Browse files- VideoEngine.py +34 -26
VideoEngine.py
CHANGED
|
@@ -134,14 +134,14 @@ class VideoEngine:
|
|
| 134 |
if self.device != "cuda":
|
| 135 |
return
|
| 136 |
|
| 137 |
-
print("→ [
|
| 138 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 139 |
torch.backends.cudnn.allow_tf32 = True
|
| 140 |
print("✓ TF32 enabled (20-30% speedup on A100/H100)")
|
| 141 |
|
| 142 |
def _enable_xformers(self):
|
| 143 |
"""Enable xFormers memory efficient attention."""
|
| 144 |
-
print("→ [
|
| 145 |
|
| 146 |
if not self._check_xformers_available():
|
| 147 |
print("⚠ xFormers not available, using standard attention")
|
|
@@ -157,10 +157,10 @@ class VideoEngine:
|
|
| 157 |
def _apply_aot_compilation(self):
|
| 158 |
"""Apply AOTI pre-compiled blocks (HF Spaces deployment only)."""
|
| 159 |
if not self.is_spaces:
|
| 160 |
-
print("→ [
|
| 161 |
return
|
| 162 |
|
| 163 |
-
print("→ [
|
| 164 |
try:
|
| 165 |
# Lazy import to avoid CUDA initialization at module load
|
| 166 |
from spaces.zero.torch.aoti import aoti_blocks_load
|
|
@@ -178,7 +178,7 @@ class VideoEngine:
|
|
| 178 |
aoti_blocks_load(self.pipeline.transformer_2, 'zerogpu-aoti/Wan2', variant=variant)
|
| 179 |
print(f"✓ AOTI blocks loaded (variant: {variant}, 60-70% speedup)")
|
| 180 |
except ImportError:
|
| 181 |
-
print("⚠ [
|
| 182 |
except Exception as e:
|
| 183 |
print(f"⚠ AOTI load failed (falling back to standard inference): {e}")
|
| 184 |
print(" This is not critical, speed will be slightly slower")
|
|
@@ -207,50 +207,41 @@ class VideoEngine:
|
|
| 207 |
print("Loading Wan2.2 I2V Engine with Optimizations")
|
| 208 |
print("=" * 60)
|
| 209 |
|
| 210 |
-
# Stage 1: Load base pipeline
|
| 211 |
-
print("→ [1/
|
| 212 |
self.pipeline = WanImageToVideoPipeline.from_pretrained(
|
| 213 |
self.MODEL_ID,
|
| 214 |
transformer=WanTransformer3DModel.from_pretrained(
|
| 215 |
self.TRANSFORMER_REPO,
|
| 216 |
subfolder='transformer',
|
| 217 |
torch_dtype=torch.bfloat16,
|
| 218 |
-
|
| 219 |
),
|
| 220 |
transformer_2=WanTransformer3DModel.from_pretrained(
|
| 221 |
self.TRANSFORMER_REPO,
|
| 222 |
subfolder='transformer_2',
|
| 223 |
torch_dtype=torch.bfloat16,
|
| 224 |
-
|
| 225 |
),
|
| 226 |
torch_dtype=torch.bfloat16,
|
| 227 |
-
)
|
| 228 |
|
| 229 |
-
# Stage 2: Load Lightning LoRA
|
| 230 |
-
print("→ [2/
|
| 231 |
self.pipeline.load_lora_weights(self.LORA_REPO, weight_name=self.LORA_WEIGHT, adapter_name="lightx2v")
|
|
|
|
| 232 |
kwargs_lora = {"load_into_transformer_2": True}
|
| 233 |
self.pipeline.load_lora_weights(self.LORA_REPO, weight_name=self.LORA_WEIGHT, adapter_name="lightx2v_2", **kwargs_lora)
|
|
|
|
| 234 |
self.pipeline.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1., 1.])
|
| 235 |
self.pipeline.fuse_lora(adapter_names=["lightx2v"], lora_scale=3., components=["transformer"])
|
| 236 |
self.pipeline.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1., components=["transformer_2"])
|
| 237 |
self.pipeline.unload_lora_weights()
|
|
|
|
| 238 |
print("✓ Lightning LoRA loaded and fused")
|
| 239 |
|
| 240 |
-
# Stage 3:
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
# Stage 4: Enable TF32 acceleration
|
| 244 |
-
self._enable_tf32()
|
| 245 |
-
|
| 246 |
-
# Stage 5: Enable xFormers attention
|
| 247 |
-
self._enable_xformers()
|
| 248 |
-
|
| 249 |
-
# Stage 6: Apply AOTI (deployment only)
|
| 250 |
-
self._apply_aot_compilation()
|
| 251 |
-
|
| 252 |
-
# Stage 7: Enable VAE optimizations (critical for ZeroGPU memory)
|
| 253 |
-
print("→ [7/8] Enabling VAE memory optimizations...")
|
| 254 |
try:
|
| 255 |
if hasattr(self.pipeline, 'enable_vae_tiling'):
|
| 256 |
self.pipeline.enable_vae_tiling()
|
|
@@ -261,6 +252,23 @@ class VideoEngine:
|
|
| 261 |
except Exception as e:
|
| 262 |
print(f"⚠ VAE optimization warning: {e}")
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
# Stage 8: Final setup
|
| 265 |
print("→ [8/8] Finalizing setup...")
|
| 266 |
self.is_loaded = True
|
|
|
|
| 134 |
if self.device != "cuda":
|
| 135 |
return
|
| 136 |
|
| 137 |
+
print("→ [5/8] Enabling TF32 acceleration...")
|
| 138 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 139 |
torch.backends.cudnn.allow_tf32 = True
|
| 140 |
print("✓ TF32 enabled (20-30% speedup on A100/H100)")
|
| 141 |
|
| 142 |
def _enable_xformers(self):
|
| 143 |
"""Enable xFormers memory efficient attention."""
|
| 144 |
+
print("→ [6/8] Enabling xFormers attention...")
|
| 145 |
|
| 146 |
if not self._check_xformers_available():
|
| 147 |
print("⚠ xFormers not available, using standard attention")
|
|
|
|
| 157 |
def _apply_aot_compilation(self):
|
| 158 |
"""Apply AOTI pre-compiled blocks (HF Spaces deployment only)."""
|
| 159 |
if not self.is_spaces:
|
| 160 |
+
print("→ [7/8] Skipping AOTI (testing mode, not needed)")
|
| 161 |
return
|
| 162 |
|
| 163 |
+
print("→ [7/8] Loading AOTI pre-compiled blocks...")
|
| 164 |
try:
|
| 165 |
# Lazy import to avoid CUDA initialization at module load
|
| 166 |
from spaces.zero.torch.aoti import aoti_blocks_load
|
|
|
|
| 178 |
aoti_blocks_load(self.pipeline.transformer_2, 'zerogpu-aoti/Wan2', variant=variant)
|
| 179 |
print(f"✓ AOTI blocks loaded (variant: {variant}, 60-70% speedup)")
|
| 180 |
except ImportError:
|
| 181 |
+
print("⚠ [7/8] Skipping AOTI (spaces.zero.torch.aoti not available)")
|
| 182 |
except Exception as e:
|
| 183 |
print(f"⚠ AOTI load failed (falling back to standard inference): {e}")
|
| 184 |
print(" This is not critical, speed will be slightly slower")
|
|
|
|
| 207 |
print("Loading Wan2.2 I2V Engine with Optimizations")
|
| 208 |
print("=" * 60)
|
| 209 |
|
| 210 |
+
# Stage 1: Load base pipeline (keep on CPU first to save VRAM)
|
| 211 |
+
print("→ [1/8] Loading base pipeline...")
|
| 212 |
self.pipeline = WanImageToVideoPipeline.from_pretrained(
|
| 213 |
self.MODEL_ID,
|
| 214 |
transformer=WanTransformer3DModel.from_pretrained(
|
| 215 |
self.TRANSFORMER_REPO,
|
| 216 |
subfolder='transformer',
|
| 217 |
torch_dtype=torch.bfloat16,
|
| 218 |
+
low_cpu_mem_usage=True,
|
| 219 |
),
|
| 220 |
transformer_2=WanTransformer3DModel.from_pretrained(
|
| 221 |
self.TRANSFORMER_REPO,
|
| 222 |
subfolder='transformer_2',
|
| 223 |
torch_dtype=torch.bfloat16,
|
| 224 |
+
low_cpu_mem_usage=True,
|
| 225 |
),
|
| 226 |
torch_dtype=torch.bfloat16,
|
| 227 |
+
)
|
| 228 |
|
| 229 |
+
# Stage 2: Load Lightning LoRA on CPU first, then fuse
|
| 230 |
+
print("→ [2/8] Loading Lightning LoRA adapters...")
|
| 231 |
self.pipeline.load_lora_weights(self.LORA_REPO, weight_name=self.LORA_WEIGHT, adapter_name="lightx2v")
|
| 232 |
+
gc.collect()
|
| 233 |
kwargs_lora = {"load_into_transformer_2": True}
|
| 234 |
self.pipeline.load_lora_weights(self.LORA_REPO, weight_name=self.LORA_WEIGHT, adapter_name="lightx2v_2", **kwargs_lora)
|
| 235 |
+
gc.collect()
|
| 236 |
self.pipeline.set_adapters(["lightx2v", "lightx2v_2"], adapter_weights=[1., 1.])
|
| 237 |
self.pipeline.fuse_lora(adapter_names=["lightx2v"], lora_scale=3., components=["transformer"])
|
| 238 |
self.pipeline.fuse_lora(adapter_names=["lightx2v_2"], lora_scale=1., components=["transformer_2"])
|
| 239 |
self.pipeline.unload_lora_weights()
|
| 240 |
+
gc.collect()
|
| 241 |
print("✓ Lightning LoRA loaded and fused")
|
| 242 |
|
| 243 |
+
# Stage 3: Enable VAE optimizations BEFORE moving to GPU
|
| 244 |
+
print("→ [3/8] Enabling VAE memory optimizations...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
try:
|
| 246 |
if hasattr(self.pipeline, 'enable_vae_tiling'):
|
| 247 |
self.pipeline.enable_vae_tiling()
|
|
|
|
| 252 |
except Exception as e:
|
| 253 |
print(f"⚠ VAE optimization warning: {e}")
|
| 254 |
|
| 255 |
+
# Stage 4: Enable model CPU offload for ZeroGPU memory efficiency
|
| 256 |
+
print("→ [4/8] Enabling model CPU offload...")
|
| 257 |
+
self.pipeline.enable_model_cpu_offload()
|
| 258 |
+
gc.collect()
|
| 259 |
+
if torch.cuda.is_available():
|
| 260 |
+
torch.cuda.empty_cache()
|
| 261 |
+
print("✓ Model CPU offload enabled (dynamic GPU/CPU memory management)")
|
| 262 |
+
|
| 263 |
+
# Stage 5: Enable TF32 acceleration
|
| 264 |
+
self._enable_tf32()
|
| 265 |
+
|
| 266 |
+
# Stage 6: Enable xFormers attention
|
| 267 |
+
self._enable_xformers()
|
| 268 |
+
|
| 269 |
+
# Stage 7: Apply AOTI (deployment only, usually skipped)
|
| 270 |
+
self._apply_aot_compilation()
|
| 271 |
+
|
| 272 |
# Stage 8: Final setup
|
| 273 |
print("→ [8/8] Finalizing setup...")
|
| 274 |
self.is_loaded = True
|