euiia commited on
Commit
bc96793
·
verified ·
1 Parent(s): be876b2

Update upscaler_specialist.py

Browse files
Files changed (1) hide show
  1. upscaler_specialist.py +22 -22
upscaler_specialist.py CHANGED
@@ -1,31 +1,32 @@
1
  # upscaler_specialist.py
2
- # Copyright (C) 2025 Carlos Rodrigues dos Santos
3
  # Especialista ADUC para upscaling espacial de tensores latentes.
4
 
5
  import torch
6
  import logging
7
  from diffusers import LTXLatentUpsamplePipeline
8
-
9
  from ltx_manager_helpers import ltx_manager_singleton
10
 
11
  logger = logging.getLogger(__name__)
12
 
13
-
14
  class UpscalerSpecialist:
15
- def __init__(self, device="cuda"):
 
 
 
 
 
16
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
17
- self.cpu_device = torch.device("cpu")
18
- self.workspace_dir = workspace_dir
19
- self.pipe_upsample = None
20
  self.base_vae = None
 
21
 
22
  def _lazy_init(self):
23
- """Inicializa o VAE e o pipeline somente quando for chamado."""
24
  if self.base_vae is None:
25
  try:
26
- from ltx_manager_helpers import ltx_manager_singleton
27
  if ltx_manager_singleton.workers:
28
  self.base_vae = ltx_manager_singleton.workers[0].pipeline.vae
 
29
  else:
30
  logger.warning("[Upscaler] Nenhum worker disponível no ltx_manager_singleton.")
31
  except Exception as e:
@@ -34,7 +35,6 @@ class UpscalerSpecialist:
34
 
35
  if self.pipe_upsample is None and self.base_vae is not None:
36
  try:
37
- from ltx_video.pipelines.latent_upscale import LTXLatentUpsamplePipeline
38
  self.pipe_upsample = LTXLatentUpsamplePipeline.from_pretrained(
39
  "linoyts/LTX-Video-spatial-upscaler-0.9.8",
40
  vae=self.base_vae,
@@ -44,25 +44,25 @@ class UpscalerSpecialist:
44
  except Exception as e:
45
  logger.error(f"[Upscaler] Falha ao carregar pipeline: {e}")
46
 
 
47
  def upscale(self, latents: torch.Tensor) -> torch.Tensor:
 
48
  self._lazy_init()
49
  if self.pipe_upsample is None:
50
  logger.warning("[Upscaler] Pipeline indisponível. Retornando latentes originais.")
51
  return latents
 
52
  try:
53
- with torch.no_grad():
54
- result = self.pipe_upsample(latents=latents, output_type="latent")
 
55
  return result.latents
56
  except Exception as e:
57
- logger.error(f"[Upscaler] Erro durante upscale: {e}")
58
  return latents
59
-
60
 
61
- # Instanciação Singleton
62
- # Depende do VAE do ltx_manager, então o obtemos de lá.
63
- try:
64
- base_vae_for_upscaler = ltx_manager_singleton.workers[0].pipeline.vae
65
- upscaler_specialist_singleton = UpscalerSpecialist(base_vae=base_vae_for_upscaler)
66
- except Exception as e:
67
- logger.error(f"Não foi possível inicializar o UpscalerSpecialist Singleton: {e}")
68
- upscaler_specialist_singleton = None
 
1
  # upscaler_specialist.py
2
+ # Copyright (C) 2025 Carlos Rodrigues
3
  # Especialista ADUC para upscaling espacial de tensores latentes.
4
 
5
  import torch
6
  import logging
7
  from diffusers import LTXLatentUpsamplePipeline
 
8
  from ltx_manager_helpers import ltx_manager_singleton
9
 
10
  logger = logging.getLogger(__name__)
11
 
 
12
  class UpscalerSpecialist:
13
+ """
14
+ Especialista responsável por aumentar a resolução espacial de tensores latentes
15
+ usando o LTX Video Spatial Upscaler.
16
+ """
17
+ def __init__(self):
18
+ # Força uso de CUDA se disponível
19
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
20
  self.base_vae = None
21
+ self.pipe_upsample = None
22
 
23
  def _lazy_init(self):
24
+ """Inicializa VAE e pipeline apenas quando necessário."""
25
  if self.base_vae is None:
26
  try:
 
27
  if ltx_manager_singleton.workers:
28
  self.base_vae = ltx_manager_singleton.workers[0].pipeline.vae
29
+ logger.info("[Upscaler] VAE base obtido com sucesso.")
30
  else:
31
  logger.warning("[Upscaler] Nenhum worker disponível no ltx_manager_singleton.")
32
  except Exception as e:
 
35
 
36
  if self.pipe_upsample is None and self.base_vae is not None:
37
  try:
 
38
  self.pipe_upsample = LTXLatentUpsamplePipeline.from_pretrained(
39
  "linoyts/LTX-Video-spatial-upscaler-0.9.8",
40
  vae=self.base_vae,
 
44
  except Exception as e:
45
  logger.error(f"[Upscaler] Falha ao carregar pipeline: {e}")
46
 
47
+ @torch.no_grad()
48
  def upscale(self, latents: torch.Tensor) -> torch.Tensor:
49
+ """Aplica o upscaling 2x nos tensores latentes fornecidos."""
50
  self._lazy_init()
51
  if self.pipe_upsample is None:
52
  logger.warning("[Upscaler] Pipeline indisponível. Retornando latentes originais.")
53
  return latents
54
+
55
  try:
56
+ logger.info(f"[Upscaler] Recebido shape {latents.shape}. Executando upscale em {self.device}...")
57
+ result = self.pipe_upsample(latents=latents, output_type="latent")
58
+ logger.info(f"[Upscaler] Upscale concluído. Novo shape: {result.latents.shape}")
59
  return result.latents
60
  except Exception as e:
61
+ logger.error(f"[Upscaler] Erro durante upscale: {e}", exc_info=True)
62
  return latents
 
63
 
64
+
65
+ # ---------------------------
66
+ # Singleton global
67
+ # ---------------------------
68
+ upscaler_specialist_singleton = UpscalerSpecialist()