SolarRenderer: Few-Step Generative Reference Rendering
Python
"""Reader for omni samples: one safetensors member per sample, from ``tools/omni_preencode.py``.
A sample carries its target latents (any ``5n+2`` length, landscape or portrait), the released
``ref2va`` prompt embeddings, any number of untyped visual observations and, optionally, the known
degree of every target row. It has no first-frame anchor and no camera unless the member provides
them. The reader is deliberately small next to the release reader: an index of members, a
deterministic shuffle per epoch, a logical data-parallel partition, and a cursor that survives a
checkpoint. Members live wherever they were written, a directory or a remote object-storage prefix.
"""
from __future__ import annotations
import contextlib
import json
import os
import tempfile
from collections.abc import Mapping
from dataclasses import replace
from pathlib import Path
from typing import Any
import numpy as np
from solarwm import storage
from solarwm.errors import DataContractError
from .artifacts import (
H3ArtifactBatch,
H3VisualObservation,
read_encoder_contract,
validate_absolute_c2w,
validate_normalized_intrinsics,
views_from_c2w,
)
def is_remote(path: str) -> bool:
"""A remote object-storage location (``<scheme>://...``), read through solarwm.storage."""
return storage.is_remote(path)
@contextlib.contextmanager
def _local(path: str):
"""Yield a local file for ``path``; a remote object goes through scratch and is removed."""
if not is_remote(path):
yield path
return
handle, scratch = tempfile.mkstemp(
suffix=Path(path).suffix, dir=os.environ.get("OMNI_SCRATCH", "/tmp")
)
os.close(handle)
try:
storage.get(path, scratch)
yield scratch
finally:
Path(scratch).unlink(missing_ok=True)
def coarse(item: H3VisualObservation, spec: Mapping[str, Any]) -> H3VisualObservation:
"""An aligned video observation read coarser than it was encoded: its latent grid averaged over
``pool`` x ``pool`` cells and every ``stride``-th latent kept, each on its own target frame.
The layout lays an aligned observation over the whole canvas whatever its grid, so a half-grid
structure video still covers the picture; it costs a quarter of the tokens, and a third of them
again in time (CWM gives its proxy at a quarter of the target, one frame in twelve). H3's front
end resizes every video reference to its own size, so the coarse grid is made here, from the
member already encoded. A single frame (the first frame) and a free observation are left alone.
"""
import torch
pool, stride = int(spec.get("pool") or 1), int(spec.get("stride") or 1)
if item.frames is None or len(item.frames) < 2 or (pool <= 1 and stride <= 1):
return item
latents, frames, boxes = item.latents, item.frames, item.boxes
if stride > 1:
latents, frames = latents[:, ::stride], frames[::stride]
# One box per frame follows its frame.
if boxes is not None and len(boxes) == len(item.frames):
boxes = boxes[::stride]
if pool > 1:
# Each side to the nearest even number of cells at 1/pool (the 2x2 patch needs even
# sides):
# 48x84 at a quarter is 12x21, read as 12x22, every cell the average of the latents it
# overlaps. The grid still spans the whole canvas, as the layout lays it.
height, width = int(latents.shape[-2]), int(latents.shape[-1])
size = tuple(max(2, 2 * int(side / pool / 2 + 0.5)) for side in (height, width))
if size == (height // pool, width // pool) and not height % pool and not width % pool:
latents = torch.nn.functional.avg_pool2d(latents.float(), pool)
else:
latents = torch.nn.functional.adaptive_avg_pool2d(latents.float(), size)
latents = latents.to(item.latents.dtype)
return replace(item, latents=latents.contiguous(), frames=tuple(frames), boxes=boxes)
class H3OmniStream:
def __init__(
self,
*,
root: str,
index: str,
topology: Any,
seed: int,
encoder_contract_path: str,
shuffle: bool = True,
aligned_control: bool = False,
length_buckets: bool = False,
) -> None:
# ``root`` and ``index`` are local paths or remote object-storage locations.
self.root = root.rstrip("/")
with _local(index) as path, open(path) as lines:
rows = [json.loads(line) for line in lines if line.strip()]
if not rows:
raise DataContractError(f"omni index {index} is empty")
self.rows = sorted(rows, key=lambda row: row["id"])
self.rank, self.world = int(topology.dp_rank), int(topology.dp_world_size)
if len(self.rows) < self.world:
raise DataContractError("omni index holds fewer samples than data-parallel readers")
self.seed, self.shuffle = int(seed), bool(shuffle)
# A run whose control trunk reads the aligned observation cannot use a sample without one
# laid over every target frame; such a sample is skipped here, with the unreadable ones,
# rather than failing inside a training step where one rank's error stalls every rank.
self.aligned_control = bool(aligned_control)
# Every data-parallel reader draws a clip of the same length at the same step, so a step is
# not held to the longest of forty random clips.
self.length_buckets = bool(length_buckets)
_, self.encoder_profile = read_encoder_contract(encoder_contract_path)
self.epoch, self.cursor = 0, 0
self.current_plan_fingerprint = f"omni:{Path(index).name}:{len(self.rows)}"
def _order(self) -> np.ndarray:
"""This reader's rows for the epoch, computed once per epoch (an index runs to millions)."""
cached = getattr(self, "_cached_order", None)
if cached is not None and cached[0] == self.epoch:
return cached[1]
order = None
if getattr(self, "length_buckets", False) and self.shuffle:
order = self._bucketed_order()
# An index too thin to fill one run per group would yield nothing and spin epochs.
order = order if len(order) else None
if order is None:
order = np.arange(len(self.rows))
if self.shuffle:
np.random.default_rng([self.seed, self.epoch]).shuffle(order)
usable = (
len(order) - len(order) % self.world
) # every reader sees the same number of samples
order = order[:usable]
mine = order[self.rank :: self.world]
self._cached_order = (self.epoch, mine)
return mine
def _bucketed_order(self) -> np.ndarray:
"""The epoch's global order in runs of ``world`` rows that share a clip length.
Rows are grouped by target length and number of observations (what sets the packed
sequence), each group shuffled and cut into runs of one row per reader, and the runs
shuffled together. A group's last partial run waits for a later epoch. Readers take every
``world``-th row as before, so at each step they all hold the same length.
"""
rng = np.random.default_rng([self.seed, self.epoch])
groups: dict[tuple[int, int], list[int]] = {}
for index, row in enumerate(self.rows):
key = (int(row.get("latent_frames") or 47), int(row.get("observations") or 0))
groups.setdefault(key, []).append(index)
runs = []
for key in sorted(groups):
members = np.asarray(groups[key], dtype=np.int64)
rng.shuffle(members)
usable = len(members) - len(members) % self.world
runs.extend(members[:usable].reshape(-1, self.world))
if not runs:
return np.zeros(0, dtype=np.int64)
runs = np.stack(runs)
rng.shuffle(runs)
return runs.reshape(-1)
def state_dict(self) -> dict[str, int]:
return {"epoch": self.epoch, "cursor": self.cursor}
def load_state_dict(self, state: dict[str, int]) -> None:
self.epoch, self.cursor = int(state["epoch"]), int(state["cursor"])
def close(self) -> None:
return None
def __iter__(self):
while True:
yield self.next()
def next(self) -> H3ArtifactBatch:
failures = 0
while True:
order = self._order()
if self.cursor >= len(order):
self.epoch, self.cursor = self.epoch + 1, 0
continue
row = self.rows[int(order[self.cursor])]
self.cursor += 1
try:
return self.read(row)
except Exception as exc: # one bad member must not take the run down
failures += 1
print(f"[omni-data] skipped {row.get('id')}: {exc!r}", flush=True)
if failures >= 32:
raise DataContractError("32 consecutive omni members failed to load") from exc
def camera(self, tensors: Mapping[str, Any], latent_frames: int) -> tuple[Any, Any]:
"""The clip's camera in the model's frame, or nothing when the member carries none.
A member stores absolute camera-to-world poses, one per latent, exactly as the released
artifacts do; the first camera of the clip becomes the origin here, as it does there.
"""
import torch
c2w, intrinsics = tensors.get("camera_c2w"), tensors.get("camera_K")
if c2w is None and intrinsics is None:
return None, None
if c2w is None or intrinsics is None:
raise DataContractError("an omni camera needs both the poses and the intrinsics")
if tuple(c2w.shape) != (latent_frames, 4, 4):
raise DataContractError(
f"omni camera holds {tuple(c2w.shape)}, not [{latent_frames},4,4]"
)
if tuple(intrinsics.shape) == (3, 3):
intrinsics = intrinsics.unsqueeze(0).expand(latent_frames, -1, -1).clone()
if tuple(intrinsics.shape) != (latent_frames, 3, 3):
raise DataContractError(
f"omni intrinsics hold {tuple(intrinsics.shape)}, not [{latent_frames},3,3]"
)
c2w, intrinsics = c2w.float(), intrinsics.float()
if not bool(torch.isfinite(c2w).all()) or not bool(torch.isfinite(intrinsics).all()):
raise DataContractError("omni camera holds non-finite values")
validate_absolute_c2w(c2w.numpy())
validate_normalized_intrinsics(intrinsics.numpy())
views = views_from_c2w(c2w)
if float(views.abs().max()) > 20.0:
raise DataContractError("omni camera exceeds the magnitude guard")
return views, intrinsics.contiguous()
def first_frame(self, path: str, target: Any) -> H3VisualObservation:
"""The clip's first frame, encoded apart from its member, as an observation on latent 0.
An index row may name a file holding ``first_frame_latents`` [24,1,h,w] (the frame held as
a still and encoded on the target's grid, as ``tools/omni_preencode.py`` encodes an aligned
image). It joins the member's observations as given; the member's prompt is kept, so the
frame reaches the model through its latent alone and dropping it leaves exactly the sample
without one - the same input a render without a first frame gives.
"""
import torch
from safetensors import safe_open
if not (is_remote(path) or path.startswith("/")):
path = f"{self.root}/{path}"
with _local(path) as local, safe_open(local, framework="pt") as handle:
latents = handle.get_tensor("first_frame_latents")
if tuple(latents.shape) != (24, 1, *target.shape[2:]):
raise DataContractError(
f"first frame latents {tuple(latents.shape)} are not [24,1,"
f"{','.join(str(v) for v in target.shape[2:])}]"
)
if not bool(torch.isfinite(latents.float()).all()):
raise DataContractError("first frame latents hold non-finite values")
return H3VisualObservation(latents=latents.contiguous(), frames=(0,), degree=0.999)
def read(self, row: dict[str, Any]) -> H3ArtifactBatch:
import torch
from safetensors import safe_open
member = row["member"]
if not (is_remote(member) or member.startswith("/")):
member = f"{self.root}/{member}" # an index may also name its members outright
with _local(member) as path, safe_open(path, framework="pt") as handle:
meta = json.loads((handle.metadata() or {}).get("omni", "{}"))
tensors = {key: handle.get_tensor(key) for key in handle.keys()} # noqa: SIM118
target, prompt, tags = (
tensors["target_latents"],
tensors["prompt_embeds"],
tensors["text_token_tags"],
)
if target.ndim != 4 or target.shape[0] != 24 or (target.shape[1] - 2) % 5:
raise DataContractError(
f"omni target latents {tuple(target.shape)} are not [24,5n+2,h,w]"
)
if tuple(target.shape[2:]) not in {(48, 84), (84, 48)}:
raise DataContractError(
f"omni target grid {tuple(target.shape[2:])} is not 48x84 or 84x48"
)
if prompt.ndim != 2 or prompt.shape[-1] != 5120 or tags.shape != prompt.shape[:1]:
raise DataContractError("omni prompt embeddings differ")
if not bool(torch.isfinite(target.float()).all()) or not bool(
torch.isfinite(prompt.float()).all()
):
raise DataContractError("omni member holds non-finite values")
observations = []
for index, item in enumerate(meta.get("observations", ())):
latents = tensors[f"observation_{index}_latents"]
frames = item.get("frames")
observations.append(
H3VisualObservation(
latents=latents.contiguous(),
frames=None if frames is None else tuple(int(f) for f in frames),
degree=float(item.get("degree", 0.999)),
boxes=None
if not item.get("boxes")
else tuple(tuple(float(v) for v in box) for box in item["boxes"]),
)
)
if row.get("coarse_observations"):
observations = [coarse(item, row["coarse_observations"]) for item in observations]
if row.get("free_observations"):
# The unaligned-reference ablation: the structure video placed as the base's own reference,
# its own stretch of the time axis; a single-frame observation (the first frame) stays aligned.
observations = [
replace(item, frames=None) if item.frames is not None and len(item.frames) > 1 else item
for item in observations
]
if row.get("first_frame"):
observations.append(self.first_frame(row["first_frame"], target))
views, intrinsics = self.camera(tensors, int(target.shape[1]))
batch = H3ArtifactBatch(
sample_id=str(row["id"]),
start_frame=0,
plan_fingerprint=self.current_plan_fingerprint,
target_latents=target.contiguous(),
anchor_latents=None,
prompt_embeds=prompt.contiguous(),
text_token_tags=tags.contiguous(),
source_frame_indices=None,
camera_viewmats=views,
camera_K=intrinsics,
source_fps=24.0,
dataset_source=str(meta.get("source", row.get("source", ""))),
# The ref2va prompt never shows the first frame, so it is also the anchor-free prompt.
text_prompt_embeds=prompt.contiguous(),
text_prompt_tags=tags.contiguous(),
visual_observations=tuple(observations) or None,
known_degrees=tensors.get("known_degrees"),
)
if self.aligned_control:
from .stage0p5 import optional_control_latents
optional_control_latents(batch) # a structure video the trunk cannot take fails here
return batch
__all__ = ["H3OmniStream"]