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
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
"""Streams + typed event taxonomy (design_v3 §12).
|
|
|
|
"A ``Stream`` is one ordered event channel for previews, media chunks, progress,
|
|
logs, finals." Event taxonomy: ``request.*``, ``session.*``, ``artifact.*``,
|
|
``media.{init,chunk,complete}``, ``trace.*``. A ``media.chunk`` must know its
|
|
stream, byte-range/buffer-ref, codec/container, timestamp range, and
|
|
preview-vs-final — invalid combinations are unrepresentable.
|
|
|
|
For mini-fastvideo the Stream is a simple synchronous ordered channel (the core is
|
|
numpy/sync and CPU-testable); a GPU/server deployment swaps in an async channel
|
|
without changing the event types.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from .._types import TensorLike
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class OmniEvent:
|
|
type: str # e.g. "request.start", "media.chunk", "artifact.ready", "trace.step"
|
|
request_id: str
|
|
seq: int = 0
|
|
payload: dict[str, object] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class StreamChunk:
|
|
"""A media.chunk payload — invalid field combinations are unrepresentable."""
|
|
stream_id: str
|
|
modality: str # "video" | "audio" | "text"
|
|
seq: int
|
|
data: TensorLike = None # in-proc buffer (frames / samples / token ids)
|
|
codec: str = "raw" # raw | h264 | opus | ...
|
|
ts_start: float = 0.0 # presentation timestamp range (seconds)
|
|
ts_end: float = 0.0
|
|
preview: bool = False # preview vs final
|
|
final: bool = False # last chunk of the stream
|
|
|
|
|
|
class Stream:
|
|
"""Ordered event channel. Sync collector for tests; subscribe for streaming."""
|
|
|
|
def __init__(self, stream_id: str):
|
|
self.stream_id = stream_id
|
|
self.events: list[OmniEvent] = []
|
|
self._subscribers: list = []
|
|
|
|
def emit(self, event: OmniEvent) -> None:
|
|
self.events.append(event)
|
|
for cb in self._subscribers:
|
|
cb(event)
|
|
|
|
def subscribe(self, callback) -> None:
|
|
self._subscribers.append(callback)
|
|
|
|
def __iter__(self):
|
|
return iter(self.events)
|