- add astrai/extension/dispatch.py: per-family decision tables over composable Specs with explicit-strict / implicit-loose resolution, ASTR_OPS env overrides, profile presets, and explain traces - make the axis schema family-owned: register_family takes an axes extractor that snapshots whatever decision axes that family needs from the call, and the core only supplies the axis() predicate vocabulary plus a tensor_axes helper - drop the central CallContext dataclass; resolve and explain take the raw call arguments, so unregistered handles are probed through supports_call on the same args - migrate attention and rotary onto family-owned axes with behavior-preserving specs and spec-vs-supports_call mirror tests - replace the non-ASCII member-of glyph in spec descriptions with plain ASCII " in "
86 lines
2.0 KiB
Python
86 lines
2.0 KiB
Python
"""CUDA attention kernel wrappers with torch fallback.
|
|
|
|
Public API:
|
|
- ``attn_decode`` — single-query decode attention
|
|
- ``attn_prefill`` — multi-query prefill attention
|
|
- ``attn_paged_decode`` — paged decode attention (direct page-table access)
|
|
- ``AttentionBackend`` — ABC for attention computation strategies
|
|
- ``TorchNativeBackend`` — default SDPA backend with KV cache I/O
|
|
- ``CudaBackend`` — CUDA kernel backend with paged decode + prefill
|
|
|
|
Layout convention: all q/k/v are ``[batch, seq_len, n_heads, head_dim]``
|
|
(blhd). Scale is always ``1/sqrt(head_dim)``.
|
|
|
|
Each wrapper calls its compiled CUDA kernel directly. Fallback to torch
|
|
SDPA is handled by the attention backend, not the wrapper functions.
|
|
"""
|
|
|
|
from astrai.extension.backend import (
|
|
ATTN_BACKEND,
|
|
AttentionBackend,
|
|
AttentionBackendFactory,
|
|
CudaBackend,
|
|
FlashAttnBackend,
|
|
TorchNativeBackend,
|
|
apply_rotary_emb,
|
|
attention,
|
|
attn_backend,
|
|
get_backend,
|
|
)
|
|
from astrai.extension.dispatch import (
|
|
Axes,
|
|
ExplicitSelectionError,
|
|
ImplRecord,
|
|
Resolution,
|
|
Spec,
|
|
axis,
|
|
explain,
|
|
explain_plan,
|
|
op_backend,
|
|
register_env_alias,
|
|
register_family,
|
|
resolve,
|
|
resolve_plan,
|
|
tensor_axes,
|
|
)
|
|
from astrai.extension.loader import KERNEL_NAMES, is_available
|
|
from astrai.extension.ops import (
|
|
TensorLayout,
|
|
attn_decode,
|
|
attn_paged_decode,
|
|
attn_prefill,
|
|
)
|
|
|
|
__all__ = [
|
|
"ATTN_BACKEND",
|
|
"AttentionBackend",
|
|
"AttentionBackendFactory",
|
|
"CudaBackend",
|
|
"TorchNativeBackend",
|
|
"FlashAttnBackend",
|
|
"TensorLayout",
|
|
"attention",
|
|
"attn_backend",
|
|
"get_backend",
|
|
"attn_decode",
|
|
"attn_paged_decode",
|
|
"attn_prefill",
|
|
"is_available",
|
|
"KERNEL_NAMES",
|
|
"apply_rotary_emb",
|
|
"Axes",
|
|
"ExplicitSelectionError",
|
|
"ImplRecord",
|
|
"Resolution",
|
|
"Spec",
|
|
"axis",
|
|
"explain",
|
|
"explain_plan",
|
|
"op_backend",
|
|
"register_env_alias",
|
|
"register_family",
|
|
"resolve",
|
|
"resolve_plan",
|
|
"tensor_axes",
|
|
]
|