HugMinjun's picture
Add native BF16 same-camera temporal conditions (23,880 pairs)
ae38f1e verified
Raw History Blame Contribute Delete
1.1 kB
"""Minimal reader for this dataset's versioned condition archives."""
import json
from pathlib import Path
import zipfile
import torch
import zstandard as zstd
from safetensors.torch import load
def load_conditions(archive, member):
"""Return saved tensors; this does not load RGB or construct external targets."""
with zipfile.ZipFile(Path(archive)) as z:
raw = zstd.ZstdDecompressor().decompress(z.read(member), max_output_size=1024**3)
header = json.loads(raw[8:8 + int.from_bytes(raw[:8], "little")])
fmt = header.get("__metadata__", {}).get("format")
if fmt not in {"bf16_dynamic_zstd_v1", "int8_dynamic_zstd_v1", "int8_dynamic_zstd_v2"}:
raise ValueError(f"Unsupported condition format: {fmt}")
tensors = load(raw)
for key in ("memory_latent", "control_latents"):
scale = tensors.pop("_qscale_" + key, None)
if scale is not None:
tensors[key] = (tensors[key].float() * scale).to(torch.bfloat16)
if tensors[key].dtype != torch.bfloat16:
raise ValueError(f"{key} is not BF16")
return tensors