The v2 top level had grown to ~27 dirs + 13 loose files — half of them
single-concept abstraction shells (memory/ 86 LOC, transport/ 222, parity/ 178,
extend/ 226) and half vendored fastvideo code sitting as peers to the actual v2
design. Regroup to mirror README section 3 "Planes & dependency order":
core/ enums+types, card, loop, program, parity, request, parallel
(the model-native contracts; no kernels)
runtime/ + folded-in substrate: cache, memory, transport, extend
(the import graph shows only runtime consumes them; compile/cudagraph
already lived here)
serving/ + deploy/ (products / fleet)
_vendor/ all copied fastvideo: models, layers, attention, configs, distributed,
platforms, api, hooks, logging_utils, third_party + fastvideo_args/
utils/logger/envs/forward_context — internal layout unchanged, still
mirrors upstream for diffing
28 dirs + 13 root files -> 8 dirs + 6 root files. Pure mechanical move: 947
absolute-import paths rewritten (v2.X -> v2.{core,runtime,serving,_vendor}.X),
boundary-anchored so platform/ (native dispatch) and platforms/ (vendored CUDA
detect) no longer collide and neither does hooks/ vs extend/. Deleted the empty
v2/loader/. README section 3 + 16 updated; stale test count corrected
(34 files/216 tests -> 22 files/143 tests).
Validated: `import v2` stays torch-free; `v2/run_tests.py` and `pytest v2/tests/`
-> 143 passed, 2 skipped on the numpy toy backend. (On a GPU box force the toy
backend with CUDA_VISIBLE_DEVICES="" or detect() picks cuda.) No external importer
changed — examples use the re-exported `from v2 import VideoGenerator`.
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
"""Streams + typed event taxonomy.
|
|
|
|
A ``Stream`` is one ordered event channel for previews, media chunks, progress,
|
|
logs, and finals. Event taxonomy: ``request.*``, ``session.*``, ``artifact.*``,
|
|
``media.{init,chunk,complete}``, ``trace.*``. A ``media.chunk`` carries its
|
|
stream, byte-range/buffer-ref, codec/container, timestamp range, and
|
|
preview-vs-final flag, so invalid combinations are unrepresentable.
|
|
|
|
For v2 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 v2.core.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)
|