[Model] Add DeepSeek-V4 CPU backend (#55355)
Signed-off-by: jiang1.li <jiang1.li@intel.com>
This commit is contained in:
@@ -828,10 +828,12 @@ class SparseAttnIndexer(CustomOp):
|
||||
return self.forward_cuda(hidden_states, q_quant, k, weights)
|
||||
elif current_platform.is_rocm():
|
||||
return self.forward_hip(hidden_states, q_quant, k, weights)
|
||||
elif current_platform.is_cpu():
|
||||
return self.forward_cpu(hidden_states, q_quant, k, weights)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"SparseAttnIndexer native forward is only implemented for "
|
||||
"CUDA, ROCm and XPU platforms."
|
||||
"CUDA, ROCm, XPU and CPU platforms."
|
||||
)
|
||||
|
||||
def forward_cuda(
|
||||
@@ -919,3 +921,196 @@ class SparseAttnIndexer(CustomOp):
|
||||
"Sparse attention indexer ROCm path is only supported on AITER. "
|
||||
"Please enable aiter with VLLM_ROCM_USE_AITER=1"
|
||||
)
|
||||
|
||||
def forward_cpu(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
q_quant: torch.Tensor | tuple[torch.Tensor, torch.Tensor],
|
||||
k: torch.Tensor | None,
|
||||
weights: torch.Tensor,
|
||||
):
|
||||
"""CPU sparse attention indexer: cache write stays eager Python glue
|
||||
(own K-cache layout, not shared with the main attention cache
|
||||
write). PREFILL and DECODE both call the ported
|
||||
``fp8_paged_mqa_logits_cpu``/``topk_transform_512_cpu`` kernels,
|
||||
which read the paged K-cache directly via ``page_table`` -- no
|
||||
eager gather step, no per-request Python loop.
|
||||
|
||||
``prefill_metadata.chunks`` always has exactly one entry here:
|
||||
``DeepseekV4CPUIndexerMetadataBuilder`` overrides the base chunk
|
||||
split to always return the whole step's prefill batch as one
|
||||
chunk, since the base chunking only bounds CUDA/XPU's dense M*N
|
||||
logits tensor and flat K-gather workspace, neither of which this
|
||||
paged kernel allocates.
|
||||
"""
|
||||
assert not self.use_fp4_cache, (
|
||||
"CPU sparse indexer doesn't support fp4 cache yet"
|
||||
)
|
||||
assert isinstance(q_quant, torch.Tensor), (
|
||||
"CPU sparse_attn_indexer expects a single FP8 q_quant tensor"
|
||||
)
|
||||
assert self.dcp_world_size <= 1 and not self.use_pcp, (
|
||||
"CPU sparse indexer doesn't support decode/prefill context parallelism yet."
|
||||
)
|
||||
|
||||
forward_context = get_forward_context()
|
||||
attn_metadata = forward_context.attn_metadata
|
||||
attn_metadata_narrowed: DeepseekV32IndexerMetadata | None = None
|
||||
if isinstance(attn_metadata, dict):
|
||||
metadata = attn_metadata[self.k_cache.prefix]
|
||||
assert isinstance(metadata, DeepseekV32IndexerMetadata)
|
||||
attn_metadata_narrowed = metadata
|
||||
if attn_metadata_narrowed is None:
|
||||
# Profiling/dummy run: no real metadata to act on.
|
||||
return self.topk_indices_buffer
|
||||
|
||||
kv_cache = self.k_cache.kv_cache
|
||||
topk_tokens = self.topk_tokens
|
||||
topk_indices_buffer = self.topk_indices_buffer
|
||||
slot_mapping = attn_metadata_narrowed.slot_mapping
|
||||
has_decode = attn_metadata_narrowed.num_decodes > 0
|
||||
has_prefill = attn_metadata_narrowed.num_prefills > 0
|
||||
num_decode_tokens = attn_metadata_narrowed.num_decode_tokens
|
||||
|
||||
num_tokens = slot_mapping.shape[0]
|
||||
if k is not None:
|
||||
k = k[:num_tokens]
|
||||
|
||||
if not self.skip_k_cache_insert:
|
||||
# Only reachable via DeepseekV32Attention with
|
||||
# prefill_context_parallel_size > 1 on CPU -- set_k_cpu/set_s_cpu
|
||||
# (the kernels this used to call) have been removed as unused/
|
||||
# untested (csrc/cpu/sgl-kernels/store_cache.cpp).
|
||||
raise NotImplementedError(
|
||||
"SparseAttnIndexer.forward_cpu: skip_k_cache_insert=False "
|
||||
"(prefill context parallel on CPU) is not supported."
|
||||
)
|
||||
|
||||
topk_indices_buffer[: hidden_states.shape[0]] = -1
|
||||
|
||||
if has_prefill:
|
||||
assert topk_tokens == 512, (
|
||||
"topk_transform_512_cpu only supports index_topk == 512."
|
||||
)
|
||||
prefill_metadata = attn_metadata_narrowed.prefill
|
||||
assert prefill_metadata is not None
|
||||
assert len(prefill_metadata.chunks) == 1, (
|
||||
"forward_cpu expects the prefill metadata builder to always "
|
||||
"produce a single chunk -- see "
|
||||
"DeepseekV4CPUIndexerMetadataBuilder._split_indexer_prefill_chunks."
|
||||
)
|
||||
chunk = prefill_metadata.chunks[0]
|
||||
# kv_cache is a per-layer view into vLLM's shared multi-layer
|
||||
# cache allocation, so its block stride generally exceeds
|
||||
# block_size * page_width; fp8_paged_mqa_logits_cpu reads
|
||||
# kv_view.stride(0) explicitly, so no copy is needed here.
|
||||
kv_view = kv_cache.view(kv_cache.shape[0], -1)
|
||||
block_size = kv_cache.shape[1]
|
||||
q_slice = q_quant[chunk.token_start : chunk.token_end]
|
||||
topk_indices = topk_indices_buffer[
|
||||
chunk.token_start : chunk.token_end, :topk_tokens
|
||||
]
|
||||
if chunk.local_total_seq_lens == 0:
|
||||
topk_indices.fill_(-1)
|
||||
else:
|
||||
assert chunk.local_cu_seq_lens is not None
|
||||
# Each token's own (per-request, DCP-local) causal length.
|
||||
local_seq_lens = chunk.cu_seqlen_ke - chunk.cu_seqlen_ks
|
||||
# Recover each token's owning request from its row-start
|
||||
# tag. Ties (from zero-length requests) are harmless: those
|
||||
# tokens have local length 0 and never dereference
|
||||
# page_table.
|
||||
req_idx = (
|
||||
torch.searchsorted(
|
||||
chunk.local_cu_seq_lens, chunk.cu_seqlen_ks, right=True
|
||||
)
|
||||
- 1
|
||||
)
|
||||
page_table = chunk.block_table[req_idx]
|
||||
# The true max over this chunk's own rows, NOT
|
||||
# chunk.max_local_total_seq_lens (that field sums every
|
||||
# request's length in the chunk, bounding the old
|
||||
# flat-gather buffer this paged path no longer allocates).
|
||||
max_seq_len = int(local_seq_lens.max().item())
|
||||
|
||||
logits = ops.fp8_paged_mqa_logits_cpu(
|
||||
q_slice,
|
||||
kv_view,
|
||||
weights[chunk.token_start : chunk.token_end],
|
||||
local_seq_lens,
|
||||
page_table,
|
||||
block_size,
|
||||
max_seq_len,
|
||||
)
|
||||
out_page_scratch = torch.empty(
|
||||
(q_slice.shape[0], topk_tokens),
|
||||
dtype=torch.int32,
|
||||
device=kv_cache.device,
|
||||
)
|
||||
ops.topk_transform_512_cpu(
|
||||
logits,
|
||||
local_seq_lens,
|
||||
page_table,
|
||||
out_page_scratch,
|
||||
block_size,
|
||||
topk_indices,
|
||||
)
|
||||
|
||||
if has_decode:
|
||||
decode_metadata = attn_metadata_narrowed.decode
|
||||
assert decode_metadata is not None
|
||||
assert not decode_metadata.requires_padding, (
|
||||
"CPU sparse indexer decode path does not support speculative "
|
||||
"decoding (native MTP) yet."
|
||||
)
|
||||
batch_size = decode_metadata.decode_lens.shape[0]
|
||||
if batch_size > 0:
|
||||
# No native MTP on CPU (asserted above) => exactly one
|
||||
# query token per decode request, so the flat slice below
|
||||
# is already the batch-major layout
|
||||
# fp8_paged_mqa_logits_cpu wants.
|
||||
assert num_decode_tokens == batch_size, (
|
||||
"CPU sparse indexer decode path expects exactly one query "
|
||||
"token per decode request."
|
||||
)
|
||||
assert topk_tokens == 512, (
|
||||
"topk_transform_512_cpu only supports index_topk == 512."
|
||||
)
|
||||
seq_lens = decode_metadata.seq_lens
|
||||
seq_lens = (
|
||||
seq_lens[:, -1].contiguous() if seq_lens.ndim == 2 else seq_lens
|
||||
)
|
||||
block_table = decode_metadata.block_table[:batch_size]
|
||||
block_size = kv_cache.shape[1]
|
||||
kv_view = kv_cache.view(kv_cache.shape[0], -1)
|
||||
|
||||
logits = ops.fp8_paged_mqa_logits_cpu(
|
||||
q_quant[:num_decode_tokens],
|
||||
kv_view,
|
||||
weights[:num_decode_tokens],
|
||||
seq_lens,
|
||||
block_table,
|
||||
block_size,
|
||||
attn_metadata_narrowed.max_seq_len,
|
||||
)
|
||||
|
||||
# out_page_indices is a required kernel output but unused:
|
||||
# the indexer's topk output must stay local/compressed-
|
||||
# context positions (resolved later by
|
||||
# DeepseekV4CPUAttention.forward_mqa via
|
||||
# map_local_to_global_slots_cpu).
|
||||
out_page_scratch = torch.empty(
|
||||
(batch_size, topk_tokens),
|
||||
dtype=torch.int32,
|
||||
device=kv_cache.device,
|
||||
)
|
||||
ops.topk_transform_512_cpu(
|
||||
logits,
|
||||
seq_lens,
|
||||
block_table,
|
||||
out_page_scratch,
|
||||
block_size,
|
||||
topk_indices_buffer[:num_decode_tokens, :topk_tokens],
|
||||
)
|
||||
|
||||
return topk_indices_buffer
|
||||
|
||||
Reference in New Issue
Block a user