Update api/ltx_server.py
Browse files- api/ltx_server.py +57 -37
api/ltx_server.py
CHANGED
|
@@ -743,6 +743,7 @@ class VideoService:
|
|
| 743 |
print(f"[DEBUG] output_type={call_kwargs['output_type']} skip_layer_strategy={call_kwargs['skip_layer_strategy']}")
|
| 744 |
|
| 745 |
latents = None
|
|
|
|
| 746 |
|
| 747 |
try:
|
| 748 |
ctx = torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype) if self.device == "cuda" else contextlib.nullcontext()
|
|
@@ -794,34 +795,50 @@ class VideoService:
|
|
| 794 |
del base_latents; gc.collect(); torch.cuda.empty_cache()
|
| 795 |
|
| 796 |
|
| 797 |
-
# # --- ETAPA 3: REFINAMENTO DE TEXTURA (SECOND PASS) ---
|
| 798 |
-
print("\n--- INICIANDO ETAPA 3: REFINAMENTO DE TEXTURA (SECOND PASS) ---")
|
| 799 |
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
"
|
| 813 |
-
|
| 814 |
-
"
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
print(f"[DEBUG] Second Pass concluída em {time.perf_counter() - t_pass2:.2f}s")
|
| 823 |
|
| 824 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
|
| 826 |
else: # Geração de etapa única
|
| 827 |
print("\n--- INICIANDO GERAÇÃO DE ETAPA ÚNICA ---")
|
|
@@ -835,24 +852,27 @@ class VideoService:
|
|
| 835 |
log_tensor_info(latents, "Latentes Finais (Etapa Única)")
|
| 836 |
print(f"[DEBUG] Etapa única concluída em {time.perf_counter() - t_single:.2f}s")
|
| 837 |
|
|
|
|
|
|
|
| 838 |
# --- ETAPA FINAL: DECODIFICAÇÃO E CODIFICAÇÃO MP4 ---
|
| 839 |
print("\n--- INICIANDO ETAPA FINAL: DECODIFICAÇÃO E MONTAGEM ---")
|
| 840 |
|
| 841 |
-
latents_cpu = latents.detach().to("cpu", non_blocking=True)
|
| 842 |
-
torch.cuda.empty_cache()
|
| 843 |
-
try:
|
| 844 |
-
|
| 845 |
-
except Exception:
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
latents_parts
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
|
|
|
| 852 |
|
|
|
|
| 853 |
partes_mp4 = []
|
| 854 |
par = 0
|
| 855 |
-
|
| 856 |
for latents in latents_parts:
|
| 857 |
print(f"[DEBUG] Partição {par}: {tuple(latents.shape)}")
|
| 858 |
|
|
|
|
| 743 |
print(f"[DEBUG] output_type={call_kwargs['output_type']} skip_layer_strategy={call_kwargs['skip_layer_strategy']}")
|
| 744 |
|
| 745 |
latents = None
|
| 746 |
+
latents_list[]
|
| 747 |
|
| 748 |
try:
|
| 749 |
ctx = torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype) if self.device == "cuda" else contextlib.nullcontext()
|
|
|
|
| 795 |
del base_latents; gc.collect(); torch.cuda.empty_cache()
|
| 796 |
|
| 797 |
|
|
|
|
|
|
|
| 798 |
|
| 799 |
+
par = 0
|
| 800 |
+
latents_cpu_up = upsampled_latents.detach().to("cpu", non_blocking=True)
|
| 801 |
+
torch.cuda.empty_cache()
|
| 802 |
+
try:
|
| 803 |
+
torch.cuda.ipc_collect()
|
| 804 |
+
except Exception:
|
| 805 |
+
pass
|
| 806 |
+
|
| 807 |
+
latents_parts_up = self._dividir_latentes_por_tamanho(latents_cpu_up,4,1)
|
| 808 |
+
temp_dir = tempfile.mkdtemp(prefix="ltxv_"); self._register_tmp_dir(temp_dir)
|
| 809 |
+
results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
|
| 810 |
+
|
| 811 |
|
| 812 |
+
for latents in latents_parts_up:
|
| 813 |
+
|
| 814 |
+
# # --- ETAPA 3: REFINAMENTO DE TEXTURA (SECOND PASS) ---
|
| 815 |
+
print("\n--- INICIANDO ETAPA 3: REFINAMENTO DE TEXTURA (SECOND PASS) ---")
|
| 816 |
+
|
| 817 |
+
second_pass_config = self.config.get("second_pass", {}).copy()
|
| 818 |
+
# --- <INÍCIO DA LÓGICA DE CÁLCULO EXATA PARA SECOND PASS> ---
|
| 819 |
+
# Usa as dimensões da primeira passagem dobradas, como na pipeline original
|
| 820 |
+
second_pass_width = downscaled_width * 2
|
| 821 |
+
second_pass_height = downscaled_height * 2
|
| 822 |
+
print(f"[DEBUG] Second Pass Dims: Target ({second_pass_width}x{second_pass_height})")
|
| 823 |
+
# --- <FIM DA LÓGICA DE CÁLCULO EXATA> ---
|
| 824 |
+
t_pass2 = time.perf_counter()
|
|
|
|
| 825 |
|
| 826 |
+
second_pass_kwargs = call_kwargs.copy()
|
| 827 |
+
second_pass_kwargs.update({
|
| 828 |
+
"output_type": "latent",
|
| 829 |
+
"width": second_pass_width,
|
| 830 |
+
"height": second_pass_height,
|
| 831 |
+
"latents": upsampled_latents, # O tensor upscaled
|
| 832 |
+
"guidance_scale": float(guidance_scale),
|
| 833 |
+
**second_pass_config
|
| 834 |
+
})
|
| 835 |
+
|
| 836 |
+
print(f"[DEBUG] Second Pass: Refinando em {width_padded}x{height_padded}...")
|
| 837 |
+
final_latents = self.pipeline(**second_pass_kwargs).images
|
| 838 |
+
log_tensor_info(final_latents, "Latentes Finais (Pós-Second Pass)")
|
| 839 |
+
print(f"[DEBUG] Second part Pass concluída em {time.perf_counter() - t_pass2:.2f}s")
|
| 840 |
+
|
| 841 |
+
latents_list.append(final_latents)
|
| 842 |
|
| 843 |
else: # Geração de etapa única
|
| 844 |
print("\n--- INICIANDO GERAÇÃO DE ETAPA ÚNICA ---")
|
|
|
|
| 852 |
log_tensor_info(latents, "Latentes Finais (Etapa Única)")
|
| 853 |
print(f"[DEBUG] Etapa única concluída em {time.perf_counter() - t_single:.2f}s")
|
| 854 |
|
| 855 |
+
latents_list.append(latents)
|
| 856 |
+
|
| 857 |
# --- ETAPA FINAL: DECODIFICAÇÃO E CODIFICAÇÃO MP4 ---
|
| 858 |
print("\n--- INICIANDO ETAPA FINAL: DECODIFICAÇÃO E MONTAGEM ---")
|
| 859 |
|
| 860 |
+
#latents_cpu = latents.detach().to("cpu", non_blocking=True)
|
| 861 |
+
#torch.cuda.empty_cache()
|
| 862 |
+
#try:
|
| 863 |
+
# torch.cuda.ipc_collect()
|
| 864 |
+
#except Exception:
|
| 865 |
+
# pass
|
| 866 |
+
|
| 867 |
+
latents_parts[]
|
| 868 |
+
for latents in latents_list:
|
| 869 |
+
latents_parts.append(self._dividir_latentes_por_tamanho(latents_cpu,4,1))
|
| 870 |
+
temp_dir = tempfile.mkdtemp(prefix="ltxv_"); self._register_tmp_dir(temp_dir)
|
| 871 |
+
results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
|
| 872 |
|
| 873 |
+
|
| 874 |
partes_mp4 = []
|
| 875 |
par = 0
|
|
|
|
| 876 |
for latents in latents_parts:
|
| 877 |
print(f"[DEBUG] Partição {par}: {tuple(latents.shape)}")
|
| 878 |
|