[Model][DSv4.1] FlashMLA mega attention and the NVFP4 compressed KV cache (#56935)

Signed-off-by: Yongye Zhu <yongye@inferact.ai>
Signed-off-by: 云挚 <ningyunxiao.nyx@antgroup.com>
Signed-off-by: Yongye Zhu <zyy1102000@gmail.com>
Co-authored-by: Yongye Zhu <yongye@inferact.ai>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Yunxiao Ning <73625538+foraxe@users.noreply.github.com>
Co-authored-by: AI Assistant <noreply@openai.com>
This commit is contained in:
Yongye Zhu
2026-09-16 03:07:50 -07:00
committed by GitHub
co-authored by Yongye Zhu Claude Opus 5 Yunxiao Ning AI Assistant
parent ceb87de065
commit d6a1677d55
20 changed files with 1811 additions and 156 deletions
@@ -3,7 +3,10 @@
* SPDX-FileCopyrightText: Copyright contributors to the vLLM project
*
* Horizontally-fused DeepseekV4-MLA kernel:
* - Q side: optional per-head RMSNorm + GPT-J RoPE on last ROPE_DIM
* - Q side: optional per-head RMSNorm (apply_q_norm) and GPT-J RoPE on the
* last ROPE_DIM (apply_q_rope), plus the zero-pad to
* q_head_padded, in the head-major layout or (is_q_interleaved)
* FlashMLA's mega-attention chunk-interleaved one
* - KV side: GPT-J RoPE on last ROPE_DIM + UE8M0 FP8 quant on NoPE + paged
* cache insert
*
@@ -174,6 +177,30 @@ __device__ __forceinline__ float warpSum(float val) {
return val;
}
// Offset of the 16 elements a lane owns inside a Q tensor of `num_heads`
// heads. The default layout is head-major: a head's 512 dims are contiguous.
// `Q_INTERLEAVED` is FlashMLA's mega-attention layout, 32 chunks of
// [num_heads, 16] per token, which changes the address only -- lane L still
// owns dims [16L, 16L+16) of its head either way, so `dim_base` stays the
// true dim offset and the norm and RoPE below are equally valid in both.
// What it does change is where the padding heads land: at the tail of every
// chunk rather than after the live heads.
template <bool Q_INTERLEAVED>
__device__ __forceinline__ int64_t qElemOffset(int const tokenIdx,
int const slotIdx,
int const laneId,
int const dim_base,
int const num_heads) {
if constexpr (Q_INTERLEAVED) {
return static_cast<int64_t>(tokenIdx) * num_heads * kHeadDim +
static_cast<int64_t>(laneId) * num_heads * kElemsPerLane +
static_cast<int64_t>(slotIdx) * kElemsPerLane;
} else {
return (static_cast<int64_t>(tokenIdx) * num_heads + slotIdx) * kHeadDim +
dim_base;
}
}
// ────────────────────────────────────────────────────────────────────────────
// Per-slot inner pipeline
// ────────────────────────────────────────────────────────────────────────────
@@ -192,10 +219,16 @@ __device__ __forceinline__ float warpSum(float val) {
// + paged-cache
// insert)
//
// `kv_mxfp8` is grid-uniform and only steers the KV branch, so it stays a
// runtime argument rather than doubling every (head count, q-norm)
// instantiation of a kernel whose hot path is the Q branch.
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM>
// `kv_mxfp8` and `apply_q_rope` are grid-uniform, so they stay runtime
// arguments rather than multiplying every (head count, q-norm, q-layout)
// instantiation of a kernel whose hot path is the Q branch. `Q_INTERLEAVED`
// cannot: it picks the address arithmetic, which has to fold.
//
// The three Q knobs are independent. The mega-attention layer uses
// Q_INTERLEAVED with both norm and RoPE off, because its attention kernel
// applies those itself -- not because the layout forbids them.
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM,
bool Q_INTERLEAVED = false>
__device__ __forceinline__ void processDeepseekV4Slot(
uint4 v0, uint4 v1, int const tokenIdx, int const slotIdx,
int const dim_base, int const laneId, int const num_heads_q,
@@ -203,7 +236,7 @@ __device__ __forceinline__ void processDeepseekV4Slot(
uint8_t* __restrict__ k_cache, int64_t const* __restrict__ slot_mapping,
int64_t const* __restrict__ position_ids,
float const* __restrict__ cos_sin_cache, int const cache_block_size,
int const kv_block_stride, bool const kv_mxfp8) {
int const kv_block_stride, bool const kv_mxfp8, bool const apply_q_rope) {
using Converter = vllm::_typeConvert<scalar_t_in>;
bool const isKV = (slotIdx == kNumHeadsQPadded);
bool const isPadQ = !isKV && (slotIdx >= num_heads_q);
@@ -213,10 +246,8 @@ __device__ __forceinline__ void processDeepseekV4Slot(
// zero literal is correct. Matches the live-Q branch's vectorized store.
if (isPadQ) {
scalar_t_in* dst =
q_out +
(static_cast<int64_t>(tokenIdx) * kNumHeadsQPadded + slotIdx) *
kHeadDim +
dim_base;
q_out + qElemOffset<Q_INTERLEAVED>(tokenIdx, slotIdx, laneId, dim_base,
kNumHeadsQPadded);
uint4 const zero4 = {0u, 0u, 0u, 0u};
*reinterpret_cast<uint4*>(dst) = zero4;
*reinterpret_cast<uint4*>(dst + 8) = zero4;
@@ -264,7 +295,9 @@ __device__ __forceinline__ void processDeepseekV4Slot(
// ── GPT-J RoPE on dims [NOPE_DIM, HEAD_DIM) ─────────────────────────────
// All math in fp32. cos_sin_cache is loaded as fp32 (its native storage).
bool const is_rope_lane = dim_base >= kNopeDim;
// KV is always rotated; Q only when the caller has not already had it
// rotated elsewhere (the mega-attention kernel does its own).
bool const is_rope_lane = dim_base >= kNopeDim && (isKV || apply_q_rope);
if (is_rope_lane) {
int64_t const pos = position_ids[tokenIdx];
constexpr int kHalfRope = kRopeDim / 2;
@@ -314,10 +347,8 @@ __device__ __forceinline__ void processDeepseekV4Slot(
make_float2(elements[8 + 2 * i], elements[8 + 2 * i + 1]));
}
scalar_t_in* dst =
q_out +
(static_cast<int64_t>(tokenIdx) * kNumHeadsQPadded + slotIdx) *
kHeadDim +
dim_base;
q_out + qElemOffset<Q_INTERLEAVED>(tokenIdx, slotIdx, laneId, dim_base,
kNumHeadsQPadded);
*reinterpret_cast<uint4*>(dst) = out0;
*reinterpret_cast<uint4*>(dst + 8) = out1;
} else {
@@ -432,14 +463,17 @@ __device__ __forceinline__ void processDeepseekV4Slot(
// `kNumHeadsQPadded` is a template parameter (compile-time constant) so the
// divisions in the grid math and the KV-sentinel comparison fold to fast
// constant operations. The launch wrapper dispatches the runtime value to
// the matching instantiation.
// the matching instantiation. `kNumHeadsQPadded == 0` leaves one slot per
// token and is the KV-insert-only instantiation, for a Q that is already in
// the shape its attention kernel wants.
//
// With DP padding, q/kv/position_ids can have more rows than slot_mapping.
// The live-Q and pad-Q branches cover all `num_tokens_full` rows (downstream
// attention uses them). The KV branch only inserts the first
// `num_tokens_insert` tokens (= slot_mapping length) into the paged cache.
//
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM>
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM,
bool Q_INTERLEAVED>
__global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel(
scalar_t_in const* __restrict__ q_in, // [N, num_heads_q, 512]
scalar_t_in* __restrict__ q_out, // [N, kNumHeadsQPadded, 512]
@@ -454,7 +488,8 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel(
int const num_heads_q, // live Q heads (input layout)
int const cache_block_size, // tokens per paged-cache block
int const kv_block_stride, // bytes per paged-cache block
bool const kv_mxfp8) { // V4.1 all-dims MXFP8 record
bool const kv_mxfp8, // V4.1 all-dims MXFP8 record
bool const apply_q_rope) { // rotate Q too, not just KV
#if (!defined(__CUDA_ARCH__) || __CUDA_ARCH__ < 800) && !defined(USE_ROCM)
// BF16 _typeConvert specialization is unavailable on pre-Ampere. The
// DeepseekV4 kernel only runs with bf16 inputs in practice, so compile a
@@ -497,20 +532,18 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel(
if (isKV) {
src_ptr = kv_in + static_cast<int64_t>(tokenIdx) * kHeadDim + dim_base;
} else {
int64_t const q_row_offset =
(static_cast<int64_t>(tokenIdx) * num_heads_q + slotIdx) *
kHeadDim +
dim_base;
src_ptr = q_in + q_row_offset;
src_ptr = q_in + qElemOffset<Q_INTERLEAVED>(tokenIdx, slotIdx, laneId,
dim_base, num_heads_q);
}
v0 = *reinterpret_cast<uint4 const*>(src_ptr);
v1 = *reinterpret_cast<uint4 const*>(src_ptr + 8);
}
processDeepseekV4Slot<scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM>(
processDeepseekV4Slot<scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM,
Q_INTERLEAVED>(
v0, v1, tokenIdx, slotIdx, dim_base, laneId, num_heads_q, eps, q_out,
k_cache, slot_mapping, position_ids, cos_sin_cache, cache_block_size,
kv_block_stride, kv_mxfp8);
kv_block_stride, kv_mxfp8, apply_q_rope);
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900)
cudaTriggerProgrammaticLaunchCompletion();
@@ -530,7 +563,8 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel(
// Q branch (optional RMSNorm + RoPE, in place) head_slot == num_heads_q
// KV branch (RoPE + UE8M0 quant + insert)
//
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM>
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM,
bool Q_INTERLEAVED>
__global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid(
scalar_t_in const* __restrict__ q_in, scalar_t_in* __restrict__ q_out,
scalar_t_in const* __restrict__ kv_in, uint8_t* __restrict__ k_cache,
@@ -539,7 +573,7 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid(
float const* __restrict__ cos_sin_cache, float const eps,
int const num_tokens_full, int const num_tokens_insert,
int const num_heads_q, int const cache_block_size,
int const kv_block_stride, bool const kv_mxfp8) {
int const kv_block_stride, bool const kv_mxfp8, bool const apply_q_rope) {
#if (!defined(__CUDA_ARCH__) || __CUDA_ARCH__ < 800) && !defined(USE_ROCM)
if constexpr (std::is_same_v<scalar_t_in, c10::BFloat16>) {
return;
@@ -569,11 +603,8 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid(
if (s == kNumHeadsQPadded) {
src = kv_in + static_cast<int64_t>(tokenIdx) * kHeadDim + dim_base;
} else {
src = q_in +
(static_cast<int64_t>(tokenIdx) * num_heads_q +
static_cast<int64_t>(s)) *
kHeadDim +
dim_base;
src = q_in + qElemOffset<Q_INTERLEAVED>(tokenIdx, s, laneId, dim_base,
num_heads_q);
}
va = *reinterpret_cast<uint4 const*>(src);
vb = *reinterpret_cast<uint4 const*>(src + 8);
@@ -594,10 +625,12 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid(
load_slot(next_slot, v0_next, v1_next);
}
processDeepseekV4Slot<scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM>(
processDeepseekV4Slot<scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM,
Q_INTERLEAVED>(
v0_curr, v1_curr, tokenIdx, curr_slot, dim_base, laneId,
num_heads_q, eps, q_out, k_cache, slot_mapping, position_ids,
cos_sin_cache, cache_block_size, kv_block_stride, kv_mxfp8);
cos_sin_cache, cache_block_size, kv_block_stride, kv_mxfp8,
apply_q_rope);
// ── Buffer rotation: hand the prefetched LDGs to the next iter.
v0_curr = v0_next;
@@ -617,14 +650,15 @@ __global__ void fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid(
// ────────────────────────────────────────────────────────────────────────────
// Launch wrapper
// ────────────────────────────────────────────────────────────────────────────
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM>
template <typename scalar_t_in, int kNumHeadsQPadded, bool APPLY_Q_NORM,
bool Q_INTERLEAVED>
static void launchFusedDeepseekV4Templated(
scalar_t_in const* q_in, scalar_t_in* q_out, scalar_t_in const* kv_in,
uint8_t* k_cache, int64_t const* slot_mapping, int64_t const* position_ids,
float const* cos_sin_cache, float const eps, int const num_tokens_full,
int const num_tokens_insert, int const num_heads_q,
int const cache_block_size, int const kv_block_stride, bool const kv_mxfp8,
cudaStream_t stream) {
bool const apply_q_rope, cudaStream_t stream) {
constexpr int kBlockSize = 256;
constexpr int kWarpsPerBlock = kBlockSize / 32;
int64_t const total_warps =
@@ -663,37 +697,40 @@ static void launchFusedDeepseekV4Templated(
// grid instead. Only reachable above NUM_TOKEN_CUTOFF tokens in a single
// insert, where it is worth ~2x.
if (kNumHeadsQPadded == 0 || num_tokens_full < NUM_TOKEN_CUTOFF) {
cudaLaunchKernelEx(&config,
fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel<
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM>,
q_in, q_out, 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,
kv_mxfp8);
cudaLaunchKernelEx(
&config,
fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel<
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM, Q_INTERLEAVED>,
q_in, q_out, 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, kv_mxfp8, apply_q_rope);
} else {
config.gridDim = dim3(num_tokens_full);
cudaLaunchKernelEx(
&config,
fusedDeepseekV4QNormRopeKVRopeQuantInsertKernelReducedGrid<
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM>,
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM, Q_INTERLEAVED>,
q_in, q_out, 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, kv_mxfp8);
kv_block_stride, kv_mxfp8, apply_q_rope);
}
#else
// ROCm: use standard kernel launch syntax (no PDL/stream serialization)
// clang-format off
fusedDeepseekV4QNormRopeKVRopeQuantInsertKernel<
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM>
scalar_t_in, kNumHeadsQPadded, APPLY_Q_NORM, Q_INTERLEAVED>
<<<grid, kBlockSize, 0, stream>>>(
q_in, q_out, 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, kv_mxfp8);
cache_block_size, kv_block_stride, kv_mxfp8, apply_q_rope);
#endif
}
// Runtime dispatch into one of the precompiled `kNumHeadsQPadded`
// instantiations. Supported padded head counts: 8, 16, 32, 64, 128.
// instantiations. Supported padded head counts: 8, 16, 32, 64, 128, plus 0
// for a KV-only launch that writes no Q at all. `apply_q_norm` and
// `is_q_interleaved` are template parameters, `apply_q_rope` is not (see
// processDeepseekV4Slot), so each head count carries four instantiations.
template <typename scalar_t_in>
void launchFusedDeepseekV4QNormRopeKVRopeQuantInsert(
scalar_t_in const* q_in, scalar_t_in* q_out, scalar_t_in const* kv_in,
@@ -702,22 +739,32 @@ void launchFusedDeepseekV4QNormRopeKVRopeQuantInsert(
int const num_tokens_full, int const num_tokens_insert,
int const num_heads_q, int const num_heads_q_padded,
int const cache_block_size, int const kv_block_stride,
bool const apply_q_norm, bool const kv_mxfp8, cudaStream_t stream) {
#define DISPATCH(N) \
case N: \
if (apply_q_norm) { \
launchFusedDeepseekV4Templated<scalar_t_in, N, true>( \
q_in, q_out, 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, kv_mxfp8, stream); \
} else { \
launchFusedDeepseekV4Templated<scalar_t_in, N, false>( \
q_in, q_out, 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, kv_mxfp8, stream); \
} \
bool const apply_q_norm, bool const kv_mxfp8, bool const apply_q_rope,
bool const is_q_interleaved, cudaStream_t stream) {
#define LAUNCH(N, NORM, INTERLEAVED) \
launchFusedDeepseekV4Templated<scalar_t_in, N, NORM, INTERLEAVED>( \
q_in, q_out, 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, kv_mxfp8, apply_q_rope, stream)
#define DISPATCH(N) \
case N: \
if (is_q_interleaved) { \
if (apply_q_norm) { \
LAUNCH(N, true, true); \
} else { \
LAUNCH(N, false, true); \
} \
} else if (apply_q_norm) { \
LAUNCH(N, true, false); \
} else { \
LAUNCH(N, false, false); \
} \
return;
if (num_heads_q_padded == 0) {
LAUNCH(0, false, false);
return;
}
switch (num_heads_q_padded) {
DISPATCH(8)
DISPATCH(16)
@@ -729,9 +776,10 @@ void launchFusedDeepseekV4QNormRopeKVRopeQuantInsert(
"fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert: "
"unsupported num_heads_q_padded=",
num_heads_q_padded,
" (compiled instantiations: 8, 16, 32, 64, 128).");
" (compiled instantiations: 0, 8, 16, 32, 64, 128).");
}
#undef DISPATCH
#undef LAUNCH
}
// ────────────────────────────────────────────────────────────────────────────
@@ -1059,10 +1107,15 @@ void fused_deepseek_v4_kv_rope_insert(
// Zero query heads schedules only the existing KV branch, preserving its
// RoPE rounding and quantization without allocating any query tensors.
if (packed) {
vllm::deepseek_v4_fused_ops::launchFusedDeepseekV4Templated<scalar_t, 0, false>(
// With zero query heads no Q slot is scheduled, so neither Q knob is
// reachable: Q_INTERLEAVED picks addressing nothing uses, and apply_q_rope
// gates a branch only a Q slot takes (KV is rotated unconditionally).
vllm::deepseek_v4_fused_ops::launchFusedDeepseekV4Templated<scalar_t, 0,
false, false>(
nullptr, nullptr, input, cache, slots, positions, cos_sin, 0.0f,
num_tokens, num_tokens, 0, static_cast<int>(cache_block_size),
static_cast<int>(k_cache.stride(0)), kv_mxfp8, stream);
static_cast<int>(k_cache.stride(0)), kv_mxfp8, /*apply_q_rope=*/true,
stream);
} else if (fp8) {
vllm::deepseek_v4_fused_ops::launchFullCacheKernel<scalar_t, false, true>(
nullptr, nullptr, 0, 0, input, cache, slots, positions, cos_sin,
@@ -1085,8 +1138,9 @@ torch::stable::Tensor fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
torch::stable::Tensor const& slot_mapping, // [N] int64
torch::stable::Tensor const& position_ids, // [N] int64
torch::stable::Tensor const& cos_sin_cache, // [max_pos, rope_dim] bf16
int64_t q_head_padded, // padded Q head count for output
double eps, int64_t cache_block_size, bool apply_q_norm, bool kv_mxfp8) {
int64_t q_head_padded, // padded Q head count for output, 0 for no Q
double eps, int64_t cache_block_size, bool apply_q_norm, bool kv_mxfp8,
bool apply_q_rope, bool is_q_interleaved) {
STD_TORCH_CHECK(q_in.device().is_cuda() && q_in.is_contiguous(),
"q_in must be contiguous CUDA");
STD_TORCH_CHECK(kv.device().is_cuda() && kv.is_contiguous(),
@@ -1106,8 +1160,11 @@ torch::stable::Tensor fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
STD_TORCH_CHECK(kv.dim() == 2 && kv.size(1) == 512, "kv shape [N, 512]");
STD_TORCH_CHECK(q_in.scalar_type() == kv.scalar_type(),
"q_in and kv dtype must match");
STD_TORCH_CHECK(q_head_padded >= q_in.size(1),
"q_head_padded must be >= q_in.size(1) (num_heads_q)");
// q_head_padded == 0 asks for the KV insert alone: Q is left untouched and
// an empty tensor comes back, for a caller whose Q is already in the shape
// its attention kernel reads.
STD_TORCH_CHECK(q_head_padded == 0 || q_head_padded >= q_in.size(1),
"q_head_padded must be 0 or >= q_in.size(1) (num_heads_q)");
STD_TORCH_CHECK(k_cache.scalar_type() == torch::headeronly::ScalarType::Byte,
"k_cache must be uint8");
STD_TORCH_CHECK(cos_sin_cache.dim() == 2 && cos_sin_cache.size(1) == 64,
@@ -1138,8 +1195,12 @@ torch::stable::Tensor fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
// Allocate the padded q output. The kernel writes every element (live
// region gets optional RMSNorm+RoPE; pad region gets zeros), so `empty` is
// safe.
auto q_out = torch::stable::new_empty(
q_in, {q_in.size(0), q_head_padded, q_in.size(2)}, q_in.scalar_type());
auto q_out =
q_head_padded == 0
? torch::stable::new_empty(q_in, {int64_t{0}}, q_in.scalar_type())
: torch::stable::new_empty(
q_in, {q_in.size(0), q_head_padded, q_in.size(2)},
q_in.scalar_type());
VLLM_STABLE_DISPATCH_HALF_TYPES(
q_in.scalar_type(), "fused_deepseek_v4_qnorm_rope_kv_insert", [&] {
@@ -1155,7 +1216,8 @@ torch::stable::Tensor fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
cos_sin_cache.const_data_ptr<float>(), static_cast<float>(eps),
num_tokens_full, num_tokens_insert, num_heads_q,
num_heads_q_padded, cache_block_size_i, kv_block_stride,
apply_q_norm, kv_mxfp8, stream);
apply_q_norm, kv_mxfp8, apply_q_rope, is_q_interleaved,
stream);
});
return q_out;
}
+2 -1
View File
@@ -271,7 +271,8 @@ torch::stable::Tensor fused_deepseek_v4_qnorm_rope_kv_rope_quant_insert(
torch::stable::Tensor& k_cache, torch::stable::Tensor const& slot_mapping,
torch::stable::Tensor const& position_ids,
torch::stable::Tensor const& cos_sin_cache, int64_t q_head_padded,
double eps, int64_t cache_block_size, bool apply_q_norm, bool kv_mxfp8);
double eps, int64_t cache_block_size, bool apply_q_norm, bool kv_mxfp8,
bool apply_q_rope, bool is_q_interleaved);
void fused_deepseek_v4_qnorm_rope_kv_rope_full_cache_bf16_insert(
torch::stable::Tensor& q, torch::stable::Tensor const& kv,
+8 -1
View File
@@ -425,6 +425,12 @@ STABLE_TORCH_LIBRARY_FRAGMENT(_C, ops) {
"bool is_neox, Tensor position_ids, "
"int forced_token_heads_per_warp=-1) -> ()");
// q_head_padded is the padded Q head count of the returned tensor, or 0 to
// do the KV insert alone and return an empty tensor. The Q knobs are
// independent: apply_q_norm and apply_q_rope each drop that step for Q
// alone (KV is always rotated), and is_q_interleaved reads and writes Q in
// FlashMLA's mega-attention chunk-interleaved layout, which moves the
// padding heads to the tail of every head-dim chunk.
ops.def(
"fused_deepseek_v4_kv_rope_insert("
"Tensor kv, Tensor! k_cache, Tensor slot_mapping, Tensor position_ids, "
@@ -436,7 +442,8 @@ STABLE_TORCH_LIBRARY_FRAGMENT(_C, ops) {
"Tensor q_in, Tensor kv, Tensor! k_cache, "
"Tensor slot_mapping, Tensor position_ids, Tensor cos_sin_cache, "
"int q_head_padded, float eps, int cache_block_size, "
"bool apply_q_norm=True, bool kv_mxfp8=False) -> Tensor");
"bool apply_q_norm=True, bool kv_mxfp8=False, bool apply_q_rope=True, "
"bool is_q_interleaved=False) -> Tensor");
// FlashInfer V4 full-cache variants: write Q in place (bf16) or to a separate
// FP8 tensor, and KV into a contiguous 512-wide token-strided cache.