Eueuiaa commited on
Commit
1c1a25a
·
verified ·
1 Parent(s): 465c791

Update api/ltx_server_refactored.py

Browse files
Files changed (1) hide show
  1. api/ltx_server_refactored.py +48 -23
api/ltx_server_refactored.py CHANGED
@@ -342,12 +342,23 @@ class VideoService:
342
  }
343
 
344
  # --- NÓ 1.3: CHAMADA AO PIPELINE ---
345
- with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
346
- latents_bruto = self.pipeline(**first_pass_kwargs).images
347
- log_tensor_info(latents_bruto, f"Latente Bruto Gerado para: '{prompt[:40]}...'")
 
 
 
 
 
 
 
348
 
349
- print("-" * 20 + " FIM: _generate_single_chunk_low " + "-"*20)
350
- return latents_bruto
 
 
 
 
351
 
352
  # ==============================================================================
353
  # --- FUNÇÃO #2: ORQUESTRADOR NARRATIVO (MÚLTIPLOS PROMPTS) ---
@@ -439,16 +450,22 @@ class VideoService:
439
  final_latents = torch.cat(latentes_chunk_video, dim=2)
440
  log_tensor_info(final_latents, "Tensor de Latentes Final Concatenado")
441
 
442
- tensor_path = os.path.join(results_dir, f"latents_narrative_{used_seed}.pt")
443
- torch.save(final_latents.cpu(), tensor_path)
444
-
445
- with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
446
- pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(ltx_configs_override.get("decode_timestep", 0.05) if ltx_configs_override else 0.05))
447
- video_path = self._save_and_log_video(pixel_tensor, "narrative_video", FPS, temp_dir, results_dir, used_seed)
 
 
448
 
449
- self.finalize(keep_paths=[video_path, tensor_path])
450
- return video_path, tensor_path, used_seed
451
-
 
 
 
 
452
  # ==============================================================================
453
  # --- FUNÇÃO #3: ORQUESTRADOR SIMPLES (PROMPT ÚNICO) ---
454
  # ==============================================================================
@@ -487,16 +504,24 @@ class VideoService:
487
  print("\n--- Finalizando Geração Simples: Salvando e decodificando ---")
488
  log_tensor_info(final_latents, "Tensor de Latentes Final")
489
 
490
- tensor_path = os.path.join(results_dir, f"latents_single_{used_seed}.pt")
491
- torch.save(final_latents.cpu(), tensor_path)
492
-
493
- with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
494
- pixel_tensor = vae_manager_singleton.decode(final_latents, decode_timestep=float(ltx_configs_override.get("decode_timestep", 0.05) if ltx_configs_override else 0.05))
495
- video_path = self._save_and_log_video(pixel_tensor, "single_video", FPS, temp_dir, results_dir, used_seed)
496
-
497
- self.finalize(keep_paths=[video_path, tensor_path])
498
- return video_path, tensor_path, used_seed
 
 
 
 
 
 
 
499
 
 
500
  # ==============================================================================
501
  # --- FUNÇÃO #4: ORQUESTRADOR (Upscaler + texturas hd) ---
502
  # ==============================================================================
 
342
  }
343
 
344
  # --- NÓ 1.3: CHAMADA AO PIPELINE ---
345
+ try:
346
+ with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
347
+ latents_bruto = self.pipeline(**first_pass_kwargs).images
348
+ latents_cpu_bruto = latents_bruto.detach().to("cpu")
349
+ tensor_path_cpu = os.path.join(results_dir, f"latents_low_res_{used_seed}.pt")
350
+ torch.save(latents_cpu_bruto, tensor_path_cpu)
351
+ log_tensor_info(latents_bruto, f"Latente Bruto Gerado para: '{prompt[:40]}...'")
352
+
353
+ print("-" * 20 + " FIM: _generate_single_chunk_low " + "-"*20)
354
+ return tensor_path_cpu
355
 
356
+ except Exception as e:
357
+ print("-" * 20 + f" ERRO: _generate_single_chunk_low {e} " + "-"*20)
358
+ finally:
359
+ torch.cuda.empty_cache()
360
+ torch.cuda.ipc_collect()
361
+ self.finalize(keep_paths=[])
362
 
363
  # ==============================================================================
364
  # --- FUNÇÃO #2: ORQUESTRADOR NARRATIVO (MÚLTIPLOS PROMPTS) ---
 
450
  final_latents = torch.cat(latentes_chunk_video, dim=2)
451
  log_tensor_info(final_latents, "Tensor de Latentes Final Concatenado")
452
 
453
+ try:
454
+ with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
455
+ pixel_tensor = vae_manager_singleton.decode(final_latents.clone(), decode_timestep=float(self.config.get("decode_timestep", 0.05)))
456
+ video_path = self._save_and_log_video(pixel_tensor, "narrative_video", FPS, temp_dir, results_dir, used_seed)
457
+ latents_cpu = latents.detach().to("cpu")
458
+ tensor_path = os.path.join(results_dir, f"latents_low_res_{used_seed}.pt")
459
+ torch.save(latents_cpu, tensor_path)
460
+ return video_path, tensor_path, used_seed
461
 
462
+ except Exception as e:
463
+ pass
464
+ finally:
465
+ torch.cuda.empty_cache()
466
+ torch.cuda.ipc_collect()
467
+ self.finalize(keep_paths=[])
468
+
469
  # ==============================================================================
470
  # --- FUNÇÃO #3: ORQUESTRADOR SIMPLES (PROMPT ÚNICO) ---
471
  # ==============================================================================
 
504
  print("\n--- Finalizando Geração Simples: Salvando e decodificando ---")
505
  log_tensor_info(final_latents, "Tensor de Latentes Final")
506
 
507
+ try:
508
+ with torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype, enabled=self.device.type == 'cuda'):
509
+ pixel_tensor = vae_manager_singleton.decode(final_latents.clone(), decode_timestep=float(self.config.get("decode_timestep", 0.05)))
510
+ video_path = self._save_and_log_video(pixel_tensor, "single_video", FPS, temp_dir, results_dir, used_seed)
511
+ latents_cpu = latents.detach().to("cpu")
512
+ tensor_path = os.path.join(results_dir, f"latents_single_{used_seed}.pt")
513
+ torch.save(latents_cpu, tensor_path)
514
+ return video_path, tensor_path, used_seed
515
+
516
+ except Exception as e:
517
+ pass
518
+ finally:
519
+ torch.cuda.empty_cache()
520
+ torch.cuda.ipc_collect()
521
+ self.finalize(keep_paths=[])
522
+
523
 
524
+
525
  # ==============================================================================
526
  # --- FUNÇÃO #4: ORQUESTRADOR (Upscaler + texturas hd) ---
527
  # ==============================================================================