Kernels:
Trusted publisher
File size: 7,730 Bytes
e19323e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | # Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# For a list of all contributors, visit:
# https://github.com/fla-org/flash-linear-attention/graphs/contributors
"""Generic backend dispatch system for FLA operations."""
from __future__ import annotations
import contextlib
import logging
import os
import threading
from collections.abc import Callable
from functools import cache, wraps
from typing import Any, ClassVar, TypeVar
import torch
from ...utils import find_spec_cached
logger = logging.getLogger(__name__)
F = TypeVar('F', bound=Callable)
_DISPATCH_DISABLED = os.environ.get("FLA_DISABLE_BACKEND_DISPATCH") == "1"
if _DISPATCH_DISABLED:
logger.info("[FLA Backend] FLA_DISABLE_BACKEND_DISPATCH=1 — all dispatch bypassed")
class BaseBackend:
"""Base class for operation-specific backends.
Attributes:
backend_type (str, Optional):
Identifier for the backend type, used to distinguish different backend implementations.
Default: `"base"`.
package_name (str, Optional):
Name of the external package required by the backend.
`None` indicates no external dependency. Default: `None`.
env_var (str, Optional):
Environment variable name that controls whether the backend is enabled.
`None` means always enabled. Default: `None`.
default_enable (bool, Optional):
Whether the backend is enabled by default when `env_var` is not set.
Set to `False` to require explicit user opt-in. Default: `True`.
priority (int, Optional):
Backend priority. Lower values indicate higher priority. Default: 5.
"""
backend_type: ClassVar[str] = "base"
package_name: ClassVar[str | None] = None
env_var: ClassVar[str | None] = None
default_enable: ClassVar[bool] = True
# Lower number = higher priority, default is 5
priority: ClassVar[int] = 5
@classmethod
def is_available(cls) -> bool:
if cls.package_name is None:
return True
return find_spec_cached(cls.package_name) is not None
@classmethod
def is_enabled(cls) -> bool:
if cls.env_var is None:
return True
default_value = "1" if cls.default_enable else "0"
return os.environ.get(cls.env_var, default_value) != "0"
@classmethod
@cache
def can_use(cls) -> bool:
return cls.is_available() and cls.is_enabled()
def verify(self, func_name: str, *args, **kwargs) -> tuple[bool, str | None]:
"""Check if backend can handle the function call."""
verifier_name = f"{func_name}_verifier"
verifier = getattr(self, verifier_name, None)
if verifier is None:
return True, None
try:
return verifier(*args, **kwargs)
except Exception as e:
return False, str(e)
_OPERATION_BACKEND_MODULES: dict[str, str] = {
'modules': 'fla.modules.backends',
}
class BackendRegistry:
"""Per-operation backend registry."""
_registries: ClassVar[dict[str, BackendRegistry]] = {}
_initialized: ClassVar[set[str]] = set()
_init_lock: ClassVar[threading.Lock] = threading.Lock()
def __init__(self, operation_name: str):
self.operation_name = operation_name
self._backends: dict[str, BaseBackend] = {}
self._active: BaseBackend | None = None
self._lock = threading.RLock()
self._logged: set[str] = set()
BackendRegistry._registries[operation_name] = self
def register(self, backend: BaseBackend) -> None:
"""Register a backend."""
with self._lock:
self._backends[backend.backend_type] = backend
# Update active backend based on priority
self._update_active_backend()
def _get_sorted_backends(self) -> list[BaseBackend]:
"""Get backends sorted by priority (lower number = higher priority).
Backends with the same priority are sorted by registration order.
"""
return sorted(
self._backends.values(),
key=lambda b: (b.priority, list(self._backends.values()).index(b))
)
def _update_active_backend(self) -> None:
"""Update active backend based on priority."""
for backend in self._get_sorted_backends():
if backend.can_use():
self._active = backend
return
def get_active(self) -> BaseBackend | None:
"""Get active backend."""
return self._active
@classmethod
def ensure_initialized(cls, operation: str) -> None:
"""Lazy-load backends on first use."""
if operation in cls._initialized:
return
with cls._init_lock:
if operation in cls._initialized:
return
# Import backend module to trigger registration
module_path = _OPERATION_BACKEND_MODULES.get(
operation,
f'fla.ops.{operation}.backends',
)
with contextlib.suppress(ImportError):
__import__(module_path, fromlist=[''])
cls._initialized.add(operation)
def dispatch(operation: str):
"""Dispatch decorator with verifier support.
Iterates through all registered backends and selects the first one
that passes the verifier for the given function call.
"""
def decorator(func: F) -> F:
if _DISPATCH_DISABLED:
return func
func_name = func.__name__
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
# Lazy initialization of backends
BackendRegistry.ensure_initialized(operation)
registry = BackendRegistry._registries.get(operation)
if registry is None:
return func(*args, **kwargs)
# Iterate through all registered backends sorted by priority
# to find one that can handle this call
backends_list = registry._get_sorted_backends()
for be in backends_list:
# Avoid be.can_use(): its @cache wrapper breaks torch.compile tracing.
if not (be.is_available() and be.is_enabled()):
continue
can_use, reason = be.verify(func_name, *args, **kwargs)
if not can_use:
fail_key = f"{operation}:{func_name}:{be.backend_type}:fail"
if fail_key not in registry._logged:
registry._logged.add(fail_key)
logger.info(
f"[FLA Backend] {operation}.{func_name} -> {be.backend_type} "
f"rejected: {reason}"
)
continue
impl = getattr(be, func_name, None)
if impl is None:
continue
result = impl(*args, **kwargs)
log_key = f"{operation}:{func_name}:{be.backend_type}"
if log_key not in registry._logged:
registry._logged.add(log_key)
logger.info(f"[FLA Backend] {operation}.{func_name} -> {be.backend_type}")
return result
# No backend can handle this call, use default implementation
return func(*args, **kwargs)
# Dispatch performs runtime backend selection; keep it out of torch.compile graphs.
wrapper = torch.compiler.disable(wrapper)
return wrapper
return decorator
__all__ = ['BackendRegistry', 'BaseBackend', 'dispatch']
|