Skip to content

Commit d06ba4e

Browse files
jinzhen-linmgoin
andauthored
[Kernel] moe wna16 marlin kernel (#14447)
Signed-off-by: Jinzhen Lin <[email protected]> Co-authored-by: Michael Goin <[email protected]> Co-authored-by: mgoin <[email protected]>
1 parent 6b40996 commit d06ba4e

File tree

16 files changed

+3484
-336
lines changed

16 files changed

+3484
-336
lines changed

CMakeLists.txt

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,21 +609,51 @@ if(VLLM_GPU_LANG STREQUAL "CUDA")
609609
list(APPEND VLLM_MOE_EXT_SRC "${VLLM_MOE_WNA16_SRC}")
610610
cuda_archs_loose_intersection(MARLIN_MOE_ARCHS "8.0;8.6;8.7;8.9;9.0;10.0;10.1;12.0" "${CUDA_ARCHS}")
611611
if (MARLIN_MOE_ARCHS)
612-
set(MARLIN_MOE_SRC
613-
"csrc/moe/marlin_kernels/marlin_moe_kernel.h"
614-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku4b8.h"
615-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku4b8.cu"
616-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku8b128.h"
617-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku8b128.cu"
618-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku4.h"
619-
"csrc/moe/marlin_kernels/marlin_moe_kernel_ku4.cu"
620-
"csrc/moe/marlin_moe_ops.cu")
621612

613+
#
614+
# For the Marlin MOE kernels we automatically generate sources for various
615+
# preselected input type pairs and schedules.
616+
# Generate sources:
617+
set(MOE_MARLIN_GEN_SCRIPT
618+
${CMAKE_CURRENT_SOURCE_DIR}/csrc/moe/marlin_moe_wna16/generate_kernels.py)
619+
file(MD5 ${MOE_MARLIN_GEN_SCRIPT} MOE_MARLIN_GEN_SCRIPT_HASH)
620+
621+
message(STATUS "Marlin MOE generation script hash: ${MOE_MARLIN_GEN_SCRIPT_HASH}")
622+
message(STATUS "Last run Marlin MOE generate script hash: $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH}")
623+
624+
if (NOT DEFINED CACHE{MOE_MARLIN_GEN_SCRIPT_HASH}
625+
OR NOT $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH} STREQUAL ${MOE_MARLIN_GEN_SCRIPT_HASH})
626+
execute_process(
627+
COMMAND ${CMAKE_COMMAND} -E env
628+
PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/csrc/cutlass_extensions/:${CUTLASS_DIR}/python/:${VLLM_PYTHON_PATH}:$PYTHONPATH
629+
${Python_EXECUTABLE} ${MOE_MARLIN_GEN_SCRIPT}
630+
RESULT_VARIABLE moe_marlin_generation_result
631+
OUTPUT_VARIABLE moe_marlin_generation_output
632+
OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log
633+
ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log
634+
)
635+
636+
if (NOT moe_marlin_generation_result EQUAL 0)
637+
message(FATAL_ERROR "Marlin MOE generation failed."
638+
" Result: \"${moe_marlin_generation_result}\""
639+
"\nCheck the log for details: "
640+
"${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log")
641+
else()
642+
set(MOE_MARLIN_GEN_SCRIPT_HASH ${MOE_MARLIN_GEN_SCRIPT_HASH}
643+
CACHE STRING "Last run Marlin MOE generate script hash" FORCE)
644+
message(STATUS "Marlin MOE generation completed successfully.")
645+
endif()
646+
else()
647+
message(STATUS "Marlin MOE generation script has not changed, skipping generation.")
648+
endif()
649+
650+
file(GLOB MOE_WNAA16_MARLIN_SRC "csrc/moe/marlin_moe_wna16/*.cu")
622651
set_gencode_flags_for_srcs(
623-
SRCS "${MARLIN_MOE_SRC}"
652+
SRCS "${MOE_WNAA16_MARLIN_SRC}"
624653
CUDA_ARCHS "${MARLIN_MOE_ARCHS}")
625654

626-
list(APPEND VLLM_MOE_EXT_SRC "${MARLIN_MOE_SRC}")
655+
list(APPEND VLLM_MOE_EXT_SRC ${MOE_WNAA16_MARLIN_SRC})
656+
627657
message(STATUS "Building Marlin MOE kernels for archs: ${MARLIN_MOE_ARCHS}")
628658
else()
629659
message(STATUS "Not building Marlin MOE kernels as no compatible archs found"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
import glob
3+
import itertools
4+
import os
5+
import subprocess
6+
7+
import jinja2
8+
9+
FILE_HEAD = """
10+
// auto generated by generate.py
11+
// clang-format off
12+
13+
#include "kernel.h"
14+
#include "marlin_template.h"
15+
16+
namespace MARLIN_NAMESPACE_NAME {
17+
""".strip()
18+
19+
TEMPLATE = ("template __global__ void Marlin<"
20+
"{{scalar_t}}, "
21+
"{{w_type_id}}, "
22+
"{{threads}}, "
23+
"{{thread_m_blocks}}, "
24+
"{{thread_n_blocks}}, "
25+
"{{thread_k_blocks}}, "
26+
"{{'true' if m_block_size_8 else 'false'}}, "
27+
"{{stages}}, "
28+
"{{'true' if has_act_order else 'false'}}, "
29+
"{{'true' if has_zp else 'false'}}, "
30+
"{{group_blocks}}, "
31+
"{{'true' if is_zp_float else 'false'}}>"
32+
"( MARLIN_KERNEL_PARAMS );")
33+
34+
# int8 with zero point case (vllm::kU8) is also supported,
35+
# we don't add it to reduce wheel size.
36+
SCALAR_TYPES = ["vllm::kU4", "vllm::kU4B8", "vllm::kU8B128"]
37+
THREAD_CONFIGS = [(128, 128, 256), (64, 256, 256), (64, 128, 128)]
38+
39+
THREAD_M_BLOCKS = [0.5, 1, 2, 3, 4]
40+
# group_blocks:
41+
# = 0 : act order case
42+
# = -1 : channelwise quantization
43+
# > 0 : group_size=16*group_blocks
44+
GROUP_BLOCKS = [0, -1, 2, 4, 8]
45+
DTYPES = ["fp16", "bf16"]
46+
47+
48+
def remove_old_kernels():
49+
for filename in glob.glob(os.path.dirname(__file__) + "/kernel_*.cu"):
50+
subprocess.call(["rm", "-f", filename])
51+
52+
53+
def generate_new_kernels():
54+
for scalar_type, dtype in itertools.product(SCALAR_TYPES, DTYPES):
55+
has_zp = "B" not in scalar_type
56+
all_template_str_list = []
57+
58+
for group_blocks, m_blocks, thread_configs in itertools.product(
59+
GROUP_BLOCKS, THREAD_M_BLOCKS, THREAD_CONFIGS):
60+
61+
has_act_order = group_blocks == 0
62+
if has_zp and has_act_order:
63+
continue
64+
if thread_configs[2] == 256:
65+
if m_blocks <= 1 and thread_configs[0] != 128:
66+
continue
67+
if m_blocks > 1 and thread_configs[0] != 64:
68+
continue
69+
70+
k_blocks = thread_configs[0] // 16
71+
n_blocks = thread_configs[1] // 16
72+
threads = thread_configs[2]
73+
74+
c_dtype = "half" if dtype == "fp16" else "nv_bfloat16"
75+
76+
template_str = jinja2.Template(TEMPLATE).render(
77+
scalar_t=c_dtype,
78+
w_type_id=scalar_type + ".id()",
79+
threads=threads,
80+
thread_m_blocks=max(m_blocks, 1),
81+
thread_n_blocks=n_blocks,
82+
thread_k_blocks=k_blocks,
83+
m_block_size_8=m_blocks == 0.5,
84+
stages="pipe_stages",
85+
has_act_order=has_act_order,
86+
has_zp=has_zp,
87+
group_blocks=group_blocks,
88+
is_zp_float=False,
89+
)
90+
91+
all_template_str_list.append(template_str)
92+
93+
file_content = FILE_HEAD + "\n\n"
94+
file_content += "\n\n".join(all_template_str_list) + "\n\n}\n"
95+
filename = f"kernel_{dtype}_{scalar_type[6:].lower()}.cu"
96+
97+
with open(os.path.join(os.path.dirname(__file__), filename), "w") as f:
98+
f.write(file_content)
99+
100+
101+
if __name__ == "__main__":
102+
remove_old_kernels()
103+
generate_new_kernels()

csrc/moe/marlin_moe_wna16/kernel.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#ifndef MARLIN_NAMESPACE_NAME
3+
#define MARLIN_NAMESPACE_NAME marlin_moe_wna16
4+
#endif
5+
6+
#include "quantization/gptq_marlin/marlin.cuh"
7+
#include "quantization/gptq_marlin/marlin_dtypes.cuh"
8+
#include "core/scalar_type.hpp"
9+
10+
#define MARLIN_KERNEL_PARAMS \
11+
const int4 *__restrict__ A, const int4 *__restrict__ B, \
12+
int4 *__restrict__ C, int4 *__restrict__ C_tmp, \
13+
const int4 *__restrict__ scales_ptr, const int4 *__restrict__ zp_ptr, \
14+
const int *__restrict__ g_idx, \
15+
const int32_t *__restrict__ sorted_token_ids_ptr, \
16+
const int32_t *__restrict__ expert_ids_ptr, \
17+
const int32_t *__restrict__ num_tokens_past_padded_ptr, \
18+
const float *__restrict__ topk_weights_ptr, int top_k, \
19+
bool mul_topk_weights, bool is_ep, int num_groups, int prob_m, \
20+
int prob_n, int prob_k, int *locks, bool use_atomic_add, \
21+
bool use_fp32_reduce
22+
23+
namespace MARLIN_NAMESPACE_NAME {
24+
template <typename scalar_t, // compute dtype, half or nv_float16
25+
const vllm::ScalarTypeId w_type_id, // weight ScalarType id
26+
const int threads, // number of threads in a threadblock
27+
const int thread_m_blocks, // number of 16x16 blocks in the m
28+
// dimension (batchsize) of the
29+
// threadblock
30+
const int thread_n_blocks, // same for n dimension (output)
31+
const int thread_k_blocks, // same for k dimension (reduction)
32+
const bool m_block_size_8, // whether m_block_size == 8
33+
// only works when thread_m_blocks == 1
34+
const int stages, // number of stages for the async global->shared
35+
// fetch pipeline
36+
const bool has_act_order, // whether act_order is enabled
37+
const bool has_zp, // whether zero-points are enabled
38+
const int group_blocks, // number of consecutive 16x16 blocks
39+
// with a separate quantization scale
40+
const bool is_zp_float // is zero point of float16 type?
41+
>
42+
__global__ void Marlin(MARLIN_KERNEL_PARAMS);
43+
44+
}

0 commit comments

Comments
 (0)