forked from Karylab-cklius/vllm
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b4e83934d | ||
|
|
628c436301 | ||
|
|
2228fe6868 | ||
|
|
84bd8a3c1e | ||
|
|
b786ec8e74 | ||
|
|
20dcd984f9 | ||
|
|
6fca518157 |
+6
-6
@@ -307,12 +307,12 @@ set(VLLM_EXT_SRC
|
||||
"csrc/quantization/activation_kernels.cu"
|
||||
"csrc/cuda_utils_kernels.cu"
|
||||
"csrc/custom_all_reduce.cu"
|
||||
"csrc/torch_bindings.cpp")
|
||||
"csrc/torch_bindings.cpp"
|
||||
"csrc/fused_deepseek_v4_qnorm_rope_kv_insert_kernel.cu")
|
||||
|
||||
if(VLLM_GPU_LANG STREQUAL "CUDA")
|
||||
list(APPEND VLLM_EXT_SRC
|
||||
"csrc/minimax_reduce_rms_kernel.cu"
|
||||
"csrc/fused_deepseek_v4_qnorm_rope_kv_insert_kernel.cu")
|
||||
"csrc/minimax_reduce_rms_kernel.cu")
|
||||
|
||||
SET(CUTLASS_ENABLE_HEADERS_ONLY ON CACHE BOOL "Enable only the header library")
|
||||
|
||||
@@ -1047,13 +1047,13 @@ endif()
|
||||
set(VLLM_MOE_EXT_SRC
|
||||
"csrc/moe/torch_bindings.cpp"
|
||||
"csrc/moe/moe_align_sum_kernels.cu"
|
||||
"csrc/moe/topk_softmax_kernels.cu")
|
||||
"csrc/moe/topk_softmax_kernels.cu"
|
||||
"csrc/moe/topk_softplus_sqrt_kernels.cu")
|
||||
|
||||
if(VLLM_GPU_LANG STREQUAL "CUDA")
|
||||
list(APPEND VLLM_MOE_EXT_SRC
|
||||
"csrc/moe/moe_wna16.cu"
|
||||
"csrc/moe/grouped_topk_kernels.cu"
|
||||
"csrc/moe/topk_softplus_sqrt_kernels.cu")
|
||||
"csrc/moe/grouped_topk_kernels.cu")
|
||||
endif()
|
||||
|
||||
if(VLLM_GPU_LANG STREQUAL "CUDA")
|
||||
|
||||
@@ -29,7 +29,11 @@
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <cuda_fp8.h>
|
||||
#ifndef USE_ROCM
|
||||
#include <cuda_fp8.h>
|
||||
#else
|
||||
#include <hip/hip_fp8.h>
|
||||
#endif
|
||||
#include <cuda_runtime.h>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -42,7 +46,23 @@
|
||||
#include "type_convert.cuh"
|
||||
|
||||
#ifndef FINAL_MASK
|
||||
#define FINAL_MASK 0xffffffffu
|
||||
#ifdef USE_ROCM
|
||||
#define FINAL_MASK 0xffffffffffffffffULL
|
||||
#else
|
||||
#define FINAL_MASK 0xffffffffu
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef USE_ROCM
|
||||
// ROCm-compatible FP8 conversion helpers
|
||||
__device__ __forceinline__ uint8_t rocm_cvt_float_to_fp8_e4m3(float val) {
|
||||
#if defined(HIP_FP8_TYPE_OCP)
|
||||
__hip_fp8_e4m3 fp8_val(val);
|
||||
#else
|
||||
__hip_fp8_e4m3_fnuz fp8_val(val);
|
||||
#endif
|
||||
return reinterpret_cast<uint8_t&>(fp8_val);
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace vllm {
|
||||
@@ -314,9 +334,13 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel(
|
||||
for (int i = 0; i < kElemsPerLane; i++) {
|
||||
float scaled = elements[i] * inv_scale;
|
||||
scaled = fminf(fmaxf(scaled, -kFp8Max), kFp8Max);
|
||||
#ifndef USE_ROCM
|
||||
__nv_fp8_storage_t s =
|
||||
__nv_cvt_float_to_fp8(scaled, __NV_SATFINITE, __NV_E4M3);
|
||||
out_bytes[i] = static_cast<uint8_t>(s);
|
||||
#else
|
||||
out_bytes[i] = rocm_cvt_float_to_fp8_e4m3(scaled);
|
||||
#endif
|
||||
}
|
||||
// One 16-byte STG per lane.
|
||||
*reinterpret_cast<uint4*>(token_fp8_ptr + dim_base) =
|
||||
@@ -384,6 +408,7 @@ void launchFusedDeepseekV4QNormRopeKVRopeQuantInsert(
|
||||
// PDL: enable programmatic stream serialization whenever the hardware
|
||||
// supports it (SM90+). On pre-Hopper GPUs the attribute is unavailable,
|
||||
// so leave numAttrs = 0 and launch as a regular kernel.
|
||||
#ifndef USE_ROCM
|
||||
static int const sm_version = getSMVersion();
|
||||
// Host-side guard: the device kernel body is compiled as a no-op for
|
||||
// bf16 on pre-Ampere (sm_70/sm_75) because _typeConvert<BFloat16> is
|
||||
@@ -410,6 +435,15 @@ void launchFusedDeepseekV4QNormRopeKVRopeQuantInsert(
|
||||
q_inout, kv_in, k_cache, slot_mapping, position_ids, cos_sin_cache, eps,
|
||||
num_tokens_full, num_tokens_insert, num_heads_q, cache_block_size,
|
||||
kv_block_stride);
|
||||
#else
|
||||
// ROCm: use standard kernel launch syntax (no PDL/stream serialization)
|
||||
// clang-format off
|
||||
fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel<scalar_t_in>
|
||||
<<<grid, kBlockSize, 0, stream>>>(
|
||||
q_inout, kv_in, k_cache, slot_mapping, position_ids, cos_sin_cache,
|
||||
eps, num_tokens_full, num_tokens_insert, num_heads_q,
|
||||
cache_block_size, kv_block_stride);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace deepseek_v4_fused_ops
|
||||
|
||||
@@ -60,15 +60,6 @@ __device__ __forceinline__ float toFloat(T value) {
|
||||
}
|
||||
}
|
||||
|
||||
#define FINAL_MASK 0xffffffff
|
||||
template <typename T>
|
||||
__inline__ __device__ T warpReduceSum(T val) {
|
||||
#pragma unroll
|
||||
for (int mask = 16; mask > 0; mask >>= 1)
|
||||
val += __shfl_xor_sync(FINAL_MASK, val, mask, 32);
|
||||
return val;
|
||||
}
|
||||
|
||||
// ====================== TopK softplus_sqrt things
|
||||
// ===============================
|
||||
|
||||
@@ -272,8 +263,14 @@ __launch_bounds__(WARPS_PER_CTA* WARP_SIZE_PARAM) __global__
|
||||
}
|
||||
}
|
||||
// Compute per-thread scale (using warp reduction when renormalizing).
|
||||
// THREADS_PER_ROW-parameterized butterfly works for both warp sizes (32
|
||||
// on CUDA, 64 on ROCm CDNA) and any THREADS_PER_ROW the dispatch picks.
|
||||
if (renormalize) {
|
||||
selected_sum = warpReduceSum(selected_sum);
|
||||
#pragma unroll
|
||||
for (int mask = THREADS_PER_ROW / 2; mask > 0; mask /= 2) {
|
||||
selected_sum +=
|
||||
VLLM_SHFL_XOR_SYNC_WIDTH(selected_sum, mask, THREADS_PER_ROW);
|
||||
}
|
||||
}
|
||||
float scale = static_cast<float>(routed_scaling_factor);
|
||||
if (renormalize) {
|
||||
@@ -544,7 +541,6 @@ void topkGatingSoftplusSqrtKernelLauncher(
|
||||
const IndType* tid2eid, cudaStream_t stream) {
|
||||
static constexpr int WARPS_PER_TB = 4;
|
||||
static constexpr int BYTES_PER_LDG_POWER_OF_2 = 16;
|
||||
#ifndef USE_ROCM
|
||||
// for bfloat16 dtype, we need 4 bytes loading to make sure num_experts
|
||||
// elements can be loaded by a warp
|
||||
static constexpr int BYTES_PER_LDG_MULTIPLE_64 =
|
||||
@@ -552,6 +548,19 @@ void topkGatingSoftplusSqrtKernelLauncher(
|
||||
std::is_same_v<InputType, __half>)
|
||||
? 4
|
||||
: 8;
|
||||
// Narrower LDG (ELTS_PER_LDG=1) used by 192/320/448/576 on ROCm WARP_SIZE=64
|
||||
// where ELTS_PER_LDG=2 fails the EXPERTS%(ELTS_PER_LDG*WARP_SIZE)==0 check.
|
||||
// On CUDA WARP_SIZE=32 the wider LDG already aligns, so the alias collapses
|
||||
// back to BYTES_PER_LDG_MULTIPLE_64 — no behavioral change for CUDA.
|
||||
#ifdef USE_ROCM
|
||||
static constexpr int BYTES_PER_LDG_MULTIPLE_64_NARROW =
|
||||
(std::is_same_v<InputType, __nv_bfloat16> ||
|
||||
std::is_same_v<InputType, __half>)
|
||||
? 2
|
||||
: 4;
|
||||
#else
|
||||
static constexpr int BYTES_PER_LDG_MULTIPLE_64_NARROW =
|
||||
BYTES_PER_LDG_MULTIPLE_64;
|
||||
#endif
|
||||
switch (num_experts) {
|
||||
case 1:
|
||||
@@ -584,27 +593,29 @@ void topkGatingSoftplusSqrtKernelLauncher(
|
||||
case 512:
|
||||
LAUNCH_SOFTPLUS_SQRT(512, WARPS_PER_TB, BYTES_PER_LDG_POWER_OF_2);
|
||||
break;
|
||||
// (CUDA only) support multiples of 64 when num_experts is not power of 2.
|
||||
// ROCm uses WARP_SIZE 64 so 8 bytes loading won't fit for some of
|
||||
// num_experts, alternatively we can test 4 bytes loading and enable it in
|
||||
// future.
|
||||
#ifndef USE_ROCM
|
||||
// Multiples of 64 that are not powers of 2. The kernel requires
|
||||
// EXPERTS % (ELTS_PER_LDG * WARP_SIZE) == 0. With ELTS_PER_LDG=2
|
||||
// (BYTES_PER_LDG_MULTIPLE_64), this holds for all five values on CUDA
|
||||
// WARP_SIZE=32 but only for 384 on ROCm WARP_SIZE=64. The other four
|
||||
// use BYTES_PER_LDG_MULTIPLE_64_NARROW (ELTS_PER_LDG=1), which
|
||||
// satisfies the assertion for any multiple of 64 on either backend;
|
||||
// on CUDA the narrow alias collapses back to the wider load, so CUDA
|
||||
// behavior is unchanged.
|
||||
case 192:
|
||||
LAUNCH_SOFTPLUS_SQRT(192, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64);
|
||||
LAUNCH_SOFTPLUS_SQRT(192, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64_NARROW);
|
||||
break;
|
||||
case 320:
|
||||
LAUNCH_SOFTPLUS_SQRT(320, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64);
|
||||
LAUNCH_SOFTPLUS_SQRT(320, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64_NARROW);
|
||||
break;
|
||||
case 384:
|
||||
LAUNCH_SOFTPLUS_SQRT(384, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64);
|
||||
break;
|
||||
case 448:
|
||||
LAUNCH_SOFTPLUS_SQRT(448, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64);
|
||||
LAUNCH_SOFTPLUS_SQRT(448, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64_NARROW);
|
||||
break;
|
||||
case 576:
|
||||
LAUNCH_SOFTPLUS_SQRT(576, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64);
|
||||
LAUNCH_SOFTPLUS_SQRT(576, WARPS_PER_TB, BYTES_PER_LDG_MULTIPLE_64_NARROW);
|
||||
break;
|
||||
#endif
|
||||
default: {
|
||||
TORCH_CHECK(false, "Unsupported expert number: ", num_experts);
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, m) {
|
||||
"bias) -> ()");
|
||||
m.impl("topk_sigmoid", torch::kCUDA, &topk_sigmoid);
|
||||
|
||||
#ifndef USE_ROCM
|
||||
m.def(
|
||||
"topk_softplus_sqrt(Tensor! topk_weights, Tensor! topk_indices, Tensor! "
|
||||
"token_expert_indices, Tensor gating_output, bool renormalize, float "
|
||||
"routed_scaling_factor, Tensor? "
|
||||
"bias, Tensor? input_ids, Tensor? tid2eid) -> ()");
|
||||
m.impl("topk_softplus_sqrt", torch::kCUDA, &topk_softplus_sqrt);
|
||||
#endif
|
||||
|
||||
// Calculate the result of moe by summing up the partial results
|
||||
// from all selected experts.
|
||||
m.def("moe_sum(Tensor input, Tensor! output) -> ()");
|
||||
|
||||
@@ -183,7 +183,6 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
||||
"int forced_token_heads_per_warp=-1) -> ()");
|
||||
ops.impl("fused_qk_norm_rope", torch::kCUDA, &fused_qk_norm_rope);
|
||||
|
||||
#ifndef USE_ROCM
|
||||
// Horizontally-fused DeepseekV4-MLA: per-head RMSNorm + GPT-J RoPE for Q, and
|
||||
// GPT-J RoPE + UE8M0 FP8 quant + paged cache insert for KV, all in one
|
||||
// kernel launch.
|
||||
@@ -194,7 +193,6 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
||||
"float eps, int cache_block_size) -> ()");
|
||||
ops.impl("fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert", torch::kCUDA,
|
||||
&fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert);
|
||||
#endif
|
||||
|
||||
// Apply repetition penalties to logits in-place
|
||||
ops.def(
|
||||
|
||||
@@ -21,3 +21,6 @@ timm>=1.0.17
|
||||
# amd-quark: required for Quark quantization on ROCm
|
||||
# To be consistent with test_quark.py
|
||||
amd-quark>=0.8.99
|
||||
# tilelang has to be installed for mhc module to be
|
||||
# imported correctly.
|
||||
tilelang==0.1.9
|
||||
|
||||
@@ -70,7 +70,8 @@ def test_sqrtsoftplus_bias_uses_deepseek_v4_routing_method():
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.is_cuda(), reason="This test is skipped on non-CUDA platform."
|
||||
not current_platform.is_cuda_alike(),
|
||||
reason="This test is skipped on non-CUDA platform.",
|
||||
)
|
||||
@pytest.mark.parametrize("num_tokens", [1, 33, 128])
|
||||
@pytest.mark.parametrize("hidden_size", [1024, 2048])
|
||||
@@ -125,7 +126,8 @@ def test_fused_topk_softplus_sqrt(
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.is_cuda(), reason="This test is skipped on non-CUDA platform."
|
||||
not current_platform.is_cuda_alike(),
|
||||
reason="This test is skipped on non-CUDA platform.",
|
||||
)
|
||||
@pytest.mark.parametrize("num_tokens", [1, 33, 128])
|
||||
@pytest.mark.parametrize("hidden_size", [1024, 2048])
|
||||
|
||||
@@ -119,6 +119,7 @@ MoEBackend = Literal[
|
||||
"flashinfer_cutedsl",
|
||||
"marlin",
|
||||
"humming",
|
||||
"triton_unfused",
|
||||
"aiter",
|
||||
"emulation",
|
||||
]
|
||||
@@ -150,6 +151,7 @@ class KernelConfig:
|
||||
- "flashinfer_cutedsl": Use FlashInfer with CuteDSL kernels (FP4 only)
|
||||
- "marlin": Use Marlin kernels (weight-only quantization)
|
||||
- "humming": Use Humming Mixed Precision kernels
|
||||
- "triton_unfused": Use Triton unfused MoE kernels
|
||||
- "aiter": Use AMD AITer kernels (ROCm only)
|
||||
- "emulation": use BF16/FP16 GEMM, dequantizing weights and
|
||||
running QDQ on activations.
|
||||
|
||||
@@ -50,6 +50,7 @@ MTPModelTypes = Literal[
|
||||
"pangu_ultra_moe_mtp",
|
||||
"step3p5_mtp",
|
||||
"hy_v3_mtp",
|
||||
"gemma4_mtp",
|
||||
]
|
||||
NgramGPUTypes = Literal["ngram_gpu"]
|
||||
DFlashModelTypes = Literal["dflash"]
|
||||
@@ -491,6 +492,17 @@ class SpeculativeConfig:
|
||||
{"n_predict": n_predict, "architectures": ["HYV3MTPModel"]}
|
||||
)
|
||||
|
||||
if hf_config.model_type == "gemma4_assistant":
|
||||
hf_config.model_type = "gemma4_mtp"
|
||||
text_config = getattr(hf_config, "text_config", hf_config)
|
||||
# The assistant runs all decoder layers in a single forward
|
||||
# call to produce one draft token, so n_predict=1.
|
||||
# num_kv_shared_layers must be 0: cross-model KV sharing is
|
||||
# set up by the proposer after model construction.
|
||||
if hasattr(text_config, "num_kv_shared_layers"):
|
||||
text_config.num_kv_shared_layers = 0
|
||||
hf_config.update({"n_predict": 1, "architectures": ["Gemma4MTPModel"]})
|
||||
|
||||
return hf_config
|
||||
|
||||
def __post_init__(self):
|
||||
@@ -1032,6 +1044,14 @@ class SpeculativeConfig:
|
||||
slots_per_req += 1
|
||||
return slots_per_req
|
||||
|
||||
def use_gemma4_mtp(self) -> bool:
|
||||
return (
|
||||
self.method == "mtp"
|
||||
and self.draft_model_config is not None
|
||||
and getattr(self.draft_model_config.hf_config, "model_type", None)
|
||||
== "gemma4_mtp"
|
||||
)
|
||||
|
||||
def use_eagle(self) -> bool:
|
||||
return self.method in ("eagle", "eagle3", "mtp", "dflash")
|
||||
|
||||
|
||||
@@ -312,6 +312,21 @@ class AiterFp8BlockScaledMMKernel(Fp8BlockScaledMMLinearKernel):
|
||||
As: torch.Tensor,
|
||||
Bs: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
if As.dtype != Bs.dtype:
|
||||
from vllm.model_executor.layers.quantization.utils.fp8_utils import (
|
||||
_upcast_e8m0_to_fp32,
|
||||
)
|
||||
|
||||
if As.dtype == torch.float8_e8m0fnu:
|
||||
As = _upcast_e8m0_to_fp32(As).contiguous()
|
||||
else:
|
||||
As = As.to(torch.float32)
|
||||
|
||||
if Bs.dtype == torch.float8_e8m0fnu:
|
||||
Bs = _upcast_e8m0_to_fp32(Bs).contiguous()
|
||||
else:
|
||||
Bs = Bs.to(torch.float32)
|
||||
|
||||
out_dtype = self.config.out_dtype
|
||||
if self.use_triton:
|
||||
gemm_a8w8_blockscale_op = rocm_aiter_ops.triton_gemm_a8w8_blockscale
|
||||
|
||||
@@ -169,7 +169,9 @@ class SiluAndMulWithClamp(CustomOp):
|
||||
def __init__(self, swiglu_limit: float, *, compile_native: bool = True):
|
||||
super().__init__(compile_native=compile_native)
|
||||
self.swiglu_limit = float(swiglu_limit)
|
||||
if current_platform.is_cuda_alike() or current_platform.is_xpu():
|
||||
if current_platform.is_rocm():
|
||||
self._forward_method = self.forward_native
|
||||
elif current_platform.is_cuda_alike() or current_platform.is_xpu():
|
||||
self.op = torch.ops._C.silu_and_mul_with_clamp
|
||||
elif current_platform.is_cpu():
|
||||
self._forward_method = self.forward_native
|
||||
|
||||
@@ -300,6 +300,7 @@ class DeepseekCompressor(nn.Module):
|
||||
state_cache = self.state_cache.kv_cache
|
||||
# kv_state stored in first half, score_state stored in second half
|
||||
state_width = state_cache.shape[-1] // 2
|
||||
pdl_kwargs = {} if current_platform.is_rocm() else {"launch_pdl": False}
|
||||
|
||||
# Store the KV and score (with fused APE addition) in the state.
|
||||
# NOTE: PDL is disabled — both this kernel and _fused_kernel below
|
||||
@@ -324,7 +325,7 @@ class DeepseekCompressor(nn.Module):
|
||||
TRITON_BLOCK_SIZE=triton.next_power_of_2(kv.shape[-1]),
|
||||
STATE_WIDTH=state_width,
|
||||
COMPRESS_RATIO=self.compress_ratio,
|
||||
launch_pdl=False,
|
||||
**pdl_kwargs,
|
||||
)
|
||||
|
||||
# Fused: compress → RMSNorm → RoPE → FP8 quant → KV cache write.
|
||||
@@ -373,7 +374,7 @@ class DeepseekCompressor(nn.Module):
|
||||
SCALE_DIM=self._scale_dim,
|
||||
KV_BLOCK_STRIDE=kv_cache.stride(0),
|
||||
num_warps=self._num_warps,
|
||||
launch_pdl=False,
|
||||
**pdl_kwargs,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,11 @@ from vllm.v1.attention.ops.deepseek_v4_ops import (
|
||||
fused_inv_rope_fp8_quant,
|
||||
fused_q_kv_rmsnorm,
|
||||
)
|
||||
from vllm.v1.attention.ops.rocm_aiter_mla_sparse import (
|
||||
rocm_forward_decode_fallback,
|
||||
rocm_inv_rope_einsum,
|
||||
rocm_sparse_attn_prefill,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.v1.attention.backends.mla.sparse_swa import (
|
||||
@@ -53,6 +58,7 @@ from vllm.model_executor.layers.quantization.input_quant_fp8 import (
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
GroupShape,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.multi_stream_utils import (
|
||||
execute_in_parallel,
|
||||
maybe_execute_in_parallel,
|
||||
@@ -198,8 +204,6 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
# Pick fp8_einsum recipe based on GPU arch:
|
||||
# SM90: FP32 block scales stay [g, r/128, d/128] → sfb_gran_mn=128
|
||||
# SM100: INT32 packed scales become [g, r, ...] → sfb_gran_mn=1
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
cap = current_platform.get_device_capability()
|
||||
assert cap is not None, "DeepseekV4 attention requires a CUDA device"
|
||||
self._einsum_recipe = (1, 128, 128) if cap.major <= 9 else (1, 1, 128)
|
||||
@@ -222,6 +226,7 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
+ 1 # 1B pad
|
||||
)
|
||||
|
||||
# Will be None on ROCm for now.
|
||||
self.aux_stream_list = mla_modules.aux_stream_list
|
||||
# [0]: GEMM start / post-GEMM event0. [1..3]: GEMM done events;
|
||||
# [1] doubles as post-GEMM event1. Reuse is safe: GEMM fully joins
|
||||
@@ -303,6 +308,19 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
)
|
||||
o = o_padded[:, : self.n_local_heads, :]
|
||||
|
||||
# Keep ROCm on the BF16 reference wo_a path util kernel ready.
|
||||
if current_platform.is_rocm():
|
||||
z = rocm_inv_rope_einsum(
|
||||
self.rotary_emb,
|
||||
o,
|
||||
positions,
|
||||
self.rope_head_dim,
|
||||
self.n_local_groups,
|
||||
self.o_lora_rank,
|
||||
self.wo_a,
|
||||
)
|
||||
return self.wo_b(z.flatten(1))
|
||||
|
||||
# O projection: inverse RoPE + FP8 quant + einsum + wo_b
|
||||
o_fp8, o_scale = fused_inv_rope_fp8_quant(
|
||||
o,
|
||||
@@ -336,12 +354,15 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
return self.wo_b(z.flatten(1))
|
||||
|
||||
def attn_gemm_parallel_execute(self, hidden_states) -> tuple[Any, ...]:
|
||||
assert self.aux_stream_list is not None
|
||||
assert len(self.aux_stream_list) >= 3
|
||||
aux_streams = self.aux_stream_list
|
||||
if aux_streams is not None:
|
||||
assert len(aux_streams) >= 3
|
||||
aux_streams = aux_streams[:3]
|
||||
|
||||
# fused_wqa_wkv (heaviest) on default; the three lighter input GEMMs
|
||||
# on aux streams 0..2 when their owning module exists. ln_events[0]
|
||||
# is the fan-out start event; ln_events[1..3] are per-aux done events.
|
||||
# On ROCm, aux_streams is None and execute_in_parallel runs serially.
|
||||
aux_fns: list[Callable[[], Any] | None] = [None, None, None]
|
||||
|
||||
if self.compressor is not None:
|
||||
@@ -385,7 +406,7 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
aux_fns,
|
||||
self.ln_events[0],
|
||||
self.ln_events[1:4],
|
||||
self.aux_stream_list[:3],
|
||||
aux_streams,
|
||||
enable=hidden_states.shape[0]
|
||||
<= envs.VLLM_MULTI_STREAM_GEMM_TOKEN_THRESHOLD,
|
||||
)
|
||||
@@ -419,8 +440,9 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
# downstream reads q on default). Indexer/compressor go on aux for
|
||||
# overlap with default's GEMM + cache write.
|
||||
if self.indexer is not None:
|
||||
assert self.aux_stream_list is not None
|
||||
aux_stream = self.aux_stream_list[0]
|
||||
aux_stream = (
|
||||
self.aux_stream_list[0] if self.aux_stream_list is not None else None
|
||||
)
|
||||
indexer = self.indexer
|
||||
# Local ref so the closure keeps a non-None type for mypy.
|
||||
assert self.compressor is not None
|
||||
@@ -448,8 +470,9 @@ class DeepseekV4MultiHeadLatentAttentionWrapper(PluggableLayer):
|
||||
)
|
||||
elif self.compressor is not None:
|
||||
# wq_b + kv_insert on default, compressor on aux.
|
||||
assert self.aux_stream_list is not None
|
||||
aux_stream = self.aux_stream_list[0]
|
||||
aux_stream = (
|
||||
self.aux_stream_list[0] if self.aux_stream_list is not None else None
|
||||
)
|
||||
compressor = self.compressor
|
||||
|
||||
def wq_b_kv_insert() -> torch.Tensor:
|
||||
@@ -668,7 +691,7 @@ class DeepseekV4MLAAttention(nn.Module, AttentionLayerBase):
|
||||
vllm_config.scheduler_config.max_num_batched_tokens
|
||||
)
|
||||
self.max_model_len = vllm_config.model_config.max_model_len
|
||||
# DeepseekV4 only supports fp8 kv-cache format for now
|
||||
# DeepseekV4 only supports fp8 kv-cache format for now.
|
||||
kv_cache_dtype = cache_config.cache_dtype if cache_config is not None else "fp8"
|
||||
|
||||
assert kv_cache_dtype.startswith("fp8"), (
|
||||
@@ -816,6 +839,25 @@ class DeepseekV4MLAAttention(nn.Module, AttentionLayerBase):
|
||||
swa_indices = swa_metadata.decode_swa_indices
|
||||
swa_lens = swa_metadata.decode_swa_lens
|
||||
|
||||
if current_platform.is_rocm():
|
||||
rocm_forward_decode_fallback(
|
||||
q=q,
|
||||
kv_cache=kv_cache,
|
||||
swa_k_cache=self.swa_cache_layer.kv_cache,
|
||||
swa_only=swa_only,
|
||||
topk_indices=topk_indices,
|
||||
topk_lens=topk_lens,
|
||||
swa_indices=swa_indices,
|
||||
swa_lens=swa_lens,
|
||||
attn_sink=self.attn_sink,
|
||||
scale=self.scale,
|
||||
head_dim=self.head_dim,
|
||||
nope_head_dim=self.nope_head_dim,
|
||||
rope_head_dim=self.rope_head_dim,
|
||||
output=output,
|
||||
)
|
||||
return
|
||||
|
||||
# We treat queries in the same seq as different queries
|
||||
# and later we only attend by generated indices.
|
||||
# q arrives pre-padded to self.padded_heads by the outer wrapper.
|
||||
@@ -980,15 +1022,27 @@ class DeepseekV4MLAAttention(nn.Module, AttentionLayerBase):
|
||||
N,
|
||||
)
|
||||
|
||||
output_chunk, _, _ = flash_mla_sparse_fwd(
|
||||
q=q[query_start:query_end],
|
||||
kv=kv.view(-1, 1, q.shape[-1]),
|
||||
indices=combined_indices.unsqueeze(1),
|
||||
sm_scale=self.scale,
|
||||
attn_sink=self.attn_sink,
|
||||
topk_length=combined_lens,
|
||||
out=output[query_start:query_end],
|
||||
)
|
||||
if current_platform.is_rocm():
|
||||
rocm_sparse_attn_prefill(
|
||||
q=q[query_start:query_end],
|
||||
kv=kv.view(-1, 1, q.shape[-1]),
|
||||
indices=combined_indices.unsqueeze(1),
|
||||
topk_length=combined_lens,
|
||||
scale=self.scale,
|
||||
head_dim=self.head_dim,
|
||||
attn_sink=self.attn_sink,
|
||||
output=output[query_start:query_end],
|
||||
)
|
||||
else:
|
||||
output_chunk, _, _ = flash_mla_sparse_fwd(
|
||||
q=q[query_start:query_end],
|
||||
kv=kv.view(-1, 1, q.shape[-1]),
|
||||
indices=combined_indices.unsqueeze(1),
|
||||
sm_scale=self.scale,
|
||||
attn_sink=self.attn_sink,
|
||||
topk_length=combined_lens,
|
||||
out=output[query_start:query_end],
|
||||
)
|
||||
|
||||
|
||||
class DeepseekV4IndexerCache(torch.nn.Module, AttentionLayerBase):
|
||||
|
||||
@@ -18,6 +18,7 @@ from vllm.model_executor.layers.fused_moe.all2all_utils import (
|
||||
from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEQuantConfig,
|
||||
FusedMoEQuantDesc,
|
||||
RoutingMethodType,
|
||||
mxfp4_mxfp8_moe_quant_config,
|
||||
mxfp4_w4a8_moe_quant_config,
|
||||
mxfp4_w4a16_moe_quant_config,
|
||||
@@ -64,6 +65,8 @@ class Mxfp4MoeBackend(Enum):
|
||||
MARLIN = "MARLIN"
|
||||
# ROCm AITER backends
|
||||
AITER_MXFP4_BF16 = "AITER_MXFP4_BF16" # W4A16: CK kernel
|
||||
# Keep the legacy name as an alias while the ROCm split backend rename settles.
|
||||
AITER = "AITER_MXFP4_BF16"
|
||||
AITER_MXFP4_FP8 = "AITER_MXFP4_FP8" # W4A8: triton kernel
|
||||
# Triton
|
||||
TRITON = "TRITON"
|
||||
@@ -253,6 +256,8 @@ def _get_priority_backends() -> list[Mxfp4MoeBackend]:
|
||||
TRTLLM MXFP8; SM90 falls through to Triton_unfused or Marlin (the
|
||||
backend-level ``is_supported_config`` check filters by device capability).
|
||||
"""
|
||||
if current_platform.is_rocm():
|
||||
return [Mxfp4MoeBackend.AITER_MXFP4_BF16]
|
||||
_AVAILABLE_BACKENDS = [
|
||||
Mxfp4MoeBackend.FLASHINFER_TRTLLM_MXFP4_MXFP8,
|
||||
Mxfp4MoeBackend.DEEPGEMM_MXFP4,
|
||||
@@ -543,8 +548,22 @@ def select_deepseek_v4_mxfp4_moe_backend(
|
||||
activation_format,
|
||||
)
|
||||
|
||||
# DeepSeek-V4 on ROCm is more accurate with the unfused Triton MXFP4 path
|
||||
# than the default AITER path. Prefer Triton-unfused for this routing mode,
|
||||
# while keeping AITER as a fallback if Triton-unfused rejects the config.
|
||||
if (
|
||||
current_platform.is_rocm()
|
||||
and config.routing_method == RoutingMethodType.DeepseekV4
|
||||
):
|
||||
priority_backends = [
|
||||
Mxfp4MoeBackend.TRITON_UNFUSED,
|
||||
Mxfp4MoeBackend.AITER_MXFP4_BF16,
|
||||
]
|
||||
else:
|
||||
priority_backends = _get_priority_backends()
|
||||
|
||||
# Iterate priority backends: TRTLLM MXFP8, then Triton.
|
||||
for backend in _get_priority_backends():
|
||||
for backend in priority_backends:
|
||||
activation_key = _backend_activation_key(backend)
|
||||
for k_cls in backend_to_kernel_cls(backend):
|
||||
supported, reason = k_cls.is_supported_config(
|
||||
@@ -1252,6 +1271,64 @@ def convert_weight_to_mxfp4_moe_kernel_format(
|
||||
w2_bias,
|
||||
)
|
||||
|
||||
elif mxfp4_backend == Mxfp4MoeBackend.AITER_MXFP4_BF16:
|
||||
from vllm._aiter_ops import rocm_aiter_ops
|
||||
|
||||
if w13_bias is not None:
|
||||
w13_bias = w13_bias.data.to(torch.float32)
|
||||
if w2_bias is not None:
|
||||
w2_bias = w2_bias.data.to(torch.float32)
|
||||
|
||||
e, n, k = w13_weight.shape
|
||||
|
||||
w13_weight.view(torch.uint8).copy_(
|
||||
w13_weight.data.view(torch.uint8)
|
||||
.view(e, n // 2, 2, k)
|
||||
.permute(0, 2, 1, 3)
|
||||
.contiguous()
|
||||
.view(e, n, k)
|
||||
)
|
||||
w13_weight_scale.data = (
|
||||
w13_weight_scale.data.view(e, n // 2, 2, -1)
|
||||
.permute(0, 2, 1, 3)
|
||||
.contiguous()
|
||||
.view(e, n, -1)
|
||||
)
|
||||
|
||||
w13_weight.data = w13_weight.data.view(torch.float4_e2m1fn_x2)
|
||||
w2_weight.data = w2_weight.data.view(torch.float4_e2m1fn_x2)
|
||||
|
||||
w13_weight.data = rocm_aiter_ops.shuffle_weight_a16w4(w13_weight, 16, True)
|
||||
shuffled_w13_scale = rocm_aiter_ops.shuffle_scale_a16w4(
|
||||
w13_weight_scale.view(-1, w13_weight_scale.shape[-1]),
|
||||
num_experts,
|
||||
True,
|
||||
)
|
||||
|
||||
w2_weight.data = rocm_aiter_ops.shuffle_weight_a16w4(w2_weight, 16, False)
|
||||
shuffled_w2_scale = rocm_aiter_ops.shuffle_scale_a16w4(
|
||||
w2_weight_scale.view(-1, w2_weight_scale.shape[-1]),
|
||||
num_experts,
|
||||
False,
|
||||
)
|
||||
|
||||
if w13_bias is not None:
|
||||
w13_bias = (
|
||||
w13_bias.data.view(-1, n // 2, 2)
|
||||
.permute(0, 2, 1)
|
||||
.contiguous()
|
||||
.view(-1, n)
|
||||
)
|
||||
|
||||
return (
|
||||
w13_weight,
|
||||
w2_weight,
|
||||
shuffled_w13_scale,
|
||||
shuffled_w2_scale,
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
|
||||
elif mxfp4_backend in TRITON_BACKENDS:
|
||||
from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig
|
||||
|
||||
@@ -1307,7 +1384,7 @@ def convert_weight_to_mxfp4_moe_kernel_format(
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unsupported mxfp4_backend for Mxfp4MoEMethod: {mxfp4_backend}. "
|
||||
f"Expected TRTLLM or Triton backend."
|
||||
f"Expected TRTLLM, Triton, or AITER backend."
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -268,10 +268,13 @@ class LinearBase(PluggableLayer):
|
||||
self.quant_config = quant_config
|
||||
self.prefix = prefix
|
||||
self.allow_fp8_block_shape_mismatch = False
|
||||
self.quant_method: QuantizeMethodBase
|
||||
if quant_config is None:
|
||||
self.quant_method: QuantizeMethodBase | None = UnquantizedLinearMethod()
|
||||
self.quant_method = UnquantizedLinearMethod()
|
||||
elif quant_method := quant_config.get_quant_method(self, prefix=prefix):
|
||||
self.quant_method = quant_method
|
||||
else:
|
||||
self.quant_method = quant_config.get_quant_method(self, prefix=prefix)
|
||||
raise ValueError("All linear layers should support quant method.")
|
||||
self.return_bias = return_bias
|
||||
self.disable_tp = disable_tp
|
||||
self.tp_rank = get_tensor_model_parallel_rank() if not disable_tp else 0
|
||||
@@ -335,8 +338,6 @@ class ReplicatedLinear(LinearBase):
|
||||
disable_tp=disable_tp,
|
||||
)
|
||||
|
||||
# All the linear layer supports quant method.
|
||||
assert self.quant_method is not None
|
||||
self.quant_method.create_weights(
|
||||
self,
|
||||
self.input_size,
|
||||
@@ -389,7 +390,6 @@ class ReplicatedLinear(LinearBase):
|
||||
x: torch.Tensor,
|
||||
) -> torch.Tensor | tuple[torch.Tensor, Parameter | None]:
|
||||
bias = self.bias if not self.skip_bias_add else None
|
||||
assert self.quant_method is not None
|
||||
|
||||
output = self.quant_method.apply(self, x, bias)
|
||||
|
||||
@@ -474,7 +474,6 @@ class ColumnParallelLinear(LinearBase):
|
||||
self._maybe_allow_fp8_block_shape_mismatch()
|
||||
self.gather_output = gather_output
|
||||
|
||||
assert self.quant_method is not None
|
||||
self.quant_method.create_weights(
|
||||
layer=self,
|
||||
input_size_per_partition=self.input_size_per_partition,
|
||||
@@ -583,7 +582,6 @@ class ColumnParallelLinear(LinearBase):
|
||||
bias = self.bias if not self.skip_bias_add else None
|
||||
|
||||
# Matrix multiply.
|
||||
assert self.quant_method is not None
|
||||
output_parallel = self.quant_method.apply(self, input_, bias)
|
||||
|
||||
if self.gather_output and self.tp_size > 1:
|
||||
@@ -1463,7 +1461,6 @@ class RowParallelLinear(LinearBase):
|
||||
self.input_is_parallel = input_is_parallel
|
||||
self.reduce_results = reduce_results
|
||||
|
||||
assert self.quant_method is not None
|
||||
self.quant_method.create_weights(
|
||||
layer=self,
|
||||
input_size_per_partition=self.input_size_per_partition,
|
||||
@@ -1553,7 +1550,6 @@ class RowParallelLinear(LinearBase):
|
||||
input_parallel = split_input[self.tp_rank].contiguous()
|
||||
|
||||
# Matrix multiply.
|
||||
assert self.quant_method is not None
|
||||
# Only fuse bias add into GEMM for rank 0 (this ensures that
|
||||
# bias will not get added more than once in TP>1 case)
|
||||
bias_ = None if (self.tp_rank > 0 or self.skip_bias_add) else self.bias
|
||||
|
||||
@@ -234,6 +234,39 @@ def mhc_pre(
|
||||
num_tokens = residual_flat.shape[0]
|
||||
fn_flat = fn
|
||||
|
||||
if current_platform.is_rocm():
|
||||
x = residual_flat.view(num_tokens, hc_mult * hidden_size).to(torch.float32)
|
||||
mixes = torch.matmul(x, fn_flat.t())
|
||||
sqrsum = x.square().sum(dim=-1, keepdim=True)
|
||||
mixes = mixes * torch.rsqrt(sqrsum / (hc_mult * hidden_size) + rms_eps)
|
||||
|
||||
pre_logits = mixes[:, :hc_mult] * hc_scale[0] + hc_base[:hc_mult]
|
||||
pre_mix = torch.sigmoid(pre_logits) + hc_pre_eps
|
||||
|
||||
post_logits = (
|
||||
mixes[:, hc_mult : 2 * hc_mult] * hc_scale[1]
|
||||
+ hc_base[hc_mult : 2 * hc_mult]
|
||||
)
|
||||
post_mix = torch.sigmoid(post_logits) * hc_post_mult_value
|
||||
|
||||
comb_logits = mixes[:, 2 * hc_mult :].view(
|
||||
num_tokens, hc_mult, hc_mult
|
||||
) * hc_scale[2] + hc_base[2 * hc_mult :].view(1, hc_mult, hc_mult)
|
||||
comb_mix = torch.softmax(comb_logits, dim=-1) + hc_sinkhorn_eps
|
||||
comb_mix = comb_mix / (comb_mix.sum(dim=-2, keepdim=True) + hc_sinkhorn_eps)
|
||||
for _ in range(sinkhorn_repeat - 1):
|
||||
comb_mix = comb_mix / (comb_mix.sum(dim=-1, keepdim=True) + hc_sinkhorn_eps)
|
||||
comb_mix = comb_mix / (comb_mix.sum(dim=-2, keepdim=True) + hc_sinkhorn_eps)
|
||||
|
||||
layer_input = torch.sum(
|
||||
pre_mix.unsqueeze(-1) * residual_flat.to(torch.float32), dim=1
|
||||
).to(torch.bfloat16)
|
||||
return (
|
||||
post_mix.view(*outer_shape, hc_mult, 1),
|
||||
comb_mix.view(*outer_shape, hc_mult, hc_mult),
|
||||
layer_input.view(*outer_shape, hidden_size),
|
||||
)
|
||||
|
||||
# these number are from deepgemm kernel impl
|
||||
block_k = 64
|
||||
block_m = 64
|
||||
@@ -414,6 +447,14 @@ def mhc_post(
|
||||
post_layer_mix: torch.Tensor,
|
||||
comb_res_mix: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
if current_platform.is_rocm():
|
||||
mixed_residual = torch.einsum(
|
||||
"...ij,...ih->...jh",
|
||||
comb_res_mix.to(torch.float32),
|
||||
residual.to(torch.float32),
|
||||
)
|
||||
post_term = post_layer_mix.to(torch.float32) * x.unsqueeze(-2).to(torch.float32)
|
||||
return (mixed_residual + post_term).to(residual.dtype)
|
||||
out = torch.empty_like(residual)
|
||||
mhc_post_tilelang(
|
||||
comb_res_mix,
|
||||
@@ -551,6 +592,49 @@ def hc_head_fuse_tilelang(
|
||||
T.pdl_trigger()
|
||||
|
||||
|
||||
def _hc_head_fused_reference(
|
||||
hs_flat: torch.Tensor,
|
||||
fn: torch.Tensor,
|
||||
hc_scale: torch.Tensor,
|
||||
hc_base: torch.Tensor,
|
||||
out: torch.Tensor,
|
||||
hidden_size: int,
|
||||
rms_eps: float,
|
||||
hc_eps: float,
|
||||
hc_mult: int,
|
||||
) -> None:
|
||||
"""Pure-PyTorch reference for `hc_head_fuse_tilelang`.
|
||||
|
||||
Used on platforms where the tilelang HIP/CUDA backend is not available
|
||||
(e.g. ROCm builds shipping a tilelang wheel without `target.build.tilelang_hip`).
|
||||
Mirrors the math of the tilelang kernel exactly:
|
||||
|
||||
x = hs_flat.flatten(-2, -1) # (T, hc_mult * H), fp32
|
||||
mixes = x @ fn.T # (T, hc_mult)
|
||||
rsqrt = 1 / sqrt(||x||^2 / (hc_mult * H) + rms_eps)
|
||||
pre[m] = sigmoid(mixes[m] * rsqrt * hc_scale[0] + hc_base[m]) + hc_eps
|
||||
out = sum_m pre[m] * hs_flat[:, m, :] # cast back to bf16
|
||||
|
||||
`out` is mutated in place to keep the same op contract
|
||||
(`mutates_args=["out"]`).
|
||||
"""
|
||||
num_tokens = hs_flat.shape[0]
|
||||
if num_tokens == 0:
|
||||
return
|
||||
x = hs_flat.reshape(num_tokens, hc_mult * hidden_size).to(torch.float32)
|
||||
# fn: (hc_mult, hc_mult * hidden_size) → mixes: (T, hc_mult)
|
||||
mixes = torch.matmul(x, fn.t())
|
||||
sqrsum = x.square().sum(dim=-1, keepdim=True)
|
||||
rsqrt = torch.rsqrt(sqrsum / (hc_mult * hidden_size) + rms_eps)
|
||||
# hc_scale has shape (1,); hc_base has shape (hc_mult,)
|
||||
pre_mix = torch.sigmoid(mixes * rsqrt * hc_scale[0] + hc_base) + hc_eps
|
||||
# weighted sum over the hc_mult channel dim
|
||||
result = torch.sum(pre_mix.unsqueeze(-1) * hs_flat.to(torch.float32), dim=1).to(
|
||||
out.dtype
|
||||
)
|
||||
out.copy_(result)
|
||||
|
||||
|
||||
def _hc_head_fused_kernel(
|
||||
hs_flat: torch.Tensor,
|
||||
fn: torch.Tensor,
|
||||
@@ -563,8 +647,15 @@ def _hc_head_fused_kernel(
|
||||
hc_mult: int,
|
||||
) -> None:
|
||||
"""Fill pre-allocated `out` (T, H) in-place with the hc_head result."""
|
||||
if hs_flat.shape[0] > 0:
|
||||
hc_head_fuse_tilelang(
|
||||
if hs_flat.shape[0] == 0:
|
||||
return
|
||||
if current_platform.is_rocm():
|
||||
# tilelang ships only the CUDA codegen in upstream wheels, so the HIP
|
||||
# FFI target (`target.build.tilelang_hip`) is missing and the JIT call
|
||||
# would raise `ValueError: Cannot find global function ...`. Use a
|
||||
# numerically equivalent torch fallback instead. `mhc_pre` and
|
||||
# `mhc_post` already follow this same pattern above.
|
||||
_hc_head_fused_reference(
|
||||
hs_flat,
|
||||
fn,
|
||||
hc_scale,
|
||||
@@ -575,6 +666,18 @@ def _hc_head_fused_kernel(
|
||||
hc_eps,
|
||||
hc_mult,
|
||||
)
|
||||
return
|
||||
hc_head_fuse_tilelang(
|
||||
hs_flat,
|
||||
fn,
|
||||
hc_scale,
|
||||
hc_base,
|
||||
out,
|
||||
hidden_size,
|
||||
rms_eps,
|
||||
hc_eps,
|
||||
hc_mult,
|
||||
)
|
||||
|
||||
|
||||
direct_register_custom_op(
|
||||
|
||||
@@ -843,6 +843,15 @@ def w8a8_triton_block_scaled_mm(
|
||||
assert len(block_size) == 2
|
||||
block_n, block_k = block_size[0], block_size[1]
|
||||
|
||||
# Triton cannot currently bind E8M0 scale tensors directly. On ROCm,
|
||||
# DeepSeek-V4 checkpoints store block scales in exponent-only E8M0 format,
|
||||
# so decode them to fp32 before launching the kernel.
|
||||
if current_platform.is_rocm():
|
||||
if As.dtype == torch.float8_e8m0fnu:
|
||||
As = _upcast_e8m0_to_fp32(As).contiguous()
|
||||
if Bs.dtype == torch.float8_e8m0fnu:
|
||||
Bs = _upcast_e8m0_to_fp32(Bs).contiguous()
|
||||
|
||||
assert A.shape[-1] == B.shape[-1]
|
||||
assert A.shape[:-1] == As.shape[:-1] and A.is_contiguous()
|
||||
assert triton.cdiv(A.shape[-1], block_k) == As.shape[-1]
|
||||
|
||||
@@ -499,13 +499,31 @@ class SparseAttnIndexer(CustomOp):
|
||||
k: torch.Tensor,
|
||||
weights: torch.Tensor,
|
||||
):
|
||||
assert not self.skip_k_cache_insert, (
|
||||
"AMD platform doesn't support skip cache insert yet"
|
||||
)
|
||||
assert not self.use_fp4_cache, "AMD platform doesn't support fp4 cache yet"
|
||||
assert isinstance(q_quant, torch.Tensor), (
|
||||
"AMD sparse_attn_indexer expects a single FP8 q_quant tensor"
|
||||
)
|
||||
if self.skip_k_cache_insert or not rocm_aiter_ops.is_enabled():
|
||||
from vllm.v1.attention.ops.rocm_aiter_mla_sparse import (
|
||||
rocm_aiter_sparse_attn_indexer_native,
|
||||
)
|
||||
|
||||
return rocm_aiter_sparse_attn_indexer_native(
|
||||
hidden_states,
|
||||
_encode_layer_name(self.k_cache.prefix),
|
||||
self.k_cache.kv_cache,
|
||||
q_quant,
|
||||
k,
|
||||
weights,
|
||||
self.quant_block_size,
|
||||
self.scale_fmt,
|
||||
self.topk_tokens,
|
||||
self.head_dim,
|
||||
self.max_model_len,
|
||||
self.max_total_seq_len,
|
||||
self.topk_indices_buffer,
|
||||
skip_k_cache_insert=self.skip_k_cache_insert,
|
||||
)
|
||||
if rocm_aiter_ops.is_enabled():
|
||||
return torch.ops.vllm.rocm_aiter_sparse_attn_indexer(
|
||||
hidden_states,
|
||||
@@ -522,8 +540,4 @@ class SparseAttnIndexer(CustomOp):
|
||||
self.max_total_seq_len,
|
||||
self.topk_indices_buffer,
|
||||
)
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"Sparse attention indexer ROCm custom op requires ROCm "
|
||||
"Aiter ops to be enabled."
|
||||
)
|
||||
raise RuntimeError("Sparse attention indexer ROCm path could not be selected.")
|
||||
|
||||
@@ -1245,7 +1245,12 @@ class DeepseekV4Model(nn.Module):
|
||||
# DeepseekV4MultiHeadLatentAttentionWrapper.attn_gemm_parallel_execute
|
||||
# (compressor kv_score, indexer.weights_proj, indexer.compressor
|
||||
# kv_score). fused_wqa_wkv stays on the default stream.
|
||||
aux_stream_list = [torch.cuda.Stream() for _ in range(3)]
|
||||
# Disable them on ROCm because of hang issues.
|
||||
aux_stream_list = (
|
||||
None
|
||||
if current_platform.is_rocm()
|
||||
else [torch.cuda.Stream() for _ in range(3)]
|
||||
)
|
||||
|
||||
self.device = current_platform.device_type
|
||||
# Reserved topk indices buffer for all Indexer layers to reuse.
|
||||
|
||||
@@ -167,8 +167,12 @@ class DeepSeekV4MultiTokenPredictor(nn.Module):
|
||||
)
|
||||
|
||||
# Three aux streams shared across all MTP layers, mirroring
|
||||
# DeepseekV4Model.
|
||||
aux_stream_list = [torch.cuda.Stream() for _ in range(3)]
|
||||
# DeepseekV4Model. ROCm runs the same work serially for now.
|
||||
aux_stream_list = (
|
||||
None
|
||||
if current_platform.is_rocm()
|
||||
else [torch.cuda.Stream() for _ in range(3)]
|
||||
)
|
||||
|
||||
# to map the exact layer index from weights
|
||||
self.layers = torch.nn.ModuleDict(
|
||||
|
||||
@@ -0,0 +1,602 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Inference-only Gemma4 MTP (Multi-Token Prediction) model.
|
||||
|
||||
The Gemma4 assistant model is a lightweight decoder that shares KV cache
|
||||
with the target (backbone) model. All assistant decoder layers are
|
||||
KV-shared: they only have Q projections (no K/V projections or norms),
|
||||
and read K/V from the target model's cache at runtime.
|
||||
|
||||
Checkpoint layout (``gemma4_assistant``)::
|
||||
|
||||
model.embed_tokens.* -- token embeddings
|
||||
model.layers.{i}.* -- decoder layers (Q-only attention + MLP)
|
||||
model.norm.* -- final RMSNorm
|
||||
pre_projection.* -- Linear(2 * backbone_hidden_size, hidden_size)
|
||||
post_projection.* -- Linear(hidden_size, backbone_hidden_size)
|
||||
lm_head.* -- language model head (tied to embed_tokens)
|
||||
masked_embedding.centroids.* -- centroid projection (when use_ordered_embeddings)
|
||||
masked_embedding.token_ordering -- token-to-centroid mapping buffer
|
||||
"""
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from vllm.compilation.decorators import support_torch_compile
|
||||
from vllm.config import CacheConfig, VllmConfig
|
||||
from vllm.distributed import (
|
||||
get_tensor_model_parallel_world_size,
|
||||
tensor_model_parallel_all_gather,
|
||||
)
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention import Attention
|
||||
from vllm.model_executor.layers.layernorm import RMSNorm
|
||||
from vllm.model_executor.layers.linear import (
|
||||
ColumnParallelLinear,
|
||||
RowParallelLinear,
|
||||
)
|
||||
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
||||
from vllm.model_executor.layers.quantization import QuantizationConfig
|
||||
from vllm.model_executor.layers.rotary_embedding import get_rope
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
||||
ParallelLMHead,
|
||||
VocabParallelEmbedding,
|
||||
)
|
||||
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
from .gemma4 import Gemma4MLP, _get_text_config
|
||||
from .utils import (
|
||||
AutoWeightsLoader,
|
||||
WeightsMapper,
|
||||
extract_layer_index,
|
||||
maybe_prefix,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class Gemma4MTPMaskedEmbedder(nn.Module):
|
||||
"""Sparse logit computation via centroid-based vocabulary masking.
|
||||
|
||||
Instead of computing logits against the full vocabulary, projects
|
||||
hidden states to centroid scores, selects top-K centroids, and
|
||||
computes logits only for the ~top_k * (vocab_size / num_centroids)
|
||||
tokens belonging to those centroids.
|
||||
"""
|
||||
|
||||
token_ordering: torch.Tensor
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hidden_size: int,
|
||||
vocab_size: int,
|
||||
num_centroids: int,
|
||||
centroid_intermediate_top_k: int,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.hidden_size = hidden_size
|
||||
self.vocab_size = vocab_size
|
||||
self.num_centroids = num_centroids
|
||||
self.centroid_intermediate_top_k = centroid_intermediate_top_k
|
||||
self.vocab_size_per_centroid = vocab_size // num_centroids
|
||||
self.num_selected = centroid_intermediate_top_k * self.vocab_size_per_centroid
|
||||
|
||||
self.centroids = nn.Linear(hidden_size, num_centroids, bias=False)
|
||||
self.register_buffer(
|
||||
"token_ordering",
|
||||
torch.empty(vocab_size, dtype=torch.long),
|
||||
)
|
||||
|
||||
def _select_and_score(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
lm_head_weight: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Centroid selection + sparse dot product.
|
||||
|
||||
Returns:
|
||||
logits: (num_tokens, num_selected) sparse logits.
|
||||
indices: (num_tokens, num_selected) corresponding vocab indices.
|
||||
"""
|
||||
num_tokens = hidden_states.shape[0]
|
||||
_, top_k_indices = torch.topk(
|
||||
self.centroids(hidden_states),
|
||||
k=self.centroid_intermediate_top_k,
|
||||
dim=-1,
|
||||
)
|
||||
clusters = self.token_ordering.view(
|
||||
self.num_centroids,
|
||||
self.vocab_size_per_centroid,
|
||||
)
|
||||
selected = clusters[top_k_indices]
|
||||
embeddings = lm_head_weight[selected.reshape(-1)].view(
|
||||
num_tokens,
|
||||
self.num_selected,
|
||||
self.hidden_size,
|
||||
)
|
||||
logits = torch.einsum("td,tsd->ts", hidden_states, embeddings)
|
||||
return logits, selected.view(num_tokens, -1)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
lm_head_weight: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""Full-vocab logits with non-selected positions masked to -inf."""
|
||||
logits, indices = self._select_and_score(hidden_states, lm_head_weight)
|
||||
output = torch.full(
|
||||
(hidden_states.shape[0], self.vocab_size),
|
||||
fill_value=torch.finfo(hidden_states.dtype).min,
|
||||
dtype=hidden_states.dtype,
|
||||
device=hidden_states.device,
|
||||
)
|
||||
return output.scatter_(-1, indices, logits)
|
||||
|
||||
def get_top_tokens(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
lm_head_weight: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""Sparse argmax — returns vocab token IDs without full-vocab tensor."""
|
||||
logits, indices = self._select_and_score(hidden_states, lm_head_weight)
|
||||
return indices.gather(-1, logits.argmax(-1, keepdim=True)).squeeze(-1)
|
||||
|
||||
|
||||
class Gemma4MTPAttention(nn.Module):
|
||||
"""Q-only attention for Gemma4 MTP layers.
|
||||
|
||||
K/V come from the target model's KV cache via
|
||||
``kv_sharing_target_layer_name`` (set by the proposer after
|
||||
model construction).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config,
|
||||
hidden_size: int,
|
||||
num_heads: int,
|
||||
num_kv_heads: int,
|
||||
head_dim: int,
|
||||
max_position_embeddings: int,
|
||||
cache_config: CacheConfig | None = None,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
attn_logits_soft_cap: float | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.config = config
|
||||
self.hidden_size = hidden_size
|
||||
|
||||
tp_size = get_tensor_model_parallel_world_size()
|
||||
self.total_num_heads = num_heads
|
||||
self.num_heads = self.total_num_heads // tp_size
|
||||
self.total_num_kv_heads = num_kv_heads
|
||||
self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size)
|
||||
self.head_dim = head_dim
|
||||
self.q_size = self.num_heads * self.head_dim
|
||||
self.scaling = 1.0
|
||||
|
||||
self.q_proj = ColumnParallelLinear(
|
||||
hidden_size,
|
||||
self.total_num_heads * self.head_dim,
|
||||
bias=config.attention_bias,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.q_proj",
|
||||
)
|
||||
self.o_proj = RowParallelLinear(
|
||||
self.total_num_heads * self.head_dim,
|
||||
hidden_size,
|
||||
bias=config.attention_bias,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.o_proj",
|
||||
)
|
||||
self.q_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
||||
|
||||
layer_idx = extract_layer_index(prefix)
|
||||
layer_type = config.layer_types[layer_idx]
|
||||
self.is_sliding = layer_type == "sliding_attention"
|
||||
sliding_window = config.sliding_window if self.is_sliding else None
|
||||
|
||||
if layer_type in config.rope_parameters:
|
||||
rope_parameters = dict(config.rope_parameters[layer_type])
|
||||
else:
|
||||
rope_parameters = dict(config.rope_parameters.copy())
|
||||
if self.is_sliding:
|
||||
rope_parameters["rope_theta"] = getattr(
|
||||
config, "rope_local_base_freq", 10000.0
|
||||
)
|
||||
|
||||
self.rotary_emb = get_rope(
|
||||
self.head_dim,
|
||||
max_position=max_position_embeddings,
|
||||
rope_parameters=rope_parameters,
|
||||
is_neox_style=True,
|
||||
)
|
||||
|
||||
# kv_sharing_target_layer_name is set after model construction
|
||||
# by Gemma4Proposer._setup_gemma4_kv_sharing().
|
||||
self.is_kv_shared_layer = True
|
||||
self.attn = Attention(
|
||||
self.num_heads,
|
||||
self.head_dim,
|
||||
self.scaling,
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
cache_config=cache_config,
|
||||
quant_config=quant_config,
|
||||
logits_soft_cap=attn_logits_soft_cap,
|
||||
per_layer_sliding_window=sliding_window,
|
||||
prefix=f"{prefix}.attn",
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
**kwargs,
|
||||
) -> torch.Tensor:
|
||||
q, _ = self.q_proj(hidden_states)
|
||||
|
||||
q = q.unflatten(-1, (self.num_heads, self.head_dim))
|
||||
q = self.q_norm(q)
|
||||
q = q.flatten(-2, -1)
|
||||
|
||||
q, _ = self.rotary_emb(positions, q, None)
|
||||
|
||||
# Attention reads K/V from the target's cache via KV sharing;
|
||||
# these dummy tensors are never consumed but required by the API.
|
||||
num_tokens = q.shape[0]
|
||||
kv_dummy = torch.empty(
|
||||
num_tokens,
|
||||
self.num_kv_heads * self.head_dim,
|
||||
dtype=q.dtype,
|
||||
device=q.device,
|
||||
)
|
||||
attn_output = self.attn(q, kv_dummy, kv_dummy)
|
||||
output, _ = self.o_proj(attn_output)
|
||||
return output
|
||||
|
||||
|
||||
class Gemma4MTPDecoderLayer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
config,
|
||||
cache_config: CacheConfig | None = None,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.hidden_size = config.hidden_size
|
||||
|
||||
layer_idx = extract_layer_index(prefix)
|
||||
layer_type = config.layer_types[layer_idx]
|
||||
is_full_attention = layer_type == "full_attention"
|
||||
head_dim = (
|
||||
getattr(config, "global_head_dim", config.head_dim)
|
||||
if is_full_attention
|
||||
else config.head_dim
|
||||
)
|
||||
|
||||
self.self_attn = Gemma4MTPAttention(
|
||||
config=config,
|
||||
hidden_size=self.hidden_size,
|
||||
num_heads=config.num_attention_heads,
|
||||
num_kv_heads=config.num_key_value_heads,
|
||||
head_dim=head_dim,
|
||||
max_position_embeddings=config.max_position_embeddings,
|
||||
cache_config=cache_config,
|
||||
quant_config=quant_config,
|
||||
attn_logits_soft_cap=getattr(config, "attn_logit_softcapping", None),
|
||||
prefix=f"{prefix}.self_attn",
|
||||
)
|
||||
|
||||
self.mlp = Gemma4MLP(
|
||||
hidden_size=self.hidden_size,
|
||||
intermediate_size=config.intermediate_size,
|
||||
hidden_activation=config.hidden_activation,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.mlp",
|
||||
)
|
||||
|
||||
self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
||||
self.post_attention_layernorm = RMSNorm(
|
||||
config.hidden_size, eps=config.rms_norm_eps
|
||||
)
|
||||
self.pre_feedforward_layernorm = RMSNorm(
|
||||
config.hidden_size, eps=config.rms_norm_eps
|
||||
)
|
||||
self.post_feedforward_layernorm = RMSNorm(
|
||||
config.hidden_size, eps=config.rms_norm_eps
|
||||
)
|
||||
|
||||
self.register_buffer("layer_scalar", torch.ones(1))
|
||||
|
||||
def forward(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
residual: torch.Tensor | None,
|
||||
**kwargs,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
residual = hidden_states
|
||||
hidden_states = self.input_layernorm(residual)
|
||||
|
||||
hidden_states = self.self_attn(
|
||||
positions=positions,
|
||||
hidden_states=hidden_states,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
hidden_states = self.post_attention_layernorm(hidden_states)
|
||||
hidden_states = hidden_states + residual
|
||||
residual = hidden_states
|
||||
|
||||
hidden_states = self.pre_feedforward_layernorm(hidden_states)
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
|
||||
hidden_states = self.post_feedforward_layernorm(hidden_states)
|
||||
hidden_states = hidden_states + residual
|
||||
|
||||
hidden_states = hidden_states * self.layer_scalar
|
||||
return hidden_states, None
|
||||
|
||||
|
||||
class Gemma4MultiTokenPredictor(nn.Module):
|
||||
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
||||
super().__init__()
|
||||
|
||||
config = vllm_config.speculative_config.draft_model_config.hf_config
|
||||
text_config = _get_text_config(config)
|
||||
self.config = text_config
|
||||
|
||||
self.hidden_size = text_config.hidden_size
|
||||
self.backbone_hidden_size = getattr(
|
||||
config, "backbone_hidden_size", self.hidden_size
|
||||
)
|
||||
self.vocab_size = text_config.vocab_size
|
||||
self.num_mtp_layers = text_config.num_hidden_layers
|
||||
|
||||
self.embed_tokens = VocabParallelEmbedding(
|
||||
self.vocab_size,
|
||||
self.hidden_size,
|
||||
)
|
||||
|
||||
self.pre_projection = ColumnParallelLinear(
|
||||
2 * self.backbone_hidden_size,
|
||||
self.hidden_size,
|
||||
bias=False,
|
||||
gather_output=True,
|
||||
prefix=f"{prefix}.pre_projection",
|
||||
)
|
||||
|
||||
self.post_projection = RowParallelLinear(
|
||||
self.hidden_size,
|
||||
self.backbone_hidden_size,
|
||||
bias=False,
|
||||
input_is_parallel=False,
|
||||
prefix=f"{prefix}.post_projection",
|
||||
)
|
||||
|
||||
self.layers = nn.ModuleList(
|
||||
Gemma4MTPDecoderLayer(
|
||||
text_config,
|
||||
cache_config=vllm_config.cache_config,
|
||||
quant_config=vllm_config.quant_config,
|
||||
prefix=f"{prefix}.layers.{idx}",
|
||||
)
|
||||
for idx in range(self.num_mtp_layers)
|
||||
)
|
||||
|
||||
self.norm = RMSNorm(self.hidden_size, eps=text_config.rms_norm_eps)
|
||||
|
||||
# After embedding sharing, embed_tokens is replaced with the
|
||||
# target model's backbone-dim embedding. Scale by
|
||||
# sqrt(backbone_hidden_size) to match the target's convention.
|
||||
self.register_buffer(
|
||||
"normalizer",
|
||||
torch.tensor(self.backbone_hidden_size**0.5),
|
||||
persistent=False,
|
||||
)
|
||||
|
||||
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
|
||||
return self.embed_tokens(input_ids) * self.normalizer
|
||||
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
|
||||
stacked_params_mapping = [
|
||||
("gate_up_proj", "gate_proj", 0),
|
||||
("gate_up_proj", "up_proj", 1),
|
||||
]
|
||||
|
||||
params_dict = dict(self.named_parameters())
|
||||
params_dict.update(dict(self.named_buffers()))
|
||||
loaded_params: set[str] = set()
|
||||
|
||||
for name, loaded_weight in weights:
|
||||
if "rotary_emb.inv_freq" in name:
|
||||
continue
|
||||
|
||||
for param_name, weight_name, shard_id in stacked_params_mapping:
|
||||
if weight_name not in name:
|
||||
continue
|
||||
name = name.replace(weight_name, param_name)
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
continue
|
||||
if name not in params_dict:
|
||||
continue
|
||||
param = params_dict[name]
|
||||
weight_loader = param.weight_loader
|
||||
weight_loader(param, loaded_weight, shard_id)
|
||||
break
|
||||
else:
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
continue
|
||||
if name not in params_dict:
|
||||
continue
|
||||
param = params_dict[name]
|
||||
weight_loader = getattr(param, "weight_loader", default_weight_loader)
|
||||
weight_loader(param, loaded_weight)
|
||||
loaded_params.add(name)
|
||||
|
||||
return loaded_params
|
||||
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
intermediate_tensors: IntermediateTensors | None = None,
|
||||
inputs_embeds: torch.Tensor | None = None,
|
||||
spec_step_idx: int = 0,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Returns (draft_hidden_states, backbone_hidden_states).
|
||||
|
||||
draft_hidden_states: draft-dim, used by compute_logits via lm_head.
|
||||
backbone_hidden_states: backbone-dim, stored in the proposer's
|
||||
hidden-state buffer and fed back as input to the next step.
|
||||
"""
|
||||
if inputs_embeds is None:
|
||||
inputs_embeds = self.embed_input_ids(input_ids)
|
||||
|
||||
combined = torch.cat([inputs_embeds, hidden_states], dim=-1)
|
||||
hidden_states, _ = self.pre_projection(combined)
|
||||
|
||||
residual = None
|
||||
for layer in self.layers:
|
||||
hidden_states, residual = layer(
|
||||
positions=positions,
|
||||
hidden_states=hidden_states,
|
||||
residual=residual,
|
||||
)
|
||||
|
||||
draft_hidden_states = self.norm(hidden_states)
|
||||
|
||||
backbone_hidden_states, _ = self.post_projection(draft_hidden_states)
|
||||
return draft_hidden_states, backbone_hidden_states
|
||||
|
||||
|
||||
@support_torch_compile
|
||||
class Gemma4MTP(nn.Module):
|
||||
"""Gemma4 Multi-Token Prediction model for speculative decoding.
|
||||
|
||||
forward() returns (draft_hidden_states, backbone_hidden_states).
|
||||
The proposer uses draft_hidden_states for compute_logits (via
|
||||
the draft-dim lm_head) and backbone_hidden_states for the
|
||||
hidden-state feedback buffer.
|
||||
"""
|
||||
|
||||
has_own_lm_head = True
|
||||
|
||||
hf_to_vllm_mapper = WeightsMapper(
|
||||
orig_to_new_prefix={
|
||||
"pre_projection.": "model.pre_projection.",
|
||||
"post_projection.": "model.post_projection.",
|
||||
},
|
||||
)
|
||||
|
||||
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
||||
super().__init__()
|
||||
config = vllm_config.speculative_config.draft_model_config.hf_config
|
||||
text_config = _get_text_config(config)
|
||||
self.config = config
|
||||
|
||||
self.model = Gemma4MultiTokenPredictor(
|
||||
vllm_config=vllm_config,
|
||||
prefix=maybe_prefix(prefix, "model"),
|
||||
)
|
||||
|
||||
# lm_head operates in draft-dim. Tied to embed_tokens at init
|
||||
# so load_weights populates both from a single checkpoint entry.
|
||||
# After embedding sharing, lm_head.weight still references the
|
||||
# original draft-dim tensor.
|
||||
self.lm_head = ParallelLMHead(
|
||||
text_config.vocab_size,
|
||||
text_config.hidden_size,
|
||||
prefix=maybe_prefix(prefix, "lm_head"),
|
||||
)
|
||||
if getattr(config, "tie_word_embeddings", True):
|
||||
self.lm_head.weight = self.model.embed_tokens.weight
|
||||
|
||||
self.logits_processor = LogitsProcessor(
|
||||
text_config.vocab_size,
|
||||
soft_cap=getattr(text_config, "final_logit_softcapping", None),
|
||||
)
|
||||
|
||||
if getattr(config, "use_ordered_embeddings", False):
|
||||
num_centroids = getattr(config, "num_centroids", 2048)
|
||||
top_k = getattr(config, "centroid_intermediate_top_k", 32)
|
||||
self.masked_embedding = Gemma4MTPMaskedEmbedder(
|
||||
hidden_size=text_config.hidden_size,
|
||||
vocab_size=text_config.vocab_size,
|
||||
num_centroids=num_centroids,
|
||||
centroid_intermediate_top_k=top_k,
|
||||
)
|
||||
logger.info(
|
||||
"Gemma4 MTP: centroids masking enabled "
|
||||
"(num_centroids=%d, top_k=%d, active_tokens=%d/%d).",
|
||||
num_centroids,
|
||||
top_k,
|
||||
top_k * (text_config.vocab_size // num_centroids),
|
||||
text_config.vocab_size,
|
||||
)
|
||||
else:
|
||||
self.masked_embedding = None
|
||||
|
||||
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
|
||||
return self.model.embed_input_ids(input_ids)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
input_ids: torch.Tensor | None,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
intermediate_tensors: IntermediateTensors | None = None,
|
||||
inputs_embeds: torch.Tensor | None = None,
|
||||
spec_step_idx: int = 0,
|
||||
**kwargs: object,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
return self.model(
|
||||
input_ids,
|
||||
positions,
|
||||
hidden_states,
|
||||
intermediate_tensors,
|
||||
inputs_embeds,
|
||||
spec_step_idx,
|
||||
)
|
||||
|
||||
def _get_full_lm_head_weight(self) -> torch.Tensor:
|
||||
lm_head_weight = self.lm_head.weight
|
||||
tp_size = get_tensor_model_parallel_world_size()
|
||||
if tp_size > 1:
|
||||
lm_head_weight = tensor_model_parallel_all_gather(
|
||||
lm_head_weight,
|
||||
dim=0,
|
||||
)
|
||||
return lm_head_weight[: self.masked_embedding.vocab_size]
|
||||
|
||||
def compute_logits(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
spec_step_idx: int = 0,
|
||||
) -> torch.Tensor | None:
|
||||
if self.masked_embedding is not None:
|
||||
return self.masked_embedding(
|
||||
hidden_states,
|
||||
self._get_full_lm_head_weight(),
|
||||
)
|
||||
return self.logits_processor(self.lm_head, hidden_states)
|
||||
|
||||
def get_top_tokens(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""Sparse argmax via centroids masking. Returns token IDs directly."""
|
||||
return self.masked_embedding.get_top_tokens(
|
||||
hidden_states,
|
||||
self._get_full_lm_head_weight(),
|
||||
)
|
||||
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
|
||||
loader = AutoWeightsLoader(self)
|
||||
return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper)
|
||||
@@ -601,6 +601,7 @@ _SPECULATIVE_DECODING_MODELS = {
|
||||
"EagleDeepSeekMTPModel": ("deepseek_eagle", "EagleDeepseekV3ForCausalLM"),
|
||||
"DeepSeekMTPModel": ("deepseek_mtp", "DeepSeekMTP"),
|
||||
"DeepSeekV4MTPModel": ("deepseek_v4_mtp", "DeepSeekV4MTP"),
|
||||
"Gemma4MTPModel": ("gemma4_mtp", "Gemma4MTP"),
|
||||
"ErnieMTPModel": ("ernie_mtp", "ErnieMTP"),
|
||||
"ExaoneMoeMTP": ("exaone_moe_mtp", "ExaoneMoeMTP"),
|
||||
"Exaone4_5_MTP": ("exaone4_5_mtp", "Exaone4_5_MTP"),
|
||||
|
||||
@@ -268,28 +268,6 @@ class InputProcessingContext:
|
||||
try:
|
||||
output = hf_processor(**data, **allowed_kwargs)
|
||||
except Exception as exc:
|
||||
# See https://github.com/huggingface/tokenizers/issues/537
|
||||
if (
|
||||
isinstance(exc, RuntimeError)
|
||||
and exc
|
||||
and exc.args[0] == "Already borrowed"
|
||||
and num_tries < max_tries
|
||||
):
|
||||
logger.warning(
|
||||
"Failed to acquire tokenizer in current thread. "
|
||||
"Retrying (%d/%d)...",
|
||||
num_tries,
|
||||
max_tries,
|
||||
)
|
||||
time.sleep(0.5)
|
||||
return self.call_hf_processor(
|
||||
hf_processor,
|
||||
data,
|
||||
kwargs,
|
||||
num_tries=num_tries + 1,
|
||||
max_tries=max_tries,
|
||||
)
|
||||
|
||||
msg = (
|
||||
f"Failed to apply {type(hf_processor).__name__} "
|
||||
f"on data={data} with kwargs={allowed_kwargs}"
|
||||
|
||||
@@ -409,6 +409,7 @@ class RocmPlatform(Platform):
|
||||
"gptq",
|
||||
"gptq_marlin", # will be overwritten with gptq
|
||||
"fp8",
|
||||
"deepseek_v4_fp8",
|
||||
"compressed-tensors",
|
||||
"fbgemm_fp8",
|
||||
"gguf",
|
||||
|
||||
+2
-11
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import asyncio
|
||||
import copy
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Mapping, Sequence
|
||||
@@ -108,17 +107,10 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
if mm_registry.supports_multimodal_inputs(config.model_config):
|
||||
mm_processor_cache = mm_registry.processor_cache_from_config(config)
|
||||
|
||||
# Deep-copy the tokenizer so the multimodal processor gets its
|
||||
# own Rust tokenizer backend. Without this, concurrent access
|
||||
# from AsyncMicrobatchTokenizer and call_hf_processor causes
|
||||
# "RuntimeError: Already borrowed" from the Rust RefCell.
|
||||
# See: https://github.com/huggingface/tokenizers/issues/537
|
||||
mm_tokenizer = copy.deepcopy(tokenizer)
|
||||
|
||||
with set_default_torch_num_threads():
|
||||
self.mm_processor = mm_registry.create_processor(
|
||||
config.model_config,
|
||||
tokenizer=mm_tokenizer,
|
||||
tokenizer=self.tokenizer,
|
||||
cache=mm_processor_cache,
|
||||
)
|
||||
|
||||
@@ -130,11 +122,10 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
# requests don't pollute the sender cache.
|
||||
ro_cache = mm_registry.processor_only_cache_from_config(config)
|
||||
if ro_cache is not None:
|
||||
ro_tokenizer = copy.deepcopy(tokenizer)
|
||||
with set_default_torch_num_threads():
|
||||
self._readonly_mm_processor = mm_registry.create_processor(
|
||||
config.model_config,
|
||||
tokenizer=ro_tokenizer,
|
||||
tokenizer=self.tokenizer,
|
||||
cache=ro_cache,
|
||||
)
|
||||
|
||||
|
||||
+15
-1
@@ -2,6 +2,7 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import inspect
|
||||
import itertools
|
||||
import weakref
|
||||
@@ -42,7 +43,7 @@ from vllm.multimodal.processing.processor import (
|
||||
apply_token_matches,
|
||||
find_mm_placeholders,
|
||||
)
|
||||
from vllm.tokenizers.hf import HfTokenizer
|
||||
from vllm.tokenizers.hf import HfTokenizer, maybe_make_thread_pool
|
||||
from vllm.transformers_utils.chat_templates import get_chat_template_fallback_path
|
||||
from vllm.transformers_utils.processor import cached_get_processor
|
||||
from vllm.utils.async_utils import make_async
|
||||
@@ -785,6 +786,14 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
config: VllmConfig,
|
||||
tokenizer: HfTokenizer | None,
|
||||
) -> None:
|
||||
# Ensure the og tokenizer is never modified by maybe_make_thread_pool
|
||||
tokenizer = copy.copy(tokenizer)
|
||||
if (
|
||||
# Skip for mock configs and tokenizers
|
||||
getattr(config.model_config, "enable_prompt_embeds", False)
|
||||
and isinstance(tokenizer, HfTokenizer)
|
||||
):
|
||||
_ensure_prompt_embeds_placeholder_token(tokenizer)
|
||||
super().__init__(config, tokenizer)
|
||||
|
||||
self.use_unified_vision_chunk = getattr(
|
||||
@@ -795,6 +804,11 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
safe_apply_chat_template, executor=self._executor
|
||||
)
|
||||
|
||||
if self.tokenizer is not None:
|
||||
maybe_make_thread_pool(
|
||||
self.tokenizer, config.model_config.renderer_num_workers + 1
|
||||
)
|
||||
|
||||
def render_messages(
|
||||
self,
|
||||
messages: list[ChatCompletionMessageParam],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from .hf import maybe_make_thread_pool
|
||||
from .protocol import TokenizerLike
|
||||
from .registry import (
|
||||
TokenizerRegistry,
|
||||
@@ -15,4 +16,5 @@ __all__ = [
|
||||
"cached_get_tokenizer",
|
||||
"get_tokenizer",
|
||||
"cached_tokenizer_from_config",
|
||||
"maybe_make_thread_pool",
|
||||
]
|
||||
|
||||
+92
-2
@@ -2,8 +2,9 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import contextlib
|
||||
import copy
|
||||
import queue
|
||||
from pathlib import Path
|
||||
from typing import TypeAlias
|
||||
from typing import TypeAlias, TypeVar
|
||||
|
||||
from transformers import AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast
|
||||
|
||||
@@ -12,6 +13,92 @@ from vllm.transformers_utils.config import get_sentence_transformer_tokenizer_co
|
||||
from .protocol import TokenizerLike
|
||||
|
||||
HfTokenizer: TypeAlias = PreTrainedTokenizer | PreTrainedTokenizerFast
|
||||
_T = TypeVar("_T", bound=TokenizerLike)
|
||||
|
||||
|
||||
class ThreadSafeHFTokenizerMixin:
|
||||
"""Mixin class for thread-safe HF fast tokenizers."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def maybe_make_thread_pool(tokenizer: _T, copies: int = 1):
|
||||
"""
|
||||
If `tokenizer` is a `PreTrainedTokenizerFast`, modify the tokenizer
|
||||
in-place to make the public interface thread-safe by routing calls
|
||||
through a deep-copied tokenizer pool.
|
||||
|
||||
Note that:
|
||||
- Only ``TokenizerLike``'s public interface is thread-safe.
|
||||
This doesn't include ``_tokenizer`` property nor any mutation
|
||||
methods like ``add_special_tokens`` or ``add_tokens``.
|
||||
- Adjacent method calls could happen on different deep copies.
|
||||
"""
|
||||
if not isinstance(tokenizer, PreTrainedTokenizerFast) or isinstance(
|
||||
tokenizer, ThreadSafeHFTokenizerMixin
|
||||
):
|
||||
return tokenizer
|
||||
|
||||
og_tokenizer = copy.copy(tokenizer)
|
||||
|
||||
tokenizer_pool: queue.Queue[PreTrainedTokenizerFast] = queue.Queue()
|
||||
for _ in range(copies):
|
||||
tokenizer_pool.put(copy.deepcopy(og_tokenizer))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def _borrow_from_pool():
|
||||
try:
|
||||
tok = tokenizer_pool.get_nowait()
|
||||
yield tok
|
||||
except queue.Empty:
|
||||
tok = copy.deepcopy(og_tokenizer)
|
||||
yield tok
|
||||
finally:
|
||||
tokenizer_pool.put(tok)
|
||||
|
||||
class TokenizerPool(tokenizer.__class__, ThreadSafeHFTokenizerMixin): # type: ignore
|
||||
def apply_chat_template(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.apply_chat_template(*args, **kwargs)
|
||||
|
||||
def batch_decode(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.batch_decode(*args, **kwargs)
|
||||
|
||||
def batch_encode(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.batch_encode(*args, **kwargs)
|
||||
|
||||
def convert_tokens_to_ids(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.convert_tokens_to_ids(*args, **kwargs)
|
||||
|
||||
def convert_ids_to_tokens(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.convert_ids_to_tokens(*args, **kwargs)
|
||||
|
||||
def convert_tokens_to_string(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.convert_tokens_to_string(*args, **kwargs)
|
||||
|
||||
def decode(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.decode(*args, **kwargs)
|
||||
|
||||
def encode(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok.encode(*args, **kwargs)
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
with _borrow_from_pool() as tok:
|
||||
return tok(*args, **kwargs)
|
||||
|
||||
def __reduce__(self):
|
||||
return maybe_make_thread_pool, (og_tokenizer, copies)
|
||||
|
||||
TokenizerPool.__name__ = f"TokenizerPool{og_tokenizer.__class__.__name__}"
|
||||
|
||||
tokenizer.__class__ = TokenizerPool
|
||||
|
||||
|
||||
def get_cached_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
||||
@@ -103,7 +190,10 @@ class CachedHfTokenizer(TokenizerLike):
|
||||
"is a custom tokenizer not yet available in the "
|
||||
"HuggingFace transformers library, consider "
|
||||
"setting `trust_remote_code=True` in LLM or using "
|
||||
"the `--trust-remote-code` flag in the CLI."
|
||||
"the `--trust-remote-code` flag in the CLI. If the "
|
||||
"model was created with a newer version of "
|
||||
"transformers, consider upgrading: "
|
||||
"`uv pip install --upgrade transformers`"
|
||||
)
|
||||
raise RuntimeError(err_msg) from e
|
||||
else:
|
||||
|
||||
@@ -512,6 +512,17 @@ class LongCatFlashMTPModelArchConfigConvertor(ModelArchConfigConvertorBase):
|
||||
return getattr(self.hf_text_config, "num_nextn_predict_layers", 1)
|
||||
|
||||
|
||||
class Gemma4MTPModelArchConfigConvertor(ModelArchConfigConvertorBase):
|
||||
def get_hidden_size(self) -> int:
|
||||
# The speculator buffer must match the backbone (target) model's
|
||||
# hidden dimension, not the draft model's smaller dimension.
|
||||
return getattr(self.hf_config, "backbone_hidden_size",
|
||||
super().get_hidden_size())
|
||||
|
||||
def get_num_hidden_layers(self) -> int:
|
||||
return getattr(self.hf_text_config, "num_hidden_layers", 0)
|
||||
|
||||
|
||||
class Gemma4ModelArchConfigConvertor(ModelArchConfigConvertorBase):
|
||||
def is_mm_prefix_lm(self) -> bool:
|
||||
return (
|
||||
@@ -541,6 +552,7 @@ MODEL_ARCH_CONFIG_CONVERTORS = {
|
||||
"falcon": FalconModelArchConfigConvertor,
|
||||
"gemma4": Gemma4ModelArchConfigConvertor,
|
||||
"gemma4_text": Gemma4ModelArchConfigConvertor,
|
||||
"gemma4_mtp": Gemma4MTPModelArchConfigConvertor,
|
||||
"RefinedWeb": FalconModelArchConfigConvertor,
|
||||
"RefinedWebModel": FalconModelArchConfigConvertor,
|
||||
"nemotron-nas": NemotronNasModelArchConfigConvertor,
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from functools import cache
|
||||
|
||||
@@ -78,7 +78,7 @@ def parse_id_list(raw_str: str) -> list[int]:
|
||||
|
||||
|
||||
def get_memory_node_info(node_id: int = 0) -> MemoryNodeInfo:
|
||||
if platform.system() == "Darwin":
|
||||
if sys.platform == "darwin":
|
||||
# MacOS has no memory node
|
||||
return MemoryNodeInfo(
|
||||
total_memory=psutil.virtual_memory().total,
|
||||
@@ -122,17 +122,14 @@ def get_memory_node_info(node_id: int = 0) -> MemoryNodeInfo:
|
||||
|
||||
def get_allowed_cpu_list() -> list[LogicalCPUInfo]:
|
||||
cpu_list = _get_cpu_list()
|
||||
if platform.system() == "Darwin":
|
||||
return cpu_list
|
||||
|
||||
global_allowed_cpu_id_list = os.sched_getaffinity(0) # type: ignore[attr-defined]
|
||||
logical_cpu_list = [x for x in cpu_list if x.id in global_allowed_cpu_id_list]
|
||||
|
||||
return logical_cpu_list
|
||||
if sys.platform == "linux":
|
||||
allowed = os.sched_getaffinity(0)
|
||||
return [x for x in cpu_list if x.id in allowed]
|
||||
return cpu_list
|
||||
|
||||
|
||||
def get_visible_memory_node() -> list[int]:
|
||||
if platform.system() == "Darwin":
|
||||
if sys.platform == "darwin":
|
||||
return [0]
|
||||
|
||||
allowed_memory_node_list = get_memory_affinity()
|
||||
@@ -163,7 +160,7 @@ def _synthesize_cpu_list() -> list[LogicalCPUInfo]:
|
||||
|
||||
|
||||
def _get_cpu_list() -> list[LogicalCPUInfo]:
|
||||
if platform.system() == "Darwin":
|
||||
if sys.platform == "darwin":
|
||||
# For MacOS, no user-level CPU affinity and SMT, return all CPUs
|
||||
return _synthesize_cpu_list()
|
||||
|
||||
|
||||
@@ -115,22 +115,29 @@ def get_flash_attn_version(
|
||||
)
|
||||
fa_version = 2
|
||||
|
||||
# The FA3 kernel rejects s_aux (sinks) when hdim != hdim_v; upgrade to
|
||||
# FA4 on SM90 when available.
|
||||
# Some FA3 unsupported SM90 cases can use FA4 when available.
|
||||
if (
|
||||
fa_version == 3
|
||||
and has_sinks
|
||||
and head_size is not None
|
||||
and head_size_v is not None
|
||||
and head_size != head_size_v
|
||||
and device_capability.major == 9
|
||||
and is_fa_version_supported(4)
|
||||
):
|
||||
logger.info_once(
|
||||
"Diff-KV with sinks: upgrading FlashAttention 3 -> 4",
|
||||
scope="local",
|
||||
)
|
||||
fa_version = 4
|
||||
upgrade_reason = None
|
||||
if head_size is not None and head_size > 256:
|
||||
upgrade_reason = f"FA3 does not support head_size={head_size} on SM90"
|
||||
elif (
|
||||
has_sinks
|
||||
and head_size is not None
|
||||
and head_size_v is not None
|
||||
and head_size != head_size_v
|
||||
):
|
||||
upgrade_reason = "Diff-KV with sinks"
|
||||
if upgrade_reason:
|
||||
logger.info_once(
|
||||
"%s: upgrading FlashAttention 3 -> 4",
|
||||
upgrade_reason,
|
||||
scope="local",
|
||||
)
|
||||
fa_version = 4
|
||||
|
||||
# FA4 currently uses batch-shape-dependent scheduling
|
||||
# heuristics on SM100+, which breaks batch invariance.
|
||||
|
||||
@@ -638,14 +638,6 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
requires_alibi=alibi_slopes is not None,
|
||||
head_size=head_size,
|
||||
)
|
||||
# head_size > 256 requires FA4 on SM90+; force upgrade from FA3
|
||||
if (
|
||||
head_size > 256
|
||||
and self.vllm_flash_attn_version == 3
|
||||
and current_platform.is_cuda()
|
||||
and current_platform.is_device_capability_family(90)
|
||||
):
|
||||
self.vllm_flash_attn_version = 4
|
||||
logger.info_once(
|
||||
"Using FlashAttention version %s",
|
||||
self.vllm_flash_attn_version,
|
||||
|
||||
@@ -7,6 +7,7 @@ import torch
|
||||
|
||||
from vllm.config import CacheConfig, VllmConfig, get_current_vllm_config
|
||||
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.v1.attention.backend import (
|
||||
AttentionBackend,
|
||||
@@ -360,7 +361,7 @@ class DeepseekSparseSWAMetadataBuilder(AttentionMetadataBuilder):
|
||||
_LAYER_TYPE_C4A: None,
|
||||
_LAYER_TYPE_C128A: None,
|
||||
}
|
||||
if num_decode_tokens == 0:
|
||||
if num_decode_tokens == 0 or current_platform.is_rocm():
|
||||
return out
|
||||
for layer_type in self._layer_types:
|
||||
# get_mla_metadata() is the official FlashMLA entry point that
|
||||
|
||||
@@ -9,6 +9,7 @@ INT32-packed UE8M0 on SM100) so fp8_einsum skips transform_sf_into_required_layo
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.torch_utils import direct_register_custom_op
|
||||
|
||||
@@ -242,6 +243,7 @@ def _fused_inv_rope_fp8_quant_kernel_impl(
|
||||
(scale_inner * tma_aligned_T, 1, tma_aligned_T),
|
||||
)
|
||||
grid = (tma_aligned_T, n_groups * heads_per_group)
|
||||
pdl_kwargs = {} if current_platform.is_rocm() else {"launch_pdl": False}
|
||||
_fused_inv_rope_fp8_quant_per_head[grid](
|
||||
o,
|
||||
positions,
|
||||
@@ -265,7 +267,7 @@ def _fused_inv_rope_fp8_quant_kernel_impl(
|
||||
HALF_ROPE=half_rope,
|
||||
TMA_ALIGNED_SCALES=tma_aligned_scales,
|
||||
num_stages=1,
|
||||
launch_pdl=False,
|
||||
**pdl_kwargs,
|
||||
num_warps=1,
|
||||
)
|
||||
return fp8_buf, scale_buf
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import functools
|
||||
import importlib
|
||||
import math
|
||||
from importlib.util import find_spec
|
||||
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
from vllm.forward_context import get_forward_context
|
||||
from vllm.platforms import current_platform
|
||||
@@ -13,6 +15,11 @@ from vllm.utils.torch_utils import LayerNameType
|
||||
from vllm.v1.attention.backends.mla.indexer import DeepseekV32IndexerMetadata
|
||||
from vllm.v1.attention.ops.common import pack_seq_triton, unpack_seq_triton
|
||||
|
||||
if current_platform.is_rocm():
|
||||
from vllm.platforms.rocm import _ON_GFX942
|
||||
else:
|
||||
_ON_GFX942 = False
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _indexer_k_quant_and_cache_kernel(
|
||||
@@ -230,6 +237,43 @@ def fp8_paged_mqa_logits_torch(
|
||||
|
||||
fp8_dtype = current_platform.fp8_dtype()
|
||||
batch_size, next_n, _, dim = q.size()
|
||||
if next_n == 1:
|
||||
block_size = kv_cache.shape[1]
|
||||
logits = torch.full(
|
||||
[batch_size, max_model_len],
|
||||
float("-inf"),
|
||||
device=q.device,
|
||||
dtype=torch.float32,
|
||||
)
|
||||
if context_lens.dim() > 1:
|
||||
context_lens = context_lens.squeeze(-1)
|
||||
kv_cache_flat = kv_cache.view(-1, block_size * (dim + 4))
|
||||
for i in range(batch_size):
|
||||
q_i = q[i, 0].to(torch.float32)
|
||||
q_scale = weights[i]
|
||||
seq_len = int(context_lens[i].item())
|
||||
assert seq_len <= max_model_len
|
||||
num_pages = cdiv(seq_len, block_size)
|
||||
padded_seq_len = num_pages * block_size
|
||||
pages = block_tables[i, :num_pages]
|
||||
cache = kv_cache_flat[pages]
|
||||
scale_offset = block_size * dim
|
||||
cache_value = (
|
||||
cache[..., :scale_offset].view(dtype=fp8_dtype).to(torch.float32)
|
||||
)
|
||||
cache_scale = (
|
||||
cache[..., scale_offset:].view(dtype=torch.float32).contiguous()
|
||||
)
|
||||
cache_value = cache_value.view(padded_seq_len, dim)
|
||||
cache_scale = cache_scale.view(padded_seq_len)
|
||||
score = F.linear(cache_value, q_i)
|
||||
score = F.relu(score)
|
||||
score *= q_scale[None, :]
|
||||
score = score.sum(dim=1)
|
||||
score *= cache_scale
|
||||
logits[i, :seq_len] = score[:seq_len]
|
||||
return logits
|
||||
|
||||
kv_cache, scale = kv_cache[..., :dim], kv_cache[..., dim:]
|
||||
scale = scale.contiguous().view(torch.float)
|
||||
q = q.float()
|
||||
@@ -241,20 +285,30 @@ def fp8_paged_mqa_logits_torch(
|
||||
device=q.device,
|
||||
dtype=torch.float32,
|
||||
)
|
||||
context_lens = context_lens.tolist()
|
||||
for i in range(batch_size):
|
||||
context_len = context_lens[i]
|
||||
q_offsets = torch.arange(context_len - next_n, context_len, device="cuda")
|
||||
if context_len.ndim == 0:
|
||||
context_len_i = int(context_len.item())
|
||||
q_offsets = torch.arange(
|
||||
context_len_i - next_n, context_len_i, device=q.device
|
||||
)
|
||||
context_limit = torch.full(
|
||||
(next_n,), context_len_i, dtype=torch.int32, device=q.device
|
||||
)
|
||||
else:
|
||||
context_limit = context_len.to(device=q.device, dtype=torch.int32)
|
||||
q_offsets = context_limit - 1
|
||||
weight_slice = (
|
||||
weights[i * next_n : (i + 1) * next_n, :].transpose(0, 1).contiguous()
|
||||
)
|
||||
for block_rk in range(cdiv(context_len, block_size)):
|
||||
max_context_len = int(context_limit.max().item())
|
||||
for block_rk in range(cdiv(max_context_len, block_size)):
|
||||
block_idx = block_tables[i][block_rk]
|
||||
qx, kx = q[i], kv_cache[block_idx]
|
||||
k_offsets = torch.arange(
|
||||
block_rk * block_size, (block_rk + 1) * block_size, device="cuda"
|
||||
block_rk * block_size, (block_rk + 1) * block_size, device=q.device
|
||||
)
|
||||
mask = (k_offsets[None, :] < context_len) & (
|
||||
mask = (k_offsets[None, :] < context_limit[:, None]) & (
|
||||
k_offsets[None, :] <= q_offsets[:, None]
|
||||
)
|
||||
s = torch.where(
|
||||
@@ -331,30 +385,52 @@ def rocm_fp8_paged_mqa_logits(
|
||||
aiter_paged_mqa_logits_module = paged_mqa_logits_module()
|
||||
|
||||
if aiter_paged_mqa_logits_module is not None:
|
||||
deepgemm_fp8_paged_mqa_logits = (
|
||||
aiter_paged_mqa_logits_module.deepgemm_fp8_paged_mqa_logits
|
||||
if _ON_GFX942:
|
||||
deepgemm_fp8_paged_mqa_logits = (
|
||||
aiter_paged_mqa_logits_module.deepgemm_fp8_paged_mqa_logits
|
||||
)
|
||||
batch_size, next_n, heads, _ = q_fp8.shape
|
||||
out_logits = torch.full(
|
||||
[batch_size * next_n, max_model_len],
|
||||
float("-inf"),
|
||||
device="cuda",
|
||||
dtype=torch.float32,
|
||||
)
|
||||
deepgemm_fp8_paged_mqa_logits(
|
||||
q_fp8,
|
||||
kv_cache_fp8,
|
||||
weights,
|
||||
out_logits,
|
||||
context_lens,
|
||||
block_tables,
|
||||
max_model_len,
|
||||
ChunkK=256,
|
||||
Preshuffle=block_size == 64,
|
||||
KVBlockSize=block_size,
|
||||
WavePerEU=2,
|
||||
)
|
||||
return out_logits
|
||||
deepgemm_fp8_paged_mqa_logits_stage1 = (
|
||||
aiter_paged_mqa_logits_module.deepgemm_fp8_paged_mqa_logits_stage1
|
||||
)
|
||||
batch_size, next_n, heads, _ = q_fp8.shape
|
||||
out_logits = torch.full(
|
||||
[batch_size * next_n, max_model_len],
|
||||
out_qk = torch.full(
|
||||
(heads, batch_size * next_n, max_model_len),
|
||||
float("-inf"),
|
||||
device="cuda",
|
||||
dtype=torch.float32,
|
||||
)
|
||||
deepgemm_fp8_paged_mqa_logits(
|
||||
deepgemm_fp8_paged_mqa_logits_stage1(
|
||||
q_fp8,
|
||||
kv_cache_fp8,
|
||||
weights,
|
||||
out_logits,
|
||||
out_qk,
|
||||
context_lens,
|
||||
block_tables,
|
||||
max_model_len,
|
||||
ChunkK=256,
|
||||
Preshuffle=block_size == 64,
|
||||
KVBlockSize=block_size,
|
||||
WavePerEU=2,
|
||||
ChunkQ=heads,
|
||||
)
|
||||
return out_logits
|
||||
return out_qk.sum(dim=0)
|
||||
else:
|
||||
return fp8_paged_mqa_logits_torch(
|
||||
q_fp8, kv_cache_fp8, weights, context_lens, block_tables, max_model_len
|
||||
@@ -464,6 +540,27 @@ def rocm_fp8_mqa_logits(
|
||||
return fp8_mqa_logits_torch(q, kv, weights, cu_seqlen_ks, cu_seqlen_ke)
|
||||
|
||||
|
||||
def _topk_indices_torch(logits: torch.Tensor, topk_tokens: int) -> torch.Tensor:
|
||||
k = min(topk_tokens, logits.shape[-1])
|
||||
values, indices = torch.topk(logits, k=k, dim=-1)
|
||||
indices = indices.to(torch.int32)
|
||||
indices = torch.where(
|
||||
values == float("-inf"),
|
||||
torch.full_like(indices, -1, dtype=torch.int32),
|
||||
indices,
|
||||
)
|
||||
if k == topk_tokens:
|
||||
return indices
|
||||
padded = torch.full(
|
||||
(logits.shape[0], topk_tokens),
|
||||
-1,
|
||||
dtype=torch.int32,
|
||||
device=logits.device,
|
||||
)
|
||||
padded[:, :k] = indices
|
||||
return padded
|
||||
|
||||
|
||||
def rocm_aiter_sparse_attn_indexer_fake(
|
||||
hidden_states: torch.Tensor,
|
||||
k_cache_prefix: LayerNameType,
|
||||
@@ -482,8 +579,9 @@ def rocm_aiter_sparse_attn_indexer_fake(
|
||||
# profile run
|
||||
# NOTE(Chen): create the max possible flattened_kv. So that
|
||||
# profile_run can get correct memory usage.
|
||||
device = hidden_states.device if k is None else k.device
|
||||
_flattened_kv = torch.empty(
|
||||
[total_seq_lens, head_dim + 4], device=k.device, dtype=torch.uint8
|
||||
[total_seq_lens, head_dim + 4], device=device, dtype=torch.uint8
|
||||
)
|
||||
fp8_dtype = current_platform.fp8_dtype()
|
||||
_k_fp8 = _flattened_kv[..., :head_dim].view(fp8_dtype).contiguous()
|
||||
@@ -491,7 +589,7 @@ def rocm_aiter_sparse_attn_indexer_fake(
|
||||
return topk_indices_buffer
|
||||
|
||||
|
||||
def rocm_aiter_sparse_attn_indexer(
|
||||
def rocm_aiter_sparse_attn_indexer_native(
|
||||
hidden_states: torch.Tensor,
|
||||
k_cache_prefix: LayerNameType,
|
||||
kv_cache: torch.Tensor,
|
||||
@@ -505,10 +603,12 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
max_model_len: int,
|
||||
total_seq_lens: int,
|
||||
topk_indices_buffer: torch.Tensor | None,
|
||||
skip_k_cache_insert: bool = False,
|
||||
) -> torch.Tensor:
|
||||
# careful! this will be None in dummy run
|
||||
attn_metadata = get_forward_context().attn_metadata
|
||||
fp8_dtype = current_platform.fp8_dtype()
|
||||
from vllm import _custom_ops as ops
|
||||
from vllm.utils.torch_utils import _resolve_layer_name
|
||||
|
||||
k_cache_prefix = _resolve_layer_name(k_cache_prefix)
|
||||
@@ -537,19 +637,33 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
has_decode = layer_attn_metadata.num_decodes > 0
|
||||
has_prefill = layer_attn_metadata.num_prefills > 0
|
||||
num_decode_tokens = layer_attn_metadata.num_decode_tokens
|
||||
device = hidden_states.device if k is None else k.device
|
||||
|
||||
# during speculative decoding, k may be padded to the CUDA graph batch
|
||||
# size while slot_mapping only covers actual tokens.
|
||||
num_tokens = slot_mapping.shape[0]
|
||||
k = k[:num_tokens]
|
||||
if k is not None:
|
||||
k = k[:num_tokens]
|
||||
elif not skip_k_cache_insert:
|
||||
raise ValueError("k must be provided when skip_k_cache_insert is False")
|
||||
|
||||
indexer_k_quant_and_cache_triton(
|
||||
k,
|
||||
kv_cache,
|
||||
slot_mapping,
|
||||
quant_block_size,
|
||||
scale_fmt,
|
||||
)
|
||||
if not skip_k_cache_insert:
|
||||
if _ON_GFX942:
|
||||
ops.indexer_k_quant_and_cache(
|
||||
k,
|
||||
kv_cache,
|
||||
slot_mapping,
|
||||
quant_block_size,
|
||||
scale_fmt,
|
||||
)
|
||||
else:
|
||||
indexer_k_quant_and_cache_triton(
|
||||
k,
|
||||
kv_cache,
|
||||
slot_mapping,
|
||||
quant_block_size,
|
||||
scale_fmt,
|
||||
)
|
||||
|
||||
topk_indices_buffer[: hidden_states.shape[0]] = -1
|
||||
if has_prefill:
|
||||
@@ -558,22 +672,31 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
for chunk in prefill_metadata.chunks:
|
||||
k_fp8 = torch.empty(
|
||||
[chunk.total_seq_lens, head_dim],
|
||||
device=k.device,
|
||||
device=device,
|
||||
dtype=fp8_dtype,
|
||||
)
|
||||
k_scale = torch.empty(
|
||||
[chunk.total_seq_lens, 4],
|
||||
device=k.device,
|
||||
device=device,
|
||||
dtype=torch.uint8,
|
||||
)
|
||||
cp_gather_indexer_k_quant_cache_triton(
|
||||
kv_cache,
|
||||
k_fp8,
|
||||
k_scale,
|
||||
chunk.block_table,
|
||||
chunk.cu_seq_lens,
|
||||
chunk.token_to_seq,
|
||||
)
|
||||
if _ON_GFX942:
|
||||
ops.cp_gather_indexer_k_quant_cache(
|
||||
kv_cache,
|
||||
k_fp8,
|
||||
k_scale,
|
||||
chunk.block_table,
|
||||
chunk.cu_seq_lens,
|
||||
)
|
||||
else:
|
||||
cp_gather_indexer_k_quant_cache_triton(
|
||||
kv_cache,
|
||||
k_fp8,
|
||||
k_scale,
|
||||
chunk.block_table,
|
||||
chunk.cu_seq_lens,
|
||||
token_to_seq=chunk.token_to_seq,
|
||||
)
|
||||
|
||||
logits = rocm_fp8_mqa_logits(
|
||||
q_fp8[chunk.token_start : chunk.token_end],
|
||||
@@ -582,21 +705,10 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
chunk.cu_seqlen_ks,
|
||||
chunk.cu_seqlen_ke,
|
||||
)
|
||||
num_rows = logits.shape[0]
|
||||
assert topk_tokens == 2048, "top_k_per_row assumes size 2048"
|
||||
topk_indices = topk_indices_buffer[
|
||||
chunk.token_start : chunk.token_end, :topk_tokens
|
||||
]
|
||||
torch.ops._C.top_k_per_row_prefill(
|
||||
logits,
|
||||
chunk.cu_seqlen_ks,
|
||||
chunk.cu_seqlen_ke,
|
||||
topk_indices,
|
||||
num_rows,
|
||||
logits.stride(0),
|
||||
logits.stride(1),
|
||||
topk_tokens,
|
||||
)
|
||||
topk_indices.copy_(_topk_indices_torch(logits, topk_tokens))
|
||||
|
||||
if has_decode:
|
||||
decode_metadata = layer_attn_metadata.decode
|
||||
@@ -633,19 +745,8 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
max_model_len=max_model_len,
|
||||
)
|
||||
|
||||
num_rows = logits.shape[0]
|
||||
assert topk_tokens == 2048, "top_k_per_row assumes size 2048"
|
||||
topk_indices = topk_indices_buffer[:num_decode_tokens, :topk_tokens]
|
||||
torch.ops._C.top_k_per_row_decode(
|
||||
logits,
|
||||
next_n,
|
||||
decode_metadata.seq_lens,
|
||||
topk_indices,
|
||||
num_rows,
|
||||
logits.stride(0),
|
||||
logits.stride(1),
|
||||
topk_tokens,
|
||||
)
|
||||
topk_indices.copy_(_topk_indices_torch(logits, topk_tokens)[:num_decode_tokens])
|
||||
|
||||
if decode_metadata.requires_padding:
|
||||
# if padded, we need to unpack
|
||||
@@ -659,3 +760,370 @@ def rocm_aiter_sparse_attn_indexer(
|
||||
)
|
||||
|
||||
return topk_indices_buffer
|
||||
|
||||
|
||||
def rocm_aiter_sparse_attn_indexer(
|
||||
hidden_states: torch.Tensor,
|
||||
k_cache_prefix: LayerNameType,
|
||||
kv_cache: torch.Tensor,
|
||||
q_fp8: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
weights: torch.Tensor,
|
||||
quant_block_size: int,
|
||||
scale_fmt: str | None,
|
||||
topk_tokens: int,
|
||||
head_dim: int,
|
||||
max_model_len: int,
|
||||
total_seq_lens: int,
|
||||
topk_indices_buffer: torch.Tensor | None,
|
||||
) -> torch.Tensor:
|
||||
return rocm_aiter_sparse_attn_indexer_native(
|
||||
hidden_states,
|
||||
k_cache_prefix,
|
||||
kv_cache,
|
||||
q_fp8,
|
||||
k,
|
||||
weights,
|
||||
quant_block_size,
|
||||
scale_fmt,
|
||||
topk_tokens,
|
||||
head_dim,
|
||||
max_model_len,
|
||||
total_seq_lens,
|
||||
topk_indices_buffer,
|
||||
skip_k_cache_insert=False,
|
||||
)
|
||||
|
||||
|
||||
def _decode_e8m0_scales(scale: torch.Tensor) -> torch.Tensor:
|
||||
if scale.dtype == torch.float8_e8m0fnu:
|
||||
from vllm.model_executor.layers.quantization.utils.fp8_utils import (
|
||||
_upcast_e8m0_to_fp32,
|
||||
)
|
||||
|
||||
return _upcast_e8m0_to_fp32(scale).contiguous()
|
||||
return scale.to(torch.float32)
|
||||
|
||||
|
||||
def _expand_2d_block_scales(
|
||||
scale: torch.Tensor,
|
||||
rows: int,
|
||||
cols: int,
|
||||
) -> torch.Tensor:
|
||||
scale = _decode_e8m0_scales(scale)
|
||||
row_blocks, col_blocks = scale.shape[-2:]
|
||||
row_block = math.ceil(rows / row_blocks)
|
||||
col_block = math.ceil(cols / col_blocks)
|
||||
scale = torch.repeat_interleave(scale, row_block, dim=-2)[..., :rows, :]
|
||||
scale = torch.repeat_interleave(scale, col_block, dim=-1)[..., :, :cols]
|
||||
return scale
|
||||
|
||||
|
||||
def _apply_gptj_inv_rope_ref(
|
||||
x: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
cos_sin_cache: torch.Tensor,
|
||||
rope_dim: int,
|
||||
) -> torch.Tensor:
|
||||
if rope_dim == 0 or x.numel() == 0:
|
||||
return x
|
||||
half_rot = rope_dim // 2
|
||||
nope_dim = x.shape[-1] - rope_dim
|
||||
dtype = x.dtype
|
||||
x = x.to(torch.float32)
|
||||
cache = cos_sin_cache.index_select(0, positions.to(torch.long))
|
||||
cos = cache[:, :half_rot].to(torch.float32)
|
||||
sin = cache[:, half_rot : 2 * half_rot].to(torch.float32)
|
||||
view_shape = (positions.shape[0],) + (1,) * (x.dim() - 2) + (half_rot,)
|
||||
cos = cos.view(view_shape)
|
||||
sin = sin.view(view_shape)
|
||||
rope = x[..., nope_dim:]
|
||||
y_even = rope[..., 0::2]
|
||||
y_odd = rope[..., 1::2]
|
||||
rope_out = torch.stack(
|
||||
(y_even * cos + y_odd * sin, y_odd * cos - y_even * sin),
|
||||
dim=-1,
|
||||
).flatten(-2)
|
||||
x = x.clone()
|
||||
x[..., nope_dim:] = rope_out
|
||||
return x.to(dtype)
|
||||
|
||||
|
||||
def _apply_inv_rope_ref(
|
||||
rotary_emb: torch.nn.Module,
|
||||
x: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
rope_dim: int,
|
||||
) -> torch.Tensor:
|
||||
if hasattr(rotary_emb, "forward_native"):
|
||||
try:
|
||||
query, _ = rotary_emb.forward_native(
|
||||
positions,
|
||||
x.clone(),
|
||||
None,
|
||||
inverse=True,
|
||||
)
|
||||
return query
|
||||
except TypeError:
|
||||
pass
|
||||
return _apply_gptj_inv_rope_ref(x, positions, rotary_emb.cos_sin_cache, rope_dim)
|
||||
|
||||
|
||||
def rocm_inv_rope_einsum(
|
||||
rotary_emb: torch.nn.Module,
|
||||
o: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
rope_head_dim: int,
|
||||
n_local_groups: int,
|
||||
o_lora_rank: int,
|
||||
wo_a: torch.nn.Module,
|
||||
) -> torch.Tensor:
|
||||
"""Reference inverse-RoPE + WO_A einsum path used on ROCm."""
|
||||
o_ref = _apply_inv_rope_ref(rotary_emb, o, positions, rope_head_dim).to(
|
||||
torch.bfloat16
|
||||
)
|
||||
o_ref = o_ref.view(o.shape[0], n_local_groups, -1)
|
||||
|
||||
hidden_dim = o_ref.shape[-1]
|
||||
if hasattr(wo_a, "weight_scale_inv"):
|
||||
wo_a_weight = wo_a.weight.view(n_local_groups, o_lora_rank, hidden_dim).to(
|
||||
torch.float32
|
||||
)
|
||||
wo_a_scale = _expand_2d_block_scales(
|
||||
wo_a.weight_scale_inv.view(
|
||||
n_local_groups, -1, wo_a.weight_scale_inv.shape[-1]
|
||||
),
|
||||
o_lora_rank,
|
||||
hidden_dim,
|
||||
)
|
||||
wo_a_weight = (wo_a_weight * wo_a_scale).to(torch.bfloat16)
|
||||
else:
|
||||
wo_a_weight = wo_a.weight.view(n_local_groups, o_lora_rank, hidden_dim).to(
|
||||
torch.bfloat16
|
||||
)
|
||||
|
||||
return torch.einsum("tgd,grd->tgr", o_ref, wo_a_weight)
|
||||
|
||||
|
||||
def rocm_ref_sparse_attn_prefill(
|
||||
q: torch.Tensor,
|
||||
kv: torch.Tensor,
|
||||
indices: torch.Tensor,
|
||||
topk_length: torch.Tensor | None,
|
||||
scale: float,
|
||||
head_dim: int,
|
||||
attn_sink: torch.Tensor | None,
|
||||
) -> torch.Tensor:
|
||||
indices = indices.clone().squeeze(1)
|
||||
s_q, h_q, d_qk = q.shape
|
||||
topk = indices.shape[-1]
|
||||
s_kv = kv.shape[0]
|
||||
if topk_length is not None:
|
||||
mask = torch.arange(topk, device=indices.device).unsqueeze(
|
||||
0
|
||||
) >= topk_length.unsqueeze(1)
|
||||
indices[mask] = -1
|
||||
invalid_mask = (indices < 0) | (indices >= s_kv)
|
||||
indices[invalid_mask] = 0
|
||||
|
||||
qf = q.float()
|
||||
gathered_kv = kv.index_select(0, indices.flatten()).reshape(s_q, topk, d_qk).float()
|
||||
scores = qf @ gathered_kv.transpose(1, 2)
|
||||
scores *= scale
|
||||
scores[invalid_mask.unsqueeze(1).expand_as(scores)] = float("-inf")
|
||||
|
||||
orig_lse = torch.logsumexp(scores, dim=-1)
|
||||
lse_for_o = orig_lse
|
||||
if attn_sink is not None:
|
||||
lse_for_o = torch.logsumexp(
|
||||
torch.stack(
|
||||
[orig_lse, attn_sink[:h_q].view(1, h_q).expand_as(orig_lse)],
|
||||
dim=0,
|
||||
),
|
||||
dim=0,
|
||||
)
|
||||
lse_for_o = lse_for_o.clone()
|
||||
lse_for_o[lse_for_o == float("-inf")] = float("+inf")
|
||||
probs = torch.exp(scores - lse_for_o.unsqueeze(-1))
|
||||
out = probs @ gathered_kv[..., :head_dim]
|
||||
lonely_q_mask = orig_lse == float("-inf")
|
||||
out[lonely_q_mask.unsqueeze(-1).expand_as(out)] = 0.0
|
||||
return out.to(torch.bfloat16)
|
||||
|
||||
|
||||
def rocm_sparse_attn_prefill(
|
||||
q: torch.Tensor,
|
||||
kv: torch.Tensor,
|
||||
indices: torch.Tensor,
|
||||
topk_length: torch.Tensor | None,
|
||||
scale: float,
|
||||
head_dim: int,
|
||||
attn_sink: torch.Tensor | None,
|
||||
output: torch.Tensor,
|
||||
) -> None:
|
||||
output_chunk = rocm_ref_sparse_attn_prefill(
|
||||
q=q,
|
||||
kv=kv,
|
||||
indices=indices,
|
||||
topk_length=topk_length,
|
||||
scale=scale,
|
||||
head_dim=head_dim,
|
||||
attn_sink=attn_sink,
|
||||
)
|
||||
output.copy_(output_chunk.to(output.dtype))
|
||||
|
||||
|
||||
def rocm_dequantize_blocked_k_cache(
|
||||
quant_k_cache: torch.Tensor,
|
||||
head_dim: int,
|
||||
nope_head_dim: int,
|
||||
rope_head_dim: int,
|
||||
) -> torch.Tensor:
|
||||
fp8_dtype = current_platform.fp8_dtype()
|
||||
tile_size = 64
|
||||
num_tiles = nope_head_dim // tile_size
|
||||
|
||||
num_blocks, block_size, _ = quant_k_cache.shape
|
||||
quant_k_cache = quant_k_cache.view(num_blocks, -1)
|
||||
input_nope_rope = quant_k_cache[
|
||||
:, : block_size * (nope_head_dim + 2 * rope_head_dim)
|
||||
].view(num_blocks, block_size, nope_head_dim + 2 * rope_head_dim)
|
||||
input_nope = input_nope_rope[:, :, :nope_head_dim].view(fp8_dtype)
|
||||
input_rope = input_nope_rope[:, :, nope_head_dim:].view(torch.bfloat16)
|
||||
input_scale = (
|
||||
quant_k_cache[:, block_size * (nope_head_dim + 2 * rope_head_dim) :]
|
||||
.view(num_blocks, block_size, 8)[:, :, :num_tiles]
|
||||
.view(torch.float8_e8m0fnu)
|
||||
)
|
||||
|
||||
result = torch.empty(
|
||||
(num_blocks, block_size, 1, head_dim),
|
||||
dtype=torch.bfloat16,
|
||||
device=quant_k_cache.device,
|
||||
)
|
||||
result[..., nope_head_dim:] = input_rope.unsqueeze(2)
|
||||
for tile_idx in range(num_tiles):
|
||||
cur_nope = input_nope[
|
||||
..., tile_idx * tile_size : (tile_idx + 1) * tile_size
|
||||
].to(torch.bfloat16)
|
||||
cur_scales = input_scale[:, :, tile_idx].to(torch.bfloat16).unsqueeze(-1)
|
||||
result[..., tile_idx * tile_size : (tile_idx + 1) * tile_size] = (
|
||||
cur_nope * cur_scales
|
||||
).unsqueeze(2)
|
||||
return result
|
||||
|
||||
|
||||
def rocm_ref_sparse_attn_decode(
|
||||
q: torch.Tensor,
|
||||
blocked_k: torch.Tensor,
|
||||
indices_in_kvcache: torch.Tensor,
|
||||
topk_length: torch.Tensor | None,
|
||||
scale: float,
|
||||
head_dim: int,
|
||||
attn_sink: torch.Tensor | None,
|
||||
extra_blocked_k: torch.Tensor | None = None,
|
||||
extra_indices_in_kvcache: torch.Tensor | None = None,
|
||||
extra_topk_length: torch.Tensor | None = None,
|
||||
) -> torch.Tensor:
|
||||
b, s_q, h_q, d_qk = q.shape
|
||||
|
||||
def process_scope(
|
||||
cur_blocked_k: torch.Tensor,
|
||||
cur_indices: torch.Tensor,
|
||||
cur_topk_length: torch.Tensor | None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
cur_indices = cur_indices.reshape(b, s_q, -1)
|
||||
topk = cur_indices.size(-1)
|
||||
fixed_indices = torch.clamp_min(cur_indices, 0)
|
||||
gathered_kv = (
|
||||
cur_blocked_k.view(-1, d_qk)
|
||||
.index_select(0, fixed_indices.view(-1))
|
||||
.view(b, s_q, topk, d_qk)
|
||||
)
|
||||
invalid_mask = cur_indices == -1
|
||||
if cur_topk_length is not None:
|
||||
cur_topk_length = cur_topk_length.reshape(b)
|
||||
invalid_mask |= torch.arange(0, topk, device=invalid_mask.device).view(
|
||||
1, 1, topk
|
||||
) >= cur_topk_length.view(b, 1, 1)
|
||||
return gathered_kv, invalid_mask
|
||||
|
||||
gathered_kv, invalid_mask = process_scope(
|
||||
blocked_k, indices_in_kvcache, topk_length
|
||||
)
|
||||
if extra_blocked_k is not None:
|
||||
assert extra_indices_in_kvcache is not None
|
||||
gathered_kv1, invalid_mask1 = process_scope(
|
||||
extra_blocked_k, extra_indices_in_kvcache, extra_topk_length
|
||||
)
|
||||
gathered_kv = torch.cat([gathered_kv, gathered_kv1], dim=2)
|
||||
invalid_mask = torch.cat([invalid_mask, invalid_mask1], dim=2)
|
||||
|
||||
gathered_kv = gathered_kv.view(b * s_q, -1, d_qk).float()
|
||||
gathered_kv[gathered_kv != gathered_kv] = 0.0
|
||||
qf = q.float().view(b * s_q, h_q, d_qk)
|
||||
attn_weight = qf @ gathered_kv.transpose(-1, -2)
|
||||
attn_weight *= scale
|
||||
attn_weight[
|
||||
invalid_mask.view(b * s_q, 1, -1).expand(b * s_q, h_q, invalid_mask.size(-1))
|
||||
] = float("-inf")
|
||||
lse = attn_weight.logsumexp(dim=-1)
|
||||
attn_weight = torch.exp(attn_weight - lse.unsqueeze(-1))
|
||||
output = attn_weight @ gathered_kv[..., :head_dim]
|
||||
output = output.view(b, s_q, h_q, head_dim)
|
||||
lse = lse.view(b, s_q, h_q)
|
||||
|
||||
if attn_sink is not None:
|
||||
output *= (1.0 / (1.0 + torch.exp(attn_sink.view(1, 1, h_q) - lse))).unsqueeze(
|
||||
-1
|
||||
)
|
||||
|
||||
lonely_q_mask = lse == float("-inf")
|
||||
output[lonely_q_mask.unsqueeze(-1).expand_as(output)] = 0.0
|
||||
return output.squeeze(1).to(torch.bfloat16)
|
||||
|
||||
|
||||
def rocm_forward_decode_fallback(
|
||||
q: torch.Tensor,
|
||||
kv_cache: torch.Tensor | None,
|
||||
swa_k_cache: torch.Tensor,
|
||||
swa_only: bool,
|
||||
topk_indices: torch.Tensor | None,
|
||||
topk_lens: torch.Tensor | None,
|
||||
swa_indices: torch.Tensor,
|
||||
swa_lens: torch.Tensor,
|
||||
attn_sink: torch.Tensor | None,
|
||||
scale: float,
|
||||
head_dim: int,
|
||||
nope_head_dim: int,
|
||||
rope_head_dim: int,
|
||||
output: torch.Tensor,
|
||||
) -> None:
|
||||
blocked_swa = rocm_dequantize_blocked_k_cache(
|
||||
swa_k_cache,
|
||||
head_dim=head_dim,
|
||||
nope_head_dim=nope_head_dim,
|
||||
rope_head_dim=rope_head_dim,
|
||||
)
|
||||
blocked_extra = None
|
||||
if not swa_only:
|
||||
assert kv_cache is not None
|
||||
blocked_extra = rocm_dequantize_blocked_k_cache(
|
||||
kv_cache,
|
||||
head_dim=head_dim,
|
||||
nope_head_dim=nope_head_dim,
|
||||
rope_head_dim=rope_head_dim,
|
||||
)
|
||||
attn_out = rocm_ref_sparse_attn_decode(
|
||||
q=q.unsqueeze(1),
|
||||
blocked_k=blocked_swa,
|
||||
indices_in_kvcache=swa_indices.unsqueeze(1),
|
||||
topk_length=swa_lens,
|
||||
scale=scale,
|
||||
head_dim=head_dim,
|
||||
attn_sink=attn_sink[: q.shape[1]] if attn_sink is not None else None,
|
||||
extra_blocked_k=blocked_extra,
|
||||
extra_indices_in_kvcache=topk_indices,
|
||||
extra_topk_length=topk_lens,
|
||||
)
|
||||
output.copy_(attn_out.to(output.dtype))
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Gemma4 MTP (Multi-Token Prediction) proposer for speculative decoding.
|
||||
|
||||
The Gemma4 assistant model runs all decoder layers per draft step
|
||||
(producing one token), and all its attention layers share KV cache
|
||||
with the target model via cross-model KV sharing.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
from copy import copy
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from vllm.config import VllmConfig, get_layers_from_vllm_config, replace
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
|
||||
from vllm.v1.attention.backend import CommonAttentionMetadata
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
KVCacheConfig,
|
||||
KVCacheSpec,
|
||||
UniformTypeKVCacheSpecs,
|
||||
)
|
||||
from vllm.v1.spec_decode.llm_base_proposer import SpecDecodeBaseProposer
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class Gemma4Proposer(SpecDecodeBaseProposer):
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
device: torch.device,
|
||||
runner=None,
|
||||
):
|
||||
super().__init__(
|
||||
vllm_config,
|
||||
device,
|
||||
pass_hidden_states_to_model=True,
|
||||
runner=runner,
|
||||
)
|
||||
# All draft steps predict from the same position (the last
|
||||
# target-model position), so positions and seq_lens must not
|
||||
# advance between steps.
|
||||
self.constant_draft_positions = True
|
||||
|
||||
# Per-group block tables for multi-group KV cache models.
|
||||
# Populated by gpu_model_runner during _prepare_inputs.
|
||||
self._per_group_block_tables: dict[int, torch.Tensor] = {}
|
||||
|
||||
# Centroids CUDA graphs — populated in load_model if centroids
|
||||
# masking is active. _centroids_sizes is pre-sorted for fast
|
||||
# lookup in _greedy_sample.
|
||||
self._centroids_sizes: list[int] = []
|
||||
self._centroids_graphs: dict[int, torch.cuda.CUDAGraph] = {}
|
||||
self._centroids_inputs: dict[int, torch.Tensor] = {}
|
||||
self._centroids_outputs: dict[int, torch.Tensor] = {}
|
||||
|
||||
def set_per_group_block_table(self, gid: int, block_table: torch.Tensor) -> None:
|
||||
self._per_group_block_tables[gid] = block_table
|
||||
|
||||
def model_returns_tuple(self) -> bool:
|
||||
# forward() returns (draft_hidden_states, backbone_hidden_states).
|
||||
# The proposer uses draft_hidden_states for compute_logits and
|
||||
# backbone_hidden_states for the hidden-state feedback buffer.
|
||||
return True
|
||||
|
||||
def build_per_group_and_layer_attn_metadata(
|
||||
self,
|
||||
common_attn_metadata: CommonAttentionMetadata,
|
||||
draft_index: int = 0,
|
||||
) -> tuple[list[object], dict[str, object]]:
|
||||
"""Build attention metadata using the correct block table per group.
|
||||
|
||||
Gemma4 has multiple KV cache groups (sliding vs full attention)
|
||||
with different block tables. The base class receives a single
|
||||
common_attn_metadata whose block_table belongs to one group.
|
||||
We swap in the correct block table for each draft attention group.
|
||||
"""
|
||||
per_group_attn_metadata: list[object] = []
|
||||
per_layer_attn_metadata: dict[str, object] = {}
|
||||
for attn_group in self.draft_attn_groups:
|
||||
gid = attn_group.kv_cache_group_id
|
||||
if gid in self._per_group_block_tables:
|
||||
cm = copy(common_attn_metadata)
|
||||
cm.block_table_tensor = self._per_group_block_tables[gid]
|
||||
else:
|
||||
cm = common_attn_metadata
|
||||
attn_metadata = attn_group.get_metadata_builder().build_for_drafting(
|
||||
common_attn_metadata=cm, draft_index=draft_index
|
||||
)
|
||||
per_group_attn_metadata.append(attn_metadata)
|
||||
for layer_name in attn_group.layer_names:
|
||||
per_layer_attn_metadata[layer_name] = attn_metadata
|
||||
return per_group_attn_metadata, per_layer_attn_metadata
|
||||
|
||||
def _greedy_sample(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
||||
if self._centroids_sizes:
|
||||
T = hidden_states.shape[0]
|
||||
for size in self._centroids_sizes:
|
||||
if size >= T:
|
||||
self._centroids_inputs[size][:T].copy_(hidden_states)
|
||||
self._centroids_graphs[size].replay()
|
||||
return self._centroids_outputs[size][:T].clone()
|
||||
return self.model.get_top_tokens(hidden_states)
|
||||
return super()._greedy_sample(hidden_states)
|
||||
|
||||
def _setup_centroids_cuda_graphs(self) -> None:
|
||||
"""Capture CUDA graphs for centroids get_top_tokens at key sizes."""
|
||||
masked_emb = self.model.masked_embedding
|
||||
lm_head_weight = self.model._get_full_lm_head_weight()
|
||||
|
||||
for size in [1, 2, 4, 8, 16, 32, 64]:
|
||||
static_input = torch.zeros(
|
||||
size,
|
||||
masked_emb.hidden_size,
|
||||
dtype=self.dtype,
|
||||
device=self.device,
|
||||
)
|
||||
for _ in range(3):
|
||||
masked_emb.get_top_tokens(static_input, lm_head_weight)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
g = torch.cuda.CUDAGraph()
|
||||
with torch.cuda.graph(g):
|
||||
static_output = masked_emb.get_top_tokens(
|
||||
static_input,
|
||||
lm_head_weight,
|
||||
)
|
||||
self._centroids_graphs[size] = g
|
||||
self._centroids_inputs[size] = static_input
|
||||
self._centroids_outputs[size] = static_output
|
||||
|
||||
self._centroids_sizes = sorted(self._centroids_graphs)
|
||||
logger.info(
|
||||
"Gemma4 MTP: captured centroids CUDA graphs for sizes %s.",
|
||||
self._centroids_sizes,
|
||||
)
|
||||
|
||||
def _create_draft_vllm_config(self) -> VllmConfig:
|
||||
"""Preserve the target's forced TRITON_ATTN backend for draft layers.
|
||||
|
||||
Gemma4 forces TRITON_ATTN due to heterogeneous head dimensions
|
||||
(head_dim=256 sliding, global_head_dim=512 full). The base class
|
||||
resets attention_config.backend to None for draft models, causing
|
||||
sliding layers to fall back to FLASH_ATTN which cannot handle
|
||||
KV-shared cache. Override to carry the target's backend through.
|
||||
"""
|
||||
base = super()._create_draft_vllm_config()
|
||||
target_backend = self.vllm_config.attention_config.backend
|
||||
if target_backend is not None:
|
||||
base = replace(
|
||||
base,
|
||||
attention_config=replace(
|
||||
base.attention_config,
|
||||
backend=target_backend,
|
||||
),
|
||||
)
|
||||
return base
|
||||
|
||||
def _maybe_share_lm_head(self, target_language_model: nn.Module) -> None:
|
||||
"""Gemma4 MTP always keeps its own draft-dim lm_head.
|
||||
|
||||
The draft model's lm_head operates in draft hidden_size (e.g. 256),
|
||||
which differs from the target's backbone hidden_size (e.g. 1536).
|
||||
Sharing would break compute_logits (and centroids masking when
|
||||
use_ordered_embeddings is enabled).
|
||||
"""
|
||||
logger.info(
|
||||
"Gemma4 MTP: keeping draft model's own lm_head (draft_dim != backbone_dim)."
|
||||
)
|
||||
|
||||
def load_model(self, target_model: nn.Module) -> None:
|
||||
target_attn_layer_names = set(
|
||||
get_layers_from_vllm_config(
|
||||
self.vllm_config,
|
||||
AttentionLayerBase,
|
||||
).keys()
|
||||
)
|
||||
|
||||
super().load_model(target_model)
|
||||
|
||||
self._setup_gemma4_kv_sharing(target_attn_layer_names)
|
||||
|
||||
if getattr(self.model, "masked_embedding", None) is not None:
|
||||
self._setup_centroids_cuda_graphs()
|
||||
|
||||
def validate_same_kv_cache_group(self, kv_cache_config: KVCacheConfig) -> None:
|
||||
"""Draft layers span multiple KV cache groups (sliding + full
|
||||
attention with different head dimensions), so skip the base
|
||||
class single-group assertion."""
|
||||
|
||||
def initialize_attn_backend(
|
||||
self,
|
||||
kv_cache_config: KVCacheConfig,
|
||||
kernel_block_sizes: list[int] | None = None,
|
||||
) -> None:
|
||||
"""Create separate AttentionGroup objects per KV cache spec
|
||||
so that each head-dim variant gets its own metadata builder."""
|
||||
all_attn_layers = get_layers_from_vllm_config(
|
||||
self.vllm_config,
|
||||
AttentionLayerBase,
|
||||
)
|
||||
|
||||
layer_to_gid: dict[str, int] = {}
|
||||
layer_to_spec: dict[str, KVCacheSpec] = {}
|
||||
for gid, group in enumerate(kv_cache_config.kv_cache_groups):
|
||||
group_spec = group.kv_cache_spec
|
||||
for ln in group.layer_names:
|
||||
layer_to_gid[ln] = gid
|
||||
if isinstance(group_spec, UniformTypeKVCacheSpecs):
|
||||
if ln in group_spec.kv_cache_specs:
|
||||
layer_to_spec[ln] = group_spec.kv_cache_specs[ln]
|
||||
else:
|
||||
tgt = getattr(
|
||||
all_attn_layers.get(ln),
|
||||
"kv_sharing_target_layer_name",
|
||||
None,
|
||||
)
|
||||
if tgt and tgt in group_spec.kv_cache_specs:
|
||||
layer_to_spec[ln] = group_spec.kv_cache_specs[tgt]
|
||||
else:
|
||||
layer_to_spec[ln] = group_spec
|
||||
else:
|
||||
layer_to_spec[ln] = group_spec
|
||||
|
||||
attention_groups: dict[tuple[str, KVCacheSpec], AttentionGroup] = {}
|
||||
for layer_name in self._draft_attn_layer_names:
|
||||
if layer_name not in layer_to_spec:
|
||||
continue
|
||||
attn_layer = all_attn_layers[layer_name]
|
||||
attn_backend = attn_layer.get_attn_backend()
|
||||
spec = layer_to_spec[layer_name]
|
||||
gid = layer_to_gid[layer_name]
|
||||
group_key = (attn_backend.full_cls_name(), spec)
|
||||
|
||||
if group_key not in attention_groups:
|
||||
kernel_block_size = (
|
||||
kernel_block_sizes[gid]
|
||||
if kernel_block_sizes is not None and gid < len(kernel_block_sizes)
|
||||
else None
|
||||
)
|
||||
attn_group = AttentionGroup(
|
||||
backend=attn_backend,
|
||||
layer_names=[layer_name],
|
||||
kv_cache_spec=spec,
|
||||
kv_cache_group_id=gid,
|
||||
)
|
||||
attn_group.create_metadata_builders(
|
||||
self.vllm_config,
|
||||
self.device,
|
||||
kernel_block_size=kernel_block_size,
|
||||
)
|
||||
attention_groups[group_key] = attn_group
|
||||
else:
|
||||
attention_groups[group_key].layer_names.append(layer_name)
|
||||
|
||||
self.draft_attn_groups = list(attention_groups.values())
|
||||
if self.draft_attn_groups:
|
||||
self.kv_cache_gid = self.draft_attn_groups[0].kv_cache_group_id
|
||||
self.block_size = (
|
||||
self.draft_attn_groups[0]
|
||||
.get_metadata_builder()
|
||||
.kv_cache_spec.block_size
|
||||
)
|
||||
else:
|
||||
self.kv_cache_gid = 0
|
||||
self.block_size = kv_cache_config.kv_cache_groups[
|
||||
0
|
||||
].kv_cache_spec.block_size
|
||||
logger.debug("Using block size %d for drafting layers", self.block_size)
|
||||
|
||||
def _setup_gemma4_kv_sharing(
|
||||
self,
|
||||
target_attn_layer_names: set[str],
|
||||
) -> None:
|
||||
"""Wire draft layers to share KV with the target model.
|
||||
|
||||
Each draft decoder layer is mapped to the last non-KV-shared
|
||||
target layer of the same attention type (sliding or full).
|
||||
"""
|
||||
draft_config = self.speculative_config.draft_model_config.hf_config
|
||||
draft_text_config = draft_config.get_text_config()
|
||||
target_config = self.vllm_config.model_config.hf_config
|
||||
target_text_config = target_config.get_text_config()
|
||||
target_layer_types = getattr(target_text_config, "layer_types", [])
|
||||
|
||||
if not (hasattr(self.model, "model") and hasattr(self.model.model, "layers")):
|
||||
return
|
||||
|
||||
target_num_kv_shared = getattr(target_text_config, "num_kv_shared_layers", 0)
|
||||
num_non_shared = len(target_layer_types) - target_num_kv_shared
|
||||
type_to_target_indices: dict[str, list[int]] = defaultdict(list)
|
||||
for idx, lt in enumerate(target_layer_types[:num_non_shared]):
|
||||
type_to_target_indices[lt].append(idx)
|
||||
|
||||
target_prefix = "model.layers"
|
||||
for name in target_attn_layer_names:
|
||||
if ".layers." in name:
|
||||
target_prefix = name.split(".layers.")[0] + ".layers"
|
||||
break
|
||||
|
||||
draft_layer_types = getattr(draft_text_config, "layer_types", [])
|
||||
for draft_idx, layer in enumerate(self.model.model.layers):
|
||||
if not hasattr(layer, "self_attn"):
|
||||
continue
|
||||
attn = getattr(layer.self_attn, "attn", None)
|
||||
if attn is None:
|
||||
continue
|
||||
|
||||
draft_layer_type = (
|
||||
draft_layer_types[draft_idx]
|
||||
if draft_idx < len(draft_layer_types)
|
||||
else "full_attention"
|
||||
)
|
||||
candidates = type_to_target_indices.get(draft_layer_type, [])
|
||||
if not candidates:
|
||||
logger.warning(
|
||||
"No target layer of type '%s' for draft layer %d",
|
||||
draft_layer_type,
|
||||
draft_idx,
|
||||
)
|
||||
continue
|
||||
|
||||
target_idx = candidates[-1]
|
||||
target_layer_name = f"{target_prefix}.{target_idx}.self_attn.attn"
|
||||
attn.kv_sharing_target_layer_name = target_layer_name
|
||||
logger.info(
|
||||
"Gemma4 MTP: draft layer %d (%s) -> %s",
|
||||
draft_idx,
|
||||
draft_layer_type,
|
||||
target_layer_name,
|
||||
)
|
||||
@@ -105,6 +105,12 @@ class SpecDecodeBaseProposer:
|
||||
)
|
||||
self.needs_extra_input_slots = self.net_num_new_slots_per_request > 0
|
||||
|
||||
# When True, all draft steps reuse the same position as the
|
||||
# first step instead of advancing by one each iteration.
|
||||
# Used by draft models with Q-only attention that share KV
|
||||
# with the target and always predict from the same position.
|
||||
self.constant_draft_positions: bool = False
|
||||
|
||||
self.parallel_drafting_token_id: int = 0
|
||||
self.parallel_drafting_hidden_state_tensor: torch.Tensor | None = None
|
||||
if self.parallel_drafting:
|
||||
@@ -388,9 +394,9 @@ class SpecDecodeBaseProposer:
|
||||
return {name: view for name in self._draft_attn_layer_names}
|
||||
|
||||
def initialize_cudagraph_keys(self, cudagraph_mode: CUDAGraphMode) -> None:
|
||||
"""Initialize cudagraph dispatcher keys for eagle.
|
||||
"""Initialize cudagraph dispatcher keys for the drafter.
|
||||
|
||||
Eagle only supports PIECEWISE cudagraphs (via mixed_mode).
|
||||
Only supports PIECEWISE cudagraphs (via mixed_mode).
|
||||
This should be called after adjust_cudagraph_sizes_for_spec_decode.
|
||||
"""
|
||||
if (
|
||||
@@ -499,6 +505,12 @@ class SpecDecodeBaseProposer:
|
||||
positions = self.positions[token_indices_to_sample]
|
||||
hidden_states = hidden_states[token_indices_to_sample]
|
||||
|
||||
if self.constant_draft_positions:
|
||||
# Write the sampling positions into the front of the
|
||||
# positions buffer so that subsequent loop iterations
|
||||
# (which read via _get_positions) use the correct values.
|
||||
self.positions[:batch_size] = positions
|
||||
|
||||
if any(isinstance(md, TreeAttentionMetadata) for md in per_group_attn_metadata):
|
||||
# Draft using tree attention - requires full logits for top-k
|
||||
logits = self.model.compute_logits(sample_hidden_states)
|
||||
@@ -556,59 +568,25 @@ class SpecDecodeBaseProposer:
|
||||
# cast to int32 is crucial when eagle model is compiled.
|
||||
# tensor.argmax() returns int64 by default.
|
||||
input_ids = draft_token_ids_list[-1].int()
|
||||
# Use fused kernel for slot mapping and metadata updates.
|
||||
# Write clamped positions directly into the positions buffer to
|
||||
# avoid an extra D2D copy for the common (non-mrope) case.
|
||||
positions_1d = positions[0] if self.uses_mrope else positions
|
||||
if self.uses_mrope:
|
||||
out_pos = self.mrope_positions[0, :batch_size]
|
||||
elif self.uses_xdrope_dim > 0 and self.draft_uses_xdrope_dim > 0:
|
||||
out_pos = self.xdrope_positions[0, :batch_size]
|
||||
else:
|
||||
out_pos = self.positions[:batch_size]
|
||||
eagle_step_update_slot_mapping_and_metadata(
|
||||
positions_1d=positions_1d,
|
||||
block_table_tensor=common_attn_metadata.block_table_tensor,
|
||||
seq_lens=common_attn_metadata.seq_lens,
|
||||
block_size=block_size,
|
||||
max_model_len=self.max_model_len,
|
||||
out_clamped_positions=out_pos,
|
||||
out_slot_mapping=self._slot_mapping_buffer[:input_batch_size],
|
||||
input_batch_size=input_batch_size,
|
||||
)
|
||||
common_attn_metadata.slot_mapping = self._slot_mapping_buffer[:batch_size]
|
||||
if self.uses_mrope:
|
||||
self.mrope_positions[1:, :batch_size] = self.mrope_positions[
|
||||
0, :batch_size
|
||||
]
|
||||
positions = self.mrope_positions[:, :batch_size]
|
||||
elif self.uses_xdrope_dim > 0 and self.draft_uses_xdrope_dim > 0:
|
||||
self.xdrope_positions[1:, :batch_size] = self.xdrope_positions[
|
||||
0, :batch_size
|
||||
]
|
||||
positions = self.xdrope_positions[0, :batch_size]
|
||||
else:
|
||||
positions = self.positions[:batch_size]
|
||||
# Increment the maximum sequence length. We increment max_seq_len
|
||||
# unconditionally even though some seq_lens may have been capped above,
|
||||
# as max_seq_len serves as an upper bound for sequence lengths.
|
||||
common_attn_metadata.max_seq_len = min(
|
||||
common_attn_metadata.max_seq_len + 1, self.max_model_len
|
||||
)
|
||||
|
||||
# Also update the CPU-side shadow; NOTE: this is hacky and should be
|
||||
# removed in when common_attn_metadata.seq_lens_cpu is deprecated.
|
||||
if common_attn_metadata._seq_lens_cpu is not None:
|
||||
common_attn_metadata._seq_lens_cpu += 1
|
||||
if common_attn_metadata._num_computed_tokens_cpu is not None:
|
||||
common_attn_metadata._num_computed_tokens_cpu += 1
|
||||
if common_attn_metadata.seq_lens_cpu_upper_bound is not None:
|
||||
common_attn_metadata.seq_lens_cpu_upper_bound += 1
|
||||
if not self.constant_draft_positions:
|
||||
positions = self._update_positions_dependent_metadata(
|
||||
positions,
|
||||
common_attn_metadata,
|
||||
batch_size,
|
||||
input_batch_size,
|
||||
block_size,
|
||||
)
|
||||
|
||||
# Rebuild attention metadata
|
||||
_, per_layer_attn_metadata = self.build_per_group_and_layer_attn_metadata(
|
||||
common_attn_metadata, draft_index=token_index + 1
|
||||
)
|
||||
# Rebuild attention metadata. When draft positions are constant
|
||||
# (e.g. Gemma4 MTP), common_attn_metadata is invariant across
|
||||
# loop iterations so we build once and reuse.
|
||||
if not self.constant_draft_positions or token_index == 0:
|
||||
_, per_layer_attn_metadata = (
|
||||
self.build_per_group_and_layer_attn_metadata(
|
||||
common_attn_metadata, draft_index=token_index + 1
|
||||
)
|
||||
)
|
||||
|
||||
# copy inputs to buffer for cudagraph
|
||||
self.input_ids[:batch_size] = input_ids
|
||||
@@ -654,6 +632,58 @@ class SpecDecodeBaseProposer:
|
||||
draft_token_ids = torch.stack(draft_token_ids_list, dim=1)
|
||||
return draft_token_ids
|
||||
|
||||
def _update_positions_dependent_metadata(
|
||||
self,
|
||||
positions: torch.Tensor,
|
||||
common_attn_metadata,
|
||||
batch_size: int,
|
||||
input_batch_size: int,
|
||||
block_size: int,
|
||||
) -> torch.Tensor:
|
||||
"""Update positions, slot mappings, and sequence metadata for the
|
||||
next draft step. Returns the updated positions tensor."""
|
||||
positions_1d = positions[0] if self.uses_mrope else positions
|
||||
if self.uses_mrope:
|
||||
out_pos = self.mrope_positions[0, :batch_size]
|
||||
elif self.uses_xdrope_dim > 0 and self.draft_uses_xdrope_dim > 0:
|
||||
out_pos = self.xdrope_positions[0, :batch_size]
|
||||
else:
|
||||
out_pos = self.positions[:batch_size]
|
||||
eagle_step_update_slot_mapping_and_metadata(
|
||||
positions_1d=positions_1d,
|
||||
block_table_tensor=common_attn_metadata.block_table_tensor,
|
||||
seq_lens=common_attn_metadata.seq_lens,
|
||||
block_size=block_size,
|
||||
max_model_len=self.max_model_len,
|
||||
out_clamped_positions=out_pos,
|
||||
out_slot_mapping=self._slot_mapping_buffer[:input_batch_size],
|
||||
input_batch_size=input_batch_size,
|
||||
)
|
||||
common_attn_metadata.slot_mapping = self._slot_mapping_buffer[:batch_size]
|
||||
if self.uses_mrope:
|
||||
self.mrope_positions[1:, :batch_size] = self.mrope_positions[0, :batch_size]
|
||||
positions = self.mrope_positions[:, :batch_size]
|
||||
elif self.uses_xdrope_dim > 0 and self.draft_uses_xdrope_dim > 0:
|
||||
self.xdrope_positions[1:, :batch_size] = self.xdrope_positions[
|
||||
0, :batch_size
|
||||
]
|
||||
positions = self.xdrope_positions[0, :batch_size]
|
||||
else:
|
||||
positions = self.positions[:batch_size]
|
||||
common_attn_metadata.max_seq_len = min(
|
||||
common_attn_metadata.max_seq_len + 1,
|
||||
self.max_model_len,
|
||||
)
|
||||
|
||||
if common_attn_metadata._seq_lens_cpu is not None:
|
||||
common_attn_metadata._seq_lens_cpu += 1
|
||||
if common_attn_metadata._num_computed_tokens_cpu is not None:
|
||||
common_attn_metadata._num_computed_tokens_cpu += 1
|
||||
if common_attn_metadata.seq_lens_cpu_upper_bound is not None:
|
||||
common_attn_metadata.seq_lens_cpu_upper_bound += 1
|
||||
|
||||
return positions
|
||||
|
||||
def set_inputs_first_pass(
|
||||
self,
|
||||
target_token_ids: torch.Tensor,
|
||||
|
||||
@@ -169,6 +169,7 @@ from vllm.v1.spec_decode.dflash import DFlashProposer
|
||||
from vllm.v1.spec_decode.draft_model import DraftModelProposer
|
||||
from vllm.v1.spec_decode.eagle import EagleProposer
|
||||
from vllm.v1.spec_decode.extract_hidden_states import ExtractHiddenStatesProposer
|
||||
from vllm.v1.spec_decode.gemma4 import Gemma4Proposer
|
||||
from vllm.v1.spec_decode.medusa import MedusaProposer
|
||||
from vllm.v1.spec_decode.metadata import SpecDecodeMetadata
|
||||
from vllm.v1.spec_decode.ngram_proposer_gpu import (
|
||||
@@ -524,6 +525,7 @@ class GPUModelRunner(
|
||||
| DraftModelProposer
|
||||
| MedusaProposer
|
||||
| ExtractHiddenStatesProposer
|
||||
| Gemma4Proposer
|
||||
)
|
||||
if self.speculative_config.method == "ngram":
|
||||
from vllm.v1.spec_decode.ngram_proposer import NgramProposer
|
||||
@@ -552,6 +554,8 @@ class GPUModelRunner(
|
||||
self._ngram_pinned_val_buf = torch.zeros(
|
||||
self.max_num_reqs, dtype=torch.int32, pin_memory=True
|
||||
)
|
||||
elif self.speculative_config.use_gemma4_mtp():
|
||||
self.drafter = Gemma4Proposer(self.vllm_config, self.device, self)
|
||||
elif self.speculative_config.use_dflash():
|
||||
self.drafter = DFlashProposer(self.vllm_config, self.device, self)
|
||||
self.use_aux_hidden_state_outputs = True
|
||||
@@ -2310,11 +2314,18 @@ class GPUModelRunner(
|
||||
cm.slot_mapping = slot_mappings[kv_cache_gid]
|
||||
|
||||
if self.speculative_config and spec_decode_common_attn_metadata is None:
|
||||
if isinstance(self.drafter, (EagleProposer, DFlashProposer)):
|
||||
if isinstance(
|
||||
self.drafter, (EagleProposer, DFlashProposer, Gemma4Proposer)
|
||||
):
|
||||
if self.drafter.kv_cache_gid == kv_cache_gid:
|
||||
spec_decode_common_attn_metadata = cm
|
||||
else:
|
||||
spec_decode_common_attn_metadata = cm
|
||||
# Capture per-group block tables for multi-group proposers.
|
||||
if self.speculative_config and isinstance(self.drafter, Gemma4Proposer):
|
||||
self.drafter.set_per_group_block_table(
|
||||
kv_cache_gid, cm.block_table_tensor
|
||||
)
|
||||
|
||||
for attn_gid in range(len(self.attn_groups[kv_cache_gid])):
|
||||
if ubatch_slices is not None:
|
||||
@@ -4276,7 +4287,8 @@ class GPUModelRunner(
|
||||
EagleProposer
|
||||
| DFlashProposer
|
||||
| DraftModelProposer
|
||||
| ExtractHiddenStatesProposer,
|
||||
| ExtractHiddenStatesProposer
|
||||
| Gemma4Proposer,
|
||||
)
|
||||
sampled_token_ids = sampler_output.sampled_token_ids
|
||||
if input_fits_in_drafter:
|
||||
@@ -4672,7 +4684,8 @@ class GPUModelRunner(
|
||||
or spec_config.uses_draft_model()
|
||||
):
|
||||
assert isinstance(
|
||||
self.drafter, EagleProposer | DFlashProposer | DraftModelProposer
|
||||
self.drafter,
|
||||
EagleProposer | DFlashProposer | DraftModelProposer | Gemma4Proposer,
|
||||
)
|
||||
|
||||
if spec_config.disable_padded_drafter_batch:
|
||||
@@ -5594,7 +5607,8 @@ class GPUModelRunner(
|
||||
EagleProposer
|
||||
| DFlashProposer
|
||||
| DraftModelProposer
|
||||
| ExtractHiddenStatesProposer,
|
||||
| ExtractHiddenStatesProposer
|
||||
| Gemma4Proposer,
|
||||
)
|
||||
assert self.speculative_config is not None
|
||||
# Eagle currently only supports PIECEWISE cudagraphs.
|
||||
@@ -6395,7 +6409,8 @@ class GPUModelRunner(
|
||||
or self.speculative_config.uses_draft_model()
|
||||
):
|
||||
assert isinstance(
|
||||
self.drafter, EagleProposer | DFlashProposer | DraftModelProposer
|
||||
self.drafter,
|
||||
EagleProposer | DFlashProposer | DraftModelProposer | Gemma4Proposer,
|
||||
)
|
||||
self.drafter.initialize_attn_backend(kv_cache_config, kernel_block_sizes)
|
||||
|
||||
@@ -6448,7 +6463,10 @@ class GPUModelRunner(
|
||||
):
|
||||
assert isinstance(
|
||||
self.drafter,
|
||||
EagleProposer | DFlashProposer | ExtractHiddenStatesProposer,
|
||||
EagleProposer
|
||||
| DFlashProposer
|
||||
| ExtractHiddenStatesProposer
|
||||
| Gemma4Proposer,
|
||||
)
|
||||
self.drafter.initialize_cudagraph_keys(cudagraph_mode)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user