[Feat] CPU fp8 attn for AMX/AVX-512 (#39445)

Signed-off-by: Li, Tianmu <tianmu.li@intel.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Li, Jiang <jiang1.li@intel.com>
This commit is contained in:
Tianmu Li
2026-04-29 20:43:21 +08:00
committed by GitHub
co-authored by Claude Li, Jiang <jiang1.li@intel.com>
parent 9d8ad5b408
commit 22524f7a92
19 changed files with 1067 additions and 283 deletions
+12
View File
@@ -3403,6 +3403,9 @@ def cpu_attn_reshape_and_cache(
value_cache: torch.Tensor,
slot_mapping: torch.Tensor,
isa: str,
k_scale: float = 1.0,
v_scale: float = 1.0,
kv_cache_dtype: str = "auto",
) -> None:
torch.ops._C.cpu_attn_reshape_and_cache(
key,
@@ -3411,6 +3414,9 @@ def cpu_attn_reshape_and_cache(
value_cache,
slot_mapping,
isa,
k_scale,
v_scale,
kv_cache_dtype,
)
@@ -3429,6 +3435,9 @@ def cpu_attention_with_kv_cache(
softcap: float,
scheduler_metadata: torch.Tensor,
s_aux: torch.Tensor | None,
k_scale: float = 1.0,
v_scale: float = 1.0,
kv_cache_dtype: str = "auto",
) -> None:
torch.ops._C.cpu_attention_with_kv_cache(
query,
@@ -3446,6 +3455,9 @@ def cpu_attention_with_kv_cache(
softcap,
scheduler_metadata,
s_aux,
k_scale,
v_scale,
kv_cache_dtype,
)
+4 -15
View File
@@ -16,7 +16,6 @@ from vllm.utils.cpu_resource_utils import (
get_memory_node_info,
)
from vllm.utils.mem_constants import GiB_bytes
from vllm.utils.torch_utils import is_quantized_kv_cache
from vllm.v1.attention.backends.registry import AttentionBackendEnum
from .interface import CpuArchEnum, Platform, PlatformEnum
@@ -134,20 +133,6 @@ class CpuPlatform(Platform):
scheduler_config = vllm_config.scheduler_config
# async scheduling is not required on CPU
scheduler_config.async_scheduling = False
if (
scheduler_config.enable_chunked_prefill
or cache_config.enable_prefix_caching
) and is_quantized_kv_cache(cache_config.cache_dtype):
raise RuntimeError(
"Chunked-prefill and prefix-cache on the CPU "
"backend is not compatible with FP8 KV cache."
)
if is_quantized_kv_cache(cache_config.cache_dtype):
logger.warning(
"CPU backend doesn't support KV cache quantization fallback to auto."
)
cache_config.cache_dtype = "auto"
parallel_config = vllm_config.parallel_config
# OMP requires the MP executor to function correctly, UniProc is not
@@ -458,6 +443,10 @@ class CpuPlatform(Platform):
block_offsets.reshape(1, block_size)
+ indices.reshape(num_blocks, 1) * block_size
).flatten()
if key_cache.dtype == torch.uint8:
raise NotImplementedError(
"FP8 KV cache is not yet supported with KV transfer on CPU"
)
cpu_attn_reshape_and_cache(
key,
value,
+38 -5
View File
@@ -1,7 +1,10 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from dataclasses import dataclass
from typing import ClassVar
from typing import TYPE_CHECKING, ClassVar
if TYPE_CHECKING:
from vllm.config.cache import CacheDType
import torch
@@ -35,6 +38,12 @@ class CPUAttentionBackend(AttentionBackend):
torch.bfloat16,
torch.float32,
]
supported_kv_cache_dtypes: ClassVar[list["CacheDType"]] = [
"auto",
"fp8",
"fp8_e4m3",
"fp8_e5m2",
]
@classmethod
def get_supported_head_sizes(cls) -> list[int]:
@@ -133,7 +142,13 @@ class CPUAttentionMetadataBuilder(AttentionMetadataBuilder[CPUAttentionMetadata]
if self.window_size is None:
self.window_size = -1
self.block_size = vllm_config.cache_config.block_size
self.isa = _get_attn_isa(self.dtype, self.block_size, self.head_dim)
kv_cache_dtype_str = vllm_config.cache_config.cache_dtype
self.isa = _get_attn_isa(
self.dtype,
self.block_size,
self.head_dim,
kv_cache_dtype_str,
)
self.is_cross_attention = isinstance(kv_cache_spec, CrossAttentionSpec)
def build(
@@ -247,8 +262,7 @@ class CPUAttentionBackendImpl(AttentionImpl):
self.kv_cache_dtype = kv_cache_dtype
self.num_queries_per_kv = self.num_heads // self.num_kv_heads
if is_quantized_kv_cache(kv_cache_dtype):
raise NotImplementedError("FP8 KV cache is unsupported in CPU_ATTN")
self.is_fp8_kv_cache = is_quantized_kv_cache(kv_cache_dtype)
self.attn_type = attn_type
self.sinks = sinks
@@ -325,6 +339,9 @@ class CPUAttentionBackendImpl(AttentionImpl):
value_cache,
attn_metadata.slot_mapping,
attn_metadata.isa,
k_scale=layer._k_scale_float,
v_scale=layer._v_scale_float,
kv_cache_dtype=self.kv_cache_dtype,
)
if attn_metadata.use_sdpa_prefill:
@@ -356,6 +373,9 @@ class CPUAttentionBackendImpl(AttentionImpl):
softcap=self.logits_soft_cap,
scheduler_metadata=attn_metadata.scheduler_metadata,
s_aux=self.sinks,
k_scale=layer._k_scale_float,
v_scale=layer._v_scale_float,
kv_cache_dtype=self.kv_cache_dtype,
)
return output
@@ -477,13 +497,26 @@ def _make_sliding_window_bias(
def _get_attn_isa(
dtype: torch.dtype, block_size: int, head_size: int | None = None
dtype: torch.dtype,
block_size: int,
head_size: int | None = None,
kv_cache_dtype: str | None = None,
) -> str:
fp8_kv = is_quantized_kv_cache(kv_cache_dtype) if kv_cache_dtype else False
if head_size is not None and head_size % 32 != 0 and head_size % 16 == 0:
if fp8_kv:
raise NotImplementedError(
"FP8 KV cache requires head_size divisible by 32 on CPU."
)
return "vec16"
supports_amx = torch.cpu._is_amx_tile_supported()
supports_arm = current_platform.get_cpu_architecture() == CpuArchEnum.ARM
supports_vxe = current_platform.get_cpu_architecture() == CpuArchEnum.S390X
supports_avx512 = torch.cpu._is_avx512_supported()
if fp8_kv and not supports_amx and not supports_avx512:
raise NotImplementedError(
"FP8 KV cache on CPU requires x86 with AVX-512 or AMX."
)
if supports_amx and dtype in (torch.bfloat16,) and block_size % 32 == 0:
return "amx"
elif block_size % 32 == 0: