Spaces:
Paused
Paused
Update api/gpu_manager.py
Browse files- api/gpu_manager.py +64 -26
api/gpu_manager.py
CHANGED
|
@@ -1,56 +1,94 @@
|
|
| 1 |
-
# api/gpu_manager.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
class GPUManager:
|
| 7 |
"""
|
| 8 |
-
Gerencia e aloca GPUs
|
|
|
|
| 9 |
"""
|
| 10 |
def __init__(self):
|
| 11 |
self.total_gpus = torch.cuda.device_count()
|
| 12 |
-
self.
|
|
|
|
| 13 |
self.seedvr_gpus = []
|
|
|
|
| 14 |
self._allocate_gpus()
|
| 15 |
|
| 16 |
def _allocate_gpus(self):
|
| 17 |
-
""
|
| 18 |
-
|
| 19 |
-
"""
|
| 20 |
-
print("="*50)
|
| 21 |
-
print("🤖 Gerenciador de GPUs inicializado.")
|
| 22 |
print(f" > Total de GPUs detectadas: {self.total_gpus}")
|
| 23 |
|
|
|
|
|
|
|
| 24 |
if self.total_gpus == 0:
|
| 25 |
print(" > Nenhuma GPU detectada. Operando em modo CPU.")
|
| 26 |
elif self.total_gpus == 1:
|
| 27 |
-
print(" > 1 GPU
|
| 28 |
-
|
| 29 |
-
self.
|
| 30 |
self.seedvr_gpus = [0]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self.
|
| 35 |
-
self.
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
"""Retorna o dispositivo
|
| 42 |
-
if not self.
|
| 43 |
return torch.device("cpu")
|
| 44 |
-
|
| 45 |
-
return torch.device(f"cuda:{self.ltx_gpus[0]}")
|
| 46 |
|
| 47 |
def get_seedvr_devices(self) -> list:
|
| 48 |
"""Retorna a lista de IDs de GPU para o SeedVR."""
|
| 49 |
return self.seedvr_gpus
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def requires_memory_swap(self) -> bool:
|
| 52 |
-
"""Verifica se
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# Instância global
|
| 56 |
gpu_manager = GPUManager()
|
|
|
|
| 1 |
+
# api/gpu_manager.py (Versão com VAE Dedicado)
|
| 2 |
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
+
import math
|
| 6 |
|
| 7 |
class GPUManager:
|
| 8 |
"""
|
| 9 |
+
Gerencia e aloca GPUs de forma inteligente entre LTX (com VAE dedicado),
|
| 10 |
+
SeedVR e VINCIE.
|
| 11 |
"""
|
| 12 |
def __init__(self):
|
| 13 |
self.total_gpus = torch.cuda.device_count()
|
| 14 |
+
self.ltx_main_gpus = []
|
| 15 |
+
self.ltx_vae_gpu = []
|
| 16 |
self.seedvr_gpus = []
|
| 17 |
+
self.vincie_gpus = []
|
| 18 |
self._allocate_gpus()
|
| 19 |
|
| 20 |
def _allocate_gpus(self):
|
| 21 |
+
print("="*60)
|
| 22 |
+
print("🤖 Gerenciador de GPUs (com VAE dedicado para LTX)")
|
|
|
|
|
|
|
|
|
|
| 23 |
print(f" > Total de GPUs detectadas: {self.total_gpus}")
|
| 24 |
|
| 25 |
+
all_indices = list(range(self.total_gpus))
|
| 26 |
+
|
| 27 |
if self.total_gpus == 0:
|
| 28 |
print(" > Nenhuma GPU detectada. Operando em modo CPU.")
|
| 29 |
elif self.total_gpus == 1:
|
| 30 |
+
print(" > 1 GPU: Modo de compartilhamento total.")
|
| 31 |
+
self.ltx_main_gpus = [0]
|
| 32 |
+
self.ltx_vae_gpu = [0] # Compartilha com o principal
|
| 33 |
self.seedvr_gpus = [0]
|
| 34 |
+
self.vincie_gpus = [0]
|
| 35 |
+
elif self.total_gpus == 2:
|
| 36 |
+
print(" > 2 GPUs: LTX com VAE dedicado, outros compartilham a GPU principal.")
|
| 37 |
+
self.ltx_main_gpus = [0]
|
| 38 |
+
self.ltx_vae_gpu = [1] # VAE fica com a segunda GPU
|
| 39 |
+
self.seedvr_gpus = [0] # Compartilha com LTX principal
|
| 40 |
+
self.vincie_gpus = [0] # Compartilha com LTX principal
|
| 41 |
+
else: # 3 ou mais GPUs
|
| 42 |
+
print(f" > {self.total_gpus} GPUs: Alocação distribuída.")
|
| 43 |
+
# LTX sempre fica com as duas primeiras GPUs se disponíveis
|
| 44 |
+
self.ltx_main_gpus = [0]
|
| 45 |
+
self.ltx_vae_gpu = [1]
|
| 46 |
+
|
| 47 |
+
remaining_gpus = all_indices[2:]
|
| 48 |
+
|
| 49 |
+
if not remaining_gpus: # Caso de exatamente 2 GPUs, já coberto, mas por segurança
|
| 50 |
+
self.seedvr_gpus = [0]
|
| 51 |
+
self.vincie_gpus = [0]
|
| 52 |
+
else:
|
| 53 |
+
# O resto é dividido entre SeedVR e VINCIE
|
| 54 |
+
vincie_count = max(1, math.ceil(len(remaining_gpus) / 2))
|
| 55 |
+
seedvr_count = len(remaining_gpus) - vincie_count
|
| 56 |
+
|
| 57 |
+
self.vincie_gpus = remaining_gpus[:vincie_count]
|
| 58 |
+
self.seedvr_gpus = remaining_gpus[vincie_count:]
|
| 59 |
|
| 60 |
+
print(f" > Alocação Final:")
|
| 61 |
+
print(f" - LTX (Transformer): GPUs {self.ltx_main_gpus}")
|
| 62 |
+
print(f" - LTX (VAE): GPUs {self.ltx_vae_gpu}")
|
| 63 |
+
print(f" - SeedVR: GPUs {self.seedvr_gpus}")
|
| 64 |
+
print(f" - VINCIE: GPUs {self.vincie_gpus}")
|
| 65 |
+
print("="*60)
|
| 66 |
+
|
| 67 |
+
def get_ltx_device(self) -> torch.device:
|
| 68 |
+
"""Retorna o dispositivo principal para o Transformer do LTX."""
|
| 69 |
+
if not self.ltx_main_gpus:
|
| 70 |
+
return torch.device("cpu")
|
| 71 |
+
return torch.device(f"cuda:{self.ltx_main_gpus[0]}")
|
| 72 |
|
| 73 |
+
def get_ltx_vae_device(self) -> torch.device:
|
| 74 |
+
"""Retorna o dispositivo dedicado para o VAE do LTX."""
|
| 75 |
+
if not self.ltx_vae_gpu:
|
| 76 |
return torch.device("cpu")
|
| 77 |
+
return torch.device(f"cuda:{self.ltx_vae_gpu[0]}")
|
|
|
|
| 78 |
|
| 79 |
def get_seedvr_devices(self) -> list:
|
| 80 |
"""Retorna a lista de IDs de GPU para o SeedVR."""
|
| 81 |
return self.seedvr_gpus
|
| 82 |
+
|
| 83 |
+
def get_vincie_devices(self) -> list:
|
| 84 |
+
"""Retorna a lista de IDs de GPU para o VINCIE."""
|
| 85 |
+
return self.vincie_gpus
|
| 86 |
|
| 87 |
def requires_memory_swap(self) -> bool:
|
| 88 |
+
"""Verifica se múltiplos serviços estão compartilhando a mesma GPU."""
|
| 89 |
+
all_allocations = self.ltx_main_gpus + self.seedvr_gpus + self.vincie_gpus
|
| 90 |
+
# O swap é necessário se o número de alocações for maior que o número de GPUs únicas
|
| 91 |
+
return len(all_allocations) > len(set(all_allocations))
|
| 92 |
|
| 93 |
+
# Instância global
|
| 94 |
gpu_manager = GPUManager()
|