Spaces:
Paused
Paused
Update api/ltx_server_refactored.py
Browse files- api/ltx_server_refactored.py +8 -108
api/ltx_server_refactored.py
CHANGED
|
@@ -172,9 +172,9 @@ class VideoService:
|
|
| 172 |
print(f"[DEBUG] Aplicando política de precisão: {prec}")
|
| 173 |
if prec == "float8_e4m3fn":
|
| 174 |
self.runtime_autocast_dtype = torch.bfloat16
|
| 175 |
-
force_promote = os.getenv("LTXV_FORCE_BF16_ON_FP8", "0") == "1"
|
| 176 |
print(f"[DEBUG] FP8 detectado. force_promote={force_promote}")
|
| 177 |
-
if force_promote and hasattr(torch, "float8_e4m3fn"):
|
| 178 |
try:
|
| 179 |
self._promote_fp8_weights_to_bf16(self.pipeline)
|
| 180 |
except Exception as e:
|
|
@@ -253,9 +253,6 @@ class VideoService:
|
|
| 253 |
print(f"[DEBUG] Vídeo salvo em: {final_path}")
|
| 254 |
return final_path
|
| 255 |
|
| 256 |
-
# ==============================================================================
|
| 257 |
-
# --- FUNÇÕES MODULARES COM A LÓGICA DE CHUNKING SIMPLIFICADA ---
|
| 258 |
-
# ==============================================================================
|
| 259 |
|
| 260 |
def prepare_condition_items(
|
| 261 |
self, items_list: List, height: int,
|
|
@@ -272,7 +269,11 @@ class VideoService:
|
|
| 272 |
conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
|
| 273 |
return conditioning_items
|
| 274 |
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
self, prompt, negative_prompt,
|
| 277 |
height, width, duration, seed,
|
| 278 |
conditioning_items=None,
|
|
@@ -310,114 +311,13 @@ class VideoService:
|
|
| 310 |
tensor_path = os.path.join(results_dir, f"latents_low_res_{used_seed}.pt")
|
| 311 |
torch.save(latents_cpu, tensor_path)
|
| 312 |
return video_path, tensor_path, used_seed
|
| 313 |
-
|
| 314 |
except Exception as e:
|
| 315 |
pass
|
| 316 |
finally:
|
| 317 |
torch.cuda.empty_cache()
|
| 318 |
torch.cuda.ipc_collect()
|
| 319 |
self.finalize(keep_paths=[])
|
| 320 |
-
|
| 321 |
-
# ==============================================================================
|
| 322 |
-
# --- FUNÇÕES DE GERAÇÃO ATUALIZADAS E MODULARES ---
|
| 323 |
-
# ==============================================================================
|
| 324 |
-
|
| 325 |
-
def generate_narrative_low(
|
| 326 |
-
self, prompt: str, negative_prompt,
|
| 327 |
-
height, width, duration,
|
| 328 |
-
seed, conditions_itens=None,
|
| 329 |
-
ltx_configs_override: dict = None):
|
| 330 |
-
|
| 331 |
-
print("\n" + "="*80)
|
| 332 |
-
print("====== INICIANDO GERAÇÃO NARRATIVA EM CHUNKS (LOW-RES) ======")
|
| 333 |
-
print("="*80)
|
| 334 |
-
|
| 335 |
-
prompt_list = [p.strip() for p in prompt.splitlines() if p.strip()]
|
| 336 |
-
num_chunks = len(prompt_list)
|
| 337 |
-
if num_chunks == 0: raise ValueError("O prompt está vazio ou não contém linhas válidas.")
|
| 338 |
-
|
| 339 |
-
latentes_chunk_video = []
|
| 340 |
-
latentes_chunk_video_list = []
|
| 341 |
-
latentes_chunk = None
|
| 342 |
-
eco_latents_condition_overlap = None
|
| 343 |
-
results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
|
| 344 |
-
|
| 345 |
-
for i, prompt_x in enumerate(prompt_list):
|
| 346 |
-
print(f"\n--- Gerando Chunk Narrativo {i+1}/{num_chunks}: '{prompt}' ---")
|
| 347 |
-
|
| 348 |
-
current_image_conditions = []
|
| 349 |
-
#if conditions_itens:
|
| 350 |
-
# cond_item_original = conditions_itens[0]
|
| 351 |
-
# if i == 0:
|
| 352 |
-
# current_image_conditions.append(cond_item_original)
|
| 353 |
-
# else:
|
| 354 |
-
# cond_item_fraco = ConditioningItem(
|
| 355 |
-
# media_item=cond_item_original.media_item, media_frame_number=0, conditioning_strength=0.1
|
| 356 |
-
# )
|
| 357 |
-
# current_image_conditions.append(cond_item_fraco)
|
| 358 |
-
|
| 359 |
-
#if ltx_configs_override is None: ltx_configs_override = {}
|
| 360 |
-
|
| 361 |
-
#eco_current_conditions_list = []
|
| 362 |
-
#if current_image_conditions: eco_current_conditions_list.extend(current_image_conditions)
|
| 363 |
-
#if eco_latents_condition_overlap: eco_current_conditions_list.append(eco_latents_condition_overlap)
|
| 364 |
-
#ltx_configs_override["conditioning_items"] = current_conditions
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
video_path, tensor_path, used_seed = self._generate_low(
|
| 368 |
-
prompt, negative_prompt, height,
|
| 369 |
-
width, duration, seed,
|
| 370 |
-
conditions_itens, ltx_configs_override,
|
| 371 |
-
)
|
| 372 |
-
|
| 373 |
-
log_tensor_info(tensor_path, f"tensor_path")
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
#eco_latents_overlap = latentes_chunk[:, :, -4:, :, :]
|
| 377 |
-
#log_tensor_info(eco_latents_overlap, f"eco_latents_overlap")
|
| 378 |
-
|
| 379 |
-
#eco_latents_condition_overlap = ConditioningItem(
|
| 380 |
-
# media_item=eco_latents_overlap, media_frame_number=0, conditioning_strength=1.0
|
| 381 |
-
#)
|
| 382 |
-
|
| 383 |
-
#if i > 0:
|
| 384 |
-
#latentes_chunk_podado = latentes_chunk[:, :, 5:, :, :]
|
| 385 |
-
#latentes_chunk_video_list.append(latentes_chunk_podado)
|
| 386 |
-
#log_tensor_info(latentes_chunk_podado, "latentes_chunk_podado")
|
| 387 |
-
#else:
|
| 388 |
-
latentes_chunk_video_list.append(tensor_path)
|
| 389 |
-
log_tensor_info(tensor_path, "tensor_path")
|
| 390 |
-
|
| 391 |
-
#final_latents_cpu = torch.cat(latentes_chunk_video_list, dim=2).cpu()
|
| 392 |
-
#log_tensor_info(final_latents_cpu, "Tensor de Latentes Final Concatenado (CPU)")
|
| 393 |
-
|
| 394 |
-
#tensor_path = os.path.join(results_dir, f"latents_narrative_{used_seed}.pt")
|
| 395 |
-
#torch.save(final_latents_cpu, tensor_path)
|
| 396 |
-
|
| 397 |
-
if false:
|
| 398 |
-
ctx = torch.autocast(device_type="cuda", dtype=self.runtime_autocast_dtype) if self.device == "cuda" else contextlib.nullcontext()
|
| 399 |
-
with ctx:
|
| 400 |
-
#final_latents_video = torch.cat(latentes_chunk_video_list, dim=2)
|
| 401 |
-
pixel_tensor = vae_manager_singleton.decode(final_latents_video, decode_timestep=float(self.config.get("decode_timestep", 0.05)))
|
| 402 |
-
video_path = self._save_and_log_video(pixel_tensor, "narrative_video", FPS, temp_dir, results_dir, used_seed)
|
| 403 |
-
tensor_path = os.path.join(results_dir, f"latents_narrative_{used_seed}.pt")
|
| 404 |
-
torch.save(final_latents_video, tensor_path)
|
| 405 |
-
del final_latents_video; gc.collect(); torch.cuda.empty_cache()
|
| 406 |
-
del pixel_tensor; gc.collect(); torch.cuda.empty_cache()
|
| 407 |
-
|
| 408 |
-
return video_path, tensor_path, used_seed
|
| 409 |
-
#except Exception as e:
|
| 410 |
-
# print("-" * 20 + f" ERRO: generate_narrative_low {e} " + "-"*20)
|
| 411 |
-
# traceback.print_exc()
|
| 412 |
-
# return None
|
| 413 |
-
#finally:
|
| 414 |
-
# gc.collect()
|
| 415 |
-
# torch.cuda.empty_cache()
|
| 416 |
-
# torch.cuda.ipc_collect()
|
| 417 |
-
# self.finalize(keep_paths=[])
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
# ==============================================================================
|
| 422 |
# --- FUNÇÃO #4: ORQUESTRADOR (Upscaler + texturas hd) ---
|
| 423 |
# ==============================================================================
|
|
|
|
| 172 |
print(f"[DEBUG] Aplicando política de precisão: {prec}")
|
| 173 |
if prec == "float8_e4m3fn":
|
| 174 |
self.runtime_autocast_dtype = torch.bfloat16
|
| 175 |
+
force_promote = True #os.getenv("LTXV_FORCE_BF16_ON_FP8", "0") == "1"
|
| 176 |
print(f"[DEBUG] FP8 detectado. force_promote={force_promote}")
|
| 177 |
+
if force_promote: # and hasattr(torch, "float8_e4m3fn"):
|
| 178 |
try:
|
| 179 |
self._promote_fp8_weights_to_bf16(self.pipeline)
|
| 180 |
except Exception as e:
|
|
|
|
| 253 |
print(f"[DEBUG] Vídeo salvo em: {final_path}")
|
| 254 |
return final_path
|
| 255 |
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
def prepare_condition_items(
|
| 258 |
self, items_list: List, height: int,
|
|
|
|
| 269 |
conditioning_items.append(ConditioningItem(tensor, safe_frame, float(weight)))
|
| 270 |
return conditioning_items
|
| 271 |
|
| 272 |
+
|
| 273 |
+
# ==============================================================================
|
| 274 |
+
# --- FUNÇÕES MODULARES COM A LÓGICA DE CHUNKING SIMPLIFICADA ---
|
| 275 |
+
# ==============================================================================
|
| 276 |
+
def generate_low(
|
| 277 |
self, prompt, negative_prompt,
|
| 278 |
height, width, duration, seed,
|
| 279 |
conditioning_items=None,
|
|
|
|
| 311 |
tensor_path = os.path.join(results_dir, f"latents_low_res_{used_seed}.pt")
|
| 312 |
torch.save(latents_cpu, tensor_path)
|
| 313 |
return video_path, tensor_path, used_seed
|
|
|
|
| 314 |
except Exception as e:
|
| 315 |
pass
|
| 316 |
finally:
|
| 317 |
torch.cuda.empty_cache()
|
| 318 |
torch.cuda.ipc_collect()
|
| 319 |
self.finalize(keep_paths=[])
|
| 320 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
# ==============================================================================
|
| 322 |
# --- FUNÇÃO #4: ORQUESTRADOR (Upscaler + texturas hd) ---
|
| 323 |
# ==============================================================================
|