Files
hao-ai-lab-FastVideo/v2_examples/inference/01_basic_t2v.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

52 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""01 — Basic text→video with Wan2.1-1.3B.
The minimal inference path on the v2 runtime:
build_default_engine() # registers the (recipe, runtime) cards
→ make_request(T2V, "wan2.1-1.3b", prompt, DiffusionParams(...)) # a typed Request
→ engine.run(req) # drives the diffusion_denoise loop to completion
→ out.artifacts["video"].frames # the decoded video tensor [C, T, H, W]
Run: python3 v2_examples/inference/01_basic_t2v.py
"""
from __future__ import annotations
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) # repo root → `import v2`
import numpy as np
from v2._vendor.models import build_default_engine
from v2.core.request import DiffusionParams, TaskType, make_request
MODEL = "wan2.1-1.3b"
def main() -> None:
eng = build_default_engine() # one resident instance per card
print("registered cards:", list(eng._registry))
req = make_request(
TaskType.T2V, MODEL, "a cat surfing a wave at sunset",
diffusion=DiffusionParams(num_steps=12, guidance_scale=5.0, seed=7))
out = eng.run(req) # synchronous, single request
video = np.asarray(out.artifacts["video"].frames) # [C, T, H, W]
latent = np.asarray(out.artifacts["latents"].latent) # the final denoised latent
print(f"prompt : {req.prompt()!r}")
print(f"video : shape={video.shape} dtype={video.dtype} "
f"range=[{video.min():.3f}, {video.max():.3f}]")
print(f"latent : shape={latent.shape}")
print(f"metrics : denoise_steps={out.metrics['denoise_steps']:.0f} "
f"gpu_seconds={out.metrics['gpu_seconds']:.2e}")
print("\nThe shapes and control flow are real; the pixels are a numpy toy. On GPU, swap "
"ComponentSpec.factory\nfor the torch Wan adapter — this script is unchanged.")
if __name__ == "__main__":
main()