Spaces:
Paused
Paused
File size: 13,256 Bytes
9185209 60431a3 fb84d49 db85898 9185209 db85898 9185209 db85898 85dbcff a5cbce5 db85898 9185209 2f3144c 0d5ddf4 db85898 60431a3 db85898 9185209 db85898 60431a3 db85898 9185209 db85898 fb84d49 9185209 0d5ddf4 9185209 fb84d49 db85898 0d5ddf4 db85898 0d5ddf4 8906472 0d5ddf4 db4bbd0 0d5ddf4 02633f1 fb84d49 0d5ddf4 fb84d49 0d5ddf4 8906472 fb84d49 db4bbd0 60431a3 9185209 0d5ddf4 8906472 0d5ddf4 8906472 0d5ddf4 60431a3 8906472 9185209 8906472 9185209 8906472 fb84d49 8906472 9185209 8906472 fb84d49 9185209 8906472 9185209 8906472 fb84d49 db85898 60431a3 db85898 9185209 a5cbce5 9185209 8906472 0d5ddf4 8906472 9185209 8906472 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# FILE: api/ltx/ltx_utils.py
# DESCRIPTION: A pure utility library for the LTX ecosystem.
# Contains the official low-level builder function for the complete pipeline
# and other stateless helper functions.
import os
import random
import json
import logging
import sys
from pathlib import Path
from typing import Dict, Tuple, Union
import torchvision.transforms.functional as TVF
from PIL import Image
import torch
from safetensors import safe_open
from transformers import T5EncoderModel, T5Tokenizer
# ==============================================================================
# --- CONFIGURAÇÃO DE PATH E IMPORTS DA BIBLIOTECA LTX ---
# ==============================================================================
LTX_VIDEO_REPO_DIR = Path("/data/LTX-Video")
def add_deps_to_path():
"""Adiciona o diretório do repositório LTX ao sys.path para importação de suas bibliotecas."""
repo_path = str(LTX_VIDEO_REPO_DIR.resolve())
if repo_path not in sys.path:
sys.path.insert(0, repo_path)
logging.info(f"[ltx_utils] LTX-Video repository added to sys.path: {repo_path}")
add_deps_to_path()
try:
from ltx_video.pipelines.pipeline_ltx_video import LTXVideoPipeline
from ltx_video.models.autoencoders.causal_video_autoencoder import CausalVideoAutoencoder
from ltx_video.models.transformers.transformer3d import Transformer3DModel
from ltx_video.models.transformers.symmetric_patchifier import SymmetricPatchifier
from ltx_video.schedulers.rf import RectifiedFlowScheduler
except ImportError as e:
logging.critical("Failed to import a core LTX-Video library component.", exc_info=True)
raise ImportError(f"Could not import from LTX-Video library. Check repo integrity at '{LTX_VIDEO_REPO_DIR}'. Error: {e}")
# ==============================================================================
# --- FUNÇÃO HELPER 'create_transformer' (Essencial) ---
# ==============================================================================
def create_transformer(ckpt_path: str, precision: str) -> Transformer3DModel:
"""
Cria e carrega o modelo Transformer3D com a lógica de precisão correta,
incluindo suporte para a otimização float8_e4m3fn.
"""
if precision == "float8_e4m3fn":
try:
from q8_kernels.integration.patch_transformer import patch_diffusers_transformer as patch_transformer_for_q8_kernels
transformer = Transformer3DModel.from_pretrained(ckpt_path, dtype=torch.float8_e4m3fn)
patch_transformer_for_q8_kernels(transformer)
return transformer
except ImportError:
raise ValueError("Q8-Kernels not found. To use FP8 checkpoint, please install Q8 kernels from the project's wheels.")
elif precision == "bfloat16":
return Transformer3DModel.from_pretrained(ckpt_path).to(torch.bfloat16)
else:
return Transformer3DModel.from_pretrained(ckpt_path)
# ==============================================================================
# --- BUILDER DE BAIXO NÍVEL OFICIAL ---
# ==============================================================================
def build_complete_pipeline_on_cpu(checkpoint_path: str, config: Dict) -> LTXVideoPipeline:
"""
Constrói o pipeline LTX COMPLETO, incluindo o VAE, e o mantém na CPU.
Esta é a função de construção fundamental usada pelo LTXAducManager.
"""
logging.info(f"Building complete LTX pipeline from checkpoint: {Path(checkpoint_path).name}")
with safe_open(checkpoint_path, framework="pt") as f:
metadata = f.metadata() or {}
config_str = metadata.get("config", "{}")
allowed_inference_steps = json.loads(config_str).get("allowed_inference_steps")
precision = config.get("precision", "bfloat16")
# Usa a função helper correta para criar o transformer
transformer = create_transformer(checkpoint_path, precision).to("cpu")
scheduler = RectifiedFlowScheduler.from_pretrained(checkpoint_path)
text_encoder = T5EncoderModel.from_pretrained(config["text_encoder_model_name_or_path"], subfolder="text_encoder").to("cpu")
tokenizer = T5Tokenizer.from_pretrained(config["text_encoder_model_name_or_path"], subfolder="tokenizer")
patchifier = SymmetricPatchifier(patch_size=1)
vae = CausalVideoAutoencoder.from_pretrained(checkpoint_path).to("cpu")
if precision == "bfloat16":
text_encoder.to(torch.bfloat16)
vae.to(torch.bfloat16)
# O transformer já foi convertido para bfloat16 dentro de create_transformer, se aplicável
pipeline = LTXVideoPipeline(
transformer=transformer,
patchifier=patchifier,
text_encoder=text_encoder,
tokenizer=tokenizer,
scheduler=scheduler,
vae=vae, # VAE é incluído para que o pipeline possa ser auto-suficiente
allowed_inference_steps=allowed_inference_steps,
prompt_enhancer_image_caption_model=None,
prompt_enhancer_image_caption_processor=None,
prompt_enhancer_llm_model=None,
prompt_enhancer_llm_tokenizer=None,
)
return pipeline
# ==============================================================================
# --- FUNÇÕES AUXILIARES GENÉRICAS ---
# ==============================================================================
# # FILE: api/ltx/ltx_utils.py
# DESCRIPTION: A pure utility library for the LTX ecosystem.
# Contains the official low-level builder function for the complete pipeline
# and other stateless helper functions.
import os
import random
import json
import logging
import sys
from pathlib import Path
from typing import Dict, Tuple
import torch
from safetensors import safe_open
from transformers import T5EncoderModel, T5Tokenizer
# ==============================================================================
# --- CONFIGURAÇÃO DE PATH E IMPORTS DA BIBLIOTECA LTX ---
# ==============================================================================
LTX_VIDEO_REPO_DIR = Path("/data/LTX-Video")
def add_deps_to_path():
"""Adiciona o diretório do repositório LTX ao sys.path para importação de suas bibliotecas."""
repo_path = str(LTX_VIDEO_REPO_DIR.resolve())
if repo_path not in sys.path:
sys.path.insert(0, repo_path)
logging.info(f"[ltx_utils] LTX-Video repository added to sys.path: {repo_path}")
add_deps_to_path()
try:
from ltx_video.pipelines.pipeline_ltx_video import LTXVideoPipeline
from ltx_video.models.autoencoders.causal_video_autoencoder import CausalVideoAutoencoder
from ltx_video.models.transformers.transformer3d import Transformer3DModel
from ltx_video.models.transformers.symmetric_patchifier import SymmetricPatchifier
from ltx_video.schedulers.rf import RectifiedFlowScheduler
except ImportError as e:
logging.critical("Failed to import a core LTX-Video library component.", exc_info=True)
raise ImportError(f"Could not import from LTX-Video library. Check repo integrity at '{LTX_VIDEO_REPO_DIR}'. Error: {e}")
# ==============================================================================
# --- FUNÇÃO HELPER 'create_transformer' (Essencial) ---
# ==============================================================================
def create_transformer(ckpt_path: str, precision: str) -> Transformer3DModel:
"""
Cria e carrega o modelo Transformer3D com a lógica de precisão correta,
incluindo suporte para a otimização float8_e4m3fn.
"""
if precision == "float8_e4m3fn":
try:
from q8_kernels.integration.patch_transformer import patch_diffusers_transformer as patch_transformer_for_q8_kernels
transformer = Transformer3DModel.from_pretrained(ckpt_path, dtype=torch.float8_e4m3fn)
patch_transformer_for_q8_kernels(transformer)
return transformer
except ImportError:
raise ValueError("Q8-Kernels not found. To use FP8 checkpoint, please install Q8 kernels from the project's wheels.")
elif precision == "bfloat16":
return Transformer3DModel.from_pretrained(ckpt_path).to(torch.bfloat16)
else:
return Transformer3DModel.from_pretrained(ckpt_path)
# ==============================================================================
# --- BUILDER DE BAIXO NÍVEL OFICIAL ---
# ==============================================================================
def build_complete_pipeline_on_cpu(checkpoint_path: str, config: Dict) -> LTXVideoPipeline:
"""
Constrói o pipeline LTX COMPLETO, incluindo o VAE, e o mantém na CPU.
Esta é a função de construção fundamental usada pelo LTXAducManager.
"""
logging.info(f"Building complete LTX pipeline from checkpoint: {Path(checkpoint_path).name}")
with safe_open(checkpoint_path, framework="pt") as f:
metadata = f.metadata() or {}
config_str = metadata.get("config", "{}")
allowed_inference_steps = json.loads(config_str).get("allowed_inference_steps")
precision = config.get("precision", "bfloat16")
# Usa a função helper correta para criar o transformer
transformer = create_transformer(checkpoint_path, precision).to("cpu")
scheduler = RectifiedFlowScheduler.from_pretrained(checkpoint_path)
text_encoder = T5EncoderModel.from_pretrained(config["text_encoder_model_name_or_path"], subfolder="text_encoder").to("cpu")
tokenizer = T5Tokenizer.from_pretrained(config["text_encoder_model_name_or_path"], subfolder="tokenizer")
patchifier = SymmetricPatchifier(patch_size=1)
vae = CausalVideoAutoencoder.from_pretrained(checkpoint_path).to("cpu")
if precision == "bfloat16":
text_encoder.to(torch.bfloat16)
vae.to(torch.bfloat16)
# O transformer já foi convertido para bfloat16 dentro de create_transformer, se aplicável
pipeline = LTXVideoPipeline(
transformer=transformer,
patchifier=patchifier,
text_encoder=text_encoder,
tokenizer=tokenizer,
scheduler=scheduler,
vae=vae, # VAE é incluído para que o pipeline possa ser auto-suficiente
allowed_inference_steps=allowed_inference_steps,
prompt_enhancer_image_caption_model=None,
prompt_enhancer_image_caption_processor=None,
prompt_enhancer_llm_model=None,
prompt_enhancer_llm_tokenizer=None,
)
return pipeline
# ==============================================================================
# --- FUNÇÕES AUXILIARES GENÉRICAS ---
# ==============================================================================
def seed_everything(seed: int):
"""
Define a semente para PyTorch, NumPy e Python para garantir reprodutibilidade.
"""
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = Fals
def load_image_to_tensor_with_resize_and_crop(
image_input: Union[str, Image.Image],
target_height: int,
target_width: int,
) -> torch.Tensor:
"""
Carrega, redimensiona, corta e processa uma imagem para um tensor de pixel 5D,
normalizado para [-1, 1], pronto para ser enviado ao VAE para encoding.
"""
if isinstance(image_input, str):
image = Image.open(image_input).convert("RGB")
elif isinstance(image_input, Image.Image):
image = image_input.convert("RGB")
else:
raise ValueError("image_input must be a file path or a PIL Image object")
input_width, input_height = image.size
aspect_ratio_target = target_width / target_height
aspect_ratio_frame = input_width / input_height
if aspect_ratio_frame > aspect_ratio_target:
new_width, new_height = int(input_height * aspect_ratio_target), input_height
x_start = (input_width - new_width) // 2
image = image.crop((x_start, 0, x_start + new_width, new_height))
else:
new_height = int(input_width / aspect_ratio_target)
y_start = (input_height - new_height) // 2
image = image.crop((0, y_start, input_width, y_start + new_height))
image = image.resize((target_width, target_height), Image.Resampling.LANCZOS)
frame_tensor = TVF.to_tensor(image)
# Esta parte depende de 'crf_compressor', então precisamos importá-lo aqui também
try:
from ltx_video.pipelines import crf_compressor
frame_tensor_hwc = frame_tensor.permute(1, 2, 0)
frame_tensor_hwc = crf_compressor.compress(frame_tensor_hwc)
frame_tensor = frame_tensor_hwc.permute(2, 0, 1)
except ImportError:
logging.warning("CRF Compressor not found. Skipping compression step.")
frame_tensor = (frame_tensor * 2.0) - 1.0
return frame_tensor.unsqueeze(0).unsqueeze(2)
def seed_everything(seed: int):
"""
Define a semente para PyTorch, NumPy e Python para garantir reprodutibilidade.
"""
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False |