[MiniMax-M3] Decode: paged K/V tile loads and a tiny GEMM for the router projection (#38841)
Co-authored-by: Hao Phan <htphan@nvidia.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Hao Phan
Claude Opus 5.5
parent
6e6ccb26f2
commit
11b1fb21de
@@ -88,6 +88,7 @@ def _gqa_share_sparse_decode_kernel(
|
||||
BATCH_SIZE_BUCKET: tl.constexpr,
|
||||
BLOCK_SIZE_H: tl.constexpr,
|
||||
BLOCK_SIZE_N: tl.constexpr,
|
||||
PAGED_CONTIG: tl.constexpr,
|
||||
BLOCK_SIZE_D: tl.constexpr,
|
||||
BLOCK_SIZE_T: tl.constexpr,
|
||||
NUM_TOPK_CHUNKS: tl.constexpr,
|
||||
@@ -184,13 +185,22 @@ def _gqa_share_sparse_decode_kernel(
|
||||
other=0,
|
||||
).to(tl.int64)
|
||||
hisparse_topk_counter = hisparse_topk_counter + 1
|
||||
slots = (slots + max_slots) % max_slots # safety against negative
|
||||
elif PAGED_CONTIG:
|
||||
# A block lies inside one page, and slots are contiguous within a page,
|
||||
# so the block's first slot fixes the whole tile.
|
||||
base_slot = tl.load(
|
||||
req_to_token_ptr + sid * stride_r2t_b + c, mask=c < seq_len, other=0
|
||||
).to(tl.int64)
|
||||
base_slot = (base_slot + max_slots) % max_slots
|
||||
slots = base_slot + off_n.to(tl.int64)
|
||||
else:
|
||||
slots = tl.load(
|
||||
req_to_token_ptr + sid * stride_r2t_b + pos,
|
||||
mask=pos_mask,
|
||||
other=0,
|
||||
).to(tl.int64)
|
||||
slots = (slots + max_slots) % max_slots # safety against negative
|
||||
slots = (slots + max_slots) % max_slots # safety against negative
|
||||
# load K as (head_dim, BLOCK_SIZE_N) via indirect addressing
|
||||
k_off = (
|
||||
slots[None, :] * stride_k_s
|
||||
@@ -335,6 +345,7 @@ def flash_decode_with_gqa_share_sparse(
|
||||
topk_idx: torch.Tensor, # [num_kv_heads, batch_size, topk]
|
||||
sm_scale: Optional[float] = None,
|
||||
use_tma: bool = True,
|
||||
page_size: int = 0,
|
||||
q_scale: Optional[float] = None,
|
||||
k_scale: Optional[float] = None,
|
||||
v_scale: Optional[float] = None,
|
||||
@@ -353,6 +364,9 @@ def flash_decode_with_gqa_share_sparse(
|
||||
f"block_size must be a power of 2, but got {block_size}"
|
||||
)
|
||||
# assert slot_ids.max() < max_slots, f"get slot_ids {slot_ids}, but kv_cache shape is {kv_cache.shape}"
|
||||
# A selected block cannot straddle a page boundary under this condition, so its
|
||||
# slots are contiguous. page_size == 0 means unknown: keep the gather.
|
||||
paged_contig = page_size >= block_size and page_size % block_size == 0
|
||||
max_kv_len = req_to_token.shape[1]
|
||||
# gqa
|
||||
assert num_q_heads % num_kv_heads == 0
|
||||
@@ -439,6 +453,7 @@ def flash_decode_with_gqa_share_sparse(
|
||||
lse_partial.stride(1),
|
||||
lse_partial.stride(2),
|
||||
BLOCK_SIZE_N=block_size,
|
||||
PAGED_CONTIG=paged_contig,
|
||||
NUM_TOPK_CHUNKS=NUM_TOPK_CHUNKS,
|
||||
IS_FP8=is_fp8,
|
||||
)
|
||||
|
||||
@@ -454,6 +454,7 @@ def minimax_sparse_decode(
|
||||
block_size=block_size_k,
|
||||
topk_idx=topk_idx,
|
||||
sm_scale=sm_scale,
|
||||
page_size=page_size,
|
||||
q_scale=q_scale,
|
||||
k_scale=k_scale,
|
||||
v_scale=v_scale,
|
||||
@@ -471,6 +472,7 @@ def minimax_sparse_decode(
|
||||
block_size=block_size_k,
|
||||
topk_idx=topk_idx,
|
||||
sm_scale=sm_scale,
|
||||
page_size=page_size,
|
||||
q_scale=q_scale,
|
||||
k_scale=k_scale,
|
||||
v_scale=v_scale,
|
||||
|
||||
@@ -87,6 +87,7 @@ from sglang.srt.model_loader.weight_utils import (
|
||||
default_weight_loader,
|
||||
maybe_remap_kv_scale_name,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.utils import tiny_router_gemm_max_tokens
|
||||
from sglang.srt.models.minimax_m2 import MiniMaxM2RMSNormTP
|
||||
from sglang.srt.models.utils import WeightsMapper
|
||||
from sglang.srt.runtime_context import get_exec, get_parallel, get_stream
|
||||
@@ -116,6 +117,12 @@ if _is_gfx95_supported:
|
||||
else:
|
||||
router_gemv = router_gemv_supported = None
|
||||
|
||||
# Import-time CUDA kernels would block processor imports on CPU CI.
|
||||
if _is_cuda:
|
||||
from sglang.kernels.ops.gemm.tiny_gemm import tiny_gemm_bf16
|
||||
else:
|
||||
tiny_gemm_bf16 = None
|
||||
|
||||
_FP8_KV_DTYPES = (
|
||||
torch.float8_e4m3fn,
|
||||
torch.float8_e5m2,
|
||||
@@ -418,6 +425,11 @@ class MiniMaxM3MoE(nn.Module):
|
||||
quant_config=None,
|
||||
prefix=add_prefix("gate", prefix),
|
||||
)
|
||||
self.tiny_router_gemm_max_tokens = tiny_router_gemm_max_tokens(
|
||||
num_experts=config.num_local_experts,
|
||||
hidden_size=config.hidden_size,
|
||||
weight_dtype=self.gate.weight.dtype,
|
||||
)
|
||||
|
||||
self.layer_id = layer_id
|
||||
|
||||
@@ -520,6 +532,18 @@ class MiniMaxM3MoE(nn.Module):
|
||||
if _is_npu:
|
||||
# NPU lacks aten::mm.dtype; bf16 mm then cast keeps topk semantics.
|
||||
return torch.mm(hidden_states, self.gate.weight.t()).float()
|
||||
if (
|
||||
not get_exec().deterministic.enable_deterministic_inference
|
||||
and hidden_states.shape[0] <= self.tiny_router_gemm_max_tokens
|
||||
):
|
||||
# N is num_local_experts, so cuBLAS splits K and runs the
|
||||
# projection as two launches; the tiny GEMM does it in one.
|
||||
return tiny_gemm_bf16(
|
||||
hidden_states,
|
||||
self.gate.weight,
|
||||
out_dtype=torch.float32,
|
||||
max_m=self.tiny_router_gemm_max_tokens,
|
||||
)
|
||||
return torch.mm(
|
||||
hidden_states, self.gate.weight.t(), out_dtype=torch.float32
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user