已合并
refactor: 重构kernel编译配置体系,将配置从JSON/INI迁移至CMake声明式函数 #3010
songkai111创建于 5月28日
refactor: 重构kernel编译配置体系,将配置从JSON/INI迁移至CMake声明式函数 #3010
已合并
共 11 个文件变更+341-77
| @@ -746,6 +746,10 @@ macro(add_all_ut_sources) | |||
| 746 | add_aicpu_op_test_case(${MODULE_OP_NAME}) | 746 | add_aicpu_op_test_case(${MODULE_OP_NAME}) |
| 747 | endif() | 747 | endif() |
| 748 | 748 | ||
| 749 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/op_kernel/CMakeLists.txt") | ||
| 750 | + add_subdirectory(${SOURCE_DIR}/op_kernel) | ||
| 751 | + endif() | ||
| 752 | + | ||
| 749 | if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/ut/op_kernel/CMakeLists.txt") | 753 | if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/ut/op_kernel/CMakeLists.txt") |
| 750 | add_subdirectory(${SOURCE_DIR}/tests/ut/op_kernel) | 754 | add_subdirectory(${SOURCE_DIR}/tests/ut/op_kernel) |
| 751 | endif() | 755 | endif() |
| @@ -755,6 +759,92 @@ macro(add_all_ut_sources) | |||
| 755 | endif() | 759 | endif() |
| 756 | endmacro() | 760 | endmacro() |
| 757 | 761 | ||
| 762 | +# ###################################################################################################################### | ||
| 763 | +# get op_type from *_def.cpp | ||
| 764 | +# ###################################################################################################################### | ||
| 765 | +function(get_op_type_from_op_name OP_NAME OP_TYPE) | ||
| 766 | + execute_process( | ||
| 767 | + COMMAND | ||
| 768 | + find ${OP_DIR} -name ${OP_NAME}_def.cpp -exec grep OP_ADD {} \; | ||
| 769 | + OUTPUT_VARIABLE op_type | ||
| 770 | + ) | ||
| 771 | + if(NOT op_type) | ||
| 772 | + set(op_type "") | ||
| 773 | + else() | ||
| 774 | + string(REGEX REPLACE "[\t ]*OP_ADD\\([\t ]*" "" op_type ${op_type}) | ||
| 775 | + string(REGEX REPLACE "[\t ]*\\).*$" "" op_type ${op_type}) | ||
| 776 | + endif() | ||
| 777 | + set(${OP_TYPE} | ||
| 778 | + ${op_type} | ||
| 779 | + PARENT_SCOPE | ||
| 780 | + ) | ||
| 781 | +endfunction() | ||
| 782 | + | ||
| 783 | +# usage: add_kernel_sources(OPTYPE) | ||
| 784 | +# 1.调用asc_opc 工具 编译二进制kernel时, --simplified_key_mode/--impl_mode 选项中填写的值。 | ||
| 785 | +# 2.调用asc_opc工具 算子名.py 中auto_sync与compile_option中的配置项。 | ||
| 786 | +# 格式如下所示: | ||
| 787 | +# [COMPUTE_UNITS ascendxx] soc版本,支持配置多个unit,缺省为配置所有的soc(支持单独配置soc,单独配置优先级更高) | ||
| 788 | +# [SIMPLIFIED_KEY 0/None] 缺省为0,最终的编译参数则是--simplified_key_mode=0,若设置为None,则不会携带该参数 | ||
| 789 | +# [AUTO_SYNC false] 同步选项,缺省为true | ||
| 790 | +# [IMPL_MODE high_performance] 高性能模式,缺省为high_performance[,optional] [optional]是可选的 | ||
| 791 | +# [OPTIONS "option1" "option2"] 其他编译选项 | ||
| 792 | +function(add_kernel_sources) | ||
| 793 | + set(oneValueArgs SIMPLIFIED_KEY AUTO_SYNC IMPL_MODE) | ||
| 794 | + set(multiValueArgs COMPUTE_UNITS OPTIONS) | ||
| 795 | + cmake_parse_arguments(MODULE "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
| 796 | + | ||
| 797 | + set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) | ||
| 798 | + get_filename_component(PARENT_DIR ${SOURCE_DIR} DIRECTORY) | ||
| 799 | + get_filename_component(OP_NAME ${PARENT_DIR} NAME) | ||
| 800 | + set(OP_DIR ${PARENT_DIR}) | ||
| 801 | + set(op_type "") | ||
| 802 | + get_op_type_from_op_name("${OP_NAME}" op_type) | ||
| 803 | + | ||
| 804 | + set(simplified_key ${MODULE_SIMPLIFIED_KEY}) | ||
| 805 | + set(auto_sync ${MODULE_AUTO_SYNC}) | ||
| 806 | + set(impl_mode ${MODULE_IMPL_MODE}) | ||
| 807 | + set(options ${MODULE_OPTIONS}) | ||
| 808 | + | ||
| 809 | + if(NOT simplified_key) | ||
| 810 | + set(simplified_key "0") | ||
| 811 | + endif() | ||
| 812 | + | ||
| 813 | + if(NOT impl_mode) | ||
| 814 | + set(impl_mode "high_performance,optional") | ||
| 815 | + endif() | ||
| 816 | + | ||
| 817 | + set(auto_sync_option "") | ||
| 818 | + if(auto_sync STREQUAL "false") | ||
| 819 | + set(auto_sync_option ${auto_sync}) | ||
| 820 | + endif() | ||
| 821 | + | ||
| 822 | + string(JOIN "," unit_options ${options}) | ||
| 823 | + | ||
| 824 | + if(NOT MODULE_COMPUTE_UNITS OR MODULE_COMPUTE_UNITS STREQUAL "") | ||
| 825 | + set(target_compute_units "ALL") | ||
| 826 | + else() | ||
| 827 | + set(target_compute_units "${ASCEND_COMPUTE_UNIT}") | ||
| 828 | + endif() | ||
| 829 | + | ||
| 830 | + if(NOT MODULE_COMPUTE_UNITS OR "${target_compute_units}" IN_LIST MODULE_COMPUTE_UNITS) | ||
| 831 | + set(current_simplified_key_list "${op_type} ${target_compute_units} ${simplified_key}") | ||
| 832 | + set(current_impl_mode_list "${op_type} ${target_compute_units} ${impl_mode}") | ||
| 833 | + set(current_auto_sync_list "${op_type} ${target_compute_units} ${auto_sync}") | ||
| 834 | + set(current_option_list "${op_type} ${target_compute_units} ${unit_options}") | ||
| 835 | + | ||
| 836 | + list(APPEND simplified_key_list ${current_simplified_key_list}) | ||
| 837 | + list(APPEND impl_mode_list ${current_impl_mode_list}) | ||
| 838 | + list(APPEND auto_sync_list ${current_auto_sync_list}) | ||
| 839 | + list(APPEND option_list ${current_option_list}) | ||
| 840 | + | ||
| 841 | + set(simplified_key_list ${simplified_key_list} CACHE INTERNAL "simplified_key") | ||
| 842 | + set(impl_mode_list ${impl_mode_list} CACHE INTERNAL "impl_mode") | ||
| 843 | + set(auto_sync_list ${auto_sync_list} CACHE INTERNAL "auto_sync") | ||
| 844 | + set(option_list ${option_list} CACHE INTERNAL "options") | ||
| 845 | + endif() | ||
| 846 | +endfunction() | ||
| 847 | + | ||
| 758 | # usage: add_graph_plugin_sources() | 848 | # usage: add_graph_plugin_sources() |
| 759 | macro(add_graph_plugin_sources) | 849 | macro(add_graph_plugin_sources) |
| 760 | set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) | 850 | set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| @@ -87,7 +87,7 @@ function(get_op_type_and_validate OP_DIR compute_unit op_name_var op_type_var is | |||
| 87 | return() | 87 | return() |
| 88 | endif() | 88 | endif() |
| 89 | else() | 89 | else() |
| 90 | - get_op_type_from_op_name("${op_name}" "${OP_DIR}" op_type) | 90 | + get_op_type_from_op_name("${op_name}" op_type) |
| 91 | if(NOT op_type) | 91 | if(NOT op_type) |
| 92 | message(STATUS "[INFO] On [${compute_unit}], [${op_name}] not need to compile.") | 92 | message(STATUS "[INFO] On [${compute_unit}], [${op_name}] not need to compile.") |
| 93 | set(${cache_key} "" CACHE INTERNAL "") | 93 | set(${cache_key} "" CACHE INTERNAL "") |
| @@ -257,28 +257,6 @@ function(generate_bin_scripts) | |||
| 257 | add_dependencies(${GENBIN_TARGET} generate_bin_scripts_${GENBIN_COMPUTE_UNIT}_${GENBIN_OP_NAME}) | 257 | add_dependencies(${GENBIN_TARGET} generate_bin_scripts_${GENBIN_COMPUTE_UNIT}_${GENBIN_OP_NAME}) |
| 258 | endfunction() | 258 | endfunction() |
| 259 | 259 | ||
| 260 | - | ||
| 261 | -# ###################################################################################################################### | ||
| 262 | -# get op_type from *_def.cpp | ||
| 263 | -# ###################################################################################################################### | ||
| 264 | -function(get_op_type_from_op_name OP_NAME OP_DIR OP_TYPE) | ||
| 265 | - execute_process( | ||
| 266 | - COMMAND | ||
| 267 | - find ${OP_DIR} -name ${OP_NAME}_def.cpp -exec grep OP_ADD {} \; | ||
| 268 | - OUTPUT_VARIABLE op_type | ||
| 269 | - ) | ||
| 270 | - if(NOT op_type) | ||
| 271 | - set(op_type "") | ||
| 272 | - else() | ||
| 273 | - string(REGEX REPLACE "[\t ]*OP_ADD\\([\t ]*" "" op_type ${op_type}) | ||
| 274 | - string(REGEX REPLACE "[\t ]*\\).*$" "" op_type ${op_type}) | ||
| 275 | - endif() | ||
| 276 | - set(${OP_TYPE} | ||
| 277 | - ${op_type} | ||
| 278 | - PARENT_SCOPE | ||
| 279 | - ) | ||
| 280 | -endfunction() | ||
| 281 | - | ||
| 282 | # ###################################################################################################################### | 260 | # ###################################################################################################################### |
| 283 | # get op_type from *_def.cpp | 261 | # get op_type from *_def.cpp |
| 284 | # ###################################################################################################################### | 262 | # ###################################################################################################################### |
| @@ -517,7 +495,17 @@ function(gen_ops_info_and_python) | |||
| 517 | ${ASCEND_KERNEL_SRC_DST} | 495 | ${ASCEND_KERNEL_SRC_DST} |
| 518 | ) | 496 | ) |
| 519 | 497 | ||
| 520 | - set(ascendc_impl_gen_depends ascendc_kernel_src_copy opbuild_custom_gen_aclnn_all) | 498 | + string(JOIN "/" simplified_key_str ${simplified_key_list}) |
| 499 | + string(JOIN "/" impl_mode_str ${impl_mode_list}) | ||
| 500 | + string(JOIN "/" auto_sync_str ${auto_sync_list}) | ||
| 501 | + string(JOIN "/" options_str ${option_list}) | ||
| 502 | + | ||
| 503 | + add_custom_target(gen_kernel_options | ||
| 504 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${OPS_KERNEL_BINARY_SCRIPT}/gen_ops_compile_ini.py ${CMAKE_BINARY_DIR}/tbe/config | ||
| 505 | + ${simplified_key_str} ${impl_mode_str} ${auto_sync_str} ${options_str} | ||
| 506 | + ) | ||
| 507 | + | ||
| 508 | + set(ascendc_impl_gen_depends ascendc_kernel_src_copy opbuild_custom_gen_aclnn_all gen_kernel_options) | ||
| 521 | foreach(compute_unit ${ASCEND_ALL_COMPUTE_UNIT}) | 509 | foreach(compute_unit ${ASCEND_ALL_COMPUTE_UNIT}) |
| 522 | # generate aic-${compute_unit}-ops-info.json, operator infos | 510 | # generate aic-${compute_unit}-ops-info.json, operator infos |
| 523 | if(ENABLE_CUSTOM) | 511 | if(ENABLE_CUSTOM) |
| @@ -558,7 +546,7 @@ function(gen_ops_info_and_python) | |||
| 558 | get_filename_component(op_name ${OP_DIR} NAME) | 546 | get_filename_component(op_name ${OP_DIR} NAME) |
| 559 | 547 | ||
| 560 | set(op_type) | 548 | set(op_type) |
| 561 | - get_op_type_from_op_name("${op_name}" "${OP_DIR}" op_type) | 549 | + get_op_type_from_op_name("${op_name}" op_type) |
| 562 | if(NOT op_type) | 550 | if(NOT op_type) |
| 563 | message(STATUS "[INFO] op type undefined, skip binary compile. op name=${op_name}") | 551 | message(STATUS "[INFO] op type undefined, skip binary compile. op name=${op_name}") |
| 564 | continue() | 552 | continue() |
| @@ -31,6 +31,12 @@ set(NEED_COMPILE_OPS "${ASCEND_OP_NAME}" CACHE STRING "Need to be compiled Ops" | |||
| 31 | set(COMPILED_OPS CACHE STRING "Compiled Ops" FORCE) | 31 | set(COMPILED_OPS CACHE STRING "Compiled Ops" FORCE) |
| 32 | set(COMPILED_OP_DIRS CACHE STRING "Compiled Ops Dirs" FORCE) | 32 | set(COMPILED_OP_DIRS CACHE STRING "Compiled Ops Dirs" FORCE) |
| 33 | 33 | ||
| 34 | +# kernel compile options | ||
| 35 | +set(simplified_key_list CACHE STRING "kernel compile simplified key list" FORCE) | ||
R | |||
| 36 | +set(impl_mode_list CACHE STRING "kernel compile impl mode list" FORCE) | ||
| 37 | +set(auto_sync_list CACHE STRING "kernel compile auto sync list" FORCE) | ||
| 38 | +set(option_list CACHE STRING "kernel compile options list" FORCE) | ||
| 39 | + | ||
| 34 | # src path | 40 | # src path |
| 35 | get_filename_component(OPS_MATH_CMAKE_DIR "${OPS_MATH_DIR}/cmake" REALPATH) | 41 | get_filename_component(OPS_MATH_CMAKE_DIR "${OPS_MATH_DIR}/cmake" REALPATH) |
| 36 | get_filename_component(OPS_MATH_COMMON_INC "${OPS_MATH_DIR}/common/inc" REALPATH) | 42 | get_filename_component(OPS_MATH_COMMON_INC "${OPS_MATH_DIR}/common/inc" REALPATH) |
Dmath/add/op_host/config/ascend950/add_simplified_key.ini+0-13
| @@ -1,13 +0,0 @@ | |||
| 1 | -; 该文件主要影响 opc 工具 编译二进制kernel时, --simplified_key_mode 选项中填写的值,格式如下所示: | ||
| 2 | -; [某算子] | ||
| 3 | -; default=xx | ||
| 4 | -; ascendxx=xx | ||
| 5 | -; 其中,default为默认mode,ascnedxx为可选mode,如果不同芯片有差异化要求时,需要配置; | ||
| 6 | -; 1)如果没有配置:非ascendC算子继续按空处理,即opc编译命令中不添加 --simplified_key_mode 选项,AscendC算子按照 simplified_key_mode=0 处理 | ||
| 7 | -; 2)如果仅有default配置:各个版本按default配置 | ||
| 8 | -; 3)如果仅有某些平台的配置,没有default配置:对应平台的按照配置的值传递,非对应平台的:非AscendC算子继续按空处理,AscendC算子按照 simplified_key_mode=0 处理 | ||
| 9 | -; 4)如果default配置和平台配置都有:对应平台的使用平台的配置,非对应的平台的以default值配置。 | ||
| 10 | -; 5)对于自定义simplified key的情况,需要在binary_simplified_key_mode.ini 文件中显式配置为None,不传入 --simplified_key_mode 选项,由opc工具和FE框架自行判断使用何种模式 | ||
| 11 | -; 6)是否是AscendC算子,由 ops/build-in/tbe/op_info_cfg/parser/ascendc_config.json 中配置的算子名字和对于的平台决定 | ||
| 12 | -[Add] | ||
| 13 | -default=0 | ||
| @@ -0,0 +1,15 @@ | |||
| 1 | +# ---------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +add_kernel_sources( | ||
| 12 | + COMPUTE_UNITS ascend950 | ||
| 13 | + AUTO_SYNC false | ||
| 14 | + OPTIONS "--cce-simd-vf-fusion=true" "-mllvm -cce-aicore-dcci-before-kernel-end=false" | ||
| 15 | +) | ||
| @@ -139,7 +139,6 @@ | |||
| 139 | {"name":"ForeachAtan", "compute_units": ["ascend910b", "ascend910_93"], "auto_sync" : false}, | 139 | {"name":"ForeachAtan", "compute_units": ["ascend910b", "ascend910_93"], "auto_sync" : false}, |
| 140 | {"name":"AdvanceStep", "compute_units": ["ascend910_93", "ascend910b"], "auto_sync" : false}, | 140 | {"name":"AdvanceStep", "compute_units": ["ascend910_93", "ascend910b"], "auto_sync" : false}, |
| 141 | {"name":"ApplyRotaryPosEmb", "compute_units": ["ascend910", "ascend910b", "ascend910_93", "ascend310p", "ascend950"], "auto_sync" : true}, | 141 | {"name":"ApplyRotaryPosEmb", "compute_units": ["ascend910", "ascend910b", "ascend910_93", "ascend310p", "ascend950"], "auto_sync" : true}, |
| 142 | - {"name":"Add", "compute_units": ["ascend950"], "auto_sync": false, "compile_options": {"ascend950": ["--cce-simd-vf-fusion=true", "-mllvm -cce-aicore-dcci-before-kernel-end=false"]}, "impl_mode": ""}, | ||
| 143 | {"name":"Sub", "compute_units": ["ascend950"], "auto_sync": false, "compile_options": {"ascend950": ["--cce-simd-vf-fusion=true", "-mllvm -cce-aicore-dcci-before-kernel-end=false"]}, "impl_mode": ""}, | 142 | {"name":"Sub", "compute_units": ["ascend950"], "auto_sync": false, "compile_options": {"ascend950": ["--cce-simd-vf-fusion=true", "-mllvm -cce-aicore-dcci-before-kernel-end=false"]}, "impl_mode": ""}, |
| 144 | {"name":"Mul", "compute_units": ["ascend950"], "auto_sync" : false, "impl_mode" : "", "compile_options": {"ascend950": ["--cce-simd-vf-fusion=true", "-mllvm -cce-aicore-dcci-before-kernel-end=false"]}}, | 143 | {"name":"Mul", "compute_units": ["ascend950"], "auto_sync" : false, "impl_mode" : "", "compile_options": {"ascend950": ["--cce-simd-vf-fusion=true", "-mllvm -cce-aicore-dcci-before-kernel-end=false"]}}, |
| 145 | {"name":"Muls", "compute_units": ["ascend950"], "auto_sync" : false, "impl_mode" : "", "compile_options": {"ascend950": ["-mllvm -cce-aicore-dcci-before-kernel-end=false"]}}, | 144 | {"name":"Muls", "compute_units": ["ascend950"], "auto_sync" : false, "impl_mode" : "", "compile_options": {"ascend950": ["-mllvm -cce-aicore-dcci-before-kernel-end=false"]}}, |
| @@ -53,6 +53,23 @@ function get_binary_config_file() { | |||
| 53 | return 0 | 53 | return 0 |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | +function get_kernel_option_config_file() { | ||
| 57 | + if [ $# -ne 2 ]; then | ||
| 58 | + echo "error invalid param number:$#, must be 2" >&2 | ||
| 59 | + return 1 | ||
| 60 | + fi | ||
| 61 | + local workdir="$1" | ||
| 62 | + local soc_version_lower="$2" | ||
| 63 | + local topdir=$(readlink -f ${workdir}/../../..) | ||
| 64 | + local binary_config_dir=${topdir}/build/tbe/config | ||
| 65 | + | ||
| 66 | + local primary_pattern="${binary_config_dir}/kernel-options-${soc_version_lower}.ini" | ||
| 67 | + if [ -f "${primary_pattern}" ]; then | ||
| 68 | + echo "${primary_pattern}" | ||
| 69 | + return 0 | ||
| 70 | + fi | ||
| 71 | +} | ||
| 72 | + | ||
| 56 | function get_simplified_key_config_file() { | 73 | function get_simplified_key_config_file() { |
| 57 | if [ $# -ne 4 ]; then | 74 | if [ $# -ne 4 ]; then |
| 58 | echo "error invalid param number:$#, must be 4" >&2 | 75 | echo "error invalid param number:$#, must be 4" >&2 |
| @@ -187,26 +204,55 @@ main() { | |||
| 187 | fi | 204 | fi |
| 188 | 205 | ||
| 189 | # step 4: get simplified_key_mode from binary_simplified_key_mode.ini | 206 | # step 4: get simplified_key_mode from binary_simplified_key_mode.ini |
| 190 | - local simplified_key_file=$(get_simplified_key_config_file ${workdir} ${op_type} ${op_file_name_prefix} ${soc_version_lower}) | 207 | + local kernel_config_file=$(get_kernel_option_config_file ${workdir} ${soc_version_lower}) |
| 191 | - local key_mode_default=0 | 208 | + local kernel_config_file_all=$(get_kernel_option_config_file ${workdir} "ALL") |
| 192 | - if [ -z ${simplified_key_file} ]; then | 209 | + |
| 193 | - key_mode_default="" | 210 | + local impl_list="" |
| 194 | - else | 211 | + local key_mode="" |
| 195 | - if [ -f ${simplified_key_file} ]; then | 212 | + local is_optional=false |
| 196 | - dos2unix $simplified_key_file >/dev/null 2>&1 | 213 | + |
| 197 | - # if no file binary_simplified_key_mode.ini use mode=0 | 214 | + if [ -f "${kernel_config_file}" ]; then |
| 215 | + impl_list=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /impl_mode/{print $2; exit}' "$kernel_config_file") | ||
| 216 | + key_mode=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /simplified_key/{print $2; exit}' "$kernel_config_file") | ||
| 217 | + fi | ||
| 218 | + if [ -z "${impl_list}" ] && [ -z "${key_mode}" ]; then | ||
| 219 | + if [ -f "${kernel_config_file_all}" ]; then | ||
| 220 | + impl_list=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /impl_mode/{print $2; exit}' "$kernel_config_file_all") | ||
| 221 | + key_mode=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /simplified_key/{print $2; exit}' "$kernel_config_file_all") | ||
| 222 | + fi | ||
| 223 | + fi | ||
| 224 | + if [ -n "${impl_list}" ]; then | ||
| 225 | + if [[ "${impl_list}" == *",optional"* ]]; then | ||
| 226 | + is_optional=true | ||
| 227 | + impl_list=$(echo "${impl_list}" | sed 's/,optional$//') | ||
| 228 | + fi | ||
| 229 | + fi | ||
| 230 | + | ||
| 231 | + if [ -z "$key_mode" ]; then | ||
| 232 | + local simplified_key_file=$(get_simplified_key_config_file ${workdir} ${op_type} ${op_file_name_prefix} ${soc_version_lower}) | ||
| 233 | + local key_mode_default=0 | ||
| 234 | + if [ -z "${simplified_key_file}" ] || [ ! -f "${simplified_key_file}" ]; then | ||
| 235 | + echo "[INFO] No simplified_key_file found. Using default key_mode_default=0" | ||
| 236 | + else | ||
| 237 | + if file "$simplified_key_file" | grep -q "CRLF"; then | ||
| 238 | + if ! command -v dos2unix &> /dev/null; then | ||
| 239 | + echo "[ERROR] dos2unix is not installed. Cannot convert simplified_key_file to unix line endings !" | ||
| 240 | + exit 5 | ||
| 241 | + fi | ||
| 242 | + dos2unix $simplified_key_file | ||
| 243 | + fi | ||
| 198 | key_mode_default=$(awk -F "=" '/\['${op_type}'\]/{flag=1;next}/\[/{flag=0} flag && /default/{print $2}' $simplified_key_file) | 244 | key_mode_default=$(awk -F "=" '/\['${op_type}'\]/{flag=1;next}/\[/{flag=0} flag && /default/{print $2}' $simplified_key_file) |
| 199 | key_mode_default=$(echo $key_mode_default | tr -d '[:space:]') | 245 | key_mode_default=$(echo $key_mode_default | tr -d '[:space:]') |
| 200 | fi | 246 | fi |
| 201 | - fi | 247 | + local ascendc_config_file="${workdir}/../binary_config/ascendc_config.json" |
| 202 | - local ascendc_config_file="${workdir}/../binary_config/ascendc_config.json" | 248 | + local key_word_in_list="\"name\":\s*\"${op_type}\"" |
| 203 | - local key_word_in_list="\"name\":\s*\"${op_type}\"" | 249 | + local ascendc_op_conf=$(grep ${key_word_in_list} ${ascendc_config_file} | grep -w $soc_version_lower) |
| 204 | - local ascendc_op_conf=$(grep ${key_word_in_list} ${ascendc_config_file} | grep -w $soc_version_lower) | ||
| 205 | 250 | ||
| 206 | - if [ "$key_mode_default" != "" ]; then | 251 | + if [ "$key_mode_default" != "" ]; then |
| 207 | - key_mode=${key_mode_default} | 252 | + key_mode=${key_mode_default} |
| 208 | - else | 253 | + else |
| 209 | - key_mode=0 | 254 | + key_mode=0 |
| 255 | + fi | ||
| 210 | fi | 256 | fi |
| 211 | 257 | ||
| 212 | if [ "$key_mode" != "None" ]; then | 258 | if [ "$key_mode" != "None" ]; then |
| @@ -218,11 +264,13 @@ main() { | |||
| 218 | # step 5: get impl_mode from all_ops_impl_mode.ini | 264 | # step 5: get impl_mode from all_ops_impl_mode.ini |
| 219 | # ascendc_config.json 配置超过两种以上的implmode使用ascendc_config的配置,否则使用binary_implmode_default | 265 | # ascendc_config.json 配置超过两种以上的implmode使用ascendc_config的配置,否则使用binary_implmode_default |
| 220 | # 如果都没有配置,默认high_performance | 266 | # 如果都没有配置,默认high_performance |
| 221 | - local impl_mode_full_path="${workdir}/../binary_config/${IMPL_FILE_NAME}" | 267 | + if [ -z "$impl_list" ]; then |
| 222 | - local impl_list=$(awk -F '=' '/^'${op_type}'=/{print $2;exit}' ${impl_mode_full_path}) | 268 | + local impl_mode_full_path="${workdir}/../binary_config/${IMPL_FILE_NAME}" |
| 223 | - if [ "${impl_list}" = "" ]; then | 269 | + local impl_list=$(awk -F '=' '/^'${op_type}'=/{print $2;exit}' ${impl_mode_full_path}) |
| 224 | - # 默认高性能模式 | 270 | + if [ "${impl_list}" = "" ]; then |
| 225 | - impl_list="high_performance" | 271 | + # 默认高性能模式 |
| 272 | + impl_list="high_performance" | ||
| 273 | + fi | ||
| 226 | fi | 274 | fi |
| 227 | 275 | ||
| 228 | # 获取 impl_mode 默认值 | 276 | # 获取 impl_mode 默认值 |
| @@ -263,7 +311,7 @@ main() { | |||
| 263 | for ((i = 0; i < ${thread_num}; i = i + 1)); do | 311 | for ((i = 0; i < ${thread_num}; i = i + 1)); do |
| 264 | { | 312 | { |
| 265 | new_file="${binary_config_new_full_path}_${i}" | 313 | new_file="${binary_config_new_full_path}_${i}" |
| 266 | - if [ "${val}" = "${impl_mode}" ]; then | 314 | + if [[ "${val}" == "${impl_mode}" ]] || [[ "${is_optional}" == "true" ]]; then |
| 267 | impl_mode_default="${impl_mode},optional" | 315 | impl_mode_default="${impl_mode},optional" |
| 268 | cmd="asc_opc ${op_python_path} --main_func=${op_func} --input_param=${new_file} --soc_version=${opc_soc_version} --output=${binary_bin_path} --impl_mode=${impl_mode_default} ${simplified_key_param} --op_mode=dynamic" | 316 | cmd="asc_opc ${op_python_path} --main_func=${op_func} --input_param=${new_file} --soc_version=${opc_soc_version} --output=${binary_bin_path} --impl_mode=${impl_mode_default} ${simplified_key_param} --op_mode=dynamic" |
| 269 | else | 317 | else |
| @@ -234,7 +234,8 @@ def gen_ops_config(json_file, soc, config): | |||
| 234 | def del_params_info_not_same_op(config): | 234 | def del_params_info_not_same_op(config): |
| 235 | if not need_to_cut_op: | 235 | if not need_to_cut_op: |
| 236 | return | 236 | return |
| 237 | - print(f'[WARNING] op_type[{need_to_cut_op}] params info is not same, cannot be write into binary_info_config.json') | 237 | + print('[INFO] op_type[{}] params info is not same, cannot be write into \ |
| 238 | + binary_info_config.json'.format(need_to_cut_op)) | ||
| 238 | for op in need_to_cut_op: | 239 | for op in need_to_cut_op: |
| 239 | if config.get(op) is not None: | 240 | if config.get(op) is not None: |
| 240 | del config[op] | 241 | del config[op] |
| @@ -243,8 +244,8 @@ def del_params_info_not_same_op(config): | |||
| 243 | def del_simply_mode_not_same(config): | 244 | def del_simply_mode_not_same(config): |
| 244 | if not simplified_mode_not_same_op: | 245 | if not simplified_mode_not_same_op: |
| 245 | return | 246 | return |
| 246 | - print(f'[WARNING] op_type[{simplified_mode_not_same_op}] simplifiedKeyMode is not same, ' | 247 | + print(f'[INFO] op_type[{simplified_mode_not_same_op}] simplifiedKeyMode is not same, cannot be write into \ |
| 247 | - 'cannot be write into binary_info_config.json') | 248 | + binary_info_config.json') |
| 248 | for op in simplified_mode_not_same_op: | 249 | for op in simplified_mode_not_same_op: |
| 249 | if config.get(op) is not None: | 250 | if config.get(op) is not None: |
| 250 | del config[op] | 251 | del config[op] |
| @@ -253,7 +254,8 @@ def del_simply_mode_not_same(config): | |||
| 253 | def del_null_simply_key_op(config): | 254 | def del_null_simply_key_op(config): |
| 254 | if not no_simpliy_key_op: | 255 | if not no_simpliy_key_op: |
| 255 | return | 256 | return |
| 256 | - print(f'[WARNING] op_type[{no_simpliy_key_op}] simplifiedKey is none, cannot be write into binary_info_config.json') | 257 | + print('[INFO] op_type[{}] simplifiedKey is none, cannot be write into \ |
| 258 | + binary_info_config.json'.format(no_simpliy_key_op)) | ||
| 257 | for op in no_simpliy_key_op: | 259 | for op in no_simpliy_key_op: |
| 258 | if config.get(op) is not None: | 260 | if config.get(op) is not None: |
| 259 | del config[op] | 261 | del config[op] |
| @@ -333,7 +335,7 @@ def gen_binary_info_config(kernel_dir, soc): | |||
| 333 | if "fusion_ops" in _json: | 335 | if "fusion_ops" in _json: |
| 334 | continue | 336 | continue |
| 335 | gen_ops_config(_json, soc, config) | 337 | gen_ops_config(_json, soc, config) |
| 336 | - print("[gen_binary_info_config] config.keys is ", config.keys()) | 338 | + print("[gen_binary_info_config] config.keys is", config.keys()) |
| 337 | modify_binary_list_with_config(config, cfg_dir) | 339 | modify_binary_list_with_config(config, cfg_dir) |
| 338 | del_params_info_not_same_op(config) | 340 | del_params_info_not_same_op(config) |
| 339 | del_simply_mode_not_same(config) | 341 | del_simply_mode_not_same(config) |
| @@ -0,0 +1,83 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +import sys | ||
| 14 | +from pathlib import Path | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +def parse_list(raw): | ||
| 18 | + entries = [] | ||
| 19 | + for item in raw: | ||
| 20 | + item = item.strip() | ||
| 21 | + if not item: | ||
| 22 | + continue | ||
| 23 | + parts = item.split() | ||
| 24 | + if len(parts) < 2: | ||
| 25 | + continue | ||
| 26 | + op_name = parts[0] | ||
| 27 | + unit_name = parts[1] | ||
| 28 | + if len(parts) > 2 and "none" == parts[2].lower(): | ||
| 29 | + parts[2] = "None" | ||
| 30 | + rest = " ".join(parts[2:]) | ||
| 31 | + entries.append((op_name, unit_name, rest)) | ||
| 32 | + return entries | ||
| 33 | + | ||
| 34 | +config_dict = {} | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +def add_to_config(entries, key_name): | ||
| 38 | + for op_name_raw, unit_name_raw, value in entries: | ||
| 39 | + if unit_name_raw not in config_dict: | ||
| 40 | + config_dict[unit_name_raw] = {} | ||
| 41 | + if op_name_raw not in config_dict[unit_name_raw]: | ||
| 42 | + config_dict[unit_name_raw][op_name_raw] = {} | ||
| 43 | + config_dict[unit_name_raw][op_name_raw][key_name] = value | ||
| 44 | + | ||
| 45 | + | ||
| 46 | +def main(): | ||
| 47 | + if len(sys.argv) < 5: | ||
| 48 | + raise SystemExit(0) | ||
| 49 | + | ||
| 50 | + output_dir = Path(sys.argv[1]) | ||
| 51 | + simplified_key_str = sys.argv[2] | ||
| 52 | + impl_mode_str = sys.argv[3] | ||
| 53 | + auto_sync_str = sys.argv[4] | ||
| 54 | + options_str = sys.argv[5] | ||
| 55 | + | ||
| 56 | + simplified_key_list = simplified_key_str.split("/") if simplified_key_str else [] | ||
| 57 | + impl_mode_list = impl_mode_str.split("/") if impl_mode_str else [] | ||
| 58 | + auto_sync_list = auto_sync_str.split("/") if auto_sync_str else [] | ||
| 59 | + options_list = options_str.split("/") if options_str else [] | ||
| 60 | + output_dir.mkdir(exist_ok=True) | ||
| 61 | + | ||
| 62 | + simplified_keys = parse_list(simplified_key_list) | ||
| 63 | + impl_modes = parse_list(impl_mode_list) | ||
| 64 | + auto_syncs = parse_list(auto_sync_list) | ||
| 65 | + op_options = parse_list(options_list) | ||
| 66 | + | ||
| 67 | + add_to_config(simplified_keys, "simplified_key") | ||
| 68 | + add_to_config(impl_modes, "impl_mode") | ||
| 69 | + add_to_config(auto_syncs, "auto_sync") | ||
| 70 | + add_to_config(op_options, "options") | ||
| 71 | + | ||
| 72 | + for unit_name, op_configs in config_dict.items(): | ||
| 73 | + ini_path = output_dir / f"kernel-options-{unit_name}.ini" | ||
| 74 | + with open(ini_path, "w", encoding="utf-8") as f: | ||
| 75 | + for op_name, config in op_configs.items(): | ||
| 76 | + f.write(f"[{op_name}]\n") | ||
| 77 | + for key, value in config.items(): | ||
| 78 | + f.write(f"{key}={value}\n") | ||
| 79 | + f.write("\n") | ||
| 80 | + | ||
| 81 | + | ||
| 82 | +if __name__ == "__main__": | ||
| 83 | + main() | ||
| @@ -197,8 +197,8 @@ remove_ops_math() { | |||
| 197 | 197 | ||
| 198 | whl_uninstall_package() { | 198 | whl_uninstall_package() { |
| 199 | local _module="$1" | 199 | local _module="$1" |
| 200 | - local _module_apth="$2" | 200 | + local _module_path="$2" |
| 201 | - if [ ! -d "${_module_apth}/${_module}" ]; then | 201 | + if [ ! -d "${_module_path}/${_module}" ]; then |
| 202 | pip3 show "${_module}" > /dev/null 2>&1 | 202 | pip3 show "${_module}" > /dev/null 2>&1 |
| 203 | if [ $? -ne 0 ]; then | 203 | if [ $? -ne 0 ]; then |
| 204 | logandprint "[WARNING]: ${_module} is not exist." | 204 | logandprint "[WARNING]: ${_module} is not exist." |
| @@ -213,7 +213,7 @@ whl_uninstall_package() { | |||
| 213 | fi | 213 | fi |
| 214 | fi | 214 | fi |
| 215 | else | 215 | else |
| 216 | - export PYTHONPATH="${_module_apth}" | 216 | + export PYTHONPATH="${_module_path}" |
| 217 | pip3 uninstall -y "${_module}" > /dev/null 2>&1 | 217 | pip3 uninstall -y "${_module}" > /dev/null 2>&1 |
| 218 | local ret=$? | 218 | local ret=$? |
| 219 | if [ $ret -ne 0 ]; then | 219 | if [ $ret -ne 0 ]; then |
| @@ -31,7 +31,6 @@ call_write_scripts() { | |||
| 31 | 31 | ||
| 32 | local op_compile_option="{\"$op_type\": {" | 32 | local op_compile_option="{\"$op_type\": {" |
| 33 | local inner_properties=() | 33 | local inner_properties=() |
| 34 | - # auto_sync 默认true | ||
| 35 | if [ "$auto_sync" == "false" ]; then | 34 | if [ "$auto_sync" == "false" ]; then |
| 36 | inner_properties+=("\"auto_sync\": $auto_sync") | 35 | inner_properties+=("\"auto_sync\": $auto_sync") |
| 37 | fi | 36 | fi |
| @@ -99,6 +98,22 @@ write_scripts( | |||
| 99 | " | 98 | " |
| 100 | } | 99 | } |
| 101 | 100 | ||
| 101 | +function get_kernel_option_config_file() { | ||
| 102 | + if [ $# -ne 2 ]; then | ||
| 103 | + echo "error invalid param number:$#, must be 2" >&2 | ||
| 104 | + return 1 | ||
| 105 | + fi | ||
| 106 | + local workdir="$1" | ||
| 107 | + local soc_version_lower="$2" | ||
| 108 | + local binary_config_dir=${topdir}/build/tbe/config | ||
| 109 | + | ||
| 110 | + local primary_pattern="${binary_config_dir}/kernel-options-${soc_version_lower}.ini" | ||
| 111 | + if [ -f "${primary_pattern}" ]; then | ||
| 112 | + echo "${primary_pattern}" | ||
| 113 | + return 0 | ||
| 114 | + fi | ||
| 115 | +} | ||
| 116 | + | ||
| 102 | main() { | 117 | main() { |
| 103 | echo "[INFO]excute file: $0 $*" | 118 | echo "[INFO]excute file: $0 $*" |
| 104 | local all_pairs=("$@") | 119 | local all_pairs=("$@") |
| @@ -124,8 +139,37 @@ main() { | |||
| 124 | 139 | ||
| 125 | json_line=$(echo "$ascendc_op_conf" | tr -d '\n\r') | 140 | json_line=$(echo "$ascendc_op_conf" | tr -d '\n\r') |
| 126 | 141 | ||
| 127 | - auto_sync="" | 142 | + local kernel_config_file=$(get_kernel_option_config_file ${workdir} ${soc_version_lower}) |
| 128 | - if [ -n "$json_line" ]; then | 143 | + local kernel_config_file_all=$(get_kernel_option_config_file ${workdir} "ALL") |
| 144 | + | ||
| 145 | + local auto_sync="" | ||
| 146 | + local compile_options="" | ||
| 147 | + if [ -f "${kernel_config_file}" ]; then | ||
| 148 | + auto_sync=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /auto_sync/{print $2; exit}' "$kernel_config_file") | ||
| 149 | + compile_options=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /options/{print substr($0, length("options=") + 1); exit}' "$kernel_config_file") | ||
| 150 | + fi | ||
| 151 | + if [ -z "${auto_sync}" ] && [ -z "${compile_options}" ]; then | ||
| 152 | + if [ -f "${kernel_config_file_all}" ]; then | ||
| 153 | + auto_sync=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /auto_sync/{print $2; exit}' "$kernel_config_file_all") | ||
| 154 | + compile_options=$(awk -F "=" '/\['${op_type}'\]/{flag=1; next}/\[/{flag=0} flag && /options/{print substr($0, length("options=") + 1); exit}' "$kernel_config_file_all") | ||
| 155 | + fi | ||
| 156 | + fi | ||
| 157 | + | ||
| 158 | + if [ -n "${compile_options:-}" ]; then | ||
| 159 | + IFS=',' read -ra options_array <<< "$compile_options" | ||
| 160 | + array="" | ||
| 161 | + for opt in "${options_array[@]}"; do | ||
| 162 | + opt=$(echo "$opt" | xargs) | ||
| 163 | + if [[ -n "$array" ]]; then | ||
| 164 | + array="$array, \"$opt\"" | ||
| 165 | + else | ||
| 166 | + array="\"$opt\"" | ||
| 167 | + fi | ||
| 168 | + done | ||
| 169 | + printf -v compile_options '{"%s": [%s]}' "${soc_version_lower}" "${array}" | ||
| 170 | + fi | ||
| 171 | + | ||
| 172 | + if [ -z "$auto_sync" ] && [ -n "$json_line" ]; then | ||
| 129 | val_part=$(echo "$json_line" | sed -E 's/.*"auto_sync"[[:space:]]*:[[:space:]]*(\{[^}]*\}|true|false).*/\1/') | 173 | val_part=$(echo "$json_line" | sed -E 's/.*"auto_sync"[[:space:]]*:[[:space:]]*(\{[^}]*\}|true|false).*/\1/') |
| 130 | if [ "$val_part" = "true" ] || [ "$val_part" = "false" ]; then | 174 | if [ "$val_part" = "true" ] || [ "$val_part" = "false" ]; then |
| 131 | auto_sync="$val_part" | 175 | auto_sync="$val_part" |
| @@ -149,14 +193,16 @@ main() { | |||
| 149 | print str | 193 | print str |
| 150 | }') | 194 | }') |
| 151 | 195 | ||
| 152 | - compile_options=$(echo "$json_line" | awk ' | 196 | + if [ -z "$compile_options" ]; then |
| 153 | - match($0, /"compile_options"[[:space:]]*:[[:space:]]*(\{[^}]*\})/, arr) { | 197 | + compile_options=$(echo "$json_line" | awk ' |
| 154 | - print arr[1] | 198 | + match($0, /"compile_options"[[:space:]]*:[[:space:]]*(\{[^}]*\})/, arr) { |
| 155 | - }') | 199 | + print arr[1] |
| 200 | + }') | ||
| 201 | + fi | ||
| 156 | 202 | ||
| 157 | call_write_scripts "$op_type" "$soc_version_lower" "$auto_sync" "$compute_units" "$compile_options" | 203 | call_write_scripts "$op_type" "$soc_version_lower" "$auto_sync" "$compute_units" "$compile_options" |
| 158 | done | 204 | done |
| 159 | exit 0 | 205 | exit 0 |
| 160 | } | 206 | } |
| 161 | set -o pipefail | 207 | set -o pipefail |
| 162 | -main "$@" | while IFS= read -r line; do echo "$(date '+[%Y-%m-%d %H:%M:%S]') $line"; done | 208 | +main "$@" |
CMAKE全局变量一般使用全大写