Files
hao-ai-lab-FastVideo/v2/core/loop/driver.py
T
Will Lin 1fe50c0092 [refactor] v2: group flat top-level into planes; isolate vendored under _vendor/
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`.
2026-07-04 17:15:34 +00:00

69 lines
2.4 KiB
Python

"""LoopRunner — the only place iteration lives.
The runtime's driver, factored so the engine can either run it to completion
(``run``) or advance it one step at a time (``peek`` + ``step``) to **interleave** the steps
of concurrent requests. Per-request state is entirely in ``LoopState``; the runner holds no
hidden iteration state beyond a cached pending plan, so interleaving is safe by construction.
"""
from __future__ import annotations
from typing import Any
from v2.core.loop.contracts import Done, LoopContext, LoopResult, LoopState, WorkPlan
class LoopRunner:
def __init__(self, loop: Any, ctx: LoopContext, request: Any, model: Any):
self.loop = loop
self.ctx = ctx
self.state: LoopState = loop.init(request, model, ctx)
self._done = False
self._result: LoopResult | None = None
self._pending: WorkPlan | None = None
@property
def done(self) -> bool:
return self._done
@property
def result(self) -> LoopResult | None:
return self._result
def peek(self) -> WorkPlan | None:
"""Compute (and cache) the next WorkPlan. ``next`` is kernel-free, so this is cheap.
Returns None when the loop is finished (and runs ``finalize`` exactly once)."""
if self._done:
return None
if self._pending is None:
self.ctx.check_cancel()
nxt = self.loop.next(self.state)
if isinstance(nxt, Done):
self._result = self.loop.finalize(self.state)
self._done = True
return None
self._pending = nxt
return self._pending
def step(self) -> bool:
"""Execute the pending plan (the inversion point) and fold the result in.
Returns True when the loop has just finished."""
plan = self.peek()
if self._done or plan is None:
return True
# the engine binds the current state onto ctx so interceptors see per-request state
if hasattr(self.ctx, "bind_state"):
self.ctx.bind_state(self.state)
result = self.ctx.execute(plan)
for chunk in plan.emits:
self.ctx.emit(chunk)
self.state = self.loop.advance(self.state, result)
self._pending = None
return self._done
def run(self) -> LoopResult:
while not self._done:
self.step()
assert self._result is not None
return self._result