CANN/catlass Tiling自动寻优工具
msTuner_CATLASS (MindStudio Tuner for CATLASS) - Tiling自动寻优工具【免费下载链接】catlass本项目是CANN的算子模板库提供NPU上高性能矩阵乘及其相关融合类算子模板样例。项目地址: https://gitcode.com/cann/catlassmsTuner_CATLASS 是一款用于 CATLASS 模板库算子 Tiling 参数寻优的工具支持用户自定义搜索空间能够实例化搜索空间内的所有算子并批量完成在板性能测试为算子 Tiling 参数的寻优提供参考依据。快速上手以m256n512k1024 的00_basic_matmul的tiling参数寻优为例使用默认的搜索空间配置执行以下命令完成工具的编译。bash scripts/build.sh -DCATLASS_LIBRARY_KERNELS00_basic_matmul mstuner_catlass输入mstuner_catlass命令启动性能测试。export LD_LIBRARY_PATH$PWD/output/lib64/:$LD_LIBRARY_PATH ./output/bin/mstuner_catlass --m256 --n512 --k1024 --device0 --outputresults.csv运行成功后将会出现以下回显信息实际运行结果因硬件差异与硬件性能波动不一定完全相同。$ ./output/bin/mstuner_catlass --m256 --n512 --k1024 --device0 --outputresults.csv [INFO ] Set profile output file /path_to_my_repo/catlass/output/results.csv [INFO ] Start to initialize device 0 [INFO ] Initializing device 0 success [INFO ] Initializing 1701 operations [WARN ] Current freq 800 is lower than rated freq 1800, run warm up [INFO ] Warm up finished, rated freq 1800, current freq 1800 case_id : 1 task_duration(us) : 19.380 device_id : 0 operation : Gemm description : catlass_gemm_00_basic_matmul_fp16xRowMajor_fp16xRowMajor_fp16xRowMajor_32x128x128_32x128x32_swizzle3x0 l0_tile_shape : 32x128x32 l1_tile_shape : 32x128x128 swizzle : swizzle3x0 m : 256 n : 512 k : 1024 A : fp16:row B : fp16:row C : fp16:row ... Top 10: case_id,task_duration(us),device_id,operation,description,m,n,k,A,B,C 489,12.740,7,Gemm,catlass_gemm_00_basic_matmul_fp16xRowMajor_fp16xRowMajor_fp16xRowMajor_64x128x128_64x128x64_swizzle3x1,256,512,1024,fp16:row,fp16:row,fp16:row ... [INFO ] Save profile data to /path_to_my_repo/catlass/output/results.csv success编译支持通过-DCATLASS_LIBRARY_KERNELSkernel_name命令过滤算子当算子的description信息包含kernel_name时该算子用例代码会被生成并编译比如通过如下命令指定编译00_basic_matmul类算子。bash scripts/build.sh -DCATLASS_LIBRARY_KERNELS00_basic_matmul mstuner_catlass可直接指定具体的单个算子实例的description信息比如使用如下命令仅编译快速上手中所展示的case_id为1的算子。bash scripts/build.sh -DCATLASS_LIBRARY_KERNELScatlass_gemm_00_basic_matmul_fp16xRowMajor_fp16xRowMajor_fp16xRowMajor_32x128x128_32x128x32_swizzle3x1 mstuner_catlass当前已支持如下算子类型。00_basic_matmul02_grouped_matmul_slice_m06_optimized_matmul08_grouped_matmul12_quant_matmul27_matmul_gelu注意06_optimized_matmul 算子在不同的m/n/k输入时会启用不同的kernel包括06_optimized_matmul_padding_ab, 06_optimized_matmul_padding_a_only, 06_optimized_matmul_padding_b_only, 06_optimized_matmul_without_padding。可直接指定预期运行的kernel以加速寻优过程kernel匹配逻辑请参考 optimized_matmul.cpp。除直接使用上述命令外编译也可通过cmake命令完成。mkdir build cd build cmake .. -DCATLASS_LIBRARY_KERNELS00_basic_matmul make mstuner_catlass -j cmake --install . --component catlass_kernels cmake --install . --component mstuner_catlass工具运行命令mstuner_catlass工具支持以下命令。命令示例默认值描述--help, -h--help/展示工具支持的命令。--kernels--kernels00_basic_matmul/过滤寻优的算子类型其与算子的description列字符串进行子串匹配未匹配时该算子会被跳过。--output--output./profile_result.csv/指定算子性能数据落盘文件路径。--device--device00指定运行的单卡ID。--m--m256256指定输入矩阵的维度m。--n--n512512指定输入矩阵的维度n。--k--k10241024指定输入矩阵的维度k。--A--Afp16:row/通过指定矩阵A的数据类型与内存排布过滤算子。--B--Bfp16:column/通过指定矩阵B的数据类型与内存排布过滤算子。--C--Cfp16:row/通过指定矩阵C的数据类型与内存排布过滤算子。--group_count--group_count128128指定grouped_matmul类算子的group数量。当搜索空间配置并生成了多种A、B、C的数据类型与内存排布时支持通过--A/--B/--C数据类型:内存排布命令对算子进行过滤。数据类型支持u8, int8, int32, fp16, bf16, fp32。内存排布支持row, column, nZ, zN, zZ, padding_row_major, padding_column_major, nN。要求输入data:layout的格式如fp16:rowfp32:zZ。 注意不指定--output时不会落盘算子性能数据。搜索空间配置msTuner_CATLASS支持对算子tiling参数的搜索空间进行自定义配置支持自定义配置排布方式layouts、数据类型data types、L1/L0的TileShape、Swizzle策略等参数自动正交生成全量搜索空间并可自定义剪枝函数过滤搜索空间最终每种正交配置组合会实例化为一个独立算子生成的算子实例化代码位于build/tools/library/generated目录中。当搜索空间范围配置较广时可能导致上万个算子被实例化代码膨胀致使编译耗时较长。此外过多的算子可能超过硬件限制无法保证编译成功。同时算子数量较多时算子下发前注册耗时也较长。因此建议将搜索空间的规模控制在5000以内以确保工具运行顺畅获得最佳体验。算子数量可通过查看日志文件build/tools/library/catlass_library_code_generation.log如下所示00_basic_matmul的搜索空间实例了1701个算子。INFO:search_space:00_basic_matmul tile_shapes size1701 INFO:search_space:08_grouped_matmul tile_shapes size576 INFO:manifest:operations that will be generated in total: 1701 ...搜索空间配置支持入门级配置与高级配置。入门级配置msTuner_CATLASS支持对算子tiling参数搜索空间进行入门级的简化配置见tools/library/scripts/search_space_config.py文件开发者可自由调整以下参数来设置搜索范围。kernel_type算子类型data_type_a/data_type_b/data_type_cA/B/C输入矩阵的元素类型layout_a/layout_b/layout_cA/B/C输入矩阵的内存排布l1_tile_m_rangeL1 Tile Shape的m轴取值搜索范围l1_tile_n_rangeL1 Tile Shape的n轴取值搜索范围l1_tile_k_rangeL1 Tile Shape的k轴取值搜索范围block_swizzleSwizzle策略00_basic_matmul算子的一种搜索空间配置如下OperationRegistry.register_high_priority(00_basic_matmul) def register(manifest): config search_space.SearchSpaceConfiguration( kernel_type00_basic_matmul, data_type_alibrary.DataType.fp16, data_type_blibrary.DataType.fp16, data_type_clibrary.DataType.fp16, layout_alibrary.LayoutType.RowMajor, layout_blibrary.LayoutType.RowMajor, layout_clibrary.LayoutType.RowMajor, l1_tile_m_range(32, 128), # min and max of a range are set here l1_tile_n_range(128, 256), l1_tile_k_range(128, 256), block_swizzleGemm::Block::GemmIdentityBlockSwizzle3, 0, ) search_space.register_custom_kernel(config, manifest)入门级配置的算子搜索空间会覆盖高级配置中同类型算子的配置若暂不需要使能入门级配置而使用高级配置可把该行代码注释掉。# OperationRegistry.register_high_priority(00_basic_matmul)高级配置msTuner_CATLASS支持在tools/library/scripts/search_space.py文件中对算子tiling参数的搜索空间进行更为灵活的自定义配置支持自定义配置layouts、data types、L1/L0 Tile Shapes、Swizzle策略等参数的正交组合方式自定义剪枝函数过滤筛选搜索空间遍历。以00_basic_matmul算子的搜索空间为例其配置位于函数register_gemm_00_basic_matmul_operation中。layouts配置layouts [ [library.LayoutType.RowMajor, library.LayoutType.RowMajor, library.LayoutType.RowMajor], ]data types配置data_types [ [library.DataType.fp16, library.DataType.fp16, library.DataType.fp16] ]L1/L0 Tile Shapes配置与自定义剪枝函数tile_shape_constraint_for_pingpongtile_shapes list(generate_tile_shapes( tile_shape_constraint_for_pingpong, # set constraint function based on dispatch policy # below are arguments for constraint function element_sizes(2, 2, 4), # size of ElementA, ElementB, ElementAccumulator stages[2], # stages of dispatch policy for estimating boundary conditions, e.g. 2 for UB stages step16, # step size for iterating the next tile shape on each dimension of L1/L0 tile shape tile_shape_rangeTileShapeRange( l1_tile_m_range(32, 128), # range of L1TileShape::M/N/K l1_tile_n_range(128, 256), l1_tile_k_range(128, 256), l0_tile_m_range(32, 128), # range of L0TileShape::M/N/K l0_tile_n_range(128, 256), l0_tile_k_range(32, 64) ) ))Swizzle策略配置block_swizzle_descriptions [ Gemm::Block::GemmIdentityBlockSwizzle3, 0, ]类似的08_grouped_matmul算子的搜索空间配置位于函数register_gemm_08_grouped_matmul_operation中支持自定义配置。【免费下载链接】catlass本项目是CANN的算子模板库提供NPU上高性能矩阵乘及其相关融合类算子模板样例。项目地址: https://gitcode.com/cann/catlass创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考