Carlos s commited on
Commit
61b0958
·
verified ·
1 Parent(s): c63f8b4

Update api/ltx_server.py

Browse files
Files changed (1) hide show
  1. api/ltx_server.py +37 -45
api/ltx_server.py CHANGED
@@ -398,53 +398,45 @@ class VideoService:
398
  return out
399
 
400
 
401
- def _dividir_latentes_por_tamanho(self, latents_brutos, num_latente_por_chunk: int, overlap: int = 1):
402
- """
403
- Divide o tensor de latentes em chunks com tamanho definido em número de latentes.
404
-
405
- Args:
406
- latents_brutos: tensor [B, C, T, H, W]
407
- num_latente_por_chunk: número de latentes por chunk
408
- overlap: número de frames que se sobrepõem entre chunks
409
-
410
- Returns:
411
- List[tensor]: lista de chunks cloneados
412
- """
413
- sum_latent = latents_brutos.shape[2]
414
- chunks = []
415
 
416
- if num_latente_por_chunk >= sum_latent:
417
- return [latents_brutos]
 
 
 
 
 
418
 
419
- n_chunks = (sum_latent) // num_latente_por_chunk
420
- steps = sum_latent//n_chunks
421
- print("================PODA CAUSAL=================")
422
- print(f"[DEBUG] TOTAL LATENTES = {sum_latent}")
423
- print(f"[DEBUG] Num LATENTES por chunk = {num_latente_por_chunk}")
424
- print(f"[DEBUG] Número de chunks = {n_chunks}")
425
-
426
-
427
-
428
- if n_chunks > 1:
429
- start = 0
430
- i = 0
431
- end=1
432
- while i < n_chunks:
433
- start += end
434
- end = start+num_latente_por_chunk
435
- if end+3>=sum_latent-1:
436
- end = (sum_latent-1)
437
- i = n_chunks
438
- else:
439
- i += 1
440
- chunk = latents_brutos[:, :, start-1:end, :, :].clone().detach()
441
- chunks.append(chunk)
442
- print(f"[DEBUG] chunk{i}[:, :, {start-1}:{end}, :, :] = {chunk.shape[2]}")
443
- else:
444
- print(f"[DEBUG] numero chunks minimo")
445
- print(f"[DEBUG] latents_brutos[:, :, :, :, :] = {latents_brutos.shape[2]}")
446
- chunks.append(latents_brutos)
447
- print("================PODA CAUSAL=================")
448
  return chunks
449
 
450
  def _get_total_frames(self, video_path: str) -> int:
 
398
  return out
399
 
400
 
401
+ def dividir_latentes(latents, num_por_chunk: int, overlap: int = 1):
402
+ # latents: [B, C, T, H, W]
403
+ T = latents.shape[2]
404
+ L = num_por_chunk + overlap # janela com stop exclusivo
405
+ stride = num_por_chunk - overlap # avanço
406
+ print("================= DEBUG =================") # debug
407
+ print(f"[DEBUG] T={T} L={L} stride={stride}") # debug
408
+ print(f"[DEBUG] regra: slice [start:stop) com stop exclusivo") # debug
 
 
 
 
 
 
409
 
410
+ if T <= L:
411
+ start = max(0, T - L)
412
+ stop = start + L
413
+ chunk = latents[:, :, start:stop, :, :].clone().detach()
414
+ print(f"[DEBUG] único chunk: [:, :, {start}:{stop}, :, :] len={stop-start}") # debug
415
+ print("=========================================") # debug
416
+ return [chunk]
417
 
418
+ starts = []
419
+ s = 0
420
+ while s + L <= T:
421
+ starts.append(s)
422
+ s += stride
423
+ if starts[-1] + L < T:
424
+ starts.append(T - L)
425
+
426
+ # remove duplicata contígua
427
+ uniq = []
428
+ for st in starts:
429
+ if not uniq or uniq[-1] != st:
430
+ uniq.append(st)
431
+
432
+ chunks = []
433
+ for i, st in enumerate(uniq):
434
+ sp = st + L
435
+ chunk = latents[:, :, st:sp, :, :].clone().detach()
436
+ print(f"[DEBUG] chunk{i}: [:, :, {st}:{sp}, :, :] len={sp-st}") # debug
437
+ chunks.append(chunk)
438
+
439
+ print("=========================================") # debug
 
 
 
 
 
 
 
440
  return chunks
441
 
442
  def _get_total_frames(self, video_path: str) -> int: