MindStudio Kernel Launcher API接口文档
接口列表
msKL工具提供的接口可以调用msOpGen工程中的tiling函数以及用户自定义的Kernel函数,同时提供了autotune系列接口支持开发者可以高效地针对多个调优点进行代码替换、编译、运行以及性能对比。
表 1 msKL工具接口列表
接口详情
tiling_func
功能说明
调用用户的tiling函数。
Note
tiling_func不支持调用《基础数据结构和接口参考》中的GetCompileInfo接口。
函数原型
def tiling_func(op_type: str, inputs: list, outputs: list, lib_path: str,
inputs_info: list = None, outputs_info: list = None, attr=None, soc_version: str = None) -> TilingOutput
参数说明
需根据tiling函数的实现填写,例如AddCustom、 MatmulLeakyreluCustom等。msKL工具查找tiling函数的唯一依据,查找逻辑请参见lib_path参数。 |
|||
按Kernel函数入参顺序填入tensor信息,不使用某个参数的情况,对应位置请传入None占位。 数据类型为list,每个元素必须是tensor或者list[tensor],不在inputs_info中显式指定format或者ori_format时,所有tensor默认为ND格式。 |
|||
按Kernel函数入参顺序填入tensor信息,不使用某个参数的情况,对应位置请传入None占位。 数据类型:list,每个元素必须是tensor或者list[tensor],不在inputs_info中显式指定format或者ori_format时,所有tensor默认为ND格式。 |
|||
按Kernel函数入参顺序填写info信息,不使用某个参数的情况,对应位置请传入空dict或者None占位。 数据类型为list,inputs_info参数中元素的数据类型为dict或list[dict],每个dict的元素说明如下:
[{"ori_shape": [8, 2048], "shape": [8, 2048], "dtype": "float16", "ori_format": "ND", "format": "ND"}, {"ori_shape": [8, 2048], "shape": [8, 2048], "dtype": "float16", "ori_format": "ND", "format": "ND"}] |
|||
存放输出的信息,不使用某个参数的情况,对应位置请传入空dict占位。 数据类型为list,outputs_info参数中元素的数据类型为dict或list[dict],每个dict的元素说明如下:
[{"shape": [8, 2048], "dtype": "float16", "format": "ND"}, {"shape": [8, 2048], "dtype": "float16", "format": "ND"}] |
|||
|
说明:
|
|||
msOpGen工程编译生成的liboptiling.so文件的路径,可在工程目录下通过find . -name 'liboptiling.so'进行查找。msKL工具会按已部署算子、.so文件的查找顺序获取用户tiling函数。 |
|||
|
说明:
|
返回值说明
调用示例
M = 1024
N = 640
K = 256
input_a = np.random.randint(1, 10, [M, K]).astype(np.float16)
input_b = np.random.randint(1, 10, [K, N]).astype(np.float16)
input_bias = np.random.randint(1, 10, [N]).astype(np.float32)
output = np.zeros([M, N]).astype(np.float32)
# tiling data
tiling_output = mskl.tiling_func(
op_type="MatmulLeakyreluCustom",
inputs=[input_a, input_b, input_bias], outputs=[output],
lib_path="liboptiling.so", # tiling代码编译产物
)
get_kernel_from_binary
功能说明
生成一个可以调用用户Kernel函数的实例。
函数原型
def get_kernel_from_binary(kernel_binary_file: str, kernel_type: str = None, tiling_key: int = None) -> CompiledKernel
参数说明
调用用户Kernel函数时使用的tiling_key。若不配置该参数,msKL工具将会使用最近一次调用tiling_func的结果。 |
返回值说明
可运行的Kernel对象。
表 1 Kernel入参介绍
Note
Kernel对象类型为CompiledKernel,支持如下方式调用Kernel:kernel[blockdim](arg1, arg2, ..., timeout=-1, device_id=0, repeat=1),实际调用时,需保证CompiledKernel函数的入参和调用Kernel时的入参一致。
调用示例
-
示例一:
def run_kernel(input_a, input_b, input_bias, output, workspace, tiling_data): kernel_binary_file = "MatmulLeakyreluCustom.o" #不同的硬件和操作系统展示的.o文件的名称稍有不同 kernel = get_kernel_from_binary(kernel_binary_file) return kernel(input_a, input_b, input_bias, output, workspace, tiling_data) -
示例二:
def run_kernel(input_a, input_b, input_bias, output, workspace, tiling_data, tiling_key, blockdim): kernel_binary_file = "MatmulLeakyreluCustom.o" #不同的硬件和操作系统展示的.o文件的名称稍有不同 kernel = get_kernel_from_binary(kernel_binary_file, kernel_type='mix', tiling_key=tiling_key) return kernel[blockdim](input_a, input_b, input_bias, output, workspace, tiling_data, device_id=1, timeout=-1) #运行仿真时,需要手动将timeout参数设置为-1
autotune
功能说明
遍历搜索空间,尝试不同参数组合,展示每个组合的运行耗时与最优组合。
函数原型
def autotune(configs: List[Dict], warmup: int = 300, repeat: int = 1, device_ids = [0]):
参数说明
返回值说明
无。
调用示例
@mskl.autotune(configs=[
{'L1TileShape': 'MatmulShape<64, 64, 64>', 'L0TileShape': 'MatmulShape<128, 256, 64>'},
{'L1TileShape': 'MatmulShape<64, 64, 128>', 'L0TileShape': 'MatmulShape<128, 256, 64>'},
{'L1TileShape': 'MatmulShape<64, 128, 128>', 'L0TileShape': 'MatmulShape<128, 256, 64>'},
{'L1TileShape': 'MatmulShape<64, 128, 128>', 'L0TileShape': 'MatmulShape<64, 256, 64>'},
{'L1TileShape': 'MatmulShape<128, 128, 128>', 'L0TileShape': 'MatmulShape<128, 256, 64>'},
], warmup=500, repeat=10, device_ids=[0])
def basic_matmul(problem_shape, a, layout_a, b, layout_b, c, layout_c):
kernel = get_kernel()
blockdim = 20
return kernel[blockdim](problem_shape, a, layout_a, b, layout_b, c, layout_c)
code_gen
功能说明
根据输入的模板库Kernel信息,生成Kernel下发代码。
函数原型
gen_file = mskl.Launcher(config).code_gen()
参数说明
返回值说明
生成代码的文件路径。
调用示例
config = mskl.KernelInvokeConfig(kernel_file, kernel_name)
gen_file = mskl.Launcher(config).code_gen()
相关类/结构体定义
class KernelInvokeConfig:
...
A configuration descriptor for a possible kernel developed based on an Act example
...
def __init__(self, kernel_src_file : str, kernel_name : str):
pass
# 用户仅能传KernelInvokeConfig类型
class Launcher:
def __init__(self, config: KernelInvokeConfig):
...
a class that generates launch source code for a kernel
Args:
config (KernelInvokeConfig): A configuration descriptor for a kernel
...
compile
功能说明
编译Kernel下发代码,返回一个可执行的Kernel对象。
函数原型
kernel = compile(build_script, gen_file)
参数说明
返回值说明
可运行的Kernel对象,类型:CompiledKernel,支持如下方式调用kernel:kernel[blockdim](arg1, arg2, ..., timeout=-1, device_id=0, repeat=1),其中arg1、arg2、...是Kernel的入参。
调用示例
kernel = compile(build_script, gen_file)
kernel[blockdim](arg1, arg2, ..., device_id=0)
表 1 CompiledKernel可选入参介绍
autotune_v2
功能说明
遍历搜索空间,尝试不同参数组合,展示每个组合的运行耗时与最优组合。
函数原型
def autotune_v2(configs: List[Dict], warmup_times = 5)
参数说明
返回值说明
无。
调用示例
@mskl.autotune_v2(configs=[
{'L1TileShape': 'GemmShape<128, 256, 256>', 'L0TileShape': 'GemmShape<128, 256, 64>'},
{'L1TileShape': 'GemmShape<256, 128, 256>', 'L0TileShape': 'GemmShape<256, 128, 64>'},
{'L1TileShape': 'GemmShape<128, 128, 256>', 'L0TileShape': 'GemmShape<128, 128, 64>'},
{'L1TileShape': 'GemmShape<128, 128, 512>', 'L0TileShape': 'GemmShape<128, 128, 64>'},
{'L1TileShape': 'GemmShape<64, 256, 128>', 'L0TileShape': 'GemmShape<64, 256, 64>'},
], warmup_times=10)
def run_executable(m, n, k, device_id):
src_file = "./basic_matmul.cpp"
build_script = "./jit_build_executable.sh" # executable compile script
executable = mskl.compile_executable(build_script=build_script, src_file=src_file, use_cache=False)
return executable(m, n, k, device_id)
compile_executable
功能说明
编译代码,返回一个可执行的executable对象。
函数原型
executable = compile_executable(build_script, src_file)
参数说明
返回值说明
可执行程序对象executable,类型:CompiledExecutable,支持如下方式调用:executable(arg1, arg2, ...),其中arg1、arg2、...是程序自定义入参。
调用示例
executable = compile_executable(build_script, src_file)
executable(a, b, c)