|
| 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() |
0 commit comments