"""Stateless layer primitives for the Waypoint-1.5 DiT.
Port of ``world_engine/src/model/nn.py`` plus the three conditioning modules
from ``world_engine/src/model/world_model.py``. Parameter names track
checkpoint keys (``fc1``/``fc2``, ``bias_in``, ``cond_proj``, ``mlp``), not
mstar's usual ``components.mlp.MLP`` spelling, so this doesn't reuse it.
"""
import contextlib
import torch
import torch.nn.functional as F
from torch import nn
from mstar.model.waypoint.config import WaypointConfig
# Submodule paths, relative to the WaypointDiT root, whose parameters must be
# restored to fp32 after a global .to(torch.bfloat16). The reference's
# NoCastModule set has three members; the other two (OrthoRoPEAngles, OrthoRoPE)
# carry no parameters or buffers for a cast to corrupt.
FP32_MODULE_PATHS: tuple[str, ...] = ("denoise_step_emb",)
[docs]
def bf16_roundtrip(table: torch.Tensor) -> torch.Tensor:
"""Quantize an fp32 derived table the way ``NoCastModule._apply`` does.
Only reachable under ``WaypointConfig.reference_compat``.
"""
return table.to(torch.bfloat16).to(table.dtype)
[docs]
@contextlib.contextmanager
def fp32_matmul_highest():
"""Run fp32 GEMMs as IEEE fp32, whatever the process-wide
``float32_matmul_precision`` (``mstar.engine`` sets ``'high'``). Eager
only: the setting is global, so dynamo cannot trace a switch of it."""
previous = torch.get_float32_matmul_precision()
torch.set_float32_matmul_precision("highest")
try:
yield
finally:
torch.set_float32_matmul_precision(previous)
def _bf16_bits(x: torch.Tensor) -> torch.Tensor:
"""bf16 storage reinterpreted as an unsigned ``0..65535`` LUT index."""
return x.contiguous().view(torch.int16).to(torch.int32) & 0xFFFF
[docs]
class DeviceTableCache:
"""Per-device replicas of small derived fp32 tables (RoPE frequencies,
Fourier frequencies), held outside the module tree so they stay out of
``state_dict``/``to_empty``'s reach and fp32 forever.
Callers MUST build the CPU tables with an explicit ``device="cpu"``:
``__init__`` runs under ``with torch.device("meta")``, where an
ambient-device ``torch.arange`` produces a data-less meta tensor.
"""
_CPU = torch.device("cpu")
def __init__(self, *tables: torch.Tensor):
for table in tables:
if table.device.type != "cpu":
raise ValueError(
f"DeviceTableCache expects CPU-built tables (got {table.device}); "
"pass device='cpu' explicitly so the meta build context cannot capture it."
)
self._by_device: dict[torch.device, tuple[torch.Tensor, ...]] = {self._CPU: tables}
[docs]
def get(self, device: torch.device) -> tuple[torch.Tensor, ...]:
device = torch.device(device)
if device not in self._by_device:
self._by_device[device] = tuple(t.to(device) for t in self._by_device[self._CPU])
return self._by_device[device]
[docs]
def rms_norm(x: torch.Tensor) -> torch.Tensor:
"""Unweighted RMS norm over the last dim (reference ``nn.rms_norm``).
No learned gain: every use site either gets its scale from adaLN or wants a
bare normalization (Q/K norm, the two ``MLPFusion`` inputs).
"""
return F.rms_norm(x, (x.size(-1),))
[docs]
def ada_rmsnorm(x: torch.Tensor, scale: torch.Tensor, bias: torch.Tensor) -> torch.Tensor:
"""Per-frame adaLN modulation: ``rms_norm(x) * (1 + scale) + bias``.
``x`` is ``[B, N*T, D]``; ``scale`` and ``bias`` are ``[B, N, D]``, one
modulation vector per *frame*. The unflatten is what makes those vectors
broadcast over their own frame's tokens.
"""
x4 = x.unflatten(1, (scale.size(1), -1))
y4 = rms_norm(x4) * (1 + scale.unsqueeze(2)) + bias.unsqueeze(2)
return y4.flatten(1, 2)
[docs]
def ada_gate(x: torch.Tensor, gate: torch.Tensor) -> torch.Tensor:
"""Per-frame adaLN output gate: ``x * gate``, same shapes as
``ada_rmsnorm``. No ``1 +`` here -- the gate multiplies the sublayer output
before the residual add, so a zero gate contributes nothing."""
x4 = x.unflatten(1, (gate.size(1), -1))
return (x4 * gate.unsqueeze(2)).flatten(1, 2)
[docs]
class AdaLN(nn.Module):
"""adaLN with the scale/shift projection folded in: one Linear produces
``[scale | shift]`` from ``silu(cond)``.
The DiT's output head only; blocks get their six modulation tensors from
``CondHead``. ``cond`` is per-frame ``[B, N, D]``, expanded over its tokens.
"""
def __init__(self, dim: int):
super().__init__()
self.fc = nn.Linear(dim, 2 * dim, bias=False)
[docs]
def forward(self, x: torch.Tensor, cond: torch.Tensor) -> torch.Tensor:
b, n, d = cond.shape
_, nm, _ = x.shape
m = nm // n
ab = self.fc(F.silu(cond)) # [b, n, 2d]
ab = ab.view(b, n, 1, 2 * d).expand(-1, -1, m, -1).reshape(b, nm, 2 * d)
scale, shift = ab.chunk(2, dim=-1)
return rms_norm(x) * (1 + scale) + shift
[docs]
class MLP(nn.Module):
"""Two-layer SiLU MLP, both projections bias-free.
``bias=False`` is a checkpoint fact: the reference's ``MLPFusion`` calls
``F.linear(h, self.mlp.fc2.weight)`` with no bias argument, which is only
correct because there is no bias to pass.
"""
def __init__(self, dim_in: int, dim_middle: int, dim_out: int):
super().__init__()
self.fc1 = nn.Linear(dim_in, dim_middle, bias=False)
self.fc2 = nn.Linear(dim_middle, dim_out, bias=False)
[docs]
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.fc2(F.silu(self.fc1(x)))
[docs]
class NoiseConditioner(nn.Module):
"""sigma -> Fourier features -> MLP, the DiT's only noise-level input.
fp32 island: ``FP32_MODULE_PATHS`` pins ``self.mlp`` back to fp32 after the
serving bf16 cast, and the body runs under ``autocast(enabled=False)`` on a
``.float()`` sigma and, eagerly, under ``fp32_matmul_highest`` (a batched
call rounds through TF32 at ``'high'``, and cuBLAS 13 on a B200 rounds
even the served ``[1, 512] @ [512, 8192]`` GEMV). Compiled regions read
the rows ``WaypointDiT`` builds eagerly instead of running the island.
``* 1000`` scales [0, 1] into the Fourier basis's
rotating range; ``* 2**0.5`` restores unit variance after the sin/cos concat.
"""
def __init__(
self,
dim: int,
fourier_dim: int = 512,
base: float = 10_000.0,
*,
reference_compat: bool = False,
cached_sigmas: tuple[float, ...] = (),
):
super().__init__()
if fourier_dim % 2:
raise ValueError(f"NoiseConditioner needs an even fourier_dim; got {fourier_dim}.")
self.fourier_dim = fourier_dim
# Derived, not checkpoint state. device="cpu" is required: __init__ runs
# under the meta-device build context.
freq = torch.logspace(
0, -1, steps=fourier_dim // 2, base=base, dtype=torch.float32, device="cpu"
)
self._freq = DeviceTableCache(bf16_roundtrip(freq) if reference_compat else freq)
self.mlp = MLP(fourier_dim, dim * 4, dim)
self.reference_compat = reference_compat
self.cached_sigmas = tuple(float(s) for s in cached_sigmas)
self._lut: dict[torch.device, tuple[torch.Tensor, torch.Tensor, torch.Tensor]] = {}
if reference_compat:
if not self.cached_sigmas:
raise ValueError(
"reference_compat NoiseConditioner needs the sigma schedule to cache."
)
levels = torch.tensor(self.cached_sigmas, dtype=torch.bfloat16, device="cpu")
if len(set(_bf16_bits(levels).tolist())) != len(self.cached_sigmas):
raise ValueError(
f"scheduler_sigmas {self.cached_sigmas} collide in bf16; the reference's "
"LUT would be ambiguous."
)
[docs]
def forward(self, s: torch.Tensor) -> torch.Tensor:
"""``s`` is ``[B, N]`` sigma; returns ``[B, N, dim]`` in ``s``'s dtype
(bf16 under ``reference_compat``, whose table is stored bf16)."""
if self.reference_compat:
return self._cached_embed(s)
if torch.compiler.is_compiling():
return self._embed(s)
with fp32_matmul_highest():
return self._embed(s)
[docs]
def materialize(self, device: torch.device | str) -> None:
"""Materialize frequency data and the optional post-load sigma LUT."""
device = torch.device(device)
self._freq.get(device)
if self.reference_compat:
self._reference_lut(device)
def _embed(self, s: torch.Tensor) -> torch.Tensor:
orig_dtype, shape = s.dtype, s.shape
(freq,) = self._freq.get(s.device)
with torch.amp.autocast("cuda", enabled=False):
s = s.reshape(-1).float() * 1000 # fp32 for Fourier stability; x1000 for rotation range
phase = s[:, None] * freq[None, :]
emb = torch.cat((torch.sin(phase), torch.cos(phase)), dim=-1)
emb = self.mlp(emb * 2**0.5)
return emb.to(orig_dtype).view(*shape, -1)
def _cached_embed(self, s: torch.Tensor) -> torch.Tensor:
"""The reference's ``CachedDenoiseStepEmb``: read the embedding out of a
bf16 table keyed on sigma's own bf16 bits. A sigma that is not on the
schedule indexes one past the table and raises, rather than returning a
neighbouring row."""
if s.dtype is not torch.bfloat16:
raise RuntimeError(f"reference_compat NoiseConditioner expects bf16 sigma; got {s.dtype}.")
table, lut, oob = self._reference_lut(s.device)
idx = lut[_bf16_bits(s)]
return table[torch.where(idx >= 0, idx, oob).to(torch.int64)]
def _reference_lut(
self, device: torch.device
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""Build (once per device) the reference's sigma table.
The batch shape is load-bearing: an M=S GEMM (all S sigmas in one
call) rounds through TF32 under ``float32_matmul_precision('high')``,
where the served M=1 GEMV stays exact fp32 (not with cuBLAS 13 on a
B200). Built on first use, so an eager forward must warm it before
``compile_regions``.
"""
device = torch.device(device)
if device not in self._lut:
levels = torch.tensor(self.cached_sigmas, dtype=torch.bfloat16, device=device)
with torch.no_grad():
table = self._embed(levels[:, None]).squeeze(1).to(torch.bfloat16).contiguous()
lut = torch.full((65536,), -1, dtype=torch.int32, device=device)
lut[_bf16_bits(levels)] = torch.arange(
len(self.cached_sigmas), dtype=torch.int32, device=device
)
oob = torch.tensor(len(self.cached_sigmas), dtype=torch.int32, device=device)
self._lut[device] = (table, lut, oob)
return self._lut[device]
[docs]
class MLPFusion(nn.Module):
"""Fuses a per-frame conditioning vector into that frame's tokens.
Parameter tree is ``MLP(2*D, D, D)`` over ``cat([x, cond])`` -- one
``[D, 2D]`` ``mlp.fc1`` for the loader to fill. Compute splits that matrix
instead (``chunk(2, dim=1)``) so ``cond`` broadcasts over the T tokens of
its frame, compute-time only -- stored split parameters would undo the
loader's concat into ``mlp.fc1``.
"""
lora_unservable = dict.fromkeys(("mlp.fc1", "mlp.fc2"), "run as F.linear over its weight, never its own forward")
def __init__(self, config: WaypointConfig):
super().__init__()
self.mlp = MLP(2 * config.d_model, config.d_model, config.d_model)
[docs]
def forward(self, x: torch.Tensor, cond: torch.Tensor) -> torch.Tensor:
"""``x`` ``[B, N*T, D]``, ``cond`` ``[B, N, D]`` -> ``[B, N*T, D]``."""
B, _, D = x.shape
L = cond.shape[1]
Wx, Wc = self.mlp.fc1.weight.chunk(2, dim=1) # each [D, D]
x = x.view(B, L, -1, D)
h = F.linear(x, Wx) + F.linear(cond, Wc).unsqueeze(2) # broadcast, no repeat/cat
h = F.silu(h)
y = F.linear(h, self.mlp.fc2.weight)
return y.flatten(1, 2)
[docs]
class CondHead(nn.Module):
"""Per-layer WAN-style conditioning head: the noise embedding becomes the
block's six adaLN modulation tensors (scale/shift/gate for attention, then
the same three for the MLP).
``bias_in`` is genuinely per-layer; the six ``cond_proj`` Linears are
physically shared across all 24 blocks via ``WaypointDiT.retie_cond_proj``
(must run after ``to_empty`` -- see its docstring).
"""
n_cond = 6
def __init__(self, config: WaypointConfig):
super().__init__()
if config.noise_conditioning == "wan":
self.bias_in = nn.Parameter(torch.zeros(config.d_model))
else:
self.register_parameter("bias_in", None)
self.cond_proj = nn.ModuleList(
nn.Linear(config.d_model, config.d_model, bias=False) for _ in range(self.n_cond)
)
# ``[S, 6, 1, 1, D]`` after ``build_cache``; None keeps ``forward`` on
# the live projection until the runtime tables are materialized.
self._cache: torch.Tensor | None = None
def _project(self, cond: torch.Tensor) -> tuple[torch.Tensor, ...]:
"""The live head: ``cond`` ``[B, N, D]`` -> six ``[B, N, D]`` tensors, in
block order ``(s0, b0, g0, s1, b1, g1)``."""
cond = cond + self.bias_in if self.bias_in is not None else cond
h = F.silu(cond)
return tuple(p(h) for p in self.cond_proj)
[docs]
def build_cache(self, conds: torch.Tensor) -> None:
"""Precompute the six modulation tensors for every scheduled sigma.
``conds`` is ``[S, 1, D]`` -- one embedding per ``scheduler_sigmas``
entry, each built at M=1 exactly as ``forward`` receives it, so a cached
row is bit-identical to the live ``_project`` it replaces. Switches
``forward`` to a gather; idempotent enough to rebuild.
"""
with torch.no_grad():
rows = [
torch.stack(self._project(conds[i : i + 1]), dim=0)
for i in range(conds.size(0))
]
self._cache = torch.stack(rows, dim=0)
[docs]
def forward(self, cond: torch.Tensor, cond_idx: int) -> tuple[torch.Tensor, ...]:
"""``cond`` ``[B, N, D]``, ``cond_idx`` the ``scheduler_sigmas`` slot ->
six ``[B, N, D]`` tensors ``(s0, b0, g0, s1, b1, g1)``.
After ``build_cache`` the six come from the cache row ``cond_idx`` and
``cond`` is unused
"""
if self._cache is not None:
return tuple(self._cache[cond_idx].unbind(0))
return self._project(cond)