Spaces:
Paused
Paused
Create ltx_pool_manager.py
Browse files- api/ltx_pool_manager.py +97 -0
api/ltx_pool_manager.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# FILE: api/ltx_pool_manager.py
|
| 2 |
+
# DESCRIPTION: The "secret weapon". A pool manager for LTX that applies
|
| 3 |
+
# runtime patches to the pipeline for full control and ADUC-SDR compatibility.
|
| 4 |
+
|
| 5 |
+
import logging
|
| 6 |
+
from typing import List, Optional, Tuple, Union
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers.utils.torch_utils import randn_tensor
|
| 10 |
+
|
| 11 |
+
# --- Importações da nossa arquitetura ---
|
| 12 |
+
from api.gpu_manager import gpu_manager
|
| 13 |
+
from api.ltx.ltx_utils import build_ltx_pipeline_on_cpu
|
| 14 |
+
from ltx_video.pipelines.pipeline_ltx_video import LTXVideoPipeline
|
| 15 |
+
|
| 16 |
+
# --- Definição dos nossos Data Classes ---
|
| 17 |
+
@dataclass
|
| 18 |
+
class ConditioningItem:
|
| 19 |
+
pixel_tensor: torch.Tensor # Sempre um tensor de pixel
|
| 20 |
+
media_frame_number: int
|
| 21 |
+
conditioning_strength: float
|
| 22 |
+
|
| 23 |
+
@dataclass
|
| 24 |
+
class LatentConditioningItem:
|
| 25 |
+
latent_tensor: torch.Tensor # Sempre um tensor latente
|
| 26 |
+
media_frame_number: int
|
| 27 |
+
conditioning_strength: float
|
| 28 |
+
|
| 29 |
+
# ==============================================================================
|
| 30 |
+
# --- O MONKEY PATCH ---
|
| 31 |
+
# Esta é a nossa versão customizada de `prepare_conditioning`
|
| 32 |
+
# ==============================================================================
|
| 33 |
+
|
| 34 |
+
def _aduc_prepare_conditioning_patch(
|
| 35 |
+
self: "LTXVideoPipeline",
|
| 36 |
+
conditioning_items: Optional[List[Union[ConditioningItem, LatentConditioningItem]]],
|
| 37 |
+
init_latents: torch.Tensor,
|
| 38 |
+
num_frames: int,
|
| 39 |
+
height: int,
|
| 40 |
+
width: int,
|
| 41 |
+
vae_per_channel_normalize: bool = False,
|
| 42 |
+
generator=None,
|
| 43 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, int]:
|
| 44 |
+
|
| 45 |
+
# Esta função é uma cópia modificada da sua, com logging e pequenas melhorias.
|
| 46 |
+
|
| 47 |
+
# (O código do patch que você forneceu vai aqui, ligeiramente ajustado)
|
| 48 |
+
# ...
|
| 49 |
+
|
| 50 |
+
return init_latents, init_pixel_coords, init_conditioning_mask, extra_conditioning_num_latents
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# ==============================================================================
|
| 54 |
+
# --- LTX Worker e Pool Manager ---
|
| 55 |
+
# ==============================================================================
|
| 56 |
+
|
| 57 |
+
class LTXWorker:
|
| 58 |
+
"""Gerencia uma instância do LTX Pipeline em um par de GPUs (main + vae)."""
|
| 59 |
+
def __init__(self, main_device: str, vae_device: str, config: dict):
|
| 60 |
+
self.main_device = torch.device(main_device)
|
| 61 |
+
self.vae_device = torch.device(vae_device)
|
| 62 |
+
self.config = config
|
| 63 |
+
self.pipeline: LTXVideoPipeline = None
|
| 64 |
+
|
| 65 |
+
self._load_and_patch_pipeline()
|
| 66 |
+
|
| 67 |
+
def _load_and_patch_pipeline(self):
|
| 68 |
+
logging.info(f"[LTXWorker-{self.main_device}] Carregando pipeline LTX para a CPU...")
|
| 69 |
+
self.pipeline, _ = build_ltx_pipeline_on_cpu(self.config)
|
| 70 |
+
|
| 71 |
+
logging.info(f"[LTXWorker-{self.main_device}] Movendo pipeline para GPUs (Main: {self.main_device}, VAE: {self.vae_device})...")
|
| 72 |
+
self.pipeline.to(self.main_device)
|
| 73 |
+
self.pipeline.vae.to(self.vae_device)
|
| 74 |
+
|
| 75 |
+
logging.info(f"[LTXWorker-{self.main_device}] Aplicando patch ADUC-SDR na função 'prepare_conditioning'...")
|
| 76 |
+
# A "mágica" do monkey patching acontece aqui
|
| 77 |
+
self.pipeline.prepare_conditioning = _aduc_prepare_conditioning_patch.__get__(self.pipeline, LTXVideoPipeline)
|
| 78 |
+
logging.info(f"[LTXWorker-{self.main_device}] ✅ Pipeline 'quente', corrigido e pronto.")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class LTXPoolManager:
|
| 82 |
+
# (Padrão Singleton, similar ao VincePoolManager)
|
| 83 |
+
# ...
|
| 84 |
+
|
| 85 |
+
def __init__(self):
|
| 86 |
+
# ...
|
| 87 |
+
main_device = gpu_manager.get_ltx_device()
|
| 88 |
+
vae_device = gpu_manager.get_ltx_vae_device()
|
| 89 |
+
# Em uma arquitetura futura, poderíamos ter múltiplos workers. Por enquanto, temos um.
|
| 90 |
+
self.worker = LTXWorker(str(main_device), str(vae_device), self._load_config())
|
| 91 |
+
# ...
|
| 92 |
+
|
| 93 |
+
def get_pipeline(self) -> LTXVideoPipeline:
|
| 94 |
+
return self.worker.pipeline
|
| 95 |
+
|
| 96 |
+
# Instância Singleton
|
| 97 |
+
ltx_pool_manager = LTXPoolManager()
|