Example
This section uses the matmulleakyrelu operator project as an example to describe how to use the mskpp.tiling_func and mskpp.get_kernel_from_binary APIs provided by msKPP to call the tiling function in the msOpGen project and the user-defined kernel function. The procedure of other operator operations is similar.
Setting Up the Environment
- Configure environment variables by referring to Environment Setup.
- Click here to obtain the sample project for operator check.
- ${git_clone_path} is the installation path of the sample repository.
- This sample project uses
Atlas A2 training products /Atlas A2 inference products as an example. - When downloading the code sample, run the following command to specify the branch version:
git clone https://gitee.com/ascend/samples.git -b master
Procedure
- Run the install.sh script in the ${git_clone_path}/operator/ascendc/0_introduction/12_matmulleakyrelu_frameworklaunch directory to generate a custom operator project and implement operators on the host and kernel. For details, see the sample project in Setting Up the Environment.
bash install.sh -v Ascendxxxyy # xxxyy indicates the type of the chip used by the user.
- Switch to the custom operator project directory.
cd CustomOp
- Edit the operator startup script matmulleakyrelu.py.
import numpy as np import mskpp # The input parameters of this function must be the same as those of the kernel function. def run_kernel(input_a, input_b, input_bias, output, workspace, tiling_data): kernel_binary_file = "MatmulLeakyreluCustom.o" # The .o file name varies depending on the hardware and operating system. For details about the path, see the parameter -reloc in Table 1. kernel = mskpp.get_kernel_from_binary(kernel_binary_file, 'mix') return kernel(input_a, input_b, input_bias, output, workspace, tiling_data) if __name__ == "__main__": # input/output tensor 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) # shape info inputs_info = [{"shape": [M, K], "dtype": "float16", "format": "ND"}, {"shape": [K, N], "dtype": "float16", "format": "ND"}, {"shape": [N], "dtype": "float32", "format": "ND"}] outputs_info = [{"shape": [M, N], "dtype": "float32", "format": "ND"}] attr = {} # Calling the tiling function tiling_output = mskpp.tiling_func( op_type="MatmulLeakyreluCustom", inputs_info=inputs_info, outputs_info=outputs_info, # Optional inputs=[input_a, input_b, input_bias], outputs=[output], attr=attr # Optional lib_path="liboptiling.so", # Compilation product of the tiling code. For details about the directory structure, see Step 2 in DPP Deployment. # soc_version="", # Optional ) blockdim = tiling_output.blockdim workspace_size = tiling_output.workspace_size tiling_data = tiling_output.tiling_data # numpy array. workspace = np.zeros(workspace_size).astype(np.uint8) # The workspace needs to be applied for by the user. # Calling the kernel function run_kernel(input_a, input_b, input_bias, output, workspace, tiling_data) # Verification output alpha = 0.001 golden = (np.matmul(input_a.astype(np.float32), input_b.astype(np.float32)) + input_bias).astype(np.float32) golden = np.where(golden >= 0, golden, golden * alpha) is_equal = np.array_equal(output, golden) result = "success" if is_equal else "failed" print("compare {}.".format(result)) - Run the script.
python3 matmulleakyrelu.py
Parent topic: Calling the msOpGen Operator Project