[Kernel] TD operand loads for batched MoE GEMM (moe_mmk) on XPU (#46340)
Signed-off-by: oonyshch <xonyshch@gmail.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com>
This commit is contained in:
co-authored by
Kunshang Ji
parent
d30b1ecd1b
commit
0111002323
@@ -32,7 +32,15 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
kFp8StaticTensorSym,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.triton_utils import tl, triton, use_tensor_descriptor
|
||||
from vllm.triton_utils.allocation import set_triton_allocator
|
||||
|
||||
|
||||
def _is_capturing_or_compiling() -> bool:
|
||||
# torch.cuda.is_current_stream_capturing() is unavailable on non-CUDA (XPU) torch.
|
||||
return torch.compiler.is_compiling() or (
|
||||
current_platform.is_cuda_alike() and torch.cuda.is_current_stream_capturing()
|
||||
)
|
||||
|
||||
|
||||
@triton.jit
|
||||
@@ -71,9 +79,32 @@ def moe_mmk(
|
||||
use_w8a8: tl.constexpr,
|
||||
use_w8a16: tl.constexpr,
|
||||
per_act_token_quant: tl.constexpr,
|
||||
# TD: a_base_ptr/b_base_ptr are the expert/CTA-offset bases of A[M,K]/B[N,K].
|
||||
a_base_ptr=None,
|
||||
b_base_ptr=None,
|
||||
M=0,
|
||||
N=0,
|
||||
stride_am: tl.int64 = 0,
|
||||
stride_bn: tl.int64 = 0,
|
||||
USE_TD: tl.constexpr = False,
|
||||
):
|
||||
offs_k = tl.arange(0, BLOCK_K)
|
||||
|
||||
if USE_TD:
|
||||
# make_tensor_descriptor requires the last (K) stride to be a
|
||||
# compile-time 1; the launcher only enables USE_TD for K-contiguous A/B.
|
||||
a_desc = tl.make_tensor_descriptor(
|
||||
a_base_ptr,
|
||||
shape=[M, K],
|
||||
strides=[stride_am, 1],
|
||||
block_shape=[BLOCK_M, BLOCK_K],
|
||||
)
|
||||
b_desc = tl.make_tensor_descriptor(
|
||||
b_base_ptr,
|
||||
shape=[N, K],
|
||||
strides=[stride_bn, 1],
|
||||
block_shape=[BLOCK_N, BLOCK_K],
|
||||
)
|
||||
if use_w8a16:
|
||||
b_scale_ptrs = (
|
||||
b_scale_ptr + expert_id * stride_bse + offs_n[None, :] * stride_bsn
|
||||
@@ -110,12 +141,17 @@ def moe_mmk(
|
||||
for k in range(0, tl.cdiv(K, BLOCK_K)):
|
||||
# Load the next block of A and B, generate a mask by checking the
|
||||
# K dimension.
|
||||
a = tl.load(
|
||||
a_ptrs,
|
||||
mask=mask_m[:, None] & (offs_k[None, :] < K - k * BLOCK_K),
|
||||
other=0.0,
|
||||
)
|
||||
b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_K, other=0.0)
|
||||
if USE_TD:
|
||||
# B is [N, K]; tile is [BLOCK_N, BLOCK_K], transposed for dot.
|
||||
a = a_desc.load([0, k * BLOCK_K])
|
||||
b = tl.trans(b_desc.load([0, k * BLOCK_K]))
|
||||
else:
|
||||
a = tl.load(
|
||||
a_ptrs,
|
||||
mask=mask_m[:, None] & (offs_k[None, :] < K - k * BLOCK_K),
|
||||
other=0.0,
|
||||
)
|
||||
b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_K, other=0.0)
|
||||
# We accumulate along the K dimension.
|
||||
if use_w8a16:
|
||||
accumulator = tl.dot(a, b.to(compute_type), acc=accumulator)
|
||||
@@ -193,6 +229,7 @@ def expert_triton_kernel(
|
||||
BLOCK_M: tl.constexpr,
|
||||
BLOCK_N: tl.constexpr,
|
||||
BLOCK_K: tl.constexpr,
|
||||
USE_TD: tl.constexpr = False,
|
||||
):
|
||||
offs_m = tl.arange(0, BLOCK_M)
|
||||
offs_n = tl.arange(0, BLOCK_N) % N
|
||||
@@ -238,6 +275,13 @@ def expert_triton_kernel(
|
||||
use_fp8_w8a8,
|
||||
use_int8_w8a16,
|
||||
per_act_token_quant,
|
||||
a_ptr,
|
||||
b_ptr,
|
||||
M,
|
||||
N,
|
||||
stride_am,
|
||||
stride_bn,
|
||||
USE_TD,
|
||||
)
|
||||
|
||||
# store in C
|
||||
@@ -292,6 +336,7 @@ def batched_triton_kernel(
|
||||
BLOCK_M: tl.constexpr,
|
||||
BLOCK_N: tl.constexpr,
|
||||
BLOCK_K: tl.constexpr,
|
||||
USE_TD: tl.constexpr = False,
|
||||
):
|
||||
expert_id = tl.program_id(axis=0)
|
||||
e_num_tokens = tl.load(expert_num_tokens + expert_id)
|
||||
@@ -372,6 +417,7 @@ def batched_triton_kernel(
|
||||
BLOCK_M,
|
||||
BLOCK_N,
|
||||
BLOCK_K,
|
||||
USE_TD,
|
||||
)
|
||||
|
||||
|
||||
@@ -444,6 +490,18 @@ def invoke_moe_batched_triton_kernel(
|
||||
stride_asm = 0
|
||||
stride_ask = 0
|
||||
|
||||
use_td = (
|
||||
use_tensor_descriptor()
|
||||
and A.stride(2) == 1
|
||||
and B.stride(2) == 1
|
||||
and (K * A.element_size()) % 16 == 0
|
||||
and (BLOCK_M & (BLOCK_M - 1)) == 0
|
||||
and (BLOCK_N & (BLOCK_N - 1)) == 0
|
||||
and (BLOCK_K & (BLOCK_K - 1)) == 0
|
||||
)
|
||||
if use_td:
|
||||
set_triton_allocator(A.device)
|
||||
|
||||
batched_triton_kernel[grid](
|
||||
A,
|
||||
B,
|
||||
@@ -485,6 +543,7 @@ def invoke_moe_batched_triton_kernel(
|
||||
BLOCK_M=BLOCK_M,
|
||||
BLOCK_N=BLOCK_N,
|
||||
BLOCK_K=BLOCK_K,
|
||||
USE_TD=use_td,
|
||||
)
|
||||
|
||||
|
||||
@@ -616,10 +675,7 @@ class NaiveBatchedExperts(mk.FusedMoEExpertsModular):
|
||||
|
||||
for expert in range(num_local_experts):
|
||||
# Indexing expert_num_tokens doesn't work w/cudagraphs or inductor
|
||||
if (
|
||||
torch.compiler.is_compiling()
|
||||
or torch.cuda.is_current_stream_capturing()
|
||||
):
|
||||
if _is_capturing_or_compiling():
|
||||
num = hidden_states.shape[1]
|
||||
else:
|
||||
num = int(expert_num_tokens[expert].item())
|
||||
@@ -659,7 +715,7 @@ def batched_moe_kernel_quantize_input(
|
||||
per_act_token_quant: bool,
|
||||
block_shape: list[int] | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
if torch.compiler.is_compiling() or torch.cuda.is_current_stream_capturing():
|
||||
if _is_capturing_or_compiling():
|
||||
# Note: this does a bunch of extra work because expert_num_tokens is
|
||||
# ignored but it does support torch.compile + cudagraphs.
|
||||
hidden_dim = A.size(-1)
|
||||
@@ -745,7 +801,7 @@ class BatchedTritonExperts(mk.FusedMoEExpertsModular):
|
||||
|
||||
@staticmethod
|
||||
def _supports_current_device() -> bool:
|
||||
return current_platform.is_cuda_alike()
|
||||
return current_platform.is_cuda_alike() or current_platform.is_xpu()
|
||||
|
||||
@staticmethod
|
||||
def _supports_no_act_and_mul() -> bool:
|
||||
|
||||
Reference in New Issue
Block a user