Merge remote-tracking branch 'upstream/main' into patches

This commit is contained in:
2026-10-05 08:52:31 +07:00
85 changed files with 3146 additions and 769 deletions
@@ -166,8 +166,7 @@ def xpu_sparse_decode_fp8(
num_tokens = q.shape[0]
device = q.device
# Determine max topk and swa widths
if not swa_only and topk_indices is not None:
if not swa_only and topk_indices is not None and kv_cache is not None:
topk_idx_2d = (
topk_indices.squeeze(1) if topk_indices.dim() == 3 else topk_indices
)
@@ -176,109 +175,57 @@ def xpu_sparse_decode_fp8(
topk_idx_2d = None
max_topk = 0
use_topk = topk_idx_2d is not None
swa_idx_2d = swa_indices.squeeze(1) if swa_indices.dim() == 3 else swa_indices
max_swa = swa_idx_2d.shape[1]
K_total = max_topk + max_swa
# Allocate flat workspace: [num_tokens * K_total, 512] bf16
# Topk rows precede SWA rows in the flat workspace.
n_topk_rows = num_tokens * max_topk
workspace = torch.empty(
(num_tokens * K_total, OUTPUT_DIM), dtype=torch.bfloat16, device=device
)
ws_3d = workspace.view(num_tokens, K_total, OUTPUT_DIM)
# Dequant+gather topk slots from compressed cache
if not swa_only and topk_idx_2d is not None and kv_cache is not None:
topk_flat = topk_idx_2d.reshape(-1).to(torch.int32)
topk_buf = torch.empty(
(num_tokens * max_topk, OUTPUT_DIM), dtype=torch.bfloat16, device=device
if topk_idx_2d is not None and kv_cache is not None and n_topk_rows > 0:
dequant_gather_slots(
workspace[:n_topk_rows],
kv_cache,
topk_idx_2d.reshape(-1).to(torch.int32),
kv_cache.shape[1],
)
compressed_block_size = kv_cache.shape[1]
dequant_gather_slots(topk_buf, kv_cache, topk_flat, compressed_block_size)
ws_3d[:, :max_topk, :] = topk_buf.view(num_tokens, max_topk, OUTPUT_DIM)
# Dequant+gather SWA slots
swa_flat = swa_idx_2d.reshape(-1).to(torch.int32)
swa_buf = torch.empty(
(num_tokens * max_swa, OUTPUT_DIM), dtype=torch.bfloat16, device=device
dequant_gather_slots(
workspace[n_topk_rows:],
swa_kv_cache,
swa_idx_2d.reshape(-1).to(torch.int32),
swa_kv_cache.shape[1],
)
swa_block_size = swa_kv_cache.shape[1]
dequant_gather_slots(swa_buf, swa_kv_cache, swa_flat, swa_block_size)
ws_3d[:, max_topk:, :] = swa_buf.view(num_tokens, max_swa, OUTPUT_DIM)
# Build combined indices into the flat workspace and combined lengths.
# Workspace layout per token t: [topk_0..topk_{max_topk-1}, swa_0..swa_{max_swa-1}]
# Flat index for token t, position p = t * K_total + p
#
# IMPORTANT: The attention kernel uses combined_lens as a position cutoff —
# it only reads indices[0..combined_lens-1]. So indices must be PACKED
# contiguously: [valid_topk_indices..., valid_swa_indices..., -1 padding...]
if not swa_only and topk_lens is not None:
combined_lens = (topk_lens + swa_lens).to(torch.int32)
else:
combined_lens = swa_lens.to(torch.int32)
max_combined = int(combined_lens.max().item()) if combined_lens.numel() > 0 else 0
# Round up to BLOCK_N=16 alignment for kernel efficiency
# Use a shape-derived width so replay accepts changing lens without host sync.
_BLOCK_N = 16
max_combined_padded = ((max_combined + _BLOCK_N - 1) // _BLOCK_N) * _BLOCK_N
width = max(_BLOCK_N, ((K_total + _BLOCK_N - 1) // _BLOCK_N) * _BLOCK_N)
# Build packed index table: [num_tokens, max_combined_padded]
# Each token t: [topk_0..topk_{tlen-1}, swa_0..swa_{slen-1}, -1 padding]
# Vectorized: for each token, topk indices are t*K_total + 0..tlen-1,
# swa indices are t*K_total + max_topk + 0..slen-1
combined_indices = torch.full(
(num_tokens, max_combined_padded),
fill_value=-1,
dtype=torch.int32,
device=device,
pos = torch.arange(width, device=device, dtype=torch.int32).unsqueeze(0)
tok = torch.arange(num_tokens, device=device, dtype=torch.int32).unsqueeze(1)
slen = swa_lens.reshape(-1, 1).to(torch.int32).clamp(min=0, max=max_swa)
if use_topk and topk_lens is not None:
tlen = topk_lens.reshape(-1, 1).to(torch.int32).clamp(min=0, max=max_topk)
else:
tlen = torch.zeros_like(slen)
in_topk = pos < tlen
in_swa = (pos >= tlen) & (pos < tlen + slen)
topk_ws = tok * max_topk + pos
swa_ws = n_topk_rows + tok * max_swa + (pos - tlen)
combined_indices = torch.where(
in_topk,
topk_ws,
torch.where(in_swa, swa_ws, torch.full_like(topk_ws, -1)),
)
token_offsets = (
torch.arange(num_tokens, device=device, dtype=torch.int32) * K_total
) # [B]
if not swa_only and topk_lens is not None:
# Pack topk: for each token, write t*K_total + 0..tlen-1 at positions 0..tlen-1
max_tlen = int(topk_lens.max().item())
topk_range = torch.arange(max_tlen, device=device, dtype=torch.int32).unsqueeze(
0
)
topk_valid = topk_range < topk_lens.unsqueeze(1)
topk_ws_indices = token_offsets.unsqueeze(1) + topk_range
combined_indices[:, :max_tlen] = torch.where(
topk_valid,
topk_ws_indices,
torch.tensor(-1, dtype=torch.int32, device=device),
)
# Pack swa after topk: positions tlen..tlen+slen-1
# Since tlen varies per token, we need per-token offset
swa_range = torch.arange(max_swa, device=device, dtype=torch.int32).unsqueeze(0)
swa_valid = swa_range < swa_lens.unsqueeze(1)
swa_ws_indices = token_offsets.unsqueeze(1) + max_topk + swa_range
# Write at position topk_lens[t] + swa_pos for each token
for t_idx in range(num_tokens):
tlen = int(topk_lens[t_idx].item())
slen = int(swa_lens[t_idx].item())
combined_indices[t_idx, tlen : tlen + slen] = swa_ws_indices[t_idx, :slen]
else:
# SWA-only: pack swa indices at positions 0..slen-1
# Use min(max_swa, max_combined_padded) because combined_indices only
# has max_combined_padded columns, and all valid entries fit within it.
effective_swa = min(max_swa, max_combined_padded)
swa_range = torch.arange(
effective_swa, device=device, dtype=torch.int32
).unsqueeze(0)
swa_valid = swa_range < swa_lens.unsqueeze(1)
swa_ws_indices = token_offsets.unsqueeze(1) + swa_range # max_topk=0
combined_indices[:, :effective_swa] = torch.where(
swa_valid,
swa_ws_indices,
torch.tensor(-1, dtype=torch.int32, device=device),
)
# Call BF16 sparse MLA kernel
out_attn, _, _ = triton_bf16_mla_sparse_interface(
q=q,
kv=workspace.unsqueeze(1),
+5 -1
View File
@@ -75,8 +75,12 @@ def _ring_slot_mapping_kernel(
block = tl.load(block_table_ptr + req * block_table_stride, mask=valid, other=0)
pos = tl.load(positions_ptr + offsets, mask=valid, other=0)
slot = block.to(tl.int64) * CAPACITY + pos % CAPACITY
# Block 0 is the null block. A request without a ring block (dummy or padding
# rows, which the runners fill with the null block) must not write.
tl.store(
slot_mapping_ptr + offsets, tl.where(valid, slot, -1), mask=offsets < num_tokens
slot_mapping_ptr + offsets,
tl.where(valid & (block != 0), slot, -1),
mask=offsets < num_tokens,
)
+7 -1
View File
@@ -47,7 +47,13 @@ from vllm.v1.attention.selector import get_attn_backend
logger = init_logger(__name__)
_SPARSE_LAYER_TYPES = ("sparse_attention", "sparse", "deepseek_sparse_attention")
_SPARSE_LAYER_TYPES = (
"sparse_attention",
"sparse",
"indexed_attention",
# TODO: Delete below once Transformers 5.18.0 is the minimum required version.
"deepseek_sparse_attention",
)
_WEIGHT_LAYER_INDEX_RE = re.compile(r"(?:^|\.)layers\.(\d+)(?:\.|$)")
+7 -3
View File
@@ -80,6 +80,10 @@ from .low_latency_gemm import enable_qwen4_exp_low_latency_gemm
from .ple_layer import Qwen4ExpPLELayer
from .qsa import Qwen4ExpQSAAttention
# Transformers v5.18 renamed `qwen_sparse_attention` to `indexed_attention`
# TODO: Delete qwen_... once Transformers 5.18.0 is the minimum required version.
_QSA_LAYER_TYPES = ("qwen_sparse_attention", "indexed_attention")
def without_modelopt_fp4(
quant_config: QuantizationConfig | None,
@@ -208,7 +212,7 @@ class Qwen4ExpDecoderLayer(nn.Module):
prefix=f"{prefix}.linear_attn",
gqa_interleaved_layout=False,
)
elif layer_type == "qwen_sparse_attention":
elif layer_type in _QSA_LAYER_TYPES:
use_qsa = getattr(config, "indexer_n_heads", None) is not None
if not use_qsa:
self.self_attn = Qwen3NextAttention(
@@ -290,7 +294,7 @@ class Qwen4ExpDecoderLayer(nn.Module):
if self.layer_type == "linear_attention":
attn_out = self.linear_attn(hidden_states=block_input)
elif self.layer_type == "qwen_sparse_attention":
elif self.layer_type in _QSA_LAYER_TYPES:
attn_out = self.self_attn(
hidden_states=block_input,
positions=positions,
@@ -381,7 +385,7 @@ class Qwen4ExpModel(nn.Module):
self._qsa_layer_ids = frozenset(
layer_idx
for layer_idx, layer_type in enumerate(config.layer_types)
if layer_type == "qwen_sparse_attention"
if layer_type in _QSA_LAYER_TYPES
and getattr(config, "indexer_n_heads", None) is not None
)
self.embed_tokens = VocabParallelEmbedding(self.vocab_size, config.hidden_size)
@@ -27,6 +27,7 @@ from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tenso
CompressedTensorsConfig,
)
from vllm.model_executor.layers.quantization.fp8 import Fp8Config
from vllm.model_executor.layers.quantization.inc import INCConfig
from vllm.model_executor.layers.quantization.modelopt import (
ModelOptMixedPrecisionConfig,
ModelOptQuantConfigBase,
@@ -190,6 +191,11 @@ class Qwen4ExpPLEEmbeddingMethod(QuantizeMethodBase):
and quant_config.get_scheme_dict(None, layer_name=prefix) is None
):
return Qwen4ExpPLEUnquantizedEmbeddingMethod()
if (
isinstance(quant_config, INCConfig)
and not quant_config.config_parser.resolve(None, prefix).quantized
):
return Qwen4ExpPLEUnquantizedEmbeddingMethod()
if not isinstance(quant_config, Fp8Config):
raise NotImplementedError(
"Qwen4Exp PLE embedding does not support quantization config "
+7 -3
View File
@@ -82,6 +82,10 @@ from .ops.cute_dsl.hc_down_silu import request_hc_down_silu_warmup
from .ple_layer import Qwen4ExpPLELayer
from .qsa import Qwen4ExpQSAAttention
# Transformers v5.18 renamed `qwen_sparse_attention` to `indexed_attention`
# TODO: Delete qwen_... once Transformers 5.18.0 is the minimum required version.
_QSA_LAYER_TYPES = ("qwen_sparse_attention", "indexed_attention")
def without_modelopt_fp4(
quant_config: QuantizationConfig | None,
@@ -212,7 +216,7 @@ class Qwen4ExpDecoderLayer(nn.Module):
prefix=f"{prefix}.linear_attn",
gqa_interleaved_layout=False,
)
elif layer_type == "qwen_sparse_attention":
elif layer_type in _QSA_LAYER_TYPES:
use_qsa = getattr(config, "indexer_n_heads", None) is not None
if not use_qsa:
self.self_attn = Qwen3NextAttention(
@@ -296,7 +300,7 @@ class Qwen4ExpDecoderLayer(nn.Module):
if self.layer_type == "linear_attention":
attn_out = self.linear_attn(hidden_states=block_input)
elif self.layer_type == "qwen_sparse_attention":
elif self.layer_type in _QSA_LAYER_TYPES:
attn_out = self.self_attn(
hidden_states=block_input,
positions=positions,
@@ -376,7 +380,7 @@ class Qwen4ExpModel(nn.Module):
self._qsa_layer_ids = frozenset(
layer_idx
for layer_idx, layer_type in enumerate(config.layer_types)
if layer_type == "qwen_sparse_attention"
if layer_type in _QSA_LAYER_TYPES
and getattr(config, "indexer_n_heads", None) is not None
)
self.embed_tokens = VocabParallelEmbedding(self.vocab_size, config.hidden_size)