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`.
86 lines
3.9 KiB
Python
86 lines
3.9 KiB
Python
"""RuntimeLoopContext — the concrete inversion point.
|
|
|
|
This is the single seam: ``execute(plan)`` runs the kernel thunk built by the model's
|
|
``next()``, wrapped by interceptors (compute-altering) and observers (read-only). The model
|
|
never sees the scheduler; the scheduler never sees the model's math. Per-request loop state
|
|
is bound here so interceptors read state from ``LoopState`` (never module globals).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from v2.core.enums import ExecutionProfile
|
|
from v2.core.loop.contracts import StepResult, WorkPlan
|
|
from v2.core.request.streams import OmniEvent, Stream, StreamChunk
|
|
|
|
|
|
class RuntimeLoopContext:
|
|
|
|
def __init__(self, instance: Any, *, observers, interceptors, slots: dict[str, Any], stream: Stream, cancel_scope,
|
|
profile: ExecutionProfile, metrics: dict, request_id: str):
|
|
self.instance = instance
|
|
self.observers = observers
|
|
self.interceptors = interceptors
|
|
self.slots = slots # the program's typed named edges
|
|
self.stream = stream
|
|
self.cancel_scope = cancel_scope
|
|
self.profile = profile
|
|
self.metrics = metrics
|
|
self.request_id = request_id
|
|
self.state = None # bound per step by the LoopRunner
|
|
|
|
def bind_state(self, state) -> None:
|
|
self.state = state
|
|
|
|
def execute(self, plan: WorkPlan) -> StepResult:
|
|
self.observers.emit("step_start", plan=plan)
|
|
# Interceptors may supply an override for the *expensive forward* (e.g. a cached
|
|
# prediction). The step body still runs the cheap scheduler step (combine + solver),
|
|
# so the override changes compute, not the loop's state-transition shape.
|
|
override = None
|
|
if self.interceptors.active and self.state is not None:
|
|
override = self.interceptors.before(plan, self.state)
|
|
# only count a skip the step body will actually honor (must carry a forward result)
|
|
if override is not None and "noise_pred" in override:
|
|
self.metrics["skipped_steps"] = self.metrics.get("skipped_steps", 0) + 1
|
|
|
|
def _eager(ov):
|
|
return plan.run(self.instance, ov) if plan.run is not None else {}
|
|
|
|
out = self._run_step(plan, override, _eager)
|
|
result = out if isinstance(out, StepResult) else StepResult(output=dict(out))
|
|
|
|
if self.interceptors.active and self.state is not None:
|
|
self.interceptors.after(plan, self.state, result)
|
|
|
|
self.observers.emit("step_complete", plan=plan, result=result)
|
|
self.metrics["steps"] = self.metrics.get("steps", 0) + 1
|
|
return result
|
|
|
|
def _run_step(self, plan: WorkPlan, override, eager):
|
|
"""Execute the step body, under piecewise CUDA-graph capture/replay when the loop declares
|
|
graph_capture="breakable_cudagraph". Otherwise plain eager — the unchanged path. Replay
|
|
still runs the current thunk (CPU models the lifecycle, not the speedup); see
|
|
runtime/cudagraph.py."""
|
|
if plan.run is None:
|
|
return {}
|
|
loop = self.instance.card.loops.get(plan.loop_id) if self.instance.card else None
|
|
if getattr(loop, "graph_capture", "eager") != "breakable_cudagraph":
|
|
return eager(override)
|
|
if self.instance.graphs is None:
|
|
from v2.runtime.cudagraph import GraphCapturer
|
|
self.instance.graphs = GraphCapturer()
|
|
return self.instance.graphs.dispatch(plan, self.instance, override, eager)
|
|
|
|
def emit(self, chunk: StreamChunk) -> None:
|
|
self.stream.emit(
|
|
OmniEvent(type="media.chunk", request_id=self.request_id, seq=chunk.seq, payload={"chunk": chunk}))
|
|
self.metrics["stream_chunks"] = self.metrics.get("stream_chunks", 0) + 1
|
|
|
|
def check_cancel(self) -> None:
|
|
if self.cancel_scope is not None:
|
|
self.cancel_scope.check() # raises request.Cancelled at the step boundary
|
|
|
|
def observe(self, event: str, **kw) -> None:
|
|
self.observers.emit(event, **kw)
|