Files
hao-ai-lab-FastVideo/v2/core/parallel/validation.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

60 lines
2.2 KiB
Python

"""Pre-flight parallelism validation.
Validate at load or fail, never halfway. Ownership conflicts are build errors, and
applicability conditions travel with axes (e.g. ``pp_patch`` is invalid for causal/AR).
All checks are CPU-testable on a fake mesh.
"""
from __future__ import annotations
from v2.core.enums import LoopKind
from v2.core.parallel.plan import AXIS_NAMES, ParallelPlan
class ParallelValidationError(ValueError):
pass
# loop kinds whose causality is broken by PipeFusion displaced-patch pipelining
_CAUSAL_KINDS = {LoopKind.CHUNK_ROLLOUT, LoopKind.AR_DECODE}
def validate_plan(plan: ParallelPlan,
card=None,
*,
world_size: int | None = None,
cfg_policy_batched: bool = False) -> ParallelPlan:
"""Validate a plan, optionally against a card. Returns the plan or raises.
Checks:
1. axis names are known; degrees >= 1
2. cfgp <= 2
3. product of degrees matches world_size (when given)
4. pp_patch invalid for any causal/AR loop on the card
5. ownership conflict: cfgp>1 AND a batched CFGPolicy is rejected
"""
errs: list[str] = []
for name, deg in plan.axes.items():
if name not in AXIS_NAMES:
errs.append(f"unknown parallel axis {name!r} (known: {AXIS_NAMES})")
if int(deg) < 1:
errs.append(f"axis {name!r} has degree {deg} < 1")
if plan.degree("cfgp") > 2:
errs.append(f"cfgp degree {plan.degree('cfgp')} > 2 (CFG has at most 2 branches)")
if world_size is not None and plan.world_size() != world_size:
errs.append(f"product of degrees {plan.world_size()} != world_size {world_size}")
if plan.degree("cfgp") > 1 and cfg_policy_batched:
errs.append("ownership conflict: a request owns a BatchedCFG policy OR a cfgp group, never both")
if card is not None and plan.degree("pp_patch") > 1:
causal = [lid for lid, lp in card.loops.items() if lp.kind in _CAUSAL_KINDS]
if causal:
errs.append(f"pp_patch is invalid for causal/AR loops {causal} (stale KV breaks causality, §8)")
if errs:
raise ParallelValidationError("ParallelPlan failed validation:\n - " + "\n - ".join(errs))
return plan