[Kernel] Add B12X dense linear backends (#52016)
Signed-off-by: Luke Alonso <lalonso@gmail.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Lazy accessors for the optional ``b12x`` package."""
|
||||
|
||||
import functools
|
||||
import importlib
|
||||
import importlib.util
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import fields, is_dataclass
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
@functools.cache
|
||||
def has_b12x() -> bool:
|
||||
"""Return whether the B12X package is installed."""
|
||||
return importlib.util.find_spec("b12x") is not None
|
||||
|
||||
|
||||
@functools.cache
|
||||
def _get_submodule(module_name: str) -> ModuleType | None:
|
||||
if not has_b12x():
|
||||
return None
|
||||
try:
|
||||
return importlib.import_module(module_name)
|
||||
except (ImportError, ModuleNotFoundError):
|
||||
return None
|
||||
|
||||
|
||||
def get_b12x_blockscaled() -> ModuleType | None:
|
||||
return _get_submodule("b12x.gemm.blockscaled")
|
||||
|
||||
|
||||
def get_b12x_intrinsics() -> ModuleType | None:
|
||||
return _get_submodule("b12x._lib.intrinsics")
|
||||
|
||||
|
||||
def get_b12x_mxfp8_linear() -> ModuleType | None:
|
||||
return _get_submodule("b12x.gemm.mxfp8_linear")
|
||||
|
||||
|
||||
def get_b12x_tensor_fp8_linear() -> ModuleType | None:
|
||||
return _get_submodule("b12x.gemm.tensor_fp8_linear")
|
||||
|
||||
|
||||
def b12x_warmup_token_counts(
|
||||
*,
|
||||
max_tokens: int,
|
||||
cudagraph_capture_sizes: Iterable[int] = (),
|
||||
) -> tuple[int, ...]:
|
||||
# B12X deduplicates shapes that select the same internal kernel policy.
|
||||
# Keep the complete serving shape set here rather than duplicating its
|
||||
# policy-selection heuristics in vLLM.
|
||||
counts = {1}
|
||||
counts.update(int(size) for size in cudagraph_capture_sizes if int(size) > 0)
|
||||
if int(max_tokens) > 0:
|
||||
counts.add(int(max_tokens))
|
||||
return tuple(sorted(counts))
|
||||
|
||||
|
||||
def _same_packed_layout(current: Any, replacement: Any) -> bool:
|
||||
if type(current) is not type(replacement):
|
||||
return False
|
||||
if isinstance(current, torch.Tensor):
|
||||
return (
|
||||
current.shape == replacement.shape
|
||||
and current.stride() == replacement.stride()
|
||||
and current.dtype == replacement.dtype
|
||||
and current.device == replacement.device
|
||||
)
|
||||
if is_dataclass(current):
|
||||
return all(
|
||||
_same_packed_layout(
|
||||
getattr(current, field.name),
|
||||
getattr(replacement, field.name),
|
||||
)
|
||||
for field in fields(current)
|
||||
)
|
||||
return bool(current == replacement)
|
||||
|
||||
|
||||
def _copy_packed_tensors(current: Any, replacement: Any) -> None:
|
||||
if isinstance(current, torch.Tensor):
|
||||
current.copy_(replacement)
|
||||
elif is_dataclass(current):
|
||||
for field in fields(current):
|
||||
_copy_packed_tensors(
|
||||
getattr(current, field.name),
|
||||
getattr(replacement, field.name),
|
||||
)
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def reuse_packed_weight_storage(current: Any, replacement: Any) -> Any:
|
||||
"""Reuse packed tensor addresses when a compatible weight is reloaded."""
|
||||
if current is None or not _same_packed_layout(current, replacement):
|
||||
return replacement
|
||||
_copy_packed_tensors(current, replacement)
|
||||
return current
|
||||
Reference in New Issue
Block a user