已开启
[Code Detective Challenge 04] 完成 DivCustomTemplate 工程化算子开发 #2116
aodebiao创建于 9 天前
[Code Detective Challenge 04] 完成 DivCustomTemplate 工程化算子开发 #2116
已开启
共 68 个文件变更+9063-0
A2026/CANN-Code-Detective/Challenge04-DivCustomTemplate/gxhgxh2475698/DivCustomTemplate/.gitignore+6-0
| @@ -0,0 +1,6 @@ | |||
| 1 | +custom_op/build_out/ | ||
| 2 | +execute_div_op | ||
| 3 | +*.o | ||
| 4 | +*.so | ||
| 5 | +*.run | ||
| 6 | +.ipynb_checkpoints/ | ||
| @@ -0,0 +1,76 @@ | |||
| 1 | +cmake_minimum_required(VERSION 3.16.0) | ||
| 2 | +project(opp) | ||
| 3 | + | ||
| 4 | +include(cmake/config.cmake) | ||
| 5 | +include(cmake/func.cmake) | ||
| 6 | +include(cmake/intf.cmake) | ||
| 7 | + | ||
| 8 | +if(ENABLE_CROSS_COMPILE) | ||
| 9 | + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL x86_64) | ||
| 10 | + set(CROSS_COMPILE_PLATFORM aarch64) | ||
| 11 | + else() | ||
| 12 | + set(CROSS_COMPILE_PLATFORM x86_64) | ||
| 13 | + endif() | ||
| 14 | + set(PLATFORM ${CMAKE_SYSTEM_PROCESSOR}) | ||
| 15 | + set(CMAKE_COMPILE_COMPILER_LIBRARY ${ASCEND_CANN_PACKAGE_PATH}/${PLATFORM}-linux/devlib/linux/${CROSS_COMPILE_PLATFORM}/) | ||
| 16 | + set(CMAKE_COMPILE_RUNTIME_LIBRARY ${ASCEND_CANN_PACKAGE_PATH}/${PLATFORM}-linux/devlib/${CROSS_COMPILE_PLATFORM}/) | ||
| 17 | + if(CMAKE_CROSS_LIBRARY_PATH) | ||
| 18 | + set(CMAKE_COMPILE_COMPILER_LIBRARY ${CMAKE_CROSS_LIBRARY_PATH}) | ||
| 19 | + set(CMAKE_COMPILE_RUNTIME_LIBRARY ${CMAKE_CROSS_LIBRARY_PATH}) | ||
| 20 | + endif() | ||
| 21 | + set(CMAKE_SYSTEM_PROCESSOR ${CROSS_COMPILE_PLATFORM}) | ||
| 22 | + set(CMAKE_COMPILE ${CMAKE_CXX_COMPILER}) | ||
| 23 | + set(CMAKE_CXX_COMPILER ${CMAKE_CROSS_PLATFORM_COMPILER}) | ||
| 24 | +else() | ||
| 25 | + set(CMAKE_COMPILE ${CMAKE_CXX_COMPILER}) | ||
| 26 | +endif() | ||
| 27 | + | ||
| 28 | +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/framework) | ||
| 29 | + add_subdirectory(framework) | ||
| 30 | +endif() | ||
| 31 | +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/op_host) | ||
| 32 | + add_subdirectory(op_host) | ||
| 33 | +endif() | ||
| 34 | +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/op_kernel) | ||
| 35 | + add_subdirectory(op_kernel) | ||
| 36 | +endif() | ||
| 37 | +if(ENABLE_TEST AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testcases) | ||
| 38 | + add_subdirectory(testcases) | ||
| 39 | +endif() | ||
| 40 | + | ||
| 41 | +# modify vendor_name in install.sh and upgrade.sh | ||
| 42 | +add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/scripts/install.sh ${CMAKE_BINARY_DIR}/scripts/upgrade.sh | ||
| 43 | + COMMAND mkdir -p ${CMAKE_BINARY_DIR}/scripts | ||
| 44 | + COMMAND cp -r ${CMAKE_SOURCE_DIR}/scripts/* ${CMAKE_BINARY_DIR}/scripts/ | ||
| 45 | + COMMAND sed -i "s/vendor_name=customize/vendor_name=${vendor_name}/g" ${CMAKE_BINARY_DIR}/scripts/* | ||
| 46 | +) | ||
| 47 | +add_custom_target(modify_vendor ALL DEPENDS ${CMAKE_BINARY_DIR}/scripts/install.sh ${CMAKE_BINARY_DIR}/scripts/upgrade.sh) | ||
| 48 | + | ||
| 49 | +get_system_info(SYSTEM_INFO) | ||
| 50 | + | ||
| 51 | +# gen version.info | ||
| 52 | +add_custom_target(gen_version_info ALL | ||
| 53 | + COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/cmake/util/gen_version_info.sh ${ASCEND_CANN_PACKAGE_PATH} ${CMAKE_CURRENT_BINARY_DIR} | ||
| 54 | +) | ||
| 55 | + | ||
| 56 | +if(NOT ASCEND_PACK_SHARED_LIBRARY) | ||
| 57 | + install(DIRECTORY ${CMAKE_BINARY_DIR}/scripts/ DESTINATION . FILE_PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_READ) | ||
| 58 | + | ||
| 59 | + install(FILES ${CMAKE_SOURCE_DIR}/custom.proto DESTINATION packages OPTIONAL) | ||
| 60 | + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/version.info | ||
| 61 | + DESTINATION packages/vendors/${vendor_name}/) | ||
| 62 | + | ||
| 63 | + # CPack config | ||
| 64 | + set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME}) | ||
| 65 | + set(CPACK_PACKAGE_VERSION ${CMAKE_PROJECT_VERSION}) | ||
| 66 | + set(CPACK_PACKAGE_DESCRIPTION "CPack opp project") | ||
| 67 | + set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "CPack opp project") | ||
| 68 | + set(CPACK_PACKAGE_DIRECTORY ${CMAKE_INSTALL_PREFIX}) | ||
| 69 | + set(CPACK_PACKAGE_FILE_NAME "custom_opp_${SYSTEM_INFO}.run") | ||
| 70 | + set(CPACK_GENERATOR External) | ||
| 71 | + set(CPACK_CMAKE_GENERATOR "Unix Makefiles") | ||
| 72 | + set(CPACK_EXTERNAL_ENABLE_STAGING TRUE) | ||
| 73 | + set(CPACK_EXTERNAL_PACKAGE_SCRIPT ${CMAKE_SOURCE_DIR}/cmake/makeself.cmake) | ||
| 74 | + set(CPACK_EXTERNAL_BUILT_PACKAGES ${CPACK_PACKAGE_DIRECTORY}/_CPack_Packages/Linux/External/${CPACK_PACKAGE_FILE_NAME}/${CPACK_PACKAGE_FILE_NAME}) | ||
| 75 | + include(CPack) | ||
| 76 | +endif() | ||
| @@ -0,0 +1,67 @@ | |||
| 1 | +{ | ||
| 2 | + "version": 1, | ||
| 3 | + "cmakeMinimumRequired": { | ||
| 4 | + "major": 3, | ||
| 5 | + "minor": 19, | ||
| 6 | + "patch": 0 | ||
| 7 | + }, | ||
| 8 | + "configurePresets": [ | ||
| 9 | + { | ||
| 10 | + "name": "default", | ||
| 11 | + "displayName": "Default Config", | ||
| 12 | + "description": "Default build using Unix Makefiles generator", | ||
| 13 | + "generator": "Unix Makefiles", | ||
| 14 | + "binaryDir": "${sourceDir}/build_out", | ||
| 15 | + "cacheVariables": { | ||
| 16 | + "CMAKE_BUILD_TYPE": { | ||
| 17 | + "type": "STRING", | ||
| 18 | + "value": "Release" | ||
| 19 | + }, | ||
| 20 | + "ENABLE_SOURCE_PACKAGE": { | ||
| 21 | + "type": "BOOL", | ||
| 22 | + "value": "True" | ||
| 23 | + }, | ||
| 24 | + "ENABLE_BINARY_PACKAGE": { | ||
| 25 | + "type": "BOOL", | ||
| 26 | + "value": "True" | ||
| 27 | + }, | ||
| 28 | + "ASCEND_COMPUTE_UNIT": { | ||
| 29 | + "type": "STRING", | ||
| 30 | + "value": "ascend910b" | ||
| 31 | + }, | ||
| 32 | + "ENABLE_TEST": { | ||
| 33 | + "type": "BOOL", | ||
| 34 | + "value": "True" | ||
| 35 | + }, | ||
| 36 | + "vendor_name": { | ||
| 37 | + "type": "STRING", | ||
| 38 | + "value": "customize" | ||
| 39 | + }, | ||
| 40 | + "ASCEND_CANN_PACKAGE_PATH": { | ||
| 41 | + "type": "PATH", | ||
| 42 | + "value": "/usr/local/Ascend/cann-8.5.0" | ||
| 43 | + }, | ||
| 44 | + "ASCEND_PYTHON_EXECUTABLE": { | ||
| 45 | + "type": "STRING", | ||
| 46 | + "value": "python3" | ||
| 47 | + }, | ||
| 48 | + "CMAKE_INSTALL_PREFIX": { | ||
| 49 | + "type": "PATH", | ||
| 50 | + "value": "${sourceDir}/build_out" | ||
| 51 | + }, | ||
| 52 | + "ENABLE_CROSS_COMPILE": { | ||
| 53 | + "type": "BOOL", | ||
| 54 | + "value": "False" | ||
| 55 | + }, | ||
| 56 | + "CMAKE_CROSS_PLATFORM_COMPILER": { | ||
| 57 | + "type": "PATH", | ||
| 58 | + "value": "/usr/bin/aarch64-linux-gnu-g++" | ||
| 59 | + }, | ||
| 60 | + "ASCEND_PACK_SHARED_LIBRARY": { | ||
| 61 | + "type": "BOOL", | ||
| 62 | + "value": "False" | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + ] | ||
| 67 | +} | ||
| @@ -0,0 +1,63 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +if [ -z "$BASE_LIBS_PATH" ]; then | ||
| 3 | + if [ -z "$ASCEND_HOME_PATH" ]; then | ||
| 4 | + if [ -z "$ASCEND_AICPU_PATH" ]; then | ||
| 5 | + echo "please set env." | ||
| 6 | + exit 1 | ||
| 7 | + else | ||
| 8 | + export ASCEND_HOME_PATH=$ASCEND_AICPU_PATH | ||
| 9 | + fi | ||
| 10 | + else | ||
| 11 | + export ASCEND_HOME_PATH=$ASCEND_HOME_PATH | ||
| 12 | + fi | ||
| 13 | +else | ||
| 14 | + export ASCEND_HOME_PATH=$BASE_LIBS_PATH | ||
| 15 | +fi | ||
| 16 | +echo "using ASCEND_HOME_PATH: $ASCEND_HOME_PATH" | ||
| 17 | +script_path=$(realpath $(dirname $0)) | ||
| 18 | + | ||
| 19 | +BUILD_DIR="build_out" | ||
| 20 | +HOST_NATIVE_DIR="host_native_tiling" | ||
| 21 | +mkdir -p build_out | ||
| 22 | +rm -rf build_out/* | ||
| 23 | + | ||
| 24 | +opts=$(python3 $script_path/cmake/util/preset_parse.py $script_path/CMakePresets.json) | ||
| 25 | +ENABLE_CROSS="-DENABLE_CROSS_COMPILE=True" | ||
| 26 | +ENABLE_BINARY="-DENABLE_BINARY_PACKAGE=True" | ||
| 27 | +ENABLE_LIBRARY="-DASCEND_PACK_SHARED_LIBRARY=True" | ||
| 28 | +cmake_version=$(cmake --version | grep "cmake version" | awk '{print $3}') | ||
| 29 | + | ||
| 30 | +target=package | ||
| 31 | +if [ "$1"x != ""x ]; then target=$1; fi | ||
| 32 | +if [[ $opts =~ $ENABLE_LIBRARY ]]; then target=install; fi | ||
| 33 | + | ||
| 34 | +if [[ $opts =~ $ENABLE_CROSS ]] && [[ $opts =~ $ENABLE_BINARY ]] | ||
| 35 | +then | ||
| 36 | + if [ "$cmake_version" \< "3.19.0" ] ; then | ||
| 37 | + cmake -S . -B "$BUILD_DIR" $opts -DENABLE_CROSS_COMPILE=0 | ||
| 38 | + else | ||
| 39 | + cmake -S . -B "$BUILD_DIR" --preset=default -DENABLE_CROSS_COMPILE=0 | ||
| 40 | + fi | ||
| 41 | + cmake --build "$BUILD_DIR" --target cust_optiling | ||
| 42 | + mkdir $BUILD_DIR/$HOST_NATIVE_DIR | ||
| 43 | + cp $(find $BUILD_DIR -name "libcust_opmaster_rt2.0.so") $BUILD_DIR/$HOST_NATIVE_DIR | ||
| 44 | + cp -r $BUILD_DIR/$HOST_NATIVE_DIR . | ||
| 45 | + rm -rf $BUILD_DIR/* | ||
| 46 | + mv $HOST_NATIVE_DIR $BUILD_DIR | ||
| 47 | + host_native_tiling_lib=$(realpath $(find $BUILD_DIR -type f -name "libcust_opmaster_rt2.0.so")) | ||
| 48 | + if [ "$cmake_version" \< "3.19.0" ] ; then | ||
| 49 | + cmake -S . -B "$BUILD_DIR" $opts -DHOST_NATIVE_TILING_LIB=$host_native_tiling_lib | ||
| 50 | + else | ||
| 51 | + cmake -S . -B "$BUILD_DIR" --preset=default -DHOST_NATIVE_TILING_LIB=$host_native_tiling_lib | ||
| 52 | + fi | ||
| 53 | + cmake --build "$BUILD_DIR" --target binary -j$(nproc) | ||
| 54 | + cmake --build "$BUILD_DIR" --target $target -j$(nproc) | ||
| 55 | +else | ||
| 56 | + if [ "$cmake_version" \< "3.19.0" ] ; then | ||
| 57 | + cmake -S . -B "$BUILD_DIR" $opts | ||
| 58 | + else | ||
| 59 | + cmake -S . -B "$BUILD_DIR" --preset=default | ||
| 60 | + fi | ||
| 61 | + cmake --build "$BUILD_DIR" --target binary -j$(nproc) | ||
| 62 | + cmake --build "$BUILD_DIR" --target $target -j$(nproc) | ||
| 63 | +fi | ||
| @@ -0,0 +1,50 @@ | |||
| 1 | + | ||
| 2 | +set(CMAKE_CXX_FLAGS_DEBUG "") | ||
| 3 | +set(CMAKE_CXX_FLAGS_RELEASE "") | ||
| 4 | + | ||
| 5 | +if (NOT DEFINED vendor_name) | ||
| 6 | + set(vendor_name customize CACHE STRING "") | ||
| 7 | +endif() | ||
| 8 | +if (NOT DEFINED CMAKE_BUILD_TYPE) | ||
| 9 | + set(CMAKE_BUILD_TYPE Release CACHE STRING "") | ||
| 10 | +endif() | ||
| 11 | +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) | ||
| 12 | + set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/build_out" CACHE PATH "" FORCE) | ||
| 13 | +endif() | ||
| 14 | +if (NOT DEFINED ASCEND_CANN_PACKAGE_PATH) | ||
| 15 | + set(ASCEND_CANN_PACKAGE_PATH /usr/local/Ascend/latest CACHE PATH "") | ||
| 16 | +endif() | ||
| 17 | +if (NOT DEFINED ASCEND_PYTHON_EXECUTABLE) | ||
| 18 | + set(ASCEND_PYTHON_EXECUTABLE python3 CACHE STRING "") | ||
| 19 | +endif() | ||
| 20 | +if (NOT DEFINED ASCEND_COMPUTE_UNIT) | ||
| 21 | + set(ASCEND_COMPUTE_UNIT ascend910b CACHE STRING "") | ||
| 22 | +endif() | ||
| 23 | +if (NOT DEFINED ENABLE_TEST) | ||
| 24 | + set(ENABLE_TEST FALSE CACHE BOOL "") | ||
| 25 | +endif() | ||
| 26 | +if (NOT DEFINED ENABLE_CROSS_COMPILE) | ||
| 27 | + set(ENABLE_CROSS_COMPILE FALSE CACHE BOOL "") | ||
| 28 | +endif() | ||
| 29 | +if (NOT DEFINED CMAKE_CROSS_PLATFORM_COMPILER) | ||
| 30 | + set(CMAKE_CROSS_PLATFORM_COMPILER "/your/cross/compiler/path" CACHE PATH "") | ||
| 31 | +endif() | ||
| 32 | +if (NOT DEFINED CMAKE_CROSS_LIBRARY_PATH) | ||
| 33 | + set(CMAKE_CROSS_LIBRARY_PATH "" CACHE PATH "") | ||
| 34 | +endif() | ||
| 35 | +if (NOT DEFINED ASCEND_PACK_SHARED_LIBRARY) | ||
| 36 | + set(ASCEND_PACK_SHARED_LIBRARY False CACHE BOOL "") | ||
| 37 | +endif() | ||
| 38 | +set(ASCEND_TENSOR_COMPILER_PATH ${ASCEND_CANN_PACKAGE_PATH}/compiler) | ||
| 39 | +set(ASCEND_CCEC_COMPILER_PATH ${ASCEND_TENSOR_COMPILER_PATH}/ccec_compiler/bin) | ||
| 40 | +set(ASCEND_AUTOGEN_PATH ${CMAKE_BINARY_DIR}/autogen) | ||
| 41 | +set(ASCEND_AUTOGEN_GROUPPROTO_PATH ${CMAKE_BINARY_DIR}/autogen/group_proto) | ||
| 42 | +set(ASCEND_FRAMEWORK_TYPE tensorflow) | ||
| 43 | +file(MAKE_DIRECTORY ${ASCEND_AUTOGEN_PATH} ${ASCEND_AUTOGEN_GROUPPROTO_PATH}) | ||
| 44 | +set(CUSTOM_COMPILE_OPTIONS "custom_compile_options.ini") | ||
| 45 | +set(CUSTOM_OPC_OPTIONS "custom_opc_options.ini") | ||
| 46 | +execute_process(COMMAND rm -rf ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS} | ||
| 47 | + COMMAND rm -rf ${ASCEND_AUTOGEN_PATH}/${CUSTOM_OPC_OPTIONS} | ||
| 48 | + COMMAND touch ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS} | ||
| 49 | + COMMAND touch ${ASCEND_AUTOGEN_PATH}/${CUSTOM_OPC_OPTIONS} | ||
| 50 | + ) | ||
| @@ -0,0 +1,50 @@ | |||
| 1 | +message(STATUS "TILING SINK TASK BEGIN") | ||
| 2 | +message(STATUS "TARGET: ${TARGET}") | ||
| 3 | +message(STATUS "OPTION: ${OPTION}") | ||
| 4 | +message(STATUS "SRC: ${SRC}") | ||
| 5 | +message(STATUS "VENDOR: ${VENDOR_NAME}") | ||
| 6 | + | ||
| 7 | +set(CMAKE_CXX_COMPILER ${ASCEND_CANN_PACKAGE_PATH}/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++) | ||
| 8 | +set(CMAKE_C_COMPILER ${ASCEND_CANN_PACKAGE_PATH}/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-gcc) | ||
| 9 | + | ||
| 10 | +string(REPLACE " " ";" SRC "${SRC}") | ||
| 11 | +add_library(${TARGET} ${OPTION} | ||
| 12 | + ${SRC} | ||
| 13 | +) | ||
| 14 | +target_compile_definitions(${TARGET} PRIVATE | ||
| 15 | + DEVICE_OP_TILING_LIB | ||
| 16 | + DEVICE_OP_LOG_BY_DUMP | ||
| 17 | + _FORTIFY_SOURCE=2 | ||
| 18 | + google=ascend_private | ||
| 19 | +) | ||
| 20 | +target_include_directories(${TARGET} PRIVATE | ||
| 21 | + ${ASCEND_CANN_PACKAGE_PATH}/include | ||
| 22 | +) | ||
| 23 | +target_compile_options(${TARGET} PRIVATE | ||
| 24 | + -fPIC | ||
| 25 | + -fstack-protector-strong | ||
| 26 | + -fstack-protector-all | ||
| 27 | + -O2 | ||
| 28 | + -std=c++11 | ||
| 29 | + -fvisibility-inlines-hidden | ||
| 30 | + -fvisibility=hidden | ||
| 31 | +) | ||
| 32 | +target_link_libraries(${TARGET} PRIVATE | ||
| 33 | + -Wl,--whole-archive | ||
| 34 | + device_register | ||
| 35 | + c_sec | ||
| 36 | + mmpa | ||
| 37 | + tiling_api | ||
| 38 | + platform_static | ||
| 39 | + ascend_protobuf | ||
| 40 | + exe_meta_device | ||
| 41 | + aicpu_cust_log | ||
| 42 | + -Wl,--no-whole-archive | ||
| 43 | +) | ||
| 44 | +target_link_directories(${TARGET} PRIVATE | ||
| 45 | + ${ASCEND_CANN_PACKAGE_PATH}/lib64/device/lib64 | ||
| 46 | + ${ASCEND_CANN_PACKAGE_PATH}/compiler/lib64 | ||
| 47 | +) | ||
| 48 | +set_target_properties(${TARGET} PROPERTIES | ||
| 49 | + OUTPUT_NAME cust_opmaster | ||
| 50 | +) | ||
| @@ -0,0 +1,427 @@ | |||
| 1 | +include(ExternalProject) | ||
| 2 | + | ||
| 3 | +function(get_system_info SYSTEM_INFO) | ||
| 4 | + if (UNIX) | ||
| 5 | + execute_process(COMMAND grep -i ^id= /etc/os-release OUTPUT_VARIABLE TEMP) | ||
| 6 | + string(REGEX REPLACE "\n|id=|ID=|\"" "" SYSTEM_NAME ${TEMP}) | ||
| 7 | + set(${SYSTEM_INFO} ${SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR} PARENT_SCOPE) | ||
| 8 | + elseif (WIN32) | ||
| 9 | + message(STATUS "System is Windows. Only for pre-build.") | ||
| 10 | + else () | ||
| 11 | + message(FATAL_ERROR "${CMAKE_SYSTEM_NAME} not support.") | ||
| 12 | + endif () | ||
| 13 | +endfunction() | ||
| 14 | + | ||
| 15 | +function(opbuild) | ||
| 16 | + message(STATUS "Opbuild generating sources") | ||
| 17 | + cmake_parse_arguments(OPBUILD "" "OUT_DIR;PROJECT_NAME;ACCESS_PREFIX;ENABLE_SOURCE" "OPS_SRC" ${ARGN}) | ||
| 18 | + execute_process(COMMAND ${CMAKE_COMPILE} -g -fPIC -shared -std=c++11 ${OPBUILD_OPS_SRC} -D_GLIBCXX_USE_CXX11_ABI=0 | ||
| 19 | + -I ${ASCEND_CANN_PACKAGE_PATH}/include -I ${CMAKE_CURRENT_SOURCE_DIR}/../op_kernel | ||
| 20 | + -L ${ASCEND_CANN_PACKAGE_PATH}/lib64 -lexe_graph -lregister -ltiling_api | ||
| 21 | + -o ${OPBUILD_OUT_DIR}/libascend_all_ops.so | ||
| 22 | + RESULT_VARIABLE EXEC_RESULT | ||
| 23 | + OUTPUT_VARIABLE EXEC_INFO | ||
| 24 | + ERROR_VARIABLE EXEC_ERROR | ||
| 25 | + ) | ||
| 26 | + if (${EXEC_RESULT}) | ||
| 27 | + message("build ops lib info: ${EXEC_INFO}") | ||
| 28 | + message("build ops lib error: ${EXEC_ERROR}") | ||
| 29 | + message(FATAL_ERROR "opbuild run failed!") | ||
| 30 | + endif() | ||
| 31 | + set(proj_env "") | ||
| 32 | + set(prefix_env "") | ||
| 33 | + if (NOT "${OPBUILD_PROJECT_NAME}x" STREQUAL "x") | ||
| 34 | + set(ENV{OPS_PROJECT_NAME} ${OPBUILD_PROJECT_NAME}) | ||
| 35 | + endif() | ||
| 36 | + if (NOT "${OPBUILD_ACCESS_PREFIX}x" STREQUAL "x") | ||
| 37 | + set(ENV{OPS_DIRECT_ACCESS_PREFIX} ${OPBUILD_ACCESS_PREFIX}) | ||
| 38 | + endif() | ||
| 39 | + | ||
| 40 | + set(ENV{ENABLE_SOURCE_PACAKGE} ${OPBUILD_ENABLE_SOURCE}) | ||
| 41 | + if(${ASCEND_PACK_SHARED_LIBRARY}) | ||
| 42 | + if (NOT vendor_name) | ||
| 43 | + message(FATAL_ERROR "ERROR: vendor_name is invalid!") | ||
| 44 | + return() | ||
| 45 | + endif() | ||
| 46 | + set(ENV{ASCEND_VENDOR_NAME} ${vendor_name}) | ||
| 47 | + set(ENV{OPS_PRODUCT_NAME} ${ASCEND_COMPUTE_UNIT}) | ||
| 48 | + set(ENV{SYSTEM_PROCESSOR} ${CMAKE_SYSTEM_PROCESSOR}) | ||
| 49 | + endif() | ||
| 50 | + execute_process(COMMAND ${ASCEND_CANN_PACKAGE_PATH}/toolkit/tools/opbuild/op_build | ||
| 51 | + ${OPBUILD_OUT_DIR}/libascend_all_ops.so ${OPBUILD_OUT_DIR} | ||
| 52 | + RESULT_VARIABLE EXEC_RESULT | ||
| 53 | + OUTPUT_VARIABLE EXEC_INFO | ||
| 54 | + ERROR_VARIABLE EXEC_ERROR | ||
| 55 | + ) | ||
| 56 | + unset(ENV{ENABLE_SOURCE_PACAKGE}) | ||
| 57 | + if(${ASCEND_PACK_SHARED_LIBRARY}) | ||
| 58 | + unset(ENV{ASCEND_VENDOR_NAME}) | ||
| 59 | + unset(ENV{OPS_PRODUCT_NAME}) | ||
| 60 | + unset(ENV{SYSTEM_PROCESSOR}) | ||
| 61 | + endif() | ||
| 62 | + if (${EXEC_RESULT}) | ||
| 63 | + message("opbuild ops info: ${EXEC_INFO}") | ||
| 64 | + message("opbuild ops error: ${EXEC_ERROR}") | ||
| 65 | + endif() | ||
| 66 | + message(STATUS "Opbuild generating sources - done") | ||
| 67 | +endfunction() | ||
| 68 | + | ||
| 69 | +function(add_ops_info_target) | ||
| 70 | + cmake_parse_arguments(OPINFO "" "TARGET;OPS_INFO;OUTPUT;INSTALL_DIR" "" ${ARGN}) | ||
| 71 | + get_filename_component(opinfo_file_path "${OPINFO_OUTPUT}" DIRECTORY) | ||
| 72 | + add_custom_command(OUTPUT ${OPINFO_OUTPUT} | ||
| 73 | + COMMAND mkdir -p ${opinfo_file_path} | ||
| 74 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/parse_ini_to_json.py | ||
| 75 | + ${OPINFO_OPS_INFO} ${OPINFO_OUTPUT} | ||
| 76 | + ) | ||
| 77 | + add_custom_target(${OPINFO_TARGET} ALL | ||
| 78 | + DEPENDS ${OPINFO_OUTPUT} | ||
| 79 | + ) | ||
| 80 | + if(NOT ${ASCEND_PACK_SHARED_LIBRARY}) | ||
| 81 | + install(FILES ${OPINFO_OUTPUT} | ||
| 82 | + DESTINATION ${OPINFO_INSTALL_DIR} | ||
| 83 | + ) | ||
| 84 | + endif() | ||
| 85 | +endfunction() | ||
| 86 | + | ||
| 87 | +function(add_ops_compile_options OP_TYPE) | ||
| 88 | + cmake_parse_arguments(OP_COMPILE "" "OP_TYPE" "COMPUTE_UNIT;OPTIONS" ${ARGN}) | ||
| 89 | + execute_process(COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_gen_options.py | ||
| 90 | + ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS} ${OP_TYPE} ${OP_COMPILE_COMPUTE_UNIT} | ||
| 91 | + ${OP_COMPILE_OPTIONS} | ||
| 92 | + RESULT_VARIABLE EXEC_RESULT | ||
| 93 | + OUTPUT_VARIABLE EXEC_INFO | ||
| 94 | + ERROR_VARIABLE EXEC_ERROR) | ||
| 95 | + if (${EXEC_RESULT}) | ||
| 96 | + message("add ops compile options info: ${EXEC_INFO}") | ||
| 97 | + message("add ops compile options error: ${EXEC_ERROR}") | ||
| 98 | + message(FATAL_ERROR "add ops compile options failed!") | ||
| 99 | + endif() | ||
| 100 | +endfunction() | ||
| 101 | + | ||
| 102 | +function(add_npu_support_target) | ||
| 103 | + cmake_parse_arguments(NPUSUP "" "TARGET;OPS_INFO_DIR;OUT_DIR;INSTALL_DIR" "" ${ARGN}) | ||
| 104 | + get_filename_component(npu_sup_file_path "${NPUSUP_OUT_DIR}" DIRECTORY) | ||
| 105 | + add_custom_command(OUTPUT ${NPUSUP_OUT_DIR}/npu_supported_ops.json | ||
| 106 | + COMMAND mkdir -p ${NPUSUP_OUT_DIR} | ||
| 107 | + COMMAND ${CMAKE_SOURCE_DIR}/cmake/util/gen_ops_filter.sh | ||
| 108 | + ${NPUSUP_OPS_INFO_DIR} | ||
| 109 | + ${NPUSUP_OUT_DIR} | ||
| 110 | + ) | ||
| 111 | + add_custom_target(npu_supported_ops ALL | ||
| 112 | + DEPENDS ${NPUSUP_OUT_DIR}/npu_supported_ops.json | ||
| 113 | + ) | ||
| 114 | + if(NOT ${ASCEND_PACK_SHARED_LIBRARY}) | ||
| 115 | + install(FILES ${NPUSUP_OUT_DIR}/npu_supported_ops.json | ||
| 116 | + DESTINATION ${NPUSUP_INSTALL_DIR} | ||
| 117 | + ) | ||
| 118 | + endif() | ||
| 119 | +endfunction() | ||
| 120 | + | ||
| 121 | +function(add_simple_kernel_compile) | ||
| 122 | + set(options "") | ||
| 123 | + set(single_value_args "OPS_INFO;OUT_DIR;TILING_LIB;OP_TYPE;SRC;COMPUTE_UNIT;JSON_FILE;DYNAMIC_PATH") | ||
| 124 | + set(multi_value_args "OPTIONS;CONFIGS") | ||
| 125 | + cmake_parse_arguments(BINCMP "${options}" "${single_value_args}" "${multi_value_args}" ${ARGN}) | ||
| 126 | + if (NOT DEFINED BINCMP_OUT_DIR) | ||
| 127 | + set(BINCMP_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/binary) | ||
| 128 | + endif() | ||
| 129 | + if (NOT DEFINED BINCMP_TILING_LIB) | ||
| 130 | + set(BINCMP_TILING_LIB $<TARGET_FILE:cust_optiling>) | ||
| 131 | + endif() | ||
| 132 | + if (${ASCEND_PACK_SHARED_LIBRARY}) | ||
| 133 | + if (NOT TARGET op_kernel_pack) | ||
| 134 | + add_custom_target(op_kernel_pack | ||
| 135 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_pack_kernel.py | ||
| 136 | + --input-path=${BINCMP_OUT_DIR} | ||
| 137 | + --output-path=${BINCMP_OUT_DIR}/library | ||
| 138 | + --enable-library=${ASCEND_PACK_SHARED_LIBRARY} | ||
| 139 | + --platform=${CMAKE_SYSTEM_PROCESSOR}) | ||
| 140 | + add_library(ascend_kernels INTERFACE) | ||
| 141 | + target_link_libraries(ascend_kernels INTERFACE kernels) | ||
| 142 | + target_link_directories(ascend_kernels INTERFACE ${BINCMP_OUT_DIR}/library) | ||
| 143 | + target_include_directories(ascend_kernels INTERFACE ${BINCMP_OUT_DIR}/library) | ||
| 144 | + add_dependencies(ascend_kernels op_kernel_pack) | ||
| 145 | + add_dependencies(op_kernel_pack ascendc_bin_${BINCMP_COMPUTE_UNIT}_gen_ops_config) | ||
| 146 | + endif() | ||
| 147 | + endif() | ||
| 148 | + # add Environment Variable Configurations of ccache | ||
| 149 | + set(_ASCENDC_ENV_VAR) | ||
| 150 | + if(${CMAKE_CXX_COMPILER_LAUNCHER} MATCHES "ccache$") | ||
| 151 | + list(APPEND _ASCENDC_ENV_VAR export ASCENDC_CCACHE_EXECUTABLE=${CMAKE_CXX_COMPILER_LAUNCHER} &&) | ||
| 152 | + endif() | ||
| 153 | + | ||
| 154 | + if (NOT DEFINED BINCMP_OPS_INFO) | ||
| 155 | + set(BINCMP_OPS_INFO ${ASCEND_AUTOGEN_PATH}/aic-${BINCMP_COMPUTE_UNIT}-ops-info.ini) | ||
| 156 | + endif() | ||
| 157 | + if (NOT ${ENABLE_CROSS_COMPILE}) | ||
| 158 | + add_custom_target(${BINCMP_OP_TYPE}_${BINCMP_COMPUTE_UNIT} | ||
| 159 | + COMMAND ${_ASCENDC_ENV_VAR} ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_compile_kernel.py | ||
| 160 | + --op-name=${BINCMP_OP_TYPE} | ||
| 161 | + --src-file=${BINCMP_SRC} | ||
| 162 | + --compute-unit=${BINCMP_COMPUTE_UNIT} | ||
| 163 | + --compile-options=\"${BINCMP_OPTIONS}\" | ||
| 164 | + --debug-config=\"${BINCMP_CONFIGS}\" | ||
| 165 | + --config-ini=${BINCMP_OPS_INFO} | ||
| 166 | + --tiling-lib=${BINCMP_TILING_LIB} | ||
| 167 | + --output-path=${BINCMP_OUT_DIR} | ||
| 168 | + --dynamic-dir=${BINCMP_DYNAMIC_PATH} | ||
| 169 | + --enable-binary=\"${ENABLE_BINARY_PACKAGE}\" | ||
| 170 | + --json-file=${BINCMP_JSON_FILE} | ||
| 171 | + --build-tool=$(MAKE)) | ||
| 172 | + add_dependencies(${BINCMP_OP_TYPE}_${BINCMP_COMPUTE_UNIT} cust_optiling) | ||
| 173 | + else() | ||
| 174 | + if (${ENABLE_BINARY_PACKAGE} AND NOT DEFINED HOST_NATIVE_TILING_LIB) | ||
| 175 | + message(FATAL_ERROR "Native host libs was not set for cross compile!") | ||
| 176 | + endif() | ||
| 177 | + add_custom_target(${BINCMP_OP_TYPE}_${BINCMP_COMPUTE_UNIT} | ||
| 178 | + COMMAND ${_ASCENDC_ENV_VAR} ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_compile_kernel.py | ||
| 179 | + --op-name=${BINCMP_OP_TYPE} | ||
| 180 | + --src-file=${BINCMP_SRC} | ||
| 181 | + --compute-unit=${BINCMP_COMPUTE_UNIT} | ||
| 182 | + --compile-options=\"${BINCMP_OPTIONS}\" | ||
| 183 | + --debug-config=\"${BINCMP_CONFIGS}\" | ||
| 184 | + --config-ini=${BINCMP_OPS_INFO} | ||
| 185 | + --tiling-lib=${HOST_NATIVE_TILING_LIB} | ||
| 186 | + --output-path=${BINCMP_OUT_DIR} | ||
| 187 | + --dynamic-dir=${BINCMP_DYNAMIC_PATH} | ||
| 188 | + --enable-binary=\"${ENABLE_BINARY_PACKAGE}\" | ||
| 189 | + --json-file=${BINCMP_JSON_FILE} | ||
| 190 | + --build-tool=$(MAKE)) | ||
| 191 | + endif() | ||
| 192 | + add_dependencies(ascendc_bin_${BINCMP_COMPUTE_UNIT}_gen_ops_config ${BINCMP_OP_TYPE}_${BINCMP_COMPUTE_UNIT}) | ||
| 193 | + add_dependencies(${BINCMP_OP_TYPE}_${BINCMP_COMPUTE_UNIT} ops_info_gen_${BINCMP_COMPUTE_UNIT}) | ||
| 194 | +endfunction() | ||
| 195 | + | ||
| 196 | +function(ascendc_device_library) | ||
| 197 | + message(STATUS "Ascendc device library generating") | ||
| 198 | + cmake_parse_arguments(DEVICE "" "TARGET;OPTION" "SRC" ${ARGN}) | ||
| 199 | + execute_process( | ||
| 200 | + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/tiling_sink | ||
| 201 | + COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/tiling_sink/CMakeLists.txt | ||
| 202 | + ) | ||
| 203 | + execute_process( | ||
| 204 | + COMMAND ${CMAKE_COMMAND} -E echo "cmake_minimum_required(VERSION 3.16.0)\nproject(cust_tiling_sink)\ninclude(${CMAKE_SOURCE_DIR}/cmake/device_task.cmake)\n" | ||
| 205 | + OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/tiling_sink/CMakeLists.txt | ||
| 206 | + RESULT_VARIABLE result | ||
| 207 | + ) | ||
| 208 | + string(REPLACE ";" " " DEVICE_SRC "${DEVICE_SRC}") | ||
| 209 | + ExternalProject_Add(tiling_sink_task | ||
| 210 | + SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/tiling_sink | ||
| 211 | + CONFIGURE_COMMAND ${CMAKE_COMMAND} | ||
| 212 | + -DASCEND_CANN_PACKAGE_PATH=${ASCEND_CANN_PACKAGE_PATH} | ||
| 213 | + -DTARGET=${DEVICE_TARGET} | ||
| 214 | + -DOPTION=${DEVICE_OPTION} | ||
| 215 | + -DSRC=${DEVICE_SRC} | ||
| 216 | + -DVENDOR_NAME=${vendor_name} | ||
| 217 | + <SOURCE_DIR> | ||
| 218 | + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} | ||
| 219 | + INSTALL_COMMAND "" | ||
| 220 | + BUILD_ALWAYS TRUE | ||
| 221 | + ) | ||
| 222 | + ExternalProject_Get_Property(tiling_sink_task BINARY_DIR) | ||
| 223 | + set(TILINGSINK_LIB_PATH "") | ||
| 224 | + if ("${DEVICE_OPTION}" STREQUAL "SHARED") | ||
| 225 | + set(TILINGSINK_LIB_PATH "${BINARY_DIR}/libcust_opmaster.so") | ||
| 226 | + else() | ||
| 227 | + set(TILINGSINK_LIB_PATH "${BINARY_DIR}/libcust_opmaster.a") | ||
| 228 | + endif() | ||
| 229 | + install(FILES ${TILINGSINK_LIB_PATH} | ||
| 230 | + DESTINATION packages/vendors/${vendor_name}/op_impl/ai_core/tbe/op_master_device/lib | ||
| 231 | + ) | ||
| 232 | +endfunction() | ||
| 233 | +function(add_opregistry_target) | ||
| 234 | + string(REPLACE ";" "-" COMPUTE_UNIT "${ASCEND_COMPUTE_UNIT}") | ||
| 235 | + add_custom_target(op_registry_pack | ||
| 236 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_pack_opregistry.py | ||
| 237 | + --registry-file-path=${ASCEND_AUTOGEN_PATH} | ||
| 238 | + --input-path=${CMAKE_SOURCE_DIR}/build_out/ | ||
| 239 | + --base-path=${CMAKE_SOURCE_DIR}/build_out/tmp/vendors/ | ||
| 240 | + --output-path=${CMAKE_SOURCE_DIR}/build_out/library/ | ||
| 241 | + --vendor-name=${vendor_name} | ||
| 242 | + --compute-unit=${COMPUTE_UNIT} | ||
| 243 | + --framework-type=${ASCEND_FRAMEWORK_TYPE} | ||
| 244 | + --platform=${CMAKE_SYSTEM_PROCESSOR}) | ||
| 245 | + add_library(ascend_opregistry INTERFACE) | ||
| 246 | + target_link_libraries(ascend_opregistry INTERFACE opregistry) | ||
| 247 | + target_link_directories(ascend_opregistry INTERFACE ${CMAKE_SOURCE_DIR}/build_out/library) | ||
| 248 | + target_include_directories(ascend_opregistry INTERFACE ${CMAKE_SOURCE_DIR}/build_out/library) | ||
| 249 | + add_dependencies(ascend_opregistry op_registry_pack) | ||
| 250 | + if(EXISTS "${CMAKE_SOURCE_DIR}/framework/caffe_plugin") | ||
| 251 | + add_dependencies(op_registry_pack cust_caffe_parsers) | ||
| 252 | + elseif(EXISTS "${CMAKE_SOURCE_DIR}/framework/tf_plugin") | ||
| 253 | + add_dependencies(op_registry_pack cust_tf_parsers) | ||
| 254 | + elseif(EXISTS "${CMAKE_SOURCE_DIR}/framework/onnx_plugin") | ||
| 255 | + add_dependencies(op_registry_pack cust_onnx_parsers) | ||
| 256 | + endif() | ||
| 257 | +endfunction() | ||
| 258 | + | ||
| 259 | +function(add_kernels_install) | ||
| 260 | + # install kernel file | ||
| 261 | + if (${ENABLE_SOURCE_PACKAGE}) | ||
| 262 | + install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/binary/dynamic/ | ||
| 263 | + DESTINATION packages/vendors/${vendor_name}/op_impl/ai_core/tbe/${vendor_name}_impl/dynamic/ | ||
| 264 | + ) | ||
| 265 | + endif() | ||
| 266 | + | ||
| 267 | + # install *.o files and *.json files | ||
| 268 | + if (${ENABLE_BINARY_PACKAGE}) | ||
| 269 | + set(INSTALL_DIR packages/vendors/${vendor_name}/op_impl/ai_core/tbe/) | ||
| 270 | + foreach(compute_unit ${ASCEND_COMPUTE_UNIT}) | ||
| 271 | + install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/binary/${compute_unit}/ | ||
| 272 | + DESTINATION ${INSTALL_DIR}/kernel/${compute_unit}/ | ||
| 273 | + ) | ||
| 274 | + endforeach() | ||
| 275 | + install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/binary/config/ | ||
| 276 | + DESTINATION ${INSTALL_DIR}/kernel/config/ | ||
| 277 | + ) | ||
| 278 | + endif() | ||
| 279 | +endfunction() | ||
| 280 | + | ||
| 281 | +function(add_static_library STATIC_LIB_NAME) | ||
| 282 | + set(WORKING_DIR ${CMAKE_CURRENT_BINARY_DIR}) | ||
| 283 | + set(OUTPUT_LIB lib${STATIC_LIB_NAME}.a) | ||
| 284 | + set(FILE_LIST_TMP_DIR ${WORKING_DIR}/.static_lib_file_list) | ||
| 285 | + | ||
| 286 | + file(REMOVE_RECURSE ${FILE_LIST_TMP_DIR}) | ||
| 287 | + file(MAKE_DIRECTORY ${FILE_LIST_TMP_DIR}) | ||
| 288 | + | ||
| 289 | + set(FILE_LIST "") | ||
| 290 | + set(TARGET_LIST "") | ||
| 291 | + foreach(TARGET IN LISTS ARGN) | ||
| 292 | + set(CUR_FILE ${FILE_LIST_TMP_DIR}/${TARGET}.txt) | ||
| 293 | + list(APPEND FILE_LIST ${CUR_FILE}) | ||
| 294 | + list(APPEND TARGET_LIST ${TARGET}) | ||
| 295 | + file(GENERATE OUTPUT ${CUR_FILE} | ||
| 296 | + CONTENT "$<JOIN:$<TARGET_OBJECTS:${TARGET}>,\n>" | ||
| 297 | + ) | ||
| 298 | + endforeach() | ||
| 299 | + | ||
| 300 | + string(JOIN " " FILE_LIST_PARAM ${FILE_LIST}) | ||
| 301 | + set(COLLECT_TMP_DIR ${WORKING_DIR}/.static_lib_tmp) | ||
| 302 | + set(KERNEL_LIBS "${CMAKE_BINARY_DIR}/library/libopregistry.a ${CMAKE_BINARY_DIR}/op_kernel/binary/library/libkernels.a") | ||
| 303 | + add_custom_command( | ||
| 304 | + OUTPUT ${OUTPUT_LIB} | ||
| 305 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_gen_static_library.py | ||
| 306 | + --object-files=${FILE_LIST_PARAM} | ||
| 307 | + --kernel-libs=${KERNEL_LIBS} | ||
| 308 | + --tmp-obj-dir=${COLLECT_TMP_DIR} | ||
| 309 | + --output-file=${OUTPUT_LIB} | ||
| 310 | + --remove-tmp-files=1 | ||
| 311 | + DEPENDS ${TARGET_LIST} ${FILE_LIST} | ||
| 312 | + COMMENT "Merging static libraries into ${OUTPUT_LIB}" | ||
| 313 | + VERBATIM | ||
| 314 | + ) | ||
| 315 | + add_custom_target(${STATIC_LIB_NAME} ALL DEPENDS ${OUTPUT_LIB}) | ||
| 316 | +endfunction() | ||
| 317 | + | ||
| 318 | +function(add_vendor_cmake VENDOR_NAME OUTPUT_PATH STATIC_LIB_NAME DYNAMIC_LIB_NAME) | ||
| 319 | + file(MAKE_DIRECTORY ${OUTPUT_PATH}) | ||
| 320 | + execute_process( | ||
| 321 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_gen_vendor_cmake.py | ||
| 322 | + --vendor-name=${VENDOR_NAME} | ||
| 323 | + --output-path=${OUTPUT_PATH} | ||
| 324 | + --static-lib-name=${STATIC_LIB_NAME} | ||
| 325 | + --dynamic-lib-name=${DYNAMIC_LIB_NAME} | ||
| 326 | + RESULT_VARIABLE result | ||
| 327 | + ) | ||
| 328 | + | ||
| 329 | + if(NOT result EQUAL 0) | ||
| 330 | + message(FATAL_ERROR "Create config.cmake and targets.cmake failed.") | ||
| 331 | + endif() | ||
| 332 | +endfunction() | ||
| 333 | + | ||
| 334 | +function(add_kernels_compile) | ||
| 335 | + set(DYNAMIC_PATH "") | ||
| 336 | + if (${ENABLE_SOURCE_PACKAGE}) | ||
| 337 | + set(DYNAMIC_PATH ${CMAKE_CURRENT_BINARY_DIR}/binary/dynamic) | ||
| 338 | + execute_process(COMMAND sh -c "mkdir -p ${DYNAMIC_PATH} && | ||
| 339 | + cp -rf ${CMAKE_SOURCE_DIR}/op_kernel/* ${DYNAMIC_PATH}/ && | ||
| 340 | + rm ${DYNAMIC_PATH}/CMakeLists.txt" | ||
| 341 | + RESULT_VARIABLE EXEC_RESULT | ||
| 342 | + ERROR_VARIABLE EXEC_ERROR | ||
| 343 | + ) | ||
| 344 | + if (${EXEC_RESULT}) | ||
| 345 | + message(FATAL_ERROR, "copy_source_files failed, gen error:${EXEC_ERROR}" ) | ||
| 346 | + endif() | ||
| 347 | + endif() | ||
| 348 | + | ||
| 349 | + foreach(compute_unit ${ASCEND_COMPUTE_UNIT}) | ||
| 350 | + # generate aic-${compute_unit}-ops-info.json | ||
| 351 | + add_ops_info_target(TARGET ops_info_gen_${compute_unit} | ||
| 352 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tbe/op_info_cfg/ai_core/${compute_unit}/aic-${compute_unit}-ops-info.json | ||
| 353 | + OPS_INFO ${ASCEND_AUTOGEN_PATH}/aic-${compute_unit}-ops-info.ini | ||
| 354 | + INSTALL_DIR packages/vendors/${vendor_name}/op_impl/ai_core/tbe/config/${compute_unit} | ||
| 355 | + ) | ||
| 356 | + | ||
| 357 | + # define a target:binary to prevent kernel file from being rebuilt during the preinstall process | ||
| 358 | + if (NOT TARGET binary) | ||
| 359 | + add_custom_target(binary) | ||
| 360 | + endif() | ||
| 361 | + | ||
| 362 | + if (${ENABLE_BINARY_PACKAGE} OR ${ENABLE_SOURCE_PACKAGE}) | ||
| 363 | + if (${ENABLE_BINARY_PACKAGE}) | ||
| 364 | + # gen binary_info_config.json and <file_name>.json | ||
| 365 | + add_custom_target(ascendc_bin_${compute_unit}_gen_ops_config | ||
| 366 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/insert_simplified_keys.py | ||
| 367 | + -p ${CMAKE_CURRENT_BINARY_DIR}/binary/${compute_unit} | ||
| 368 | + COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_ops_config.py | ||
| 369 | + -p ${CMAKE_CURRENT_BINARY_DIR}/binary/${compute_unit} | ||
| 370 | + -s ${compute_unit} | ||
| 371 | + COMMAND ${CMAKE_COMMAND} -E make_directory | ||
| 372 | + ${CMAKE_CURRENT_BINARY_DIR}/binary/config/${compute_unit} | ||
| 373 | + COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/binary/${compute_unit}/*.json | ||
| 374 | + ${CMAKE_CURRENT_BINARY_DIR}/binary/config/${compute_unit} | ||
| 375 | + ) | ||
| 376 | + else() | ||
| 377 | + if (NOT TARGET ascendc_bin_${compute_unit}_gen_ops_config) | ||
| 378 | + add_custom_target(ascendc_bin_${compute_unit}_gen_ops_config) | ||
| 379 | + endif() | ||
| 380 | + endif() | ||
| 381 | + add_dependencies(binary ascendc_bin_${compute_unit}_gen_ops_config) | ||
| 382 | + | ||
| 383 | + # get op_type-op_name from aic-${compute_unit}-ops-info.ini | ||
| 384 | + execute_process(COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/util/ascendc_get_op_name.py | ||
| 385 | + --ini-file=${ASCEND_AUTOGEN_PATH}/aic-${compute_unit}-ops-info.ini | ||
| 386 | + OUTPUT_VARIABLE OP_TYPE_NAME | ||
| 387 | + RESULT_VARIABLE EXEC_RESULT | ||
| 388 | + ERROR_VARIABLE EXEC_ERROR | ||
| 389 | + ) | ||
| 390 | + if (${EXEC_RESULT}) | ||
| 391 | + message(FATAL_ERROR, "get op name failed, gen error: ${EXEC_ERROR}") | ||
| 392 | + endif() | ||
| 393 | + | ||
| 394 | + # compile op one by one with ascendc_compile_kernel.py | ||
| 395 | + string(REPLACE "\n" ";" TYPE_NAME_LIST "${OP_TYPE_NAME}") | ||
| 396 | + foreach(TYPE_NAME IN LISTS TYPE_NAME_LIST) | ||
| 397 | + if (NOT "${TYPE_NAME}" STREQUAL "") | ||
| 398 | + string(REPLACE "-" ";" bin_sep ${TYPE_NAME}) | ||
| 399 | + list(GET bin_sep 0 op_type) | ||
| 400 | + list(GET bin_sep 1 op_file) | ||
| 401 | + add_simple_kernel_compile(OP_TYPE ${op_type} | ||
| 402 | + SRC ${CMAKE_SOURCE_DIR}/op_kernel/${op_file}.cpp | ||
| 403 | + COMPUTE_UNIT ${compute_unit} | ||
| 404 | + JSON_FILE ${CMAKE_CURRENT_BINARY_DIR}/tbe/op_info_cfg/ai_core/${compute_unit}/aic-${compute_unit}-ops-info.json | ||
| 405 | + DYNAMIC_PATH ${DYNAMIC_PATH}) | ||
| 406 | + endif() | ||
| 407 | + endforeach() | ||
| 408 | + endif() | ||
| 409 | + endforeach() | ||
| 410 | + | ||
| 411 | + # generate npu_supported_ops.json | ||
| 412 | + add_npu_support_target(TARGET npu_supported_ops | ||
| 413 | + OPS_INFO_DIR ${ASCEND_AUTOGEN_PATH} | ||
| 414 | + OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/tbe/op_info_cfg/ai_core | ||
| 415 | + INSTALL_DIR packages/vendors/${vendor_name}/framework/${ASCEND_FRAMEWORK_TYPE} | ||
| 416 | + ) | ||
| 417 | + | ||
| 418 | + if(ENABLE_TEST AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testcases) | ||
| 419 | + add_subdirectory(testcases) | ||
| 420 | + endif() | ||
| 421 | + | ||
| 422 | + if(NOT ASCEND_PACK_SHARED_LIBRARY) | ||
| 423 | + add_kernels_install() | ||
| 424 | + else() | ||
| 425 | + add_opregistry_target() | ||
| 426 | + endif() | ||
| 427 | +endfunction() | ||
| @@ -0,0 +1,28 @@ | |||
| 1 | + | ||
| 2 | +add_library(intf_pub INTERFACE) | ||
| 3 | +target_compile_options(intf_pub INTERFACE | ||
| 4 | + -fPIC | ||
| 5 | + -fvisibility=hidden | ||
| 6 | + -fvisibility-inlines-hidden | ||
| 7 | + $<$<CONFIG:Release>:-O2> | ||
| 8 | + $<$<CONFIG:Debug>:-O0 -g> | ||
| 9 | + $<$<COMPILE_LANGUAGE:CXX>:-std=c++11> | ||
| 10 | + $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:Debug>>:-ftrapv -fstack-check> | ||
| 11 | + $<$<COMPILE_LANGUAGE:C>:-pthread -Wfloat-equal -Wshadow -Wformat=2 -Wno-deprecated -Wextra> | ||
| 12 | + $<IF:$<VERSION_GREATER:${CMAKE_C_COMPILER_VERSION},4.8.5>,-fstack-protector-strong,-fstack-protector-all> | ||
| 13 | +) | ||
| 14 | +target_compile_definitions(intf_pub INTERFACE | ||
| 15 | + _GLIBCXX_USE_CXX11_ABI=0 | ||
| 16 | + $<$<CONFIG:Release>:_FORTIFY_SOURCE=2> | ||
| 17 | +) | ||
| 18 | +target_include_directories(intf_pub INTERFACE ${ASCEND_CANN_PACKAGE_PATH}/include | ||
| 19 | + ${CMAKE_CURRENT_SOURCE_DIR}/op_kernel | ||
| 20 | +) | ||
| 21 | +target_link_options(intf_pub INTERFACE | ||
| 22 | + $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-pie> | ||
| 23 | + $<$<CONFIG:Release>:-s> | ||
| 24 | + -Wl,-z,relro | ||
| 25 | + -Wl,-z,now | ||
| 26 | + -Wl,-z,noexecstack | ||
| 27 | +) | ||
| 28 | +target_link_directories(intf_pub INTERFACE ${ASCEND_CANN_PACKAGE_PATH}/lib64) | ||
| @@ -0,0 +1,33 @@ | |||
| 1 | +execute_process(COMMAND bash ${CMAKE_CURRENT_LIST_DIR}/util/makeself/makeself.sh | ||
| 2 | + --header ${CMAKE_CURRENT_LIST_DIR}/util/makeself/makeself-header.sh | ||
| 3 | + --help-header ./help.info --tar-format posix | ||
| 4 | + --gzip --complevel 4 --nomd5 --sha256 | ||
| 5 | + ./ ${CPACK_PACKAGE_FILE_NAME} "version:1.0" ./install.sh | ||
| 6 | + WORKING_DIRECTORY ${CPACK_TEMPORARY_DIRECTORY} | ||
| 7 | + RESULT_VARIABLE EXEC_RESULT | ||
| 8 | + ERROR_VARIABLE EXEC_ERROR | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +if (NOT "${EXEC_RESULT}x" STREQUAL "0x") | ||
| 12 | + message(FATAL_ERROR "CPack Command error: ${EXEC_RESULT}\n${EXEC_ERROR}") | ||
| 13 | +endif() | ||
| 14 | + | ||
| 15 | +execute_process(COMMAND cp ${CPACK_EXTERNAL_BUILT_PACKAGES} ${CPACK_PACKAGE_DIRECTORY}/ | ||
| 16 | + COMMAND echo "Copy ${CPACK_EXTERNAL_BUILT_PACKAGES} to ${CPACK_PACKAGE_DIRECTORY}/" | ||
| 17 | + WORKING_DIRECTORY ${CPACK_TEMPORARY_DIRECTORY} | ||
| 18 | + ) | ||
| 19 | + | ||
| 20 | +if (NOT "${CPACK_PACKAGE_DIRECTORY}x" STREQUAL "${CPACK_INSTALL_PREFIX}x") | ||
| 21 | + execute_process( | ||
| 22 | + COMMAND ${CMAKE_COMMAND} -E make_directory ${CPACK_INSTALL_PREFIX} | ||
| 23 | + WORKING_DIRECTORY ${CPACK_TEMPORARY_DIRECTORY} | ||
| 24 | + ) | ||
| 25 | + | ||
| 26 | + execute_process( | ||
| 27 | + COMMAND cp ${CPACK_EXTERNAL_BUILT_PACKAGES} ${CPACK_INSTALL_PREFIX}/ | ||
| 28 | + COMMAND echo "Copy ${CPACK_EXTERNAL_BUILT_PACKAGES} to ${CPACK_INSTALL_PREFIX}/" | ||
| 29 | + WORKING_DIRECTORY ${CPACK_TEMPORARY_DIRECTORY} | ||
| 30 | + ) | ||
| 31 | +endif() | ||
| 32 | + | ||
| 33 | + | ||
| @@ -0,0 +1,8 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | + | ||
| 4 | +import sys | ||
| 5 | +import os | ||
| 6 | + | ||
| 7 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 8 | +sys.path.append(PYF_PATH) | ||
| @@ -0,0 +1,548 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import argparse | ||
| 9 | +import sys | ||
| 10 | +import os | ||
| 11 | +import json | ||
| 12 | +import hashlib | ||
| 13 | +import re | ||
| 14 | +import copy | ||
| 15 | +from collections import defaultdict | ||
| 16 | +from typing import Dict, List, Set, Tuple, NamedTuple | ||
| 17 | + | ||
| 18 | +import const_var | ||
| 19 | +import opdesc_parser | ||
| 20 | + | ||
| 21 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +class ParamInfo(NamedTuple): | ||
| 25 | + dtype_list: list | ||
| 26 | + format_list: list | ||
| 27 | + dtype_for_bin_list: dict | ||
| 28 | + format_for_bin_list: dict | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +class BinParamBuilder(opdesc_parser.OpDesc): | ||
| 32 | + def __init__(self: any, op_type: str): | ||
| 33 | + super().__init__(op_type) | ||
| 34 | + self.soc = '' | ||
| 35 | + self.out_path = '' | ||
| 36 | + self.tiling_keys = set() | ||
| 37 | + self.op_debug_config = '' | ||
| 38 | + self.op_super_config = [] | ||
| 39 | + | ||
| 40 | + def set_soc_version(self: any, soc: str): | ||
| 41 | + self.soc = soc | ||
| 42 | + | ||
| 43 | + def set_out_path(self: any, out_path: str): | ||
| 44 | + self.out_path = out_path | ||
| 45 | + | ||
| 46 | + def set_tiling_key(self: any, tiling_key_info: Set): | ||
| 47 | + if tiling_key_info: | ||
| 48 | + self.tiling_keys.update(tiling_key_info) | ||
| 49 | + | ||
| 50 | + def set_op_debug_config(self: any, op_debug_config: str): | ||
| 51 | + if op_debug_config: | ||
| 52 | + self.op_debug_config = op_debug_config | ||
| 53 | + | ||
| 54 | + def set_op_super_config(self: any, op_super_config: str): | ||
| 55 | + if op_super_config: | ||
| 56 | + self.op_super_config = op_super_config | ||
| 57 | + | ||
| 58 | + def get_full_list(self: any): | ||
| 59 | + dtype_list = [] | ||
| 60 | + for dtype_in in self.input_dtype: | ||
| 61 | + dtype_list.append(dtype_in.split(',')) | ||
| 62 | + for dtype_out in self.output_dtype: | ||
| 63 | + dtype_list.append(dtype_out.split(',')) | ||
| 64 | + | ||
| 65 | + format_list = [] | ||
| 66 | + for fmt_in in self.input_fmt: | ||
| 67 | + format_list.append(fmt_in.split(',')) | ||
| 68 | + for fmt_out in self.output_fmt: | ||
| 69 | + format_list.append(fmt_out.split(',')) | ||
| 70 | + | ||
| 71 | + dtype_for_bin_list = [[] for _ in range(len(self.input_dtype) + len(self.output_dtype))] | ||
| 72 | + format_for_bin_list = copy.deepcopy(dtype_for_bin_list) | ||
| 73 | + | ||
| 74 | + for key, value in self.input_dtype_for_bin.items(): | ||
| 75 | + dtype_for_bin_list[key] = value.split(',') | ||
| 76 | + for key, value in self.output_dtype_for_bin.items(): | ||
| 77 | + dtype_for_bin_list[key + len(self.input_dtype)] = value.split(',') | ||
| 78 | + for key, value in self.input_fmt_for_bin.items(): | ||
| 79 | + format_for_bin_list[key] = value.split(',') | ||
| 80 | + for key, value in self.output_fmt_for_bin.items(): | ||
| 81 | + format_for_bin_list[key + len(self.input_dtype)] = value.split(',') | ||
| 82 | + | ||
| 83 | + return ParamInfo(dtype_list, format_list, dtype_for_bin_list, format_for_bin_list) | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + def gen_bin_cprs_list(self: any, param_info: ParamInfo): | ||
| 87 | + combine_dict = {} | ||
| 88 | + origin_combine_dict = {} | ||
| 89 | + for cob_idx in range(0, len(self.input_dtype[0].split(','))): | ||
| 90 | + origin_combine = "" | ||
| 91 | + combine = "" | ||
| 92 | + for param_idx in range(0, len(self.input_dtype) + len(self.output_dtype)): | ||
| 93 | + if (param_info.dtype_for_bin_list[param_idx]): | ||
| 94 | + combine += param_info.dtype_for_bin_list[param_idx][cob_idx] | ||
| 95 | + else: | ||
| 96 | + combine += param_info.dtype_list[param_idx][cob_idx] | ||
| 97 | + origin_combine += param_info.dtype_list[param_idx][cob_idx] | ||
| 98 | + if (param_info.format_for_bin_list[param_idx]): | ||
| 99 | + combine += param_info.format_for_bin_list[param_idx][cob_idx] | ||
| 100 | + else: | ||
| 101 | + combine += param_info.format_list[param_idx][cob_idx] | ||
| 102 | + origin_combine += param_info.format_list[param_idx][cob_idx] | ||
| 103 | + if (combine not in combine_dict): | ||
| 104 | + combine_dict[combine] = [] | ||
| 105 | + combine_dict[combine].append(cob_idx) | ||
| 106 | + origin_combine_dict[origin_combine] = cob_idx | ||
| 107 | + for key, value in combine_dict.items(): | ||
| 108 | + if (key not in origin_combine_dict): | ||
| 109 | + print(f"WARNING: ForBinQuery {key} not in origin combine") | ||
| 110 | + self.bin_save_list += value | ||
| 111 | + continue | ||
| 112 | + if len(value) == 1 and value[0] == origin_combine_dict[key]: | ||
| 113 | + self.bin_save_list += value | ||
| 114 | + continue | ||
| 115 | + self.bin_cprs_head.append(origin_combine_dict[key]) | ||
| 116 | + self.bin_cprs_list.append(value) | ||
| 117 | + for index, sub_list in enumerate(self.bin_cprs_list): | ||
| 118 | + if self.bin_cprs_head[index] not in self.bin_save_list: | ||
| 119 | + continue | ||
| 120 | + sub_list.append(self.bin_cprs_head[index]) | ||
| 121 | + self.bin_save_list += self.bin_cprs_head | ||
| 122 | + | ||
| 123 | + | ||
| 124 | + def gen_for_bin_list(self: any, param_info: ParamInfo): | ||
| 125 | + combine_size = len(self.input_dtype[0].split(',')) | ||
| 126 | + input_size = len(self.input_dtype) | ||
| 127 | + output_size = len(self.output_dtype) | ||
| 128 | + | ||
| 129 | + self.input_dtype_for_bin_list = [[] for _ in range(input_size)] | ||
| 130 | + self.output_dtype_for_bin_list = [[] for _ in range(output_size)] | ||
| 131 | + for i in range(0, input_size): | ||
| 132 | + self.input_dtype_for_bin_list[i] = [[] for _ in range(combine_size)] | ||
| 133 | + for i in range(0, output_size): | ||
| 134 | + self.output_dtype_for_bin_list[i] = [[] for _ in range(combine_size)] | ||
| 135 | + self.input_fmt_for_bin_list = copy.deepcopy(self.input_dtype_for_bin_list) | ||
| 136 | + self.output_fmt_for_bin_list = copy.deepcopy(self.output_dtype_for_bin_list) | ||
| 137 | + | ||
| 138 | + for index, sub_list in enumerate(self.bin_cprs_list): | ||
| 139 | + head_idx = self.bin_cprs_head[index] | ||
| 140 | + for cmb_idx in sub_list: | ||
| 141 | + for i in range(0, input_size): | ||
| 142 | + self.input_dtype_for_bin_list[i][head_idx].append(param_info.dtype_list[i][cmb_idx]) | ||
| 143 | + self.input_fmt_for_bin_list[i][head_idx].append(param_info.format_list[i][cmb_idx]) | ||
| 144 | + for i in range(0, output_size): | ||
| 145 | + self.output_dtype_for_bin_list[i][head_idx].append(param_info.dtype_list[i + input_size][cmb_idx]) | ||
| 146 | + self.output_fmt_for_bin_list[i][head_idx].append(param_info.format_list[i + input_size][cmb_idx]) | ||
| 147 | + | ||
| 148 | + | ||
| 149 | + def rm_cprs_cmb(self: any, dtype_list, format_list, input_size, output_size): | ||
| 150 | + for i in range(0, input_size): | ||
| 151 | + self.input_dtype_for_bin_list[i] = [ | ||
| 152 | + element for index, element in enumerate(self.input_dtype_for_bin_list[i]) | ||
| 153 | + if index in self.bin_save_list | ||
| 154 | + ] | ||
| 155 | + self.input_fmt_for_bin_list[i] = [ | ||
| 156 | + element for index, element in enumerate(self.input_fmt_for_bin_list[i]) | ||
| 157 | + if index in self.bin_save_list | ||
| 158 | + ] | ||
| 159 | + new_dtype_list = [ | ||
| 160 | + element for index, element in enumerate(dtype_list[i]) | ||
| 161 | + if index in self.bin_save_list | ||
| 162 | + ] | ||
| 163 | + new_dtype_str = "" | ||
| 164 | + for dtype in new_dtype_list: | ||
| 165 | + new_dtype_str += f"{dtype}," | ||
| 166 | + self.input_dtype[i] = new_dtype_str[:-1] | ||
| 167 | + new_format_list = [ | ||
| 168 | + element for index, element in enumerate(format_list[i]) | ||
| 169 | + if index in self.bin_save_list | ||
| 170 | + ] | ||
| 171 | + new_format_str = "" | ||
| 172 | + for fmt in new_format_list: | ||
| 173 | + new_format_str += f"{fmt}," | ||
| 174 | + self.input_fmt[i] = new_format_str[:-1] | ||
| 175 | + for i in range(0, output_size): | ||
| 176 | + self.output_dtype_for_bin_list[i] = [ | ||
| 177 | + element for index, element in enumerate(self.output_dtype_for_bin_list[i]) | ||
| 178 | + if index in self.bin_save_list | ||
| 179 | + ] | ||
| 180 | + self.output_fmt_for_bin_list[i] = [ | ||
| 181 | + element for index, element in enumerate(self.output_fmt_for_bin_list[i]) | ||
| 182 | + if index in self.bin_save_list | ||
| 183 | + ] | ||
| 184 | + new_dtype_list = [ | ||
| 185 | + element for index, element in enumerate(dtype_list[i + input_size]) | ||
| 186 | + if index in self.bin_save_list | ||
| 187 | + ] | ||
| 188 | + new_dtype_str = "" | ||
| 189 | + for dtype in new_dtype_list: | ||
| 190 | + new_dtype_str += f"{dtype}," | ||
| 191 | + self.output_dtype[i] = new_dtype_str[:-1] | ||
| 192 | + new_format_list = [ | ||
| 193 | + element for index, element in enumerate(format_list[i + input_size]) | ||
| 194 | + if index in self.bin_save_list | ||
| 195 | + ] | ||
| 196 | + new_format_str = "" | ||
| 197 | + for fmt in new_format_list: | ||
| 198 | + new_format_str += f"{fmt}," | ||
| 199 | + self.output_fmt[i] = new_format_str[:-1] | ||
| 200 | + | ||
| 201 | + | ||
| 202 | + def is_set_for_bin_query(self: any): | ||
| 203 | + return any([ | ||
| 204 | + self.input_dtype_for_bin, | ||
| 205 | + self.output_dtype_for_bin, | ||
| 206 | + self.input_fmt_for_bin, | ||
| 207 | + self.output_fmt_for_bin, | ||
| 208 | + ]) | ||
| 209 | + | ||
| 210 | + | ||
| 211 | + def for_bin_list_match(self: any): | ||
| 212 | + if not self.is_set_for_bin_query(): | ||
| 213 | + return | ||
| 214 | + input_size = len(self.input_dtype) | ||
| 215 | + output_size = len(self.output_dtype) | ||
| 216 | + param_info = self.get_full_list() | ||
| 217 | + self.gen_bin_cprs_list(param_info) | ||
| 218 | + self.gen_for_bin_list(param_info) | ||
| 219 | + if len(self.bin_save_list) == len(self.input_dtype[0].split(',')): | ||
| 220 | + print(f'WARNING: ForBinQuery can not compress number of bin file with this set, please check!!.') | ||
| 221 | + return | ||
| 222 | + self.rm_cprs_cmb(param_info.dtype_list, param_info.format_list, input_size, output_size) | ||
| 223 | + | ||
| 224 | + | ||
| 225 | + def gen_input_json(self: any, auto_gen_path: str): | ||
| 226 | + key_map = {} | ||
| 227 | + self.for_bin_list_match() | ||
| 228 | + if len(self.input_dtype) == 0: | ||
| 229 | + count = len(self.output_dtype[0].split(',')) | ||
| 230 | + else: | ||
| 231 | + count = len(self.input_dtype[0].split(',')) | ||
| 232 | + if count == 0: | ||
| 233 | + raise RuntimeError(f'Op {self.op_type} must have at least one input or output') | ||
| 234 | + required_parameters = set() | ||
| 235 | + index_value = -1 | ||
| 236 | + | ||
| 237 | + for i in range(0, count): | ||
| 238 | + inputs = [] | ||
| 239 | + outputs = [] | ||
| 240 | + attrs = [] | ||
| 241 | + required_parameter = [] | ||
| 242 | + op_node = {} | ||
| 243 | + | ||
| 244 | + for idx in range(0, len(self.input_name)): | ||
| 245 | + idtypes = self.input_dtype[idx].split(',') | ||
| 246 | + ifmts = self.input_fmt[idx].split(',') | ||
| 247 | + itype = self.input_type[idx] | ||
| 248 | + para = {} | ||
| 249 | + para['name'] = self.input_name[idx][:-5] | ||
| 250 | + para['index'] = idx | ||
| 251 | + para['dtype'] = idtypes[i] | ||
| 252 | + if self.is_set_for_bin_query() and self.input_dtype_for_bin_list[idx][i]: | ||
| 253 | + para['dtypeForBinQuery'] = self.input_dtype_for_bin_list[idx][i] | ||
| 254 | + para['format'] = ifmts[i] | ||
| 255 | + if self.is_set_for_bin_query() and self.input_fmt_for_bin_list[idx][i]: | ||
| 256 | + para['formatForBinQuery'] = self.input_fmt_for_bin_list[idx][i] | ||
| 257 | + para['paramType'] = itype | ||
| 258 | + para['shape'] = [-2] | ||
| 259 | + para['format_match_mode'] = 'FormatAgnostic' | ||
| 260 | + | ||
| 261 | + input_parameter_key = (idtypes[i], ifmts[i]) | ||
| 262 | + if itype == 'dynamic': | ||
| 263 | + inputs.append([para]) | ||
| 264 | + required_parameter.append(input_parameter_key) | ||
| 265 | + elif itype == 'required': | ||
| 266 | + inputs.append(para) | ||
| 267 | + required_parameter.append(input_parameter_key) | ||
| 268 | + else: | ||
| 269 | + inputs.append(para) | ||
| 270 | + | ||
| 271 | + for idx in range(0, len(self.output_name)): | ||
| 272 | + odtypes = self.output_dtype[idx].split(',') | ||
| 273 | + ofmts = self.output_fmt[idx].split(',') | ||
| 274 | + otype = self.output_type[idx] | ||
| 275 | + para = {} | ||
| 276 | + para['name'] = self.output_name[idx][:-5] | ||
| 277 | + para['index'] = idx | ||
| 278 | + para['dtype'] = odtypes[i] | ||
| 279 | + if self.is_set_for_bin_query() and self.output_dtype_for_bin_list[idx][i]: | ||
| 280 | + para['dtypeForBinQuery'] = self.output_dtype_for_bin_list[idx][i] | ||
| 281 | + para['format'] = ofmts[i] | ||
| 282 | + if self.is_set_for_bin_query() and self.output_fmt_for_bin_list[idx][i]: | ||
| 283 | + para['formatForBinQuery'] = self.output_fmt_for_bin_list[idx][i] | ||
| 284 | + para['paramType'] = otype | ||
| 285 | + para['shape'] = [-2] | ||
| 286 | + para['format_match_mode'] = 'FormatAgnostic' | ||
| 287 | + output_parameter_key = (odtypes[i], ofmts[i]) | ||
| 288 | + if otype == 'dynamic': | ||
| 289 | + outputs.append([para]) | ||
| 290 | + required_parameter.append(output_parameter_key) | ||
| 291 | + elif otype == 'required': | ||
| 292 | + outputs.append(para) | ||
| 293 | + required_parameter.append(output_parameter_key) | ||
| 294 | + else: | ||
| 295 | + outputs.append(para) | ||
| 296 | + | ||
| 297 | + for attr in self.attr_list: | ||
| 298 | + att = {} | ||
| 299 | + att['name'] = attr | ||
| 300 | + atype = self.attr_val.get(attr).get('type').lower() | ||
| 301 | + att['dtype'] = atype | ||
| 302 | + att['value'] = const_var.ATTR_DEF_VAL.get(atype) | ||
| 303 | + attrs.append(att) | ||
| 304 | + | ||
| 305 | + required_parameter_tuple = tuple(required_parameter) | ||
| 306 | + if required_parameter_tuple in required_parameters: | ||
| 307 | + continue | ||
| 308 | + else: | ||
| 309 | + required_parameters.add(required_parameter_tuple) | ||
| 310 | + index_value +=1 | ||
| 311 | + | ||
| 312 | + op_node['bin_filename'] = '' | ||
| 313 | + op_node['inputs'] = inputs | ||
| 314 | + op_node['outputs'] = outputs | ||
| 315 | + if len(attrs) > 0: | ||
| 316 | + op_node['attrs'] = attrs | ||
| 317 | + | ||
| 318 | + param = {} | ||
| 319 | + param['op_type'] = self.op_type | ||
| 320 | + param['op_list'] = [op_node] | ||
| 321 | + objstr = json.dumps(param, indent=' ') | ||
| 322 | + md5sum = hashlib.md5(objstr.encode('utf-8')).hexdigest() | ||
| 323 | + while key_map.get(md5sum) is not None: | ||
| 324 | + objstr += '1' | ||
| 325 | + md5sum = hashlib.md5(objstr.encode('utf-8')).hexdigest() | ||
| 326 | + key_map[md5sum] = md5sum | ||
| 327 | + bin_file = self.op_type + '_' + md5sum | ||
| 328 | + op_node['bin_filename'] = bin_file | ||
| 329 | + param_file = os.path.join(self.out_path, bin_file + '_param.json') | ||
| 330 | + param_file = os.path.realpath(param_file) | ||
| 331 | + | ||
| 332 | + self._write_build_json(param_file, param) | ||
| 333 | + self._write_build_cmd(param_file, bin_file, index_value, auto_gen_path) | ||
| 334 | + if self.op_super_config: | ||
| 335 | + bin_file += "_relocatable" | ||
| 336 | + op_node['bin_filename'] = bin_file | ||
| 337 | + param_file = os.path.join(self.out_path, bin_file + '_param.json') | ||
| 338 | + param_file = os.path.realpath(param_file) | ||
| 339 | + self._write_build_json(param_file, param) | ||
| 340 | + index_value += 1 | ||
| 341 | + self._write_build_cmd(param_file, bin_file, index_value, auto_gen_path, True) | ||
| 342 | + | ||
| 343 | + def _write_build_json(self: any, param_file: str, param): | ||
| 344 | + with os.fdopen(os.open(param_file, const_var.WFLAGS, const_var.WMODES), 'w') as fd: | ||
| 345 | + json.dump(param, fd, indent=' ') | ||
| 346 | + | ||
| 347 | + def _generate_check_result(self: any, enable_tiling_keys: bool, bin_file: str): | ||
| 348 | + check_result = "" | ||
| 349 | + if enable_tiling_keys is False: | ||
| 350 | + check_result += "echo \"${res}\"\n" | ||
| 351 | + check_result += const_var.CHK_CMD.format(res_file=bin_file + '.json') | ||
| 352 | + check_result += const_var.CHK_CMD.format(res_file=bin_file + '.o') | ||
| 353 | + else: | ||
| 354 | + check_result += "if [ $? -eq 1 ]; then\n" | ||
| 355 | + check_result += " if echo \"${res}\" | \ | ||
| 356 | +grep -q \"None of the given tiling keys are in the supported list\"; then\n" | ||
| 357 | + check_result += " echo \"${res}\"\n" | ||
| 358 | + check_result += " else\n" | ||
| 359 | + check_result += " echo \"${res}\"\n" | ||
| 360 | + check_result += " exit 1\n" | ||
| 361 | + check_result += " fi\n" | ||
| 362 | + check_result += "else\n" | ||
| 363 | + check_result += "echo \"${res}\"\n" | ||
| 364 | + check_result += const_var.CHK_CMD.format(res_file=bin_file + '.json') | ||
| 365 | + check_result += const_var.CHK_CMD.format(res_file=bin_file + '.o') | ||
| 366 | + check_result += "fi\n" | ||
| 367 | + return check_result | ||
| 368 | + | ||
| 369 | + def _write_build_cmd(self: any, param_file: str, bin_file: str, index: int, auto_gen_path: str, super_mode=False): | ||
| 370 | + hard_soc = const_var.conv_soc_ver(self.soc) | ||
| 371 | + if not hard_soc: | ||
| 372 | + hard_soc = self.soc.capitalize() | ||
| 373 | + name_com = [self.op_type, self.op_file, str(index)] | ||
| 374 | + compile_file = os.path.join(self.out_path, '-'.join(name_com) + '.sh') | ||
| 375 | + compile_file = os.path.realpath(compile_file) | ||
| 376 | + | ||
| 377 | + bin_cmd_str = 'res=$(opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \ | ||
| 378 | + --output=$2 --impl_mode={impl} --simplified_key_mode=0 --op_mode=dynamic ' | ||
| 379 | + | ||
| 380 | + build_cmd_var = "#!/bin/bash\n" | ||
| 381 | + build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} ..."\n' | ||
| 382 | + plog_level = os.environ.get("ASCEND_GLOBAL_LOG_LEVEL") | ||
| 383 | + plog_stdout = os.environ.get("ASCEND_SLOG_PRINT_TO_STDOUT") | ||
| 384 | + if plog_level is None: | ||
| 385 | + build_cmd_var += const_var.SET_PLOG_LEVEL_ERROR | ||
| 386 | + if plog_stdout is None: | ||
| 387 | + build_cmd_var += const_var.SET_PLOG_STDOUT | ||
| 388 | + build_cmd_var += const_var.SRC_ENV | ||
| 389 | + if hard_soc == "Ascend610Lite": | ||
| 390 | + build_cmd_var += f'export ASCEND_CUSTOM_OPP_PATH={auto_gen_path}:$ASCEND_CUSTOM_OPP_PATH \n' | ||
| 391 | + build_cmd_var += bin_cmd_str.format(fun=self.op_intf, soc=hard_soc, param=param_file, | ||
| 392 | + impl='high_performance,optional') | ||
| 393 | + enable_tiling_keys = False | ||
| 394 | + if self.tiling_keys: | ||
| 395 | + tiling_keys_list = sorted(list(self.tiling_keys)) | ||
| 396 | + tiling_key_str = ','.join([str(_key) for _key in tiling_keys_list]) | ||
| 397 | + build_cmd_var += f' --tiling_key="{tiling_key_str}"' | ||
| 398 | + enable_tiling_keys = True | ||
| 399 | + | ||
| 400 | + if self.op_debug_config: | ||
| 401 | + op_debug_str = ','.join([str(_key) for _key in list(self.op_debug_config)]) | ||
| 402 | + build_cmd_var += f' --op_debug_config={op_debug_str}' | ||
| 403 | + | ||
| 404 | + if super_mode and self.op_super_config: | ||
| 405 | + op_super_config_str = ' '.join([str(_key) for _key in list(self.op_super_config)]) | ||
| 406 | + build_cmd_var += f' {op_super_config_str}' | ||
| 407 | + | ||
| 408 | + build_cmd_var += ")\n" | ||
| 409 | + build_cmd_var += "\n" | ||
| 410 | + | ||
| 411 | + check_result = self._generate_check_result(enable_tiling_keys, bin_file) | ||
| 412 | + build_cmd_var += check_result | ||
| 413 | + build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} Done"\n' | ||
| 414 | + | ||
| 415 | + with os.fdopen(os.open(compile_file, const_var.WFLAGS, const_var.WMODES), 'w') as fd: | ||
| 416 | + fd.write(build_cmd_var) | ||
| 417 | + | ||
| 418 | + | ||
| 419 | +def get_tiling_keys(tiling_keys: str) -> Set: | ||
| 420 | + all_tiling_keys = set() | ||
| 421 | + if not tiling_keys: | ||
| 422 | + return all_tiling_keys | ||
| 423 | + | ||
| 424 | + tiling_key_list = tiling_keys.split(';') | ||
| 425 | + for tiling_key_value in tiling_key_list: | ||
| 426 | + pattern = r"(?<![^\s])(\d+)-(\d+)(?![^\s])" | ||
| 427 | + results = re.findall(pattern, tiling_key_value) | ||
| 428 | + if results: | ||
| 429 | + start, end = results[0] | ||
| 430 | + if int(start) > int(end): | ||
| 431 | + continue | ||
| 432 | + for i in range(int(start), int(end) + 1): | ||
| 433 | + all_tiling_keys.add(i) | ||
| 434 | + elif tiling_key_value.isdigit(): | ||
| 435 | + all_tiling_keys.add(int(tiling_key_value)) | ||
| 436 | + return all_tiling_keys | ||
| 437 | + | ||
| 438 | + | ||
| 439 | +def trans_soc_verion(soc_ver: str): | ||
| 440 | + low_soc_ver = soc_ver.lower() | ||
| 441 | + if low_soc_ver not in opdesc_parser.SOC_TO_SHORT_SOC_MAP: | ||
| 442 | + return low_soc_ver | ||
| 443 | + return opdesc_parser.SOC_TO_SHORT_SOC_MAP[low_soc_ver] | ||
| 444 | + | ||
| 445 | + | ||
| 446 | +def parse_op_debug_confg(opc_config_file: str, soc: str) -> Dict: | ||
| 447 | + tiling_key_info = defaultdict(set) | ||
| 448 | + op_debug_config = defaultdict(set) | ||
| 449 | + if not opc_config_file: | ||
| 450 | + return tiling_key_info, op_debug_config | ||
| 451 | + | ||
| 452 | + if not os.path.exists(opc_config_file): | ||
| 453 | + return tiling_key_info, op_debug_config | ||
| 454 | + | ||
| 455 | + with open(opc_config_file, 'r') as file: | ||
| 456 | + contents = file.readlines() | ||
| 457 | + | ||
| 458 | + for _content in contents: | ||
| 459 | + content = _content.strip() | ||
| 460 | + opc_configs = content.split('@') | ||
| 461 | + if len(opc_configs) < 3: | ||
| 462 | + continue | ||
| 463 | + | ||
| 464 | + op_type = opc_configs[0] | ||
| 465 | + if not op_type: | ||
| 466 | + continue | ||
| 467 | + | ||
| 468 | + compute_unit = opc_configs[1] | ||
| 469 | + if compute_unit: | ||
| 470 | + compute_unit_list = compute_unit.split(';') | ||
| 471 | + soc_lists = [] | ||
| 472 | + for soc_ver in compute_unit_list: | ||
| 473 | + short_soc_ver = trans_soc_verion(soc_ver) | ||
| 474 | + soc_lists.append(short_soc_ver) | ||
| 475 | + if soc not in soc_lists: | ||
| 476 | + continue | ||
| 477 | + | ||
| 478 | + for options in opc_configs[2:]: | ||
| 479 | + if "--tiling_key" in options: | ||
| 480 | + format_tiling_keys = get_tiling_keys(options.split('=')[1]) | ||
| 481 | + if format_tiling_keys: | ||
| 482 | + tiling_key_info[op_type].update(format_tiling_keys) | ||
| 483 | + if "--op_debug_config" in options: | ||
| 484 | + first_index = options.find('=') | ||
| 485 | + if first_index != -1: | ||
| 486 | + debug_config = options[first_index + 1:] | ||
| 487 | + else: | ||
| 488 | + debug_config = "" | ||
| 489 | + | ||
| 490 | + format_debug_config = set(debug_config.split(';')) | ||
| 491 | + for _config in format_debug_config: | ||
| 492 | + op_debug_config[op_type].add(_config) | ||
| 493 | + return tiling_key_info, op_debug_config | ||
| 494 | + | ||
| 495 | + | ||
| 496 | +def gen_bin_param_file(cfgfile: str, out_dir: str, soc: str, | ||
| 497 | + opc_config_file: str = '', ops: list = None): | ||
| 498 | + if not os.path.exists(cfgfile): | ||
| 499 | + print(f'INFO: {cfgfile} does not exists in this project, skip generating compile commands.') | ||
| 500 | + return | ||
| 501 | + | ||
| 502 | + debug_config = defaultdict(set) | ||
| 503 | + super_config = defaultdict(set) | ||
| 504 | + | ||
| 505 | + op_descs = opdesc_parser.get_op_desc(cfgfile, [], [], BinParamBuilder, ops) | ||
| 506 | + tiling_key_info, op_debug_config = parse_op_debug_confg(opc_config_file, soc) | ||
| 507 | + for _op_type, _op_option in op_debug_config.items(): | ||
| 508 | + for _option in _op_option: | ||
| 509 | + if (_option.startswith("--op_relocatable_kernel_binary") | ||
| 510 | + or _option.startswith("--op_super_kernel_options")): | ||
| 511 | + super_config[_op_type].add(_option) | ||
| 512 | + else: | ||
| 513 | + debug_config[_op_type].add(_option) | ||
| 514 | + | ||
| 515 | + auto_gen_path_dir = os.path.dirname(cfgfile) | ||
| 516 | + all_soc_key = "ALL" | ||
| 517 | + for op_desc in op_descs: | ||
| 518 | + op_desc.set_soc_version(soc) | ||
| 519 | + op_desc.set_out_path(out_dir) | ||
| 520 | + if op_desc.op_type in debug_config: | ||
| 521 | + op_desc.set_op_debug_config(debug_config[op_desc.op_type]) | ||
| 522 | + if all_soc_key in debug_config: | ||
| 523 | + op_desc.set_op_debug_config(debug_config[all_soc_key]) | ||
| 524 | + if op_desc.op_type in super_config: | ||
| 525 | + op_desc.set_op_super_config(super_config[op_desc.op_type]) | ||
| 526 | + if op_desc.op_type in tiling_key_info: | ||
| 527 | + op_desc.set_tiling_key(tiling_key_info[op_desc.op_type]) | ||
| 528 | + if all_soc_key in tiling_key_info: | ||
| 529 | + op_desc.set_tiling_key(tiling_key_info[all_soc_key]) | ||
| 530 | + op_desc.gen_input_json(auto_gen_path_dir) | ||
| 531 | + | ||
| 532 | + | ||
| 533 | +def parse_args(argv): | ||
| 534 | + """Command line parameter parsing""" | ||
| 535 | + parser = argparse.ArgumentParser() | ||
| 536 | + parser.add_argument('argv', nargs='+') | ||
| 537 | + parser.add_argument('--opc-config-file', nargs='?', const='', default='') | ||
| 538 | + return parser.parse_args(argv) | ||
| 539 | + | ||
| 540 | + | ||
| 541 | +if __name__ == '__main__': | ||
| 542 | + args = parse_args(sys.argv) | ||
| 543 | + if len(args.argv) <= 3: | ||
| 544 | + raise RuntimeError('arguments must greater than 3') | ||
| 545 | + gen_bin_param_file(args.argv[1], | ||
| 546 | + args.argv[2], | ||
| 547 | + args.argv[3], | ||
| 548 | + opc_config_file=args.opc_config_file) | ||
| @@ -0,0 +1,220 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import sys | ||
| 8 | +import os | ||
| 9 | +import subprocess | ||
| 10 | +import time | ||
| 11 | +import glob | ||
| 12 | +import shutil | ||
| 13 | +import argparse | ||
| 14 | +import const_var | ||
| 15 | +import ascendc_impl_build | ||
| 16 | +import ascendc_bin_param_build | ||
| 17 | +import ascendc_op_info | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +class CompileKernel: | ||
| 21 | + def __init__(self: any, args: any): | ||
| 22 | + self.op_type = args.op_name | ||
| 23 | + self.op_cpp_file = os.path.realpath(args.src_file) | ||
| 24 | + self.op_soc_ver = args.compute_unit | ||
| 25 | + self.compile_options = args.compile_options | ||
| 26 | + self.op_debug_config = args.debug_config | ||
| 27 | + self.op_cfg_ini = os.path.realpath(args.config_ini) | ||
| 28 | + self.op_tiling = os.path.realpath(args.tiling_lib) | ||
| 29 | + self.op_output = os.path.realpath(args.output_path) | ||
| 30 | + self.op_impl_py = None | ||
| 31 | + self.compile_sh = [] | ||
| 32 | + self.working_dir = os.path.join( | ||
| 33 | + os.getcwd(), | ||
| 34 | + self.op_type + "_" + self.op_soc_ver, | ||
| 35 | + ) | ||
| 36 | + self.build_opp_path = os.path.join(self.working_dir, "customize") | ||
| 37 | + os.makedirs(self.working_dir) | ||
| 38 | + os.makedirs(self.op_output, exist_ok=True) | ||
| 39 | + if args.dynamic_dir is not None and args.dynamic_dir != "": | ||
| 40 | + self.dynamic_dir = os.path.realpath(args.dynamic_dir) | ||
| 41 | + else: | ||
| 42 | + self.dynamic_dir = None | ||
| 43 | + if args.json_file is not None and args.json_file != "": | ||
| 44 | + self.json_file = args.json_file | ||
| 45 | + else: | ||
| 46 | + self.json_file = None | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + def clean(self: any): | ||
| 50 | + if 'dump_cce' not in self.op_debug_config: | ||
| 51 | + shutil.rmtree(self.working_dir) | ||
| 52 | + return | ||
| 53 | + | ||
| 54 | + def ascendc_gen_impl(self: any): | ||
| 55 | + rep_cfg = {} | ||
| 56 | + rep_cfg[const_var.REPLAY_BATCH] = "" | ||
| 57 | + rep_cfg[const_var.REPLAY_ITERATE] = "" | ||
| 58 | + cfg_dir = {} | ||
| 59 | + cfg_dir[const_var.CFG_IMPL_DIR] = os.path.dirname(self.op_cpp_file) | ||
| 60 | + cfg_dir[const_var.CFG_OUT_DIR] = os.path.join(self.working_dir, "dynamic") | ||
| 61 | + os.makedirs(os.path.join(self.working_dir, "dynamic"), exist_ok=True) | ||
| 62 | + cfg_dir[const_var.AUTO_GEN_DIR] = os.path.dirname(self.op_cfg_ini) | ||
| 63 | + ascendc_impl_build.write_scripts( | ||
| 64 | + self.op_cfg_ini, rep_cfg, cfg_dir, [self.op_type], self.compile_options | ||
| 65 | + ) | ||
| 66 | + py_files = glob.glob(os.path.join(self.working_dir, "dynamic", "*.py")) | ||
| 67 | + if py_files is None or len(py_files) != 1: | ||
| 68 | + self.clean() | ||
| 69 | + raise RuntimeError("compile py file {} generated error!".format(py_files)) | ||
| 70 | + self.op_impl_py = os.path.join( | ||
| 71 | + self.working_dir, "dynamic", self.op_type + ".py" | ||
| 72 | + ) | ||
| 73 | + if self.dynamic_dir is not None: | ||
| 74 | + shutil.copy(py_files[0], self.dynamic_dir) | ||
| 75 | + os.rename(py_files[0], self.op_impl_py) | ||
| 76 | + if not os.path.exists(self.op_impl_py): | ||
| 77 | + self.clean() | ||
| 78 | + raise RuntimeError( | ||
| 79 | + "compile py file {} not generated!".format(self.op_impl_py) | ||
| 80 | + ) | ||
| 81 | + | ||
| 82 | + def ascendc_gen_param(self: any): | ||
| 83 | + bin_param_path = os.path.join(self.working_dir, "bin_param") | ||
| 84 | + os.makedirs(bin_param_path) | ||
| 85 | + base_dir = os.path.dirname(self.op_cfg_ini) | ||
| 86 | + opc_config_file = os.path.join(base_dir, "custom_opc_options.ini") | ||
| 87 | + ascendc_bin_param_build.gen_bin_param_file( | ||
| 88 | + self.op_cfg_ini, bin_param_path, self.op_soc_ver, opc_config_file, [self.op_type] | ||
| 89 | + ) | ||
| 90 | + tiling_key_info, op_debug_config = ascendc_bin_param_build.parse_op_debug_confg(opc_config_file, self.op_type) | ||
| 91 | + if self.op_type in op_debug_config: | ||
| 92 | + self.op_debug_config = op_debug_config[self.op_type] | ||
| 93 | + if "ALL" in op_debug_config: | ||
| 94 | + self.op_debug_config = op_debug_config["ALL"] | ||
| 95 | + bin_param_files = glob.glob(os.path.join(bin_param_path, "*.json")) | ||
| 96 | + if bin_param_files is None or len(bin_param_files) <= 0: | ||
| 97 | + self.clean() | ||
| 98 | + raise RuntimeError("compile binary param json file not generated!") | ||
| 99 | + self.compile_sh = glob.glob(os.path.join(bin_param_path, "*.sh")) | ||
| 100 | + if self.compile_sh is None or len(self.compile_sh) != len(bin_param_files): | ||
| 101 | + self.clean() | ||
| 102 | + raise RuntimeError("compile binary shell file not generated!") | ||
| 103 | + | ||
| 104 | + def ascendc_put_tiling(self: any): | ||
| 105 | + tiling_path = os.path.join( | ||
| 106 | + self.build_opp_path, "op_impl", "ai_core", "tbe", "op_tiling" | ||
| 107 | + ) | ||
| 108 | + os.makedirs(tiling_path) | ||
| 109 | + tiling_so = os.path.join(tiling_path, "liboptiling.so") | ||
| 110 | + os.symlink(self.op_tiling, tiling_so) | ||
| 111 | + if not os.path.exists(tiling_so): | ||
| 112 | + self.clean() | ||
| 113 | + raise RuntimeError("prepare tiling lib {} link failed!".format(tiling_so)) | ||
| 114 | + | ||
| 115 | + def ascendc_put_json(self: any): | ||
| 116 | + if self.json_file is not None: | ||
| 117 | + json_file_dir = os.path.join(self.build_opp_path, | ||
| 118 | + "op_impl", | ||
| 119 | + "ai_core", | ||
| 120 | + "tbe", | ||
| 121 | + "config", | ||
| 122 | + self.op_soc_ver) | ||
| 123 | + os.makedirs(json_file_dir) | ||
| 124 | + shutil.copy(self.json_file, json_file_dir) | ||
| 125 | + build_json_file = os.path.join(json_file_dir, "aic-{}-ops-info.json".format(self.op_soc_ver)) | ||
| 126 | + if not os.path.exists(build_json_file): | ||
| 127 | + self.clean() | ||
| 128 | + raise RuntimeError("prepare json file aic-{}-ops-info.json failed!".format(self.op_soc_ver)) | ||
| 129 | + | ||
| 130 | + def ascendc_build(self: any): | ||
| 131 | + op_info = ascendc_op_info.OpInfo(self.op_type, self.op_cfg_ini) | ||
| 132 | + op_file = op_info.get_op_file() | ||
| 133 | + op_bin_dir = os.path.join(self.op_output, self.op_soc_ver, op_file) | ||
| 134 | + os.makedirs(op_bin_dir, exist_ok=True) | ||
| 135 | + all_tar = [] | ||
| 136 | + sub_cmd = [] | ||
| 137 | + index = 0 | ||
| 138 | + for sh in self.compile_sh: | ||
| 139 | + tar = op_file + str(index) | ||
| 140 | + build_path = os.path.join(self.working_dir, "kernel_" + str(index)) | ||
| 141 | + os.makedirs(build_path) | ||
| 142 | + all_tar.append(tar) | ||
| 143 | + sub_cmd.append(tar + ":") | ||
| 144 | + sub_cmd.append( | ||
| 145 | + "\tcd {} && bash {} --kernel-src=$(CPP) $(PY) $(OUT) $(MAKE)".format( | ||
| 146 | + build_path, sh | ||
| 147 | + ) | ||
| 148 | + ) | ||
| 149 | + index += 1 | ||
| 150 | + mkfile = os.path.join(self.working_dir, op_file + ".make") | ||
| 151 | + with os.fdopen(os.open(mkfile, const_var.WFLAGS, const_var.WMODES), "w") as fd: | ||
| 152 | + sub_cmd.insert(0, "all: " + " ".join(all_tar)) | ||
| 153 | + fd.write("\n".join(sub_cmd)) | ||
| 154 | + | ||
| 155 | + if os.getenv("TILINGKEY_PAR_COMPILE") is None: | ||
| 156 | + cmd_str = ('export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && export TILINGKEY_PAR_COMPILE=1' | ||
| 157 | + '&& make -f {} PY={} OUT={} CPP={}') | ||
| 158 | + else: | ||
| 159 | + cmd_str = ('export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && make -f {} PY={} OUT={} CPP={}') | ||
| 160 | + | ||
| 161 | + if os.system(cmd_str.format(self.build_opp_path, mkfile, self.op_impl_py, op_bin_dir, self.op_cpp_file)) != 0: | ||
| 162 | + raise RuntimeError('Kernel Compilation Error: OpType {} Kernel File {}!'.format( | ||
| 163 | + self.op_type, self.op_cpp_file)) | ||
| 164 | + | ||
| 165 | + | ||
| 166 | +def args_parse(): | ||
| 167 | + parser = argparse.ArgumentParser() | ||
| 168 | + parser.add_argument( | ||
| 169 | + "-n", "--op-name", nargs="?", help="Op name(Camel string) to compile." | ||
| 170 | + ) | ||
| 171 | + parser.add_argument("-s", "--src-file", nargs="?", help="Op kernel source file.") | ||
| 172 | + | ||
| 173 | + parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.") | ||
| 174 | + parser.add_argument( | ||
| 175 | + "-c", "--compile-options", nargs="?", help="Compile options of compiler." | ||
| 176 | + ) | ||
| 177 | + parser.add_argument( | ||
| 178 | + "-d", | ||
| 179 | + "--debug-config", | ||
| 180 | + nargs="?", | ||
| 181 | + help="Debug config of op, ref opc op-debug-config.", | ||
| 182 | + ) | ||
| 183 | + parser.add_argument("-i", "--config-ini", nargs="?", help="Op config ini file.") | ||
| 184 | + parser.add_argument( | ||
| 185 | + "-t", "--tiling-lib", nargs="?", help="Tiling shared library file." | ||
| 186 | + ) | ||
| 187 | + | ||
| 188 | + parser.add_argument( | ||
| 189 | + "-o", "--output-path", nargs="?", help="Output path of compile result." | ||
| 190 | + ) | ||
| 191 | + parser.add_argument( | ||
| 192 | + "-dy", "--dynamic-dir", nargs="?", default=None, help="dynamic path of source compile." | ||
| 193 | + ) | ||
| 194 | + parser.add_argument( | ||
| 195 | + "-eb", "--enable-binary", nargs="?", default=None, help="whether binary compile is enabled." | ||
| 196 | + ) | ||
| 197 | + parser.add_argument( | ||
| 198 | + "-j", "--json-file", nargs="?", default=None, help="aic-<compute-unit>-ops-info.json file path." | ||
| 199 | + ) | ||
| 200 | + # $(MAKE) is necessary for parallel compiling | ||
| 201 | + parser.add_argument( | ||
| 202 | + "-b", "--build-tool", nargs="?", default=None, help="build tool must be make." | ||
| 203 | + ) | ||
| 204 | + return parser.parse_args() | ||
| 205 | + | ||
| 206 | + | ||
| 207 | +if __name__ == "__main__": | ||
| 208 | + args = args_parse() | ||
| 209 | + kernel_builder = CompileKernel(args) | ||
| 210 | + kernel_builder.clean() | ||
| 211 | + if args.enable_binary == "False": | ||
| 212 | + kernel_builder.ascendc_gen_impl() | ||
| 213 | + kernel_builder.clean() | ||
| 214 | + else: | ||
| 215 | + kernel_builder.ascendc_gen_impl() | ||
| 216 | + kernel_builder.ascendc_gen_param() | ||
| 217 | + kernel_builder.ascendc_put_json() | ||
| 218 | + kernel_builder.ascendc_put_tiling() | ||
| 219 | + kernel_builder.ascendc_build() | ||
| 220 | + kernel_builder.clean() | ||
| @@ -0,0 +1,90 @@ | |||
| 1 | +#!/usr/bin/python | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 4 | +# | ||
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | +# you may not use this file except in compliance with the License. | ||
| 7 | +# You may obtain a copy of the License at | ||
| 8 | +# | ||
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | +# | ||
| 11 | +# Unless required by applicable law or agreed to in writing, software | ||
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +# See the License for the specific language governing permissions and | ||
| 15 | +# limitations under the License. | ||
| 16 | +# ============================================================================ | ||
| 17 | + | ||
| 18 | +import sys | ||
| 19 | +import stat | ||
| 20 | +import os | ||
| 21 | +import re | ||
| 22 | +import json | ||
| 23 | +import const_var | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +def write_options_to_file(file_name: str, options_str: str, \ | ||
| 27 | + op_type: str, compute_unit: str, split_char: str): | ||
| 28 | + flags = os.O_WRONLY | os.O_CREAT | ||
| 29 | + modes = stat.S_IWUSR | stat.S_IRUSR | ||
| 30 | + try: | ||
| 31 | + with os.fdopen(os.open(file_name, flags, modes), 'a') as fd: | ||
| 32 | + fd.write(op_type + split_char + compute_unit + split_char + options_str + '\n') | ||
| 33 | + except Exception as err: | ||
| 34 | + print("write compile options config file failed") | ||
| 35 | + raise(err) | ||
| 36 | + | ||
| 37 | + | ||
| 38 | +def gen_compile_options(compile_options_file: str, op_type: str, \ | ||
| 39 | + compute_unit: str, compile_options: list): | ||
| 40 | + base_dir = os.path.dirname(compile_options_file) | ||
| 41 | + opc_config_file = os.path.join(base_dir, "custom_opc_options.ini") | ||
| 42 | + compile_opt = [] | ||
| 43 | + opc_debug_config = [] | ||
| 44 | + opc_tiling_keys = "" | ||
| 45 | + for opts in compile_options: | ||
| 46 | + if "oom" in opts: | ||
| 47 | + if opts == "--oom": | ||
| 48 | + opc_debug_config.append("oom") | ||
| 49 | + else: | ||
| 50 | + raise RuntimeError(f"Unknown oom option format {opts}") | ||
| 51 | + elif "--save-temp-files" in opts: | ||
| 52 | + opc_debug_config.append("dump_cce") | ||
| 53 | + elif opts.startswith("--op_relocatable_kernel_binary"): | ||
| 54 | + opc_debug_config.append(opts) | ||
| 55 | + elif opts.startswith("--op_super_kernel_options"): | ||
| 56 | + opc_debug_config.append(opts) | ||
| 57 | + elif "--tiling_key" in opts: | ||
| 58 | + keys = opts.strip().split('=')[1].split(',') | ||
| 59 | + keys_str = ";".join([key for key in keys]) | ||
| 60 | + opc_tiling_keys = keys_str | ||
| 61 | + else: | ||
| 62 | + compile_opt.append(opts) | ||
| 63 | + if len(compile_opt) > 0: | ||
| 64 | + options_str = ';'.join([opt for opt in compile_opt]) | ||
| 65 | + write_options_to_file(compile_options_file, options_str, op_type, compute_unit, ",") | ||
| 66 | + opc_config_str = "" | ||
| 67 | + if opc_debug_config: | ||
| 68 | + opc_config_str = "--op_debug_config=" + ';'.join([opt for opt in opc_debug_config]) | ||
| 69 | + if len(opc_tiling_keys) > 0: | ||
| 70 | + if opc_config_str != "": | ||
| 71 | + opc_config_str += "@" | ||
| 72 | + opc_config_str += "--tiling_key=" + opc_tiling_keys | ||
| 73 | + | ||
| 74 | + if opc_config_str != "": | ||
| 75 | + write_options_to_file(opc_config_file, opc_config_str, op_type, compute_unit, "@") | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +if __name__ == '__main__': | ||
| 79 | + if len(sys.argv) < 4: | ||
| 80 | + raise RuntimeError('arguments must greater than 4') | ||
| 81 | + compute_soc = "" | ||
| 82 | + comp_options = [] | ||
| 83 | + for i in range(len(sys.argv) - 3): | ||
| 84 | + if sys.argv[i + 3].upper().startswith("ASCEND"): | ||
| 85 | + compute_soc += sys.argv[i + 3] + ";" | ||
| 86 | + else: | ||
| 87 | + comp_options.append(sys.argv[i + 3]) | ||
| 88 | + if compute_soc != "": | ||
| 89 | + compute_soc = compute_soc[0:-1] | ||
| 90 | + gen_compile_options(sys.argv[1], sys.argv[2], compute_soc, comp_options) | ||
| @@ -0,0 +1,131 @@ | |||
| 1 | +""" | ||
| 2 | +#!/usr/bin/env python | ||
| 3 | +# -*- coding: UTF-8 -*- | ||
| 4 | +# Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import os | ||
| 8 | +import sys | ||
| 9 | +import glob | ||
| 10 | +import shutil | ||
| 11 | +import argparse | ||
| 12 | +import subprocess | ||
| 13 | +from tbe.tikcpp.log_utils import LogUtil, AscendCLogLevel | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +def run_command(args, **others): | ||
| 17 | + try: | ||
| 18 | + subprocess.run(args, check=True, **others) | ||
| 19 | + except subprocess.CalledProcessError as e: | ||
| 20 | + LogUtil.print_compile_log("", f"Command failed: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +def args_parse(): | ||
| 24 | + parser = argparse.ArgumentParser() | ||
| 25 | + parser.add_argument( | ||
| 26 | + "-f", "--object-files", help="Output files from host object targets, generated by cmake." | ||
| 27 | + ) | ||
| 28 | + | ||
| 29 | + parser.add_argument( | ||
| 30 | + "-k", "--kernel-libs", default=[], help="Output files from kernel libs." | ||
| 31 | + ) | ||
| 32 | + | ||
| 33 | + parser.add_argument( | ||
| 34 | + "-t", "--tmp-obj-dir", help="Temporary dir for combining object files to static library." | ||
| 35 | + ) | ||
| 36 | + | ||
| 37 | + parser.add_argument( | ||
| 38 | + "-o", "--output-file", help="Output static library of current customize operator project." | ||
| 39 | + ) | ||
| 40 | + | ||
| 41 | + parser.add_argument( | ||
| 42 | + "-r", "--remove-tmp-files", default="1", help="Whether to remove temporary files." | ||
| 43 | + ) | ||
| 44 | + return parser.parse_args() | ||
| 45 | + | ||
| 46 | + | ||
| 47 | +def get_object_list(obj_file, ori_object_list): | ||
| 48 | + with open(obj_file) as f: | ||
| 49 | + cur_obj_target = os.path.basename(obj_file) | ||
| 50 | + cur_obj_target = cur_obj_target[:cur_obj_target.find(".")] | ||
| 51 | + | ||
| 52 | + cur_obj_list = [] | ||
| 53 | + for line in f: | ||
| 54 | + cur_obj_path = line.strip("\n \r") | ||
| 55 | + if not os.path.exists(cur_obj_path): | ||
| 56 | + raise RuntimeError(f"object file {obj_real_path} doesn't exist.") | ||
| 57 | + cur_obj_list.append(cur_obj_path) | ||
| 58 | + ori_object_list[cur_obj_target] = cur_obj_list | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +def collect_object_from_files(object_files, tmp_obj_dir): | ||
| 62 | + object_files_parts = [x.strip() for x in object_files.split(" ") if x.strip() != ""] | ||
| 63 | + if len(object_files_parts) == 0: | ||
| 64 | + raise RuntimeError("-o/--object-files is empty, please check and reset.") | ||
| 65 | + | ||
| 66 | + if os.path.isfile(tmp_obj_dir): | ||
| 67 | + raise RuntimeError("-t/--tmp-obj-dir is an existing file, is must be a directory, please check and reset.") | ||
| 68 | + | ||
| 69 | + if os.path.exists(tmp_obj_dir): | ||
| 70 | + shutil.rmtree(tmp_obj_dir) | ||
| 71 | + | ||
| 72 | + os.makedirs(tmp_obj_dir, exist_ok=True) | ||
| 73 | + | ||
| 74 | + ori_object_list = {} | ||
| 75 | + for obj_file in object_files_parts: | ||
| 76 | + get_object_list(obj_file, ori_object_list) | ||
| 77 | + | ||
| 78 | + if len(ori_object_list) == 0: | ||
| 79 | + raise RuntimeError("object parsed from file is empty.") | ||
| 80 | + | ||
| 81 | + dst_object_list = [] | ||
| 82 | + for target, obj_list in ori_object_list.items(): | ||
| 83 | + for obj in obj_list: | ||
| 84 | + file_name = target + "_" + os.path.basename(obj) | ||
| 85 | + dst_file = os.path.join(tmp_obj_dir, file_name) | ||
| 86 | + shutil.copyfile(obj, dst_file) | ||
| 87 | + dst_object_list.append(file_name) | ||
| 88 | + | ||
| 89 | + return dst_object_list | ||
| 90 | + | ||
| 91 | + | ||
| 92 | +def unpack_kernel_library(kernel_libs, objects, tmp_obj_dir): | ||
| 93 | + kernel_libs_parts = kernel_libs.split(" ") | ||
| 94 | + for lib in kernel_libs_parts: | ||
| 95 | + output = subprocess.check_output(["ar", "-t", lib]) | ||
| 96 | + objects += [x.strip() for x in output.decode("utf-8").split("\n") if x.strip() != ""] | ||
| 97 | + run_command(["ar", "x", lib], cwd=tmp_obj_dir) | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +def pack_static_library(output_file, objects, tmp_obj_dir): | ||
| 101 | + output_abs_file = os.path.abspath(output_file) | ||
| 102 | + if os.path.exists(output_abs_file): | ||
| 103 | + os.remove(output_abs_file) | ||
| 104 | + | ||
| 105 | + step_size = 30 | ||
| 106 | + for index in range(0, len(objects), step_size): | ||
| 107 | + run_command(["ar", "qc", output_abs_file] + objects[index: index + step_size], cwd=tmp_obj_dir) | ||
| 108 | + run_command(["ranlib", output_abs_file]) | ||
| 109 | + | ||
| 110 | + | ||
| 111 | +def remove_temporary_files(tmp_obj_dir): | ||
| 112 | + if os.path.exists(tmp_obj_dir): | ||
| 113 | + shutil.rmtree(tmp_obj_dir) | ||
| 114 | + | ||
| 115 | + | ||
| 116 | +def main(): | ||
| 117 | + try: | ||
| 118 | + args = args_parse() | ||
| 119 | + | ||
| 120 | + objects = collect_object_from_files(args.object_files, args.tmp_obj_dir) | ||
| 121 | + unpack_kernel_library(args.kernel_libs, objects, args.tmp_obj_dir) | ||
| 122 | + pack_static_library(args.output_file, objects, args.tmp_obj_dir) | ||
| 123 | + | ||
| 124 | + if args.remove_tmp_files == "1": | ||
| 125 | + remove_temporary_files(args.tmp_obj_dir) | ||
| 126 | + except Exception as e: | ||
| 127 | + raise(e) | ||
| 128 | + | ||
| 129 | + | ||
| 130 | +if __name__ == "__main__": | ||
| 131 | + main() | ||
| @@ -0,0 +1,168 @@ | |||
| 1 | +""" | ||
| 2 | +#!/usr/bin/env python | ||
| 3 | +# -*- coding: UTF-8 -*- | ||
| 4 | +# Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import os | ||
| 8 | +import re | ||
| 9 | +import argparse | ||
| 10 | + | ||
| 11 | +CONFIG_CONTEXT = """ | ||
| 12 | +include(CMakeFindDependencyMacro) | ||
| 13 | + | ||
| 14 | +include("${CMAKE_CURRENT_LIST_DIR}/##{replace_op}##-targets.cmake") | ||
| 15 | + | ||
| 16 | +set(##{replace_op}##_VERSION 1.0.0) | ||
| 17 | +set(##{replace_op}##_VERSION_COMPATIBLE 1.0.0) | ||
| 18 | +""" | ||
| 19 | + | ||
| 20 | +TARGETS_CONTEXT = """ | ||
| 21 | +#Generated by CMake | ||
| 22 | + | ||
| 23 | +if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.6) | ||
| 24 | + message(FATAL_ERROR "CMake >= 2.6.0 required") | ||
| 25 | +endif() | ||
| 26 | +cmake_policy(PUSH) | ||
| 27 | +cmake_policy(VERSION 2.6...3.20) | ||
| 28 | +#---------------------------------- | ||
| 29 | +# Generated CMake target import file. | ||
| 30 | +#---------------------------------- | ||
| 31 | + | ||
| 32 | +# Commands may need to know the format version. | ||
| 33 | +set(CMAKE_IMPORT_FILE_VERSION 1) | ||
| 34 | + | ||
| 35 | +#Protect against multiple inclusion, which would fail when already imported targets are added once more. | ||
| 36 | +set(_targetsDefined) | ||
| 37 | +set(_targetsNotDefined) | ||
| 38 | +set(_expectedTargets) | ||
| 39 | +foreach(_expectedTarget ##{replace_op}##::static ##{replace_op}##::shared) | ||
| 40 | + list(APPEND _expectedTargets ${_expectedTarget}) | ||
| 41 | + if(NOT TARGET ${_expectedTarget}) | ||
| 42 | + list(APPEND _targetsNotDefined ${_expectedTarget}) | ||
| 43 | + endif() | ||
| 44 | + if(TARGET ${_expectedTarget}) | ||
| 45 | + list(APPEND _targetsDefined ${_expectedTarget}) | ||
| 46 | + endif() | ||
| 47 | +endforeach() | ||
| 48 | +if("${_targetsDefined}" STREQUAL "${_expectedTargets}") | ||
| 49 | + unset(_targetsDefined) | ||
| 50 | + unset(_targetsNotDefined) | ||
| 51 | + unset(_expectedTargets) | ||
| 52 | + set(CMAKE_IMPORT_FILE_VERSION) | ||
| 53 | + cmake_policy(POP) | ||
| 54 | + return() | ||
| 55 | +endif() | ||
| 56 | +if(NOT "${_targetsDefined}" STREQUAL "") | ||
| 57 | + message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\\nTargets Defined: ${_targetsDefined}\\nTargets not yet defined: ${_targetsNotDefined}\\n") | ||
| 58 | +endif() | ||
| 59 | +unset(_targetsDefined) | ||
| 60 | +unset(_targetsNotDefined) | ||
| 61 | +unset(_expectedTargets) | ||
| 62 | + | ||
| 63 | + | ||
| 64 | +#Compute the installation prefix relative to this file. | ||
| 65 | +get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) | ||
| 66 | +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) | ||
| 67 | +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) | ||
| 68 | +get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) | ||
| 69 | +if(_IMPORT_PREFIX STREQUAL "/") | ||
| 70 | + set(_IMPORT_PREFIX "") | ||
| 71 | +endif() | ||
| 72 | + | ||
| 73 | +#Check CANN env | ||
| 74 | +if (NOT DEFINED ENV{ASCEND_HOME_PATH}) | ||
| 75 | + message(STATUS "Cannot found CANN env ASCEND_HOME_PATH, please check and reset") | ||
| 76 | + return() | ||
| 77 | +endif() | ||
| 78 | + | ||
| 79 | +set(_ASCEND_HOME_PATH $ENV{ASCEND_HOME_PATH}) | ||
| 80 | + | ||
| 81 | +add_library(_asc_##{replace_op}##_static STATIC IMPORTED) | ||
| 82 | +set_target_properties(_asc_##{replace_op}##_static PROPERTIES | ||
| 83 | + IMPORTED_LOCATION "${_IMPORT_PREFIX}/lib/lib##{static_lib_name}##.a" | ||
| 84 | + INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include" | ||
| 85 | +) | ||
| 86 | + | ||
| 87 | +add_library(_asc_##{replace_op}##_base_deps INTERFACE) | ||
| 88 | +set_target_properties(_asc_##{replace_op}##_base_deps PROPERTIES | ||
| 89 | + INTERFACE_LINK_DIRECTORIES "${_ASCEND_HOME_PATH}/lib" | ||
| 90 | + INTERFACE_LINK_LIBRARIES "\$<LINK_ONLY:ascendcl>;\$<LINK_ONLY:nnopbase>;\$<LINK_ONLY:exe_graph>;\$<LINK_ONLY:register>;\$<LINK_ONLY:tiling_api>;-Wl,--push-state,--whole-archive -lrt2_registry -Wl,--pop-state" | ||
| 91 | +) | ||
| 92 | + | ||
| 93 | +add_library(_asc_intf_##{replace_op}##_static INTERFACE) | ||
| 94 | +add_library(##{replace_op}##::static ALIAS _asc_intf_##{replace_op}##_static) | ||
| 95 | +set_target_properties(_asc_intf_##{replace_op}##_static PROPERTIES | ||
| 96 | + INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include" | ||
| 97 | + INTERFACE_LINK_LIBRARIES "-Wl,--push-state,--whole-archive $<TARGET_PROPERTY:_asc_##{replace_op}##_static,IMPORTED_LOCATION> -Wl,--pop-state;\$<LINK_ONLY:_asc_##{replace_op}##_base_deps>" | ||
| 98 | +) | ||
| 99 | + | ||
| 100 | +# shared library | ||
| 101 | +add_library(##{replace_op}##::shared SHARED IMPORTED) | ||
| 102 | +set_target_properties(##{replace_op}##::shared PROPERTIES | ||
| 103 | + IMPORTED_LOCATION "${_IMPORT_PREFIX}/lib/lib##{dynamic_lib_name}##.so" | ||
| 104 | + INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include" | ||
| 105 | +) | ||
| 106 | + | ||
| 107 | +if(CMAKE_VERSION VERSION_LESS 2.8.12) | ||
| 108 | + message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.") | ||
| 109 | +endif() | ||
| 110 | + | ||
| 111 | + | ||
| 112 | +#Commands beyond this point should not need to know the version. | ||
| 113 | +set(CMAKE_IMPORT_FILE_VERSION) | ||
| 114 | +cmake_policy(POP) | ||
| 115 | +""" | ||
| 116 | + | ||
| 117 | + | ||
| 118 | +def make_config_file(filename, output_dir): | ||
| 119 | + output_config_file = os.path.join(output_dir, f"{filename}-config.cmake") | ||
| 120 | + os.makedirs(output_dir, exist_ok=True) | ||
| 121 | + | ||
| 122 | + content = re.sub(r"##{replace_op}##", filename, CONFIG_CONTEXT.strip()) | ||
| 123 | + with open(output_config_file, "w") as f: | ||
| 124 | + f.write(content) | ||
| 125 | + | ||
| 126 | + | ||
| 127 | +def make_targets_file(filename, output_dir, static_lib_name, dynamic_lib_name): | ||
| 128 | + output_targets_file = os.path.join(output_dir, f"{filename}-targets.cmake") | ||
| 129 | + os.makedirs(output_dir, exist_ok=True) | ||
| 130 | + | ||
| 131 | + values = {"replace_op": filename, "static_lib_name": static_lib_name, "dynamic_lib_name": dynamic_lib_name} | ||
| 132 | + content = TARGETS_CONTEXT.strip() | ||
| 133 | + for key in sorted(values, key=lambda x: len(x), reverse=True): | ||
| 134 | + content = re.sub(f"##{{{key}}}##", values.get(key, ""), content) | ||
| 135 | + | ||
| 136 | + with open(output_targets_file, "w") as f: | ||
| 137 | + f.write(content) | ||
| 138 | + | ||
| 139 | + | ||
| 140 | +def args_parse(): | ||
| 141 | + parser = argparse.ArgumentParser() | ||
| 142 | + parser.add_argument( | ||
| 143 | + "-v", "--vendor-name", help="Vendor name for cmake file." | ||
| 144 | + ) | ||
| 145 | + parser.add_argument( | ||
| 146 | + "-o", "--output-path", help="Ouput path for cmake file." | ||
| 147 | + ) | ||
| 148 | + parser.add_argument( | ||
| 149 | + "-s", "--static-lib-name", help="Static lib name." | ||
| 150 | + ) | ||
| 151 | + parser.add_argument( | ||
| 152 | + "-d", "--dynamic-lib-name", help="Dynamic lib name." | ||
| 153 | + ) | ||
| 154 | + return parser.parse_args() | ||
| 155 | + | ||
| 156 | + | ||
| 157 | +def main(): | ||
| 158 | + try: | ||
| 159 | + args = args_parse() | ||
| 160 | + | ||
| 161 | + make_config_file(args.vendor_name, args.output_path) | ||
| 162 | + make_targets_file(args.vendor_name, args.output_path, args.static_lib_name, args.dynamic_lib_name) | ||
| 163 | + except Exception as e: | ||
| 164 | + raise(e) | ||
| 165 | + | ||
| 166 | + | ||
| 167 | +if __name__ == "__main__": | ||
| 168 | + main() | ||
| @@ -0,0 +1,24 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import configparser | ||
| 8 | +import argparse | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +def args_parse(): | ||
| 12 | + parser = argparse.ArgumentParser() | ||
| 13 | + parser.add_argument( | ||
| 14 | + "-i", "--ini-file", help="op info ini." | ||
| 15 | + ) | ||
| 16 | + return parser.parse_args() | ||
| 17 | + | ||
| 18 | +if __name__ == "__main__": | ||
| 19 | + args = args_parse() | ||
| 20 | + op_config = configparser.ConfigParser() | ||
| 21 | + op_config.read(args.ini_file) | ||
| 22 | + for section in op_config.sections(): | ||
| 23 | + print(section, end="-") | ||
| 24 | + print(op_config.get(section, "opFile.value"), end="\n") | ||
| @@ -0,0 +1,698 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import argparse | ||
| 8 | +import glob | ||
| 9 | +import sys | ||
| 10 | +import os | ||
| 11 | +import re | ||
| 12 | +import datetime | ||
| 13 | +from typing import List | ||
| 14 | +import json | ||
| 15 | +import opdesc_parser | ||
| 16 | +import const_var | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 20 | + | ||
| 21 | +IMPL_HEAD = '''#!/usr/bin/env python | ||
| 22 | +# -*- coding: UTF-8 -*- | ||
| 23 | +""" | ||
| 24 | +Copyright (c) Huawei Technologies Co., Ltd. {}-{}. All rights reserved. | ||
| 25 | +""" | ||
| 26 | + | ||
| 27 | +import re | ||
| 28 | +import os, sys | ||
| 29 | +import ctypes | ||
| 30 | +import json | ||
| 31 | +import shutil | ||
| 32 | +from tbe.common.platform import get_soc_spec | ||
| 33 | +from tbe.common.utils import para_check | ||
| 34 | +from tbe.tikcpp import compile_op, replay_op, check_op_cap, generalize_op_params, get_code_channel, OpInfo | ||
| 35 | +from tbe.tikcpp.compile_op import CommonUtility, AscendCLogLevel | ||
| 36 | +from tbe.common.buildcfg import get_default_build_config | ||
| 37 | +import tbe.common.register as tbe_register | ||
| 38 | +from tbe.common.buildcfg import get_current_build_config | ||
| 39 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 40 | + | ||
| 41 | +DTYPE_MAP = {{"float32": ["DT_FLOAT", "float"], | ||
| 42 | + "float16": ["DT_FLOAT16", "half"], | ||
| 43 | + "int8": ["DT_INT8", "int8_t"], | ||
| 44 | + "int16": ["DT_INT16", "int16_t"], | ||
| 45 | + "int32": ["DT_INT32", "int32_t"], | ||
| 46 | + "int64": ["DT_INT64", "int64_t"], | ||
| 47 | + "uint1": ["DT_UINT1", "uint1b_t"], | ||
| 48 | + "uint8": ["DT_UINT8", "uint8_t"], | ||
| 49 | + "uint16": ["DT_UINT16", "uint16_t"], | ||
| 50 | + "uint32": ["DT_UINT32", "uint32_t"], | ||
| 51 | + "uint64": ["DT_UINT64", "uint64_t"], | ||
| 52 | + "bool": ["DT_BOOL", "bool"], | ||
| 53 | + "double": ["DT_DOUBLE", "double"], | ||
| 54 | + "dual": ["DT_DUAL", "unknown"], | ||
| 55 | + "dual_sub_int8": ["DT_DUAL_SUB_INT8", "unknown"], | ||
| 56 | + "dual_sub_uint8": ["DT_DUAL_SUB_UINT8", "unknown"], | ||
| 57 | + "string": ["DT_STRING", "unknown"], | ||
| 58 | + "complex32": ["DT_COMPLEX32", "complex32"], | ||
| 59 | + "complex64": ["DT_COMPLEX64", "complex64"], | ||
| 60 | + "complex128": ["DT_COMPLEX128", "unknown"], | ||
| 61 | + "qint8": ["DT_QINT8", "unknown"], | ||
| 62 | + "qint16": ["DT_QINT16", "unknown"], | ||
| 63 | + "qint32": ["DT_QINT32", "unknown"], | ||
| 64 | + "quint8": ["DT_QUINT8", "unknown"], | ||
| 65 | + "quint16": ["DT_QUINT16", "unknown"], | ||
| 66 | + "resource": ["DT_RESOURCE", "unknown"], | ||
| 67 | + "string_ref": ["DT_STRING_REF", "unknown"], | ||
| 68 | + "int4": ["DT_INT4", "int4b_t"], | ||
| 69 | + "bfloat16": ["DT_BF16", "bfloat16_t"], | ||
| 70 | + "float8_e5m2": ["DT_FLOAT8_E5M2", "fp8_e5m2_t"], | ||
| 71 | + "float8_e4m3fn": ["DT_FLOAT8_E4M3FN", "fp8_e4m3fn_t"], | ||
| 72 | + "hifloat8":["DT_HIFLOAT8", "hifloat8_t"], | ||
| 73 | + "float8_e8m0":["DT_FLOAT8_E8M0", "fp8_e8m0_t"], | ||
| 74 | + "float4_e2m1":["DT_FLOAT4_E2M1", "fp4x2_e2m1_t"], | ||
| 75 | + "float4_e1m2":["DT_FLOAT4_E1M2", "fp4x2_e1m2_t"], | ||
| 76 | + "int2": ["DT_INT2", "int2b_t"]}} | ||
| 77 | + | ||
| 78 | +def add_dtype_fmt_option_single(x, x_n, is_ref: bool = False): | ||
| 79 | + options = [] | ||
| 80 | + x_fmt = x.get("format") | ||
| 81 | + x_dtype = x.get("dtype") | ||
| 82 | + x_n_in_kernel = x_n + '_REF' if is_ref else x_n | ||
| 83 | + options.append("-DDTYPE_{{n}}={{t}}".format(n=x_n_in_kernel, t=DTYPE_MAP.get(x_dtype)[1])) | ||
| 84 | + options.append("-DORIG_DTYPE_{{n}}={{ot}}".format(n=x_n_in_kernel, ot=DTYPE_MAP.get(x_dtype)[0])) | ||
| 85 | + options.append("-DFORMAT_{{n}}=FORMAT_{{f}}".format(n=x_n_in_kernel, f=x_fmt)) | ||
| 86 | + return options | ||
| 87 | + | ||
| 88 | +def get_dtype_fmt_options(__inputs__, __outputs__): | ||
| 89 | + options = [] | ||
| 90 | + input_names = {} | ||
| 91 | + output_names = {} | ||
| 92 | + unique_param_name_set = set() | ||
| 93 | + for idx, x in enumerate(__inputs__): | ||
| 94 | + if x is None: | ||
| 95 | + continue | ||
| 96 | + x_n = input_names[idx].upper() | ||
| 97 | + unique_param_name_set.add(x_n) | ||
| 98 | + options += add_dtype_fmt_option_single(x, x_n) | ||
| 99 | + | ||
| 100 | + for idx, x in enumerate(__outputs__): | ||
| 101 | + if x is None: | ||
| 102 | + continue | ||
| 103 | + x_n = output_names[idx].upper() | ||
| 104 | + if x_n in unique_param_name_set: | ||
| 105 | + options += add_dtype_fmt_option_single(x, x_n, True) | ||
| 106 | + else: | ||
| 107 | + options += add_dtype_fmt_option_single(x, x_n) | ||
| 108 | + return options | ||
| 109 | + | ||
| 110 | +def load_dso(so_path): | ||
| 111 | + try: | ||
| 112 | + ctypes.CDLL(so_path) | ||
| 113 | + except OSError as error : | ||
| 114 | + CommonUtility.print_compile_log("", error, AscendCLogLevel.LOG_ERROR) | ||
| 115 | + raise RuntimeError("cannot open %s" %(so_path)) | ||
| 116 | + else: | ||
| 117 | + msg = "load so succ " + so_path | ||
| 118 | + CommonUtility.print_compile_log("", msg, AscendCLogLevel.LOG_INFO) | ||
| 119 | + | ||
| 120 | +def get_shortsoc_compile_option(compile_option_list: list, shortsoc:str): | ||
| 121 | + compile_options = [] | ||
| 122 | + if shortsoc in compile_option_list: | ||
| 123 | + compile_options.extend(compile_option_list[shortsoc]) | ||
| 124 | + if '__ALLSOC__' in compile_option_list: | ||
| 125 | + compile_options.extend(compile_option_list['__ALLSOC__']) | ||
| 126 | + return compile_options | ||
| 127 | + | ||
| 128 | +def get_kernel_source(src_file, dir_snake, dir_ex): | ||
| 129 | + src_ex = os.path.join(PYF_PATH, "..", "ascendc", dir_ex, src_file) | ||
| 130 | + if os.path.exists(src_ex): | ||
| 131 | + return src_ex | ||
| 132 | + src = os.environ.get('BUILD_KERNEL_SRC') | ||
| 133 | + if src and os.path.exists(src): | ||
| 134 | + return src | ||
| 135 | + src = os.path.join(PYF_PATH, "..", "ascendc", dir_snake, src_file) | ||
| 136 | + if os.path.exists(src): | ||
| 137 | + return src | ||
| 138 | + src = os.path.join(PYF_PATH, src_file) | ||
| 139 | + if os.path.exists(src): | ||
| 140 | + return src | ||
| 141 | + src = os.path.join(PYF_PATH, "..", "ascendc", dir_snake, dir_snake + ".cpp") | ||
| 142 | + if os.path.exists(src): | ||
| 143 | + return src | ||
| 144 | + src = os.path.join(PYF_PATH, "..", "ascendc", dir_ex, dir_ex + ".cpp") | ||
| 145 | + if os.path.exists(src): | ||
| 146 | + return src | ||
| 147 | + src = os.path.join(PYF_PATH, "..", "ascendc", os.path.splitext(src_file)[0], src_file) | ||
| 148 | + if os.path.exists(src): | ||
| 149 | + return src | ||
| 150 | + return src_ex | ||
| 151 | + | ||
| 152 | +''' | ||
| 153 | + | ||
| 154 | +IMPL_API = ''' | ||
| 155 | + | ||
| 156 | + | ||
| 157 | +def {}({}, kernel_name="{}"{}): | ||
| 158 | +{} | ||
| 159 | + if get_current_build_config("enable_op_prebuild"): | ||
| 160 | + return | ||
| 161 | + __inputs__, __outputs__, __attrs__ = _build_args({}) | ||
| 162 | + options = get_dtype_fmt_options(__inputs__, __outputs__) | ||
| 163 | + options += ["-x", "cce"] | ||
| 164 | + bisheng = os.environ.get('BISHENG_REAL_PATH') | ||
| 165 | + if bisheng is None: | ||
| 166 | + bisheng = shutil.which("bisheng") | ||
| 167 | + if bisheng != None: | ||
| 168 | + bisheng_path = os.path.dirname(bisheng) | ||
| 169 | + tikcpp_path = os.path.realpath(os.path.join(bisheng_path, "..", "..", "tikcpp")) | ||
| 170 | + else: | ||
| 171 | + tikcpp_path = os.path.realpath("/usr/local/Ascend/latest/compiler/tikcpp") | ||
| 172 | + options.append("-I" + tikcpp_path) | ||
| 173 | + options.append("-I" + os.path.join(tikcpp_path, "..", "..", "include")) | ||
| 174 | + options.append("-I" + os.path.join(tikcpp_path, "..", "..", "include", "ascendc")) | ||
| 175 | + options.append("-I" + os.path.join(tikcpp_path, "tikcfw")) | ||
| 176 | + options.append("-I" + os.path.join(tikcpp_path, "tikcfw", "impl")) | ||
| 177 | + options.append("-I" + os.path.join(tikcpp_path, "tikcfw", "interface")) | ||
| 178 | + options.append("-I" + os.path.join(tikcpp_path, "..", "ascendc", "act")) | ||
| 179 | + options.append("-I" + os.path.join(PYF_PATH, "..", "ascendc", "common")) | ||
| 180 | + if "impl_mode" in locals(): | ||
| 181 | + if impl_mode == "high_performance": | ||
| 182 | + options.append("-DHIGH_PERFORMANCE=1") | ||
| 183 | + elif impl_mode == "high_precision": | ||
| 184 | + options.append("-DHIGH_PRECISION=1") | ||
| 185 | + elif "high_precision" in impl_mode and "high_performance" in impl_mode: | ||
| 186 | + options.append("-DHIGH_PRECISION=1 -DHIGH_PERFORMANCE=1") | ||
| 187 | + if get_current_build_config("enable_deterministic_mode") == 1: | ||
| 188 | + options.append("-DDETERMINISTIC_MODE=1") | ||
| 189 | + else: | ||
| 190 | + options.append("-DDETERMINISTIC_MODE=0") | ||
| 191 | + ascendc_api_version_header_path = os.path.join(tikcpp_path, "tikcfw/lib/ascendc_api_version.h") | ||
| 192 | + if os.path.exists(ascendc_api_version_header_path): | ||
| 193 | + with open(ascendc_api_version_header_path, "r") as ascendc_api_version_file: | ||
| 194 | + ascendc_api_version = re.findall(r"#define ASCENDC_API_VERSION (\d+)", ascendc_api_version_file.read()) | ||
| 195 | + if ascendc_api_version: | ||
| 196 | + options.append(f"-DASCENDC_API_VERSION={{ascendc_api_version[0]}}") | ||
| 197 | + custom_compile_options = {}, | ||
| 198 | + custom_all_compile_options = {}, | ||
| 199 | + soc_version = get_soc_spec("SOC_VERSION") | ||
| 200 | + soc_short = get_soc_spec("SHORT_SOC_VERSION").lower() | ||
| 201 | + custom_compile_options_soc = get_shortsoc_compile_option(custom_compile_options[0], soc_short) | ||
| 202 | + custom_all_compile_options_soc = get_shortsoc_compile_option(custom_all_compile_options[0], soc_short) | ||
| 203 | + options += custom_all_compile_options_soc | ||
| 204 | + options += custom_compile_options_soc | ||
| 205 | + | ||
| 206 | + origin_func_name = "{}" | ||
| 207 | + ascendc_src_dir_ex = "{}" | ||
| 208 | + ascendc_src_dir = "{}" | ||
| 209 | + ascendc_src_file = "{}" | ||
| 210 | + src = get_kernel_source(ascendc_src_file, ascendc_src_dir, ascendc_src_dir_ex) | ||
| 211 | +''' | ||
| 212 | + | ||
| 213 | +REPLAY_OP_API = ''' | ||
| 214 | + msg = "start replay Ascend C Operator {}, kernel name is {}" | ||
| 215 | + CommonUtility.print_compile_log("", msg, AscendCLogLevel.LOG_INFO) | ||
| 216 | + tikreplay_codegen_path = tikcpp_path + "/tikreplaylib/lib" | ||
| 217 | + tikreplay_stub_path = tikcpp_path + "/tikreplaylib/lib/" + soc_version | ||
| 218 | + msg = "start load libtikreplaylib_codegen.so and libtikreplaylib_stub.so" | ||
| 219 | + CommonUtility.print_compile_log("", msg, AscendCLogLevel.LOG_INFO) | ||
| 220 | + codegen_so_path = tikreplay_codegen_path + "/libtikreplaylib_codegen.so" | ||
| 221 | + replaystub_so_path = tikreplay_stub_path + "/libtikreplaylib_stub.so" | ||
| 222 | + if PYF_PATH.endswith("dynamic"): | ||
| 223 | + op_replay_path = os.path.join(PYF_PATH, "..", "..", "op_replay") | ||
| 224 | + else: | ||
| 225 | + op_replay_path = os.path.join(PYF_PATH, "..", "op_replay") | ||
| 226 | + replayapi_so_path = os.path.join(op_replay_path, "libreplay_{}_" + soc_short + ".so") | ||
| 227 | + load_dso(codegen_so_path) | ||
| 228 | + load_dso(replaystub_so_path) | ||
| 229 | + load_dso(replayapi_so_path) | ||
| 230 | + op_type = "{}" | ||
| 231 | + entry_obj = os.path.join(op_replay_path, "{}_entry_" + soc_short + ".o") | ||
| 232 | + code_channel = get_code_channel(src, kernel_name, op_type, options) | ||
| 233 | + op_info = OpInfo(kernel_name = kernel_name, op_type = op_type, inputs = __inputs__, outputs = __outputs__,\\ | ||
| 234 | + attrs = __attrs__, impl_mode = impl_mode, param_type_dynamic = {}) | ||
| 235 | + res, msg = replay_op(op_info, entry_obj, code_channel, src, options) | ||
| 236 | + if not res: | ||
| 237 | + print("call replay op failed for %s and get into call compile op" %(msg)) | ||
| 238 | + compile_op(src, origin_func_name, op_info, options, code_channel, '{}') | ||
| 239 | +''' | ||
| 240 | + | ||
| 241 | +COMPILE_OP_API = ''' | ||
| 242 | + msg = "start compile Ascend C Operator {}, kernel name is " + kernel_name | ||
| 243 | + CommonUtility.print_compile_log("", msg, AscendCLogLevel.LOG_INFO) | ||
| 244 | + op_type = "{}" | ||
| 245 | + code_channel = get_code_channel(src, kernel_name, op_type, options) | ||
| 246 | + op_info = OpInfo(kernel_name = kernel_name, op_type = op_type, inputs = __inputs__, outputs = __outputs__,\\ | ||
| 247 | + attrs = __attrs__ {}, origin_inputs=[{}], origin_outputs = [{}],\\ | ||
| 248 | + param_type_dynamic = {}, mc2_ctx = {}, param_type_list = {}, init_value_list = {},\\ | ||
| 249 | + output_shape_depend_on_compute = {}) | ||
| 250 | + compile_op(src, origin_func_name, op_info, options, code_channel, '{}', {}) | ||
| 251 | +''' | ||
| 252 | +COMPILE_OP_API_BUILT_IN = ''' | ||
| 253 | + msg = "start compile Ascend C Operator {}, kernel name is " + kernel_name | ||
| 254 | + CommonUtility.print_compile_log("", msg, AscendCLogLevel.LOG_INFO) | ||
| 255 | + op_type = "{}" | ||
| 256 | + code_channel = get_code_channel(src, kernel_name, op_type, options) | ||
| 257 | + op_info = OpInfo(kernel_name = kernel_name, op_type = op_type, inputs = __inputs__, outputs = __outputs__,\\ | ||
| 258 | + attrs = __attrs__ {}, origin_inputs=[{}], origin_outputs = [{}],\\ | ||
| 259 | + param_type_dynamic = {}, mc2_ctx = {}, param_type_list = {}, init_value_list = {},\\ | ||
| 260 | + output_shape_depend_on_compute = {}) | ||
| 261 | + | ||
| 262 | + op_compile_option = '{}' | ||
| 263 | + opp_path = os.environ.get('ASCEND_OPP_PATH') | ||
| 264 | + dat_path = os.path.realpath(os.path.join(opp_path, "built-in", "op_impl", "ai_core", "tbe", "ascendc_impl.dat")) | ||
| 265 | + if opp_path and os.path.exists(dat_path): | ||
| 266 | + # dat file exists: built in hidden src file online compiling process. append vfs compile option in compile_op | ||
| 267 | + abs_rel_kernel_src_path = "{}" | ||
| 268 | + extend_options = {} | ||
| 269 | + extend_options['opp_kernel_hidden_dat_path'] = dat_path | ||
| 270 | + compile_op(abs_rel_kernel_src_path, origin_func_name, op_info, options, code_channel, op_compile_option,\\ | ||
| 271 | + extend_options) | ||
| 272 | + else: | ||
| 273 | + raise RuntimeError("built-in opp compile, ascendc_impl.dat file path does not exist: %s" %(dat_path)) | ||
| 274 | +''' | ||
| 275 | +SUP_API = ''' | ||
| 276 | +def {}({}{}): | ||
| 277 | + __inputs__, __outputs__, __attrs__ = _build_args({}) | ||
| 278 | + ret_str = check_op_cap("{}", "{}", __inputs__, __outputs__, __attrs__) | ||
| 279 | + ret_dict = json.loads(ret_str) | ||
| 280 | + err_code = ret_dict.get("ret_code") | ||
| 281 | + sup = "Unknown" | ||
| 282 | + reason = "Unknown reason" | ||
| 283 | + if err_code is not None: | ||
| 284 | + if err_code == 0: | ||
| 285 | + sup = "True" | ||
| 286 | + reason = "" | ||
| 287 | + elif err_code == 1: | ||
| 288 | + sup = "False" | ||
| 289 | + reason = ret_dict.get("reason") | ||
| 290 | + else: | ||
| 291 | + sup = "Unknown" | ||
| 292 | + reason = ret_dict.get("reason") | ||
| 293 | + return sup, reason | ||
| 294 | +''' | ||
| 295 | +CAP_API = ''' | ||
| 296 | +def {}({}{}): | ||
| 297 | + __inputs__, __outputs__, __attrs__ = _build_args({}) | ||
| 298 | + result = check_op_cap("{}", "{}", __inputs__, __outputs__, __attrs__) | ||
| 299 | + return result.decode("utf-8") | ||
| 300 | +''' | ||
| 301 | +GLZ_API = ''' | ||
| 302 | + | ||
| 303 | +def {}_generalization({}, generalize_config=None): | ||
| 304 | + __inputs__, __outputs__, __attrs__ = _build_args({}) | ||
| 305 | + ret_str = generalize_op_params("{}", __inputs__, __outputs__, __attrs__, generalize_config) | ||
| 306 | + return [json.loads(ret_str)] | ||
| 307 | +''' | ||
| 308 | + | ||
| 309 | +ATTR_DEFAULT = {'bool': 'False', 'int': '0', 'float': '0.0', 'list_int': '[]', | ||
| 310 | + 'list_float': '[]', 'list_bool': '[]', 'list_list_int': '[[]]', 'str': ''} | ||
| 311 | + | ||
| 312 | + | ||
| 313 | +def optype_snake(origin_str): | ||
| 314 | + temp_str = origin_str[0].lower() + origin_str[1:] | ||
| 315 | + new_str = re.sub(r'([A-Z])', r'_\1', temp_str).lower() | ||
| 316 | + return new_str | ||
| 317 | + | ||
| 318 | + | ||
| 319 | +def optype_snake_ex(s): | ||
| 320 | + snake_case = "" | ||
| 321 | + for i, c in enumerate(s): | ||
| 322 | + if i == 0: | ||
| 323 | + snake_case += c.lower() | ||
| 324 | + elif c.isupper(): | ||
| 325 | + if s[i - 1] != '_': | ||
| 326 | + if not s[i - 1].isupper(): | ||
| 327 | + snake_case += "_" | ||
| 328 | + elif s[i - 1].isupper() and (i + 1) < len(s) and s[i + 1].islower(): | ||
| 329 | + snake_case += "_" | ||
| 330 | + snake_case += c.lower() | ||
| 331 | + else: | ||
| 332 | + snake_case += c | ||
| 333 | + return snake_case | ||
| 334 | + | ||
| 335 | + | ||
| 336 | +class AdpBuilder(opdesc_parser.OpDesc): | ||
| 337 | + def __init__(self: any, op_type: str): | ||
| 338 | + self.argsdefv = [] | ||
| 339 | + self.op_compile_option:str = '{}' | ||
| 340 | + super().__init__(op_type) | ||
| 341 | + | ||
| 342 | + | ||
| 343 | + def write_adapt(self: any, impl_path, path: str, op_compile_option_all: list = None): | ||
| 344 | + self._build_paradefault() | ||
| 345 | + if os.environ.get('BUILD_BUILTIN_OPP') != '1' and impl_path != "": | ||
| 346 | + src_file = os.path.join(impl_path, self.op_file + '.cpp') | ||
| 347 | + if not os.path.exists(src_file): | ||
| 348 | + print(f"[ERROR]: operator: {self.op_file} source file: {src_file} does not found, please check.") | ||
| 349 | + return | ||
| 350 | + out_path = os.path.abspath(path) | ||
| 351 | + if self.dynamic_shape and not out_path.endswith('dynamic'): | ||
| 352 | + out_path = os.path.join(path, 'dynamic') | ||
| 353 | + os.makedirs(out_path, exist_ok=True) | ||
| 354 | + adpfile = os.path.join(out_path, self.op_file + '.py') | ||
| 355 | + self._gen_op_compile_option(op_compile_option_all) | ||
| 356 | + with os.fdopen(os.open(adpfile, const_var.WFLAGS, const_var.WMODES), 'w') as fd: | ||
| 357 | + self._write_head(fd) | ||
| 358 | + self._write_argparse(fd) | ||
| 359 | + self._get_impl_mode() | ||
| 360 | + self._write_impl(fd, impl_path) | ||
| 361 | + if self.op_chk_support: | ||
| 362 | + self._write_cap('check_supported', fd) | ||
| 363 | + self._write_cap('get_op_support_info', fd) | ||
| 364 | + if self.op_fmt_sel: | ||
| 365 | + self._write_cap('op_select_format', fd) | ||
| 366 | + self._write_cap('get_op_specific_info', fd) | ||
| 367 | + if self.op_range_limit == 'limited' or self.op_range_limit == 'dynamic': | ||
| 368 | + self._write_glz(fd) | ||
| 369 | + | ||
| 370 | + | ||
| 371 | + def _gen_op_compile_option(self: any, op_compile_option_all: list = None): | ||
| 372 | + if op_compile_option_all is not None: | ||
| 373 | + if self.op_type in op_compile_option_all: | ||
| 374 | + self.op_compile_option = op_compile_option_all[self.op_type] | ||
| 375 | + elif "__all__" in op_compile_option_all: | ||
| 376 | + self.op_compile_option = op_compile_option_all["__all__"] | ||
| 377 | + | ||
| 378 | + | ||
| 379 | + def _ip_argpack(self: any, default: bool = True) -> list: | ||
| 380 | + args = [] | ||
| 381 | + for i in range(len(self.input_name)): | ||
| 382 | + arg = self.input_name[i] | ||
| 383 | + if default and self.argsdefv[i] is not None: | ||
| 384 | + arg += '=' + self.argsdefv[i] | ||
| 385 | + args.append(arg) | ||
| 386 | + return args | ||
| 387 | + | ||
| 388 | + def _op_argpack(self: any, default: bool = True) -> list: | ||
| 389 | + args = [] | ||
| 390 | + argidx = len(self.input_name) | ||
| 391 | + for i in range(len(self.output_name)): | ||
| 392 | + arg = self.output_name[i] | ||
| 393 | + if default and self.argsdefv[i + argidx] is not None: | ||
| 394 | + arg += '=' + self.argsdefv[i + argidx] | ||
| 395 | + args.append(arg) | ||
| 396 | + return args | ||
| 397 | + | ||
| 398 | + def _attr_argpack(self: any, default: bool = True) -> list: | ||
| 399 | + args = [] | ||
| 400 | + argidx = len(self.input_name) + len(self.output_name) | ||
| 401 | + for i in range(len(self.attr_list)): | ||
| 402 | + att = self.attr_list[i] | ||
| 403 | + arg = att | ||
| 404 | + if default and self.argsdefv[i + argidx] is not None: | ||
| 405 | + if self.attr_val.get(att).get('type') == 'str': | ||
| 406 | + arg += '="' + self.argsdefv[i + argidx] + '"' | ||
| 407 | + elif self.attr_val.get(att).get('type') == 'bool': | ||
| 408 | + arg += '=' + self.argsdefv[i + argidx].capitalize() | ||
| 409 | + elif self.attr_val.get(att).get('type') == 'list_bool': | ||
| 410 | + arg += '=' + "[" + ", ".join(word.strip().capitalize() \ | ||
| 411 | + for word in self.argsdefv[i + argidx].strip('[]').split(',')) + "]" | ||
| 412 | + else: | ||
| 413 | + arg += '=' + self.argsdefv[i + argidx] | ||
| 414 | + args.append(arg) | ||
| 415 | + return args | ||
| 416 | + | ||
| 417 | + def _build_paralist(self: any, default: bool = True) -> str: | ||
| 418 | + args = [] | ||
| 419 | + args.extend(self._ip_argpack(default)) | ||
| 420 | + args.extend(self._op_argpack(default)) | ||
| 421 | + args.extend(self._attr_argpack(default)) | ||
| 422 | + return ', '.join(args) | ||
| 423 | + | ||
| 424 | + def _io_parachk(self: any, types: list, type_name: str) -> list: | ||
| 425 | + chk = [] | ||
| 426 | + for iot in types: | ||
| 427 | + if iot == 'optional': | ||
| 428 | + ptype = 'OPTION' | ||
| 429 | + else: | ||
| 430 | + ptype = iot.upper() | ||
| 431 | + chk.append('para_check.{}_{}'.format(ptype, type_name)) | ||
| 432 | + return chk | ||
| 433 | + | ||
| 434 | + def _attr_parachk(self: any) -> list: | ||
| 435 | + chk = [] | ||
| 436 | + for att in self.attr_list: | ||
| 437 | + att_type = self.attr_val.get(att).get('type').upper() | ||
| 438 | + chk.append('para_check.{}_ATTR_{}'.format('OPTION', att_type)) | ||
| 439 | + return chk | ||
| 440 | + | ||
| 441 | + def _build_parachk(self: any) -> str: | ||
| 442 | + chk = [] | ||
| 443 | + chk.extend(self._io_parachk(self.input_type, 'INPUT')) | ||
| 444 | + chk.extend(self._io_parachk(self.output_type, 'OUTPUT')) | ||
| 445 | + chk.extend(self._attr_parachk()) | ||
| 446 | + chk.append('para_check.KERNEL_NAME') | ||
| 447 | + return ', '.join(chk) | ||
| 448 | + | ||
| 449 | + def _build_virtual(self: any) -> str: | ||
| 450 | + virt_exp = [] | ||
| 451 | + for index in range(len(self.input_name)): | ||
| 452 | + if self.input_virt.get(index) is None: | ||
| 453 | + continue | ||
| 454 | + val = [] | ||
| 455 | + val.append('"param_name":"{}"'.format(self.input_name[index])) | ||
| 456 | + val.append('"index":{}'.format(index)) | ||
| 457 | + val.append('"dtype":"{}"'.format(self.input_dtype[index].split(',')[0])) | ||
| 458 | + val.append('"format":"{}"'.format(self.input_fmt[index].split(',')[0])) | ||
| 459 | + val.append('"ori_format":"{}"'.format(self.input_fmt[index].split(',')[0])) | ||
| 460 | + val.append('"paramType":"optional"') | ||
| 461 | + val.append('"shape":[1]') | ||
| 462 | + val.append('"ori_shape":[1]') | ||
| 463 | + virt_exp.append(' ' + self.input_name[index] + ' = {' + ','.join(val) + '}') | ||
| 464 | + if len(virt_exp) > 0: | ||
| 465 | + return '\n'.join(virt_exp) | ||
| 466 | + else: | ||
| 467 | + return ' # do ascendc build step' | ||
| 468 | + | ||
| 469 | + def _build_mc2_ctx(self: any): | ||
| 470 | + if len(self.mc2_ctx) != 0: | ||
| 471 | + return '["' + '", "'.join(self.mc2_ctx) + '"]' | ||
| 472 | + return '[]' | ||
| 473 | + | ||
| 474 | + def _build_paradefault(self: any): | ||
| 475 | + optional = False | ||
| 476 | + argtypes = [] | ||
| 477 | + argtypes.extend(self.input_type) | ||
| 478 | + argtypes.extend(self.output_type) | ||
| 479 | + in_idx = 0 | ||
| 480 | + for atype in argtypes: | ||
| 481 | + if atype == 'optional': | ||
| 482 | + optional = True | ||
| 483 | + if optional: | ||
| 484 | + self.argsdefv.append('None') | ||
| 485 | + else: | ||
| 486 | + self.argsdefv.append(None) | ||
| 487 | + in_idx += 1 | ||
| 488 | + for attr in self.attr_list: | ||
| 489 | + atype = self.attr_val.get(attr).get('paramType') | ||
| 490 | + if atype == 'optional': | ||
| 491 | + optional = True | ||
| 492 | + attrval = self.attr_val.get(attr).get('defaultValue') | ||
| 493 | + if attrval is not None: | ||
| 494 | + optional = True | ||
| 495 | + if type == "bool": | ||
| 496 | + attrval = attrval.capitalize() | ||
| 497 | + elif type == "str": | ||
| 498 | + attrval = "\"" + attrval + "\"" | ||
| 499 | + self.argsdefv.append(attrval) | ||
| 500 | + continue | ||
| 501 | + if optional: | ||
| 502 | + self.argsdefv.append(ATTR_DEFAULT.get(self.attr_val.get(attr).get('type'))) | ||
| 503 | + else: | ||
| 504 | + self.argsdefv.append(None) | ||
| 505 | + | ||
| 506 | + def _write_head(self: any, fd: object): | ||
| 507 | + now = datetime.datetime.now() | ||
| 508 | + curr_year = now.year | ||
| 509 | + former_year = curr_year - 1 | ||
| 510 | + fd.write(IMPL_HEAD.format(former_year, curr_year, self.input_ori_name, self.output_ori_name)) | ||
| 511 | + | ||
| 512 | + def _write_argparse(self: any, fd: object): | ||
| 513 | + args = self._build_paralist(False) | ||
| 514 | + fd.write('def _build_args({}):\n'.format(args)) | ||
| 515 | + fd.write(' __inputs__ = []\n') | ||
| 516 | + fd.write(' for arg in [{}]:\n'.format(', '.join(self.input_name))) | ||
| 517 | + fd.write(' if arg != None:\n') | ||
| 518 | + fd.write(' if isinstance(arg, (list, tuple)):\n') | ||
| 519 | + fd.write(' if len(arg) == 0:\n') | ||
| 520 | + fd.write(' continue\n') | ||
| 521 | + fd.write(' __inputs__.append(arg[0])\n') | ||
| 522 | + fd.write(' else:\n') | ||
| 523 | + fd.write(' __inputs__.append(arg)\n') | ||
| 524 | + fd.write(' else:\n') | ||
| 525 | + fd.write(' __inputs__.append(arg)\n') | ||
| 526 | + fd.write(' __outputs__ = []\n') | ||
| 527 | + fd.write(' for arg in [{}]:\n'.format(', '.join(self.output_name))) | ||
| 528 | + fd.write(' if arg != None:\n') | ||
| 529 | + fd.write(' if isinstance(arg, (list, tuple)):\n') | ||
| 530 | + fd.write(' if len(arg) == 0:\n') | ||
| 531 | + fd.write(' continue\n') | ||
| 532 | + fd.write(' __outputs__.append(arg[0])\n') | ||
| 533 | + fd.write(' else:\n') | ||
| 534 | + fd.write(' __outputs__.append(arg)\n') | ||
| 535 | + fd.write(' else:\n') | ||
| 536 | + fd.write(' __outputs__.append(arg)\n') | ||
| 537 | + fd.write(' __attrs__ = []\n') | ||
| 538 | + for attr in self.attr_list: | ||
| 539 | + fd.write(' if {} != None:\n'.format(attr)) | ||
| 540 | + fd.write(' attr = {}\n') | ||
| 541 | + fd.write(' attr["name"] = "{}"\n'.format(attr)) | ||
| 542 | + fd.write(' attr["dtype"] = "{}"\n'.format(self.attr_val.get(attr).get('type'))) | ||
| 543 | + fd.write(' attr["value"] = {}\n'.format(attr)) | ||
| 544 | + fd.write(' __attrs__.append(attr)\n') | ||
| 545 | + fd.write(' return __inputs__, __outputs__, __attrs__\n') | ||
| 546 | + | ||
| 547 | + def _get_kernel_source(self: any, kernel_src_dir, src_file, dir_snake, dir_ex): | ||
| 548 | + src_ex = os.path.join(kernel_src_dir, dir_ex, src_file) | ||
| 549 | + if os.path.exists(src_ex): | ||
| 550 | + return src_ex | ||
| 551 | + src = os.environ.get('BUILD_KERNEL_SRC') | ||
| 552 | + if src and os.path.exists(src): | ||
| 553 | + return src | ||
| 554 | + src = os.path.join(kernel_src_dir, dir_snake, src_file) | ||
| 555 | + if os.path.exists(src): | ||
| 556 | + return src | ||
| 557 | + src = os.path.join(kernel_src_dir, src_file) | ||
| 558 | + if os.path.exists(src): | ||
| 559 | + return src | ||
| 560 | + src = os.path.join(kernel_src_dir, dir_snake, dir_snake + ".cpp") | ||
| 561 | + if os.path.exists(src): | ||
| 562 | + return src | ||
| 563 | + src = os.path.join(kernel_src_dir, dir_ex, dir_ex + ".cpp") | ||
| 564 | + if os.path.exists(src): | ||
| 565 | + return src | ||
| 566 | + src = os.path.join(kernel_src_dir, os.path.splitext(src_file)[0], src_file) | ||
| 567 | + if os.path.exists(src): | ||
| 568 | + return src | ||
| 569 | + return src_ex | ||
| 570 | + | ||
| 571 | + def _get_impl_mode(self: any): | ||
| 572 | + op_compile_options = json.loads(self.op_compile_option) | ||
| 573 | + if "impl_mode" in op_compile_options: | ||
| 574 | + if op_compile_options['impl_mode'] == "": | ||
| 575 | + self.impl_mode = "" | ||
| 576 | + self.impl_mode_op_info = "" | ||
| 577 | + del op_compile_options['impl_mode'] | ||
| 578 | + self.op_compile_option = json.dumps(op_compile_options) | ||
| 579 | + else: | ||
| 580 | + self.impl_mode = ", impl_mode ='" + op_compile_options["impl_mode"] + "'" | ||
| 581 | + self.impl_mode_op_info = ", impl_mode ='" + op_compile_options["impl_mode"] + "'" | ||
| 582 | + else: | ||
| 583 | + self.impl_mode = ', impl_mode = ""' | ||
| 584 | + self.impl_mode_op_info = ", impl_mode = impl_mode" | ||
| 585 | + | ||
| 586 | + def _write_impl(self: any, fd: object, impl_path: str = ""): | ||
| 587 | + argsdef = self._build_paralist() | ||
| 588 | + argsval = self._build_paralist(False) | ||
| 589 | + pchk = self._build_parachk() | ||
| 590 | + if len(self.kern_name) > 0: | ||
| 591 | + kern_name = self.kern_name | ||
| 592 | + else: | ||
| 593 | + kern_name = self.op_intf | ||
| 594 | + src = self.op_file + '.cpp' | ||
| 595 | + virt_exprs = self._build_virtual() | ||
| 596 | + fd.write(IMPL_API.format(self.op_type, pchk, \ | ||
| 597 | + self.op_intf, argsdef, kern_name, self.impl_mode, virt_exprs, argsval,\ | ||
| 598 | + self.custom_compile_options, self.custom_all_compile_options, self.op_intf,\ | ||
| 599 | + optype_snake_ex(self.op_type), optype_snake(self.op_type), src)) | ||
| 600 | + if self.op_replay_flag: | ||
| 601 | + fd.write(REPLAY_OP_API.format(self.op_type, kern_name, self.op_file,\ | ||
| 602 | + self.op_type, self.op_file, self.param_type_dynamic, self.op_compile_option)) | ||
| 603 | + else: | ||
| 604 | + value_depend_obj = {key: value for key, value in self.input_value_depend.items()} | ||
| 605 | + extend_opt = {"valueDepend": value_depend_obj} | ||
| 606 | + if os.environ.get('BUILD_BUILTIN_OPP') == '1': | ||
| 607 | + relative_kernel_src_path = os.path.realpath(self._get_kernel_source(impl_path, src,\ | ||
| 608 | + optype_snake(self.op_type), optype_snake_ex(self.op_type))) | ||
| 609 | + # to match src path in .dat file system, turn relative path into absolute path | ||
| 610 | + abs_rel_kernel_src_path = os.path.join("/", os.path.relpath(relative_kernel_src_path, impl_path)) | ||
| 611 | + | ||
| 612 | + # compiling hidden src file requires src path before packaging .dat file, | ||
| 613 | + # hard code such src path to <op_type>.py | ||
| 614 | + fd.write(COMPILE_OP_API_BUILT_IN.format(self.op_type, self.op_type,\ | ||
| 615 | + self.impl_mode_op_info, ', '.join(self.input_name), \ | ||
| 616 | + ', '.join(self.output_name), self.param_type_dynamic,\ | ||
| 617 | + self._build_mc2_ctx(), self.input_type + self.output_type, self.output_init_value,\ | ||
| 618 | + self.output_shape_depend_on_compute, self.op_compile_option, abs_rel_kernel_src_path, repr(extend_opt))) | ||
| 619 | + else: | ||
| 620 | + fd.write(COMPILE_OP_API.format(self.op_type, | ||
| 621 | + self.op_type, self.impl_mode_op_info, ', '.join(self.input_name), \ | ||
| 622 | + ', '.join(self.output_name), self.param_type_dynamic, self._build_mc2_ctx(),\ | ||
| 623 | + self.input_type + self.output_type, self.output_init_value, self.output_shape_depend_on_compute,\ | ||
| 624 | + self.op_compile_option, repr(extend_opt))) | ||
| 625 | + | ||
| 626 | + def _write_cap(self: any, cap_name: str, fd: object): | ||
| 627 | + argsdef = self._build_paralist() | ||
| 628 | + argsval = self._build_paralist(False) | ||
| 629 | + if cap_name == 'check_supported': | ||
| 630 | + fd.write(SUP_API.format(cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type)) | ||
| 631 | + else: | ||
| 632 | + fd.write(CAP_API.format(cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type)) | ||
| 633 | + | ||
| 634 | + def _write_glz(self: any, fd: object): | ||
| 635 | + argsdef = self._build_paralist() | ||
| 636 | + argsval = self._build_paralist(False) | ||
| 637 | + fd.write(GLZ_API.format(self.op_type, self.op_intf, argsdef, argsval, self.op_type)) | ||
| 638 | + | ||
| 639 | + | ||
| 640 | +def write_scripts(cfgfile: str, cfgs: dict, dirs: dict, ops: list = None, op_compile_option:list = None): | ||
| 641 | + batch_lists = cfgs.get(const_var.REPLAY_BATCH).split(';') | ||
| 642 | + iterator_lists = cfgs.get(const_var.REPLAY_ITERATE).split(';') | ||
| 643 | + file_map = {} | ||
| 644 | + op_descs = opdesc_parser.get_op_desc(cfgfile, batch_lists, iterator_lists, AdpBuilder,\ | ||
| 645 | + ops, dirs.get(const_var.AUTO_GEN_DIR)) | ||
| 646 | + for op_desc in op_descs: | ||
| 647 | + op_desc.write_adapt(dirs.get(const_var.CFG_IMPL_DIR), dirs.get(const_var.CFG_OUT_DIR), op_compile_option) | ||
| 648 | + file_map[op_desc.op_type] = op_desc.op_file | ||
| 649 | + return file_map | ||
| 650 | + | ||
| 651 | + | ||
| 652 | +class OpFileNotExistsError(Exception): | ||
| 653 | + """File does not exist error.""" | ||
| 654 | + def __str__(self) -> str: | ||
| 655 | + return f"File aic-*-ops-info.ini does not exist in directory {super().__str__()}" | ||
| 656 | + | ||
| 657 | + | ||
| 658 | +def get_ops_info_files(opsinfo_dir: List[str]) -> List[str]: | ||
| 659 | + """Get all ops info files.""" | ||
| 660 | + ops_info_files = [] | ||
| 661 | + for _dir in opsinfo_dir: | ||
| 662 | + ops_info_files.extend(glob.glob(f'{_dir}/aic-*-ops-info.ini')) | ||
| 663 | + return sorted(ops_info_files) | ||
| 664 | + | ||
| 665 | + | ||
| 666 | +def parse_args(argv): | ||
| 667 | + """Command line parameter parsing""" | ||
| 668 | + parser = argparse.ArgumentParser() | ||
| 669 | + parser.add_argument('argv', nargs='+') | ||
| 670 | + parser.add_argument('--opsinfo-dir', nargs='*', default=None) | ||
| 671 | + return parser.parse_args(argv) | ||
| 672 | + | ||
| 673 | + | ||
| 674 | +if __name__ == '__main__': | ||
| 675 | + args = parse_args(sys.argv) | ||
| 676 | + | ||
| 677 | + if len(args.argv) <= 6: | ||
| 678 | + raise RuntimeError('arguments must greater equal than 6') | ||
| 679 | + | ||
| 680 | + rep_cfg = {} | ||
| 681 | + rep_cfg[const_var.REPLAY_BATCH] = args.argv[2] | ||
| 682 | + rep_cfg[const_var.REPLAY_ITERATE] = args.argv[3] | ||
| 683 | + | ||
| 684 | + cfg_dir = {} | ||
| 685 | + cfg_dir[const_var.CFG_IMPL_DIR] = args.argv[4] | ||
| 686 | + cfg_dir[const_var.CFG_OUT_DIR] = args.argv[5] | ||
| 687 | + cfg_dir[const_var.AUTO_GEN_DIR] = args.argv[6] | ||
| 688 | + | ||
| 689 | + ops_infos = [] | ||
| 690 | + if args.opsinfo_dir: | ||
| 691 | + ops_infos.extend(get_ops_info_files(args.opsinfo_dir)) | ||
| 692 | + if not ops_infos: | ||
| 693 | + raise OpFileNotExistsError(args.opsinfo_dir) | ||
| 694 | + else: | ||
| 695 | + ops_infos.append(args.argv[1]) | ||
| 696 | + | ||
| 697 | + for ops_info in ops_infos: | ||
| 698 | + write_scripts(cfgfile=ops_info, cfgs=rep_cfg, dirs=cfg_dir) | ||
| @@ -0,0 +1,43 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 5 | +""" | ||
| 6 | + | ||
| 7 | +import sys | ||
| 8 | +import os | ||
| 9 | +import opdesc_parser | ||
| 10 | + | ||
| 11 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 12 | + | ||
| 13 | + | ||
| 14 | +class OpInfo: | ||
| 15 | + def __init__(self: any, op_type: str, cfg_file: str): | ||
| 16 | + op_descs = opdesc_parser.get_op_desc( | ||
| 17 | + cfg_file, [], [], opdesc_parser.OpDesc, [op_type] | ||
| 18 | + ) | ||
| 19 | + if op_descs is None or len(op_descs) != 1: | ||
| 20 | + raise RuntimeError("cannot get op info of {}".format(op_type)) | ||
| 21 | + self.op_desc = op_descs[0] | ||
| 22 | + | ||
| 23 | + def get_op_file(self: any): | ||
| 24 | + return self.op_desc.op_file | ||
| 25 | + | ||
| 26 | + def get_op_intf(self: any): | ||
| 27 | + return self.op_desc.op_intf | ||
| 28 | + | ||
| 29 | + def get_inputs_name(self: any): | ||
| 30 | + return self.op_desc.input_ori_name | ||
| 31 | + | ||
| 32 | + def get_outputs_name(self: any): | ||
| 33 | + return self.op_desc.output_ori_name | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +if __name__ == "__main__": | ||
| 37 | + if len(sys.argv) <= 2: | ||
| 38 | + raise RuntimeError("arguments must greater than 2") | ||
| 39 | + op_info = OpInfo(sys.argv[1], sys.argv[2]) | ||
| 40 | + print(op_info.get_op_file()) | ||
| 41 | + print(op_info.get_op_intf()) | ||
| 42 | + print(op_info.get_inputs_name()) | ||
| 43 | + print(op_info.get_outputs_name()) | ||
| @@ -0,0 +1,361 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2024. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import os | ||
| 9 | +import glob | ||
| 10 | +import json | ||
| 11 | +import sys | ||
| 12 | +import argparse | ||
| 13 | +from typing import NamedTuple, Dict | ||
| 14 | +import const_var | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +class OpConfig(NamedTuple): | ||
| 18 | + op_type: str | ||
| 19 | + support_info: Dict | ||
| 20 | + core_type: str | ||
| 21 | + task_ration: str | ||
| 22 | + obj_file: str | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +def load_json(json_file: str): | ||
| 26 | + with open(json_file, encoding='utf-8') as file: | ||
| 27 | + json_content = json.load(file) | ||
| 28 | + return json_content | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +def get_specified_suffix_file(root_dir, suffix): | ||
| 32 | + specified_suffix = os.path.join(root_dir, '**/*{}'.format(suffix)) | ||
| 33 | + all_suffix_files = glob.glob(specified_suffix, recursive=True) | ||
| 34 | + return sorted(all_suffix_files) | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +def add_dict_key(dict_to_add, key, value): | ||
| 38 | + if value is None: | ||
| 39 | + return | ||
| 40 | + dict_to_add[key] = value | ||
| 41 | + | ||
| 42 | + | ||
| 43 | +def correct_format_mode(format_mode): | ||
| 44 | + if format_mode == 'FormatDefault': | ||
| 45 | + return 'nd_agnostic' | ||
| 46 | + if format_mode == 'FormatAgnostic': | ||
| 47 | + return 'static_nd_agnostic' | ||
| 48 | + if format_mode == 'FormatFixed': | ||
| 49 | + return 'normal' | ||
| 50 | + return format_mode | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def get_input_or_output_config(in_or_out): | ||
| 54 | + param_dict = {} | ||
| 55 | + name = in_or_out.get('name') | ||
| 56 | + index = in_or_out.get('index') | ||
| 57 | + param_type = in_or_out.get('paramType') | ||
| 58 | + | ||
| 59 | + format_match_mode = in_or_out.get('format_match_mode') | ||
| 60 | + format_mode = correct_format_mode(format_match_mode) | ||
| 61 | + | ||
| 62 | + dtype_mode = in_or_out.get('dtype_match_mode') | ||
| 63 | + if dtype_mode == 'DtypeByte': | ||
| 64 | + dtype_mode = 'bit' | ||
| 65 | + | ||
| 66 | + add_dict_key(param_dict, 'name', name) | ||
| 67 | + add_dict_key(param_dict, 'index', index) | ||
| 68 | + add_dict_key(param_dict, 'paramType', param_type) | ||
| 69 | + add_dict_key(param_dict, 'dtypeMode', dtype_mode) | ||
| 70 | + add_dict_key(param_dict, 'formatMode', format_mode) | ||
| 71 | + return param_dict | ||
| 72 | + | ||
| 73 | + | ||
| 74 | +def get_inputs_or_outputs_config(inputs_or_outputs): | ||
| 75 | + if inputs_or_outputs is None: | ||
| 76 | + return None | ||
| 77 | + inputs_or_outputs_list = [] | ||
| 78 | + | ||
| 79 | + for in_or_out in inputs_or_outputs: | ||
| 80 | + if isinstance(in_or_out, dict): | ||
| 81 | + dict_param_config = get_input_or_output_config(in_or_out) | ||
| 82 | + inputs_or_outputs_list.append(dict_param_config) | ||
| 83 | + elif isinstance(in_or_out, list): | ||
| 84 | + param_info = in_or_out[0] | ||
| 85 | + list_param_config = get_input_or_output_config(param_info) | ||
| 86 | + tmp_list = [list_param_config] | ||
| 87 | + inputs_or_outputs_list.append(tmp_list) | ||
| 88 | + return inputs_or_outputs_list | ||
| 89 | + | ||
| 90 | + | ||
| 91 | +def gen_attrs_config(attrs): | ||
| 92 | + attrs_list = [] | ||
| 93 | + for attr in attrs: | ||
| 94 | + attrs_dict = {} | ||
| 95 | + name = attr.get('name') | ||
| 96 | + mode = attr.get('mode') | ||
| 97 | + add_dict_key(attrs_dict, 'name', name) | ||
| 98 | + add_dict_key(attrs_dict, 'mode', mode) | ||
| 99 | + attrs_list.append(attrs_dict) | ||
| 100 | + return attrs_list | ||
| 101 | + | ||
| 102 | + | ||
| 103 | +def get_params_config(support_info): | ||
| 104 | + params_dict = {} | ||
| 105 | + | ||
| 106 | + inputs = support_info.get('inputs') | ||
| 107 | + inputs_list = get_inputs_or_outputs_config(inputs) | ||
| 108 | + params_dict['inputs'] = inputs_list | ||
| 109 | + | ||
| 110 | + outputs = support_info.get('outputs') | ||
| 111 | + outputs_list = get_inputs_or_outputs_config(outputs) | ||
| 112 | + params_dict['outputs'] = outputs_list | ||
| 113 | + | ||
| 114 | + attrs = support_info.get('attrs') | ||
| 115 | + if attrs is not None: | ||
| 116 | + attrs_list = gen_attrs_config(attrs) | ||
| 117 | + params_dict['attrs'] = attrs_list | ||
| 118 | + | ||
| 119 | + return params_dict | ||
| 120 | + | ||
| 121 | + | ||
| 122 | +def add_simplified_config(op_info, binary_info_config, config): | ||
| 123 | + simplified_key = op_info.support_info.get('simplifiedKey') | ||
| 124 | + | ||
| 125 | + json_path = op_info.obj_file.split('.')[0] + '.json' | ||
| 126 | + | ||
| 127 | + simple_cfg = config.get(binary_info_config) | ||
| 128 | + op_cfg = simple_cfg.get(op_info.op_type) | ||
| 129 | + if not op_cfg: | ||
| 130 | + op_cfg = {'dynamicRankSupport': True} | ||
| 131 | + | ||
| 132 | + simplified_key_mode = op_info.support_info.get('simplifiedKeyMode') | ||
| 133 | + add_dict_key(op_cfg, 'simplifiedKeyMode', simplified_key_mode) | ||
| 134 | + | ||
| 135 | + optional_input_mode = op_info.support_info.get('optionalInputMode') | ||
| 136 | + optional_output_mode = op_info.support_info.get('optionalOutputMode') | ||
| 137 | + add_dict_key(op_cfg, 'optionalInputMode', optional_input_mode) | ||
| 138 | + if optional_output_mode is not None: | ||
| 139 | + add_dict_key(op_cfg, 'optionalOutputMode', optional_output_mode) | ||
| 140 | + | ||
| 141 | + params_info = get_params_config(op_info.support_info) | ||
| 142 | + op_cfg['params'] = params_info | ||
| 143 | + op_cfg['binaryList'] = [] | ||
| 144 | + simple_cfg[op_info.op_type] = op_cfg | ||
| 145 | + | ||
| 146 | + bin_list = op_cfg.get('binaryList') | ||
| 147 | + if op_info.core_type == 0 and op_info.task_ration == "tilingKey": | ||
| 148 | + bin_list.append({'coreType': op_info.core_type, 'simplifiedKey': simplified_key, | ||
| 149 | + 'multiKernelType': 1, 'binPath': op_info.obj_file, 'jsonPath': json_path}) | ||
| 150 | + else: | ||
| 151 | + bin_list.append({'coreType': op_info.core_type, 'simplifiedKey': simplified_key, | ||
| 152 | + 'binPath': op_info.obj_file, 'jsonPath': json_path}) | ||
| 153 | + | ||
| 154 | + | ||
| 155 | +def add_op_config(op_file, bin_info, config): | ||
| 156 | + op_cfg = config.get(op_file) | ||
| 157 | + if not op_cfg: | ||
| 158 | + op_cfg = {'binList': []} | ||
| 159 | + config[op_file] = op_cfg | ||
| 160 | + op_cfg.get('binList').append(bin_info) | ||
| 161 | + | ||
| 162 | + | ||
| 163 | +def gen_ops_config(json_file, soc, binary_info_config, config): | ||
| 164 | + core_type_map = {'MIX': 0, 'AiCore': 1, 'VectorCore': 2, 'MIX_AICORE': 3, 'MIX_VECTOR_CORE': 4, 'MIX_AIV': 4} | ||
| 165 | + contents = load_json(json_file) | ||
| 166 | + if ('binFileName' not in contents) or ('supportInfo' not in contents): | ||
| 167 | + return | ||
| 168 | + json_base_name = os.path.basename(json_file) | ||
| 169 | + op_dir = os.path.basename(os.path.dirname(json_file)) | ||
| 170 | + | ||
| 171 | + support_info = contents.get('supportInfo') | ||
| 172 | + bin_name = contents.get('binFileName') | ||
| 173 | + bin_suffix = contents.get('binFileSuffix') | ||
| 174 | + core_type = contents.get("coreType") | ||
| 175 | + task_ration = contents.get("taskRation") | ||
| 176 | + core_type = core_type_map.get(core_type, -1) | ||
| 177 | + if core_type == -1 and soc != 'ascend310b': | ||
| 178 | + raise Exception("[ERROR]: must set coreType in json when soc version is {soc}.") | ||
| 179 | + | ||
| 180 | + bin_file_name = bin_name + bin_suffix | ||
| 181 | + op_type = bin_name.split('_')[0] | ||
| 182 | + op_file = op_dir + '.json' | ||
| 183 | + bin_info = {} | ||
| 184 | + | ||
| 185 | + add_dict_key(bin_info, 'implMode', support_info.get('implMode')) | ||
| 186 | + add_dict_key(bin_info, 'int64Mode', support_info.get('int64Mode')) | ||
| 187 | + add_dict_key(bin_info, 'simplifiedKeyMode', support_info.get('simplifiedKeyMode')) | ||
| 188 | + | ||
| 189 | + simplified_key = support_info.get('simplifiedKey') | ||
| 190 | + if simplified_key is not None: | ||
| 191 | + bin_info['simplifiedKey'] = simplified_key | ||
| 192 | + obj_file = os.path.join(soc, op_dir, bin_file_name) | ||
| 193 | + op_info = OpConfig( | ||
| 194 | + op_type=op_type, | ||
| 195 | + support_info=support_info, | ||
| 196 | + core_type=core_type, | ||
| 197 | + task_ration=task_ration, | ||
| 198 | + obj_file=obj_file, | ||
| 199 | + ) | ||
| 200 | + add_simplified_config(op_info, binary_info_config, config) | ||
| 201 | + | ||
| 202 | + add_dict_key(bin_info, 'dynamicParamMode', support_info.get('dynamicParamMode')) | ||
| 203 | + bin_info['staticKey'] = support_info.get('staticKey') | ||
| 204 | + bin_info['inputs'] = support_info.get('inputs') | ||
| 205 | + bin_info['outputs'] = support_info.get('outputs') | ||
| 206 | + if support_info.get('attrs'): | ||
| 207 | + bin_info['attrs'] = support_info.get('attrs') | ||
| 208 | + | ||
| 209 | + add_dict_key(bin_info, 'opMode', support_info.get('opMode')) | ||
| 210 | + add_dict_key(bin_info, 'optionalInputMode', support_info.get('optionalInputMode')) | ||
| 211 | + add_dict_key(bin_info, 'deterministic', support_info.get('deterministic')) | ||
| 212 | + if support_info.get('optionalOutputMode') is not None: | ||
| 213 | + add_dict_key(bin_info, 'optionalOutputMode', support_info.get('optionalOutputMode')) | ||
| 214 | + | ||
| 215 | + bin_info['binInfo'] = {'jsonFilePath': os.path.join(soc, op_dir, json_base_name)} | ||
| 216 | + add_op_config(op_file, bin_info, config) | ||
| 217 | + | ||
| 218 | + | ||
| 219 | +def check_single_op_is_void(root_dir): | ||
| 220 | + for root, dirs, _ in os.walk(root_dir): | ||
| 221 | + for sub_dir in dirs: | ||
| 222 | + dir_path = os.path.join(root, sub_dir) | ||
| 223 | + if len(os.listdir(dir_path)) == 0: | ||
| 224 | + print(f"[ERROR] op {sub_dir}: not any obj compile success") | ||
| 225 | + sys.exit(1) | ||
| 226 | + | ||
| 227 | + | ||
| 228 | +def write_jsons(out_dir, file_list, config): | ||
| 229 | + for json_name in file_list: | ||
| 230 | + json_file = os.path.join(out_dir, json_name) | ||
| 231 | + with os.fdopen(os.open(json_file, const_var.WFLAGS, const_var.WMODES), 'w') as fd: | ||
| 232 | + json.dump(config.get(json_name), fd, indent=' ') | ||
| 233 | + | ||
| 234 | + | ||
| 235 | +def generate_operator_cfg_file(json_files, | ||
| 236 | + binary_info_config, | ||
| 237 | + soc, | ||
| 238 | + out_dir, | ||
| 239 | + gen_json_status): | ||
| 240 | + | ||
| 241 | + if not json_files: | ||
| 242 | + return | ||
| 243 | + | ||
| 244 | + if gen_json_status == "not_generated": | ||
| 245 | + return | ||
| 246 | + | ||
| 247 | + json_files.sort() | ||
| 248 | + config = {binary_info_config: {}} | ||
| 249 | + for _json in json_files: | ||
| 250 | + gen_ops_config(_json, soc, binary_info_config, config) | ||
| 251 | + | ||
| 252 | + if gen_json_status == "single_json": | ||
| 253 | + file_list = [json_file for json_file in config.keys() if json_file != binary_info_config] | ||
| 254 | + elif gen_json_status == "summary_json": | ||
| 255 | + file_list = [binary_info_config] | ||
| 256 | + else: | ||
| 257 | + file_list = config.keys() | ||
| 258 | + | ||
| 259 | + write_jsons(out_dir, file_list, config) | ||
| 260 | + | ||
| 261 | + | ||
| 262 | +def gen_all_config(root_dir, soc, out_dir, | ||
| 263 | + skip_binary_info_config, op_range="all"): | ||
| 264 | + if op_range != "relocatable": | ||
| 265 | + check_single_op_is_void(root_dir) | ||
| 266 | + all_json_files = get_specified_suffix_file(root_dir, '.json') | ||
| 267 | + relocatable_json_files = get_specified_suffix_file(root_dir, '_relocatable.json') | ||
| 268 | + normal_json_files = list(set(all_json_files) - set(relocatable_json_files)) | ||
| 269 | + os.makedirs(out_dir, exist_ok=True) | ||
| 270 | + | ||
| 271 | + if op_range != "relocatable": | ||
| 272 | + for _json in all_json_files: | ||
| 273 | + file_path = soc + _json.split(soc, maxsplit=1)[1] | ||
| 274 | + with open(_json, "r+") as f: | ||
| 275 | + data = json.load(f) | ||
| 276 | + data["filePath"] = file_path | ||
| 277 | + f.seek(0) | ||
| 278 | + json.dump(data, f, indent=" ") | ||
| 279 | + f.truncate() | ||
| 280 | + | ||
| 281 | + if skip_binary_info_config: | ||
| 282 | + gen_normale_json = "single_json" | ||
| 283 | + gen_relocatable_json = "not_generated" | ||
| 284 | + else: | ||
| 285 | + gen_normale_json = "all_json" | ||
| 286 | + gen_relocatable_json = "summary_json" | ||
| 287 | + | ||
| 288 | + # normal kernel | ||
| 289 | + if op_range == "all" or op_range == "normal": | ||
| 290 | + binary_info_config = "binary_info_config.json" | ||
| 291 | + generate_operator_cfg_file(normal_json_files, binary_info_config, | ||
| 292 | + soc, out_dir, gen_normale_json) | ||
| 293 | + | ||
| 294 | + # relocatable kernel | ||
| 295 | + if op_range == "all" or op_range == "relocatable": | ||
| 296 | + binary_info_config = "relocatable_kernel_info_config.json" | ||
| 297 | + generate_operator_cfg_file(relocatable_json_files, binary_info_config, | ||
| 298 | + soc, out_dir, gen_relocatable_json) | ||
| 299 | + | ||
| 300 | + | ||
| 301 | +# Parse multiple soc_versions ops in single path. | ||
| 302 | +def gen_all_soc_config(all_path): | ||
| 303 | + soc_roots = glob.glob(os.path.join(all_path, "ascend*")) | ||
| 304 | + | ||
| 305 | + for soc_root in soc_roots: | ||
| 306 | + soc = os.path.basename(soc_root) | ||
| 307 | + gen_all_config(soc_root, soc, soc_root, True) | ||
| 308 | + cfg_files = glob.glob(os.path.join(soc_root, "*.json")) | ||
| 309 | + cfg_path = os.path.join(all_path, "config", soc) | ||
| 310 | + os.makedirs(cfg_path, exist_ok=True) | ||
| 311 | + for cfg_file in cfg_files: | ||
| 312 | + new_file = os.path.join(cfg_path, os.path.basename(cfg_file)) | ||
| 313 | + os.rename(cfg_file, new_file) | ||
| 314 | + | ||
| 315 | + | ||
| 316 | +def args_prase(): | ||
| 317 | + parser = argparse.ArgumentParser() | ||
| 318 | + parser.add_argument('-p', | ||
| 319 | + '--path', | ||
| 320 | + nargs='?', | ||
| 321 | + required=True, | ||
| 322 | + help='Parse the path of the json file.') | ||
| 323 | + | ||
| 324 | + parser.add_argument('-s', | ||
| 325 | + '--soc', | ||
| 326 | + nargs='?', | ||
| 327 | + required=True, | ||
| 328 | + help='Parse the soc_version of ops.') | ||
| 329 | + | ||
| 330 | + parser.add_argument('-o', | ||
| 331 | + '--out', | ||
| 332 | + nargs='?', | ||
| 333 | + help='Output directory.') | ||
| 334 | + | ||
| 335 | + parser.add_argument('--skip-binary-info-config', | ||
| 336 | + action='store_true', | ||
| 337 | + help='binary_info_config.json file is not parsed.') | ||
| 338 | + | ||
| 339 | + parser.add_argument('--op-range', | ||
| 340 | + type=str, | ||
| 341 | + choices=["all", "normal", "relocatable"], | ||
| 342 | + default='all', | ||
| 343 | + help='all operators/normal operators/relocatable operators.') | ||
| 344 | + | ||
| 345 | + return parser.parse_args() | ||
| 346 | + | ||
| 347 | + | ||
| 348 | +def main(): | ||
| 349 | + args = args_prase() | ||
| 350 | + if args.out is None: | ||
| 351 | + out_dir = args.path | ||
| 352 | + else: | ||
| 353 | + out_dir = args.out | ||
| 354 | + | ||
| 355 | + gen_all_config(args.path, args.soc, out_dir, | ||
| 356 | + args.skip_binary_info_config, args.op_range) | ||
| 357 | + | ||
| 358 | + | ||
| 359 | +if __name__ == '__main__': | ||
| 360 | + main() | ||
| 361 | + | ||
| @@ -0,0 +1,214 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 4 | + | ||
| 5 | +import os | ||
| 6 | +import sys | ||
| 7 | +import subprocess | ||
| 8 | +import json | ||
| 9 | +import glob | ||
| 10 | +import argparse | ||
| 11 | +import math | ||
| 12 | +import const_var | ||
| 13 | +import ascendc_ops_config | ||
| 14 | +from tbe.tikcpp.log_utils import LogUtil, AscendCLogLevel | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +class PackKernel: | ||
| 18 | + def __init__(self: any, args: any): | ||
| 19 | + self.in_path = os.path.realpath(args.input_path) | ||
| 20 | + self.out_path = os.path.realpath(args.output_path) | ||
| 21 | + self.is_lib = args.enable_library | ||
| 22 | + self.platform = args.platform | ||
| 23 | + self.op_info = {} | ||
| 24 | + self.file_info = {} | ||
| 25 | + try: | ||
| 26 | + os.makedirs(self.out_path, exist_ok=True) | ||
| 27 | + except Exception as e: | ||
| 28 | + LogUtil.print_compile_log("", f"make {self.out_path} error: {e}!", | ||
| 29 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 30 | + | ||
| 31 | + def load_json(self: any, json_file: str): | ||
| 32 | + with open(json_file, encoding="utf-8") as file: | ||
| 33 | + json_content = json.load(file) | ||
| 34 | + return json_content | ||
| 35 | + | ||
| 36 | + def get_symbol(self: any, name: str): | ||
| 37 | + name = name.replace("/", "_") | ||
| 38 | + return name.replace(".", "_") | ||
| 39 | + | ||
| 40 | + def ascendc_gen_object(self: any, in_file: str, soc: str): | ||
| 41 | + sym = self.get_symbol("_binary_" + in_file) | ||
| 42 | + out_file = os.path.join(self.out_path, sym + ".o") | ||
| 43 | + #ascend610lite only supoort aarch64 | ||
| 44 | + if soc == 'ascend610lite': | ||
| 45 | + try: | ||
| 46 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64', | ||
| 47 | + '--binary-architecture', 'aarch64', in_file, out_file]) | ||
| 48 | + except Exception as e: | ||
| 49 | + LogUtil.print_compile_log("", " ascend610lite execute objcopy fail!", | ||
| 50 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 51 | + return None | ||
| 52 | + return [sym + "_start", sym + "_end"] | ||
| 53 | + uname = os.popen("uname -m").read().strip() | ||
| 54 | + if self.platform is not None: | ||
| 55 | + target_platform = self.platform | ||
| 56 | + else: | ||
| 57 | + target_platform = uname | ||
| 58 | + try: | ||
| 59 | + if target_platform == "x86_64": | ||
| 60 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-x86-64', | ||
| 61 | + '--binary-architecture', 'i386', in_file, out_file]) | ||
| 62 | + elif target_platform == "aarch64": | ||
| 63 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64', | ||
| 64 | + '--binary-architecture', 'aarch64', in_file, out_file]) | ||
| 65 | + else: | ||
| 66 | + subprocess.run(['echo', 'unsported environment!']) | ||
| 67 | + except Exception as e: | ||
| 68 | + LogUtil.print_compile_log("", f"{target_platform} execute objcopy error: {e}!", | ||
| 69 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 70 | + return None | ||
| 71 | + return [sym + "_start", sym + "_end"] | ||
| 72 | + | ||
| 73 | + def ascendc_get_config(self: any): | ||
| 74 | + os.chdir(self.in_path) | ||
| 75 | + soc_vers = os.listdir("config") | ||
| 76 | + for soc in soc_vers: | ||
| 77 | + bin_infos = glob.glob(os.path.join("config", soc, "*.json")) | ||
| 78 | + cfgs = {} | ||
| 79 | + for bin_info in bin_infos: | ||
| 80 | + if bin_info.find("binary_info_config.json") > 0: | ||
| 81 | + continue | ||
| 82 | + jobj = self.load_json(bin_info) | ||
| 83 | + for bin_cfg in jobj.get("binList"): | ||
| 84 | + js_cfg = bin_cfg.get("binInfo").get("jsonFilePath") | ||
| 85 | + op_type = os.path.basename(js_cfg).split("_")[0] | ||
| 86 | + if cfgs.get(op_type) is None: | ||
| 87 | + op_obj = {} | ||
| 88 | + op_obj["obj"] = [] | ||
| 89 | + op_obj["cfg"] = bin_info | ||
| 90 | + cfgs[op_type] = op_obj | ||
| 91 | + op_obj = cfgs.get(op_type) | ||
| 92 | + op_obj.get("obj").append(js_cfg[:-5]) | ||
| 93 | + self.file_info[soc] = cfgs | ||
| 94 | + | ||
| 95 | + def ascendc_pack_kernel(self: any): | ||
| 96 | + for soc in self.file_info.keys(): | ||
| 97 | + os.chdir(self.in_path) | ||
| 98 | + op_cfgs = self.file_info.get(soc) | ||
| 99 | + for op_type in op_cfgs.keys(): | ||
| 100 | + op_obj = op_cfgs.get(op_type) | ||
| 101 | + if self.op_info.get(op_type) is None: | ||
| 102 | + op_info = {} | ||
| 103 | + op_info["op_fun"] = ["nullptr", "nullptr"] | ||
| 104 | + op_info["op_bin"] = {} | ||
| 105 | + op_info["op_rkb"] = [] | ||
| 106 | + self.op_info[op_type] = op_info | ||
| 107 | + op_info = self.op_info.get(op_type) | ||
| 108 | + op_bin = op_info.get("op_bin") | ||
| 109 | + if op_bin.get(soc) is None: | ||
| 110 | + op_bin[soc] = [] | ||
| 111 | + op_bin[soc].append(self.ascendc_gen_object(op_obj["cfg"], soc)) | ||
| 112 | + op_soc = op_bin.get(soc) | ||
| 113 | + for objs in op_obj["obj"]: | ||
| 114 | + op_soc.append(self.ascendc_gen_object(objs + ".json", soc)) | ||
| 115 | + op_soc.append(self.ascendc_gen_object(objs + ".o", soc)) | ||
| 116 | + | ||
| 117 | + def ascendc_gen_header(self: any): | ||
| 118 | + for op_type in self.op_info.keys(): | ||
| 119 | + op_obj = self.op_info.get(op_type) | ||
| 120 | + macro_op = "#define {}_OP_RESOURCES std::make_tuple<std::vector<void *>, \\\n" \ | ||
| 121 | + " std::map<ge::AscendString, std::vector<std::tuple<const uint8_t *, const uint8_t *>>>, \\\n" \ | ||
| 122 | + " std::vector<std::tuple<const uint8_t *, const uint8_t *>>>({{{}}}, \\\n".format( | ||
| 123 | + op_type, ", ".join(op_obj.get("op_fun")) | ||
| 124 | + ) | ||
| 125 | + op_bin = op_obj.get("op_bin") | ||
| 126 | + socs_res = [] | ||
| 127 | + op_syms = [] | ||
| 128 | + for soc in op_bin.keys(): | ||
| 129 | + soc_res = '{{ "{}", {{'.format(soc) | ||
| 130 | + soc_syms = op_bin.get(soc) | ||
| 131 | + soc_pairs = [] | ||
| 132 | + for pair_addr in soc_syms: | ||
| 133 | + pair_addr1 = ["&" + s for s in pair_addr] | ||
| 134 | + op_syms += pair_addr | ||
| 135 | + soc_pairs.append( | ||
| 136 | + " {{ {} }} ".format(", \\\n ".join(pair_addr1)) | ||
| 137 | + ) | ||
| 138 | + soc_res += ", \\\n ".join(soc_pairs) | ||
| 139 | + soc_res += " } }" | ||
| 140 | + socs_res.append(soc_res) | ||
| 141 | + macro_op += " {{ {} }}, \\\n".format(", \\\n ".join(socs_res)) | ||
| 142 | + macro_op += " {{ {} }})\n\n".format(", ".join(op_obj.get("op_rkb"))) | ||
| 143 | + macro_str = '#define {}_RESOURCES {{{{"{}", {}}}}}'.format( | ||
| 144 | + op_type, op_type, "{}_OP_RESOURCES".format(op_type) | ||
| 145 | + ) | ||
| 146 | + var_str = ("extern gert::OpImplRegisterV2 op_impl_register_optiling_{};\n".format(op_type)) | ||
| 147 | + if len(op_syms) > 0: | ||
| 148 | + var_str += ('extern uint8_t ' + ";\nextern uint8_t ".join(op_syms) + ";\n") | ||
| 149 | + head_file = os.path.join(self.out_path, "{}_op_resource.h".format(op_type)) | ||
| 150 | + try: | ||
| 151 | + with os.fdopen( | ||
| 152 | + os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w" | ||
| 153 | + ) as fd: | ||
| 154 | + fd.write("#include <stdint.h>\n") | ||
| 155 | + fd.write("#include <map>\n") | ||
| 156 | + fd.write("#include <tuple>\n") | ||
| 157 | + fd.write("#include <vector>\n") | ||
| 158 | + fd.write('#include "graph/ascend_string.h"\n') | ||
| 159 | + fd.write('#include "register/op_impl_registry.h"\n\n') | ||
| 160 | + fd.write(var_str) | ||
| 161 | + fd.write('\n') | ||
| 162 | + fd.write(macro_op) | ||
| 163 | + fd.write(macro_str) | ||
| 164 | + except Exception as e: | ||
| 165 | + LogUtil.print_compile_log("", f"{op_type}_op_resource.h create error: {e}!", | ||
| 166 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 167 | + | ||
| 168 | + def ascendc_gen_lib(self: any): | ||
| 169 | + out_lib = os.path.join(self.out_path, "libkernels.a") | ||
| 170 | + if os.path.exists(out_lib): | ||
| 171 | + os.remove(out_lib) | ||
| 172 | + objs = glob.glob(os.path.join(self.out_path, "*.o")) | ||
| 173 | + start = 0 | ||
| 174 | + batch_size = 100 | ||
| 175 | + for _ in range(math.ceil(len(objs) / batch_size)): | ||
| 176 | + sub_objs = objs[start : start + batch_size] | ||
| 177 | + start += batch_size | ||
| 178 | + try: | ||
| 179 | + subprocess.run(['ar', 'qc', out_lib] + sub_objs) | ||
| 180 | + subprocess.run(['ranlib', out_lib]) | ||
| 181 | + except Exception as e: | ||
| 182 | + LogUtil.print_compile_log("", f"execute ar/ranlib command error: {e}!", | ||
| 183 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 184 | + | ||
| 185 | + def ascendc_gen_opsinfo(self: any): | ||
| 186 | + ascendc_ops_config.gen_all_soc_config(self.in_path) | ||
| 187 | + | ||
| 188 | + | ||
| 189 | +def args_parse(): | ||
| 190 | + parser = argparse.ArgumentParser() | ||
| 191 | + parser.add_argument( | ||
| 192 | + "-i", "--input-path", nargs="?", help="Input path of compile result." | ||
| 193 | + ) | ||
| 194 | + parser.add_argument( | ||
| 195 | + "-o", "--output-path", nargs="?", help="Output path of compile result." | ||
| 196 | + ) | ||
| 197 | + parser.add_argument( | ||
| 198 | + "-l", "--enable-library", nargs="?", default=None, help="Whether library is enabled." | ||
| 199 | + ) | ||
| 200 | + parser.add_argument( | ||
| 201 | + "-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64." | ||
| 202 | + ) | ||
| 203 | + return parser.parse_args() | ||
| 204 | + | ||
| 205 | + | ||
| 206 | +if __name__ == "__main__": | ||
| 207 | + args = args_parse() | ||
| 208 | + kernel_packer = PackKernel(args) | ||
| 209 | + if kernel_packer.is_lib is None: | ||
| 210 | + kernel_packer.ascendc_gen_opsinfo() | ||
| 211 | + kernel_packer.ascendc_get_config() | ||
| 212 | + kernel_packer.ascendc_pack_kernel() | ||
| 213 | + kernel_packer.ascendc_gen_header() | ||
| 214 | + kernel_packer.ascendc_gen_lib() | ||
| @@ -0,0 +1,326 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. | ||
| 4 | + | ||
| 5 | +import os | ||
| 6 | +import sys | ||
| 7 | +import subprocess | ||
| 8 | +import glob | ||
| 9 | +import argparse | ||
| 10 | +import math | ||
| 11 | +import shutil | ||
| 12 | +import const_var | ||
| 13 | +from tbe.tikcpp.log_utils import LogUtil, AscendCLogLevel | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +class PackKernel: | ||
| 17 | + def __init__(self: any, args: any): | ||
| 18 | + self.registry_path = os.path.realpath(args.registry_file_path) | ||
| 19 | + self.in_path = os.path.realpath(args.input_path) | ||
| 20 | + self.base_path = os.path.realpath(args.base_path) | ||
| 21 | + self.copy_path = os.path.realpath(args.base_path + args.vendor_name) | ||
| 22 | + self.out_path = os.path.realpath(args.output_path) | ||
| 23 | + self.op_soc_ver = args.compute_unit.split("-") | ||
| 24 | + self.vendor_name = args.vendor_name | ||
| 25 | + self.framework_type = args.framework_type | ||
| 26 | + self.platform = args.platform | ||
| 27 | + self.op_info = {} | ||
| 28 | + self.file_info = {} | ||
| 29 | + if (os.path.exists(self.copy_path)): | ||
| 30 | + try: | ||
| 31 | + shutil.rmtree(self.copy_path) | ||
| 32 | + except OSError as e: | ||
| 33 | + LogUtil.print_compile_log("", f"remove {self.copy_path} error!", | ||
| 34 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 35 | + if (os.path.exists(self.out_path)): | ||
| 36 | + try: | ||
| 37 | + shutil.rmtree(self.out_path) | ||
| 38 | + except OSError as e: | ||
| 39 | + LogUtil.print_compile_log("", f"remove {self.out_path} error!", | ||
| 40 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 41 | + try: | ||
| 42 | + os.makedirs(self.copy_path, exist_ok=True) | ||
| 43 | + except Exception as e: | ||
| 44 | + LogUtil.print_compile_log("", f"make {self.copy_path} error: {e}!", | ||
| 45 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 46 | + try: | ||
| 47 | + os.makedirs(self.out_path, exist_ok=True) | ||
| 48 | + except Exception as e: | ||
| 49 | + LogUtil.print_compile_log("", f"make {self.out_path} error: {e}!", | ||
| 50 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 51 | + | ||
| 52 | + def get_symbol(self: any, name: str): | ||
| 53 | + name = name.replace("/", "_") | ||
| 54 | + name = name.replace("-", "_") | ||
| 55 | + return name.replace(".", "_") | ||
| 56 | + | ||
| 57 | + def ascendc_gen_object(self: any, in_file: str, path: str, vname: str): | ||
| 58 | + in_file = vname + "/" + in_file | ||
| 59 | + path = vname + "/" + path | ||
| 60 | + sym = self.get_symbol("_binary_" + in_file) | ||
| 61 | + out_file = os.path.join(self.out_path, sym + ".o") | ||
| 62 | + #ascend610lite only supoort aarch64 | ||
| 63 | + if path.find("ascend610lite") != -1: | ||
| 64 | + try: | ||
| 65 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64', | ||
| 66 | + '--binary-architecture', 'aarch64', in_file, out_file]) | ||
| 67 | + except Exception as e: | ||
| 68 | + LogUtil.print_compile_log("", " ascend610lite execute objcopy fail!", | ||
| 69 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 70 | + return None | ||
| 71 | + return [sym + "_start", sym + "_end"] | ||
| 72 | + | ||
| 73 | + uname = os.popen("uname -m").read().strip() | ||
| 74 | + if self.platform is not None: | ||
| 75 | + target_platform = self.platform | ||
| 76 | + else: | ||
| 77 | + target_platform = uname | ||
| 78 | + try: | ||
| 79 | + if target_platform == "x86_64": | ||
| 80 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-x86-64', | ||
| 81 | + '--binary-architecture', 'i386', in_file, out_file]) | ||
| 82 | + elif target_platform == "aarch64": | ||
| 83 | + subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64', | ||
| 84 | + '--binary-architecture', 'aarch64', in_file, out_file]) | ||
| 85 | + else: | ||
| 86 | + subprocess.run(['echo', 'unsupported environment!']) | ||
| 87 | + except Exception as e: | ||
| 88 | + LogUtil.print_compile_log("", f"{target_platform} execute objcopy error: {e}!", | ||
| 89 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 90 | + return None | ||
| 91 | + return [sym + "_start", sym + "_end"] | ||
| 92 | + | ||
| 93 | + def ascendc_get_config(self: any): | ||
| 94 | + os.chdir(self.copy_path) | ||
| 95 | + current_directory = os.getcwd() | ||
| 96 | + catalog_file = os.listdir(current_directory) | ||
| 97 | + for catalog in catalog_file: | ||
| 98 | + if catalog == "op_impl" or catalog == "framework": | ||
| 99 | + files_dict = {} | ||
| 100 | + for root, _, files in os.walk(catalog): | ||
| 101 | + for file in files: | ||
| 102 | + if (file.endswith(".json") or file.endswith(".so") or | ||
| 103 | + file.endswith(".cpp") or file.endswith(".py") or | ||
| 104 | + file.endswith(".o")): | ||
| 105 | + file_path = os.path.join(root, file) | ||
| 106 | + file_name = os.path.basename(file_path) | ||
| 107 | + files_dict[file_name] = file_path | ||
| 108 | + self.file_info[catalog] = files_dict | ||
| 109 | + | ||
| 110 | + def ascendc_pack_kernel(self: any): | ||
| 111 | + op_info = {} | ||
| 112 | + for files in self.file_info.keys(): | ||
| 113 | + os.chdir(self.base_path) | ||
| 114 | + op_cfgs = self.file_info.get(files) | ||
| 115 | + for file_name in op_cfgs.keys(): | ||
| 116 | + op_info[file_name] = [] | ||
| 117 | + path, filename = os.path.split(op_cfgs[file_name]) | ||
| 118 | + op_info[file_name].append(os.path.join(self.vendor_name, path)) | ||
| 119 | + op_info[file_name].append(self.ascendc_gen_object(op_cfgs[file_name], path, self.vendor_name)) | ||
| 120 | + self.op_info = op_info | ||
| 121 | + | ||
| 122 | + def ascendc_gen_register(self, macro_op, var_str): | ||
| 123 | + registry_file = os.path.join(self.registry_path, "custom_op_registry_V2.cpp") | ||
| 124 | + try: | ||
| 125 | + with os.fdopen( | ||
| 126 | + os.open(registry_file, const_var.WFLAGS, const_var.WMODES), "w" | ||
| 127 | + ) as fd: | ||
| 128 | + fd.write("#include <stdint.h>\n") | ||
| 129 | + fd.write("#include <map>\n") | ||
| 130 | + fd.write("#include <tuple>\n") | ||
| 131 | + fd.write("#include <vector>\n") | ||
| 132 | + fd.write('#include "graph/ascend_string.h"\n') | ||
| 133 | + fd.write('#include "register/op_bin_info.h"\n') | ||
| 134 | + fd.write('#include "register/op_lib_register.h"\n') | ||
| 135 | + fd.write('#include <dlfcn.h>\n') | ||
| 136 | + fd.write('#include "base/alog_pub.h"\n\n') | ||
| 137 | + fd.write(var_str) | ||
| 138 | + fd.write('\n') | ||
| 139 | + fd.write("#define ASCENDC_MODULE_NAME static_cast<int32_t>(ASCENDCKERNEL)\n") | ||
| 140 | + fd.write("#define LOG_ERROR(format, ...) \ \n") | ||
| 141 | + fd.write(" do { \ \n") | ||
| 142 | + fd.write(" if (AlogCheckDebugLevel(ASCENDC_MODULE_NAME, DLOG_ERROR) == 1) { \ \n") | ||
| 143 | + fd.write(" AlogRecord(ASCENDC_MODULE_NAME, DLOG_TYPE_DEBUG, DLOG_ERROR, ") | ||
| 144 | + fd.write("\"[%s] \" format \"\\n\", __FUNCTION__, ##__VA_ARGS__); \ \n") | ||
| 145 | + fd.write(" } \ \n") | ||
| 146 | + fd.write(" } while (0)\n") | ||
| 147 | + fd.write("namespace {\n") | ||
| 148 | + fd.write("uint32_t OpLibInitFunc(ge::AscendString& op_lib_path) {\n") | ||
| 149 | + fd.write(" static " + macro_op) | ||
| 150 | + fd.write(" static ops::OpBinInfo g_binInfo(\"" + self.vendor_name + "\", __ascendc_op_info_") | ||
| 151 | + fd.write(self.vendor_name + ");\n") | ||
| 152 | + fd.write("Dl_info dlInfo;\n") | ||
| 153 | + fd.write("if (!dladdr((void*)&OpLibInitFunc, &dlInfo)) {\n") | ||
| 154 | + fd.write(" LOG_ERROR(\"dladdr failed: %s\", dlerror());\n") | ||
| 155 | + fd.write(" return 1;\n") | ||
| 156 | + fd.write("}\n") | ||
| 157 | + fd.write("std::string targetPath = dlInfo.dli_fname;\n") | ||
| 158 | + fd.write("if (!ops::OpBinInfo::Check(targetPath)) {\n") | ||
| 159 | + fd.write(" LOG_ERROR(\"Path %s only support shared library, but it is not.\",targetPath.c_str());\n") | ||
| 160 | + fd.write(" return 1;\n") | ||
| 161 | + fd.write("}\n") | ||
| 162 | + fd.write(" return g_binInfo.Generate(&op_lib_path, targetPath);\n") | ||
| 163 | + fd.write("}\n") | ||
| 164 | + fd.write("REGISTER_OP_LIB(" + self.vendor_name + ").RegOpLibInit(OpLibInitFunc);\n") | ||
| 165 | + fd.write("}\n") | ||
| 166 | + except Exception as e: | ||
| 167 | + LogUtil.print_compile_log("", f"custom_op_registry_V2.cpp create error: {e}!", | ||
| 168 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 169 | + | ||
| 170 | + def ascendc_gen_header(self: any): | ||
| 171 | + socs_res = [] | ||
| 172 | + var_str = "" | ||
| 173 | + macro_op = ("std::vector<std::tuple<ge::AscendString, ge::AscendString, " | ||
| 174 | + "const uint8_t *, const uint8_t *>> __ascendc_op_info = \n") | ||
| 175 | + for file_name in self.op_info.keys(): | ||
| 176 | + file_addr = self.op_info.get(file_name) | ||
| 177 | + soc_pairs = [] | ||
| 178 | + op_syms = [] | ||
| 179 | + soc_res = ' {{ "{}", '.format(file_name) | ||
| 180 | + soc_res += '"{}", '.format(file_addr[0]) | ||
| 181 | + for pair_addr in file_addr[1]: | ||
| 182 | + op_syms.append(pair_addr) | ||
| 183 | + pair_addr1 = "&" + pair_addr | ||
| 184 | + soc_pairs.append(pair_addr1) | ||
| 185 | + soc_res += '{}, {}'.format(soc_pairs[0], soc_pairs[1]) | ||
| 186 | + soc_res += "}, \n" | ||
| 187 | + socs_res.append(soc_res) | ||
| 188 | + if len(op_syms) > 0: | ||
| 189 | + var_str += "".join(["extern uint8_t {};\n".format(sym) for sym in op_syms]) | ||
| 190 | + macro_op += "{{\n{}}}; \n".format("".join(socs_res)) | ||
| 191 | + head_file = os.path.join(self.out_path, "ge_table_op_resource.h") | ||
| 192 | + try: | ||
| 193 | + with os.fdopen( | ||
| 194 | + os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w" | ||
| 195 | + ) as fd: | ||
| 196 | + fd.write("#include <stdint.h>\n") | ||
| 197 | + fd.write("#include <map>\n") | ||
| 198 | + fd.write("#include <tuple>\n") | ||
| 199 | + fd.write("#include <vector>\n") | ||
| 200 | + fd.write('#include "graph/ascend_string.h"\n') | ||
| 201 | + fd.write('#include "register/op_impl_registry.h"\n\n') | ||
| 202 | + fd.write(var_str) | ||
| 203 | + fd.write('\n') | ||
| 204 | + fd.write("namespace AscendC {\n") | ||
| 205 | + fd.write(macro_op) | ||
| 206 | + fd.write("}\n") | ||
| 207 | + except Exception as e: | ||
| 208 | + LogUtil.print_compile_log("", f"ge_table_op_resource.h create error: {e}!", | ||
| 209 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 210 | + macro_op_register = ("std::vector<std::tuple<ge::AscendString, ge::AscendString, " | ||
| 211 | + "const uint8_t *, const uint8_t *>> __ascendc_op_info_" + self.vendor_name + " = \n") | ||
| 212 | + macro_op_register += "{{\n{}}}; \n".format("".join(socs_res)) | ||
| 213 | + self.ascendc_gen_register(macro_op_register, var_str) | ||
| 214 | + | ||
| 215 | + def ascendc_gen_lib(self: any): | ||
| 216 | + out_lib = os.path.join(self.out_path, "libopregistry.a") | ||
| 217 | + if os.path.exists(out_lib): | ||
| 218 | + os.remove(out_lib) | ||
| 219 | + objs = glob.glob(os.path.join(self.out_path, "*.o")) | ||
| 220 | + start = 0 | ||
| 221 | + batch_size = 100 | ||
| 222 | + for _ in range(math.ceil(len(objs) / batch_size)): | ||
| 223 | + sub_objs = objs[start : start + batch_size] | ||
| 224 | + start += batch_size | ||
| 225 | + try: | ||
| 226 | + subprocess.run(['ar', 'qc', out_lib] + sub_objs) | ||
| 227 | + subprocess.run(['ranlib', out_lib]) | ||
| 228 | + except Exception as e: | ||
| 229 | + LogUtil.print_compile_log("", f"execute ar/ranlib command error: {e}!", | ||
| 230 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 231 | + | ||
| 232 | + def ascendc_copy_dir(self: any, src_dir: str, target_dir: str): | ||
| 233 | + file_list = os.listdir(src_dir) | ||
| 234 | + for file_name in file_list: | ||
| 235 | + source_file = os.path.join(src_dir, file_name) | ||
| 236 | + target_file = os.path.join(target_dir, file_name) | ||
| 237 | + if os.path.isdir(source_file): | ||
| 238 | + try: | ||
| 239 | + shutil.copytree(source_file, target_file) | ||
| 240 | + except Exception as e: | ||
| 241 | + LogUtil.print_compile_log("", f"copy {source_file} error: {e}!", | ||
| 242 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 243 | + | ||
| 244 | + def ascendc_copy_file(self: any, src_dir: str, target_dir: str): | ||
| 245 | + file_list = os.listdir(src_dir) | ||
| 246 | + for file_name in file_list: | ||
| 247 | + source_file = os.path.join(src_dir, file_name) | ||
| 248 | + if os.path.isfile(source_file): | ||
| 249 | + try: | ||
| 250 | + os.makedirs(target_dir, exist_ok=True) | ||
| 251 | + except Exception as e: | ||
| 252 | + LogUtil.print_compile_log("", f"make {target_dir} error: {e}!", | ||
| 253 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 254 | + try: | ||
| 255 | + shutil.copy(source_file, target_dir) | ||
| 256 | + except Exception as e: | ||
| 257 | + LogUtil.print_compile_log("", f"copy {source_file} error: {e}!", | ||
| 258 | + AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC) | ||
| 259 | + | ||
| 260 | + def ascendc_copy_func(self: any): | ||
| 261 | + os.chdir(self.in_path) | ||
| 262 | + framework_catalog = os.listdir("framework") | ||
| 263 | + for catalog_file in framework_catalog: | ||
| 264 | + if catalog_file == "tf_plugin" or catalog_file == "caffe_plugin" or catalog_file == "onnx_plugin": | ||
| 265 | + source_dir = "op_kernel/tbe/op_info_cfg/ai_core" | ||
| 266 | + dst_dir = os.path.join(self.copy_path, "framework", self.framework_type) | ||
| 267 | + self.ascendc_copy_file(source_dir, dst_dir) | ||
| 268 | + source_dir = os.path.join("framework", catalog_file) | ||
| 269 | + dst_dir = os.path.join(self.copy_path, "framework", self.framework_type) | ||
| 270 | + self.ascendc_copy_file(source_dir, dst_dir) | ||
| 271 | + source_dir = "op_kernel/tbe/op_info_cfg/ai_core" | ||
| 272 | + dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/config") | ||
| 273 | + self.ascendc_copy_dir(source_dir, dst_dir) | ||
| 274 | + source_dir = "op_kernel/binary/dynamic" | ||
| 275 | + dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe", self.vendor_name + "_impl", "dynamic") | ||
| 276 | + self.ascendc_copy_file(source_dir, dst_dir) | ||
| 277 | + for compute_unit in self.op_soc_ver: | ||
| 278 | + source_dir = os.path.join("op_kernel/binary", compute_unit) | ||
| 279 | + dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel", compute_unit) | ||
| 280 | + self.ascendc_copy_dir(source_dir, dst_dir) | ||
| 281 | + source_dir = "op_kernel/binary/config" | ||
| 282 | + dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel/config") | ||
| 283 | + self.ascendc_copy_dir(source_dir, dst_dir) | ||
| 284 | + so_file = "op_impl/ai_core/tbe/op_master_device/lib/libcust_opmaster.so" | ||
| 285 | + if os.path.exists(so_file): | ||
| 286 | + dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/op_master_device/lib") | ||
| 287 | + os.makedirs(dst_dir, exist_ok=True) | ||
| 288 | + shutil.copy(so_file, dst_dir) | ||
| 289 | + | ||
| 290 | + | ||
| 291 | +def args_parse(): | ||
| 292 | + parser = argparse.ArgumentParser() | ||
| 293 | + parser.add_argument( | ||
| 294 | + "-r", "--registry-file-path", help="Output registry cpp file path." | ||
| 295 | + ) | ||
| 296 | + parser.add_argument( | ||
| 297 | + "-i", "--input-path", nargs="?", help="Input path of compile result." | ||
| 298 | + ) | ||
| 299 | + parser.add_argument( | ||
| 300 | + "-c", "--base-path", nargs="?", help="Base path of compile result." | ||
| 301 | + ) | ||
| 302 | + parser.add_argument( | ||
| 303 | + "-o", "--output-path", nargs="?", help="Output path of compile result." | ||
| 304 | + ) | ||
| 305 | + parser.add_argument( | ||
| 306 | + "-n", "--vendor-name", nargs="?", help="Vendor name." | ||
| 307 | + ) | ||
| 308 | + parser.add_argument( | ||
| 309 | + "-u", "--compute-unit", nargs="?", help="Compute unit." | ||
| 310 | + ) | ||
| 311 | + parser.add_argument( | ||
| 312 | + "-t", "--framework-type", nargs="?", help="Framework type, eg:tensorflow." | ||
| 313 | + ) | ||
| 314 | + parser.add_argument( | ||
| 315 | + "-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64." | ||
| 316 | + ) | ||
| 317 | + return parser.parse_args() | ||
| 318 | + | ||
| 319 | +if __name__ == "__main__": | ||
| 320 | + args = args_parse() | ||
| 321 | + kernel_packer = PackKernel(args) | ||
| 322 | + kernel_packer.ascendc_copy_func() | ||
| 323 | + kernel_packer.ascendc_get_config() | ||
| 324 | + kernel_packer.ascendc_pack_kernel() | ||
| 325 | + kernel_packer.ascendc_gen_header() | ||
| 326 | + kernel_packer.ascendc_gen_lib() | ||
| @@ -0,0 +1,65 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import sys | ||
| 9 | +import os | ||
| 10 | +import opdesc_parser | ||
| 11 | +import replay_codegen | ||
| 12 | +import const_var | ||
| 13 | +from replay_codegen import ReplayCodeGenParams | ||
| 14 | + | ||
| 15 | +PYF_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
| 16 | + | ||
| 17 | + | ||
| 18 | +class ReplayBuilder(opdesc_parser.OpDesc): | ||
| 19 | + def __init__(self: any, op_type: str): | ||
| 20 | + super().__init__(op_type) | ||
| 21 | + | ||
| 22 | + def gen_replay_source(self: any, impl_path: str, out_path: str, ops_product: str): | ||
| 23 | + if not self.op_replay_flag: | ||
| 24 | + print('{} replay not enabled'.format(self.op_type)) | ||
| 25 | + return | ||
| 26 | + argn = len(self.input_name) + len(self.output_name) + 1 | ||
| 27 | + if self.op_replay_batch: | ||
| 28 | + print('{} replay in batch mode'.format(self.op_type)) | ||
| 29 | + else: | ||
| 30 | + print('{} replay in normal mode'.format(self.op_type)) | ||
| 31 | + if impl_path.endswith('op_kernel'): | ||
| 32 | + implf = os.path.join(impl_path, self.op_file + '.cpp') | ||
| 33 | + tiling_file = os.path.join(impl_path, "../op_host", self.op_file + '_tiling.h') | ||
| 34 | + else: | ||
| 35 | + if self.dynamic_shape: | ||
| 36 | + dyn_path = 'dynamic' | ||
| 37 | + else: | ||
| 38 | + dyn_path = '' | ||
| 39 | + implf = os.path.join(impl_path, dyn_path, self.op_file + '.cpp') | ||
| 40 | + tiling_file = os.path.join(impl_path, "../../op_tiling", self.op_file + '_tiling.h') | ||
| 41 | + rep_conf = replay_codegen.ReplayCodeGen(ReplayCodeGenParams(self.op_type, implf, tiling_file, self.op_file, \ | ||
| 42 | + self.op_intf, argn, self.op_replay_batch, self.max_block_dim, self.max_shape_size)) | ||
| 43 | + rep_conf.set_batch(self.op_replay_batch) | ||
| 44 | + rep_conf.set_outdir(out_path) | ||
| 45 | + rep_conf.gen_replay(ops_product) | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +def gen_replay(cfgfile: str, cfgs: dict, dirs: dict, ops_product: str, ops: list = None): | ||
| 49 | + batch_lists = cfgs.get(const_var.REPLAY_BATCH).split(';') | ||
| 50 | + iterator_lists = cfgs.get(const_var.REPLAY_ITERATE).split(';') | ||
| 51 | + op_descs = opdesc_parser.get_op_desc(cfgfile, batch_lists, iterator_lists, ReplayBuilder, ops) | ||
| 52 | + for op_desc in op_descs: | ||
| 53 | + op_desc.gen_replay_source(dirs.get(const_var.CFG_IMPL_DIR), dirs.get(const_var.CFG_OUT_DIR), ops_product) | ||
| 54 | + | ||
| 55 | + | ||
| 56 | +if __name__ == '__main__': | ||
| 57 | + if len(sys.argv) <= 6: | ||
| 58 | + raise RuntimeError('arguments must greater than 6') | ||
| 59 | + rep_cfg = {} | ||
| 60 | + rep_cfg[const_var.REPLAY_BATCH] = sys.argv[2] | ||
| 61 | + rep_cfg[const_var.REPLAY_ITERATE] = sys.argv[3] | ||
| 62 | + rep_dir = {} | ||
| 63 | + rep_dir[const_var.CFG_IMPL_DIR] = sys.argv[4] | ||
| 64 | + rep_dir[const_var.CFG_OUT_DIR] = sys.argv[5] | ||
| 65 | + gen_replay(sys.argv[1], rep_cfg, rep_dir, sys.argv[6]) | ||
| @@ -0,0 +1,117 @@ | |||
| 1 | +#include <sys/types.h> | ||
| 2 | +#include <sys/stat.h> | ||
| 3 | +#include <fcntl.h> | ||
| 4 | +#include <unistd.h> | ||
| 5 | +#include <iostream> | ||
| 6 | +#include <thread> | ||
| 7 | +#include <stdlib.h> | ||
| 8 | +#include "replay_def.h" | ||
| 9 | +#include "code_gen.h" | ||
| 10 | +#include "replay_fun.h" | ||
| 11 | +#include "register/op_check.h" | ||
| 12 | +#define __ASCENDC_REPLAY_CODE__ | ||
| 13 | +#include <time.h> | ||
| 14 | + | ||
| 15 | +using namespace std; | ||
| 16 | +using namespace optiling; | ||
| 17 | +using namespace AscendCReplay; | ||
| 18 | + | ||
| 19 | +extern "C" void __KERNEL_FUN__ (__ARGS_DEF__, const char *); | ||
| 20 | +extern "C" int elf_batch_append(char *elf, uint32_t elfSize, char *jit, int kernum, char *atext[], int alen[], | ||
| 21 | + int atlen, const char* kernelname[]); | ||
| 22 | + | ||
| 23 | +#define KERNEL_N 1 | ||
| 24 | +#define ARG_N (__ARG_NUM__) | ||
| 25 | +#define MAX_L (1024 * 1024 * 100) | ||
| 26 | +#define MAX_E (1024 * 1024) | ||
| 27 | + | ||
| 28 | +int __KERNEL_FUN___replay___OPS_PRODUCT__(ReplayFuncParam& param, const int core_type) | ||
| 29 | +{ | ||
| 30 | + // gen type 1 : direct call codes 0: load .o file | ||
| 31 | + if (param.gentype < 0 || param.gentype > 1) { | ||
| 32 | + printf("Error: call replay gen type is %d, should only be 1 or 0\n", param.gentype); | ||
| 33 | + return 0; | ||
| 34 | + } else if (param.gentype == 1 && param.objptr == nullptr) { | ||
| 35 | + printf("Error: call replay with direct call mode, but code obj addr is null\n"); | ||
| 36 | + return 0; | ||
| 37 | + } else if (param.gentype == 0 && param.output_kernel_file == nullptr) { | ||
| 38 | + printf("Error: call replay with object file mode, but object file path is null\n"); | ||
| 39 | + return 0; | ||
| 40 | + } | ||
| 41 | + // core_type 0:MIX 1:CUBE 2:VEC | ||
| 42 | + if (core_type < 0 || core_type > 2) { | ||
| 43 | + printf("Error: call replay core type is %d !\n", core_type); | ||
| 44 | + return 0; | ||
| 45 | + } | ||
| 46 | + g_coreType = __CORE_TYPE__; | ||
| 47 | + g_taskRation = param.task_ration; | ||
| 48 | + g_tilingKey = param.tiling_key; | ||
| 49 | + | ||
| 50 | + unsigned char *buf, *jit; | ||
| 51 | + char *kernel[KERNEL_N]; | ||
| 52 | + int len[KERNEL_N]; | ||
| 53 | + block_idx = 0; | ||
| 54 | + block_num = param.block_dim; | ||
| 55 | + g_ubBase = block_num; | ||
| 56 | + uint8_t *code = (uint8_t *)malloc(MAX_L); | ||
| 57 | + uint8_t *pos = code; | ||
| 58 | + struct timespec tp1, tp2; | ||
| 59 | + | ||
| 60 | + clock_gettime(CLOCK_MONOTONIC, &tp1); | ||
| 61 | + if (block_num > 32) { | ||
| 62 | + printf("Error: block_num > 32\n"); | ||
| 63 | + return 0; | ||
| 64 | + } | ||
| 65 | + //__OP_FOPEN__ | ||
| 66 | + for (int i = 0; i < KERNEL_N; i++) { | ||
| 67 | + //__OP_SET_KERNEL__ | ||
| 68 | + for (int j = 0; j < ARG_N; j++) | ||
| 69 | + AddArg(j, ARG_STEP * (j + 1)); | ||
| 70 | +#ifdef FP_CEILING | ||
| 71 | + SetCtrlFloatEnable(); | ||
| 72 | +#else | ||
| 73 | + SetCtrlFloatDisable(); | ||
| 74 | +#endif | ||
| 75 | + CodeInit(pos, true); | ||
| 76 | + __KERNEL_FUN__(__KERNEL_ARGS__, param.tiling_data); | ||
| 77 | + CodeEnd(); | ||
| 78 | + kernel[i] = (char *)pos; | ||
| 79 | + len[i] = CodeLen(); | ||
| 80 | + pos += len[i]; | ||
| 81 | + } | ||
| 82 | + //__OP_FCLOSE__ | ||
| 83 | + clock_gettime(CLOCK_MONOTONIC, &tp2); | ||
| 84 | + buf = (unsigned char *)malloc(MAX_E); | ||
| 85 | + int fd = open(param.entry_file, O_RDONLY); | ||
| 86 | + if (fd < 0) { | ||
| 87 | + printf("[error]: cannot find entry.o : %s\n", param.entry_file); | ||
| 88 | + return 0; | ||
| 89 | + } | ||
| 90 | + uint32_t bufSize = read(fd, buf, MAX_E); | ||
| 91 | + if (bufSize <= 0) { | ||
| 92 | + printf("[error]: entry.o : %s is too small ! \n", param.entry_file); | ||
| 93 | + } | ||
| 94 | + close(fd); | ||
| 95 | + jit = (unsigned char *)malloc(MAX_L); | ||
| 96 | + printf("total code generated %ld\n", pos - code); | ||
| 97 | + int sz = elf_batch_append((char *)buf, bufSize, (char *)jit, KERNEL_N, kernel, len, pos - code, ¶m.kernel_name); | ||
| 98 | + if (tp1.tv_sec != tp2.tv_sec) { | ||
| 99 | + printf("%ld NS\n", tp2.tv_nsec + 1000000000 - tp1.tv_nsec); | ||
| 100 | + } else { | ||
| 101 | + printf("%ld NS\n", tp2.tv_nsec - tp1.tv_nsec); | ||
| 102 | + } | ||
| 103 | + printf("new elf size %d\n", sz); | ||
| 104 | + if (param.gentype == 0) { | ||
| 105 | + fd = open(param.output_kernel_file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); | ||
| 106 | + (void)write(fd, jit, sz); | ||
| 107 | + close(fd); | ||
| 108 | + free(jit); | ||
| 109 | + } else if (param.gentype == 1) { | ||
| 110 | + *param.objptr = (char*)jit; | ||
| 111 | + } | ||
| 112 | + free(buf); | ||
| 113 | + free(code); | ||
| 114 | + return sz; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +REG_REPLAY_FUNC(__OPTYPE__, __OPS_PRODUCT__, __KERNEL_FUN___replay___OPS_PRODUCT__); | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | +import os | ||
| 8 | +import stat | ||
| 9 | +import ctypes | ||
| 10 | +import collections | ||
| 11 | +import shutil | ||
| 12 | +import subprocess | ||
| 13 | +import copy | ||
| 14 | + | ||
| 15 | +"""CODE_* is used to cube/vector api is called in operator code | ||
| 16 | +CODE_MIX means both cube and vector api is called | ||
| 17 | +CODE_CUBE means only cube api is called | ||
| 18 | +CODE_VEC means only vector api is called | ||
| 19 | +""" | ||
| 20 | +CODE_MIX = 0 | ||
| 21 | +CODE_CUBE = 1 | ||
| 22 | +CODE_VEC = 2 | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +def _is_v220(op_product: str): | ||
| 26 | + """return if current soc version is V220 | ||
| 27 | + | ||
| 28 | + Returns: | ||
| 29 | + res: True means V220 | ||
| 30 | + """ | ||
| 31 | + if op_product == "ascend910_93" or op_product == "ascend910b": | ||
| 32 | + return True | ||
| 33 | + return False | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +InfoCodeChanelParams = collections.namedtuple('InfoCodeChanelParams',\ | ||
| 37 | +['src_file', 'tiling_header', 'kernel_name', 'outdir', 'op_product', 'compile_options']) | ||
| 38 | + | ||
| 39 | + | ||
| 40 | +def infer_code_channel(params: InfoCodeChanelParams): | ||
| 41 | + """get code channel for v220, return CODE_MIX if soc version is not V220 | ||
| 42 | + | ||
| 43 | + Args: | ||
| 44 | + src_file (str): AscendC operator code file | ||
| 45 | + src_file (str): AscendC operator tiling header file | ||
| 46 | + kernel_name (str): kernel function name | ||
| 47 | + optype (str): operator type | ||
| 48 | + compile_options (list): compile options for bisheng cmd | ||
| 49 | + | ||
| 50 | + Raises: | ||
| 51 | + Exception: if not exist L1/L0/UB if code, it's not a aicore code | ||
| 52 | + | ||
| 53 | + Returns: | ||
| 54 | + res (int): CODE_MIX/CODE_CUBE/CODE_VEC | ||
| 55 | + """ | ||
| 56 | + if not _is_v220(params.op_product): | ||
| 57 | + return CODE_MIX | ||
| 58 | + return CODE_VEC | ||
| @@ -0,0 +1,56 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Function: | ||
| 5 | +The replay funtion entry | ||
| 6 | +Copyright Information: | ||
| 7 | +Huawei Technologies Co., Ltd. All Rights Reserved © 2020 | ||
| 8 | +""" | ||
| 9 | + | ||
| 10 | +import os | ||
| 11 | +import stat | ||
| 12 | + | ||
| 13 | + | ||
| 14 | +REPLAY_BATCH = 'batch' | ||
| 15 | +REPLAY_ITERATE = 'iterate' | ||
| 16 | +CFG_IMPL_DIR = 'impl_dir' | ||
| 17 | +CFG_OUT_DIR = 'out_dir' | ||
| 18 | +AUTO_GEN_DIR = 'auto_gen_dir' | ||
| 19 | +WFLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | ||
| 20 | +WMODES = stat.S_IWUSR | stat.S_IRUSR | ||
| 21 | +SOC_MAP_EXT = {'ascend310p': 'Ascend310P3', 'ascend310b': 'Ascend310B1', | ||
| 22 | + 'ascend910': 'Ascend910A', 'ascend910b': 'Ascend910B1', | ||
| 23 | + 'ascend910_93': 'Ascend910_9391', 'ascend610lite': 'Ascend610Lite', | ||
| 24 | + 'ascend910_95': 'Ascend910_9599'} | ||
| 25 | +BIN_CMD = 'opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \ | ||
| 26 | +--output=$2 --impl_mode={impl} --simplified_key_mode=0 --op_mode=dynamic\n' | ||
| 27 | +SET_PLOG_LEVEL_ERROR = "export ASCEND_GLOBAL_LOG_LEVEL=3\n" | ||
| 28 | +SET_PLOG_STDOUT = "export ASCEND_SLOG_PRINT_TO_STDOUT=1\n" | ||
| 29 | +SRC_ENV = ''' | ||
| 30 | +while true; do | ||
| 31 | + case "$1" in | ||
| 32 | + --kernel-src=*) | ||
| 33 | + export BUILD_KERNEL_SRC=$(echo "$1" | cut -d"=" -f2-) | ||
| 34 | + shift | ||
| 35 | + ;; | ||
| 36 | + -*) | ||
| 37 | + shift | ||
| 38 | + ;; | ||
| 39 | + *) | ||
| 40 | + break | ||
| 41 | + ;; | ||
| 42 | + esac | ||
| 43 | +done | ||
| 44 | +''' | ||
| 45 | +CHK_CMD = ''' | ||
| 46 | +if ! test -f $2/{res_file} ; then | ||
| 47 | + echo "$2/{res_file} not generated!" | ||
| 48 | + exit 1 | ||
| 49 | +fi | ||
| 50 | +''' | ||
| 51 | +ATTR_DEF_VAL = {'str' : '', 'int': 0, 'float': 0.0, 'bool': False, 'list_bool': [], | ||
| 52 | + 'list_int': [], 'list_float': [], 'list_list_int': [[]]} | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +def conv_soc_ver(ver: str): | ||
| 56 | + return SOC_MAP_EXT.get(ver) | ||
| @@ -0,0 +1,21 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 3 | + | ||
| 4 | +project_path=$1 | ||
| 5 | +build_path=$2 | ||
| 6 | +vendor_name=customize | ||
| 7 | +if [[ ! -d "$project_path" ]]; then | ||
| 8 | + echo "[ERROR] No projcet path is provided" | ||
| 9 | + exit 1 | ||
| 10 | +fi | ||
| 11 | + | ||
| 12 | +if [[ ! -d "$build_path" ]]; then | ||
| 13 | + echo "[ERROR] No build path is provided" | ||
| 14 | + exit 1 | ||
| 15 | +fi | ||
| 16 | + | ||
| 17 | +# copy aicpu kernel so operators | ||
| 18 | +if [[ -d "${project_path}/cpukernel/aicpu_kernel_lib" ]]; then | ||
| 19 | + cp -f ${project_path}/cpukernel/aicpu_kernel_lib/* ${build_path}/makepkg/packages/vendors/$vendor_name/op_impl/cpu/aicpu_kernel/impl | ||
| 20 | + rm -rf ${project_path}/cpukernel/aicpu_kernel_lib | ||
| 21 | +fi | ||
| @@ -0,0 +1,62 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 3 | +# Description: Generate npu_supported_ops.json | ||
| 4 | +# ============================================================================== | ||
| 5 | + | ||
| 6 | +if [[ -z "$1" ]]; then | ||
| 7 | + echo -e "[ERROR] No source dir provided" | ||
| 8 | + exit 1 | ||
| 9 | +fi | ||
| 10 | + | ||
| 11 | +if [[ -z "$2" ]]; then | ||
| 12 | + echo -e "[ERROR] No destination dir provided" | ||
| 13 | + exit 1 | ||
| 14 | +fi | ||
| 15 | + | ||
| 16 | +src=$1 | ||
| 17 | +dest_file=$2/npu_supported_ops.json | ||
| 18 | + | ||
| 19 | +if [ -f "$dest_file" ];then | ||
| 20 | + chmod u+w $dest_file | ||
| 21 | +fi | ||
| 22 | + | ||
| 23 | +echo $* | ||
| 24 | + | ||
| 25 | +add_ops() { | ||
| 26 | + name=$1 | ||
| 27 | + isHeavy=$2 | ||
| 28 | + file=$3 | ||
| 29 | + grep -w "\"$name\"" ${file} >/dev/null | ||
| 30 | + if [ $? == 0 ];then | ||
| 31 | + return | ||
| 32 | + fi | ||
| 33 | + echo " \"${name}\": {" >> ${file} | ||
| 34 | + echo " \"isGray\": false," >> ${file} | ||
| 35 | + echo " \"isHeavy\": ${isHeavy}" >> ${file} | ||
| 36 | + echo " }," >> ${file} | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +echo "{" > ${dest_file} | ||
| 40 | +ini_files=$(find ${src} -name "*.ini") | ||
| 41 | +for file in ${ini_files} ; do | ||
| 42 | + name=$(grep '^\[' ${file} | sed 's/\[//g' | sed 's/]//g' | sed 's/\r//g') | ||
| 43 | + grep 'heavyOp.flag' ${file} >/dev/null | ||
| 44 | + if [ $? == 0 ];then | ||
| 45 | + isHeavy=$(grep 'heavyOp.flag' ${file} | awk -F= '{print $2}') | ||
| 46 | + else | ||
| 47 | + isHeavy="false" | ||
| 48 | + fi | ||
| 49 | + for op in ${name} ; do | ||
| 50 | + add_ops ${op} "false" ${dest_file} | ||
| 51 | + done | ||
| 52 | +done | ||
| 53 | +echo "}" >> ${dest_file} | ||
| 54 | +file_count=$(cat ${dest_file} | wc -l) | ||
| 55 | +line=$(($file_count-1)) | ||
| 56 | +sed -i "${line}{s/,//g}" ${dest_file} | ||
| 57 | + | ||
| 58 | +chmod 640 "${dest_file}" | ||
| 59 | +echo -e "[INFO] Succed generated ${dest_file}" | ||
| 60 | + | ||
| 61 | +exit 0 | ||
| 62 | + | ||
| @@ -0,0 +1,10 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 3 | + | ||
| 4 | + | ||
| 5 | +ascend_install_dir=$1 | ||
| 6 | +gen_file_dir=$2 | ||
| 7 | + | ||
| 8 | +# create version.info | ||
| 9 | +compiler_version=$(grep "Version" -w ${ascend_install_dir}/compiler/version.info | awk -F = '{print $2}') | ||
| 10 | +echo "custom_opp_compiler_version=${compiler_version}" > ${gen_file_dir}/version.info | ||
| @@ -0,0 +1,36 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +Created on Feb 28 20:56:45 2020 | ||
| 4 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 5 | +""" | ||
| 6 | +import json | ||
| 7 | +import os | ||
| 8 | +import sys | ||
| 9 | +import stat | ||
| 10 | +import const_var | ||
| 11 | + | ||
| 12 | + | ||
| 13 | +if __name__ == '__main__': | ||
| 14 | + if len(sys.argv) != 3: | ||
| 15 | + print(sys.argv) | ||
| 16 | + print('argv error, inert_op_info.py your_op_file lib_op_file') | ||
| 17 | + sys.exit(2) | ||
| 18 | + | ||
| 19 | + with open(sys.argv[1], 'r') as load_f: | ||
| 20 | + insert_operator = json.load(load_f) | ||
| 21 | + | ||
| 22 | + all_operators = {} | ||
| 23 | + if os.path.exists(sys.argv[2]): | ||
| 24 | + if os.path.getsize(sys.argv[2]) != 0: | ||
| 25 | + with open(sys.argv[2], 'r') as load_f: | ||
| 26 | + all_operators = json.load(load_f) | ||
| 27 | + | ||
| 28 | + for k in insert_operator.keys(): | ||
| 29 | + if k in all_operators.keys(): | ||
| 30 | + print('replace op:[', k, '] success') | ||
| 31 | + else: | ||
| 32 | + print('insert op:[', k, '] success') | ||
| 33 | + all_operators[k] = insert_operator[k] | ||
| 34 | + | ||
| 35 | + with os.fdopen(os.open(sys.argv[2], const_var.WFLAGS, const_var.WMODES), 'w') as json_file: | ||
| 36 | + json_file.write(json.dumps(all_operators, indent=4)) | ||
| @@ -0,0 +1,248 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import sys | ||
| 9 | +import os | ||
| 10 | +import re | ||
| 11 | +import glob | ||
| 12 | +import json | ||
| 13 | +import argparse | ||
| 14 | +import const_var | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +DATA_TPYE_DICT = { | ||
| 18 | + 'float32': 0, | ||
| 19 | + 'float16': 1, | ||
| 20 | + 'int8': 2, | ||
| 21 | + 'int16': 6, | ||
| 22 | + 'uint16': 7, | ||
| 23 | + 'uint8': 4, | ||
| 24 | + 'int32': 3, | ||
| 25 | + 'int64': 9, | ||
| 26 | + 'uint32': 8, | ||
| 27 | + 'uint64': 10, | ||
| 28 | + 'bool': 12, | ||
| 29 | + 'double': 11, | ||
| 30 | + 'string': 13, | ||
| 31 | + 'dual_sub_int8': 14, | ||
| 32 | + 'dual_sub_uint8': 15, | ||
| 33 | + 'complex64': 16, | ||
| 34 | + 'complex128': 17, | ||
| 35 | + 'qint8': 18, | ||
| 36 | + 'qint16': 19, | ||
| 37 | + 'qint32': 20, | ||
| 38 | + 'quint8': 21, | ||
| 39 | + 'quint16': 22, | ||
| 40 | + 'resource': 23, | ||
| 41 | + 'string_ref': 24, | ||
| 42 | + 'dual': 25, | ||
| 43 | + 'variant': 26, | ||
| 44 | + 'bf16': 27, | ||
| 45 | + 'bfloat16': 27, | ||
| 46 | + 'undefined': 28, | ||
| 47 | + 'int4': 29, | ||
| 48 | + 'uint1': 30, | ||
| 49 | + 'int2': 31, | ||
| 50 | + 'complex32': 33 | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +FORMAT_DICT = { | ||
| 54 | + 'NCHW': 0, | ||
| 55 | + 'NHWC': 1, | ||
| 56 | + 'ND': 2, | ||
| 57 | + 'NC1HWC0': 3, | ||
| 58 | + 'FRACTAL_Z': 4, | ||
| 59 | + 'NC1C0HWPAD': 5, | ||
| 60 | + 'NHWC1C0': 6, | ||
| 61 | + 'FSR_NCHW': 7, | ||
| 62 | + 'FRACTAL_DECONV': 8, | ||
| 63 | + 'C1HWNC0': 9, | ||
| 64 | + 'FRACTAL_DECONV_TRANSPOSE': 10, | ||
| 65 | + 'FRACTAL_DECONV_SP_STRIDE_TRANS': 11, | ||
| 66 | + 'NC1HWC0_C04': 12, | ||
| 67 | + 'FRACTAL_Z_C04': 13, | ||
| 68 | + 'CHWN': 14, | ||
| 69 | + 'FRACTAL_DECONV_SP_STRIDE8_TRANS': 15, | ||
| 70 | + 'HWCN': 16, | ||
| 71 | + 'NC1KHKWHWC0': 17, | ||
| 72 | + 'BN_WEIGHT': 18, | ||
| 73 | + 'FILTER_HWCK': 19, | ||
| 74 | + 'HASHTABLE_LOOKUP_LOOKUPS': 20, | ||
| 75 | + 'HASHTABLE_LOOKUP_KEYS': 21, | ||
| 76 | + 'HASHTABLE_LOOKUP_VALUE': 22, | ||
| 77 | + 'HASHTABLE_LOOKUP_OUTPUT': 23, | ||
| 78 | + 'HASHTABLE_LOOKUP_HITS': 24, | ||
| 79 | + 'C1HWNCoC0': 25, | ||
| 80 | + 'MD': 26, | ||
| 81 | + 'NDHWC': 27, | ||
| 82 | + 'FRACTAL_ZZ': 28, | ||
| 83 | + 'FRACTAL_NZ': 29, | ||
| 84 | + 'NCDHW': 30, | ||
| 85 | + 'DHWCN': 31, | ||
| 86 | + 'NDC1HWC0': 32, | ||
| 87 | + 'FRACTAL_Z_3D': 33, | ||
| 88 | + 'CN': 34, | ||
| 89 | + 'NC': 35, | ||
| 90 | + 'DHWNC': 36, | ||
| 91 | + 'FRACTAL_Z_3D_TRANSPOSE': 37, | ||
| 92 | + 'FRACTAL_ZN_LSTM': 38, | ||
| 93 | + 'FRACTAL_Z_G': 39, | ||
| 94 | + 'RESERVED': 40, | ||
| 95 | + 'ALL': 41, | ||
| 96 | + 'NULL': 42, | ||
| 97 | + 'ND_RNN_BIAS': 43, | ||
| 98 | + 'FRACTAL_ZN_RNN': 44, | ||
| 99 | + 'NYUV': 45, | ||
| 100 | + 'NYUV_A': 46 | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | + | ||
| 104 | +def load_json(json_file: str): | ||
| 105 | + with open(json_file, encoding='utf-8') as file: | ||
| 106 | + json_content = json.load(file) | ||
| 107 | + return json_content | ||
| 108 | + | ||
| 109 | + | ||
| 110 | +def get_specified_suffix_file(root_dir, suffix): | ||
| 111 | + specified_suffix = os.path.join(root_dir, '**/*.{}'.format(suffix)) | ||
| 112 | + all_suffix_files = glob.glob(specified_suffix, recursive=True) | ||
| 113 | + return all_suffix_files | ||
| 114 | + | ||
| 115 | + | ||
| 116 | +def get_deterministic_value(support_info): | ||
| 117 | + deterministic_key = 'deterministic' | ||
| 118 | + if deterministic_key not in support_info: | ||
| 119 | + return 0 | ||
| 120 | + deterministic_value = support_info.get(deterministic_key) | ||
| 121 | + if deterministic_value == 'true': | ||
| 122 | + return 1 | ||
| 123 | + else: | ||
| 124 | + return 0 | ||
| 125 | + | ||
| 126 | + | ||
| 127 | +def get_precision_value(support_info): | ||
| 128 | + precision_key = 'implMode' | ||
| 129 | + precision_value = support_info.get(precision_key) | ||
| 130 | + if precision_value == 'high_performance': | ||
| 131 | + _value = 1 | ||
| 132 | + elif precision_value == 'high_precision': | ||
| 133 | + _value = 2 | ||
| 134 | + else: | ||
| 135 | + _value = 0 | ||
| 136 | + return _value | ||
| 137 | + | ||
| 138 | + | ||
| 139 | +def get_overflow_value(support_info): | ||
| 140 | + return 0 | ||
| 141 | + | ||
| 142 | + | ||
| 143 | +def get_parameters(info): | ||
| 144 | + if info: | ||
| 145 | + if 'dtype' in info: | ||
| 146 | + data_type = info['dtype'] | ||
| 147 | + data_type_value = DATA_TPYE_DICT.get(data_type) | ||
| 148 | + else: | ||
| 149 | + data_type_value = 0 | ||
| 150 | + if 'format' in info: | ||
| 151 | + _format = info['format'] | ||
| 152 | + _format_value = FORMAT_DICT.get(_format) | ||
| 153 | + else: | ||
| 154 | + _format_value = 0 | ||
| 155 | + else: | ||
| 156 | + data_type_value = 0 | ||
| 157 | + _format_value = 0 | ||
| 158 | + return str(data_type_value), str(_format_value) | ||
| 159 | + | ||
| 160 | + | ||
| 161 | +def get_dynamic_parameters(info): | ||
| 162 | + # 动态输入时只需获取第一个参数 | ||
| 163 | + return get_parameters(info[0]) | ||
| 164 | + | ||
| 165 | + | ||
| 166 | +def get_all_parameters(support_info, _type): | ||
| 167 | + result_list = list() | ||
| 168 | + info_lists = support_info.get(_type) | ||
| 169 | + if info_lists: | ||
| 170 | + for _info in info_lists: | ||
| 171 | + # 输入为列表时是动态输入 | ||
| 172 | + if isinstance(_info, (list, tuple)): | ||
| 173 | + data_type_value, _format_value = get_dynamic_parameters(_info) | ||
| 174 | + else: | ||
| 175 | + data_type_value, _format_value = get_parameters(_info) | ||
| 176 | + result_list.append("{},{}".format(data_type_value, _format_value)) | ||
| 177 | + return result_list | ||
| 178 | + | ||
| 179 | + | ||
| 180 | +def get_all_input_parameters(support_info): | ||
| 181 | + result = get_all_parameters(support_info, 'inputs') | ||
| 182 | + return '/'.join(result) | ||
| 183 | + | ||
| 184 | + | ||
| 185 | +def insert_content_into_file(input_file, content): | ||
| 186 | + with open(input_file, 'r+') as file: | ||
| 187 | + lines = file.readlines() | ||
| 188 | + for index, line in enumerate(lines): | ||
| 189 | + match_result = re.search(r'"staticKey":', line) | ||
| 190 | + if match_result: | ||
| 191 | + count = len(line) - len(line.lstrip()) | ||
| 192 | + new_content = "{}{}".format(' ' * count, content) | ||
| 193 | + # 插入到前一行,防止插入最后时还需要考虑是否添加逗号 | ||
| 194 | + lines.insert(index, new_content) | ||
| 195 | + break | ||
| 196 | + file.seek(0) | ||
| 197 | + file.write(''.join(lines)) | ||
| 198 | + | ||
| 199 | + | ||
| 200 | +def insert_simplified_keys(json_file): | ||
| 201 | + contents = load_json(json_file) | ||
| 202 | + # 不存在'binFileName'或者'supportInfo'字段时,非需要替换的解析json文件 | ||
| 203 | + if ('binFileName' not in contents) or ('supportInfo' not in contents): | ||
| 204 | + return | ||
| 205 | + support_info = contents.get('supportInfo') | ||
| 206 | + bin_file_name = contents.get('binFileName') | ||
| 207 | + # 'simplifiedKey'字段已经存在时,直接返回,不重复生成 | ||
| 208 | + if 'simplifiedKey' in support_info: | ||
| 209 | + return | ||
| 210 | + op_type = bin_file_name.split('_')[0] | ||
| 211 | + deterministic = str(get_deterministic_value(support_info)) | ||
| 212 | + precision = str(get_precision_value(support_info)) | ||
| 213 | + overflow = str(get_overflow_value(support_info)) | ||
| 214 | + input_parameters = get_all_input_parameters(support_info) | ||
| 215 | + key = '{}/d={},p={},o={}/{}/'.format( | ||
| 216 | + op_type, | ||
| 217 | + deterministic, | ||
| 218 | + precision, | ||
| 219 | + overflow, | ||
| 220 | + input_parameters) | ||
| 221 | + result = '"simplifiedKey": "' + key + '",\n' | ||
| 222 | + insert_content_into_file(json_file, result) | ||
| 223 | + | ||
| 224 | + | ||
| 225 | +def insert_all_simplified_keys(root_dir): | ||
| 226 | + suffix = 'json' | ||
| 227 | + all_json_files = get_specified_suffix_file(root_dir, suffix) | ||
| 228 | + for _json in all_json_files: | ||
| 229 | + insert_simplified_keys(_json) | ||
| 230 | + | ||
| 231 | + | ||
| 232 | +def args_prase(): | ||
| 233 | + parser = argparse.ArgumentParser() | ||
| 234 | + parser.add_argument('-p', | ||
| 235 | + '--path', | ||
| 236 | + nargs='?', | ||
| 237 | + required=True, | ||
| 238 | + help='Parse the path of the json file.') | ||
| 239 | + return parser.parse_args() | ||
| 240 | + | ||
| 241 | + | ||
| 242 | +def main(): | ||
| 243 | + args = args_prase() | ||
| 244 | + insert_all_simplified_keys(args.path) | ||
| 245 | + | ||
| 246 | + | ||
| 247 | +if __name__ == '__main__': | ||
| 248 | + main() | ||
| @@ -0,0 +1,115 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +def gen_fun_def(title, kernel, argn, arg_type, arg_name): | ||
| 10 | + entry = [] | ||
| 11 | + entry.append(title) | ||
| 12 | + entry.append(kernel) | ||
| 13 | + entry.append('(') | ||
| 14 | + args = [] | ||
| 15 | + for i in range(0, argn): | ||
| 16 | + args.append(arg_type + ' ' + arg_name + str(i)) | ||
| 17 | + entry.append(', '.join(args)) | ||
| 18 | + entry.append(')') | ||
| 19 | + return ' '.join(entry) | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +def gen_batch_kernel_body(fname, argn, arg_name): | ||
| 23 | + body = [] | ||
| 24 | + body.append('{') | ||
| 25 | + fun = [] | ||
| 26 | + fun.append(fname) | ||
| 27 | + fun.append('(') | ||
| 28 | + args = [] | ||
| 29 | + for i in range(0, argn): | ||
| 30 | + args.append(arg_name + str(i)) | ||
| 31 | + fun.append(', '.join(args)) | ||
| 32 | + fun.append(');') | ||
| 33 | + body.append(' '.join(fun)) | ||
| 34 | + body.append('}') | ||
| 35 | + return '\n'.join(body) | ||
| 36 | + | ||
| 37 | + | ||
| 38 | +def gen_mc_kernel_body(kn, argn, arg_name, blknum): | ||
| 39 | + body = [] | ||
| 40 | + body.append('{') | ||
| 41 | + body.append(' switch(block_idx) {') | ||
| 42 | + for blk in range(0, blknum): | ||
| 43 | + fun = [] | ||
| 44 | + fun.append('{}_blk{:02d}'.format(kn, blk)) | ||
| 45 | + fun.append('(') | ||
| 46 | + args = [] | ||
| 47 | + for i in range(0, argn): | ||
| 48 | + args.append(arg_name + str(i)) | ||
| 49 | + fun.append(', '.join(args)) | ||
| 50 | + fun.append(')') | ||
| 51 | + body.append(' case {}: {}; break;'.format(blk, ' '.join(fun))) | ||
| 52 | + body.append(' default: break;') | ||
| 53 | + body.append(' }') | ||
| 54 | + body.append('}') | ||
| 55 | + return '\n'.join(body) | ||
| 56 | + | ||
| 57 | + | ||
| 58 | +def gen_proc_body(argn, arg_name): | ||
| 59 | + body = [] | ||
| 60 | + body.append('{') | ||
| 61 | + args = [] | ||
| 62 | + for i in range(0, argn): | ||
| 63 | + args.append(arg_name + str(i)) | ||
| 64 | + body.append('uint64_t __x = (uint64_t)' + ' + (uint64_t)'.join(args) + ';') | ||
| 65 | + body.append('__asm__ ("NOP");') | ||
| 66 | + body.append('__asm__ ("NOP");') | ||
| 67 | + body.append('__asm__ ("NOP");') | ||
| 68 | + body.append('}') | ||
| 69 | + return '\n'.join(body) | ||
| 70 | + | ||
| 71 | + | ||
| 72 | +def batch_code_gen(kn, argn, argt): | ||
| 73 | + codes = [] | ||
| 74 | + kernel_name = kn | ||
| 75 | + proc_name = kernel_name + '_percore' | ||
| 76 | + arg_num = int(argn) | ||
| 77 | + data_type = argt | ||
| 78 | + arg_type = '__gm__ ' + data_type + '* __restrict__' | ||
| 79 | + arg_name = 'arg' | ||
| 80 | + kernel_title = 'extern \"C\" __global__ __aicore__ void' | ||
| 81 | + proc_title = 'extern \"C\" __attribute__((noinline)) __aicore__ void' | ||
| 82 | + codes.append('#ifndef __aicore__') | ||
| 83 | + codes.append('#define __aicore__ [aicore]') | ||
| 84 | + codes.append('#endif') | ||
| 85 | + codes.append(gen_fun_def(proc_title, proc_name, arg_num, arg_type, arg_name) + ';') | ||
| 86 | + codes.append(gen_fun_def(kernel_title, kernel_name, arg_num, arg_type, arg_name)) | ||
| 87 | + codes.append(gen_batch_kernel_body(proc_name, arg_num, arg_name)) | ||
| 88 | + codes.append(gen_fun_def(proc_title, proc_name, arg_num, arg_type, arg_name)) | ||
| 89 | + codes.append(gen_proc_body(arg_num, arg_name)) | ||
| 90 | + return '\n'.join(codes) + '\n' | ||
| 91 | + | ||
| 92 | + | ||
| 93 | +def mc_code_gen(kn, argn, argt, blknum): | ||
| 94 | + codes = [] | ||
| 95 | + kernel_name = kn | ||
| 96 | + core_num = int(blknum) | ||
| 97 | + arg_num = int(argn) | ||
| 98 | + data_type = argt | ||
| 99 | + arg_type = '__gm__ ' + data_type + '* __restrict__' | ||
| 100 | + arg_name = 'arg' | ||
| 101 | + kernel_title = 'extern \"C\" __global__ __aicore__ void' | ||
| 102 | + proc_title = 'extern \"C\" __attribute__((noinline)) __aicore__ void' | ||
| 103 | + codes.append('#ifndef __aicore__') | ||
| 104 | + codes.append('#define __aicore__ [aicore]') | ||
| 105 | + codes.append('#endif') | ||
| 106 | + for i in range(0, core_num): | ||
| 107 | + proc_name = '{}_blk{:02d}'.format(kernel_name, i) | ||
| 108 | + codes.append(gen_fun_def(proc_title, proc_name, arg_num, arg_type, arg_name) + ';') | ||
| 109 | + codes.append(gen_fun_def(kernel_title, kernel_name, arg_num, arg_type, arg_name)) | ||
| 110 | + codes.append(gen_mc_kernel_body(kernel_name, arg_num, arg_name, core_num)) | ||
| 111 | + for i in range(0, core_num): | ||
| 112 | + proc_name = '{}_blk{:02d}'.format(kernel_name, i) | ||
| 113 | + codes.append(gen_fun_def(proc_title, proc_name, arg_num, arg_type, arg_name)) | ||
| 114 | + codes.append(gen_proc_body(arg_num, arg_name)) | ||
| 115 | + return '\n'.join(codes) + '\n' | ||
| @@ -0,0 +1,10 @@ | |||
| 1 | +#include <sys/types.h> | ||
| 2 | +#include <sys/stat.h> | ||
| 3 | +#include <fcntl.h> | ||
| 4 | +#include <unistd.h> | ||
| 5 | +#include <iostream> | ||
| 6 | +#include "replay_def.h" | ||
| 7 | +#include "code_gen.h" | ||
| 8 | +#include "replay_fun.h" | ||
| 9 | +#define __ASCENDC_REPLAY_CODE__ | ||
| 10 | +#include "__CCE_FILE__" | ||
| @@ -0,0 +1,339 @@ | |||
| 1 | + GNU GENERAL PUBLIC LICENSE | ||
| 2 | + Version 2, June 1991 | ||
| 3 | + | ||
| 4 | + Copyright (C) 1989, 1991 Free Software Foundation, Inc., | ||
| 5 | + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 6 | + Everyone is permitted to copy and distribute verbatim copies | ||
| 7 | + of this license document, but changing it is not allowed. | ||
| 8 | + | ||
| 9 | + Preamble | ||
| 10 | + | ||
| 11 | + The licenses for most software are designed to take away your | ||
| 12 | +freedom to share and change it. By contrast, the GNU General Public | ||
| 13 | +License is intended to guarantee your freedom to share and change free | ||
| 14 | +software--to make sure the software is free for all its users. This | ||
| 15 | +General Public License applies to most of the Free Software | ||
| 16 | +Foundation's software and to any other program whose authors commit to | ||
| 17 | +using it. (Some other Free Software Foundation software is covered by | ||
| 18 | +the GNU Lesser General Public License instead.) You can apply it to | ||
| 19 | +your programs, too. | ||
| 20 | + | ||
| 21 | + When we speak of free software, we are referring to freedom, not | ||
| 22 | +price. Our General Public Licenses are designed to make sure that you | ||
| 23 | +have the freedom to distribute copies of free software (and charge for | ||
| 24 | +this service if you wish), that you receive source code or can get it | ||
| 25 | +if you want it, that you can change the software or use pieces of it | ||
| 26 | +in new free programs; and that you know you can do these things. | ||
| 27 | + | ||
| 28 | + To protect your rights, we need to make restrictions that forbid | ||
| 29 | +anyone to deny you these rights or to ask you to surrender the rights. | ||
| 30 | +These restrictions translate to certain responsibilities for you if you | ||
| 31 | +distribute copies of the software, or if you modify it. | ||
| 32 | + | ||
| 33 | + For example, if you distribute copies of such a program, whether | ||
| 34 | +gratis or for a fee, you must give the recipients all the rights that | ||
| 35 | +you have. You must make sure that they, too, receive or can get the | ||
| 36 | +source code. And you must show them these terms so they know their | ||
| 37 | +rights. | ||
| 38 | + | ||
| 39 | + We protect your rights with two steps: (1) copyright the software, and | ||
| 40 | +(2) offer you this license which gives you legal permission to copy, | ||
| 41 | +distribute and/or modify the software. | ||
| 42 | + | ||
| 43 | + Also, for each author's protection and ours, we want to make certain | ||
| 44 | +that everyone understands that there is no warranty for this free | ||
| 45 | +software. If the software is modified by someone else and passed on, we | ||
| 46 | +want its recipients to know that what they have is not the original, so | ||
| 47 | +that any problems introduced by others will not reflect on the original | ||
| 48 | +authors' reputations. | ||
| 49 | + | ||
| 50 | + Finally, any free program is threatened constantly by software | ||
| 51 | +patents. We wish to avoid the danger that redistributors of a free | ||
| 52 | +program will individually obtain patent licenses, in effect making the | ||
| 53 | +program proprietary. To prevent this, we have made it clear that any | ||
| 54 | +patent must be licensed for everyone's free use or not licensed at all. | ||
| 55 | + | ||
| 56 | + The precise terms and conditions for copying, distribution and | ||
| 57 | +modification follow. | ||
| 58 | + | ||
| 59 | + GNU GENERAL PUBLIC LICENSE | ||
| 60 | + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
| 61 | + | ||
| 62 | + 0. This License applies to any program or other work which contains | ||
| 63 | +a notice placed by the copyright holder saying it may be distributed | ||
| 64 | +under the terms of this General Public License. The "Program", below, | ||
| 65 | +refers to any such program or work, and a "work based on the Program" | ||
| 66 | +means either the Program or any derivative work under copyright law: | ||
| 67 | +that is to say, a work containing the Program or a portion of it, | ||
| 68 | +either verbatim or with modifications and/or translated into another | ||
| 69 | +language. (Hereinafter, translation is included without limitation in | ||
| 70 | +the term "modification".) Each licensee is addressed as "you". | ||
| 71 | + | ||
| 72 | +Activities other than copying, distribution and modification are not | ||
| 73 | +covered by this License; they are outside its scope. The act of | ||
| 74 | +running the Program is not restricted, and the output from the Program | ||
| 75 | +is covered only if its contents constitute a work based on the | ||
| 76 | +Program (independent of having been made by running the Program). | ||
| 77 | +Whether that is true depends on what the Program does. | ||
| 78 | + | ||
| 79 | + 1. You may copy and distribute verbatim copies of the Program's | ||
| 80 | +source code as you receive it, in any medium, provided that you | ||
| 81 | +conspicuously and appropriately publish on each copy an appropriate | ||
| 82 | +copyright notice and disclaimer of warranty; keep intact all the | ||
| 83 | +notices that refer to this License and to the absence of any warranty; | ||
| 84 | +and give any other recipients of the Program a copy of this License | ||
| 85 | +along with the Program. | ||
| 86 | + | ||
| 87 | +You may charge a fee for the physical act of transferring a copy, and | ||
| 88 | +you may at your option offer warranty protection in exchange for a fee. | ||
| 89 | + | ||
| 90 | + 2. You may modify your copy or copies of the Program or any portion | ||
| 91 | +of it, thus forming a work based on the Program, and copy and | ||
| 92 | +distribute such modifications or work under the terms of Section 1 | ||
| 93 | +above, provided that you also meet all of these conditions: | ||
| 94 | + | ||
| 95 | + a) You must cause the modified files to carry prominent notices | ||
| 96 | + stating that you changed the files and the date of any change. | ||
| 97 | + | ||
| 98 | + b) You must cause any work that you distribute or publish, that in | ||
| 99 | + whole or in part contains or is derived from the Program or any | ||
| 100 | + part thereof, to be licensed as a whole at no charge to all third | ||
| 101 | + parties under the terms of this License. | ||
| 102 | + | ||
| 103 | + c) If the modified program normally reads commands interactively | ||
| 104 | + when run, you must cause it, when started running for such | ||
| 105 | + interactive use in the most ordinary way, to print or display an | ||
| 106 | + announcement including an appropriate copyright notice and a | ||
| 107 | + notice that there is no warranty (or else, saying that you provide | ||
| 108 | + a warranty) and that users may redistribute the program under | ||
| 109 | + these conditions, and telling the user how to view a copy of this | ||
| 110 | + License. (Exception: if the Program itself is interactive but | ||
| 111 | + does not normally print such an announcement, your work based on | ||
| 112 | + the Program is not required to print an announcement.) | ||
| 113 | + | ||
| 114 | +These requirements apply to the modified work as a whole. If | ||
| 115 | +identifiable sections of that work are not derived from the Program, | ||
| 116 | +and can be reasonably considered independent and separate works in | ||
| 117 | +themselves, then this License, and its terms, do not apply to those | ||
| 118 | +sections when you distribute them as separate works. But when you | ||
| 119 | +distribute the same sections as part of a whole which is a work based | ||
| 120 | +on the Program, the distribution of the whole must be on the terms of | ||
| 121 | +this License, whose permissions for other licensees extend to the | ||
| 122 | +entire whole, and thus to each and every part regardless of who wrote it. | ||
| 123 | + | ||
| 124 | +Thus, it is not the intent of this section to claim rights or contest | ||
| 125 | +your rights to work written entirely by you; rather, the intent is to | ||
| 126 | +exercise the right to control the distribution of derivative or | ||
| 127 | +collective works based on the Program. | ||
| 128 | + | ||
| 129 | +In addition, mere aggregation of another work not based on the Program | ||
| 130 | +with the Program (or with a work based on the Program) on a volume of | ||
| 131 | +a storage or distribution medium does not bring the other work under | ||
| 132 | +the scope of this License. | ||
| 133 | + | ||
| 134 | + 3. You may copy and distribute the Program (or a work based on it, | ||
| 135 | +under Section 2) in object code or executable form under the terms of | ||
| 136 | +Sections 1 and 2 above provided that you also do one of the following: | ||
| 137 | + | ||
| 138 | + a) Accompany it with the complete corresponding machine-readable | ||
| 139 | + source code, which must be distributed under the terms of Sections | ||
| 140 | + 1 and 2 above on a medium customarily used for software interchange; or, | ||
| 141 | + | ||
| 142 | + b) Accompany it with a written offer, valid for at least three | ||
| 143 | + years, to give any third party, for a charge no more than your | ||
| 144 | + cost of physically performing source distribution, a complete | ||
| 145 | + machine-readable copy of the corresponding source code, to be | ||
| 146 | + distributed under the terms of Sections 1 and 2 above on a medium | ||
| 147 | + customarily used for software interchange; or, | ||
| 148 | + | ||
| 149 | + c) Accompany it with the information you received as to the offer | ||
| 150 | + to distribute corresponding source code. (This alternative is | ||
| 151 | + allowed only for noncommercial distribution and only if you | ||
| 152 | + received the program in object code or executable form with such | ||
| 153 | + an offer, in accord with Subsection b above.) | ||
| 154 | + | ||
| 155 | +The source code for a work means the preferred form of the work for | ||
| 156 | +making modifications to it. For an executable work, complete source | ||
| 157 | +code means all the source code for all modules it contains, plus any | ||
| 158 | +associated interface definition files, plus the scripts used to | ||
| 159 | +control compilation and installation of the executable. However, as a | ||
| 160 | +special exception, the source code distributed need not include | ||
| 161 | +anything that is normally distributed (in either source or binary | ||
| 162 | +form) with the major components (compiler, kernel, and so on) of the | ||
| 163 | +operating system on which the executable runs, unless that component | ||
| 164 | +itself accompanies the executable. | ||
| 165 | + | ||
| 166 | +If distribution of executable or object code is made by offering | ||
| 167 | +access to copy from a designated place, then offering equivalent | ||
| 168 | +access to copy the source code from the same place counts as | ||
| 169 | +distribution of the source code, even though third parties are not | ||
| 170 | +compelled to copy the source along with the object code. | ||
| 171 | + | ||
| 172 | + 4. You may not copy, modify, sublicense, or distribute the Program | ||
| 173 | +except as expressly provided under this License. Any attempt | ||
| 174 | +otherwise to copy, modify, sublicense or distribute the Program is | ||
| 175 | +void, and will automatically terminate your rights under this License. | ||
| 176 | +However, parties who have received copies, or rights, from you under | ||
| 177 | +this License will not have their licenses terminated so long as such | ||
| 178 | +parties remain in full compliance. | ||
| 179 | + | ||
| 180 | + 5. You are not required to accept this License, since you have not | ||
| 181 | +signed it. However, nothing else grants you permission to modify or | ||
| 182 | +distribute the Program or its derivative works. These actions are | ||
| 183 | +prohibited by law if you do not accept this License. Therefore, by | ||
| 184 | +modifying or distributing the Program (or any work based on the | ||
| 185 | +Program), you indicate your acceptance of this License to do so, and | ||
| 186 | +all its terms and conditions for copying, distributing or modifying | ||
| 187 | +the Program or works based on it. | ||
| 188 | + | ||
| 189 | + 6. Each time you redistribute the Program (or any work based on the | ||
| 190 | +Program), the recipient automatically receives a license from the | ||
| 191 | +original licensor to copy, distribute or modify the Program subject to | ||
| 192 | +these terms and conditions. You may not impose any further | ||
| 193 | +restrictions on the recipients' exercise of the rights granted herein. | ||
| 194 | +You are not responsible for enforcing compliance by third parties to | ||
| 195 | +this License. | ||
| 196 | + | ||
| 197 | + 7. If, as a consequence of a court judgment or allegation of patent | ||
| 198 | +infringement or for any other reason (not limited to patent issues), | ||
| 199 | +conditions are imposed on you (whether by court order, agreement or | ||
| 200 | +otherwise) that contradict the conditions of this License, they do not | ||
| 201 | +excuse you from the conditions of this License. If you cannot | ||
| 202 | +distribute so as to satisfy simultaneously your obligations under this | ||
| 203 | +License and any other pertinent obligations, then as a consequence you | ||
| 204 | +may not distribute the Program at all. For example, if a patent | ||
| 205 | +license would not permit royalty-free redistribution of the Program by | ||
| 206 | +all those who receive copies directly or indirectly through you, then | ||
| 207 | +the only way you could satisfy both it and this License would be to | ||
| 208 | +refrain entirely from distribution of the Program. | ||
| 209 | + | ||
| 210 | +If any portion of this section is held invalid or unenforceable under | ||
| 211 | +any particular circumstance, the balance of the section is intended to | ||
| 212 | +apply and the section as a whole is intended to apply in other | ||
| 213 | +circumstances. | ||
| 214 | + | ||
| 215 | +It is not the purpose of this section to induce you to infringe any | ||
| 216 | +patents or other property right claims or to contest validity of any | ||
| 217 | +such claims; this section has the sole purpose of protecting the | ||
| 218 | +integrity of the free software distribution system, which is | ||
| 219 | +implemented by public license practices. Many people have made | ||
| 220 | +generous contributions to the wide range of software distributed | ||
| 221 | +through that system in reliance on consistent application of that | ||
| 222 | +system; it is up to the author/donor to decide if he or she is willing | ||
| 223 | +to distribute software through any other system and a licensee cannot | ||
| 224 | +impose that choice. | ||
| 225 | + | ||
| 226 | +This section is intended to make thoroughly clear what is believed to | ||
| 227 | +be a consequence of the rest of this License. | ||
| 228 | + | ||
| 229 | + 8. If the distribution and/or use of the Program is restricted in | ||
| 230 | +certain countries either by patents or by copyrighted interfaces, the | ||
| 231 | +original copyright holder who places the Program under this License | ||
| 232 | +may add an explicit geographical distribution limitation excluding | ||
| 233 | +those countries, so that distribution is permitted only in or among | ||
| 234 | +countries not thus excluded. In such case, this License incorporates | ||
| 235 | +the limitation as if written in the body of this License. | ||
| 236 | + | ||
| 237 | + 9. The Free Software Foundation may publish revised and/or new versions | ||
| 238 | +of the General Public License from time to time. Such new versions will | ||
| 239 | +be similar in spirit to the present version, but may differ in detail to | ||
| 240 | +address new problems or concerns. | ||
| 241 | + | ||
| 242 | +Each version is given a distinguishing version number. If the Program | ||
| 243 | +specifies a version number of this License which applies to it and "any | ||
| 244 | +later version", you have the option of following the terms and conditions | ||
| 245 | +either of that version or of any later version published by the Free | ||
| 246 | +Software Foundation. If the Program does not specify a version number of | ||
| 247 | +this License, you may choose any version ever published by the Free Software | ||
| 248 | +Foundation. | ||
| 249 | + | ||
| 250 | + 10. If you wish to incorporate parts of the Program into other free | ||
| 251 | +programs whose distribution conditions are different, write to the author | ||
| 252 | +to ask for permission. For software which is copyrighted by the Free | ||
| 253 | +Software Foundation, write to the Free Software Foundation; we sometimes | ||
| 254 | +make exceptions for this. Our decision will be guided by the two goals | ||
| 255 | +of preserving the free status of all derivatives of our free software and | ||
| 256 | +of promoting the sharing and reuse of software generally. | ||
| 257 | + | ||
| 258 | + NO WARRANTY | ||
| 259 | + | ||
| 260 | + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY | ||
| 261 | +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN | ||
| 262 | +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES | ||
| 263 | +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED | ||
| 264 | +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
| 265 | +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS | ||
| 266 | +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE | ||
| 267 | +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, | ||
| 268 | +REPAIR OR CORRECTION. | ||
| 269 | + | ||
| 270 | + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 271 | +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR | ||
| 272 | +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, | ||
| 273 | +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING | ||
| 274 | +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED | ||
| 275 | +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY | ||
| 276 | +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER | ||
| 277 | +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | ||
| 278 | +POSSIBILITY OF SUCH DAMAGES. | ||
| 279 | + | ||
| 280 | + END OF TERMS AND CONDITIONS | ||
| 281 | + | ||
| 282 | + How to Apply These Terms to Your New Programs | ||
| 283 | + | ||
| 284 | + If you develop a new program, and you want it to be of the greatest | ||
| 285 | +possible use to the public, the best way to achieve this is to make it | ||
| 286 | +free software which everyone can redistribute and change under these terms. | ||
| 287 | + | ||
| 288 | + To do so, attach the following notices to the program. It is safest | ||
| 289 | +to attach them to the start of each source file to most effectively | ||
| 290 | +convey the exclusion of warranty; and each file should have at least | ||
| 291 | +the "copyright" line and a pointer to where the full notice is found. | ||
| 292 | + | ||
| 293 | + <one line to give the program's name and a brief idea of what it does.> | ||
| 294 | + Copyright (C) <year> <name of author> | ||
| 295 | + | ||
| 296 | + This program is free software; you can redistribute it and/or modify | ||
| 297 | + it under the terms of the GNU General Public License as published by | ||
| 298 | + the Free Software Foundation; either version 2 of the License, or | ||
| 299 | + (at your option) any later version. | ||
| 300 | + | ||
| 301 | + This program is distributed in the hope that it will be useful, | ||
| 302 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 303 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 304 | + GNU General Public License for more details. | ||
| 305 | + | ||
| 306 | + You should have received a copy of the GNU General Public License along | ||
| 307 | + with this program; if not, write to the Free Software Foundation, Inc., | ||
| 308 | + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 309 | + | ||
| 310 | +Also add information on how to contact you by electronic and paper mail. | ||
| 311 | + | ||
| 312 | +If the program is interactive, make it output a short notice like this | ||
| 313 | +when it starts in an interactive mode: | ||
| 314 | + | ||
| 315 | + Gnomovision version 69, Copyright (C) year name of author | ||
| 316 | + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | ||
| 317 | + This is free software, and you are welcome to redistribute it | ||
| 318 | + under certain conditions; type `show c' for details. | ||
| 319 | + | ||
| 320 | +The hypothetical commands `show w' and `show c' should show the appropriate | ||
| 321 | +parts of the General Public License. Of course, the commands you use may | ||
| 322 | +be called something other than `show w' and `show c'; they could even be | ||
| 323 | +mouse-clicks or menu items--whatever suits your program. | ||
| 324 | + | ||
| 325 | +You should also get your employer (if you work as a programmer) or your | ||
| 326 | +school, if any, to sign a "copyright disclaimer" for the program, if | ||
| 327 | +necessary. Here is a sample; alter the names: | ||
| 328 | + | ||
| 329 | + Yoyodyne, Inc., hereby disclaims all copyright interest in the program | ||
| 330 | + `Gnomovision' (which makes passes at compilers) written by James Hacker. | ||
| 331 | + | ||
| 332 | + <signature of Ty Coon>, 1 April 1989 | ||
| 333 | + Ty Coon, President of Vice | ||
| 334 | + | ||
| 335 | +This General Public License does not permit incorporating your program into | ||
| 336 | +proprietary programs. If your program is a subroutine library, you may | ||
| 337 | +consider it more useful to permit linking proprietary applications with the | ||
| 338 | +library. If this is what you want to do, use the GNU Lesser General | ||
| 339 | +Public License instead of this License. | ||
| @@ -0,0 +1,18 @@ | |||
| 1 | +.PHONY: all clean test help | ||
| 2 | + | ||
| 3 | +VERSION := $(shell cat VERSION) | ||
| 4 | +OUTPUT := makeself-$(VERSION).run | ||
| 5 | + | ||
| 6 | +all: $(OUTPUT) | ||
| 7 | + | ||
| 8 | +$(OUTPUT): makeself.sh makeself-header.sh VERSION | ||
| 9 | + ./make-release.sh | ||
| 10 | + | ||
| 11 | +clean: | ||
| 12 | + $(RM) makeself-*.run | ||
| 13 | + | ||
| 14 | +test: | ||
| 15 | + ./test/run-tests.sh | ||
| 16 | + | ||
| 17 | +help: | ||
| 18 | + $(info Targets: all $(OUTPUT) clean test help) | ||
| @@ -0,0 +1,253 @@ | |||
| 1 | +[](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +# makeself - Make self-extractable archives on Unix | ||
| 5 | + | ||
| 6 | +[makeself.sh][1] is a small shell script that generates a self-extractable | ||
| 7 | +compressed tar archive from a directory. The resulting file appears as a shell script | ||
| 8 | +(many of those have a **.run** suffix), and can be launched as is. The archive | ||
| 9 | +will then uncompress itself to a temporary directory and an optional arbitrary | ||
| 10 | +command will be executed (for example an installation script). This is pretty | ||
| 11 | +similar to archives generated with WinZip Self-Extractor in the Windows world. | ||
| 12 | +Makeself archives also include checksums for integrity self-validation (CRC | ||
| 13 | +and/or MD5/SHA256 checksums). | ||
| 14 | + | ||
| 15 | +The makeself.sh script itself is used only to create the archives from a | ||
| 16 | +directory of files. The resultant archive is actually a compressed (using | ||
| 17 | +gzip, bzip2, or compress) TAR archive, with a small shell script stub at the | ||
| 18 | +beginning. This small stub performs all the steps of extracting the files, | ||
| 19 | +running the embedded command, and removing the temporary files when done. | ||
| 20 | +All the user has to do to install the software contained in such an | ||
| 21 | +archive is to "run" the archive, i.e **sh nice-software.run**. I recommend | ||
| 22 | +using the ".run" (which was introduced by some Makeself archives released by | ||
| 23 | +Loki Software) or ".sh" suffix for such archives not to confuse the users, | ||
| 24 | +so that they will know they are actually shell scripts (with quite a lot of binary data | ||
| 25 | +attached to them though!). | ||
| 26 | + | ||
| 27 | +I am trying to keep the code of this script as portable as possible, i.e it is | ||
| 28 | +not relying on any bash-specific features and only calls commands that are | ||
| 29 | +installed on any functioning UNIX-compatible system. This script as well as | ||
| 30 | +the archives it generates should run on any Unix flavor, with any compatible | ||
| 31 | +Bourne shell, provided of course that the compression programs are available. | ||
| 32 | + | ||
| 33 | +Makeself has been rewritten and tested on the following platforms : | ||
| 34 | + | ||
| 35 | +* Linux (all distributions) | ||
| 36 | +* Sun Solaris (8 and above) | ||
| 37 | +* HP-UX (tested on 11.0 and 11i on HPPA RISC) | ||
| 38 | +* SCO OpenUnix and OpenServer | ||
| 39 | +* IBM AIX | ||
| 40 | +* macOS (Darwin) | ||
| 41 | +* SGI IRIX 6.5 | ||
| 42 | +* FreeBSD | ||
| 43 | +* OpenBSD | ||
| 44 | +* NetBSD | ||
| 45 | +* UnicOS / Cray | ||
| 46 | +* Windows (Cygwin, WSL) | ||
| 47 | + | ||
| 48 | +If you successfully run Makeself and/or archives created with it on another | ||
| 49 | +system, then please [let me know][2]! | ||
| 50 | + | ||
| 51 | +Examples of publicly available archives made using makeself are : | ||
| 52 | + | ||
| 53 | +* Game patches and installers for [Id Software][3] games like Quake 3 for Linux or Return To Castle Wolfenstein ; | ||
| 54 | +* All game patches released by [Loki Software][4] for the Linux version of popular games ; | ||
| 55 | +* The [nVidia drivers][5] for Linux | ||
| 56 | +* The installer for the Linux version of [Google Earth][6] | ||
| 57 | +* The [VirtualBox][7] installers for Linux | ||
| 58 | +* The [Makeself][1] distribution itself ;-) | ||
| 59 | +* and countless others... | ||
| 60 | + | ||
| 61 | +**Important note for Apache users:** By default, most Web servers will think that Makeself archives are regular text files and thus they may show up as text in a Web browser. The correct way to prevent this is to add a MIME type for this file format, like so (in httpd.conf) : | ||
| 62 | + | ||
| 63 | +`AddType application/x-makeself .run` | ||
| 64 | + | ||
| 65 | +**Important note for certain GNU/Linux distributions:** Archives created with Makeself prior to v2.1.2 were using an old syntax for the _head_ and _tail_ Unix commands that is being progressively obsoleted in their GNU forms. Therefore you may have problems uncompressing some of these archives. A workaround for this is to set the environment variable $_POSIX2_VERSION to enable the old syntax, i.e. : | ||
| 66 | + | ||
| 67 | +`export _POSIX2_VERSION=199209` | ||
| 68 | + | ||
| 69 | +## Usage | ||
| 70 | + | ||
| 71 | +The syntax of makeself is the following: | ||
| 72 | + | ||
| 73 | +```sh | ||
| 74 | +makeself.sh [args] archive_dir file_name label startup_script [script_args] | ||
| 75 | +``` | ||
| 76 | + | ||
| 77 | + * _args_ are optional options for Makeself. The available ones are : | ||
| 78 | + | ||
| 79 | + * **`--version`** : Prints the version number on stdout, then exits immediately | ||
| 80 | + * **`--gzip`** : Use gzip for compression (the default on platforms on which gzip is commonly available, like Linux) | ||
| 81 | + * **`--bzip2`** : Use bzip2 instead of gzip for better compression. The bzip2 command must be available in the command path. It is recommended that the archive extension be set to something like '.bz2.run', so that potential users know that they'll need bzip2 to extract it. | ||
| 82 | + * **`--bzip3`** : Use bzip3 instead of gzip for better compression. | ||
| 83 | + * **`--pbzip2`** : Use pbzip2 instead of gzip for better and faster compression on machines having multiple CPUs. The pbzip2 command must be available in the command path. It is recommended that the archive extension be set to something like '.bz2.run', so that potential users know that they'll need bzip2 to extract it. | ||
| 84 | + * **`--xz`** : Use xz instead of gzip for better compression. The xz command must be available in the command path. It is recommended that the archive extension be set to something like '.xz.run' for the archive, so that potential users know that they'll need xz to extract it. | ||
| 85 | + * **`--lzo`** : Use lzop instead of gzip for better compression. The lzop command must be available in the command path. It is recommended that the archive extension be set to something like `.lzo.run` for the archive, so that potential users know that they'll need lzop to extract it. | ||
| 86 | + * **`--lz4`** : Use lz4 instead of gzip for better compression. The lz4 command must be available in the command path. It is recommended that the archive extension be set to something like '.lz4.run' for the archive, so that potential users know that they'll need lz4 to extract it. | ||
| 87 | + * **`--zstd`** : Use zstd instead of gzip for better compression. The zstd command must be available in the command path. It is recommended that the archive extension be set to something like '.zstd.run' for the archive, so that potential users know that they'll need zstd to extract it. | ||
| 88 | + * **`--pigz`** : Use pigz for compression. | ||
| 89 | + * **`--base64`** : Encode the archive to ASCII in Base64 format instead of compressing (base64 command required). | ||
| 90 | + * **`--gpg-encrypt`** : Encrypt the archive using `gpg -ac -z $COMPRESS_LEVEL`. This will prompt for a password to encrypt with. Assumes that potential users have `gpg` installed. | ||
| 91 | + * **`--ssl-encrypt`** : Encrypt the archive using `openssl aes-256-cbc -a -salt`. This will prompt for a password to encrypt with. Assumes that the potential users have the OpenSSL tools installed. | ||
| 92 | + * **`--compress`** : Use the UNIX `compress` command to compress the data. This should be the default on all platforms that don't have gzip available. | ||
| 93 | + * **`--nocomp`** : Do not use any compression for the archive, which will then be an uncompressed TAR. | ||
| 94 | + * **`--complevel`** : Specify the compression level for gzip, bzip2, pbzip2, zstd, xz, lzo or lz4. (defaults to 9) | ||
| 95 | + * **`--threads`** : Specify the number of threads to be used by compressors that support parallelization. Omit to use compressor's default. Most useful (and required) for opting into xz's threading, usually with `--threads=0` for all available cores. pbzip2 and pigz are parallel by default, and setting this value allows limiting the number of threads they use. | ||
| 96 | + * **`--notemp`** : The generated archive will not extract the files to a temporary directory, but in a new directory created in the current directory. This is better to distribute software packages that may extract and compile by themselves (i.e. launch the compilation through the embedded script). | ||
| 97 | + * **`--current`** : Files will be extracted to the current directory, instead of in a subdirectory. This option implies `--notemp` above. | ||
| 98 | + * **`--follow`** : Follow the symbolic links inside of the archive directory, i.e. store the files that are being pointed to instead of the links themselves. | ||
| 99 | + * **`--append`** _(new in 2.1.x)_: Append data to an existing archive, instead of creating a new one. In this mode, the settings from the original archive are reused (compression type, label, embedded script), and thus don't need to be specified again on the command line. | ||
| 100 | + * **`--header`** : Makeself uses a separate file to store the header stub, called `makeself-header.sh`. By default, it is assumed that it is stored in the same location as makeself.sh. This option can be used to specify its actual location if it is stored someplace else. | ||
| 101 | + * **`--cleanup`** : Specify a script that is run when execution is interrupted or finishes successfully. The script is executed with the same environment and initial `script_args` as `startup_script`. | ||
| 102 | + * **`--copy`** : Upon extraction, the archive will first extract itself to a temporary directory. The main application of this is to allow self-contained installers stored in a Makeself archive on a CD, when the installer program will later need to unmount the CD and allow a new one to be inserted. This prevents "Filesystem busy" errors for installers that span multiple CDs. | ||
| 103 | + * **`--nox11`** : Disable the automatic spawning of a new terminal in X11. | ||
| 104 | + * **`--nowait`** : When executed from a new X11 terminal, disable the user prompt at the end of the script execution. | ||
| 105 | + * **`--nomd5`** and **`--nocrc`** : Disable the creation of a MD5 / CRC checksum for the archive. This speeds up the extraction process if integrity checking is not necessary. | ||
| 106 | + * **`--sha256`** : Adds a SHA256 checksum for the archive. This is in addition to the MD5 / CRC checksums unless `--nomd5` is also used. | ||
| 107 | + * **`--lsm` _file_** : Provide a Linux Software Map (LSM) file to makeself, that will be embedded in the generated archive. LSM files are describing a software package in a way that is easily parseable. The LSM entry can then be later retrieved using the `--lsm` argument to the archive. An example of a LSM file is provided with Makeself. | ||
| 108 | + * **`--tar-format opt`** : Specify the tar archive format (default is ustar); you may use any value accepted by your tar command (such as posix, v7, etc). | ||
| 109 | + * **`--tar-extra opt`** : Append more options to the tar command line. | ||
| 110 | + | ||
| 111 | + For instance, in order to exclude the `.git` directory from the packaged archive directory using the GNU `tar`, one can use `makeself.sh --tar-extra "--exclude=.git" ...` | ||
| 112 | + | ||
| 113 | + * **`--keep-umask`** : Keep the umask set to shell default, rather than overriding when executing the self-extracting archive. | ||
| 114 | + * **`--packaging-date date`** : Use provided string as the packaging date instead of the current date. | ||
| 115 | + * **`--license` _file_** : Append a license file. | ||
| 116 | + * **`--nooverwrite`** : Do not extract the archive if the specified target directory already exists. | ||
| 117 | + * **`--help-header` _file_** : Add a header to the archive's `--help` output. | ||
| 118 | + * `archive_dir` is the name of the directory that contains the files to be archived | ||
| 119 | + * `file_name` is the name of the archive to be created | ||
| 120 | + * `label` is an arbitrary text string describing the package. It will be displayed while extracting the files. | ||
| 121 | + * `startup_script` is the command to be executed _from within_ the directory of extracted files. Thus, if you wish to execute a program contained in this directory, you must prefix your command with `./`. For example, `./program` will be fine. The `script_args` are additional arguments for this command. | ||
| 122 | + Note that `startup_script` and its arguments are not strictly required for archives that don't extract in a temporary directory (i.e. when using `--notemp`). | ||
| 123 | + | ||
| 124 | +Here is an example, assuming the user has a package image stored in a **/home/joe/mysoft**, and he wants to generate a self-extracting package named | ||
| 125 | +**mysoft.sh**, which will launch the "setup" script initially stored in /home/joe/mysoft : | ||
| 126 | + | ||
| 127 | +```sh | ||
| 128 | +makeself.sh /home/joe/mysoft mysoft.sh "Joe's Nice Software Package" ./setup | ||
| 129 | +``` | ||
| 130 | + | ||
| 131 | +Here is also how I created the [makeself.run][9] archive which contains the Makeself distribution : | ||
| 132 | + | ||
| 133 | +`makeself.sh --notemp makeself makeself.run "Makeself by Stephane Peter" echo "Makeself has extracted itself"` | ||
| 134 | + | ||
| 135 | +Archives generated with Makeself can be passed the following arguments: | ||
| 136 | + | ||
| 137 | +* **`--keep`** : Prevent the files to be extracted in a temporary directory that will be removed after the embedded script's execution. The files will then be extracted in the current working directory and will stay here until you remove them. | ||
| 138 | +* **`--verbose`** : Will prompt the user before executing the embedded command | ||
| 139 | +* **`--target dir`** : Allows to extract the archive in an arbitrary place. | ||
| 140 | +* **`--nox11`** : Do not spawn a X11 terminal. | ||
| 141 | +* **`--confirm`** : Prompt the user for confirmation before running the embedded command. | ||
| 142 | +* **`--info`** : Print out general information about the archive (does not extract). | ||
| 143 | +* **`--lsm`** : Print out the LSM entry, if it is present. | ||
| 144 | +* **`--list`** : List the files in the archive. | ||
| 145 | +* **`--check`** : Check the archive for integrity using the embedded checksums. Does not extract the archive. | ||
| 146 | +* **`--nochown`** : By default, a `chown -R` command is run on the target directory after extraction, so that all files belong to the current user. This is mostly needed if you are running as root, as tar will then try to recreate the initial user ownerships. You may disable this behavior with this flag. | ||
| 147 | +* **`--tar`** : Run the tar command on the contents of the archive, using the following arguments as parameter for the command. | ||
| 148 | +* **`--noexec`** : Do not run the embedded script after extraction. | ||
| 149 | +* **`--noexec-cleanup`** : Do not run the embedded cleanup script. | ||
| 150 | +* **`--nodiskspace`** : Do not check for available disk space before attempting to extract. | ||
| 151 | +* **`--cleanup-args`** : Specify arguments to be passed to the cleanup script. Wrap value in quotes to specify multiple arguments. | ||
| 152 | + | ||
| 153 | +Any subsequent arguments to the archive will be passed as additional arguments to the embedded command. You must explicitly use the `--` special command-line construct before any such options to make sure that Makeself will not try to interpret them. | ||
| 154 | + | ||
| 155 | +## Startup Script | ||
| 156 | + | ||
| 157 | +The startup script must be a regular Shell script. | ||
| 158 | + | ||
| 159 | +Within the startup script, you can use the `$USER_PWD` variable to get the path of the folder from which the self-extracting script is executed. This is especially useful to access files that are located in the same folder as the script, as shown in the example below. | ||
| 160 | + | ||
| 161 | +```sh | ||
| 162 | +my-self-extracting-script.sh --fooBarFileParameter foo.bar | ||
| 163 | +``` | ||
| 164 | + | ||
| 165 | +## Building and Testing | ||
| 166 | + | ||
| 167 | +Clone the git repo and execute `git submodule update --init --recursive` to obtain all submodules. | ||
| 168 | + | ||
| 169 | +* To make a release: `make` | ||
| 170 | +* To run all tests: `make test` | ||
| 171 | + | ||
| 172 | +## Maven Usage | ||
| 173 | + | ||
| 174 | +Makeself is now supported by the following maven plugin [makeself-maven-plugin](https://github.com/hazendaz/makeself-maven-plugin). Please refer to project for usage and report any bugs in regards to maven plugin on that project. | ||
| 175 | + | ||
| 176 | +## License | ||
| 177 | + | ||
| 178 | +Makeself itself is covered by the [GNU General Public License][8] (GPL) version 2 and above. Archives generated by Makeself don't have to be placed under this license (although I encourage it ;-)), since the archive itself is merely data for Makeself. | ||
| 179 | + | ||
| 180 | +## Contributing | ||
| 181 | + | ||
| 182 | +I will gladly consider merging your pull requests on the [GitHub][10] repository. However, please keep the following in mind: | ||
| 183 | + | ||
| 184 | +* One of the main purposes of Makeself is portability. Do not submit patches that will break supported platforms. The more platform-agnostic, the better. | ||
| 185 | +* Please explain clearly what the purpose of the patch is, and how you achieved it. | ||
| 186 | + | ||
| 187 | +## Download | ||
| 188 | + | ||
| 189 | +Get the latest official distribution [here][9] (version 2.5.0). | ||
| 190 | + | ||
| 191 | +The latest development version can be grabbed from [GitHub][10]. Feel free to submit any patches there through the fork and pull request process. | ||
| 192 | + | ||
| 193 | +## Version history | ||
| 194 | + | ||
| 195 | +* **v1.0:** Initial public release | ||
| 196 | +* **v1.1:** The archive can be passed parameters that will be passed on to the embedded script, thanks to John C. Quillan | ||
| 197 | +* **v1.2:** Cosmetic updates, support for bzip2 compression and non-temporary archives. Many ideas thanks to Francois Petitjean. | ||
| 198 | +* **v1.3:** More patches from Bjarni R. Einarsson and Francois Petitjean: Support for no compression (`--nocomp`), script is no longer mandatory, automatic launch in an xterm, optional verbose output, and -target archive option to indicate where to extract the files. | ||
| 199 | +* **v1.4:** Many patches from Francois Petitjean: improved UNIX compatibility, automatic integrity checking, support of LSM files to get info on the package at run time.. | ||
| 200 | +* **v1.5.x:** A lot of bugfixes, and many other patches, including automatic verification through the usage of checksums. Version 1.5.5 was the stable release for a long time, even though the Web page didn't get updated ;-). Makeself was also officially made a part of the [Loki Setup installer][11], and its source is being maintained as part of this package. | ||
| 201 | +* **v2.0:** Complete internal rewrite of Makeself. The command-line parsing was vastly improved, the overall maintenance of the package was greatly improved by separating the stub from makeself.sh. Also Makeself was ported and tested to a variety of Unix platforms. | ||
| 202 | +* **v2.0.1:** First public release of the new 2.0 branch. Prior versions are officially obsoleted. This release introduced the `--copy` argument that was introduced in response to a need for the [UT2K3][12] Linux installer. | ||
| 203 | +* **v2.1.0:** Big change : Makeself can now support multiple embedded tarballs, each stored separately with their own checksums. An existing archive can be updated with the `--append` flag. Checksums are also better managed, and the `--nochown` option for archives appeared. | ||
| 204 | +* **v2.1.1:** Fixes related to the Unix compression (compress command). Some Linux distributions made the insane choice to make it unavailable, even though gzip is capable of uncompressing these files, plus some more bugfixes in the extraction and checksum code. | ||
| 205 | +* **v2.1.2:** Some bug fixes. Use head -n to avoid problems with POSIX conformance. | ||
| 206 | +* **v2.1.3:** Bug fixes with the command line when spawning terminals. Added `--tar`, `--noexec` for archives. Added `--nomd5` and `--nocrc` to avoid creating checksums in archives. The embedded script is now run through "eval". The `--info` output now includes the command used to create the archive. A man page was contributed by Bartosz Fenski. | ||
| 207 | +* **v2.1.4:** Fixed `--info` output. Generate random directory name when extracting files to . to avoid problems. Better handling of errors with wrong permissions for the directory containing the files. Avoid some race conditions, Unset the $CDPATH variable to avoid problems if it is set. Better handling of dot files in the archive directory. | ||
| 208 | +* **v2.1.5:** Made the md5sum detection consistent with the header code. Check for the presence of the archive directory. Added `--encrypt` for symmetric encryption through gpg (Eric Windisch). Added support for the digest command on Solaris 10 for MD5 checksums. Check for available disk space before extracting to the target directory (Andreas Schweitzer). Allow extraction to run asynchronously (patch by Peter Hatch). Use file descriptors internally to avoid error messages (patch by Kay Tiong Khoo). | ||
| 209 | +* **v2.1.6:** Replaced one dot per file progress with a realtime progress percentage and a spinning cursor. Added `--noprogress` to prevent showing the progress during the decompression. Added `--target` dir to allow extracting directly to a target directory. (Guy Baconniere) | ||
| 210 | +* **v2.2.0:** First major new release in years! Includes many bugfixes and user contributions. Please look at the [project page on Github][10] for all the details. | ||
| 211 | +* **v2.3.0:** Support for archive encryption via GPG or OpenSSL. Added LZO and LZ4 compression support. Options to set the packaging date and stop the umask from being overriden. Optionally ignore check for available disk space when extracting. New option to check for root permissions before extracting. | ||
| 212 | +* **v2.3.1:** Various compatibility updates. Added unit tests for Travis CI in the GitHub repo. New `--tar-extra`, `--untar-extra`, `--gpg-extra`, `--gpg-asymmetric-encrypt-sign` options. | ||
| 213 | +* **v2.4.0:** Added optional support for SHA256 archive integrity checksums. | ||
| 214 | +* **v2.4.2:** New --cleanup and --cleanup-args arguments for cleanup scripts. Added threading support for supported compressors. Now supports zstd compression. | ||
| 215 | +* **v2.4.3:** Make explicit POSIX tar archives for increased compatibility. | ||
| 216 | +* **v2.4.4:** Fixed various compatibility issues (no longer use POSIX tar archives), Github Actions to check on Solaris and FreeBSD. | ||
| 217 | +* **v2.4.5:** Added `--tar-format` option to set the tar archive format (default is ustar) | ||
| 218 | +* **v2.5.0:** Expended support to NetBSD, OpenBSD, Busybox and other minimal distributions such as Alpine Linux. Added bzip3 compression support and expanded GPG arguments. | ||
| 219 | + | ||
| 220 | +## Links | ||
| 221 | + | ||
| 222 | +* Check out the ["Loki Setup"][11] installer, used to install many Linux games and other applications, and of which I am the co-author. Since the demise of Loki, I am now the official maintainer of the project, and it is now being hosted here on GitHub. | ||
| 223 | +* Bjarni R. Einarsson also wrote the **setup.sh** installer script, inspired by Makeself. [Check it out !][14] | ||
| 224 | + | ||
| 225 | +## Contact | ||
| 226 | + | ||
| 227 | +This script was written by [Stéphane Peter][15] (megastep at megastep.org). Any enhancements and suggestions are welcome. | ||
| 228 | + | ||
| 229 | +Contributions were included from John C. Quillan, Bjarni R. Einarsson, | ||
| 230 | +Francois Petitjean, Ryan C. Gordon, and many contributors on GitHub. If you think I forgot | ||
| 231 | +your name, don't hesitate to contact me. | ||
| 232 | + | ||
| 233 | +This project is now hosted on GitHub. Feel free to submit patches and bug reports on the [project page][10]. | ||
| 234 | + | ||
| 235 | +* * * | ||
| 236 | + | ||
| 237 | +[Stephane Peter][2] | ||
| 238 | + | ||
| 239 | + [1]: http://makeself.io/ | ||
| 240 | + [2]: mailto:megastep@megastep.org | ||
| 241 | + [3]: http://www.idsoftware.com/ | ||
| 242 | + [4]: http://www.lokigames.com/products/myth2/updates.php3 | ||
| 243 | + [5]: http://www.nvidia.com/ | ||
| 244 | + [6]: http://earth.google.com/ | ||
| 245 | + [7]: http://www.virtualbox.org/ | ||
| 246 | + [8]: http://www.gnu.org/copyleft/gpl.html | ||
| 247 | + [9]: https://github.com/megastep/makeself/releases/download/release-2.5.0/makeself-2.5.0.run | ||
| 248 | + [10]: https://github.com/megastep/makeself | ||
| 249 | + [11]: https://github.com/megastep/loki_setup/ | ||
| 250 | + [12]: http://www.unrealtournament2003.com/ | ||
| 251 | + [13]: http://www.icculus.org/ | ||
| 252 | + [14]: http://bre.klaki.net/programs/setup.sh/ | ||
| 253 | + [15]: https://stephanepeter.com/ | ||
| @@ -0,0 +1,9 @@ | |||
| 1 | +#!/bin/sh | ||
| 2 | +# | ||
| 3 | +# Create a distributable archive of the current version of Makeself | ||
| 4 | + | ||
| 5 | +VER=`cat VERSION` | ||
| 6 | +mkdir -p /tmp/makeself-$VER release | ||
| 7 | +cp -pPR makeself* README.md COPYING VERSION /tmp/makeself-$VER/ | ||
| 8 | +./makeself.sh --notemp /tmp/makeself-$VER release/makeself-$VER.run "Makeself v$VER" echo "Makeself has extracted itself" | ||
| 9 | + | ||
| @@ -0,0 +1,681 @@ | |||
| 1 | +cat << EOF > "$archname" | ||
| 2 | +#!/bin/bash | ||
| 3 | +# This script was generated using Makeself $MS_VERSION | ||
| 4 | +# The license covering this archive and its contents, if any, is wholly independent of the Makeself license (GPL) | ||
| 5 | + | ||
| 6 | +ORIG_UMASK=\`umask\` | ||
| 7 | + | ||
| 8 | +SHA="$SHAsum" | ||
| 9 | +SHA_ARG="" | ||
| 10 | +SIGNATURE="$Signature" | ||
| 11 | +TMPROOT=\${TMPDIR:="/home/tmpdir"} | ||
| 12 | + | ||
| 13 | +if ! test -d "\$TMPROOT"; then | ||
| 14 | + TMPROOT="\$HOME" | ||
| 15 | +fi | ||
| 16 | +if ! test -d "\$TMPROOT"; then | ||
| 17 | + TMPROOT="\$PWD" | ||
| 18 | +fi | ||
| 19 | +export TMPDIR="\$TMPROOT" | ||
| 20 | +USER_PWD="\$PWD" | ||
| 21 | +if ! test -d "\$USER_PWD"; then | ||
| 22 | + exit 1 | ||
| 23 | +fi | ||
| 24 | +export USER_PWD | ||
| 25 | +ARCHIVE_DIR=\`dirname "\$0"\` | ||
| 26 | +export ARCHIVE_DIR | ||
| 27 | + | ||
| 28 | +name_of_file="\$0 " | ||
| 29 | +package_name=\`echo \$name_of_file | cut -d "/" -f2 | sed "s/.run//g" \` | ||
| 30 | +pwd_of_file="\$PWD" | ||
| 31 | +label="$LABEL" | ||
| 32 | +script="$SCRIPT" | ||
| 33 | +scriptargs="$SCRIPTARGS" | ||
| 34 | +cleanup_script="${CLEANUP_SCRIPT}" | ||
| 35 | +licensetxt="$LICENSE" | ||
| 36 | +helpheader='$HELPHEADER' | ||
| 37 | +targetdir="$archdirname" | ||
| 38 | +filesizes="$filesizes" | ||
| 39 | +totalsize="$totalsize" | ||
| 40 | +keep="$KEEP" | ||
| 41 | +nooverwrite="$NOOVERWRITE" | ||
| 42 | +quiet="n" | ||
| 43 | +accept="n" | ||
| 44 | +nodiskspace="n" | ||
| 45 | +export_conf="$EXPORT_CONF" | ||
| 46 | +decrypt_cmd="$DECRYPT_CMD" | ||
| 47 | +skip="$SKIP" | ||
| 48 | +PACKAGE_LOG_NAME=makeself | ||
| 49 | +readonly user_n=\$(whoami) | ||
| 50 | +info_record_path="\$HOME/log/makeself" | ||
| 51 | +info_record_file="makeself.log" | ||
| 52 | +info_record_file_bak="makeself.log.bak" | ||
| 53 | +log_file=\$info_record_path/\$info_record_file | ||
| 54 | +LOG_SIZE_THRESHOLD=1024000 | ||
| 55 | + | ||
| 56 | +print_cmd_arg="" | ||
| 57 | +if type printf > /dev/null; then | ||
| 58 | + print_cmd="printf" | ||
| 59 | +elif test -x /usr/ucb/echo; then | ||
| 60 | + print_cmd="/usr/ucb/echo" | ||
| 61 | +else | ||
| 62 | + print_cmd="echo" | ||
| 63 | +fi | ||
| 64 | + | ||
| 65 | +if test -d /usr/xpg4/bin; then | ||
| 66 | + PATH=/usr/xpg4/bin:\$PATH | ||
| 67 | + export PATH | ||
| 68 | +fi | ||
| 69 | + | ||
| 70 | +if test -d /usr/sfw/bin; then | ||
| 71 | + PATH=\$PATH:/usr/sfw/bin | ||
| 72 | + export PATH | ||
| 73 | +fi | ||
| 74 | + | ||
| 75 | +unset CDPATH | ||
| 76 | + | ||
| 77 | +function rotate_log() { | ||
| 78 | + check_path "\$log_file" | ||
| 79 | + mv -f "\$log_file" "\$info_record_path/\$info_record_file_bak" | ||
| 80 | + touch "\$log_file" 2>/dev/null | ||
| 81 | + check_path "\$info_record_path/\$info_record_file_bak" | ||
| 82 | + chmod 440 "\$info_record_path/\$info_record_file_bak" | ||
| 83 | + check_path "\$log_file" | ||
| 84 | + chmod 640 "\$log_file" | ||
| 85 | +} | ||
| 86 | +function check_path() { | ||
| 87 | + if [ "\$1" != \$(readlink -f "\$1") ]; then | ||
| 88 | + echo >&2 | ||
| 89 | + echo "Log file is not support symlink, exiting!" >&2 | ||
| 90 | + exit 1 | ||
| 91 | + fi | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +function log_check() { | ||
| 95 | + local log_size | ||
| 96 | + log_size=\$(find \$log_file -exec ls -l {} \; | awk '{ print \$5 }') | ||
| 97 | + if [[ "\${log_size}" -ge "\${LOG_SIZE_THRESHOLD}" ]];then | ||
| 98 | + rotate_log | ||
| 99 | + fi | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +# usage log "INFO" "this is message" | ||
| 103 | +function log() { | ||
| 104 | + if [ ! -d "\$info_record_path" ];then | ||
| 105 | + mkdir -p "\$info_record_path" | ||
| 106 | + chmod 750 "\$info_record_path" | ||
| 107 | + fi | ||
| 108 | + if [[ ! -f "\$log_file" ]];then | ||
| 109 | + touch "\$log_file" | ||
| 110 | + chmod 640 "\$log_file" | ||
| 111 | + fi | ||
| 112 | + # print log to log file | ||
| 113 | + if [ -f "\$log_file" ]; then | ||
| 114 | + log_check "\$log_file" | ||
| 115 | + if ! echo -e "[\${PACKAGE_LOG_NAME}] [\${package_name}][\$(date +%Y%m%d-%H:%M:%S)] [\$user_n] [\$1] \$2" >>"\$log_file" | ||
| 116 | + then | ||
| 117 | + echo "can not write log, exiting!" >&2 | ||
| 118 | + exit 1 | ||
| 119 | + fi | ||
| 120 | + else | ||
| 121 | + echo "log file not exist, exiting!" >&2 | ||
| 122 | + exit 1 | ||
| 123 | + fi | ||
| 124 | +} | ||
| 125 | + | ||
| 126 | +MS_Printf() | ||
| 127 | +{ | ||
| 128 | + \$print_cmd \$print_cmd_arg "\$1" | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +MS_PrintLicense() | ||
| 132 | +{ | ||
| 133 | + PAGER=\${PAGER:=more} | ||
| 134 | + if test x"\$licensetxt" != x; then | ||
| 135 | + PAGER_PATH=\`exec <&- 2>&-; which \$PAGER || command -v \$PAGER || type \$PAGER\` | ||
| 136 | + if test -x "\$PAGER_PATH"; then | ||
| 137 | + echo "\$licensetxt" | \$PAGER | ||
| 138 | + else | ||
| 139 | + echo "\$licensetxt" | ||
| 140 | + fi | ||
| 141 | + if test x"\$accept" != xy; then | ||
| 142 | + while true | ||
| 143 | + do | ||
| 144 | + MS_Printf "Please type y to accept, n otherwise: " | ||
| 145 | + read yn | ||
| 146 | + if test x"\$yn" = xn; then | ||
| 147 | + keep=n | ||
| 148 | + eval \$finish; exit 1 | ||
| 149 | + break; | ||
| 150 | + elif test x"\$yn" = xy; then | ||
| 151 | + break; | ||
| 152 | + fi | ||
| 153 | + done | ||
| 154 | + fi | ||
| 155 | + fi | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +MS_diskspace() | ||
| 159 | +{ | ||
| 160 | + ( | ||
| 161 | + df -kP "\$1" | tail -1 | awk '{ if (\$4 ~ /%/) {print \$3} else {print \$4} }' | ||
| 162 | + ) | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +MS_dd() | ||
| 166 | +{ | ||
| 167 | + blocks=\`expr \$3 / 1024\` | ||
| 168 | + bytes=\`expr \$3 % 1024\` | ||
| 169 | + # Test for ibs, obs and conv feature | ||
| 170 | + if dd if=/dev/zero of=/dev/null count=1 ibs=512 obs=512 conv=sync 2> /dev/null; then | ||
| 171 | + dd if="\$1" ibs=\$2 skip=1 obs=1024 conv=sync 2> /dev/null | \\ | ||
| 172 | + { test \$blocks -gt 0 && dd ibs=1024 obs=1024 count=\$blocks ; \\ | ||
| 173 | + test \$bytes -gt 0 && dd ibs=1 obs=1024 count=\$bytes ; } 2> /dev/null | ||
| 174 | + else | ||
| 175 | + dd if="\$1" bs=\$2 skip=1 2> /dev/null | ||
| 176 | + fi | ||
| 177 | +} | ||
| 178 | + | ||
| 179 | +MS_dd_Progress() | ||
| 180 | +{ | ||
| 181 | + if test x"\$noprogress" = xy; then | ||
| 182 | + MS_dd "\$@" | ||
| 183 | + return \$? | ||
| 184 | + fi | ||
| 185 | + file="\$1" | ||
| 186 | + offset=\$2 | ||
| 187 | + length=\$3 | ||
| 188 | + pos=0 | ||
| 189 | + bsize=4194304 | ||
| 190 | + while test \$bsize -gt \$length; do | ||
| 191 | + bsize=\`expr \$bsize / 4\` | ||
| 192 | + done | ||
| 193 | + blocks=\`expr \$length / \$bsize\` | ||
| 194 | + bytes=\`expr \$length % \$bsize\` | ||
| 195 | + ( | ||
| 196 | + dd ibs=\$offset skip=1 2>/dev/null | ||
| 197 | + pos=\`expr \$pos \+ \$bsize\` | ||
| 198 | + MS_Printf " 0%% " 1>&2 | ||
| 199 | + if test \$blocks -gt 0; then | ||
| 200 | + while test \$pos -le \$length; do | ||
| 201 | + dd bs=\$bsize count=1 2>/dev/null | ||
| 202 | + pcent=\`expr \$length / 100\` | ||
| 203 | + pcent=\`expr \$pos / \$pcent\` | ||
| 204 | + if test \$pcent -lt 100; then | ||
| 205 | + MS_Printf "\b\b\b\b\b\b\b" 1>&2 | ||
| 206 | + if test \$pcent -lt 10; then | ||
| 207 | + MS_Printf " \$pcent%% " 1>&2 | ||
| 208 | + else | ||
| 209 | + MS_Printf " \$pcent%% " 1>&2 | ||
| 210 | + fi | ||
| 211 | + fi | ||
| 212 | + pos=\`expr \$pos \+ \$bsize\` | ||
| 213 | + done | ||
| 214 | + fi | ||
| 215 | + if test \$bytes -gt 0; then | ||
| 216 | + dd bs=\$bytes count=1 2>/dev/null | ||
| 217 | + fi | ||
| 218 | + MS_Printf "\b\b\b\b\b\b\b" 1>&2 | ||
| 219 | + MS_Printf " 100%% " 1>&2 | ||
| 220 | + ) < "\$file" | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +MS_Help() | ||
| 224 | +{ | ||
| 225 | + cat << EOH >&2 | ||
| 226 | +Usage: \$0 [options] | ||
| 227 | +Options: | ||
| 228 | + --help | -h Print this message | ||
| 229 | + --info Print embedded info : title, default target directory, embedded script ... | ||
| 230 | + --list Print the list of files in the archive | ||
| 231 | + --check Checks integrity and version dependency of the archive | ||
| 232 | + --quiet | -q Quiet install mode, skip human-computer interactions | ||
| 233 | + If the package requires an EULA, quiet installations are useful for scripting the installation | ||
| 234 | + Using this option means accepting the EULA | ||
| 235 | + --nox11 Do not spawn an xterm | ||
| 236 | + --noexec Do not run embedded script | ||
| 237 | + --extract=<path> Extract directly to a target directory (absolute or relative) | ||
| 238 | + Usually used with --noexec to just extract files without running | ||
| 239 | + --tar arg1 [arg2 ...] Access the contents of the archive through the tar command | ||
| 240 | +\${helpheader} | ||
| 241 | +EOH | ||
| 242 | +} | ||
| 243 | + | ||
| 244 | +MS_Verify_Sig() | ||
| 245 | +{ | ||
| 246 | + GPG_PATH=\`exec <&- 2>&-; which gpg || command -v gpg || type gpg\` | ||
| 247 | + MKTEMP_PATH=\`exec <&- 2>&-; which mktemp || command -v mktemp || type mktemp\` | ||
| 248 | + test -x "\$GPG_PATH" || GPG_PATH=\`exec <&- 2>&-; which gpg || command -v gpg || type gpg\` | ||
| 249 | + test -x "\$MKTEMP_PATH" || MKTEMP_PATH=\`exec <&- 2>&-; which mktemp || command -v mktemp || type mktemp\` | ||
| 250 | + offset=\`head -n "\$skip" "\$1" | wc -c | tr -d " "\` | ||
| 251 | + temp_sig=\`mktemp -t XXXXX\` | ||
| 252 | + echo \$SIGNATURE | base64 --decode > "\$temp_sig" | ||
| 253 | + gpg_output=\`MS_dd "\$1" \$offset \$totalsize | LC_ALL=C "\$GPG_PATH" --verify "\$temp_sig" - 2>&1\` | ||
| 254 | + gpg_res=\$? | ||
| 255 | + rm -f "\$temp_sig" | ||
| 256 | + if test \$gpg_res -eq 0 && test \`echo \$gpg_output | grep -c Good\` -eq 1; then | ||
| 257 | + if test \`echo \$gpg_output | grep -c \$sig_key\` -eq 1; then | ||
| 258 | + test x"\$quiet" = xn && echo "GPG signature is good" >&2 | ||
| 259 | + else | ||
| 260 | + echo "GPG Signature key does not match" >&2 | ||
| 261 | + exit 2 | ||
| 262 | + fi | ||
| 263 | + else | ||
| 264 | + test x"\$quiet" = xn && echo "GPG signature failed to verify" >&2 | ||
| 265 | + exit 2 | ||
| 266 | + fi | ||
| 267 | +} | ||
| 268 | + | ||
| 269 | +MS_Check() | ||
| 270 | +{ | ||
| 271 | + SHA_PATH=\`exec <&- 2>&-; which shasum || command -v shasum || type shasum\` | ||
| 272 | + test -x "\$SHA_PATH" || SHA_PATH=\`exec <&- 2>&-; which sha256sum || command -v sha256sum || type sha256sum\` | ||
| 273 | + | ||
| 274 | + if ! test -x "\$SHA_PATH"; then | ||
| 275 | + echo "Command sha256sum not found, please install it first." | ||
| 276 | + log "ERROR" "Command sha256sum not found, please install it first." | ||
| 277 | + exit 2 | ||
| 278 | + fi | ||
| 279 | + | ||
| 280 | + if test x"\$quiet" = xn; then | ||
| 281 | + MS_Printf "Verifying archive integrity..." | ||
| 282 | + fi | ||
| 283 | + offset=\`head -n "\$skip" "\$1" | wc -c | tr -d " "\` | ||
| 284 | + fsize=\`cat "\$1" | wc -c | tr -d " "\` | ||
| 285 | + if test \$totalsize -ne \`expr \$fsize - \$offset\`; then | ||
| 286 | + echo " Unexpected archive size." >&2 | ||
| 287 | + exit 2 | ||
| 288 | + fi | ||
| 289 | + verb=\$2 | ||
| 290 | + i=1 | ||
| 291 | + for s in \$filesizes | ||
| 292 | + do | ||
| 293 | + if test -x "\$SHA_PATH"; then | ||
| 294 | + if test x"\`basename \$SHA_PATH\`" = xshasum; then | ||
| 295 | + SHA_ARG="-a 256" | ||
| 296 | + fi | ||
| 297 | + sha=\`echo \$SHA | cut -d" " -f\$i\` | ||
| 298 | + if test x"\$sha" = x0000000000000000000000000000000000000000000000000000000000000000; then | ||
| 299 | + test x"\$verb" = xy && echo " \$1 does not contain an embedded SHA256 checksum." >&2 | ||
| 300 | + else | ||
| 301 | + shasum=\`MS_dd_Progress "\$1" \$offset \$s | eval "\$SHA_PATH \$SHA_ARG" | cut -b-64\`; | ||
| 302 | + if test x"\$shasum" != x"\$sha"; then | ||
| 303 | + echo "Error in SHA256 checksums: \$shasum is different from \$sha" >&2 | ||
| 304 | + log "ERROR" "Error in SHA256 checksums: \$shasum is different from \$sha" | ||
| 305 | + exit 2 | ||
| 306 | + elif test x"\$quiet" = xn; then | ||
| 307 | + MS_Printf " SHA256 checksums are OK." >&2 | ||
| 308 | + log "INFO" "SHA256 checksums are OK." | ||
| 309 | + fi | ||
| 310 | + fi | ||
| 311 | + fi | ||
| 312 | + i=\`expr \$i + 1\` | ||
| 313 | + offset=\`expr \$offset + \$s\` | ||
| 314 | + done | ||
| 315 | + if test x"\$quiet" = xn; then | ||
| 316 | + echo " All good." | ||
| 317 | + fi | ||
| 318 | +} | ||
| 319 | + | ||
| 320 | +MS_Decompress() | ||
| 321 | +{ | ||
| 322 | + if test x"\$decrypt_cmd" != x""; then | ||
| 323 | + { eval "\$decrypt_cmd" || echo " ... Decryption failed." >&2; } | eval "$GUNZIP_CMD" | ||
| 324 | + else | ||
| 325 | + eval "$GUNZIP_CMD" | ||
| 326 | + fi | ||
| 327 | + | ||
| 328 | + if test \$? -ne 0; then | ||
| 329 | + echo " ... Decompression failed." >&2 | ||
| 330 | + log "ERROR" "Decompression failed." | ||
| 331 | + fi | ||
| 332 | +} | ||
| 333 | + | ||
| 334 | +UnTAR() | ||
| 335 | +{ | ||
| 336 | + if test x"\$quiet" = xn; then | ||
| 337 | + tar \$1vf - $UNTAR_EXTRA 2>&1 || { echo " ... Extraction failed." >&2; kill -15 \$$; } | ||
| 338 | + else | ||
| 339 | + tar \$1f - $UNTAR_EXTRA 2>&1 || { echo Extraction failed. >&2; kill -15 \$$; } | ||
| 340 | + fi | ||
| 341 | +} | ||
| 342 | + | ||
| 343 | +MS_exec_cleanup() { | ||
| 344 | + if test x"\$cleanup" = xy && test x"\$cleanup_script" != x""; then | ||
| 345 | + cleanup=n | ||
| 346 | + cd "\$tmpdir" | ||
| 347 | + eval "\"\$cleanup_script\" \$scriptargs \$cleanupargs" | ||
| 348 | + fi | ||
| 349 | +} | ||
| 350 | + | ||
| 351 | +MS_cleanup() | ||
| 352 | +{ | ||
| 353 | + echo 'Signal caught, cleaning up' >&2 | ||
| 354 | + MS_exec_cleanup | ||
| 355 | + cd "\$TMPROOT" | ||
| 356 | + rm -rf "\$tmpdir" | ||
| 357 | + eval \$finish; exit 15 | ||
| 358 | +} | ||
| 359 | + | ||
| 360 | +MS_check_user() | ||
| 361 | +{ | ||
| 362 | + userid=\`id -u\` | ||
| 363 | + tmpdir_uid=\`stat -c %u \$tmpdir\` | ||
| 364 | + user_name=\`stat -c %U \$tmpdir\` | ||
| 365 | + if test x"\$userid" != x"\$tmpdir_uid"; then | ||
| 366 | + echo "Run package was modified by user \$user_name, please check security." | ||
| 367 | + exit 1 | ||
| 368 | + fi | ||
| 369 | +} | ||
| 370 | + | ||
| 371 | +Script_Args_Check() | ||
| 372 | +{ | ||
| 373 | + script_supported_args=\$(echo \${helpheader} | grep -o -E "[-][-][^ ]+" | awk -F"=" {'print \$1'}) | ||
| 374 | + arg_to_test=\$(echo \$1|awk -F"=" {'print \$1'}) | ||
| 375 | + | ||
| 376 | + for arg in \${script_supported_args}; | ||
| 377 | + do | ||
| 378 | + if test x"\$arg_to_test" = x"\$arg" ;then | ||
| 379 | + return | ||
| 380 | + fi | ||
| 381 | + done | ||
| 382 | + | ||
| 383 | + MS_Help | ||
| 384 | + exit 1 | ||
| 385 | +} | ||
| 386 | + | ||
| 387 | +finish=true | ||
| 388 | +xterm_loop= | ||
| 389 | +noprogress=$NOPROGRESS | ||
| 390 | +nox11=$NOX11 | ||
| 391 | +copy=$COPY | ||
| 392 | +ownership=$OWNERSHIP | ||
| 393 | +verbose=n | ||
| 394 | +cleanup=y | ||
| 395 | +cleanupargs= | ||
| 396 | +sig_key= | ||
| 397 | + | ||
| 398 | +initargs="\$@" | ||
| 399 | + | ||
| 400 | +while [ -n "\$*" ] | ||
| 401 | +do | ||
| 402 | + case "\$1" in | ||
| 403 | + -h | --help) | ||
| 404 | + MS_Help | ||
| 405 | + exit 0 | ||
| 406 | + ;; | ||
| 407 | + -q | --quiet) | ||
| 408 | + quiet=y | ||
| 409 | + noprogress=y | ||
| 410 | + shift | ||
| 411 | + ;; | ||
| 412 | + --info) | ||
| 413 | + echo Identification: "\$label" | ||
| 414 | + echo Target directory: "\$targetdir" | ||
| 415 | + echo Uncompressed size: $USIZE KB | ||
| 416 | + echo Compression: $COMPRESS | ||
| 417 | + if test x"$ENCRYPT" != x""; then | ||
| 418 | + echo Encryption: $ENCRYPT | ||
| 419 | + fi | ||
| 420 | + echo Date of packaging: $DATE | ||
| 421 | + echo Built with Makeself version $MS_VERSION | ||
| 422 | + echo Build command was: "$MS_COMMAND" | ||
| 423 | + if test x"\$script" != x; then | ||
| 424 | + echo Script run after extraction: | ||
| 425 | + echo " " \$script \$scriptargs | ||
| 426 | + fi | ||
| 427 | + if test x"$copy" = xcopy; then | ||
| 428 | + echo "Archive will copy itself to a temporary location" | ||
| 429 | + fi | ||
| 430 | + if test x"$NEED_ROOT" = xy; then | ||
| 431 | + echo "Root permissions required for extraction" | ||
| 432 | + fi | ||
| 433 | + if test x"$KEEP" = xy; then | ||
| 434 | + echo "directory \$targetdir is permanent" | ||
| 435 | + else | ||
| 436 | + echo "\$targetdir will be removed after extraction" | ||
| 437 | + fi | ||
| 438 | + exit 0 | ||
| 439 | + ;; | ||
| 440 | + --list) | ||
| 441 | + echo Target directory: \$targetdir | ||
| 442 | + offset=\`head -n "\$skip" "\$0" | wc -c | tr -d " "\` | ||
| 443 | + for s in \$filesizes | ||
| 444 | + do | ||
| 445 | + MS_dd "\$0" \$offset \$s | MS_Decompress | UnTAR t | ||
| 446 | + offset=\`expr \$offset + \$s\` | ||
| 447 | + done | ||
| 448 | + exit 0 | ||
| 449 | + ;; | ||
| 450 | + --tar) | ||
| 451 | + offset=\`head -n "\$skip" "\$0" | wc -c | tr -d " "\` | ||
| 452 | + arg1="\$2" | ||
| 453 | + shift 2 || { MS_Help; exit 1; } | ||
| 454 | + log "INFO" "Start --tar process." | ||
| 455 | + echo "Makeself logfile: \$log_file" | ||
| 456 | + for s in \$filesizes | ||
| 457 | + do | ||
| 458 | + MS_dd "\$0" \$offset \$s | MS_Decompress | tar "\$arg1" - "\$@" | ||
| 459 | + offset=\`expr \$offset + \$s\` | ||
| 460 | + done | ||
| 461 | + exit 0 | ||
| 462 | + ;; | ||
| 463 | + --check) | ||
| 464 | + echo "Makeself logfile: \$log_file" | ||
| 465 | + MS_Check "\$0" y | ||
| 466 | + scriptargs="\$scriptargs \$1" | ||
| 467 | + shift | ||
| 468 | + ;; | ||
| 469 | + --noexec) | ||
| 470 | + script="" | ||
| 471 | + cleanup_script="" | ||
| 472 | + shift | ||
| 473 | + ;; | ||
| 474 | + --extract=*) | ||
| 475 | + keep=y | ||
| 476 | + targetdir=\`echo \$1 | cut -d"=" -f2 \` | ||
| 477 | + if ! shift; then MS_Help; exit 1; fi | ||
| 478 | + log "INFO" "Extract files to targetdir." | ||
| 479 | + echo "Makeself logfile: \$log_file" | ||
| 480 | + ;; | ||
| 481 | + --nox11) | ||
| 482 | + nox11=y | ||
| 483 | + shift | ||
| 484 | + ;; | ||
| 485 | + --xwin) | ||
| 486 | + if test "$NOWAIT" = n; then | ||
| 487 | + finish="echo Press Return to close this window...; read junk" | ||
| 488 | + fi | ||
| 489 | + xterm_loop=1 | ||
| 490 | + shift | ||
| 491 | + ;; | ||
| 492 | + --phase2) | ||
| 493 | + copy=phase2 | ||
| 494 | + shift | ||
| 495 | + ;; | ||
| 496 | + *) | ||
| 497 | + Script_Args_Check \$1 | ||
| 498 | + scriptargs="\$scriptargs '\$1'" | ||
| 499 | + shift | ||
| 500 | + ;; | ||
| 501 | + esac | ||
| 502 | +done | ||
| 503 | + | ||
| 504 | +quiet_para="" | ||
| 505 | +if test x"\$quiet" = xy; then | ||
| 506 | + quiet_para="--quiet " | ||
| 507 | +fi | ||
| 508 | +scriptargs="--\$name_of_file""--\"\$pwd_of_file\""" \$quiet_para""\$scriptargs" | ||
| 509 | + | ||
| 510 | +if test x"\$quiet" = xy -a x"\$verbose" = xy; then | ||
| 511 | + echo Cannot be verbose and quiet at the same time. >&2 | ||
| 512 | + exit 1 | ||
| 513 | +fi | ||
| 514 | + | ||
| 515 | +if test x"$NEED_ROOT" = xy -a \`id -u\` -ne 0; then | ||
| 516 | + echo "Administrative privileges required for this archive (use su or sudo)" >&2 | ||
| 517 | + exit 1 | ||
| 518 | +fi | ||
| 519 | + | ||
| 520 | +if test x"\$copy" \!= xphase2; then | ||
| 521 | + MS_PrintLicense | ||
| 522 | +fi | ||
| 523 | + | ||
| 524 | +case "\$copy" in | ||
| 525 | +copy) | ||
| 526 | + tmpdir="\$TMPROOT"/makeself.\$RANDOM.\`date +"%y%m%d%H%M%S"\`.\$\$ | ||
| 527 | + mkdir "\$tmpdir" || { | ||
| 528 | + echo "Could not create temporary directory \$tmpdir" >&2 | ||
| 529 | + exit 1 | ||
| 530 | + } | ||
| 531 | + SCRIPT_COPY="\$tmpdir/makeself" | ||
| 532 | + echo "Copying to a temporary location..." >&2 | ||
| 533 | + cp "\$0" "\$SCRIPT_COPY" | ||
| 534 | + chmod +x "\$SCRIPT_COPY" | ||
| 535 | + cd "\$TMPROOT" | ||
| 536 | + exec "\$SCRIPT_COPY" --phase2 -- \$initargs | ||
| 537 | + ;; | ||
| 538 | +phase2) | ||
| 539 | + finish="\$finish ; rm -rf \`dirname \$0\`" | ||
| 540 | + ;; | ||
| 541 | +esac | ||
| 542 | + | ||
| 543 | +if test x"\$targetdir" = x.; then | ||
| 544 | + tmpdir="." | ||
| 545 | +else | ||
| 546 | + if test x"\$keep" = xy; then | ||
| 547 | + if test x"\$nooverwrite" = xy && test -d "\$targetdir"; then | ||
| 548 | + echo "Target directory \$targetdir already exists, aborting." >&2 | ||
| 549 | + exit 1 | ||
| 550 | + fi | ||
| 551 | + if test x"\$quiet" = xn; then | ||
| 552 | + echo "Creating directory \$targetdir" >&2 | ||
| 553 | + fi | ||
| 554 | + tmpdir="\$targetdir" | ||
| 555 | + dashp="-p" | ||
| 556 | + else | ||
| 557 | + tmpdir="\$TMPROOT/selfgz\$\$\$RANDOM" | ||
| 558 | + dashp="" | ||
| 559 | + fi | ||
| 560 | + if [ -L "\$tmpdir" ]; then | ||
| 561 | + tmpdir=\`readlink -f \$tmpdir\` | ||
| 562 | + fi | ||
| 563 | + if [ ! -d "\$tmpdir" ]; then | ||
| 564 | + mkdir \$dashp "\$tmpdir" || { | ||
| 565 | + echo 'Cannot create target directory' \$tmpdir >&2 | ||
| 566 | + echo 'You should try option --extract=<path>' >&2 | ||
| 567 | + eval \$finish | ||
| 568 | + exit 1 | ||
| 569 | + } | ||
| 570 | + fi | ||
| 571 | +fi | ||
| 572 | +tmpdir=\`readlink -f \$tmpdir\` | ||
| 573 | + | ||
| 574 | +location="\`pwd\`" | ||
| 575 | +if test x"\$SETUP_NOCHECK" != x1; then | ||
| 576 | + MS_Check "\$0" | ||
| 577 | +fi | ||
| 578 | +offset=\`head -n "\$skip" "\$0" | wc -c | tr -d " "\` | ||
| 579 | + | ||
| 580 | +if test x"\$verbose" = xy; then | ||
| 581 | + MS_Printf "About to extract $USIZE KB in \$tmpdir ... Proceed ? [Y/n] " | ||
| 582 | + read yn | ||
| 583 | + if test x"\$yn" = xn; then | ||
| 584 | + eval \$finish; exit 1 | ||
| 585 | + fi | ||
| 586 | +fi | ||
| 587 | + | ||
| 588 | +if test x"\$quiet" = xn; then | ||
| 589 | + # Decrypting with openssl will ask for password, | ||
| 590 | + # the prompt needs to start on new line | ||
| 591 | + if test x"$ENCRYPT" = x"openssl"; then | ||
| 592 | + echo "Decrypting and uncompressing \$label..." | ||
| 593 | + else | ||
| 594 | + MS_Printf "Uncompressing \$label" | ||
| 595 | + fi | ||
| 596 | +fi | ||
| 597 | +res=3 | ||
| 598 | +if test x"\$keep" = xn; then | ||
| 599 | + trap MS_cleanup 1 2 3 15 | ||
| 600 | +fi | ||
| 601 | + | ||
| 602 | +if test x"\$nodiskspace" = xn; then | ||
| 603 | + leftspace=\`MS_diskspace "\$tmpdir"\` | ||
| 604 | + if test -n "\$leftspace"; then | ||
| 605 | + if test "\$leftspace" -lt $USIZE; then | ||
| 606 | + echo | ||
| 607 | + echo "Not enough space left in "\`dirname \$tmpdir\`" (\$leftspace KB) to decompress \$0 ($USIZE KB)" >&2 | ||
| 608 | + if test x"\$keep" = xn; then | ||
| 609 | + echo "Use the (export TMPDIR=<path>) command to set a decompressed directory with more free space." | ||
| 610 | + fi | ||
| 611 | + eval \$finish; exit 1 | ||
| 612 | + fi | ||
| 613 | + fi | ||
| 614 | +fi | ||
| 615 | + | ||
| 616 | +for s in \$filesizes | ||
| 617 | +do | ||
| 618 | + if MS_dd_Progress "\$0" \$offset \$s | MS_Decompress | ( cd "\$tmpdir"; umask \$ORIG_UMASK ; UnTAR xp ) 1>/dev/null; then | ||
| 619 | + if test x"\$ownership" = xy; then | ||
| 620 | + (cd "\$tmpdir"; chown -R \`id -u\` .; chgrp -R \`id -g\` .) | ||
| 621 | + fi | ||
| 622 | + else | ||
| 623 | + echo >&2 | ||
| 624 | + echo "Unable to decompress \$0" >&2 | ||
| 625 | + eval \$finish; exit 1 | ||
| 626 | + fi | ||
| 627 | + offset=\`expr \$offset + \$s\` | ||
| 628 | +done | ||
| 629 | +if test x"\$quiet" = xn; then | ||
| 630 | + echo | ||
| 631 | +fi | ||
| 632 | + | ||
| 633 | +cd "\$tmpdir" | ||
| 634 | +res=0 | ||
| 635 | +if test x"\$script" != x; then | ||
| 636 | + if test x"\$export_conf" = x"y"; then | ||
| 637 | + MS_BUNDLE="\$0" | ||
| 638 | + MS_LABEL="\$label" | ||
| 639 | + MS_SCRIPT="\$script" | ||
| 640 | + MS_SCRIPTARGS="\$scriptargs" | ||
| 641 | + MS_ARCHDIRNAME="\$archdirname" | ||
| 642 | + MS_KEEP="\$KEEP" | ||
| 643 | + MS_NOOVERWRITE="\$NOOVERWRITE" | ||
| 644 | + MS_COMPRESS="\$COMPRESS" | ||
| 645 | + MS_CLEANUP="\$cleanup" | ||
| 646 | + export MS_BUNDLE MS_LABEL MS_SCRIPT MS_SCRIPTARGS | ||
| 647 | + export MS_ARCHDIRNAME MS_KEEP MS_NOOVERWRITE MS_COMPRESS | ||
| 648 | + fi | ||
| 649 | + | ||
| 650 | + if test x"\$verbose" = x"y"; then | ||
| 651 | + yn="x" | ||
| 652 | + while test x"\$yn" != x -a x"\$yn" != xy -a x"\$yn" != xY -a x"\$yn" != xn -a x"\$yn" != xN | ||
| 653 | + do | ||
| 654 | + MS_Printf "OK to execute: \$script \$scriptargs \$* ? [Y/n] " | ||
| 655 | + read yn | ||
| 656 | + if test x"\$yn" = x -o x"\$yn" = xy -o x"\$yn" = xY; then | ||
| 657 | + MS_check_user | ||
| 658 | + eval "\"\$script\" \$scriptargs \"\\\$@\""; res=\$?; | ||
| 659 | + elif test x"\$yn" = xn -o x"\$yn" = xN; then | ||
| 660 | + echo "Unable to decompress \$script ,because of aborting! ";res=\$? | ||
| 661 | + else | ||
| 662 | + echo "Input value is unacceptable,please try again." | ||
| 663 | + fi | ||
| 664 | + done | ||
| 665 | + else | ||
| 666 | + MS_check_user | ||
| 667 | + eval "\"\$script\" \$scriptargs \"\\\$@\""; res=\$? | ||
| 668 | + fi | ||
| 669 | + if test "\$res" -ne 0; then | ||
| 670 | + test x"\$verbose" = xy && echo "The program '\$script' returned an error code (\$res)" >&2 | ||
| 671 | + fi | ||
| 672 | +fi | ||
| 673 | + | ||
| 674 | +MS_exec_cleanup | ||
| 675 | + | ||
| 676 | +if test x"\$keep" = xn; then | ||
| 677 | + cd "\$TMPROOT" | ||
| 678 | + rm -rf "\$tmpdir" | ||
| 679 | +fi | ||
| 680 | +eval \$finish; exit \$res | ||
| 681 | +EOF | ||
| @@ -0,0 +1,158 @@ | |||
| 1 | +.TH "MAKESELF" "1" "2.5.0" | ||
| 2 | +.SH "NAME" | ||
| 3 | +makeself \- An utility to generate self-extractable archives. | ||
| 4 | +.SH "SYNTAX" | ||
| 5 | +.B makeself [\fIoptions\fP] archive_dir file_name label | ||
| 6 | +.B [\fIstartup_script\fP] [\fIargs\fP] | ||
| 7 | +.SH "DESCRIPTION" | ||
| 8 | +This program is a free (GPL) shell utility designed to create self-extractable | ||
| 9 | +compressed archives from a directory. The resulting file appears as a shell script, and can be launched as is. The archive | ||
| 10 | +will then uncompress itself to a temporary directory and an optional arbitrary | ||
| 11 | +command will be executed (for example an installation script). | ||
| 12 | +.TP | ||
| 13 | +Makeself archives also include checksums for integrity self-validation (CRC and/or MD5/SHA256 checksums). | ||
| 14 | +.SH "OPTIONS" | ||
| 15 | +The following options are supported: | ||
| 16 | +.TP 15 | ||
| 17 | +.B -v, --version | ||
| 18 | +Prints out the makeself version number and exits. | ||
| 19 | +.TP | ||
| 20 | +.B -h, --help | ||
| 21 | +Print out help information. | ||
| 22 | +.TP | ||
| 23 | +.B --tar-quietly | ||
| 24 | +Suppress verbose output from the tar command | ||
| 25 | +.TP | ||
| 26 | +.B --quiet | ||
| 27 | +Do not print any messages other than errors | ||
| 28 | +.TP | ||
| 29 | +.B --gzip | ||
| 30 | +Compress using gzip (default if detected). | ||
| 31 | +.TP | ||
| 32 | +.B --bzip2 | ||
| 33 | +Compress using bzip2. | ||
| 34 | +.TP | ||
| 35 | +.B --bzip3 | ||
| 36 | +Compress using bzip3. | ||
| 37 | +.TP | ||
| 38 | +.B --pbzip2 | ||
| 39 | +Compress using pbzip2. | ||
| 40 | +.TP | ||
| 41 | +.B --xz | ||
| 42 | +Compress using xz. | ||
| 43 | +.TP | ||
| 44 | +.B --lzo | ||
| 45 | +Compress using lzop. | ||
| 46 | +.TP | ||
| 47 | +.B --lz4 | ||
| 48 | +Compress using lz4. | ||
| 49 | +.TP | ||
| 50 | +.B --pigz | ||
| 51 | +Compress using pigz. | ||
| 52 | +.TP | ||
| 53 | +.B --zstd | ||
| 54 | +Compress using zstd. | ||
| 55 | +.TP | ||
| 56 | +.B --base64 | ||
| 57 | +Encode the archive to ASCII in Base64 format instead of compressing (base64 command required). | ||
| 58 | +.TP | ||
| 59 | +.B --gpg-encrypt | ||
| 60 | +Encrypt the archive using GPG. This will prompt for a password to encrypt with. | ||
| 61 | +.TP | ||
| 62 | +.B --ssl-encrypt | ||
| 63 | +Encrypt the archive using OpenSSL. This will prompt for a password to encrypt with. | ||
| 64 | +.TP | ||
| 65 | +.B --keep-umask | ||
| 66 | +Keep the umask set to shell default, rather than overriding when executing the self-extracting archive. | ||
| 67 | +.TP | ||
| 68 | +.B --compress | ||
| 69 | +Compress using the UNIX 'compress' command. | ||
| 70 | +.TP | ||
| 71 | +.B --nocomp | ||
| 72 | +Do not compress the data. | ||
| 73 | +.TP | ||
| 74 | +.B --complevel lvl | ||
| 75 | +Specify the compression level for gzip, bzip2, pbzip2, xz, zstd, lzo or lz4. Defaults to 9. | ||
| 76 | +.TP | ||
| 77 | +.B --threads num | ||
| 78 | +Specify the number of threads to be used by compressors that support parallelization. | ||
| 79 | +.TP | ||
| 80 | +.B --tar-format opt | ||
| 81 | + Specify the tar archive format (default is ustar); you may use any value accepted by your tar command (such as posix, v7, etc). | ||
| 82 | +.TP | ||
| 83 | +.B --tar-extra opt | ||
| 84 | +Append more options to the tar command line. | ||
| 85 | +.TP | ||
| 86 | +.B --notemp | ||
| 87 | +The archive will create archive_dir in the current directory and | ||
| 88 | +uncompress in ./archive_dir. | ||
| 89 | +.TP | ||
| 90 | +.B --copy | ||
| 91 | +Upon extraction, the archive will first copy itself to a temporary directory. | ||
| 92 | +.TP | ||
| 93 | +.B --append | ||
| 94 | +Append more files to an existing makeself archive. The label and startup scripts will then be ignored. | ||
| 95 | +.TP | ||
| 96 | +.B --current | ||
| 97 | +Files will be extracted to the current directory. Both --current and --target dir imply --notemp. | ||
| 98 | +.TP | ||
| 99 | +.B --target dir | ||
| 100 | +Extract directly to a target directory. Directory path can be either absolute or relative. | ||
| 101 | +.TP | ||
| 102 | +.B --header file | ||
| 103 | +Specify location of the header script. | ||
| 104 | +.TP | ||
| 105 | +.B --help-header file | ||
| 106 | +Add a header to the archive's help output. | ||
| 107 | +.TP | ||
| 108 | +.B --cleanup file | ||
| 109 | +Specify a cleanup script that executes on interrupt and when finished successfully. | ||
| 110 | +.TP | ||
| 111 | +.B --follow | ||
| 112 | +Follow the symlinks in the archive. | ||
| 113 | +.TP | ||
| 114 | +.B --noprogress | ||
| 115 | +Do not show the progress during the decompression. | ||
| 116 | +.TP | ||
| 117 | +.B --nooverwrite | ||
| 118 | +Do not extract the archive if the target directory already exists. | ||
| 119 | +.TP | ||
| 120 | +.B --nox11 | ||
| 121 | +Disable automatic spawn of an xterm if running in X11. | ||
| 122 | +.TP | ||
| 123 | +.B --nowait | ||
| 124 | +Do not wait for user input after executing embedded program from an xterm. | ||
| 125 | +.TP | ||
| 126 | +.B --nomd5 | ||
| 127 | +Do not create a MD5 checksum for the archive. | ||
| 128 | +.TP | ||
| 129 | +.B --sha256 | ||
| 130 | +Adds a SHA256 checksum for the archive. | ||
| 131 | +.TP | ||
| 132 | +.B --nocrc | ||
| 133 | +Do not create a CRC32 checksum for the archive. | ||
| 134 | +.TP | ||
| 135 | +.B --lsm file | ||
| 136 | +LSM file describing the package. | ||
| 137 | +.TP | ||
| 138 | +.B --license file | ||
| 139 | +Append a license file. | ||
| 140 | +.TP | ||
| 141 | +.B --packaging-date date | ||
| 142 | +Use provided string as the packaging date instead of the current date. | ||
| 143 | +.TP | ||
| 144 | +.SH "EXAMPLES" | ||
| 145 | +Here is an example, assuming the user has a package image stored in a /home/joe/mysoft, | ||
| 146 | +and he wants to generate a self-extracting package named mysoft.sh, which will launch | ||
| 147 | +the "setup" script initially stored in /home/joe/mysoft: | ||
| 148 | +.TP | ||
| 149 | +makeself.sh /home/joe/mysoft mysoft.sh "Joe's Nice Software Package" ./setup | ||
| 150 | +.TP | ||
| 151 | +Here is also how I created the makeself.run archive which contains the Makeself distribution: | ||
| 152 | +.TP | ||
| 153 | +makeself.sh --notemp makeself makeself.run "Makeself by Stephane Peter" echo "Makeself has extracted itself" | ||
| 154 | +.SH "AUTHORS" | ||
| 155 | +Makeself has been written by Stephane Peter <megastep@megastep.org>. | ||
| 156 | +.BR | ||
| 157 | +This man page was originally written by Bartosz Fenski <fenio@o2.pl> for the | ||
| 158 | +Debian GNU/Linux distribution (but it may be used by others). | ||
| @@ -0,0 +1,776 @@ | |||
| 1 | +#!/bin/sh | ||
| 2 | +# | ||
| 3 | +# Makeself version 2.5.x | ||
| 4 | +# by Stephane Peter <megastep@megastep.org> | ||
| 5 | +# | ||
| 6 | +# Utility to create self-extracting tar.gz archives. | ||
| 7 | +# The resulting archive is a file holding the tar.gz archive with | ||
| 8 | +# a small Shell script stub that uncompresses the archive to a temporary | ||
| 9 | +# directory and then executes a given script from withing that directory. | ||
| 10 | +# | ||
| 11 | +# Makeself home page: https://makeself.io/ - Version history available on GitHub | ||
| 12 | +# | ||
| 13 | +# (C) 1998-2023 by Stephane Peter <megastep@megastep.org> | ||
| 14 | +# | ||
| 15 | +# This software is released under the terms of the GNU GPL version 2 and above | ||
| 16 | +# Please read the license at http://www.gnu.org/copyleft/gpl.html | ||
| 17 | +# Self-extracting archives created with this script are explictly NOT released under the term of the GPL | ||
| 18 | +# | ||
| 19 | + | ||
| 20 | +MS_VERSION=2.5.0 | ||
| 21 | +MS_COMMAND="$0" | ||
| 22 | +unset CDPATH | ||
| 23 | + | ||
| 24 | +for f in ${1+"$@"}; do | ||
| 25 | + MS_COMMAND="$MS_COMMAND \\\\ | ||
| 26 | + \\\"$f\\\"" | ||
| 27 | +done | ||
| 28 | + | ||
| 29 | +# For Solaris systems | ||
| 30 | +if test -d /usr/xpg4/bin; then | ||
| 31 | + PATH=/usr/xpg4/bin:$PATH | ||
| 32 | + export PATH | ||
| 33 | +fi | ||
| 34 | + | ||
| 35 | +# Procedures | ||
| 36 | + | ||
| 37 | +MS_Usage() | ||
| 38 | +{ | ||
| 39 | + echo "Usage: $0 [args] archive_dir file_name label startup_script [script_args]" | ||
| 40 | + echo "args can be one or more of the following :" | ||
| 41 | + echo " --version | -v : Print out Makeself version number and exit" | ||
| 42 | + echo " --help | -h : Print out this help message" | ||
| 43 | + echo " --tar-quietly : Suppress verbose output from the tar command" | ||
| 44 | + echo " --quiet | -q : Do not print any messages other than errors." | ||
| 45 | + echo " --gzip : Compress using gzip (default if detected)" | ||
| 46 | + echo " --pigz : Compress with pigz" | ||
| 47 | + echo " --zstd : Compress with zstd" | ||
| 48 | + echo " --bzip2 : Compress using bzip2 instead of gzip" | ||
| 49 | + echo " --pbzip2 : Compress using pbzip2 instead of gzip" | ||
| 50 | + echo " --bzip3 : Compress using bzip3 instead of gzip" | ||
| 51 | + echo " --xz : Compress using xz instead of gzip" | ||
| 52 | + echo " --lzo : Compress using lzop instead of gzip" | ||
| 53 | + echo " --lz4 : Compress using lz4 instead of gzip" | ||
| 54 | + echo " --compress : Compress using the UNIX 'compress' command" | ||
| 55 | + echo " --complevel lvl : Compression level for gzip pigz zstd xz lzo lz4 bzip2 pbzip2 and bzip3 (default 9)" | ||
| 56 | + echo " --threads thds : Number of threads to be used by compressors that support parallelization." | ||
| 57 | + echo " Omit to use compressor's default. Most useful (and required) for opting" | ||
| 58 | + echo " into xz's threading, usually with '--threads=0' for all available cores." | ||
| 59 | + echo " pbzip2 and pigz are parallel by default, and setting this value allows" | ||
| 60 | + echo " limiting the number of threads they use." | ||
| 61 | + echo " --base64 : Instead of compressing, encode the data using base64" | ||
| 62 | + echo " --gpg-encrypt : Instead of compressing, encrypt the data using GPG" | ||
| 63 | + echo " --gpg-asymmetric-encrypt-sign" | ||
| 64 | + echo " : Instead of compressing, asymmetrically encrypt and sign the data using GPG" | ||
| 65 | + echo " --gpg-extra opt : Append more options to the gpg command line" | ||
| 66 | + echo " --ssl-encrypt : Instead of compressing, encrypt the data using OpenSSL" | ||
| 67 | + echo " --ssl-passwd pass : Use the given password to encrypt the data using OpenSSL" | ||
| 68 | + echo " --ssl-pass-src src : Use the given src as the source of password to encrypt the data" | ||
| 69 | + echo " using OpenSSL. See \"PASS PHRASE ARGUMENTS\" in man openssl." | ||
| 70 | + echo " If this option is not supplied, the user will be asked to enter" | ||
| 71 | + echo " encryption password on the current terminal." | ||
| 72 | + echo " --ssl-no-md : Do not use \"-md\" option not supported by older OpenSSL." | ||
| 73 | + echo " --nochown : Do not give the target folder to the current user (default)" | ||
| 74 | + echo " --chown : Give the target folder to the current user recursively" | ||
| 75 | + echo " --nocomp : Do not compress the data" | ||
| 76 | + echo " --notemp : The archive will create archive_dir in the current directory" | ||
| 77 | + echo " and uncompress in ./archive_dir" | ||
| 78 | + echo " Note: persistent archives do not strictly require a startup_script" | ||
| 79 | + echo " --needroot : Check that the root user is extracting the archive before proceeding" | ||
| 80 | + echo " --copy : Upon extraction, the archive will first copy itself to" | ||
| 81 | + echo " a temporary directory" | ||
| 82 | + echo " --append : Append more files to an existing Makeself archive" | ||
| 83 | + echo " The label and startup scripts will then be ignored" | ||
| 84 | + echo " --target dir : Extract directly to a target directory" | ||
| 85 | + echo " directory path can be either absolute or relative" | ||
| 86 | + echo " --current : Files will be extracted to the current directory" | ||
| 87 | + echo " Both --current and --target imply --notemp, and do not require a startup_script" | ||
| 88 | + echo " --nooverwrite : Do not extract the archive if the specified target directory exists" | ||
| 89 | + echo " --tar-format opt : Specify a tar archive format (default is ustar)" | ||
| 90 | + echo " --tar-extra opt : Append more options to the tar command line" | ||
| 91 | + echo " --untar-extra opt : Append more options to the during the extraction of the tar archive" | ||
| 92 | + echo " --nomd5 : Don't calculate an MD5 for archive" | ||
| 93 | + echo " --nocrc : Don't calculate a CRC for archive" | ||
| 94 | + echo " --sha256 : Compute a SHA256 checksum for the archive" | ||
| 95 | + echo " --header file : Specify location of the header script" | ||
| 96 | + echo " --cleanup file : Specify a cleanup script that executes on interrupt and when finished successfully." | ||
| 97 | + echo " --follow : Follow the symlinks in the archive" | ||
| 98 | + echo " --noprogress : Do not show the progress during the decompression" | ||
| 99 | + echo " --nox11 : Disable automatic spawn of a xterm" | ||
| 100 | + echo " --nowait : Do not wait for user input after executing embedded" | ||
| 101 | + echo " program from an xterm" | ||
| 102 | + echo " --sign passphrase : Signature private key to sign the package with" | ||
| 103 | + echo " --lsm file : LSM file describing the package" | ||
| 104 | + echo " --license file : Append a license file" | ||
| 105 | + echo " --help-header file : Add a header to the archive's --help output" | ||
| 106 | + echo " --packaging-date date" | ||
| 107 | + echo " : Use provided string as the packaging date" | ||
| 108 | + echo " instead of the current date." | ||
| 109 | + echo | ||
| 110 | + echo " --keep-umask : Keep the umask set to shell default, rather than overriding when executing self-extracting archive." | ||
| 111 | + echo " --export-conf : Export configuration variables to startup_script" | ||
| 112 | + echo | ||
| 113 | + echo "Do not forget to give a fully qualified startup script name" | ||
| 114 | + echo "(i.e. with a ./ prefix if inside the archive)." | ||
| 115 | + exit 1 | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +# Default settings | ||
| 119 | +if type gzip >/dev/null 2>&1; then | ||
| 120 | + COMPRESS=gzip | ||
| 121 | +elif type compress >/dev/null 2>&1; then | ||
| 122 | + COMPRESS=compress | ||
| 123 | +else | ||
| 124 | + echo "ERROR: missing commands: gzip, compress" >&2 | ||
| 125 | + MS_Usage | ||
| 126 | +fi | ||
| 127 | +ENCRYPT=n | ||
| 128 | +PASSWD="" | ||
| 129 | +PASSWD_SRC="" | ||
| 130 | +OPENSSL_NO_MD=n | ||
| 131 | +COMPRESS_LEVEL=9 | ||
| 132 | +DEFAULT_THREADS=123456 # Sentinel value | ||
| 133 | +THREADS=$DEFAULT_THREADS | ||
| 134 | +KEEP=n | ||
| 135 | +CURRENT=n | ||
| 136 | +NOX11=n | ||
| 137 | +NOWAIT=n | ||
| 138 | +APPEND=n | ||
| 139 | +TAR_QUIETLY=n | ||
| 140 | +KEEP_UMASK=n | ||
| 141 | +QUIET=n | ||
| 142 | +NOPROGRESS=n | ||
| 143 | +COPY=none | ||
| 144 | +NEED_ROOT=n | ||
| 145 | +TAR_ARGS=rvf | ||
| 146 | +TAR_FORMAT=ustar | ||
| 147 | +TAR_EXTRA="" | ||
| 148 | +GPG_EXTRA="" | ||
| 149 | +DU_ARGS=-ks | ||
| 150 | +HEADER=`dirname "$0"`/makeself-header.sh | ||
| 151 | +SIGNATURE="" | ||
| 152 | +TARGETDIR="" | ||
| 153 | +NOOVERWRITE=n | ||
| 154 | +DATE=`LC_ALL=C date` | ||
| 155 | +EXPORT_CONF=n | ||
| 156 | +SHA256=n | ||
| 157 | +OWNERSHIP=n | ||
| 158 | +SIGN=n | ||
| 159 | +GPG_PASSPHRASE="" | ||
| 160 | + | ||
| 161 | +# LSM file stuff | ||
| 162 | +LSM_CMD="echo No LSM. >> \"\$archname\"" | ||
| 163 | + | ||
| 164 | +while true | ||
| 165 | +do | ||
| 166 | + case "$1" in | ||
| 167 | + --version | -v) | ||
| 168 | + echo Makeself version $MS_VERSION | ||
| 169 | + exit 0 | ||
| 170 | + ;; | ||
| 171 | + --pbzip2) | ||
| 172 | + COMPRESS=pbzip2 | ||
| 173 | + shift | ||
| 174 | + ;; | ||
| 175 | + --bzip3) | ||
| 176 | + COMPRESS=bzip3 | ||
| 177 | + shift | ||
| 178 | + ;; | ||
| 179 | + --bzip2) | ||
| 180 | + COMPRESS=bzip2 | ||
| 181 | + shift | ||
| 182 | + ;; | ||
| 183 | + --gzip) | ||
| 184 | + COMPRESS=gzip | ||
| 185 | + shift | ||
| 186 | + ;; | ||
| 187 | + --pigz) | ||
| 188 | + COMPRESS=pigz | ||
| 189 | + shift | ||
| 190 | + ;; | ||
| 191 | + --zstd) | ||
| 192 | + COMPRESS=zstd | ||
| 193 | + shift | ||
| 194 | + ;; | ||
| 195 | + --xz) | ||
| 196 | + COMPRESS=xz | ||
| 197 | + shift | ||
| 198 | + ;; | ||
| 199 | + --lzo) | ||
| 200 | + COMPRESS=lzo | ||
| 201 | + shift | ||
| 202 | + ;; | ||
| 203 | + --lz4) | ||
| 204 | + COMPRESS=lz4 | ||
| 205 | + shift | ||
| 206 | + ;; | ||
| 207 | + --compress) | ||
| 208 | + COMPRESS=compress | ||
| 209 | + shift | ||
| 210 | + ;; | ||
| 211 | + --base64) | ||
| 212 | + COMPRESS=base64 | ||
| 213 | + shift | ||
| 214 | + ;; | ||
| 215 | + --gpg-encrypt) | ||
| 216 | + COMPRESS=gpg | ||
| 217 | + shift | ||
| 218 | + ;; | ||
| 219 | + --gpg-asymmetric-encrypt-sign) | ||
| 220 | + COMPRESS=gpg-asymmetric | ||
| 221 | + shift | ||
| 222 | + ;; | ||
| 223 | + --gpg-extra) | ||
| 224 | + GPG_EXTRA="$2" | ||
| 225 | + shift 2 || { MS_Usage; exit 1; } | ||
| 226 | + ;; | ||
| 227 | + --ssl-encrypt) | ||
| 228 | + ENCRYPT=openssl | ||
| 229 | + shift | ||
| 230 | + ;; | ||
| 231 | + --ssl-passwd) | ||
| 232 | + PASSWD=$2 | ||
| 233 | + shift 2 || { MS_Usage; exit 1; } | ||
| 234 | + ;; | ||
| 235 | + --ssl-pass-src) | ||
| 236 | + PASSWD_SRC=$2 | ||
| 237 | + shift 2 || { MS_Usage; exit 1; } | ||
| 238 | + ;; | ||
| 239 | + --ssl-no-md) | ||
| 240 | + OPENSSL_NO_MD=y | ||
| 241 | + shift | ||
| 242 | + ;; | ||
| 243 | + --nocomp) | ||
| 244 | + COMPRESS=none | ||
| 245 | + shift | ||
| 246 | + ;; | ||
| 247 | + --complevel) | ||
| 248 | + COMPRESS_LEVEL="$2" | ||
| 249 | + shift 2 || { MS_Usage; exit 1; } | ||
| 250 | + ;; | ||
| 251 | + --threads) | ||
| 252 | + THREADS="$2" | ||
| 253 | + shift 2 || { MS_Usage; exit 1; } | ||
| 254 | + ;; | ||
| 255 | + --nochown) | ||
| 256 | + OWNERSHIP=n | ||
| 257 | + shift | ||
| 258 | + ;; | ||
| 259 | + --chown) | ||
| 260 | + OWNERSHIP=y | ||
| 261 | + shift | ||
| 262 | + ;; | ||
| 263 | + --notemp) | ||
| 264 | + KEEP=y | ||
| 265 | + shift | ||
| 266 | + ;; | ||
| 267 | + --copy) | ||
| 268 | + COPY=copy | ||
| 269 | + shift | ||
| 270 | + ;; | ||
| 271 | + --current) | ||
| 272 | + CURRENT=y | ||
| 273 | + KEEP=y | ||
| 274 | + shift | ||
| 275 | + ;; | ||
| 276 | + --tar-format) | ||
| 277 | + TAR_FORMAT="$2" | ||
| 278 | + shift 2 || { MS_Usage; exit 1; } | ||
| 279 | + ;; | ||
| 280 | + --tar-extra) | ||
| 281 | + TAR_EXTRA="$2" | ||
| 282 | + shift 2 || { MS_Usage; exit 1; } | ||
| 283 | + ;; | ||
| 284 | + --untar-extra) | ||
| 285 | + UNTAR_EXTRA="$2" | ||
| 286 | + shift 2 || { MS_Usage; exit 1; } | ||
| 287 | + ;; | ||
| 288 | + --target) | ||
| 289 | + TARGETDIR="$2" | ||
| 290 | + KEEP=y | ||
| 291 | + shift 2 || { MS_Usage; exit 1; } | ||
| 292 | + ;; | ||
| 293 | + --sign) | ||
| 294 | + SIGN=y | ||
| 295 | + GPG_PASSPHRASE="$2" | ||
| 296 | + shift 2 || { MS_Usage; exit 1; } | ||
| 297 | + ;; | ||
| 298 | + --nooverwrite) | ||
| 299 | + NOOVERWRITE=y | ||
| 300 | + shift | ||
| 301 | + ;; | ||
| 302 | + --needroot) | ||
| 303 | + NEED_ROOT=y | ||
| 304 | + shift | ||
| 305 | + ;; | ||
| 306 | + --header) | ||
| 307 | + HEADER="$2" | ||
| 308 | + shift 2 || { MS_Usage; exit 1; } | ||
| 309 | + ;; | ||
| 310 | + --cleanup) | ||
| 311 | + CLEANUP_SCRIPT="$2" | ||
| 312 | + shift 2 || { MS_Usage; exit 1; } | ||
| 313 | + ;; | ||
| 314 | + --license) | ||
| 315 | + # We need to escape all characters having a special meaning in double quotes | ||
| 316 | + LICENSE=$(sed 's/\\/\\\\/g; s/"/\\\"/g; s/`/\\\`/g; s/\$/\\\$/g' "$2") | ||
| 317 | + shift 2 || { MS_Usage; exit 1; } | ||
| 318 | + ;; | ||
| 319 | + --follow) | ||
| 320 | + TAR_ARGS=rvhf | ||
| 321 | + DU_ARGS=-ksL | ||
| 322 | + shift | ||
| 323 | + ;; | ||
| 324 | + --noprogress) | ||
| 325 | + NOPROGRESS=y | ||
| 326 | + shift | ||
| 327 | + ;; | ||
| 328 | + --nox11) | ||
| 329 | + NOX11=y | ||
| 330 | + shift | ||
| 331 | + ;; | ||
| 332 | + --nowait) | ||
| 333 | + NOWAIT=y | ||
| 334 | + shift | ||
| 335 | + ;; | ||
| 336 | + --nomd5) | ||
| 337 | + NOMD5=y | ||
| 338 | + shift | ||
| 339 | + ;; | ||
| 340 | + --sha256) | ||
| 341 | + SHA256=y | ||
| 342 | + shift | ||
| 343 | + ;; | ||
| 344 | + --nocrc) | ||
| 345 | + NOCRC=y | ||
| 346 | + shift | ||
| 347 | + ;; | ||
| 348 | + --append) | ||
| 349 | + APPEND=y | ||
| 350 | + shift | ||
| 351 | + ;; | ||
| 352 | + --lsm) | ||
| 353 | + LSM_CMD="awk 1 \"$2\" >> \"\$archname\"" | ||
| 354 | + shift 2 || { MS_Usage; exit 1; } | ||
| 355 | + ;; | ||
| 356 | + --packaging-date) | ||
| 357 | + DATE="$2" | ||
| 358 | + shift 2 || { MS_Usage; exit 1; } | ||
| 359 | + ;; | ||
| 360 | + --help-header) | ||
| 361 | + HELPHEADER=`sed -e "s/'/'\\\\\''/g" $2` | ||
| 362 | + shift 2 || { MS_Usage; exit 1; } | ||
| 363 | + [ -n "$HELPHEADER" ] && HELPHEADER="$HELPHEADER | ||
| 364 | +" | ||
| 365 | + ;; | ||
| 366 | + --tar-quietly) | ||
| 367 | + TAR_QUIETLY=y | ||
| 368 | + shift | ||
| 369 | + ;; | ||
| 370 | + --keep-umask) | ||
| 371 | + KEEP_UMASK=y | ||
| 372 | + shift | ||
| 373 | + ;; | ||
| 374 | + --export-conf) | ||
| 375 | + EXPORT_CONF=y | ||
| 376 | + shift | ||
| 377 | + ;; | ||
| 378 | + -q | --quiet) | ||
| 379 | + QUIET=y | ||
| 380 | + shift | ||
| 381 | + ;; | ||
| 382 | + -h | --help) | ||
| 383 | + MS_Usage | ||
| 384 | + ;; | ||
| 385 | + -*) | ||
| 386 | + echo Unrecognized flag : "$1" | ||
| 387 | + MS_Usage | ||
| 388 | + ;; | ||
| 389 | + *) | ||
| 390 | + break | ||
| 391 | + ;; | ||
| 392 | + esac | ||
| 393 | +done | ||
| 394 | + | ||
| 395 | +if test $# -lt 1; then | ||
| 396 | + MS_Usage | ||
| 397 | +else | ||
| 398 | + if test -d "$1"; then | ||
| 399 | + archdir="$1" | ||
| 400 | + else | ||
| 401 | + echo "Directory $1 does not exist." >&2 | ||
| 402 | + exit 1 | ||
| 403 | + fi | ||
| 404 | +fi | ||
| 405 | +archname="$2" | ||
| 406 | + | ||
| 407 | +if test "$QUIET" = "y" || test "$TAR_QUIETLY" = "y"; then | ||
| 408 | + if test "$TAR_ARGS" = "rvf"; then | ||
| 409 | + TAR_ARGS="rf" | ||
| 410 | + elif test "$TAR_ARGS" = "rvhf"; then | ||
| 411 | + TAR_ARGS="rhf" | ||
| 412 | + fi | ||
| 413 | +fi | ||
| 414 | + | ||
| 415 | +if test "$APPEND" = y; then | ||
| 416 | + if test $# -lt 2; then | ||
| 417 | + MS_Usage | ||
| 418 | + fi | ||
| 419 | + | ||
| 420 | + # Gather the info from the original archive | ||
| 421 | + OLDENV=`sh "$archname" --dumpconf` | ||
| 422 | + if test $? -ne 0; then | ||
| 423 | + echo "Unable to update archive: $archname" >&2 | ||
| 424 | + exit 1 | ||
| 425 | + else | ||
| 426 | + eval "$OLDENV" | ||
| 427 | + OLDSKIP=`expr $SKIP + 1` | ||
| 428 | + fi | ||
| 429 | +else | ||
| 430 | + if test "$KEEP" = n -a $# = 3; then | ||
| 431 | + echo "ERROR: Making a temporary archive with no embedded command does not make sense!" >&2 | ||
| 432 | + echo >&2 | ||
| 433 | + MS_Usage | ||
| 434 | + fi | ||
| 435 | + # We don't want to create an absolute directory unless a target directory is defined | ||
| 436 | + if test "$CURRENT" = y; then | ||
| 437 | + archdirname="." | ||
| 438 | + elif test x"$TARGETDIR" != x; then | ||
| 439 | + archdirname="$TARGETDIR" | ||
| 440 | + else | ||
| 441 | + archdirname=`basename "$1"` | ||
| 442 | + fi | ||
| 443 | + | ||
| 444 | + if test $# -lt 3; then | ||
| 445 | + MS_Usage | ||
| 446 | + fi | ||
| 447 | + | ||
| 448 | + LABEL="$3" | ||
| 449 | + SCRIPT="$4" | ||
| 450 | + test "x$SCRIPT" = x || shift 1 | ||
| 451 | + shift 3 | ||
| 452 | + SCRIPTARGS="$*" | ||
| 453 | +fi | ||
| 454 | + | ||
| 455 | +if test "$KEEP" = n -a "$CURRENT" = y; then | ||
| 456 | + echo "ERROR: It is A VERY DANGEROUS IDEA to try to combine --notemp and --current." >&2 | ||
| 457 | + exit 1 | ||
| 458 | +fi | ||
| 459 | + | ||
| 460 | +case $COMPRESS in | ||
| 461 | +gzip) | ||
| 462 | + GZIP_CMD="gzip -c$COMPRESS_LEVEL" | ||
| 463 | + GUNZIP_CMD="gzip -cd" | ||
| 464 | + ;; | ||
| 465 | +pigz) | ||
| 466 | + GZIP_CMD="pigz -$COMPRESS_LEVEL" | ||
| 467 | + if test $THREADS -ne $DEFAULT_THREADS; then # Leave as the default if threads not indicated | ||
| 468 | + GZIP_CMD="$GZIP_CMD --processes $THREADS" | ||
| 469 | + fi | ||
| 470 | + GUNZIP_CMD="gzip -cd" | ||
| 471 | + ;; | ||
| 472 | +zstd) | ||
| 473 | + GZIP_CMD="zstd -$COMPRESS_LEVEL" | ||
| 474 | + if test $THREADS -ne $DEFAULT_THREADS; then # Leave as the default if threads not indicated | ||
| 475 | + GZIP_CMD="$GZIP_CMD --threads=$THREADS" | ||
| 476 | + fi | ||
| 477 | + GUNZIP_CMD="zstd -cd" | ||
| 478 | + ;; | ||
| 479 | +pbzip2) | ||
| 480 | + GZIP_CMD="pbzip2 -c$COMPRESS_LEVEL" | ||
| 481 | + if test $THREADS -ne $DEFAULT_THREADS; then # Leave as the default if threads not indicated | ||
| 482 | + GZIP_CMD="$GZIP_CMD -p$THREADS" | ||
| 483 | + fi | ||
| 484 | + GUNZIP_CMD="bzip2 -d" | ||
| 485 | + ;; | ||
| 486 | +bzip3) | ||
| 487 | + # Map the compression level to a block size in MiB as 2^(level-1). | ||
| 488 | + BZ3_COMPRESS_LEVEL=`echo "2^($COMPRESS_LEVEL-1)" | bc` | ||
| 489 | + GZIP_CMD="bzip3 -b$BZ3_COMPRESS_LEVEL" | ||
| 490 | + if test $THREADS -ne $DEFAULT_THREADS; then # Leave as the default if threads not indicated | ||
| 491 | + GZIP_CMD="$GZIP_CMD -j$THREADS" | ||
| 492 | + fi | ||
| 493 | + JOBS=`echo "10-$COMPRESS_LEVEL" | bc` | ||
| 494 | + GUNZIP_CMD="bzip3 -dj$JOBS" | ||
| 495 | + ;; | ||
| 496 | +bzip2) | ||
| 497 | + GZIP_CMD="bzip2 -$COMPRESS_LEVEL" | ||
| 498 | + GUNZIP_CMD="bzip2 -d" | ||
| 499 | + ;; | ||
| 500 | +xz) | ||
| 501 | + GZIP_CMD="xz -c$COMPRESS_LEVEL" | ||
| 502 | + # Must opt-in by specifying a value since not all versions of xz support threads | ||
| 503 | + if test $THREADS -ne $DEFAULT_THREADS; then | ||
| 504 | + GZIP_CMD="$GZIP_CMD --threads=$THREADS" | ||
| 505 | + fi | ||
| 506 | + GUNZIP_CMD="xz -d" | ||
| 507 | + ;; | ||
| 508 | +lzo) | ||
| 509 | + GZIP_CMD="lzop -c$COMPRESS_LEVEL" | ||
| 510 | + GUNZIP_CMD="lzop -d" | ||
| 511 | + ;; | ||
| 512 | +lz4) | ||
| 513 | + GZIP_CMD="lz4 -c$COMPRESS_LEVEL" | ||
| 514 | + GUNZIP_CMD="lz4 -d" | ||
| 515 | + ;; | ||
| 516 | +base64) | ||
| 517 | + GZIP_CMD="base64" | ||
| 518 | + GUNZIP_CMD="base64 --decode -i -" | ||
| 519 | + ;; | ||
| 520 | +gpg) | ||
| 521 | + GZIP_CMD="gpg $GPG_EXTRA -ac -z$COMPRESS_LEVEL" | ||
| 522 | + GUNZIP_CMD="gpg -d" | ||
| 523 | + ENCRYPT="gpg" | ||
| 524 | + ;; | ||
| 525 | +gpg-asymmetric) | ||
| 526 | + GZIP_CMD="gpg $GPG_EXTRA -z$COMPRESS_LEVEL -es" | ||
| 527 | + GUNZIP_CMD="gpg --yes -d" | ||
| 528 | + ENCRYPT="gpg" | ||
| 529 | + ;; | ||
| 530 | +compress) | ||
| 531 | + GZIP_CMD="compress -fc" | ||
| 532 | + GUNZIP_CMD="(type compress >/dev/null 2>&1 && compress -fcd || gzip -cd)" | ||
| 533 | + ;; | ||
| 534 | +none) | ||
| 535 | + GZIP_CMD="cat" | ||
| 536 | + GUNZIP_CMD="cat" | ||
| 537 | + ;; | ||
| 538 | +esac | ||
| 539 | + | ||
| 540 | +if test x"$ENCRYPT" = x"openssl"; then | ||
| 541 | + if test x"$APPEND" = x"y"; then | ||
| 542 | + echo "Appending to existing archive is not compatible with OpenSSL encryption." >&2 | ||
| 543 | + fi | ||
| 544 | + | ||
| 545 | + ENCRYPT_CMD="openssl enc -aes-256-cbc -salt" | ||
| 546 | + DECRYPT_CMD="openssl enc -aes-256-cbc -d" | ||
| 547 | + | ||
| 548 | + if test x"$OPENSSL_NO_MD" != x"y"; then | ||
| 549 | + ENCRYPT_CMD="$ENCRYPT_CMD -md sha256" | ||
| 550 | + DECRYPT_CMD="$DECRYPT_CMD -md sha256" | ||
| 551 | + fi | ||
| 552 | + | ||
| 553 | + if test -n "$PASSWD_SRC"; then | ||
| 554 | + ENCRYPT_CMD="$ENCRYPT_CMD -pass $PASSWD_SRC" | ||
| 555 | + elif test -n "$PASSWD"; then | ||
| 556 | + ENCRYPT_CMD="$ENCRYPT_CMD -pass pass:$PASSWD" | ||
| 557 | + fi | ||
| 558 | +fi | ||
| 559 | + | ||
| 560 | +tmpfile="${TMPDIR:-/tmp}/mkself$$" | ||
| 561 | + | ||
| 562 | +if test -f "$HEADER"; then | ||
| 563 | + oldarchname="$archname" | ||
| 564 | + archname="$tmpfile" | ||
| 565 | + # Generate a fake header to count its lines | ||
| 566 | + SKIP=0 | ||
| 567 | + . "$HEADER" | ||
| 568 | + SKIP=`cat "$tmpfile" |wc -l` | ||
| 569 | + # Get rid of any spaces | ||
| 570 | + SKIP=`expr $SKIP` | ||
| 571 | + rm -f "$tmpfile" | ||
| 572 | + if test "$QUIET" = "n"; then | ||
| 573 | + echo "Header is $SKIP lines long" >&2 | ||
| 574 | + fi | ||
| 575 | + archname="$oldarchname" | ||
| 576 | +else | ||
| 577 | + echo "Unable to open header file: $HEADER" >&2 | ||
| 578 | + exit 1 | ||
| 579 | +fi | ||
| 580 | + | ||
| 581 | +if test "$QUIET" = "n"; then | ||
| 582 | + echo | ||
| 583 | +fi | ||
| 584 | + | ||
| 585 | +if test "$APPEND" = n; then | ||
| 586 | + if test -f "$archname"; then | ||
| 587 | + echo "WARNING: Overwriting existing file: $archname" >&2 | ||
| 588 | + fi | ||
| 589 | +fi | ||
| 590 | + | ||
| 591 | +USIZE=`du $DU_ARGS "$archdir" | awk '{print $1}'` | ||
| 592 | + | ||
| 593 | +if test "." = "$archdirname"; then | ||
| 594 | + if test "$KEEP" = n; then | ||
| 595 | + archdirname="makeself-$$-`date +%Y%m%d%H%M%S`" | ||
| 596 | + fi | ||
| 597 | +fi | ||
| 598 | + | ||
| 599 | +test -d "$archdir" || { echo "Error: $archdir does not exist."; rm -f "$tmpfile"; exit 1; } | ||
| 600 | +if test "$QUIET" = "n"; then | ||
| 601 | + echo "About to compress $USIZE KB of data..." | ||
| 602 | + echo "Adding files to archive named \"$archname\"..." | ||
| 603 | +fi | ||
| 604 | + | ||
| 605 | +# See if we have GNU tar | ||
| 606 | +TAR=`exec <&- 2>&-; which gtar || command -v gtar || type gtar` | ||
| 607 | +test -x "$TAR" || TAR=`exec <&- 2>&-; which bsdtar || command -v bsdtar || type bsdtar` | ||
| 608 | +test -x "$TAR" || TAR=tar | ||
| 609 | + | ||
| 610 | +tmparch="${TMPDIR:-/tmp}/mkself$$.tar" | ||
| 611 | +( | ||
| 612 | + if test "$APPEND" = "y"; then | ||
| 613 | + tail -n "+$OLDSKIP" "$archname" | eval "$GUNZIP_CMD" > "$tmparch" | ||
| 614 | + fi | ||
| 615 | + cd "$archdir" | ||
| 616 | + # "Determining if a directory is empty" | ||
| 617 | + # https://www.etalabs.net/sh_tricks.html | ||
| 618 | + find . \ | ||
| 619 | + \( \ | ||
| 620 | + ! -type d \ | ||
| 621 | + -o \ | ||
| 622 | + \( -links 2 -exec sh -c ' | ||
| 623 | + is_empty () ( | ||
| 624 | + cd "$1" | ||
| 625 | + set -- .[!.]* ; test -f "$1" && return 1 | ||
| 626 | + set -- ..?* ; test -f "$1" && return 1 | ||
| 627 | + set -- * ; test -f "$1" && return 1 | ||
| 628 | + return 0 | ||
| 629 | + ) | ||
| 630 | + is_empty "$0"' {} \; \ | ||
| 631 | + \) \ | ||
| 632 | + \) -print \ | ||
| 633 | + | LC_ALL=C sort \ | ||
| 634 | + | sed 's/./\\&/g' \ | ||
| 635 | + | xargs $TAR $TAR_EXTRA --format $TAR_FORMAT -$TAR_ARGS "$tmparch" | ||
| 636 | +) || { | ||
| 637 | + echo "ERROR: failed to create temporary archive: $tmparch" | ||
| 638 | + rm -f "$tmparch" "$tmpfile" | ||
| 639 | + exit 1 | ||
| 640 | +} | ||
| 641 | + | ||
| 642 | +USIZE=`du $DU_ARGS "$tmparch" | awk '{print $1}'` | ||
| 643 | + | ||
| 644 | +eval "$GZIP_CMD" <"$tmparch" >"$tmpfile" || { | ||
| 645 | + echo "ERROR: failed to create temporary file: $tmpfile" | ||
| 646 | + rm -f "$tmparch" "$tmpfile" | ||
| 647 | + exit 1 | ||
| 648 | +} | ||
| 649 | +rm -f "$tmparch" | ||
| 650 | + | ||
| 651 | +if test x"$ENCRYPT" = x"openssl"; then | ||
| 652 | + echo "About to encrypt archive \"$archname\"..." | ||
| 653 | + { eval "$ENCRYPT_CMD -in $tmpfile -out ${tmpfile}.enc" && mv -f ${tmpfile}.enc $tmpfile; } || \ | ||
| 654 | + { echo Aborting: could not encrypt temporary file: "$tmpfile".; rm -f "$tmpfile"; exit 1; } | ||
| 655 | +fi | ||
| 656 | + | ||
| 657 | +fsize=`cat "$tmpfile" | wc -c | tr -d " "` | ||
| 658 | + | ||
| 659 | +# Compute the checksums | ||
| 660 | + | ||
| 661 | +shasum=0000000000000000000000000000000000000000000000000000000000000000 | ||
| 662 | +md5sum=00000000000000000000000000000000 | ||
| 663 | +crcsum=0000000000 | ||
| 664 | + | ||
| 665 | +if test "$NOCRC" = y; then | ||
| 666 | + if test "$QUIET" = "n"; then | ||
| 667 | + echo "skipping crc at user request" | ||
| 668 | + fi | ||
| 669 | +else | ||
| 670 | + crcsum=`CMD_ENV=xpg4 cksum < "$tmpfile" | sed -e 's/ /Z/' -e 's/ /Z/' | cut -dZ -f1` | ||
| 671 | + if test "$QUIET" = "n"; then | ||
| 672 | + echo "CRC: $crcsum" | ||
| 673 | + fi | ||
| 674 | +fi | ||
| 675 | + | ||
| 676 | +if test "$SHA256" = y; then | ||
| 677 | + SHA_PATH=`exec <&- 2>&-; which shasum || command -v shasum || type shasum` | ||
| 678 | + if test -x "$SHA_PATH"; then | ||
| 679 | + shasum=`eval "$SHA_PATH -a 256" < "$tmpfile" | cut -b-64` | ||
| 680 | + else | ||
| 681 | + SHA_PATH=`exec <&- 2>&-; which sha256sum || command -v sha256sum || type sha256sum` | ||
| 682 | + shasum=`eval "$SHA_PATH" < "$tmpfile" | cut -b-64` | ||
| 683 | + fi | ||
| 684 | + if test "$QUIET" = "n"; then | ||
| 685 | + if test -x "$SHA_PATH"; then | ||
| 686 | + echo "SHA256: $shasum" | ||
| 687 | + else | ||
| 688 | + echo "SHA256: none, SHA command not found" | ||
| 689 | + fi | ||
| 690 | + fi | ||
| 691 | +fi | ||
| 692 | +if test "$NOMD5" = y; then | ||
| 693 | + if test "$QUIET" = "n"; then | ||
| 694 | + echo "Skipping md5sum at user request" | ||
| 695 | + fi | ||
| 696 | +else | ||
| 697 | + # Try to locate a MD5 binary | ||
| 698 | + OLD_PATH=$PATH | ||
| 699 | + PATH=${GUESS_MD5_PATH:-"$OLD_PATH:/bin:/usr/bin:/sbin:/usr/local/ssl/bin:/usr/local/bin:/opt/openssl/bin"} | ||
| 700 | + MD5_ARG="" | ||
| 701 | + MD5_PATH=`exec <&- 2>&-; which md5sum || command -v md5sum || type md5sum` | ||
| 702 | + test -x "$MD5_PATH" || MD5_PATH=`exec <&- 2>&-; which md5 || command -v md5 || type md5` | ||
| 703 | + test -x "$MD5_PATH" || MD5_PATH=`exec <&- 2>&-; which digest || command -v digest || type digest` | ||
| 704 | + PATH=$OLD_PATH | ||
| 705 | + if test -x "$MD5_PATH"; then | ||
| 706 | + if test `basename ${MD5_PATH}`x = digestx; then | ||
| 707 | + MD5_ARG="-a md5" | ||
| 708 | + fi | ||
| 709 | + md5sum=`eval "$MD5_PATH $MD5_ARG" < "$tmpfile" | cut -b-32` | ||
| 710 | + if test "$QUIET" = "n"; then | ||
| 711 | + echo "MD5: $md5sum" | ||
| 712 | + fi | ||
| 713 | + else | ||
| 714 | + if test "$QUIET" = "n"; then | ||
| 715 | + echo "MD5: none, MD5 command not found" | ||
| 716 | + fi | ||
| 717 | + fi | ||
| 718 | +fi | ||
| 719 | +if test "$SIGN" = y; then | ||
| 720 | + GPG_PATH=`exec <&- 2>&-; which gpg || command -v gpg || type gpg` | ||
| 721 | + if test -x "$GPG_PATH"; then | ||
| 722 | + SIGNATURE=`$GPG_PATH --pinentry-mode=loopback --batch --yes $GPG_EXTRA --passphrase "$GPG_PASSPHRASE" --output - --detach-sig $tmpfile | base64 | tr -d \\\\n` | ||
| 723 | + if test "$QUIET" = "n"; then | ||
| 724 | + echo "Signature: $SIGNATURE" | ||
| 725 | + fi | ||
| 726 | + else | ||
| 727 | + echo "Missing gpg command" >&2 | ||
| 728 | + fi | ||
| 729 | +fi | ||
| 730 | + | ||
| 731 | +totalsize=0 | ||
| 732 | +for size in $fsize; | ||
| 733 | +do | ||
| 734 | + totalsize=`expr $totalsize + $size` | ||
| 735 | +done | ||
| 736 | + | ||
| 737 | +if test "$APPEND" = y; then | ||
| 738 | + mv "$archname" "$archname".bak || exit | ||
| 739 | + | ||
| 740 | + # Prepare entry for new archive | ||
| 741 | + filesizes="$fsize" | ||
| 742 | + CRCsum="$crcsum" | ||
| 743 | + MD5sum="$md5sum" | ||
| 744 | + SHAsum="$shasum" | ||
| 745 | + Signature="$SIGNATURE" | ||
| 746 | + # Generate the header | ||
| 747 | + . "$HEADER" | ||
| 748 | + # Append the new data | ||
| 749 | + cat "$tmpfile" >> "$archname" | ||
| 750 | + | ||
| 751 | + chmod +x "$archname" | ||
| 752 | + rm -f "$archname".bak | ||
| 753 | + if test "$QUIET" = "n"; then | ||
| 754 | + echo "Self-extractable archive \"$archname\" successfully updated." | ||
| 755 | + fi | ||
| 756 | +else | ||
| 757 | + filesizes="$fsize" | ||
| 758 | + CRCsum="$crcsum" | ||
| 759 | + MD5sum="$md5sum" | ||
| 760 | + SHAsum="$shasum" | ||
| 761 | + Signature="$SIGNATURE" | ||
| 762 | + | ||
| 763 | + # Generate the header | ||
| 764 | + . "$HEADER" | ||
| 765 | + | ||
| 766 | + # Append the compressed tar data after the stub | ||
| 767 | + if test "$QUIET" = "n"; then | ||
| 768 | + echo | ||
| 769 | + fi | ||
| 770 | + cat "$tmpfile" >> "$archname" | ||
| 771 | + chmod +x "$archname" | ||
| 772 | + if test "$QUIET" = "n"; then | ||
| 773 | + echo Self-extractable archive \"$archname\" successfully created. | ||
| 774 | + fi | ||
| 775 | +fi | ||
| 776 | +rm -f "$tmpfile" | ||
| @@ -0,0 +1,32 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 3 | + | ||
| 4 | +project_path=$1 | ||
| 5 | +build_path=$2 | ||
| 6 | +vendor_name=customize | ||
| 7 | +echo $@ | ||
| 8 | +if [[ ! -d "$project_path" ]]; then | ||
| 9 | + echo "[ERROR] No projcet path is provided" | ||
| 10 | + exit 1 | ||
| 11 | +fi | ||
| 12 | + | ||
| 13 | +if [[ ! -d "$build_path" ]]; then | ||
| 14 | + echo "[ERROR] No build path is provided" | ||
| 15 | + exit 1 | ||
| 16 | +fi | ||
| 17 | + | ||
| 18 | +if [[ ! -d "$ASCEND_OPP_PATH" ]]; then | ||
| 19 | + echo "[ERROR] No opp install path is provided" | ||
| 20 | + exit 1 | ||
| 21 | +fi | ||
| 22 | +custom_exist_info_json=$ASCEND_OPP_PATH/vendors/$vendor_name/op_impl/cpu/config/cust_aicpu_kernel.json | ||
| 23 | +custom_new_info_json=$build_path/makepkg/packages/vendors/$vendor_name/op_impl/cpu/config/cust_aicpu_kernel.json | ||
| 24 | +temp_info_json=$build_path/makepkg/packages/vendors/$vendor_name/op_impl/cpu/config/temp_cust_aicpu_kernel.json | ||
| 25 | + | ||
| 26 | +if [[ -f "$custom_exist_info_json" ]] && [[ -f "$custom_new_info_json" ]]; then | ||
| 27 | + cp -f $custom_exist_info_json $temp_info_json | ||
| 28 | + chmod +w $temp_info_json | ||
| 29 | + python3 ${project_path}/cmake/util/insert_op_info.py ${custom_new_info_json} ${temp_info_json} | ||
| 30 | + cp -f $temp_info_json $custom_new_info_json | ||
| 31 | + rm -f $temp_info_json | ||
| 32 | +fi | ||
| @@ -0,0 +1,352 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import sys | ||
| 9 | +import os | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +OP_ALL = '__ALLOP__' | ||
| 13 | +SOC_ALL = '__ALLSOC__' | ||
| 14 | +SOC_TO_SHORT_SOC_MAP = { | ||
| 15 | + "ascend910a": "ascend910", | ||
| 16 | + "ascend910proa": "ascend910", | ||
| 17 | + "ascend910b": "ascend910", | ||
| 18 | + "ascend910prob": "ascend910", | ||
| 19 | + "ascend910premiuma": "ascend910", | ||
| 20 | + "ascend910b1": "ascend910b", | ||
| 21 | + "ascend910b2": "ascend910b", | ||
| 22 | + "ascend910b2c": "ascend910b", | ||
| 23 | + "ascend910b3": "ascend910b", | ||
| 24 | + "ascend910b4": "ascend910b", | ||
| 25 | + "ascend910b4-1": "ascend910b", | ||
| 26 | + "ascend910_9391": "ascend910_93", | ||
| 27 | + "ascend910_9381": "ascend910_93", | ||
| 28 | + "ascend910_9372": "ascend910_93", | ||
| 29 | + "ascend910_9392": "ascend910_93", | ||
| 30 | + "ascend910_9382": "ascend910_93", | ||
| 31 | + "ascend910_9362": "ascend910_93", | ||
| 32 | + "ascend310p1": "ascend310p", | ||
| 33 | + "ascend310p3": "ascend310p", | ||
| 34 | + "ascend310p5": "ascend310p", | ||
| 35 | + "ascend310p7": "ascend310p", | ||
| 36 | + "ascend310p3vir01": "ascend310p", | ||
| 37 | + "ascend310p3vir02": "ascend310p", | ||
| 38 | + "ascend310p3vir04": "ascend310p", | ||
| 39 | + "ascend310p3vir08": "ascend310p", | ||
| 40 | + "ascend310b1": "ascend310b", | ||
| 41 | + "bs9sx1aa": "bs9sx1a", | ||
| 42 | + "ascend610lite": "ascend610lite", | ||
| 43 | + "ascend910_9599": "ascend910_95" | ||
| 44 | +} | ||
| 45 | +CONFLICT_KEYWORDS = { | ||
| 46 | + "and", "as", "assert", "break", "class", "continue", "def", "del", "elif", "else", | ||
| 47 | + "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", | ||
| 48 | + "not", "or", "pass", "raise", "return", "try", "while", "with", "yield", "False", | ||
| 49 | + "None", "True", "nonlocal", "arg", "__inputs__", "__outputs__", "options", "bisheng", | ||
| 50 | + "bisheng_path", "tikcpp_path", "impl_mode", "custom_compile_options", | ||
| 51 | + "custom_all_compile_options", "soc_version", "soc_short", "custom_compile_options_soc", | ||
| 52 | + "custom_all_compile_options_soc", "origin_func_name", "ascendc_src_dir_ex", | ||
| 53 | + "ascendc_src_dir", "ascendc_src_file", "src", "op_type", "code_channel", "op_info", | ||
| 54 | + "compile_op", "get_code_channel", "result", "__attrs__", "isinstance", "attr", | ||
| 55 | + "get_current_build_config", "_build_args", "get_dtype_fmt_options", "shutil", "os", | ||
| 56 | + "get_kernel_source" | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | + | ||
| 60 | +class OpDesc: | ||
| 61 | + def __init__(self: any, op_type: str): | ||
| 62 | + self.op_type = op_type | ||
| 63 | + self.attr_list = [] | ||
| 64 | + self.attr_val = {} | ||
| 65 | + self.input_name = [] | ||
| 66 | + self.input_ori_name = [] | ||
| 67 | + self.input_type = [] | ||
| 68 | + self.input_dtype = [] | ||
| 69 | + self.input_dtype_for_bin_list = [] | ||
| 70 | + self.input_dtype_for_bin = {} | ||
| 71 | + self.input_fmt = [] | ||
| 72 | + self.input_fmt_for_bin_list = [] | ||
| 73 | + self.input_fmt_for_bin = {} | ||
| 74 | + self.input_virt = {} | ||
| 75 | + self.input_value_depend = {} | ||
| 76 | + self.output_name = [] | ||
| 77 | + self.output_ori_name = [] | ||
| 78 | + self.output_type = [] | ||
| 79 | + self.output_dtype = [] | ||
| 80 | + self.output_dtype_for_bin_list = [] | ||
| 81 | + self.output_dtype_for_bin = {} | ||
| 82 | + self.output_fmt = [] | ||
| 83 | + self.output_fmt_for_bin_list = [] | ||
| 84 | + self.output_fmt_for_bin = {} | ||
| 85 | + self.output_init_value = [] | ||
| 86 | + self.output_shape_depend_on_compute = [] | ||
| 87 | + self.op_fmt_sel = False | ||
| 88 | + self.op_chk_support = False | ||
| 89 | + self.op_intf = '' | ||
| 90 | + self.kern_name = '' | ||
| 91 | + self.op_file = '' | ||
| 92 | + self.op_replay_flag = False | ||
| 93 | + self.op_replay_batch = False | ||
| 94 | + self.input_idx = -1 | ||
| 95 | + self.output_idx = -1 | ||
| 96 | + self.max_block_dim = 32 | ||
| 97 | + self.max_shape_size = 268435456 | ||
| 98 | + self.dynamic_shape = False | ||
| 99 | + self.op_range_limit = '' | ||
| 100 | + self.custom_compile_options = {} | ||
| 101 | + self.custom_all_compile_options = {} | ||
| 102 | + self.param_type_dynamic = False | ||
| 103 | + self.mc2_ctx = [] | ||
| 104 | + self.bin_cprs_list = [] | ||
| 105 | + self.bin_cprs_head = [] | ||
| 106 | + self.bin_save_list = [] | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + def _parse_digit(conf: str) -> int: | ||
| 110 | + return int(conf.split('=')[1]) | ||
| 111 | + | ||
| 112 | + | ||
| 113 | + def _parse_flag(conf: str) -> bool: | ||
| 114 | + if 'true' == conf.split('=')[1]: | ||
| 115 | + return True | ||
| 116 | + return False | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + def _parse_str(conf: str) -> str: | ||
| 120 | + return conf.split('=')[1] | ||
| 121 | + | ||
| 122 | + | ||
| 123 | + def _parse_list(conf: str) -> list: | ||
| 124 | + return conf.split('=')[1].split(',') | ||
| 125 | + | ||
| 126 | + def parse_input(self: any, conf: str): | ||
| 127 | + if conf.startswith('input{}.name'.format(int(self.input_idx) + 1)): | ||
| 128 | + self.input_idx += 1 | ||
| 129 | + self.input_ori_name.append(self._parse_str(conf)) | ||
| 130 | + self.input_name.append(self.input_ori_name[-1] + '_in__') | ||
| 131 | + elif conf.startswith('input{}.paramType'.format(int(self.input_idx))): | ||
| 132 | + param_type = self._parse_str(conf) | ||
| 133 | + self.input_type.append(param_type) | ||
| 134 | + if param_type == "dynamic": | ||
| 135 | + self.param_type_dynamic = True | ||
| 136 | + elif conf.startswith('input{}.dtype'.format(int(self.input_idx))): | ||
| 137 | + self.input_dtype.append(self._parse_str(conf)) | ||
| 138 | + elif conf.startswith('input{}.for_bin_dtype'.format(int(self.input_idx))): | ||
| 139 | + self.input_dtype_for_bin.update({self.input_idx : self._parse_str(conf)}) | ||
| 140 | + elif conf.startswith('input{}.format'.format(int(self.input_idx))): | ||
| 141 | + self.input_fmt.append(self._parse_str(conf)) | ||
| 142 | + elif conf.startswith('input{}.for_bin_format'.format(int(self.input_idx))): | ||
| 143 | + self.input_fmt_for_bin.update({self.input_idx : self._parse_str(conf)}) | ||
| 144 | + elif conf.startswith('input{}.virtual'.format(int(self.input_idx))): | ||
| 145 | + self.input_virt[self.input_idx] = self._parse_str(conf) | ||
| 146 | + elif conf.startswith('input{}.valueDepend'.format(int(self.input_idx))): | ||
| 147 | + self.input_value_depend[self.input_idx] = self._parse_str(conf) | ||
| 148 | + elif conf.startswith('input{}.initValue'.format(int(self.input_idx))): | ||
| 149 | + raise Exception(f'[ERROR]: Op: {{\'{self.op_type}\'}} input {self.input_ori_name[int(self.input_idx)]}\ | ||
| 150 | + has InitValue, which is not support!') | ||
| 151 | + else: | ||
| 152 | + return | ||
| 153 | + | ||
| 154 | + def parse_output(self: any, conf: str): | ||
| 155 | + if conf.startswith('output{}.name'.format(int(self.output_idx) + 1)): | ||
| 156 | + self.output_idx += 1 | ||
| 157 | + self.output_ori_name.append(self._parse_str(conf)) | ||
| 158 | + self.output_name.append(self.output_ori_name[-1] + '_out_') | ||
| 159 | + self.output_init_value.append(None) | ||
| 160 | + elif conf.startswith('output{}.paramType'.format(int(self.output_idx))): | ||
| 161 | + param_type = self._parse_str(conf) | ||
| 162 | + self.output_type.append(param_type) | ||
| 163 | + if param_type == "dynamic": | ||
| 164 | + self.param_type_dynamic = True | ||
| 165 | + elif conf.startswith('output{}.dtype'.format(int(self.output_idx))): | ||
| 166 | + self.output_dtype.append(self._parse_str(conf)) | ||
| 167 | + elif conf.startswith('output{}.for_bin_dtype'.format(int(self.output_idx))): | ||
| 168 | + self.output_dtype_for_bin.update({self.output_idx : self._parse_str(conf)}) | ||
| 169 | + elif conf.startswith('output{}.format'.format(int(self.output_idx))): | ||
| 170 | + self.output_fmt.append(self._parse_str(conf)) | ||
| 171 | + elif conf.startswith('output{}.for_bin_format'.format(int(self.output_idx))): | ||
| 172 | + self.output_fmt_for_bin.update({self.output_idx : self._parse_str(conf)}) | ||
| 173 | + elif conf.startswith('output{}.initValue'.format(int(self.output_idx))): | ||
| 174 | + self.output_init_value[int(self.output_idx)] = self._parse_str(conf) | ||
| 175 | + elif conf.startswith('output{}.outputShapeDependOnCompute=true'.format(int(self.output_idx))): | ||
| 176 | + self.output_shape_depend_on_compute.append(int(self.output_idx)) | ||
| 177 | + else: | ||
| 178 | + return | ||
| 179 | + | ||
| 180 | + def parse_op_format(self: any, conf: str): | ||
| 181 | + self.op_fmt_sel = self._parse_flag(conf) | ||
| 182 | + | ||
| 183 | + def parse_check_support(self: any, conf: str): | ||
| 184 | + self.op_chk_support = self._parse_flag(conf) | ||
| 185 | + | ||
| 186 | + def parse_range_limit(self: any, conf: str): | ||
| 187 | + self.op_range_limit = self._parse_str(conf) | ||
| 188 | + | ||
| 189 | + def parse_kern_name(self: any, conf: str): | ||
| 190 | + self.kern_name = self._parse_str(conf) | ||
| 191 | + | ||
| 192 | + def parse_op_intf(self: any, conf: str): | ||
| 193 | + self.op_intf = self._parse_str(conf) | ||
| 194 | + | ||
| 195 | + def parse_op_file(self: any, conf: str): | ||
| 196 | + self.op_file = self._parse_str(conf) | ||
| 197 | + | ||
| 198 | + def parse_dynamic_shape(self: any, conf: str): | ||
| 199 | + self.dynamic_shape = self._parse_flag(conf) | ||
| 200 | + | ||
| 201 | + def parse_attr_list(self: any, conf: str): | ||
| 202 | + self.attr_list = self._parse_list(conf) | ||
| 203 | + intersection_element = set(self.attr_list) & CONFLICT_KEYWORDS | ||
| 204 | + if intersection_element: | ||
| 205 | + raise Exception(f'[ERROR]: The attribute name: {intersection_element} in op: {{\'{self.op_type}\'}} \ | ||
| 206 | +conflicts with the built-in variable name. Use a complex name or prefix the operator name.') | ||
| 207 | + | ||
| 208 | + def parse_mc2_ctx(self: any, conf: str): | ||
| 209 | + self.mc2_ctx = self._parse_list(conf) | ||
| 210 | + | ||
| 211 | + | ||
| 212 | + def _camel_to_snake(camel_case_str: str): | ||
| 213 | + snake_case_str = '' | ||
| 214 | + for i, c in enumerate(camel_case_str): | ||
| 215 | + if i == 0: | ||
| 216 | + snake_case_str += c.lower() | ||
| 217 | + elif c.isupper(): | ||
| 218 | + snake_case_str += '_' + c.lower() | ||
| 219 | + else: | ||
| 220 | + snake_case_str += c | ||
| 221 | + return snake_case_str | ||
| 222 | + | ||
| 223 | + def parse_attr_val(self: any, conf: str): | ||
| 224 | + for attr in self.attr_list: | ||
| 225 | + if self.attr_val.get(attr) is None: | ||
| 226 | + self.attr_val[attr] = {} | ||
| 227 | + if conf.startswith('attr_{}.type'.format(attr)): | ||
| 228 | + self.attr_val.get(attr)['type'] = self._camel_to_snake(self._parse_str(conf)) | ||
| 229 | + elif conf.startswith('attr_{}.paramType'.format(attr)): | ||
| 230 | + self.attr_val.get(attr)['paramType'] = self._parse_str(conf) | ||
| 231 | + elif conf.startswith('attr_{}.defaultValue'.format(attr)): | ||
| 232 | + self.attr_val.get(attr)['defaultValue'] = self._parse_str(conf) | ||
| 233 | + | ||
| 234 | + def parse_replay_val(self: any, batch_list: list, iterator_list: list): | ||
| 235 | + if self.op_type in batch_list: | ||
| 236 | + self.op_replay_flag = True | ||
| 237 | + self.op_replay_batch = True | ||
| 238 | + elif self.op_type in iterator_list: | ||
| 239 | + self.op_replay_flag = True | ||
| 240 | + self.op_replay_batch = False | ||
| 241 | + | ||
| 242 | + | ||
| 243 | +def _is_op_type_in_opdesc(op_descs: list, op_type: str): | ||
| 244 | + for op in op_descs: | ||
| 245 | + if op_type == op.op_type: | ||
| 246 | + return True | ||
| 247 | + return False | ||
| 248 | + | ||
| 249 | + | ||
| 250 | +def _set_all_options_to_opdescs(op_descs, soc_ver_compile_options): | ||
| 251 | + for op in op_descs: | ||
| 252 | + op.custom_all_compile_options = soc_ver_compile_options | ||
| 253 | + | ||
| 254 | + | ||
| 255 | +def _set_options_to_opdesc(op_descs, op_type, soc_ver_compile_options): | ||
| 256 | + for op in op_descs: | ||
| 257 | + if op.op_type != op_type: | ||
| 258 | + continue | ||
| 259 | + op.custom_compile_options.update(soc_ver_compile_options) | ||
| 260 | + | ||
| 261 | + | ||
| 262 | +def _trans_soc_ver_to_short(soc_ver: str): | ||
| 263 | + low_soc_ver = soc_ver.lower() | ||
| 264 | + if low_soc_ver not in SOC_TO_SHORT_SOC_MAP: | ||
| 265 | + print(f'WARNING: caution: {soc_ver} will trans into ascend910, if not your intention,' | ||
| 266 | + f'use ascend910b1~4 instead') | ||
| 267 | + return SOC_TO_SHORT_SOC_MAP[low_soc_ver] | ||
| 268 | + | ||
| 269 | + | ||
| 270 | +def _get_op_custom_options(op_descs: list, auto_gen_dir: str): | ||
| 271 | + if auto_gen_dir is None: | ||
| 272 | + return {} | ||
| 273 | + file = os.path.join(auto_gen_dir, "custom_compile_options.ini") | ||
| 274 | + if not os.path.exists(file): | ||
| 275 | + print(f'WARNING: cannot find {auto_gen_dir}/custom_compile_options.ini') | ||
| 276 | + return {} | ||
| 277 | + with open (file, 'r') as fd: | ||
| 278 | + lines = fd.readlines() | ||
| 279 | + for line in lines: | ||
| 280 | + param_list = str.split(line.rstrip('\n'), ',') | ||
| 281 | + if len(param_list) != 3: | ||
| 282 | + raise Exception(f'ERROR: custom compile option {param_list} len is not 3') | ||
| 283 | + op_type = param_list[0] | ||
| 284 | + if op_type.upper() == 'ALL': | ||
| 285 | + op_type = OP_ALL | ||
| 286 | + if op_type != OP_ALL and _is_op_type_in_opdesc(op_descs, op_type) == False: | ||
| 287 | + continue | ||
| 288 | + soc_ver_compile_options = {} | ||
| 289 | + soc_ver = param_list[1] | ||
| 290 | + options_str = param_list[2] | ||
| 291 | + options = str.split(options_str, ';') | ||
| 292 | + if soc_ver == '': | ||
| 293 | + soc_ver_compile_options[SOC_ALL] = options | ||
| 294 | + else: | ||
| 295 | + soc_ver_list = str.split(soc_ver, ';') | ||
| 296 | + for ver in soc_ver_list: | ||
| 297 | + short_ver = _trans_soc_ver_to_short(ver) | ||
| 298 | + soc_ver_compile_options[short_ver] = options | ||
| 299 | + if op_type == OP_ALL: | ||
| 300 | + _set_all_options_to_opdescs(op_descs, soc_ver_compile_options) | ||
| 301 | + else: | ||
| 302 | + _set_options_to_opdesc(op_descs, op_type, soc_ver_compile_options) | ||
| 303 | + | ||
| 304 | + | ||
| 305 | +def get_op_desc(file: str, batch_list: list, iterator_list: list, builder: any, | ||
| 306 | + op_type: list, auto_gen_dir: str = None) -> list: | ||
| 307 | + op_descs = [] | ||
| 308 | + op_match = False | ||
| 309 | + with open (file, 'r') as fd: | ||
| 310 | + lines = fd.readlines() | ||
| 311 | + for line in lines: | ||
| 312 | + line = line.strip() | ||
| 313 | + if line.startswith('['): | ||
| 314 | + name = line[1:-1] | ||
| 315 | + if op_type is None or name in op_type: | ||
| 316 | + op_match = True | ||
| 317 | + op_desc = builder(name) | ||
| 318 | + op_desc.parse_replay_val(batch_list, iterator_list) | ||
| 319 | + op_descs.append(op_desc) | ||
| 320 | + else: | ||
| 321 | + op_match = False | ||
| 322 | + if op_type is not None and len(op_descs) == len(op_type): | ||
| 323 | + break | ||
| 324 | + continue | ||
| 325 | + if not op_match: | ||
| 326 | + continue | ||
| 327 | + if line.startswith('input'): | ||
| 328 | + op_desc.parse_input(line) | ||
| 329 | + elif line.startswith('output'): | ||
| 330 | + op_desc.parse_output(line) | ||
| 331 | + elif line.startswith('dynamicFormat.flag'): | ||
| 332 | + op_desc.parse_op_format(line) | ||
| 333 | + elif line.startswith('needCheckSupport.flag'): | ||
| 334 | + op_desc.parse_check_support(line) | ||
| 335 | + elif line.startswith('rangeLimit.value'): | ||
| 336 | + op_desc.parse_range_limit(line) | ||
| 337 | + elif line.startswith('opInterface.value'): | ||
| 338 | + op_desc.parse_op_intf(line) | ||
| 339 | + elif line.startswith('kernel.name'): | ||
| 340 | + op_desc.parse_kern_name(line) | ||
| 341 | + elif line.startswith('opFile.value'): | ||
| 342 | + op_desc.parse_op_file(line) | ||
| 343 | + elif line.startswith('dynamicShapeSupport.flag'): | ||
| 344 | + op_desc.parse_dynamic_shape(line) | ||
| 345 | + elif line.startswith('mc2.ctx'): | ||
| 346 | + op_desc.parse_mc2_ctx(line) | ||
| 347 | + elif line.startswith('attr.list'): | ||
| 348 | + op_desc.parse_attr_list(line) | ||
| 349 | + elif line.startswith('attr_'): | ||
| 350 | + op_desc.parse_attr_val(line) | ||
| 351 | + _get_op_custom_options(op_descs, auto_gen_dir) | ||
| 352 | + return op_descs | ||
| @@ -0,0 +1,347 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# Copyright 2020-2021 Huawei Technologies Co., Ltd | ||
| 4 | +# | ||
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | +# you may not use this file except in compliance with the License. | ||
| 7 | +# You may obtain a copy of the License at | ||
| 8 | +# | ||
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | +# | ||
| 11 | +# Unless required by applicable law or agreed to in writing, software | ||
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +# See the License for the specific language governing permissions and | ||
| 15 | +# limitations under the License. | ||
| 16 | + | ||
| 17 | +""" | ||
| 18 | +parser ini to json | ||
| 19 | +""" | ||
| 20 | + | ||
| 21 | +import json | ||
| 22 | +import os | ||
| 23 | +import stat | ||
| 24 | +import sys | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +ATTR_TYPE_LIST = ["int", "float", "bool", "str", "listInt", "listFloat", "listBool", "listStr", "listListInt", | ||
| 28 | + "type", "listType", "tensor", "listTensor"] | ||
| 29 | +ATTR_PARAMTYPE_LIST = ["optional", "required"] | ||
| 30 | +BOOL_FLAG_KEY = ["dynamicFormat", "dynamicShapeSupport", "dynamicRankSupport", "precision_reduce", "heavyOp", | ||
| 31 | + "needCheckSupport", "enableVectorCore"] | ||
| 32 | +BOOL_LIST = ["true", "false"] | ||
| 33 | +DTYPE_LIST = ["float16", "float", "float32", "int8", "int16", "int32", "uint8", "uint16", "uint32", "bool", | ||
| 34 | + "int64", "uint64", "qint8", "qint16", "qint32", "quint8", "quint16", "double", "complex32", "complex64", | ||
| 35 | + "complex128", "string", "resource", "dual", "dual_sub_int8", "dual_sub_uint8", "string_ref", | ||
| 36 | + "int4", "bfloat16", "uint1", "hifloat8", "float8_e4m3fn", "float8_e5m2", "float8_e8m0", "float4_e2m1", | ||
| 37 | + "float4_e1m2", "int2"] | ||
| 38 | +FORMAT_LIST = ["NCHW", "NHWC", "ND", "NC1HWC0", "FRACTAL_Z", "NC1C0HWPAD", "NHWC1C0", "FSR_NCHW", "FRACTAL_DECONV", | ||
| 39 | + "C1HWNC0", "FRACTAL_DECONV_TRANSPOSE", "FRACTAL_DECONV_SP_STRIDE_TRANS", "NC1HWC0_C04", | ||
| 40 | + "FRACTAL_Z_C04", "CHWN", "FRACTAL_DECONV_SP_STRIDE8_TRANS", "HWCN", "NC1KHKWHWC0", "BN_WEIGHT", | ||
| 41 | + "FILTER_HWCK", "HASHTABLE_LOOKUP_LOOKUPS", "HASHTABLE_LOOKUP_KEYS", "HASHTABLE_LOOKUP_VALUE", | ||
| 42 | + "HASHTABLE_LOOKUP_OUTPUT", "HASHTABLE_LOOKUP_HITS", "C1HWNCoC0", "MD", "NDHWC", "FRACTAL_ZZ", | ||
| 43 | + "FRACTAL_NZ", "NCDHW", "DHWCN", "NDC1HWC0", "FRACTAL_Z_3D", "CN", "NC", "DHWNC", | ||
| 44 | + "FRACTAL_Z_3D_TRANSPOSE", "FRACTAL_ZN_LSTM", "FRACTAL_ZN_RNN", "FRACTAL_Z_G", "NULL"] | ||
| 45 | + | ||
| 46 | + | ||
| 47 | +def parse_ini_files(ini_files): | ||
| 48 | + """ | ||
| 49 | + parse ini files to json | ||
| 50 | + Parameters: | ||
| 51 | + ---------------- | ||
| 52 | + ini_files:input file list | ||
| 53 | + return:ops_info | ||
| 54 | + ---------------- | ||
| 55 | + """ | ||
| 56 | + tbe_ops_info = {} | ||
| 57 | + for ini_file in ini_files: | ||
| 58 | + check_file_size(ini_file) | ||
| 59 | + parse_ini_to_obj(ini_file, tbe_ops_info) | ||
| 60 | + return tbe_ops_info | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +def check_file_size(input_file): | ||
| 64 | + try: | ||
| 65 | + file_size = os.path.getsize(input_file) | ||
| 66 | + except OSError as os_error: | ||
| 67 | + print('[ERROR] Failed to open "%s". %s' % (input_file, str(os_error))) | ||
| 68 | + raise OSError from os_error | ||
| 69 | + if file_size > 10*1024*1024: | ||
| 70 | + print('[WARN] The size of %s exceeds 10MB, it may take more time to run, please wait.' % input_file) | ||
| 71 | + | ||
| 72 | + | ||
| 73 | +def parse_ini_to_obj(ini_file, tbe_ops_info): | ||
| 74 | + """ | ||
| 75 | + parse ini file to json obj | ||
| 76 | + Parameters: | ||
| 77 | + ---------------- | ||
| 78 | + ini_file:ini file path | ||
| 79 | + tbe_ops_info:ops_info | ||
| 80 | + ---------------- | ||
| 81 | + """ | ||
| 82 | + with open(ini_file) as ini_file: | ||
| 83 | + lines = ini_file.readlines() | ||
| 84 | + op_dict = {} | ||
| 85 | + op_name = "" | ||
| 86 | + find_op_type = False | ||
| 87 | + for line in lines: | ||
| 88 | + line = line.rstrip() | ||
| 89 | + if line == "": | ||
| 90 | + continue | ||
| 91 | + if line.startswith("["): | ||
| 92 | + if line.endswith("]"): | ||
| 93 | + op_name = line[1:-1] | ||
| 94 | + op_dict = {} | ||
| 95 | + tbe_ops_info[op_name] = op_dict | ||
| 96 | + find_op_type = True | ||
| 97 | + elif "=" in line: | ||
| 98 | + key1 = line[:line.index("=")] | ||
| 99 | + key2 = line[line.index("=")+1:] | ||
| 100 | + key1_0, key1_1 = key1.split(".") | ||
| 101 | + if key1_0 not in op_dict: | ||
| 102 | + op_dict[key1_0] = {} | ||
| 103 | + if key1_1 in op_dict.get(key1_0): | ||
| 104 | + raise RuntimeError("Op:" + op_name + " " + key1_0 + " " + | ||
| 105 | + key1_1 + " is repeated!") | ||
| 106 | + dic_key = op_dict.get(key1_0) | ||
| 107 | + dic_key[key1_1] = key2 | ||
| 108 | + else: | ||
| 109 | + continue | ||
| 110 | + if not find_op_type: | ||
| 111 | + raise RuntimeError("Not find OpType in .ini file.") | ||
| 112 | + | ||
| 113 | + | ||
| 114 | +def check_output_exist(op_dict, is_valid): | ||
| 115 | + """ | ||
| 116 | + Function Description: | ||
| 117 | + Check output is exist | ||
| 118 | + Parameter: op_dict | ||
| 119 | + Parameter: is_valid | ||
| 120 | + """ | ||
| 121 | + if "output0" in op_dict: | ||
| 122 | + output0_dict = op_dict.get("output0") | ||
| 123 | + if output0_dict.get("name", None) is None: | ||
| 124 | + is_valid = False | ||
| 125 | + print("output0.name is required in .ini file!") | ||
| 126 | + else: | ||
| 127 | + is_valid = False | ||
| 128 | + print("output0 is required in .ini file!") | ||
| 129 | + return is_valid | ||
| 130 | + | ||
| 131 | + | ||
| 132 | +def check_attr_dict(attr_dict, is_valid, attr): | ||
| 133 | + """ | ||
| 134 | + Function Description: | ||
| 135 | + Check attr_dict | ||
| 136 | + Parameter: attr_dict | ||
| 137 | + Parameter: is_valid | ||
| 138 | + Parameter: attr | ||
| 139 | + """ | ||
| 140 | + attr_type = attr_dict.get("type") | ||
| 141 | + value = attr_dict.get("value") | ||
| 142 | + param_type = attr_dict.get("paramType") | ||
| 143 | + if attr_type is None or value is None: | ||
| 144 | + is_valid = False | ||
| 145 | + print("If attr.list is exist, {0}.type and {0}.value is required".format(attr)) | ||
| 146 | + if param_type and param_type not in ATTR_PARAMTYPE_LIST: | ||
| 147 | + is_valid = False | ||
| 148 | + print("{0}.paramType only support {1}.".format(attr, ATTR_PARAMTYPE_LIST)) | ||
| 149 | + if attr_type and attr_type not in ATTR_TYPE_LIST: | ||
| 150 | + is_valid = False | ||
| 151 | + print("{0}.type only support {1}.".format(attr, ATTR_TYPE_LIST)) | ||
| 152 | + return is_valid | ||
| 153 | + | ||
| 154 | + | ||
| 155 | +def check_attr(op_dict, is_valid): | ||
| 156 | + """ | ||
| 157 | + Function Description: | ||
| 158 | + Check attr | ||
| 159 | + Parameter: op_dict | ||
| 160 | + Parameter: is_valid | ||
| 161 | + """ | ||
| 162 | + if "attr" in op_dict: | ||
| 163 | + attr_dict = op_dict.get("attr") | ||
| 164 | + attr_list_str = attr_dict.get("list", None) | ||
| 165 | + if attr_list_str is None: | ||
| 166 | + is_valid = False | ||
| 167 | + print("attr.list is required in .ini file!") | ||
| 168 | + else: | ||
| 169 | + attr_list = attr_list_str.split(",") | ||
| 170 | + for attr_name in attr_list: | ||
| 171 | + attr = "attr_" + attr_name.strip() | ||
| 172 | + attr_dict = op_dict.get(attr) | ||
| 173 | + if attr_dict: | ||
| 174 | + is_valid = check_attr_dict(attr_dict, is_valid, attr) | ||
| 175 | + else: | ||
| 176 | + is_valid = False | ||
| 177 | + print("%s is required in .ini file, when attr.list is %s!" % (attr, attr_list_str)) | ||
| 178 | + return is_valid | ||
| 179 | + | ||
| 180 | + | ||
| 181 | +def check_bool_flag(op_dict, is_valid): | ||
| 182 | + """ | ||
| 183 | + Function Description: | ||
| 184 | + check_bool_flag | ||
| 185 | + Parameter: op_dict | ||
| 186 | + Parameter: is_valid | ||
| 187 | + """ | ||
| 188 | + for key in BOOL_FLAG_KEY: | ||
| 189 | + if key in op_dict: | ||
| 190 | + op_bool_key = op_dict.get(key) | ||
| 191 | + if op_bool_key.get("flag").strip() not in BOOL_LIST: | ||
| 192 | + is_valid = False | ||
| 193 | + print("{0}.flag only support {1}.".format(key, BOOL_LIST)) | ||
| 194 | + return is_valid | ||
| 195 | + | ||
| 196 | + | ||
| 197 | +def check_type_format(op_info, is_valid, op_info_key): | ||
| 198 | + """ | ||
| 199 | + Function Description: | ||
| 200 | + Check type and format | ||
| 201 | + Parameter: op_info | ||
| 202 | + Parameter: is_valid | ||
| 203 | + Parameter: op_info_key | ||
| 204 | + """ | ||
| 205 | + op_info_dtype_str = op_info.get("dtype") | ||
| 206 | + op_info_dtype_num = 0 | ||
| 207 | + op_info_format_num = 0 | ||
| 208 | + if op_info_dtype_str: | ||
| 209 | + op_info_dtype = op_info_dtype_str.split(",") | ||
| 210 | + op_info_dtype_num = len(op_info_dtype) | ||
| 211 | + for dtype in op_info_dtype: | ||
| 212 | + if dtype.strip() not in DTYPE_LIST: | ||
| 213 | + is_valid = False | ||
| 214 | + print("{0}.dtype not support {1}.".format(op_info_key, dtype)) | ||
| 215 | + op_info_format_str = op_info.get("format") | ||
| 216 | + if op_info_format_str: | ||
| 217 | + op_info_format = op_info_format_str.split(",") | ||
| 218 | + op_info_format_num = len(op_info_format) | ||
| 219 | + for op_format in op_info_format: | ||
| 220 | + if op_format.strip() not in FORMAT_LIST: | ||
| 221 | + is_valid = False | ||
| 222 | + print("{0}.format not support {1}.".format(op_info_key, op_format)) | ||
| 223 | + if op_info_dtype_num > 0 and op_info_format_num > 0: | ||
| 224 | + if op_info_dtype_num != op_info_format_num: | ||
| 225 | + is_valid = False | ||
| 226 | + print("The number of {0}.dtype not match the number of {0}.format.".format(op_info_key)) | ||
| 227 | + return is_valid | ||
| 228 | + | ||
| 229 | + | ||
| 230 | +def check_op_info(tbe_ops): | ||
| 231 | + """ | ||
| 232 | + Function Description: | ||
| 233 | + Check info. | ||
| 234 | + Parameter: tbe_ops | ||
| 235 | + Return Value: is_valid | ||
| 236 | + """ | ||
| 237 | + print("\n\n==============check valid for ops info start==============") | ||
| 238 | + required_op_input_info_keys = ["paramType", "name"] | ||
| 239 | + required_op_output_info_keys = ["paramType", "name"] | ||
| 240 | + param_type_valid_value = ["dynamic", "optional", "required"] | ||
| 241 | + is_valid = True | ||
| 242 | + for op_key in tbe_ops: | ||
| 243 | + op_dict = tbe_ops[op_key] | ||
| 244 | + for op_info_key in op_dict: | ||
| 245 | + if op_info_key.startswith("input"): | ||
| 246 | + op_input_info = op_dict[op_info_key] | ||
| 247 | + missing_keys = [] | ||
| 248 | + for required_op_input_info_key in required_op_input_info_keys: | ||
| 249 | + if required_op_input_info_key not in op_input_info: | ||
| 250 | + missing_keys.append(required_op_input_info_key) | ||
| 251 | + if len(missing_keys) > 0: | ||
| 252 | + print("op: " + op_key + " " + op_info_key + " missing: " + | ||
| 253 | + ",".join(missing_keys)) | ||
| 254 | + is_valid = False | ||
| 255 | + else: | ||
| 256 | + if not op_input_info["paramType"] in param_type_valid_value: | ||
| 257 | + print("op: " + op_key + " " + op_info_key + \ | ||
| 258 | + " paramType not valid, valid key:[dynamic, " | ||
| 259 | + "optional, required]") | ||
| 260 | + is_valid = False | ||
| 261 | + is_valid = check_type_format(op_input_info, is_valid, op_info_key) | ||
| 262 | + if op_info_key.startswith("output"): | ||
| 263 | + op_input_info = op_dict[op_info_key] | ||
| 264 | + missing_keys = [] | ||
| 265 | + for required_op_input_info_key in required_op_output_info_keys: | ||
| 266 | + if required_op_input_info_key not in op_input_info: | ||
| 267 | + missing_keys.append(required_op_input_info_key) | ||
| 268 | + if len(missing_keys) > 0: | ||
| 269 | + print("op: " + op_key + " " + op_info_key + " missing: " + | ||
| 270 | + ",".join(missing_keys)) | ||
| 271 | + is_valid = False | ||
| 272 | + else: | ||
| 273 | + if not op_input_info["paramType"] in param_type_valid_value: | ||
| 274 | + print("op: " + op_key + " " + op_info_key + | ||
| 275 | + " paramType not valid, valid key:[dynamic, " | ||
| 276 | + "optional, required]") | ||
| 277 | + is_valid = False | ||
| 278 | + is_valid = check_type_format(op_input_info, is_valid, op_info_key) | ||
| 279 | + is_valid = check_attr(op_dict, is_valid) | ||
| 280 | + is_valid = check_bool_flag(op_dict, is_valid) | ||
| 281 | + print("==============check valid for ops info end================\n\n") | ||
| 282 | + return is_valid | ||
| 283 | + | ||
| 284 | + | ||
| 285 | +def write_json_file(tbe_ops_info, json_file_path): | ||
| 286 | + """ | ||
| 287 | + Save info to json file | ||
| 288 | + Parameters: | ||
| 289 | + ---------------- | ||
| 290 | + tbe_ops_info: ops_info | ||
| 291 | + json_file_path: json file path | ||
| 292 | + ---------------- | ||
| 293 | + """ | ||
| 294 | + json_file_real_path = os.path.realpath(json_file_path) | ||
| 295 | + wr_flag = os.O_WRONLY | os.O_CREAT | ||
| 296 | + wr_mode = stat.S_IWUSR | stat.S_IRUSR | ||
| 297 | + with os.fdopen(os.open(json_file_real_path, wr_flag, wr_mode), 'w') as file_path: | ||
| 298 | + # The owner have all rights£¬group only have read rights | ||
| 299 | + os.chmod(json_file_real_path, stat.S_IWUSR + stat.S_IRGRP | ||
| 300 | + + stat.S_IRUSR) | ||
| 301 | + json.dump(tbe_ops_info, file_path, sort_keys=True, indent=4, | ||
| 302 | + separators=(',', ':')) | ||
| 303 | + print("Compile op info cfg successfully.") | ||
| 304 | + | ||
| 305 | + | ||
| 306 | +def parse_ini_to_json(ini_file_paths, outfile_path): | ||
| 307 | + """ | ||
| 308 | + parse ini files to json file | ||
| 309 | + Parameters: | ||
| 310 | + ---------------- | ||
| 311 | + ini_file_paths: list of ini file path | ||
| 312 | + outfile_path: output file path | ||
| 313 | + ---------------- | ||
| 314 | + """ | ||
| 315 | + tbe_ops_info = parse_ini_files(ini_file_paths) | ||
| 316 | + if not check_op_info(tbe_ops_info): | ||
| 317 | + print("Compile op info cfg failed.") | ||
| 318 | + return False | ||
| 319 | + write_json_file(tbe_ops_info, outfile_path) | ||
| 320 | + return True | ||
| 321 | + | ||
| 322 | + | ||
| 323 | +if __name__ == '__main__': | ||
| 324 | + args = sys.argv | ||
| 325 | + | ||
| 326 | + OUTPUT_FILE_PATH = "tbe_ops_info.json" | ||
| 327 | + ini_file_path_list = [] | ||
| 328 | + parse_ini_list = [] | ||
| 329 | + | ||
| 330 | + for arg in args: | ||
| 331 | + if arg.endswith("ini"): | ||
| 332 | + ini_file_path_list.append(arg) | ||
| 333 | + OUTPUT_FILE_PATH = arg.replace(".ini", ".json") | ||
| 334 | + if arg.endswith("json"): | ||
| 335 | + OUTPUT_FILE_PATH = arg | ||
| 336 | + | ||
| 337 | + if not ini_file_path_list: | ||
| 338 | + ini_file_path_list.append("tbe_ops_info.ini") | ||
| 339 | + | ||
| 340 | + for ini_file in ini_file_path_list: | ||
| 341 | + if os.path.exists(ini_file): | ||
| 342 | + parse_ini_list.append(ini_file) | ||
| 343 | + | ||
| 344 | + if parse_ini_list: | ||
| 345 | + if not parse_ini_to_json(parse_ini_list, OUTPUT_FILE_PATH): | ||
| 346 | + sys.exit(1) | ||
| 347 | + sys.exit(0) | ||
| @@ -0,0 +1,35 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 4 | + | ||
| 5 | +import json | ||
| 6 | +import sys | ||
| 7 | +import os | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +def read_json(file): | ||
| 11 | + with open(file, 'r') as fd: | ||
| 12 | + config = json.load(fd) | ||
| 13 | + return config | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +def get_config_opts(file): | ||
| 17 | + config = read_json(file) | ||
| 18 | + | ||
| 19 | + src_dir = os.path.abspath(os.path.dirname(file)) | ||
| 20 | + opts = '' | ||
| 21 | + | ||
| 22 | + for conf in config: | ||
| 23 | + if conf == 'configurePresets': | ||
| 24 | + for node in config[conf]: | ||
| 25 | + macros = node.get('cacheVariables') | ||
| 26 | + if macros is not None: | ||
| 27 | + for key in macros: | ||
| 28 | + opts += '-D{}={} '.format(key, macros[key]['value']) | ||
| 29 | + | ||
| 30 | + opts = opts.replace('${sourceDir}', src_dir) | ||
| 31 | + print(opts) | ||
| 32 | + | ||
| 33 | + | ||
| 34 | +if __name__ == "__main__": | ||
| 35 | + get_config_opts(sys.argv[1]) | ||
| @@ -0,0 +1,105 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Created on Feb 28 20:56:45 2020 | ||
| 5 | +Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved. | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import os | ||
| 9 | +import stat | ||
| 10 | +import collections | ||
| 11 | +import kernel_entry as keb | ||
| 12 | +from tiling_data_def_build import gen_tiling | ||
| 13 | +import code_channel_infer | ||
| 14 | +import const_var | ||
| 15 | + | ||
| 16 | +PYF_PATH = os.path.dirname(__file__) | ||
| 17 | + | ||
| 18 | +ReplayCodeGenParams = collections.namedtuple('ReplayCodeGenParams',\ | ||
| 19 | +['op_type', 'impl', 'tiling_file', 'kernel', 'entry', 'argn', 'op_replay_batch', 'max_block_dim', 'max_shape_size']) | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +class ReplayCodeGen: | ||
| 23 | + def __init__(self, replayCodeGenParams): | ||
| 24 | + self.op_type = replayCodeGenParams.op_type | ||
| 25 | + self.impl = replayCodeGenParams.impl | ||
| 26 | + self.tiling_file = replayCodeGenParams.tiling_file | ||
| 27 | + self.tiling_data_file = '' | ||
| 28 | + self.kernel = replayCodeGenParams.kernel | ||
| 29 | + self.entry = replayCodeGenParams.entry | ||
| 30 | + self.argn = replayCodeGenParams.argn | ||
| 31 | + self.batch = False | ||
| 32 | + self.outdir = '' | ||
| 33 | + self.data_type = 'uint8_t' | ||
| 34 | + self.blknum = 32 | ||
| 35 | + self.op_replay_batch = replayCodeGenParams.op_replay_batch | ||
| 36 | + self.max_block_dim = replayCodeGenParams.max_block_dim | ||
| 37 | + self.max_shape_size = replayCodeGenParams.max_shape_size | ||
| 38 | + | ||
| 39 | + def set_batch(self, is_batch): | ||
| 40 | + self.batch = is_batch | ||
| 41 | + | ||
| 42 | + def set_outdir(self, outdir): | ||
| 43 | + self.outdir = outdir | ||
| 44 | + | ||
| 45 | + def gen_replay(self, ops_product: str): | ||
| 46 | + kerentry = os.path.join(self.outdir, self.kernel + '_entry.cce') | ||
| 47 | + kerimpl = os.path.join(self.outdir, self.kernel + '_impl.cpp') | ||
| 48 | + replayimpl = os.path.join(self.outdir, self.kernel + '_replay.cpp') | ||
| 49 | + if self.batch: | ||
| 50 | + reptmp = os.path.join(PYF_PATH, 'batch_replay_impl.temp') | ||
| 51 | + else: | ||
| 52 | + reptmp = os.path.join(PYF_PATH, 'replay_impl.temp') | ||
| 53 | + kertmp = os.path.join(PYF_PATH, 'kernel_impl.temp') | ||
| 54 | + self._gen_kentry(kerentry) | ||
| 55 | + self._gen_kimpl_code(kerimpl, kertmp) | ||
| 56 | + self._gen_tiling_data_header() | ||
| 57 | + self._gen_replay_code(replayimpl, reptmp, ops_product) | ||
| 58 | + | ||
| 59 | + def _gen_tiling_data_header(self): | ||
| 60 | + self.tiling_data_file = os.path.join(self.outdir, self.kernel + '_tiling_data.h') | ||
| 61 | + gen_tiling(self.tiling_file, self.tiling_data_file) | ||
| 62 | + | ||
| 63 | + def _gen_kimpl_code(self, src, tmpfile): | ||
| 64 | + with open(tmpfile, 'r') as fd: | ||
| 65 | + temp = fd.read() | ||
| 66 | + temp = temp.replace('__CCE_FILE__', self.impl) | ||
| 67 | + with os.fdopen(os.open(src, const_var.WFLAGS, const_var.WMODES), 'w') as ofd: | ||
| 68 | + ofd.write(temp) | ||
| 69 | + | ||
| 70 | + def _gen_replay_code(self, src, tmpfile, ops_product: str): | ||
| 71 | + with open(tmpfile, 'r') as fd: | ||
| 72 | + temp = fd.read() | ||
| 73 | + temp = temp.replace('__ARG_NUM__', str(self.argn)) | ||
| 74 | + argdef = [] | ||
| 75 | + kargs = [] | ||
| 76 | + for i in range(0, self.argn): | ||
| 77 | + argdef.append('{} *'.format(self.data_type)) | ||
| 78 | + kargs.append('({} *)GetArg({})'.format(self.data_type, i)) | ||
| 79 | + temp = temp.replace('__ARGS_DEF__', ', '.join(argdef)) | ||
| 80 | + temp = temp.replace('__KERNEL_ARGS__', ', '.join(kargs)) | ||
| 81 | + temp = temp.replace('__KERNEL_FUN__', self.entry) | ||
| 82 | + core_type_infer = 'core_type' | ||
| 83 | + code_channel = code_channel_infer.infer_code_channel(code_channel_infer.InfoCodeChanelParams(self.impl,\ | ||
| 84 | + self.tiling_data_file, self.kernel, self.outdir, ops_product, None)) | ||
| 85 | + if code_channel == code_channel_infer.CODE_VEC: | ||
| 86 | + core_type_infer = '0' | ||
| 87 | + elif code_channel == code_channel_infer.CODE_CUBE: | ||
| 88 | + core_type_infer = '1' | ||
| 89 | + temp = temp.replace('__CORE_TYPE__', core_type_infer) | ||
| 90 | + # regist function | ||
| 91 | + temp = temp.replace('__OPS_PRODUCT__', ops_product) | ||
| 92 | + temp = temp.replace('__OPTYPE__', self.op_type) | ||
| 93 | + with os.fdopen(os.open(src, const_var.WFLAGS, const_var.WMODES), 'w') as ofd: | ||
| 94 | + ofd.write(temp) | ||
| 95 | + | ||
| 96 | + def _gen_kentry(self, src): | ||
| 97 | + kf = '' | ||
| 98 | + pre_alloc_str = 'A' * 256 | ||
| 99 | + if self.batch: | ||
| 100 | + kf += keb.batch_code_gen("K{:02d}_{}{}".format(0, self.entry, pre_alloc_str), self.argn, self.data_type) | ||
| 101 | + else: | ||
| 102 | + kf += keb.mc_code_gen("K{:02d}_{}{}".format(0, self.entry, pre_alloc_str),\ | ||
| 103 | + self.argn, self.data_type, self.blknum) | ||
| 104 | + with os.fdopen(os.open(src, const_var.WFLAGS, const_var.WMODES), 'w') as ofd: | ||
| 105 | + ofd.write(kf) | ||
| @@ -0,0 +1,120 @@ | |||
| 1 | +#include <sys/types.h> | ||
| 2 | +#include <sys/stat.h> | ||
| 3 | +#include <fcntl.h> | ||
| 4 | +#include <unistd.h> | ||
| 5 | +#include <iostream> | ||
| 6 | +#include <thread> | ||
| 7 | +#include "replay_def.h" | ||
| 8 | +#include "code_gen.h" | ||
| 9 | +#include "replay_fun.h" | ||
| 10 | +#include "register/op_check.h" | ||
| 11 | +#define __ASCENDC_REPLAY_CODE__ | ||
| 12 | +using namespace std; | ||
| 13 | +using namespace optiling; | ||
| 14 | +using namespace AscendCReplay; | ||
| 15 | + | ||
| 16 | +extern "C" void __KERNEL_FUN__ (__ARGS_DEF__, const char *); | ||
| 17 | +extern "C" int elf_append(char *elf, uint32_t elfSize, char *jit, int kernum, int blknum[], char *atext[], | ||
| 18 | + int alen[], int atlen, const char* kernelname[]); | ||
| 19 | + | ||
| 20 | +#define KERNEL_N 1 | ||
| 21 | +#define ARG_N (__ARG_NUM__) | ||
| 22 | +#define MAX_L (1024 * 1024 * 100) | ||
| 23 | +#define MAX_E (1024 * 1024) | ||
| 24 | + | ||
| 25 | +int __KERNEL_FUN___replay___OPS_PRODUCT__(ReplayFuncParam& param, const int core_type) | ||
| 26 | +{ | ||
| 27 | + // gen type 1 : direct call codes 0: load .o file | ||
| 28 | + if (param.gentype < 0 || param.gentype > 1) { | ||
| 29 | + printf("Error: call replay gen type is %d, should only be 1 or 0\n", param.gentype); | ||
| 30 | + return 0; | ||
| 31 | + } else if (param.gentype == 1 && param.objptr == nullptr) { | ||
| 32 | + printf("Error: call replay with direct call mode, but code obj addr is null\n"); | ||
| 33 | + return 0; | ||
| 34 | + } else if (param.gentype == 0 && param.output_kernel_file == nullptr) { | ||
| 35 | + printf("Error: call replay with object file mode, but object file path is null\n"); | ||
| 36 | + return 0; | ||
| 37 | + } | ||
| 38 | + // core_type 0:MIX 1:CUBE 2:VEC | ||
| 39 | + if (core_type < 0 || core_type > 2) { | ||
| 40 | + printf("Error: call replay core type is %d !\n", core_type); | ||
| 41 | + return 0; | ||
| 42 | + } | ||
| 43 | + g_coreType = __CORE_TYPE__; | ||
| 44 | + g_taskRation = param.task_ration; | ||
| 45 | + g_tilingKey = param.tiling_key; | ||
| 46 | + | ||
| 47 | + unsigned char *buf, *jit; | ||
| 48 | + char *kernel[KERNEL_N * 32]; | ||
| 49 | + int len[KERNEL_N * 32]; | ||
| 50 | + int blknum[KERNEL_N]; | ||
| 51 | + int max; | ||
| 52 | + block_num = param.block_dim; | ||
| 53 | + g_ubBase = block_num; | ||
| 54 | + uint8_t *code = (uint8_t *)malloc(MAX_L); | ||
| 55 | + uint8_t *pos = code; | ||
| 56 | + struct timespec tp1, tp2; | ||
| 57 | + | ||
| 58 | + clock_gettime(CLOCK_MONOTONIC, &tp1); | ||
| 59 | + if (block_num > 32) { | ||
| 60 | + printf("Error: block_num > 32\n"); | ||
| 61 | + return 0; | ||
| 62 | + } | ||
| 63 | + //__OP_FOPEN__ | ||
| 64 | + for (int i = 0; i < KERNEL_N; i++) { | ||
| 65 | + for (int j = 0; j < ARG_N; j++) | ||
| 66 | + AddArg(j, ARG_STEP * (j + 1)); | ||
| 67 | + for (block_idx = 0; block_idx < block_num; block_idx++) { | ||
| 68 | + //__OP_SET_KERNEL__ | ||
| 69 | + int code_idx = i * block_num + block_idx; | ||
| 70 | +#ifdef FP_CEILING | ||
| 71 | + SetCtrlFloatEnable(); | ||
| 72 | +#else | ||
| 73 | + SetCtrlFloatDisable(); | ||
| 74 | +#endif | ||
| 75 | + CodeInit(pos, false); | ||
| 76 | + __KERNEL_FUN__(__KERNEL_ARGS__, param.tiling_data); | ||
| 77 | + CodeEnd(); | ||
| 78 | + kernel[code_idx] = (char *)pos; | ||
| 79 | + len[code_idx] = CodeLen(); | ||
| 80 | + pos += len[code_idx]; | ||
| 81 | + printf("kernel %d core %ld code generated len %d\n", i, block_idx, len[code_idx]); | ||
| 82 | + } | ||
| 83 | + blknum[i] = block_num; | ||
| 84 | + } | ||
| 85 | + //__OP_FCLOSE__ | ||
| 86 | + clock_gettime(CLOCK_MONOTONIC, &tp2); | ||
| 87 | + buf = (unsigned char *)malloc(MAX_E); | ||
| 88 | + int fd = open(param.entry_file, O_RDONLY); | ||
| 89 | + if (fd < 0) { | ||
| 90 | + printf("[error]: cannot find entry.o : %s\n", param.entry_file); | ||
| 91 | + return 0; | ||
| 92 | + } | ||
| 93 | + uint32_t bufSize = read(fd, buf, MAX_E); | ||
| 94 | + if (bufSize <= 0) { | ||
| 95 | + printf("[error]: entry.o : %s is too small ! \n", param.entry_file); | ||
| 96 | + } | ||
| 97 | + close(fd); | ||
| 98 | + jit = (unsigned char *)malloc(MAX_L); | ||
| 99 | + printf("total code generated %ld\n", pos - code); | ||
| 100 | + int sz = elf_append((char *)buf, bufSize, (char *)jit, KERNEL_N, blknum, kernel, len, pos - code, ¶m.kernel_name); | ||
| 101 | + if (tp1.tv_sec != tp2.tv_sec) { | ||
| 102 | + printf("%ld NS\n", tp2.tv_nsec + 1000000000 - tp1.tv_nsec); | ||
| 103 | + } else { | ||
| 104 | + printf("%ld NS\n", tp2.tv_nsec - tp1.tv_nsec); | ||
| 105 | + } | ||
| 106 | + printf("new elf size %d\n", sz); | ||
| 107 | + if (param.gentype == 0) { | ||
| 108 | + fd = open(param.output_kernel_file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); | ||
| 109 | + (void)write(fd, jit, sz); | ||
| 110 | + close(fd); | ||
| 111 | + free(jit); | ||
| 112 | + } else if (param.gentype == 1) { | ||
| 113 | + *param.objptr = (char*)jit; | ||
| 114 | + } | ||
| 115 | + free(buf); | ||
| 116 | + free(code); | ||
| 117 | + return sz; | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +REG_REPLAY_FUNC(__OPTYPE__, __OPS_PRODUCT__, __KERNEL_FUN___replay___OPS_PRODUCT__); | ||
| @@ -0,0 +1,88 @@ | |||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +""" | ||
| 4 | +Function: | ||
| 5 | +The replay funtion entry | ||
| 6 | +Copyright Information: | ||
| 7 | +Huawei Technologies Co., Ltd. All Rights Reserved © 2020 | ||
| 8 | +""" | ||
| 9 | + | ||
| 10 | +import sys | ||
| 11 | +import os | ||
| 12 | +import stat | ||
| 13 | +import re | ||
| 14 | +import const_var | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +def gen_tiling(tiling_header_file: str, tiling_file_out: str): | ||
| 18 | + if not os.path.exists(tiling_header_file): | ||
| 19 | + print("warning: no userdef tiling header file: ", tiling_header_file) | ||
| 20 | + return | ||
| 21 | + print("generate tiling def header file: ", tiling_file_out) | ||
| 22 | + tmp_name = os.path.splitext(os.path.basename(tiling_header_file))[0].upper() | ||
| 23 | + tiling_source = '#ifndef __{}_H__\n'.format(tmp_name) | ||
| 24 | + tiling_source += '#define __{}_H__\n\n'.format(tmp_name) | ||
| 25 | + tiling_source += '#include <cstdint>\n' | ||
| 26 | + tiling_source += '#include <cstring>\n\n' | ||
| 27 | + tiling_source += '#include "kernel_tiling/kernel_tiling.h"\n\n' | ||
| 28 | + end_source = "" | ||
| 29 | + pattern = re.compile(r'[(](.*)[)]', re.S) | ||
| 30 | + with open(tiling_header_file, 'r') as fd: | ||
| 31 | + lines = fd.readlines() | ||
| 32 | + for line in lines: | ||
| 33 | + line = line.strip() | ||
| 34 | + if (line.startswith('BEGIN_TILING_DATA_DEF')): | ||
| 35 | + tiling_source += '#pragma pack(1)\n' | ||
| 36 | + tiling_source += 'struct ' | ||
| 37 | + struct_def = re.findall(pattern, line)[0] | ||
| 38 | + tiling_source += struct_def + ' {\n' | ||
| 39 | + elif (line.startswith('TILING_DATA_FIELD_DEF_ARR')): | ||
| 40 | + field_params = re.findall(pattern, line)[0] | ||
| 41 | + fds = field_params.split(',') | ||
| 42 | + tiling_source += ' {} {}[{}] = {{}};\n'.format(fds[0].strip(), fds[2].strip(), fds[1].strip()) | ||
| 43 | + elif (line.startswith('TILING_DATA_FIELD_DEF_STRUCT')): | ||
| 44 | + field_params = re.findall(pattern, line)[0] | ||
| 45 | + fds = field_params.split(',') | ||
| 46 | + tiling_source += ' {} {};\n'.format(fds[0].strip(), fds[1].strip()) | ||
| 47 | + elif (line.startswith('TILING_DATA_FIELD_DEF')): | ||
| 48 | + field_params = re.findall(pattern, line)[0] | ||
| 49 | + fds = field_params.split(',') | ||
| 50 | + tiling_source += ' {} {} = 0;\n'.format(fds[0].strip(), fds[1].strip()) | ||
| 51 | + elif (line.startswith('END_TILING_DATA_DEF')): | ||
| 52 | + tiling_source += '};\n' | ||
| 53 | + tiling_source += '#pragma pack()\n\n' | ||
| 54 | + tiling_source += '#ifdef __NPU_TILING__\n' | ||
| 55 | + tiling_source += \ | ||
| 56 | + 'inline [aicore] void Init{stru}(const __gm__ uint8_t* tiling, {stru}* const_data)\n'\ | ||
| 57 | + .format(stru=struct_def) | ||
| 58 | + tiling_source += '{\n' | ||
| 59 | + tiling_source += ' const __gm__ uint32_t *src = (const __gm__ uint32_t *)tiling;\n' | ||
| 60 | + tiling_source += ' uint32_t *dst = (uint32_t *)const_data;\n' | ||
| 61 | + tiling_source += ' for (auto i = 0; i < sizeof({}) / 4; i++) *(dst + i) = *(src + i);\n'\ | ||
| 62 | + .format(struct_def) | ||
| 63 | + tiling_source += '}\n' | ||
| 64 | + tiling_source += '#else\n' | ||
| 65 | + tiling_source += 'inline void Init{stru}(uint8_t* tiling, {stru}* const_data)\n'.format(stru=struct_def) | ||
| 66 | + tiling_source += '{\n' | ||
| 67 | + tiling_source += ' uint64_t *src = (uint64_t *)tiling;\n' | ||
| 68 | + tiling_source += ' uint64_t *dst = (uint64_t *)const_data;\n' | ||
| 69 | + tiling_source += ' for (auto i = 0; i < sizeof({}) / 8; i++) *(dst + i) = *(src + i);\n'\ | ||
| 70 | + .format(struct_def) | ||
| 71 | + tiling_source += '}\n' | ||
| 72 | + tiling_source += '#endif\n\n' | ||
| 73 | + end_source = ''' | ||
| 74 | +#undef GET_TILING_DATA | ||
| 75 | +#define GET_TILING_DATA(tiling_data, tiling_arg) \\ | ||
| 76 | +{stru} tiling_data; \\ | ||
| 77 | +Init{stru}(tiling_arg, &tiling_data)\n | ||
| 78 | +'''.format(stru=struct_def) | ||
| 79 | + tiling_source += end_source | ||
| 80 | + tiling_source += '#endif' | ||
| 81 | + with os.fdopen(os.open(tiling_file_out, const_var.WFLAGS, const_var.WMODES), 'w') as ofd: | ||
| 82 | + ofd.write(tiling_source) | ||
| 83 | + | ||
| 84 | + | ||
| 85 | +if __name__ == '__main__': | ||
| 86 | + if len(sys.argv) <= 2: | ||
| 87 | + raise RuntimeError('arguments must greater than 2') | ||
| 88 | + gen_tiling(sys.argv[1], sys.argv[2]) | ||
| @@ -0,0 +1,11 @@ | |||
| 1 | +if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/mindspore") | ||
| 2 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/caffe_plugin") | ||
| 3 | + add_subdirectory(caffe_plugin) | ||
| 4 | + endif() | ||
| 5 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tf_plugin") | ||
| 6 | + add_subdirectory(tf_plugin) | ||
| 7 | + endif() | ||
| 8 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/onnx_plugin") | ||
| 9 | + add_subdirectory(onnx_plugin) | ||
| 10 | + endif() | ||
| 11 | +endif() | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | + | ||
| 2 | +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} plugin_srcs) | ||
| 3 | +add_library(cust_tf_parsers SHARED ${plugin_srcs}) | ||
| 4 | +target_compile_definitions(cust_tf_parsers PRIVATE google=ascend_private) | ||
| 5 | +if(ENABLE_CROSS_COMPILE) | ||
| 6 | + target_link_directories(cust_tf_parsers PRIVATE | ||
| 7 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 8 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 9 | + ) | ||
| 10 | +endif() | ||
| 11 | +target_link_libraries(cust_tf_parsers PRIVATE intf_pub graph) | ||
| 12 | +install(TARGETS cust_tf_parsers | ||
| 13 | + LIBRARY DESTINATION packages/vendors/${vendor_name}/framework/tensorflow | ||
| 14 | +) | ||
| @@ -0,0 +1,23 @@ | |||
| 1 | +/* Copyright (C) 2020-2021. Huawei Technologies Co., Ltd. All | ||
| 2 | +rights reserved. | ||
| 3 | + * | ||
| 4 | + * This program is free software; you can redistribute it and/or modify | ||
| 5 | + * it under the terms of the Apache License Version 2.0. | ||
| 6 | + * You may not use this file except in compliance with the License. | ||
| 7 | + * | ||
| 8 | + * This program is distributed in the hope that it will be useful, | ||
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | + * Apache License for more details at | ||
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 13 | + */ | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +namespace domi { | ||
| 18 | +// register op info to GE | ||
| 19 | +REGISTER_CUSTOM_OP("DivCustomTemplate") | ||
| 20 | + .FrameworkType(TENSORFLOW) // type: CAFFE, TENSORFLOW | ||
| 21 | + .OriginOpType("DivCustomTemplate") // name in tf module | ||
| 22 | + .ParseParamsByOperatorFn(AutoMappingByOpFn); | ||
| 23 | +} // namespace domi | ||
| @@ -0,0 +1,221 @@ | |||
| 1 | + | ||
| 2 | +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} ops_srcs) | ||
| 3 | + | ||
| 4 | +opbuild(OPS_SRC ${ops_srcs} | ||
| 5 | + OUT_DIR ${ASCEND_AUTOGEN_PATH} | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +file(GLOB group_proto_src ${ASCEND_AUTOGEN_PATH}/group_proto/*.cc) | ||
| 9 | + | ||
| 10 | +add_library(cust_op_proto SHARED | ||
| 11 | + ${group_proto_src} | ||
| 12 | + ${ops_srcs} | ||
| 13 | + ${ASCEND_AUTOGEN_PATH}/op_proto.cc | ||
| 14 | +) | ||
| 15 | +target_compile_definitions(cust_op_proto PRIVATE OP_PROTO_LIB) | ||
| 16 | +target_compile_options(cust_op_proto PRIVATE | ||
| 17 | + -fvisibility=hidden | ||
| 18 | +) | ||
| 19 | +if(ENABLE_CROSS_COMPILE) | ||
| 20 | + target_link_directories(cust_op_proto PRIVATE | ||
| 21 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 22 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 23 | + ) | ||
| 24 | +endif() | ||
| 25 | +target_link_libraries(cust_op_proto PRIVATE | ||
| 26 | + intf_pub | ||
| 27 | + exe_graph | ||
| 28 | + register | ||
| 29 | + tiling_api | ||
| 30 | + -Wl,--whole-archive | ||
| 31 | + rt2_registry | ||
| 32 | + -Wl,--no-whole-archive | ||
| 33 | +) | ||
| 34 | +set_target_properties(cust_op_proto PROPERTIES OUTPUT_NAME | ||
| 35 | + cust_opsproto_rt2.0 | ||
| 36 | +) | ||
| 37 | +file(GLOB fallback_src ${ASCEND_AUTOGEN_PATH}/fallback_*.cpp) | ||
| 38 | +add_library(cust_optiling SHARED ${ops_srcs}) | ||
| 39 | +list(LENGTH fallback_src fallback_src_len) | ||
| 40 | +if (fallback_src_len GREATER 0) | ||
| 41 | + target_sources(cust_optiling PRIVATE ${fallback_src}) | ||
| 42 | +endif() | ||
| 43 | +target_compile_definitions(cust_optiling PRIVATE OP_TILING_LIB) | ||
| 44 | +target_compile_options(cust_optiling PRIVATE | ||
| 45 | + -fvisibility=hidden | ||
| 46 | +) | ||
| 47 | +if(ENABLE_CROSS_COMPILE) | ||
| 48 | + target_link_directories(cust_optiling PRIVATE | ||
| 49 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 50 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 51 | + ) | ||
| 52 | +endif() | ||
| 53 | +target_link_libraries(cust_optiling PRIVATE | ||
| 54 | + nnopbase | ||
| 55 | + intf_pub | ||
| 56 | + exe_graph | ||
| 57 | + register | ||
| 58 | + tiling_api | ||
| 59 | + -Wl,--whole-archive | ||
| 60 | + rt2_registry | ||
| 61 | + -Wl,--no-whole-archive | ||
| 62 | +) | ||
| 63 | +set_target_properties(cust_optiling PROPERTIES OUTPUT_NAME | ||
| 64 | + cust_opmaster_rt2.0 | ||
| 65 | +) | ||
| 66 | + | ||
| 67 | +file(GLOB aclnn_src ${ASCEND_AUTOGEN_PATH}/aclnn_*.cpp) | ||
| 68 | +file(GLOB aclnn_inc ${ASCEND_AUTOGEN_PATH}/aclnn_*.h) | ||
| 69 | +if(NOT ASCEND_PACK_SHARED_LIBRARY) | ||
| 70 | + add_library(cust_opapi SHARED ${aclnn_src}) | ||
| 71 | +else() | ||
| 72 | + set(GENERATED_SOURCE ${ASCEND_AUTOGEN_PATH}/custom_op_registry_V2.cpp) | ||
| 73 | + set_source_files_properties(${GENERATED_SOURCE} | ||
| 74 | + PROPERTIES | ||
| 75 | + GENERATED TRUE | ||
| 76 | + ) | ||
| 77 | + add_library(cust_opapi_obj OBJECT ${aclnn_src} ${GENERATED_SOURCE}) | ||
| 78 | + target_compile_options(cust_opapi_obj PRIVATE -DLOG_CPP) | ||
| 79 | + target_compile_definitions(cust_opapi_obj PRIVATE ACLNN_WITH_BINARY) | ||
| 80 | + if(ENABLE_CROSS_COMPILE) | ||
| 81 | + target_link_directories(cust_opapi_obj PRIVATE | ||
| 82 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 83 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 84 | + ) | ||
| 85 | + endif() | ||
| 86 | + target_link_libraries(cust_opapi_obj PRIVATE | ||
| 87 | + intf_pub | ||
| 88 | + exe_graph | ||
| 89 | + register | ||
| 90 | + tiling_api | ||
| 91 | + -Wl,--whole-archive | ||
| 92 | + rt2_registry | ||
| 93 | + -Wl,--no-whole-archive | ||
| 94 | + ascend_opregistry | ||
| 95 | + ascend_kernels | ||
| 96 | + ) | ||
| 97 | + target_compile_options(cust_opapi_obj PRIVATE | ||
| 98 | + -fvisibility=hidden | ||
| 99 | + ) | ||
| 100 | + add_dependencies(cust_opapi_obj ascend_opregistry) | ||
| 101 | + add_library(cust_op_proto_obj OBJECT | ||
| 102 | + ${group_proto_src} | ||
| 103 | + ${ops_srcs} | ||
| 104 | + ${ASCEND_AUTOGEN_PATH}/op_proto.cc | ||
| 105 | + ) | ||
| 106 | + target_compile_definitions(cust_op_proto_obj PRIVATE OP_PROTO_LIB) | ||
| 107 | + target_compile_options(cust_op_proto_obj PRIVATE | ||
| 108 | + -fvisibility=hidden | ||
| 109 | + ) | ||
| 110 | + if(ENABLE_CROSS_COMPILE) | ||
| 111 | + target_link_directories(cust_op_proto_obj PRIVATE | ||
| 112 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 113 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 114 | + ) | ||
| 115 | + endif() | ||
| 116 | + target_link_libraries(cust_op_proto_obj PRIVATE | ||
| 117 | + intf_pub | ||
| 118 | + exe_graph | ||
| 119 | + register | ||
| 120 | + tiling_api | ||
| 121 | + -Wl,--whole-archive | ||
| 122 | + rt2_registry | ||
| 123 | + -Wl,--no-whole-archive | ||
| 124 | + ) | ||
| 125 | + add_library(cust_optiling_obj OBJECT ${ops_srcs}) | ||
| 126 | + target_compile_definitions(cust_optiling_obj PRIVATE OP_TILING_LIB) | ||
| 127 | + target_compile_options(cust_optiling_obj PRIVATE | ||
| 128 | + -fvisibility=hidden | ||
| 129 | + ) | ||
| 130 | + if(ENABLE_CROSS_COMPILE) | ||
| 131 | + target_link_directories(cust_optiling_obj PRIVATE | ||
| 132 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 133 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 134 | + ) | ||
| 135 | + endif() | ||
| 136 | + target_link_libraries(cust_optiling_obj PRIVATE | ||
| 137 | + intf_pub | ||
| 138 | + exe_graph | ||
| 139 | + register | ||
| 140 | + tiling_api | ||
| 141 | + -Wl,--whole-archive | ||
| 142 | + rt2_registry | ||
| 143 | + -Wl,--no-whole-archive | ||
| 144 | + ) | ||
| 145 | + set(DYNAMIC_LIB_NAME "cust_opapi") | ||
| 146 | + add_library(${DYNAMIC_LIB_NAME} SHARED | ||
| 147 | + $<TARGET_OBJECTS:cust_op_proto_obj> | ||
| 148 | + $<TARGET_OBJECTS:cust_optiling_obj> | ||
| 149 | + $<TARGET_OBJECTS:cust_opapi_obj> | ||
| 150 | + ) | ||
| 151 | +endif() | ||
| 152 | +if(ENABLE_CROSS_COMPILE) | ||
| 153 | + target_link_directories(cust_opapi PRIVATE | ||
| 154 | + ${CMAKE_COMPILE_COMPILER_LIBRARY} | ||
| 155 | + ${CMAKE_COMPILE_RUNTIME_LIBRARY} | ||
| 156 | + ) | ||
| 157 | +endif() | ||
| 158 | +if(NOT ASCEND_PACK_SHARED_LIBRARY) | ||
| 159 | + target_link_libraries(cust_opapi PRIVATE intf_pub ascendcl nnopbase) | ||
| 160 | +else() | ||
| 161 | + target_link_libraries(${DYNAMIC_LIB_NAME} PRIVATE | ||
| 162 | + intf_pub | ||
| 163 | + exe_graph | ||
| 164 | + register | ||
| 165 | + tiling_api | ||
| 166 | + -Wl,--whole-archive | ||
| 167 | + rt2_registry | ||
| 168 | + -Wl,--no-whole-archive | ||
| 169 | + ) | ||
| 170 | + target_link_libraries(${DYNAMIC_LIB_NAME} PRIVATE intf_pub ascendcl nnopbase ascend_opregistry ascend_kernels) | ||
| 171 | + add_dependencies(${DYNAMIC_LIB_NAME} ascend_opregistry ascend_kernels) | ||
| 172 | + | ||
| 173 | + set(STATIC_LIB_NAME ${vendor_name}) | ||
| 174 | + add_static_library(${STATIC_LIB_NAME} cust_opapi_obj cust_op_proto_obj cust_optiling_obj) | ||
| 175 | + add_dependencies(${STATIC_LIB_NAME} ascend_opregistry ascend_kernels) | ||
| 176 | +endif() | ||
| 177 | + | ||
| 178 | +add_custom_target(optiling_compat ALL | ||
| 179 | + COMMAND ln -sf lib/linux/${CMAKE_SYSTEM_PROCESSOR}/$<TARGET_FILE_NAME:cust_optiling> | ||
| 180 | + ${CMAKE_CURRENT_BINARY_DIR}/liboptiling.so | ||
| 181 | +) | ||
| 182 | + | ||
| 183 | +if (ASCEND_PACK_SHARED_LIBRARY) | ||
| 184 | + file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/cmake/${vendor_name}) | ||
| 185 | + file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/cmake/${vendor_name}) | ||
| 186 | + add_vendor_cmake(${vendor_name} ${CMAKE_BINARY_DIR}/cmake/${vendor_name} ${STATIC_LIB_NAME} ${DYNAMIC_LIB_NAME}) | ||
| 187 | +endif() | ||
| 188 | + | ||
| 189 | +if(NOT ASCEND_PACK_SHARED_LIBRARY) | ||
| 190 | + install(TARGETS cust_op_proto | ||
| 191 | + LIBRARY DESTINATION packages/vendors/${vendor_name}/op_proto/lib/linux/${CMAKE_SYSTEM_PROCESSOR}) | ||
| 192 | + install(FILES ${ASCEND_AUTOGEN_PATH}/op_proto.h | ||
| 193 | + DESTINATION packages/vendors/${vendor_name}/op_proto/inc) | ||
| 194 | + file(GLOB GROUP_PROTO_HEADERS ${ASCEND_AUTOGEN_PATH}/group_proto/*.h) | ||
| 195 | + if (GROUP_PROTO_HEADERS) | ||
| 196 | + install(FILES ${GROUP_PROTO_HEADERS} | ||
| 197 | + DESTINATION packages/vendors/${vendor_name}/op_proto/inc) | ||
| 198 | + endif() | ||
| 199 | + install(TARGETS cust_optiling | ||
| 200 | + LIBRARY DESTINATION packages/vendors/${vendor_name}/op_impl/ai_core/tbe/op_tiling/lib/linux/${CMAKE_SYSTEM_PROCESSOR}) | ||
| 201 | + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/liboptiling.so | ||
| 202 | + DESTINATION packages/vendors/${vendor_name}/op_impl/ai_core/tbe/op_tiling) | ||
| 203 | + install(TARGETS cust_opapi | ||
| 204 | + LIBRARY DESTINATION packages/vendors/${vendor_name}/op_api/lib) | ||
| 205 | + install(FILES ${aclnn_inc} | ||
| 206 | + DESTINATION packages/vendors/${vendor_name}/op_api/include) | ||
| 207 | +else() | ||
| 208 | + file(GLOB group_inc ${ASCEND_AUTOGEN_PATH}/group_proto/*.h) | ||
| 209 | + install(TARGETS cust_opapi | ||
| 210 | + LIBRARY DESTINATION op_api/lib) | ||
| 211 | + install(FILES ${CMAKE_BINARY_DIR}/op_host/lib${STATIC_LIB_NAME}.a | ||
| 212 | + DESTINATION op_api/lib) | ||
| 213 | + install(DIRECTORY ${CMAKE_BINARY_DIR}/cmake | ||
| 214 | + DESTINATION op_api/lib) | ||
| 215 | + install(FILES ${ASCEND_AUTOGEN_PATH}/op_proto.h | ||
| 216 | + DESTINATION op_api/include) | ||
| 217 | + install(FILES ${group_inc} | ||
| 218 | + DESTINATION op_api/include) | ||
| 219 | + install(FILES ${aclnn_inc} | ||
| 220 | + DESTINATION op_api/include) | ||
| 221 | +endif() | ||
| @@ -0,0 +1,83 @@ | |||
| 1 | + | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +namespace optiling { | ||
| 5 | +static ge::graphStatus TilingFunc(gert::TilingContext* context) | ||
| 6 | +{ | ||
| 7 | + DivCustomTemplateTilingData tiling; | ||
| 8 | + | ||
| 9 | + // 1. 获取输入总元素个数 | ||
| 10 | + uint32_t totalLength = context->GetInputShape(0)->GetStorageShape().GetShapeSize(); | ||
| 11 | + tiling.set_totalLength(totalLength); | ||
| 12 | + | ||
| 13 | + // 2. 切分参数:8 核并行,每核按 1024 元素分 tile | ||
| 14 | + constexpr uint32_t BLOCK_DIM = 8; | ||
| 15 | + constexpr uint32_t TILE_LENGTH = 1024; | ||
| 16 | + tiling.set_tileLength(TILE_LENGTH); | ||
| 17 | + | ||
| 18 | + // 3. 记录数据类型,供 kernel 侧选择模板实例 | ||
| 19 | + auto dtype = context->GetInputDesc(0)->GetDataType(); | ||
| 20 | + tiling.set_dataType(dtype == ge::DT_FLOAT ? 1 : 0); | ||
| 21 | + | ||
| 22 | + // 4. 设置使用的 AI Core 数量 | ||
| 23 | + context->SetBlockDim(BLOCK_DIM); | ||
| 24 | + | ||
| 25 | + // 5. 序列化 tiling 数据供 kernel 读取 | ||
| 26 | + tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), | ||
| 27 | + context->GetRawTilingData()->GetCapacity()); | ||
| 28 | + context->GetRawTilingData()->SetDataSize(tiling.GetDataSize()); | ||
| 29 | + | ||
| 30 | + // 6. 本算子无需额外 workspace | ||
| 31 | + size_t* workspaces = context->GetWorkspaceSizes(1); | ||
| 32 | + workspaces[0] = 0; | ||
| 33 | + | ||
| 34 | + return ge::GRAPH_SUCCESS; | ||
| 35 | +} | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +namespace ge { | ||
| 39 | +static ge::graphStatus InferShape(gert::InferShapeContext* context) | ||
| 40 | +{ | ||
| 41 | + const gert::Shape* x1_shape = context->GetInputShape(0); | ||
| 42 | + gert::Shape* y_shape = context->GetOutputShape(0); | ||
| 43 | + *y_shape = *x1_shape; | ||
| 44 | + return GRAPH_SUCCESS; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +static ge::graphStatus InferDataType(gert::InferDataTypeContext* context) | ||
| 48 | +{ | ||
| 49 | + const auto inputDataType = context->GetInputDataType(0); | ||
| 50 | + context->SetOutputDataType(0, inputDataType); | ||
| 51 | + return ge::GRAPH_SUCCESS; | ||
| 52 | +} | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +namespace ops { | ||
| 56 | +class DivCustomTemplate : public OpDef { | ||
| 57 | +public: | ||
| 58 | + explicit DivCustomTemplate(const char* name) : OpDef(name) | ||
| 59 | + { | ||
| 60 | + this->Input("x") | ||
| 61 | + .ParamType(REQUIRED) | ||
| 62 | + .DataType({ge::DT_FLOAT16, ge::DT_FLOAT}) | ||
| 63 | + .Format({ge::FORMAT_ND, ge::FORMAT_ND}) | ||
| 64 | + .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND}); | ||
| 65 | + this->Input("y") | ||
| 66 | + .ParamType(REQUIRED) | ||
| 67 | + .DataType({ge::DT_FLOAT16, ge::DT_FLOAT}) | ||
| 68 | + .Format({ge::FORMAT_ND, ge::FORMAT_ND}) | ||
| 69 | + .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND}); | ||
| 70 | + this->Output("z") | ||
| 71 | + .ParamType(REQUIRED) | ||
| 72 | + .DataType({ge::DT_FLOAT16, ge::DT_FLOAT}) | ||
| 73 | + .Format({ge::FORMAT_ND, ge::FORMAT_ND}) | ||
| 74 | + .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND}); | ||
| 75 | + | ||
| 76 | + this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType); | ||
| 77 | + this->AICore() | ||
| 78 | + .SetTiling(optiling::TilingFunc); | ||
| 79 | + this->AICore().AddConfig("ascend910b"); | ||
| 80 | + } | ||
| 81 | +}; | ||
| 82 | +OP_ADD(DivCustomTemplate); | ||
| 83 | +} | ||
| @@ -0,0 +1,11 @@ | |||
| 1 | + | ||
| 2 | + | ||
| 3 | +namespace optiling { | ||
| 4 | +BEGIN_TILING_DATA_DEF(DivCustomTemplateTilingData) | ||
| 5 | + TILING_DATA_FIELD_DEF(uint32_t, totalLength); // 总元素个数(8*2048=16384) | ||
| 6 | + TILING_DATA_FIELD_DEF(uint32_t, tileLength); // 每次循环处理的 tile 长度 | ||
| 7 | + TILING_DATA_FIELD_DEF(uint32_t, dataType); // 0=float16, 1=float32 | ||
| 8 | +END_TILING_DATA_DEF; | ||
| 9 | + | ||
| 10 | +REGISTER_TILING_DATA_CLASS(DivCustomTemplate, DivCustomTemplateTilingData) | ||
| 11 | +} | ||
| @@ -0,0 +1,6 @@ | |||
| 1 | +# set custom compile options | ||
| 2 | +if ("${CMAKE_BUILD_TYPE}x" STREQUAL "Debugx") | ||
| 3 | + add_ops_compile_options(ALL OPTIONS -g -O0) | ||
| 4 | +endif() | ||
| 5 | + | ||
| 6 | +add_kernels_compile() | ||
| @@ -0,0 +1,99 @@ | |||
| 1 | + | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +using namespace AscendC; | ||
| 5 | + | ||
| 6 | +constexpr int32_t BUFFER_NUM = 2; // 双缓冲:搬运与计算并行 | ||
| 7 | +constexpr uint32_t DTYPE_FP16 = 0; // float16 标记 | ||
| 8 | +constexpr uint32_t DTYPE_FP32 = 1; // float32 标记 | ||
| 9 | + | ||
| 10 | +// 矢量除法核函数类:z = x / y | ||
| 11 | +template <typename T> | ||
| 12 | +class KernelDiv { | ||
| 13 | +public: | ||
| 14 | + __aicore__ inline KernelDiv() {} | ||
| 15 | + | ||
| 16 | + // 初始化:按核切分数据,设置 GM 指针,申请 UB 队列 | ||
| 17 | + __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, | ||
| 18 | + uint32_t totalLength, uint32_t tileLen) { | ||
| 19 | + uint32_t blockLength = totalLength / GetBlockNum(); | ||
| 20 | + this->tileLength = tileLen; | ||
| 21 | + this->tileNum = blockLength / tileLen; | ||
| 22 | + | ||
| 23 | + // GlobalTensor 管理 GM 数据,按 block idx 偏移到本核负责的数据段 | ||
| 24 | + xGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(x) + GetBlockIdx() * blockLength, blockLength); | ||
| 25 | + yGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(y) + GetBlockIdx() * blockLength, blockLength); | ||
| 26 | + zGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(z) + GetBlockIdx() * blockLength, blockLength); | ||
| 27 | + | ||
| 28 | + // 申请 UB 队列:每个队列 2 块 UB,每块 tileLength * sizeof(T) 字节 | ||
| 29 | + pipe.InitBuffer(inQueueX, BUFFER_NUM, tileLength * sizeof(T)); | ||
| 30 | + pipe.InitBuffer(inQueueY, BUFFER_NUM, tileLength * sizeof(T)); | ||
| 31 | + pipe.InitBuffer(outQueueZ, BUFFER_NUM, tileLength * sizeof(T)); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + // 主流程:按 tile 循环执行 CopyIn -> Compute -> CopyOut | ||
| 35 | + __aicore__ inline void Process() { | ||
| 36 | + for (uint32_t i = 0; i < tileNum; ++i) { | ||
| 37 | + CopyIn(i); | ||
| 38 | + Compute(i); | ||
| 39 | + CopyOut(i); | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | +private: | ||
| 44 | + // 搬入:将第 tileIndex 个 tile 的 x/y 数据从 GM 拷贝到 UB | ||
| 45 | + __aicore__ inline void CopyIn(uint32_t tileIndex) { | ||
| 46 | + LocalTensor<T> xLocal = inQueueX.AllocTensor<T>(); | ||
| 47 | + LocalTensor<T> yLocal = inQueueY.AllocTensor<T>(); | ||
| 48 | + DataCopy(xLocal, xGm[tileIndex * tileLength], tileLength); | ||
| 49 | + DataCopy(yLocal, yGm[tileIndex * tileLength], tileLength); | ||
| 50 | + inQueueX.EnQue(xLocal); | ||
| 51 | + inQueueY.EnQue(yLocal); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // 计算:出队 x/y,执行逐元素除法 z = x / y | ||
| 55 | + __aicore__ inline void Compute(uint32_t tileIndex) { | ||
| 56 | + LocalTensor<T> xLocal = inQueueX.DeQue<T>(); | ||
| 57 | + LocalTensor<T> yLocal = inQueueY.DeQue<T>(); | ||
| 58 | + LocalTensor<T> zLocal = outQueueZ.AllocTensor<T>(); | ||
| 59 | + | ||
| 60 | + Div(zLocal, xLocal, yLocal, tileLength); | ||
| 61 | + | ||
| 62 | + outQueueZ.EnQue(zLocal); | ||
| 63 | + inQueueX.FreeTensor(xLocal); | ||
| 64 | + inQueueY.FreeTensor(yLocal); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + // 搬出:将结果 z 从 UB 拷贝回 GM | ||
| 68 | + __aicore__ inline void CopyOut(uint32_t tileIndex) { | ||
| 69 | + LocalTensor<T> zLocal = outQueueZ.DeQue<T>(); | ||
| 70 | + DataCopy(zGm[tileIndex * tileLength], zLocal, tileLength); | ||
| 71 | + outQueueZ.FreeTensor(zLocal); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | +private: | ||
| 75 | + TPipe pipe; // 流水管理器 | ||
| 76 | + TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX; // 输入 x 队列 | ||
| 77 | + TQue<QuePosition::VECIN, BUFFER_NUM> inQueueY; // 输入 y 队列 | ||
| 78 | + TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueZ; // 输出 z 队列 | ||
| 79 | + GlobalTensor<T> xGm, yGm, zGm; // GM 全局数据指针 | ||
| 80 | + uint32_t tileLength = 0; // 单 tile 长度 | ||
| 81 | + uint32_t tileNum = 0; // 本核 tile 循环次数 | ||
| 82 | +}; | ||
| 83 | + | ||
| 84 | +extern "C" __global__ __aicore__ void div_custom_template(GM_ADDR x, GM_ADDR y, GM_ADDR z, | ||
| 85 | + GM_ADDR workspace, GM_ADDR tiling) { | ||
| 86 | + // 从 GM 中解析 host 侧写入的 tiling 参数 | ||
| 87 | + GET_TILING_DATA(tilingData, tiling); | ||
| 88 | + | ||
| 89 | + // 根据数据类型实例化对应模板并执行 | ||
| 90 | + if (tilingData.dataType == DTYPE_FP32) { | ||
| 91 | + KernelDiv<float> op; | ||
| 92 | + op.Init(x, y, z, tilingData.totalLength, tilingData.tileLength); | ||
| 93 | + op.Process(); | ||
| 94 | + } else { | ||
| 95 | + KernelDiv<half> op; | ||
| 96 | + op.Init(x, y, z, tilingData.totalLength, tilingData.tileLength); | ||
| 97 | + op.Process(); | ||
| 98 | + } | ||
| 99 | +} | ||
| @@ -0,0 +1,11 @@ | |||
| 1 | + | ||
| 2 | + | ||
| 3 | +namespace optiling { | ||
| 4 | +BEGIN_TILING_DATA_DEF(DivCustomTemplateTilingData) | ||
| 5 | + TILING_DATA_FIELD_DEF(uint32_t, totalLength); // 总元素个数(8*2048=16384) | ||
| 6 | + TILING_DATA_FIELD_DEF(uint32_t, tileLength); // 每次循环处理的 tile 长度 | ||
| 7 | + TILING_DATA_FIELD_DEF(uint32_t, dataType); // 0=float16, 1=float32 | ||
| 8 | +END_TILING_DATA_DEF; | ||
| 9 | + | ||
| 10 | +REGISTER_TILING_DATA_CLASS(DivCustomTemplate, DivCustomTemplateTilingData) | ||
| 11 | +} | ||
| @@ -0,0 +1,2 @@ | |||
| 1 | + --install-path Install operator package to specific dir path | ||
| 2 | + --install-for-all Allow other users to use the operator package | ||
| @@ -0,0 +1,336 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 3 | + | ||
| 4 | +vendor_name=customize | ||
| 5 | +targetdir=/usr/local/Ascend/opp | ||
| 6 | +target_custom=0 | ||
| 7 | + | ||
| 8 | +sourcedir=$PWD/packages | ||
| 9 | +vendordir=vendors/$vendor_name | ||
| 10 | + | ||
| 11 | +QUIET="y" | ||
| 12 | +INSTALL_FOR_ALL="n" | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +while true | ||
| 16 | +do | ||
| 17 | + case $1 in | ||
| 18 | + --quiet) | ||
| 19 | + QUIET="y" | ||
| 20 | + shift | ||
| 21 | + ;; | ||
| 22 | + --install-path=*) | ||
| 23 | + INSTALL_PATH=$(echo $1 | cut -d"=" -f2-) | ||
| 24 | + INSTALL_PATH=${INSTALL_PATH%*/} | ||
| 25 | + shift | ||
| 26 | + ;; | ||
| 27 | + --install-for-all) | ||
| 28 | + INSTALL_FOR_ALL="y" | ||
| 29 | + shift | ||
| 30 | + ;; | ||
| 31 | + --*) | ||
| 32 | + shift | ||
| 33 | + ;; | ||
| 34 | + *) | ||
| 35 | + break | ||
| 36 | + ;; | ||
| 37 | + esac | ||
| 38 | +done | ||
| 39 | + | ||
| 40 | +log() { | ||
| 41 | + cur_date=`date +"%Y-%m-%d %H:%M:%S"` | ||
| 42 | + echo "[ops_custom] [$cur_date] "$1 | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +if [ -n "${INSTALL_PATH}" ]; then | ||
| 46 | + if [[ ! "${INSTALL_PATH}" = /* ]]; then | ||
| 47 | + log "[ERROR] use absolute path for --install-path argument" | ||
| 48 | + exit 1 | ||
| 49 | + fi | ||
| 50 | + if [ ! -d ${INSTALL_PATH} ]; then | ||
| 51 | + mkdir ${INSTALL_PATH} >> /dev/null 2>&1 | ||
| 52 | + if [ $? -ne 0 ]; then | ||
| 53 | + log "[ERROR] create ${INSTALL_PATH} failed" | ||
| 54 | + exit 1 | ||
| 55 | + fi | ||
| 56 | + fi | ||
| 57 | + targetdir=${INSTALL_PATH} | ||
| 58 | +elif [ -n "${ASCEND_CUSTOM_OPP_PATH}" ]; then | ||
| 59 | + if [[ "${ASCEND_CUSTOM_OPP_PATH}" == *:* ]]; then | ||
| 60 | + log "[ERROR] environment variable ASCEND_CUSTOM_OPP_PATH=${ASCEND_CUSTOM_OPP_PATH} is set and \ | ||
| 61 | + has multiple path in it (colon inside), which will cause the custom op installed incorrectly. \ | ||
| 62 | + Please use the --install-path option to specify an installation path instead." | ||
| 63 | + exit 1 | ||
| 64 | + fi | ||
| 65 | + if [ ! -d ${ASCEND_CUSTOM_OPP_PATH} ]; then | ||
| 66 | + mkdir -p ${ASCEND_CUSTOM_OPP_PATH} >> /dev/null 2>&1 | ||
| 67 | + if [ $? -ne 0 ]; then | ||
| 68 | + log "[ERROR] create ${ASCEND_CUSTOM_OPP_PATH} failed" | ||
| 69 | + fi | ||
| 70 | + fi | ||
| 71 | + targetdir=${ASCEND_CUSTOM_OPP_PATH} | ||
| 72 | +else | ||
| 73 | + if [ "x${ASCEND_OPP_PATH}" == "x" ]; then | ||
| 74 | + log "[ERROR] env ASCEND_OPP_PATH no exist" | ||
| 75 | + exit 1 | ||
| 76 | + fi | ||
| 77 | + targetdir="${ASCEND_OPP_PATH}" | ||
| 78 | +fi | ||
| 79 | + | ||
| 80 | +if [ ! -d $targetdir ];then | ||
| 81 | + log "[ERROR] $targetdir no exist" | ||
| 82 | + exit 1 | ||
| 83 | +fi | ||
| 84 | + | ||
| 85 | +if [ ! -x $targetdir ] || [ ! -w $targetdir ] || [ ! -r $targetdir ];then | ||
| 86 | + log "[WARNING] The directory $targetdir does not have sufficient permissions. \ | ||
| 87 | + Please check and modify the folder permissions (e.g., using chmod), \ | ||
| 88 | + or use the --install-path option to specify an installation path and \ | ||
| 89 | + change the environment variable ASCEND_CUSTOM_OPP_PATH to the specified path." | ||
| 90 | +fi | ||
| 91 | + | ||
| 92 | +upgrade() | ||
| 93 | +{ | ||
| 94 | + if [ ! -d ${sourcedir}/$vendordir/$1 ]; then | ||
| 95 | + log "[INFO] no need to upgrade ops $1 files" | ||
| 96 | + return 0 | ||
| 97 | + fi | ||
| 98 | + | ||
| 99 | + if [ ! -d ${targetdir}/$vendordir/$1 ];then | ||
| 100 | + log "[INFO] create ${targetdir}/$vendordir/$1." | ||
| 101 | + mkdir -p ${targetdir}/$vendordir/$1 | ||
| 102 | + if [ $? -ne 0 ];then | ||
| 103 | + log "[ERROR] create ${targetdir}/$vendordir/$1 failed" | ||
| 104 | + return 1 | ||
| 105 | + fi | ||
| 106 | + else | ||
| 107 | + has_same_file=-1 | ||
| 108 | + for file_a in ${sourcedir}/$vendordir/$1/*; do | ||
| 109 | + file_b=${file_a##*/}; | ||
| 110 | + if [ "ls ${targetdir}/$vendordir/$1" = "" ]; then | ||
| 111 | + log "[INFO] ${targetdir}/$vendordir/$1 is empty !!" | ||
| 112 | + return 1 | ||
| 113 | + fi | ||
| 114 | + grep -q $file_b <<<`ls ${targetdir}/$vendordir/$1`; | ||
| 115 | + if [[ $? -eq 0 ]]; then | ||
| 116 | + echo -n "${file_b} " | ||
| 117 | + has_same_file=0 | ||
| 118 | + fi | ||
| 119 | + done | ||
| 120 | + if [ 0 -eq $has_same_file ]; then | ||
| 121 | + echo | ||
| 122 | + if test $QUIET = "n"; then | ||
| 123 | + echo "[INFO]: has old version in ${targetdir}/$vendordir/$1, \ | ||
| 124 | + you want to Overlay Installation , please enter:[o]; \ | ||
| 125 | + or replace directory installation , please enter: [r]; \ | ||
| 126 | + or not install , please enter:[n]." | ||
| 127 | + | ||
| 128 | + while true | ||
| 129 | + do | ||
| 130 | + read orn | ||
| 131 | + if [ "$orn" = n ]; then | ||
| 132 | + return 0 | ||
| 133 | + elif [ "$orn" = o ]; then | ||
| 134 | + break; | ||
| 135 | + elif [ "$orn" = r ]; then | ||
| 136 | + [ -n "${targetdir}/$vendordir/$1/" ] && rm -rf "${targetdir}/$vendordir/$1"/* | ||
| 137 | + break; | ||
| 138 | + else | ||
| 139 | + log "[ERROR] input error, please input again!" | ||
| 140 | + fi | ||
| 141 | + done | ||
| 142 | + else | ||
| 143 | + [ -n "${targetdir}/$vendordir/$1/" ] && rm -rf "${targetdir}/$vendordir/$1"/* | ||
| 144 | + fi | ||
| 145 | + fi | ||
| 146 | + log "[INFO] replace or merge old ops $1 files ......" | ||
| 147 | + fi | ||
| 148 | + | ||
| 149 | + log "[INFO] copy new ops $1 files ......" | ||
| 150 | + if [ -d ${targetdir}/$vendordir/$1/ ]; then | ||
| 151 | + chmod -R +w "$targetdir/$vendordir/$1/" >/dev/null 2>&1 | ||
| 152 | + fi | ||
| 153 | + cp -rf ${sourcedir}/$vendordir/$1/* $targetdir/$vendordir/$1/ | ||
| 154 | + if [ $? -ne 0 ];then | ||
| 155 | + log "[ERROR] copy new $1 files failed" | ||
| 156 | + return 1 | ||
| 157 | + fi | ||
| 158 | + | ||
| 159 | + return 0 | ||
| 160 | +} | ||
| 161 | +upgrade_proto() | ||
| 162 | +{ | ||
| 163 | + if [ ! -f ${sourcedir}/$vendordir/custom.proto ]; then | ||
| 164 | + log "[INFO] no need to upgrade custom.proto files" | ||
| 165 | + return 0 | ||
| 166 | + fi | ||
| 167 | + if [ ! -d ${targetdir}/$vendordir/framework/caffe ];then | ||
| 168 | + log "[INFO] create ${targetdir}/$vendordir/framework/caffe." | ||
| 169 | + mkdir -p ${targetdir}/$vendordir/framework/caffe | ||
| 170 | + if [ $? -ne 0 ];then | ||
| 171 | + log "[ERROR] create ${targetdir}/$vendordir/framework/caffe failed" | ||
| 172 | + return 1 | ||
| 173 | + fi | ||
| 174 | + else | ||
| 175 | + if [ -f ${targetdir}/$vendordir/framework/caffe/custom.proto ]; then | ||
| 176 | + # 有老版本,判断是否要覆盖式安装 | ||
| 177 | + if test $QUIET = "n"; then | ||
| 178 | + echo "[INFO] ${targetdir}/$vendordir/framework/caffe has old version"\ | ||
| 179 | + "custom.proto file. Do you want to replace? [y/n] " | ||
| 180 | + | ||
| 181 | + while true | ||
| 182 | + do | ||
| 183 | + read yn | ||
| 184 | + if [ "$yn" = n ]; then | ||
| 185 | + return 0 | ||
| 186 | + elif [ "$yn" = y ]; then | ||
| 187 | + break; | ||
| 188 | + else | ||
| 189 | + log "[ERROR] input error, please input again!" | ||
| 190 | + fi | ||
| 191 | + done | ||
| 192 | + fi | ||
| 193 | + fi | ||
| 194 | + log "[INFO] replace old caffe.proto files ......" | ||
| 195 | + fi | ||
| 196 | + chmod -R +w "$targetdir/$vendordir/framework/caffe/" >/dev/null 2>&1 | ||
| 197 | + cp -rf ${sourcedir}/$vendordir/custom.proto ${targetdir}/$vendordir/framework/caffe/ | ||
| 198 | + if [ $? -ne 0 ];then | ||
| 199 | + log "[ERROR] copy new custom.proto failed" | ||
| 200 | + return 1 | ||
| 201 | + fi | ||
| 202 | + log "[INFO] copy custom.proto success" | ||
| 203 | + | ||
| 204 | + return 0 | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +upgrade_file() | ||
| 208 | +{ | ||
| 209 | + if [ ! -e ${sourcedir}/$vendordir/$1 ]; then | ||
| 210 | + log "[INFO] no need to upgrade ops $1 file" | ||
| 211 | + return 0 | ||
| 212 | + fi | ||
| 213 | + | ||
| 214 | + log "[INFO] copy new $1 files ......" | ||
| 215 | + cp -f ${sourcedir}/$vendordir/$1 $targetdir/$vendordir/$1 | ||
| 216 | + if [ $? -ne 0 ];then | ||
| 217 | + log "[ERROR] copy new $1 file failed" | ||
| 218 | + return 1 | ||
| 219 | + fi | ||
| 220 | + | ||
| 221 | + return 0 | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +delete_optiling_file() | ||
| 225 | +{ | ||
| 226 | + if [ ! -d ${targetdir}/vendors ];then | ||
| 227 | + log "[INFO] $1 not exist, no need to uninstall" | ||
| 228 | + return 0 | ||
| 229 | + fi | ||
| 230 | + sys_info=$(uname -m) | ||
| 231 | + if [ ! -d ${sourcedir}/$vendordir/$1/ai_core/tbe/op_tiling/lib/linux/${sys_info} ];then | ||
| 232 | + rm -rf ${sourcedir}/$vendordir/$1/ai_core/tbe/op_tiling/liboptiling.so | ||
| 233 | + fi | ||
| 234 | + return 0 | ||
| 235 | +} | ||
| 236 | + | ||
| 237 | +log "[INFO] copy uninstall sh success" | ||
| 238 | + | ||
| 239 | +if [ ! -d ${targetdir}/vendors ];then | ||
| 240 | + log "[INFO] create ${targetdir}/vendors." | ||
| 241 | + mkdir -p ${targetdir}/vendors | ||
| 242 | + if [ $? -ne 0 ];then | ||
| 243 | + log "[ERROR] create ${targetdir}/vendors failed" | ||
| 244 | + exit 1 | ||
| 245 | + fi | ||
| 246 | +fi | ||
| 247 | +chmod u+w ${targetdir}/vendors | ||
| 248 | + | ||
| 249 | +log "[INFO] upgrade framework" | ||
| 250 | +upgrade framework | ||
| 251 | +if [ $? -ne 0 ];then | ||
| 252 | + exit 1 | ||
| 253 | +fi | ||
| 254 | + | ||
| 255 | +log "[INFO] upgrade op proto" | ||
| 256 | +upgrade op_proto | ||
| 257 | +if [ $? -ne 0 ];then | ||
| 258 | + exit 1 | ||
| 259 | +fi | ||
| 260 | + | ||
| 261 | +log "[INFO] upgrade op impl" | ||
| 262 | +delete_optiling_file op_impl | ||
| 263 | +upgrade op_impl | ||
| 264 | +if [ $? -ne 0 ];then | ||
| 265 | + exit 1 | ||
| 266 | +fi | ||
| 267 | + | ||
| 268 | +log "[INFO] upgrade op api" | ||
| 269 | +upgrade op_api | ||
| 270 | +if [ $? -ne 0 ];then | ||
| 271 | + exit 1 | ||
| 272 | +fi | ||
| 273 | + | ||
| 274 | +log "[INFO] upgrade version.info" | ||
| 275 | +upgrade_file version.info | ||
| 276 | +if [ $? -ne 0 ];then | ||
| 277 | + exit 1 | ||
| 278 | +fi | ||
| 279 | + | ||
| 280 | +upgrade_proto | ||
| 281 | +if [ $? -ne 0 ];then | ||
| 282 | + exit 1 | ||
| 283 | +fi | ||
| 284 | + | ||
| 285 | +# set the set_env.bash | ||
| 286 | +if [ -n "${INSTALL_PATH}" ] && [ -d ${INSTALL_PATH} ]; then | ||
| 287 | + _ASCEND_CUSTOM_OPP_PATH=${targetdir}/${vendordir} | ||
| 288 | + bin_path="${_ASCEND_CUSTOM_OPP_PATH}/bin" | ||
| 289 | + set_env_variable="#!/bin/bash\nexport ASCEND_CUSTOM_OPP_PATH=${_ASCEND_CUSTOM_OPP_PATH}:\${ASCEND_CUSTOM_OPP_PATH}\nexport LD_LIBRARY_PATH=${_ASCEND_CUSTOM_OPP_PATH}/op_api/lib/:\${LD_LIBRARY_PATH}" | ||
| 290 | + if [ ! -d ${bin_path} ]; then | ||
| 291 | + mkdir -p ${bin_path} >> /dev/null 2>&1 | ||
| 292 | + if [ $? -ne 0 ]; then | ||
| 293 | + log "[ERROR] create ${bin_path} failed" | ||
| 294 | + exit 1 | ||
| 295 | + fi | ||
| 296 | + fi | ||
| 297 | + echo -e ${set_env_variable} > ${bin_path}/set_env.bash | ||
| 298 | + if [ $? -ne 0 ]; then | ||
| 299 | + log "[ERROR] write ASCEND_CUSTOM_OPP_PATH to set_env.bash failed" | ||
| 300 | + exit 1 | ||
| 301 | + else | ||
| 302 | + log "[INFO] using requirements: when custom module install finished or before you run the custom module, \ | ||
| 303 | + execute the command [ source ${bin_path}/set_env.bash ] to set the environment path" | ||
| 304 | + fi | ||
| 305 | +else | ||
| 306 | + _ASCEND_CUSTOM_OPP_PATH=${targetdir}/${vendordir} | ||
| 307 | + config_file=${targetdir}/vendors/config.ini | ||
| 308 | + if [ ! -f ${config_file} ]; then | ||
| 309 | + touch ${config_file} | ||
| 310 | + chmod 640 ${config_file} | ||
| 311 | + echo "load_priority=$vendor_name" > ${config_file} | ||
| 312 | + if [ $? -ne 0 ];then | ||
| 313 | + log "[ERROR] echo load_priority failed" | ||
| 314 | + exit 1 | ||
| 315 | + fi | ||
| 316 | + else | ||
| 317 | + found_vendors="$(grep -w "load_priority" "$config_file" | cut --only-delimited -d"=" -f2-)" | ||
| 318 | + found_vendor=$(echo $found_vendors | sed "s/\<$vendor_name\>//g" | tr ',' ' ') | ||
| 319 | + vendor=$(echo $found_vendor | tr -s ' ' ',') | ||
| 320 | + if [ "$vendor" != "" ]; then | ||
| 321 | + sed -i "/load_priority=$found_vendors/s@load_priority=$found_vendors@load_priority=$vendor_name,$vendor@g" "$config_file" | ||
| 322 | + fi | ||
| 323 | + fi | ||
| 324 | + if test $INSTALL_FOR_ALL = "y"; then | ||
| 325 | + chmod 755 ${config_file} | ||
| 326 | + fi | ||
| 327 | + log "[INFO] using requirements: when custom module install finished or before you run the custom module, \ | ||
| 328 | + execute the command [ export LD_LIBRARY_PATH=${_ASCEND_CUSTOM_OPP_PATH}/op_api/lib/:\${LD_LIBRARY_PATH} ] to set the environment path" | ||
| 329 | +fi | ||
| 330 | + | ||
| 331 | +if [ -d ${targetdir}/$vendordir/op_impl/cpu/aicpu_kernel/impl/ ]; then | ||
| 332 | + chmod -R 440 ${targetdir}/$vendordir/op_impl/cpu/aicpu_kernel/impl/* >/dev/null 2>&1 | ||
| 333 | +fi | ||
| 334 | + | ||
| 335 | +echo "SUCCESS" | ||
| 336 | +exit 0 | ||
| @@ -0,0 +1,145 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. | ||
| 3 | + | ||
| 4 | +vendor_name=customize | ||
| 5 | +targetdir=/usr/local/Ascend/opp | ||
| 6 | +target_custom=0 | ||
| 7 | + | ||
| 8 | +sourcedir=$PWD/packages | ||
| 9 | +vendordir=vendors/$vendor_name | ||
| 10 | + | ||
| 11 | +log() { | ||
| 12 | + cur_date=`date +"%Y-%m-%d %H:%M:%S"` | ||
| 13 | + echo "[ops_custom] [$cur_date] "$1 | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +if [[ "x${ASCEND_OPP_PATH}" == "x" ]];then | ||
| 17 | + log "[ERROR] env ASCEND_OPP_PATH no exist" | ||
| 18 | + exit 1 | ||
| 19 | +fi | ||
| 20 | + | ||
| 21 | +targetdir=${ASCEND_OPP_PATH} | ||
| 22 | + | ||
| 23 | +if [ ! -d $targetdir ];then | ||
| 24 | + log "[ERROR] $targetdir no exist" | ||
| 25 | + exit 1 | ||
| 26 | +fi | ||
| 27 | + | ||
| 28 | +if [ ! -x $targetdir ] || [ ! -w $targetdir ] || [ ! -r $targetdir ];then | ||
| 29 | + log "[WARNING] The directory $targetdir does not have sufficient permissions. \ | ||
| 30 | + Please check and modify the folder permissions (e.g., using chmod), \ | ||
| 31 | + or use the --install-path option to specify an installation path and \ | ||
| 32 | + change the environment variable ASCEND_CUSTOM_OPP_PATH to the specified path." | ||
| 33 | +fi | ||
| 34 | + | ||
| 35 | +upgrade() | ||
| 36 | +{ | ||
| 37 | + if [ ! -d ${sourcedir}/$vendordir/$1 ]; then | ||
| 38 | + log "[INFO] no need to upgrade ops $1 files" | ||
| 39 | + return 0 | ||
| 40 | + fi | ||
| 41 | + | ||
| 42 | + if [ ! -d ${targetdir}/$vendordir/$1 ];then | ||
| 43 | + log "[INFO] create ${targetdir}/$vendordir/$1." | ||
| 44 | + mkdir -p ${targetdir}/$vendordir/$1 | ||
| 45 | + if [ $? -ne 0 ];then | ||
| 46 | + log "[ERROR] create ${targetdir}/$vendordir/$1 failed" | ||
| 47 | + return 1 | ||
| 48 | + fi | ||
| 49 | + else | ||
| 50 | + vendor_installed_dir=$(ls "$targetdir/vendors" 2> /dev/null) | ||
| 51 | + for i in $vendor_installed_dir;do | ||
| 52 | + vendor_installed_file=$(ls "$vendor_installed_dir/$vendor_name/$i" 2> /dev/null) | ||
| 53 | + if [ "$i" = "$vendor_name" ] && [ "$vendor_installed_file" != "" ]; then | ||
| 54 | + echo "[INFO]: $vendor_name custom opp package has been installed on the path $vendor_installed_dir, \ | ||
| 55 | + you want to Overlay Installation , please enter:[o]; \ | ||
| 56 | + or replace directory installation , please enter: [r]; \ | ||
| 57 | + or not install , please enter:[n]." | ||
| 58 | + fi | ||
| 59 | + while true | ||
| 60 | + do | ||
| 61 | + read mrn | ||
| 62 | + if [ "$mrn" = o ]; then | ||
| 63 | + break | ||
| 64 | + elif [ "$mrn" = r ]; then | ||
| 65 | + [ -n "$vendor_installed_file"] && rm -rf "$vendor_installed_file" | ||
| 66 | + break | ||
| 67 | + elif [ "$mrn" = n ]; then | ||
| 68 | + return 0 | ||
| 69 | + else | ||
| 70 | + log "[WARNING]: Input error, please input m or r or n to choose!" | ||
| 71 | + fi | ||
| 72 | + done | ||
| 73 | + done | ||
| 74 | + log "[INFO] replace old ops $1 files ......" | ||
| 75 | + fi | ||
| 76 | + | ||
| 77 | + log "copy new ops $1 files ......" | ||
| 78 | + cp -rf ${sourcedir}/$vendordir/$1/* $targetdir/$vendordir/$1/ | ||
| 79 | + if [ $? -ne 0 ];then | ||
| 80 | + log "[ERROR] copy new $1 files failed" | ||
| 81 | + return 1 | ||
| 82 | + fi | ||
| 83 | + | ||
| 84 | + return 0 | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +upgrade_file() | ||
| 88 | +{ | ||
| 89 | + if [ ! -e ${sourcedir}/$vendordir/$1 ]; then | ||
| 90 | + log "[INFO] no need to upgrade ops $1 file" | ||
| 91 | + return 0 | ||
| 92 | + fi | ||
| 93 | + | ||
| 94 | + log "copy new $1 files ......" | ||
| 95 | + cp -f ${sourcedir}/$vendordir/$1 $targetdir/$vendordir/$1 | ||
| 96 | + if [ $? -ne 0 ];then | ||
| 97 | + log "[ERROR] copy new $1 file failed" | ||
| 98 | + return 1 | ||
| 99 | + fi | ||
| 100 | + | ||
| 101 | + return 0 | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +log "[INFO] copy uninstall sh success" | ||
| 105 | + | ||
| 106 | +log "[INFO] upgrade framework" | ||
| 107 | +upgrade framework | ||
| 108 | +if [ $? -ne 0 ];then | ||
| 109 | + exit 1 | ||
| 110 | +fi | ||
| 111 | + | ||
| 112 | +log "[INFO] upgrade op proto" | ||
| 113 | +upgrade op_proto | ||
| 114 | +if [ $? -ne 0 ];then | ||
| 115 | + exit 1 | ||
| 116 | +fi | ||
| 117 | + | ||
| 118 | +log "[INFO] upgrade op impl" | ||
| 119 | +upgrade op_impl | ||
| 120 | +if [ $? -ne 0 ];then | ||
| 121 | + exit 1 | ||
| 122 | +fi | ||
| 123 | + | ||
| 124 | +log "[INFO] upgrade op api" | ||
| 125 | +upgrade op_api | ||
| 126 | +if [ $? -ne 0 ];then | ||
| 127 | + exit 1 | ||
| 128 | +fi | ||
| 129 | + | ||
| 130 | +log "[INFO] upgrade version.info" | ||
| 131 | +upgrade_file version.info | ||
| 132 | +if [ $? -ne 0 ];then | ||
| 133 | + exit 1 | ||
| 134 | +fi | ||
| 135 | + | ||
| 136 | +config_file=${targetdir}/vendors/config.ini | ||
| 137 | +found_vendors="$(grep -w "load_priority" "$config_file" | cut --only-delimited -d"=" -f2-)" | ||
| 138 | +found_vendor=$(echo $found_vendors | sed "s/\<$vendor_name\>//g" | tr ',' ' ') | ||
| 139 | +vendor=$(echo $found_vendor | tr -s ' ' ',') | ||
| 140 | +if [ "$vendor" != "" ]; then | ||
| 141 | + sed -i "/load_priority=$found_vendors/s@load_priority=$found_vendors@load_priority=$vendor_name,$vendor@g" "$config_file" | ||
| 142 | +fi | ||
| 143 | + | ||
| 144 | +echo "SUCCESS" | ||
| 145 | +exit 0 | ||
| @@ -0,0 +1,22 @@ | |||
| 1 | +[{ | ||
| 2 | + "op": "DivCustomTemplate", | ||
| 3 | + "input_desc": [{ | ||
| 4 | + "name": "x", | ||
| 5 | + "param_type": "required", | ||
| 6 | + "format": ["ND", "ND"], | ||
| 7 | + "type": ["float16", "float"] | ||
| 8 | + }, | ||
| 9 | + { | ||
| 10 | + "name": "y", | ||
| 11 | + "param_type": "required", | ||
| 12 | + "format": ["ND", "ND"], | ||
| 13 | + "type": ["float16", "float"] | ||
| 14 | + } | ||
| 15 | + ], | ||
| 16 | + "output_desc": [{ | ||
| 17 | + "name": "z", | ||
| 18 | + "param_type": "required", | ||
| 19 | + "format": ["ND", "ND"], | ||
| 20 | + "type": ["float16", "float"] | ||
| 21 | + }] | ||
| 22 | +}] | ||
| @@ -0,0 +1,90 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +set -e | ||
| 3 | + | ||
| 4 | +# 获取当前脚本所在目录,确保在任何路径下执行都能找到正确文件 | ||
| 5 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| 6 | +cd "$SCRIPT_DIR" | ||
| 7 | + | ||
| 8 | +echo "==========================================" | ||
| 9 | +echo " 1. Loading CANN Environment" | ||
| 10 | +echo "==========================================" | ||
| 11 | +# 加载 CANN 环境变量 | ||
| 12 | +if [ -n "$ASCEND_TOOLKIT_HOME" ] && [ -f "$ASCEND_TOOLKIT_HOME/set_env.sh" ]; then | ||
| 13 | + source $ASCEND_TOOLKIT_HOME/set_env.sh | ||
| 14 | +elif [ -n "$ASCEND_HOME_PATH" ] && [ -f "$ASCEND_HOME_PATH/set_env.sh" ]; then | ||
| 15 | + source $ASCEND_HOME_PATH/set_env.sh | ||
| 16 | +else | ||
| 17 | + echo "Error: Cannot find set_env.sh. Please check your CANN installation." | ||
| 18 | + exit 1 | ||
| 19 | +fi | ||
| 20 | +echo "CANN Environment loaded successfully." | ||
| 21 | + | ||
| 22 | +echo "==========================================" | ||
| 23 | +echo " 2. Generating Operator Project (msopgen)" | ||
| 24 | +echo "==========================================" | ||
| 25 | +# 若算子工程不存在,则基于原型文件使用 msopgen 生成(工程化内容由参与者自行完成) | ||
| 26 | +if [ -d "custom_op" ] && [ -f "custom_op/build.sh" ]; then | ||
| 27 | + echo ">>> custom_op already exists, skip msopgen generation." | ||
| 28 | +else | ||
| 29 | + echo ">>> Generating operator project from div_custom_template.json..." | ||
| 30 | + msopgen gen -i div_custom_template.json -c ai_core-ascend910b4 -lan cpp -out ./custom_op | ||
| 31 | + echo ">>> Operator project generated at ./custom_op" | ||
| 32 | + echo ">>> 已基于910b1生成自定义算子工程 ./custom_op,如果芯片类型不一致或者有其他修改请手动生成相应工程" | ||
| 33 | + echo ">>> 请先在 custom_op/op_kernel 中完成算子核函数实现(参考 README.md),再重新执行 bash run.sh" | ||
| 34 | + exit 0 | ||
| 35 | +fi | ||
| 36 | + | ||
| 37 | +echo "==========================================" | ||
| 38 | +echo " 3. Building Custom Operator" | ||
| 39 | +echo "==========================================" | ||
| 40 | +cd custom_op | ||
| 41 | +# 清理旧的构建目录以确保干净编译 | ||
| 42 | +rm -rf build_out | ||
| 43 | +echo ">>> Running build.sh..." | ||
| 44 | +bash build.sh | ||
| 45 | + | ||
| 46 | +echo "==========================================" | ||
| 47 | +echo " 4. Installing Custom Operator" | ||
| 48 | +echo "==========================================" | ||
| 49 | +RUN_FILE=$(ls build_out/custom_opp*.run 2>/dev/null | head -n 1) | ||
| 50 | + | ||
| 51 | +if [ -z "$RUN_FILE" ]; then | ||
| 52 | + echo "Error: .run file not found in build_out. Build might have failed." | ||
| 53 | + exit 1 | ||
| 54 | +fi | ||
| 55 | +echo ">>> Found installer: $RUN_FILE" | ||
| 56 | +# 安装算子到用户目录 | ||
| 57 | +$RUN_FILE --install-path=${HOME}/ | ||
| 58 | +echo "Operator installed successfully." | ||
| 59 | + | ||
| 60 | +echo "==========================================" | ||
| 61 | +echo " 5. Loading Custom Operator Environment" | ||
| 62 | +echo "==========================================" | ||
| 63 | +# 【关键】必须 source 自定义算子的环境变量,否则运行时找不到算子库 | ||
| 64 | +if [ -f "${HOME}/vendors/customize/bin/set_env.bash" ]; then | ||
| 65 | + source ${HOME}/vendors/customize/bin/set_env.bash | ||
| 66 | + echo "Custom operator environment loaded." | ||
| 67 | +else | ||
| 68 | + echo "Warning: Custom operator env script not found at ${HOME}/vendors/customize/bin/set_env.bash" | ||
| 69 | +fi | ||
| 70 | + | ||
| 71 | +echo "==========================================" | ||
| 72 | +echo " 6. Building Test Case" | ||
| 73 | +echo "==========================================" | ||
| 74 | +cd "$SCRIPT_DIR" | ||
| 75 | +# 编译测试代码 | ||
| 76 | +echo ">>> Compiling test/main.cpp..." | ||
| 77 | +g++ -I$ASCEND_TOOLKIT_HOME/include \ | ||
| 78 | + -I${HOME}/vendors/customize/op_api/include \ | ||
| 79 | + -L$ASCEND_TOOLKIT_HOME/lib64 \ | ||
| 80 | + -L${HOME}/vendors/customize/op_api/lib \ | ||
| 81 | + test/main.cpp \ | ||
| 82 | + -lcust_opapi -lnnopbase -lacl_rt \ | ||
| 83 | + -o execute_div_op | ||
| 84 | +echo "Test case built successfully." | ||
| 85 | + | ||
| 86 | +echo "==========================================" | ||
| 87 | +echo " 7. Running Test Case" | ||
| 88 | +echo "==========================================" | ||
| 89 | +echo ">>> Executing..." | ||
| 90 | +./execute_div_op | ||
| @@ -0,0 +1,60 @@ | |||
| 1 | +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. | ||
| 2 | + | ||
| 3 | +# CMake lowest version requirement | ||
| 4 | +cmake_minimum_required(VERSION 3.5.1) | ||
| 5 | + | ||
| 6 | +# project information | ||
| 7 | +project(acl_execute_div) | ||
| 8 | + | ||
| 9 | +# Compile options | ||
| 10 | +add_compile_options(-std=c++11) | ||
| 11 | + | ||
| 12 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "./") | ||
| 13 | + | ||
| 14 | +set(INC_PATH $ENV{DDK_PATH}) | ||
| 15 | + | ||
| 16 | +if (NOT DEFINED ENV{DDK_PATH}) | ||
| 17 | + set(INC_PATH "/usr/local/Ascend/ascend-toolkit/latest") | ||
| 18 | + message(STATUS "set default INC_PATH: ${INC_PATH}") | ||
| 19 | +else () | ||
| 20 | + message(STATUS "env INC_PATH: ${INC_PATH}") | ||
| 21 | +endif() | ||
| 22 | + | ||
| 23 | +set(CUST_PKG_PATH "${INC_PATH}/opp/vendors/customize/op_api") | ||
| 24 | + | ||
| 25 | +set(LIB_PATH $ENV{NPU_HOST_LIB}) | ||
| 26 | + | ||
| 27 | +# Dynamic libraries in the stub directory can only be used for compilation | ||
| 28 | +if (NOT DEFINED ENV{NPU_HOST_LIB}) | ||
| 29 | + string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME_LOWER) | ||
| 30 | + set(LIB_PATH "/usr/local/Ascend/ascend-toolkit/latest/${CMAKE_SYSTEM_PROCESSOR}-${SYSTEM_NAME_LOWER}/devlib") | ||
| 31 | + message(STATUS "set default LIB_PATH: ${LIB_PATH}") | ||
| 32 | +else () | ||
| 33 | + message(STATUS "env LIB_PATH: ${LIB_PATH}") | ||
| 34 | +endif() | ||
| 35 | + | ||
| 36 | +# Header path | ||
| 37 | +include_directories( | ||
| 38 | + ${INC_PATH}/include | ||
| 39 | + ${CUST_PKG_PATH}/include | ||
| 40 | +) | ||
| 41 | + | ||
| 42 | +# add host lib path | ||
| 43 | +link_directories( | ||
| 44 | + ${LIB_PATH} | ||
| 45 | + ${CUST_PKG_PATH}/lib | ||
| 46 | +) | ||
| 47 | + | ||
| 48 | +add_executable(execute_div_op | ||
| 49 | + main.cpp | ||
| 50 | +) | ||
| 51 | + | ||
| 52 | +target_link_libraries(execute_div_op | ||
| 53 | + ascendcl | ||
| 54 | + cust_opapi | ||
| 55 | + acl_op_compiler | ||
| 56 | + nnopbase | ||
| 57 | + stdc++ | ||
| 58 | +) | ||
| 59 | + | ||
| 60 | +install(TARGETS execute_div_op DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) | ||
A2026/CANN-Code-Detective/Challenge04-DivCustomTemplate/gxhgxh2475698/DivCustomTemplate/test/main.cpp+186-0
| @@ -0,0 +1,186 @@ | |||
| 1 | +/** | ||
| 2 | + * @file main.cpp | ||
| 3 | + * | ||
| 4 | + * Copyright (C) 2024. Huawei Technologies Co., Ltd. All rights reserved. | ||
| 5 | + * | ||
| 6 | + * This program is distributed in the hope that it will be useful, | ||
| 7 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 8 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + do { \ | ||
| 23 | + if (!(cond)) { \ | ||
| 24 | + return_expr; \ | ||
| 25 | + } \ | ||
| 26 | + } while (0) | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + do { \ | ||
| 30 | + printf(message, ##__VA_ARGS__); \ | ||
| 31 | + } while (0) | ||
| 32 | + | ||
| 33 | +int64_t GetShapeSize(const std::vector<int64_t> &shape) | ||
| 34 | +{ | ||
| 35 | + int64_t shapeSize = 1; | ||
| 36 | + for (auto i : shape) { | ||
| 37 | + shapeSize *= i; | ||
| 38 | + } | ||
| 39 | + return shapeSize; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +int Init(int32_t deviceId, aclrtStream *stream) | ||
| 43 | +{ | ||
| 44 | + // Fixed code, acl initialization | ||
| 45 | + auto ret = aclInit(nullptr); | ||
| 46 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return FAILED); | ||
| 47 | + ret = aclrtSetDevice(deviceId); | ||
| 48 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return FAILED); | ||
| 49 | + ret = aclrtCreateStream(stream); | ||
| 50 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return FAILED); | ||
| 51 | + | ||
| 52 | + return SUCCESS; | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +template <typename T> | ||
| 56 | +int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr, | ||
| 57 | + aclDataType dataType, aclTensor **tensor) | ||
| 58 | +{ | ||
| 59 | + auto size = GetShapeSize(shape) * sizeof(T); | ||
| 60 | + // Call aclrtMalloc to allocate device memory | ||
| 61 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 62 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return FAILED); | ||
| 63 | + | ||
| 64 | + // Call aclrtMemcpy to copy host data to device memory | ||
| 65 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 66 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return FAILED); | ||
| 67 | + | ||
| 68 | + // Call aclCreateTensor to create a aclTensor object | ||
| 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, nullptr, 0, aclFormat::ACL_FORMAT_ND, shape.data(), | ||
| 70 | + shape.size(), *deviceAddr); | ||
| 71 | + return SUCCESS; | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +void DestroyResources(std::vector<void *> tensors, std::vector<void *> deviceAddrs, aclrtStream stream, | ||
| 75 | + int32_t deviceId, void *workspaceAddr = nullptr) | ||
| 76 | +{ | ||
| 77 | + // Release aclTensor and device | ||
| 78 | + for (uint32_t i = 0; i < tensors.size(); i++) { | ||
| 79 | + if (tensors[i] != nullptr) { | ||
| 80 | + aclDestroyTensor(reinterpret_cast<aclTensor *>(tensors[i])); | ||
| 81 | + } | ||
| 82 | + if (deviceAddrs[i] != nullptr) { | ||
| 83 | + aclrtFree(deviceAddrs[i]); | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + if (workspaceAddr != nullptr) { | ||
| 87 | + aclrtFree(workspaceAddr); | ||
| 88 | + } | ||
| 89 | + // Destroy stream and reset device | ||
| 90 | + aclrtDestroyStream(stream); | ||
| 91 | + aclrtResetDevice(deviceId); | ||
| 92 | + aclFinalize(); | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +int main(int argc, char **argv) | ||
| 96 | +{ | ||
| 97 | + // 1. (Fixed code) Initialize device / stream, refer to the list of external interfaces of acl | ||
| 98 | + // Update deviceId to your own device id | ||
| 99 | + int32_t deviceId = 0; | ||
| 100 | + aclrtStream stream; | ||
| 101 | + auto ret = Init(deviceId, &stream); | ||
| 102 | + CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return FAILED); | ||
| 103 | + | ||
| 104 | + // 2. Create input and output, need to customize according to the interface of the API | ||
| 105 | + std::vector<int64_t> inputXShape = {8, 2048}; | ||
| 106 | + std::vector<int64_t> inputYShape = {8, 2048}; | ||
| 107 | + std::vector<int64_t> outputZShape = {8, 2048}; | ||
| 108 | + void *inputXDeviceAddr = nullptr; | ||
| 109 | + void *inputYDeviceAddr = nullptr; | ||
| 110 | + void *outputZDeviceAddr = nullptr; | ||
| 111 | + aclTensor *inputX = nullptr; | ||
| 112 | + aclTensor *inputY = nullptr; | ||
| 113 | + aclTensor *outputZ = nullptr; | ||
| 114 | + std::vector<aclFloat16> inputXHostData(inputXShape[0] * inputXShape[1]); | ||
| 115 | + std::vector<aclFloat16> inputYHostData(inputYShape[0] * inputYShape[1]); | ||
| 116 | + std::vector<aclFloat16> outputZHostData(outputZShape[0] * outputZShape[1]); | ||
| 117 | + for (int i = 0; i < inputXShape[0] * inputXShape[1]; ++i) { | ||
| 118 | + inputXHostData[i] = aclFloatToFloat16(1.0); | ||
| 119 | + inputYHostData[i] = aclFloatToFloat16(2.0); | ||
| 120 | + outputZHostData[i] = aclFloatToFloat16(0.0); | ||
| 121 | + } | ||
| 122 | + std::vector<void *> tensors = {inputX, inputY, outputZ}; | ||
| 123 | + std::vector<void *> deviceAddrs = {inputXDeviceAddr, inputYDeviceAddr, outputZDeviceAddr}; | ||
| 124 | + // Create inputX aclTensor | ||
| 125 | + ret = CreateAclTensor(inputXHostData, inputXShape, &inputXDeviceAddr, aclDataType::ACL_FLOAT16, &inputX); | ||
| 126 | + CHECK_RET(ret == ACL_SUCCESS, DestroyResources(tensors, deviceAddrs, stream, deviceId); return FAILED); | ||
| 127 | + // Create inputY aclTensor | ||
| 128 | + ret = CreateAclTensor(inputYHostData, inputYShape, &inputYDeviceAddr, aclDataType::ACL_FLOAT16, &inputY); | ||
| 129 | + CHECK_RET(ret == ACL_SUCCESS, DestroyResources(tensors, deviceAddrs, stream, deviceId); return FAILED); | ||
| 130 | + // Create outputZ aclTensor | ||
| 131 | + ret = CreateAclTensor(outputZHostData, outputZShape, &outputZDeviceAddr, aclDataType::ACL_FLOAT16, &outputZ); | ||
| 132 | + CHECK_RET(ret == ACL_SUCCESS, DestroyResources(tensors, deviceAddrs, stream, deviceId); return FAILED); | ||
| 133 | + | ||
| 134 | + // 3. Call the API of the custom operator library | ||
| 135 | + uint64_t workspaceSize = 0; | ||
| 136 | + aclOpExecutor *executor; | ||
| 137 | + // Calculate the workspace size and allocate memory for it | ||
| 138 | + ret = aclnnDivCustomTemplateGetWorkspaceSize(inputX, inputY, outputZ, &workspaceSize, &executor); | ||
| 139 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDivCustomTemplateGetWorkspaceSize failed. ERROR: %d\n", ret); | ||
| 140 | + DestroyResources(tensors, deviceAddrs, stream, deviceId); return FAILED); | ||
| 141 | + | ||
| 142 | + void *workspaceAddr = nullptr; | ||
| 143 | + if (workspaceSize > 0) { | ||
| 144 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 145 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); | ||
| 146 | + DestroyResources(tensors, deviceAddrs, stream, deviceId, workspaceAddr); return FAILED); | ||
| 147 | + } | ||
| 148 | + // Execute the custom operator | ||
| 149 | + ret = aclnnDivCustomTemplate(workspaceAddr, workspaceSize, executor, stream); | ||
| 150 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDivCustomTemplate failed. ERROR: %d\n", ret); | ||
| 151 | + DestroyResources(tensors, deviceAddrs, stream, deviceId, workspaceAddr); return FAILED); | ||
| 152 | + | ||
| 153 | + // 4. (Fixed code) Synchronize and wait for the task to complete | ||
| 154 | + ret = aclrtSynchronizeStream(stream); | ||
| 155 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); | ||
| 156 | + DestroyResources(tensors, deviceAddrs, stream, deviceId, workspaceAddr); return FAILED); | ||
| 157 | + | ||
| 158 | + // 5. Get the output value, copy the result from device memory to host memory, need to modify according to the | ||
| 159 | + // interface of the API | ||
| 160 | + auto size = GetShapeSize(outputZShape); | ||
| 161 | + std::vector<aclFloat16> resultData(size, 0); | ||
| 162 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outputZDeviceAddr, | ||
| 163 | + size * sizeof(aclFloat16), ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 164 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); | ||
| 165 | + DestroyResources(tensors, deviceAddrs, stream, deviceId, workspaceAddr); return FAILED); | ||
| 166 | + | ||
| 167 | + // 6. Destroy resources, need to modify according to the interface of the API | ||
| 168 | + DestroyResources(tensors, deviceAddrs, stream, deviceId, workspaceAddr); | ||
| 169 | + | ||
| 170 | + // print the output result | ||
| 171 | + // 期望结果:z = x / y = 1.0 / 2.0 = 0.5 | ||
| 172 | + std::vector<aclFloat16> goldenData(size, aclFloatToFloat16(0.5)); | ||
| 173 | + | ||
| 174 | + LOG_PRINT("result is:\n"); | ||
| 175 | + for (int64_t i = 0; i < 10; i++) { | ||
| 176 | + LOG_PRINT("%.1f ", aclFloat16ToFloat(resultData[i])); | ||
| 177 | + } | ||
| 178 | + LOG_PRINT("\n"); | ||
| 179 | + if (std::equal(resultData.begin(), resultData.end(), goldenData.begin())) { | ||
| 180 | + LOG_PRINT("test pass\n"); | ||
| 181 | + } else { | ||
| 182 | + LOG_PRINT("test failed\n"); | ||
| 183 | + return FAILED; | ||
| 184 | + } | ||
| 185 | + return SUCCESS; | ||
| 186 | +} | ||