Files
hao-ai-lab-FastVideo/mini_fastvideo/parallel/mesh.py
T
SolitaryThinker 270fae959d [feat] mini-fastvideo: model-native runtime per design_v3 (Wan2.1/LTX2 + 4 training methods)
A scoped, CPU-testable realization of design_v3.md — the architecture where the atomic unit
is a typed (recipe, runtime) ModelCard, the model owns loop semantics while the runtime owns
loop lifecycle, one resident instance runs many loops, and training records behavior on the
same loops it serves.

Implements:
- card/ loop/ runtime/ cache/ memory/ parallel/ parity/ extend/ program/ request/ training/
  spanning design_v3 §4-§13: ModelCard + validate(); driven loops (init/next/advance/finalize);
  step-interleaving Engine with reservation-before-admission + per-class caches keyed by CacheKey;
  the C0-C4 consistency ladder + the non-negotiable batch-of-N interleave parity gate.
- Inference: Wan2.1-1.3B (T2V), LTX2.3 (two-stage distilled, shared transformer), Wan-causal
  (chunk rollout + slab-KV streaming).
- Training (Wan2.1-1.3B), each driving the SAME loops the engine serves: finetune (flow-match),
  DMD2 (teacher/critic distribution matching), DiffusionNFT (likelihood-free C2, samples from the
  decay-blended old policy, group-relative advantages, shared-prompt cache reuse), self-forcing
  (causal chunk loop). The engine never imports training (the §10 dependency rule, grep-verified).

numpy-only core (no torch/GPU here); heavy Wan/LTX forwards are deterministic toy stand-ins with
lazy torch-adapter seams (ComponentSpec.load_id/factory) for a GPU box. 49 tests pass via pytest
and a zero-dependency runner; interleave parity verified (and a buggy module-global interceptor
provably breaks it). Omni-ready spine (ar_decode/chunk_step loop kinds, multi-loop instances,
LoopState.extension) for the phase-2 Cosmos3 + vllm-omni omni ports.

Run: python3 -m pytest mini_fastvideo/tests/ -q ; python3 -m mini_fastvideo.examples
2026-07-04 17:15:33 +00:00

59 lines
2.1 KiB
Python

"""FakeDeviceMesh — a ParallelDims-style builder, CPU-testable (design_v3 §8; torchtitan).
> Declarative, validated, compiled to a PyTorch ``DeviceMesh`` via a ``ParallelDims``-
> style builder (product-of-degrees validation, cached submeshes).
On a GPU box this compiles to ``torch.distributed.device_mesh.DeviceMesh``. Here it is a
pure-Python mesh of rank tuples so topology logic is unit-tested without GPUs.
Degree-one axes exist as trivial groups so component code needs no special cases.
"""
from __future__ import annotations
from itertools import product
from .plan import ParallelPlan
from .validation import validate_plan
class FakeDeviceMesh:
def __init__(self, plan: ParallelPlan):
self.plan = plan
self.order = plan.mesh_order or list(plan.axes.keys())
self.shape = tuple(plan.degree(a) for a in self.order)
self.world_size = plan.world_size()
def group_ranks(self, axis: str) -> list[list[int]]:
"""All collective groups along one axis (each is a list of global ranks)."""
if axis not in self.order:
return [[0]] # degree-one trivial group
axis_idx = self.order.index(axis)
groups: list[list[int]] = []
ranges = [range(d) for d in self.shape]
seen: set[tuple] = set()
for coord in product(*ranges):
key = coord[:axis_idx] + coord[axis_idx + 1:]
if key in seen:
continue
seen.add(key)
group = []
for i in range(self.shape[axis_idx]):
c = list(coord)
c[axis_idx] = i
group.append(self._coord_to_rank(tuple(c)))
groups.append(group)
return groups
def _coord_to_rank(self, coord: tuple[int, ...]) -> int:
rank = 0
for c, d in zip(coord, self.shape):
rank = rank * d + c
return rank
def __repr__(self) -> str:
return f"FakeDeviceMesh(order={self.order}, shape={self.shape}, world={self.world_size})"
def build_mesh(plan: ParallelPlan, card=None, **kw) -> FakeDeviceMesh:
validate_plan(plan, card=card, **kw)
return FakeDeviceMesh(plan)