#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" cd "${REPO_ROOT}" export HF_ENABLE_PARALLEL_LOADING="${HF_ENABLE_PARALLEL_LOADING:-yes}" export HF_PARALLEL_LOADING_WORKERS="${HF_PARALLEL_LOADING_WORKERS:-8}" export PYTHONPATH="${REPO_ROOT}:${PYTHONPATH:-}" BASE_MODEL_PATH="${BASE_MODEL_PATH:-${REPO_ROOT}/checkpoints/Helios-Base}" TRANSFORMER_PATH="${TRANSFORMER_PATH:-${BASE_MODEL_PATH}}" OUTPUT_ROOT="${OUTPUT_ROOT:-${REPO_ROOT}/output_helios/token_dynamics_debug}" DEBUG_DIR="${DEBUG_DIR:-${OUTPUT_ROOT}/artifacts}" HEIGHT="${HEIGHT:-384}" WIDTH="${WIDTH:-640}" NUM_FRAMES="${NUM_FRAMES:-66}" FPS="${FPS:-24}" NUM_INFERENCE_STEPS="${NUM_INFERENCE_STEPS:-50}" GUIDANCE_SCALE="${GUIDANCE_SCALE:-5.0}" SEED="${SEED:-0}" WEIGHT_DTYPE="${WEIGHT_DTYPE:-bf16}" CHUNKS="${CHUNKS:-1}" HISTORY_FRAME="${HISTORY_FRAME:--1}" NOISE_FRAME="${NOISE_FRAME:-0}" VIS_STRIDE="${VIS_STRIDE:-16}" VIS_MAX_PAIRS="${VIS_MAX_PAIRS:-32}" CLEAN_DEBUG_DIR="${CLEAN_DEBUG_DIR:-1}" PROMPT="${PROMPT:-A cinematic close-up of a red sports car driving through a rainy city street at night. Reflections of neon signs ripple across the wet road while the camera follows the car smoothly from the front. The scene has realistic motion, detailed reflections, and a shallow depth of field.}" NEGATIVE_PROMPT="${NEGATIVE_PROMPT:-Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards}" export BASE_MODEL_PATH TRANSFORMER_PATH OUTPUT_ROOT DEBUG_DIR export HEIGHT WIDTH NUM_FRAMES FPS NUM_INFERENCE_STEPS GUIDANCE_SCALE SEED WEIGHT_DTYPE export CHUNKS HISTORY_FRAME NOISE_FRAME VIS_STRIDE VIS_MAX_PAIRS PROMPT NEGATIVE_PROMPT mkdir -p "${OUTPUT_ROOT}" "${DEBUG_DIR}" if [[ "${CLEAN_DEBUG_DIR}" == "1" ]]; then find "${DEBUG_DIR}" -maxdepth 1 -type f \( -name 'token_dynamics_*.pt' -o -name 'token_dynamics_*.png' -o -name '*_match_*.png' \) -delete fi echo "Running Helios token-dynamics debug" echo " repo: ${REPO_ROOT}" echo " model: ${BASE_MODEL_PATH}" echo " output: ${OUTPUT_ROOT}" echo " artifacts: ${DEBUG_DIR}" echo " chunks=${CHUNKS} history_frame=${HISTORY_FRAME} noise_frame=${NOISE_FRAME}" python - <<'PY' import os import time from pathlib import Path import torch from diffusers.models import AutoencoderKLWan from diffusers.utils import export_to_video from helios.diffusers_version.pipeline_helios_diffusers import HeliosPipeline from helios.diffusers_version.scheduling_helios_diffusers import HeliosScheduler from helios.diffusers_version.transformer_helios_diffusers import HeliosTransformer3DModel from helios.modules.helios_kernels import ( replace_all_norms_with_flash_norms, replace_rmsnorm_with_fp32, replace_rope_with_flash_rope, ) def parse_csv_ints(value): return [int(x.strip()) for x in str(value).split(",") if x.strip()] def parse_dtype(value): if value == "fp32": return torch.float32 if value == "fp16": return torch.float16 return torch.bfloat16 base_model_path = os.environ["BASE_MODEL_PATH"] transformer_path = os.environ["TRANSFORMER_PATH"] output_root = Path(os.environ["OUTPUT_ROOT"]) debug_dir = Path(os.environ["DEBUG_DIR"]) height = int(os.environ["HEIGHT"]) width = int(os.environ["WIDTH"]) num_frames = int(os.environ["NUM_FRAMES"]) fps = int(os.environ["FPS"]) num_inference_steps = int(os.environ["NUM_INFERENCE_STEPS"]) guidance_scale = float(os.environ["GUIDANCE_SCALE"]) seed = int(os.environ["SEED"]) weight_dtype = parse_dtype(os.environ["WEIGHT_DTYPE"]) token_dynamics_debug = { "output_dir": str(debug_dir), "chunks": parse_csv_ints(os.environ["CHUNKS"]), "history_frame": int(os.environ["HISTORY_FRAME"]), "noise_frame": int(os.environ["NOISE_FRAME"]), "pass_names": ["cond"], "overwrite": True, } device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if device.type != "cuda": raise RuntimeError("This script expects a CUDA device for Helios inference.") transformer = HeliosTransformer3DModel.from_pretrained( transformer_path, subfolder="transformer", torch_dtype=weight_dtype, ) transformer = replace_rmsnorm_with_fp32(transformer) transformer = replace_all_norms_with_flash_norms(transformer) replace_rope_with_flash_rope() cuda_major = torch.cuda.get_device_capability()[0] if cuda_major >= 9: try: transformer.set_attention_backend("_flash_3_hub") except Exception: transformer.set_attention_backend("flash_hub") else: transformer.set_attention_backend("flash_hub") vae = AutoencoderKLWan.from_pretrained( base_model_path, subfolder="vae", torch_dtype=torch.float32, ) scheduler = HeliosScheduler.from_pretrained( base_model_path, subfolder="scheduler", ) pipe = HeliosPipeline.from_pretrained( base_model_path, transformer=transformer, vae=vae, scheduler=scheduler, torch_dtype=weight_dtype, ) pipe = pipe.to(device) start = time.time() pipe_output = pipe( prompt=os.environ["PROMPT"], negative_prompt=os.environ["NEGATIVE_PROMPT"], height=height, width=width, num_frames=num_frames, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=torch.Generator(device=device).manual_seed(seed), history_sizes=[16, 2, 1], num_latent_frames_per_chunk=9, keep_first_frame=True, is_enable_stage2=False, use_zero_init=False, zero_steps=1, token_dynamics_debug=token_dynamics_debug, ) elapsed = time.time() - start video = pipe_output.frames[0] video_path = output_root / "token_dynamics_debug_video.mp4" export_to_video(video, str(video_path), fps=fps) print(f"Saved video: {video_path}") print(f"Saved artifacts in: {debug_dir}") print(f"Elapsed: {elapsed:.1f}s") print(f"Max CUDA memory: {torch.cuda.max_memory_allocated() / 1024**3:.3f} GB") PY mapfile -t ARTIFACTS < <(find "${DEBUG_DIR}" -maxdepth 1 -type f -name 'token_dynamics_*.pt' | sort) if [[ "${#ARTIFACTS[@]}" -eq 0 ]]; then echo "No token dynamics artifacts were written. Check CHUNKS and NUM_FRAMES." >&2 exit 1 fi echo "Visualizing ${#ARTIFACTS[@]} artifact(s)" for artifact in "${ARTIFACTS[@]}"; do python tools/visualize_token_dynamics.py \ "${artifact}" \ --output-dir "${DEBUG_DIR}" \ --stride "${VIS_STRIDE}" \ --max-pairs "${VIS_MAX_PAIRS}" python tools/visualize_token_match_frames.py \ "${artifact}" \ --model-path "${BASE_MODEL_PATH}" \ --output-dir "${DEBUG_DIR}" done echo "Done." echo "Video: ${OUTPUT_ROOT}/token_dynamics_debug_video.mp4" echo "Artifacts and PNGs: ${DEBUG_DIR}"