Tiling is an independent and essential part of operator development, which describes the input/output data tiling of operators, block computing, and AI Core parallelism on the NPU IP accelerator of the Kirin AI SoC. It meets on-chip storage restrictions and computing pipeline requirements and maximizes computing parallelism and data locality (or data reuse), achieving ultimate hardware performance.
For single-operator, tiling computation is performed based on the tiling .so file to generate a tiling result file.
Currently, tiling debugging is required only for standard custom operator projects, ops_adv operator projects, built-in projects, and inner-source framework projects.
Currently, tiling debugging is implemented through the mskl.tiling_func API of the mskl debugging tool. You can call the tiling function in a lightweight manner in the Python script without depending on the ACLNN framework.
For details about how to use the mskl debugging tool, see Simulator Simulation Function – Usage – mskl Debugging Tool.
Currently, Kirin chips support only the simulator mode.
Using the mskl.tiling_func API, you can specify the tiling dynamic library (.so file) and operator type (op_type) to accurately call the target tiling function. In addition, by passing parameters such as inputs and attr, developers can flexibly construct TilingContext to implement lightweight tiling calling without relying on the ACLNN framework.
The execution result of the tiling function includes:
The preceding result can be used for tiling logic verification and as necessary input for subsequent kernel calls.
The following steps assume that you have installed the CANN environment and prepared and deployed the operator project. If not, see "Prerequisites" and "Procedure" in Simulator Simulation Function – Usage – mskl Debugging Tool.
Go to the operator project directory.
cd ~/ot_demo/workspace/src/add_custom
Create the run_mskl.sh startup script. The content is as follows:
soc_version=KirinX90
export LD_PRELOAD=${ASCEND_HOME_PATH}/tools/msopt/bin/lib64/libstdc++.so.6
export LD_LIBRARY_PATH=${ASCEND_TOOLKIT_HOME}/lib64:${ASCEND_TOOLKIT_HOME}/tools/msopt/bin/lib64/:${ASCEND_TOOLKIT_HOME}/x86_64-linux/simulator/${soc_version}/lib/:${LD_LIBRARY_PATH}
python3 mskl_tiling_demo.py This script sets LD_LIBRARY_PATH and adds the paths of the CANN dynamic library, msopt dynamic library, and simulator dynamic library to the search path to ensure that mskl can correctly load the dependencies of the simulator and operator.
Create the Python calling script mskl_tiling_demo.py. The content is as follows:
import os
import numpy as np
import mskl
CANN_ROOT = os.environ.get('ASCEND_HOME_PATH', '/usr/local/Ascend/cann')
TILING_LIB_PATH = os.path.join(CANN_ROOT, 'opp/vendors/customize/op_impl/ai_core/tbe/op_tiling/liboptiling.so')
# Construct input data.
a = np.random.randint(1, 5, (8, 4096)).astype(np.float16)
b = np.random.randint(1, 5, (8, 4096)).astype(np.float16)
c = np.zeros((8, 4096), dtype=np.float16)
# Call the tiling function.
out = mskl.tiling_func(
op_type='AddCustom',
inputs=[a, b],
outputs=[c],
lib_path=TILING_LIB_PATH,
attr={'bias': 0},
soc_version='KirinX90'
)
# View the tiling result.
print(f'blockdim: {out.blockdim}')
print(f'workspace size: {len(out.workspace)}')
print(f'tiling_data size: {len(out.tiling_data)}') Modify the tiling dynamic library path in mskl_tiling_demo.py based on the actual deployment path. Run the following command to query the absolute path of liboptiling.so and enter the path in TILING_LIB_PATH:
find $ASCEND_HOME_PATH -path */customize/* -name liboptiling.so
Run the script.
Call the operator after it is successfully deployed to CANN. Otherwise, an error is reported.
sh run_mskl.sh
After the execution is successful, the terminal outputs the key information of the tiling result.
$ sh run_mskl.sh ... blockdim: 1 workspace size: 0 tiling_data size: 64
You can use this information to verify whether the tiling logic is correct, for example, whether blockdim meets the expectation and whether the workspace size is proper.
The return value of mskl.tiling_func is an object that contains the following fields:
| Field | Type | Description |
|---|---|---|
| blockdim | int | Number of started kernel functions, that is, the number of kernels used after data tiling. |
| workspace | bytes | Workspace memory, which is used when the kernel is running. |
| tiling_data | bytes | Serialized tiling_data structure data, including policy information such as data tiling and block-based computation, which is the necessary input for subsequent kernel calls. |
The returned result can also be used as the necessary input for subsequent kernel calls. For details about the complete operator simulation call process, see Simulator Simulation Function – Usage – mskl Debugging Tool.