Add v2/platform/: the (recipe, runtime) backend membrane that lets CPU, GPU, and other devices coexist behind one dispatch substrate. - Two tuple-keyed registries: COMPONENTS(kind, device, variant) for weight-bearing components and KERNELS(op, device, arch, variant) for stateless primitives, each with an availability predicate and an enumerable manifest (declared-but-unavailable cells listed without importing torch). - Platform: detected (device, arch) owning the device + arch fallback chains and a per-platform cached KernelTable; detect() is honest (CPU/numpy unless torch+CUDA are actually present). Arch fallback is monotonic — only degrades to older/portable archs, never a newer binary-incompatible one. - Seams wired: ModelInstance.component() -> platform.build_component(spec,self) with spec.factory as the numpy terminal rung (existing cards untouched); the wan21 denoise thunk dispatches solver ops through model.platform.kernels. - Three backends: cpu (numpy terminal + parity oracle), accel (pure-python stand-in proving cross-device resolution, arch fallback, and the oracle), torch_cuda (declared-but-unavailable; no faked GPU). - test_platform.py: 16 tests — detection, terminal rung, arch-fallback walk + monotonicity, device precedence, variant fallback, component override + per-kind device fallback, the parity oracle (accel == cpu, bit_identical via the C1 ladder), and matrix enumeration without importing torch. Scope is honestly bounded in the README/docstrings: only wan21-denoise routes through the kernel table and only the dit kind is overridden today (each a one-line adoption); the torch/CUDA path and a cudagraph workspace-safety contract are declared/deferred, not implemented. 183 tests pass.
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
"""The multi-backend dispatch substrate (design_v3 §17).
|
|
|
|
Two tuple-keyed registries (``COMPONENTS``, ``KERNELS``) + a ``Platform`` that detects ``(device,
|
|
arch)`` and resolves both — with the numpy reference as the terminal fallback rung and parity
|
|
oracle. This package is the membrane that lets CPU, GPU, and other backends coexist: a model's
|
|
loops, policies, scheduler, caches, and training code never name a device; they call through the
|
|
component/kernel seams and the resolved ``Platform`` decides the implementation.
|
|
|
|
Importing this package is cheap and cycle-free (registry + platform classes only). The concrete
|
|
backend registrations in ``backends/`` are imported lazily on first ``Platform`` use.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from .platform import (
|
|
KernelTable,
|
|
Platform,
|
|
component_matrix,
|
|
ensure_backends_loaded,
|
|
kernel_matrix,
|
|
)
|
|
from .registry import (
|
|
COMPONENTS,
|
|
FLOW_MATCH_STEP,
|
|
FLOW_SDE_STEP,
|
|
KERNELS,
|
|
register_component,
|
|
register_kernel,
|
|
)
|
|
|
|
__all__ = [
|
|
"Platform",
|
|
"KernelTable",
|
|
"COMPONENTS",
|
|
"KERNELS",
|
|
"register_component",
|
|
"register_kernel",
|
|
"ensure_backends_loaded",
|
|
"kernel_matrix",
|
|
"component_matrix",
|
|
"FLOW_MATCH_STEP",
|
|
"FLOW_SDE_STEP",
|
|
]
|