Skip to content

Commit

Permalink
Ascendc compile and demo ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
linqingke authored and fandawei committed Jun 8, 2024
1 parent 9d07a4b commit fc521bf
Show file tree
Hide file tree
Showing 54 changed files with 4,687 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ mindspore/ccsrc/plugin/device/gpu/kernel/pyboost/auto_generate/
mindspore/ccsrc/plugin/device/ascend/kernel/pyboost/auto_generate/
mindspore/ccsrc/plugin/device/ascend/kernel/opapi/aclnn_kernel_register_auto.cc
mindspore/ccsrc/plugin/device/ascend/kernel/opapi/aclnn_auto_gen/

# ascendc compile
mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/build_out/
3 changes: 3 additions & 0 deletions .jenkins/check/config/filter_cpplint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"mindspore/mindspore/ccsrc/plugin/device/cpu/kernel/lp_norm_cpu_kernel.cc" "build/include"
"mindspore/mindspore/ccsrc/backend/operator/" "whitespace/braces"
"mindspore/mindspore/ccsrc/pybind_api/ir/dtype_py.cc" "whitespace/line_length"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/" "runtime/references"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/" "build/include_subdir"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/" "build/namespaces"
"mindspore/mindspore/ccsrc/plugin/device/cpu/kernel/linear_sum_assignment_cpu_kernel.cc" "whitespace/indent"
"mindspore/mindspore/ccsrc/plugin/device/cpu/kernel/arithmetic_self_cpu_kernel.cc" "readability/braces"
"mindspore/mindspore/ccsrc/runtime/pynative/op_function/func_object.h" "runtime/explicit"
Expand Down
11 changes: 11 additions & 0 deletions .jenkins/check/config/filter_pylint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@
"mindspore/mindspore/python/mindspore/ops/composite/multitype_ops/bitwise_or_impl.py" "too-many-function-args"
"mindspore/mindspore/python/mindspore/ops_generate/gen_ops_inner_prim.py" "super-init-not-called"
"mindspore/mindspore/python/mindspore/ops_generate/arg_handler.py" "unused-import"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "missing-docstring"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "len-as-condition"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "consider-using-ternary"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "ungrouped-imports"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "useless-super-delegation"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "pointless-string-statement"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "misplaced-comparison-constant"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "singleton-comparison"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "inconsistent-return-statements"
"mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/util" "superfluous-parens"


# MindData
"mindspore/mindspore/python/mindspore/dataset/__init__.py" "redefined-builtin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ bool ReadAclnnEnableEnv(const std::string &op_name) {
}
}

static std::set<std::string> kAscendcKernelList = {"AllFinite"};
if (kAscendcKernelList.count(op_name) != 0) {
return true;
}

return false;
}
} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_compile_options(-fstack-protector-all -D_FORTIFY_SOURCE=2 -O2)
cmake_minimum_required(VERSION 3.5.1)
project(ascendc_opp)

if(ENABLE_CROSS_COMPILE)
if(${ENABLE_CROSS_COMPILE})
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL x86_64)
set(CROSS_COMPILE_PLATFORM aarch64)
else()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
set(ENABLE_SOURCE_PACKAGE True)
set(ENABLE_BINARY_PACKAGE False)
set(ENABLE_BINARY_PACKAGE True)
set(ASCEND_COMPUTE_UNIT "ascend910;ascend910b;ascend310p")
set(vendor_name custom_ascendc_ops)
set(ASCEND_PYTHON_EXECUTABLE python3)
set(PKG_PATH lib/plugin/ascend)
set(ENABLE_CROSS_COMPILE False)

if(DEFINED ENV{ASCEND_CUSTOM_PATH})
set(ASCEND_CANN_PACKAGE_PATH $ENV{ASCEND_CUSTOM_PATH}/latest)
Expand Down
179 changes: 142 additions & 37 deletions mindspore/ccsrc/plugin/device/ascend/kernel/ascendc/cmake/func.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

function(get_system_info SYSTEM_INFO)
if(UNIX)
execute_process(COMMAND grep -i ^id= /etc/os-release OUTPUT_VARIABLE TEMP)
string(REGEX REPLACE "\n|id=|ID=|\"" "" SYSTEM_NAME ${TEMP})
set(${SYSTEM_INFO}
${SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}
PARENT_SCOPE)
set(${SYSTEM_INFO} ${SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR} PARENT_SCOPE)
elseif(WIN32)
message(STATUS "System is Windows. Only for pre-build.")
else()
Expand All @@ -14,18 +13,15 @@ endfunction()

function(opbuild)
message(STATUS "Opbuild generating sources")
cmake_parse_arguments(OPBUILD "" "OUT_DIR;PROJECT_NAME;ACCESS_PREFIX;INC_DIR;SEC_INC"
"OPS_SRC" ${ARGN})
execute_process(
COMMAND
${CMAKE_CXX_COMPILER} -g -fPIC -shared -std=c++11 ${OPBUILD_OPS_SRC}
-D_GLIBCXX_USE_CXX11_ABI=0 -I ${ASCEND_CANN_PACKAGE_PATH}/include
-I ${OPBUILD_INC_DIR} -I ${OPBUILD_SEC_INC}
-L ${ASCEND_CANN_PACKAGE_PATH}/lib64 -lexe_graph -lregister -ltiling_api -o
${OPBUILD_OUT_DIR}/libascend_all_ops.so
RESULT_VARIABLE EXEC_RESULT
OUTPUT_VARIABLE EXEC_INFO
ERROR_VARIABLE EXEC_ERROR)
cmake_parse_arguments(OPBUILD "" "OUT_DIR;PROJECT_NAME;ACCESS_PREFIX" "OPS_SRC" ${ARGN})
execute_process(COMMAND ${CMAKE_COMPILE} -g -fPIC -shared -std=c++11 ${OPBUILD_OPS_SRC} -D_GLIBCXX_USE_CXX11_ABI=0
-I ${ASCEND_CANN_PACKAGE_PATH}/include -L ${ASCEND_CANN_PACKAGE_PATH}/lib64 -lexe_graph -lregister
-ltiling_api
-o ${OPBUILD_OUT_DIR}/libascend_all_ops.so
RESULT_VARIABLE EXEC_RESULT
OUTPUT_VARIABLE EXEC_INFO
ERROR_VARIABLE EXEC_ERROR
)
if(${EXEC_RESULT})
message("build ops lib info: ${EXEC_INFO}")
message("build ops lib error: ${EXEC_ERROR}")
Expand All @@ -40,40 +36,39 @@ function(opbuild)
set(prefix_env "OPS_DIRECT_ACCESS_PREFIX=${OPBUILD_ACCESS_PREFIX}")
endif()
set(ENV{LD_LIBRARY_PATH} "${ASCEND_CANN_PACKAGE_PATH}/lib64:$ENV{LD_LIBRARY_PATH}")
execute_process(
COMMAND ${proj_env} ${prefix_env} ${ASCEND_CANN_PACKAGE_PATH}/toolkit/tools/opbuild/op_build
${OPBUILD_OUT_DIR}/libascend_all_ops.so ${OPBUILD_OUT_DIR}
RESULT_VARIABLE EXEC_RESULT
OUTPUT_VARIABLE EXEC_INFO
ERROR_VARIABLE EXEC_ERROR)
execute_process(COMMAND ${proj_env} ${prefix_env} ${ASCEND_CANN_PACKAGE_PATH}/toolkit/tools/opbuild/op_build
${OPBUILD_OUT_DIR}/libascend_all_ops.so ${OPBUILD_OUT_DIR}
RESULT_VARIABLE EXEC_RESULT
OUTPUT_VARIABLE EXEC_INFO
ERROR_VARIABLE EXEC_ERROR
)
if(${EXEC_RESULT})
message("opbuild ops info: ${EXEC_INFO}")
message("opbuild ops error: ${EXEC_ERROR}")
endif()
message(STATUS "Opbuild generating sources - done")
endfunction()

set(UTIL_DIR ${CMAKE_SOURCE_DIR}/mindspore/lite/tools/kernel_builder/ascend/ascendc/cmake/util/)
function(add_ops_info_target)
cmake_parse_arguments(OPINFO "" "TARGET;OPS_INFO;OUTPUT;INSTALL_DIR" ""
${ARGN})
cmake_parse_arguments(OPINFO "" "TARGET;OPS_INFO;OUTPUT;INSTALL_DIR" "" ${ARGN})
get_filename_component(opinfo_file_path "${OPINFO_OUTPUT}" DIRECTORY)
add_custom_command(
OUTPUT ${OPINFO_OUTPUT}
COMMAND mkdir -p ${opinfo_file_path}
COMMAND mkdir -p ${OPINFO_INSTALL_DIR}
COMMAND
${ASCEND_PYTHON_EXECUTABLE}
${UTIL_DIR}/parse_ini_to_json.py ${OPINFO_OPS_INFO}
${OPINFO_OUTPUT})
add_custom_target(${OPINFO_TARGET} ALL DEPENDS ${OPINFO_OUTPUT})
install(FILES ${OPINFO_OUTPUT} DESTINATION ${OPINFO_INSTALL_DIR})
add_custom_command(OUTPUT ${OPINFO_OUTPUT}
COMMAND mkdir -p ${opinfo_file_path}
COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/cmake/util/parse_ini_to_json.py
${OPINFO_OPS_INFO} ${OPINFO_OUTPUT}
)
add_custom_target(${OPINFO_TARGET} ALL
DEPENDS ${OPINFO_OUTPUT}
)
install(FILES ${OPINFO_OUTPUT}
DESTINATION ${OPINFO_INSTALL_DIR}
)
endfunction()

function(add_ops_compile_options OP_TYPE)
cmake_parse_arguments(OP_COMPILE "" "OP_TYPE" "COMPUTE_UNIT;OPTIONS" ${ARGN})
file(APPEND ${ASCEND_AUTOGEN_PATH}/${CUSTOM_COMPILE_OPTIONS}
"${OP_TYPE},${OP_COMPILE_COMPUTE_UNIT},${OP_COMPILE_OPTIONS}\n")
"${OP_TYPE},${OP_COMPILE_COMPUTE_UNIT},${OP_COMPILE_OPTIONS}\n")
endfunction()

function(add_ops_impl_target)
Expand All @@ -86,19 +81,129 @@ function(add_ops_impl_target)
COMMAND mkdir -p ${OPIMPL_INSTALL_DIR}
COMMAND
${ASCEND_PYTHON_EXECUTABLE}
${UTIL_DIR}/ascendc_impl_build.py ${OPIMPL_OPS_INFO}
${PROJECT_SOURCE_DIR}/cmake/util/ascendc_impl_build.py ${OPIMPL_OPS_INFO}
\"${OPIMPL_OPS_BATCH}\" \"${OPIMPL_OPS_ITERATE}\" ${OPIMPL_IMPL_DIR}
${OPIMPL_OUT_DIR}/dynamic
${ASCEND_AUTOGEN_PATH}
COMMAND chmod -R 700 ${OPIMPL_OUT_DIR}/dynamic/*

COMMAND rm -rf ${OPIMPL_OUT_DIR}/.impl_timestamp
COMMAND touch ${OPIMPL_OUT_DIR}/.impl_timestamp
DEPENDS ${OPIMPL_OPS_INFO}
${UTIL_DIR}/ascendc_impl_build.py)
${PROJECT_SOURCE_DIR}/cmake/util/ascendc_impl_build.py)
add_custom_target(${OPIMPL_TARGET} ALL
DEPENDS ${OPIMPL_OUT_DIR}/.impl_timestamp)
if(${ENABLE_SOURCE_PACKAGE})
install(DIRECTORY ${OPIMPL_OUT_DIR}/dynamic
DESTINATION ${OPIMPL_INSTALL_DIR})
endif()
endfunction()

function(add_npu_support_target)
cmake_parse_arguments(NPUSUP "" "TARGET;OPS_INFO_DIR;OUT_DIR;INSTALL_DIR" "" ${ARGN})
get_filename_component(npu_sup_file_path "${NPUSUP_OUT_DIR}" DIRECTORY)
add_custom_command(OUTPUT ${NPUSUP_OUT_DIR}/npu_supported_ops.json
COMMAND mkdir -p ${NPUSUP_OUT_DIR}
COMMAND ${PROJECT_SOURCE_DIR}/cmake/util/gen_ops_filter.sh
${NPUSUP_OPS_INFO_DIR}
${NPUSUP_OUT_DIR}
)
add_custom_target(npu_supported_ops ALL
DEPENDS ${NPUSUP_OUT_DIR}/npu_supported_ops.json
)
install(FILES ${NPUSUP_OUT_DIR}/npu_supported_ops.json
DESTINATION ${NPUSUP_INSTALL_DIR}
)
endfunction()

function(add_bin_compile_target)
cmake_parse_arguments(BINCMP
"" "TARGET;OPS_INFO;COMPUTE_UNIT;IMPL_DIR;ADP_DIR;OUT_DIR;INSTALL_DIR;OPC_PATH;OPP_PATH" "" ${ARGN})
file(MAKE_DIRECTORY ${BINCMP_OUT_DIR}/src)
file(MAKE_DIRECTORY ${BINCMP_OUT_DIR}/bin)
file(MAKE_DIRECTORY ${BINCMP_OUT_DIR}/gen)
execute_process(COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/cmake/util/ascendc_bin_param_build.py
${BINCMP_OPS_INFO} ${BINCMP_OUT_DIR}/gen ${BINCMP_COMPUTE_UNIT} ${BINCMP_OPC_PATH}
RESULT_VARIABLE EXEC_RESULT
OUTPUT_VARIABLE EXEC_INFO
ERROR_VARIABLE EXEC_ERROR
)
if(${EXEC_RESULT})
message("ops binary compile scripts gen info: ${EXEC_INFO}")
message("ops binary compile scripts gen error: ${EXEC_ERROR}")
message(FATAL_ERROR "ops binary compile scripts gen failed!")
endif()
if(NOT TARGET binary)
add_custom_target(binary)
endif()
add_custom_target(${BINCMP_TARGET} ALL
COMMAND cp -r ${BINCMP_IMPL_DIR}/*.* ${BINCMP_OUT_DIR}/src
)
add_custom_target(${BINCMP_TARGET}_gen_ops_config ALL
COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/cmake/util/insert_simplified_keys.py
-p ${BINCMP_OUT_DIR}/bin
COMMAND ${ASCEND_PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/cmake/util/ascendc_ops_config.py
-p ${BINCMP_OUT_DIR}/bin -s ${BINCMP_COMPUTE_UNIT}
)
add_dependencies(binary ${BINCMP_TARGET}_gen_ops_config)
file(GLOB bin_scripts ${BINCMP_OUT_DIR}/gen/*.sh)
set(USING_OPS "AllFinite")
foreach(bin_script ${bin_scripts})
get_filename_component(bin_file ${bin_script} NAME_WE)
string(REPLACE "-" ";" bin_sep ${bin_file})
list(GET bin_sep 0 op_type)
list(GET bin_sep 1 op_file)
list(GET bin_sep 2 op_index)
list(FIND USING_OPS ${op_type} kidx)
if(kidx LESS 0)
continue()
endif()
if(NOT TARGET ${BINCMP_TARGET}_${op_file}_copy)
file(MAKE_DIRECTORY ${BINCMP_OUT_DIR}/bin/${op_file})
add_custom_target(${BINCMP_TARGET}_${op_file}_copy ALL
COMMAND cp ${BINCMP_ADP_DIR}/${op_file}.py ${BINCMP_OUT_DIR}/src/${op_type}.py
)
install(DIRECTORY ${BINCMP_OUT_DIR}/bin/${op_file}
DESTINATION ${BINCMP_INSTALL_DIR}/${BINCMP_COMPUTE_UNIT} OPTIONAL
)
install(FILES ${BINCMP_OUT_DIR}/bin/${op_file}.json
DESTINATION ${BINCMP_INSTALL_DIR}/config/${BINCMP_COMPUTE_UNIT}/ OPTIONAL
)
endif()
add_custom_target(${BINCMP_TARGET}_${op_file}_${op_index} ALL
COMMAND export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION="python" &&
export PYTHONPATH=${BINCMP_OPP_PATH}:${PYTHONPATH} &&
export HI_PYTHON=${ASCEND_PYTHON_EXECUTABLE} &&
bash ${bin_script} ${BINCMP_OUT_DIR}/src/${op_type}.py ${BINCMP_OUT_DIR}/bin/${op_file}
${BINCMP_OPC_PATH} && echo $(MAKE) WORKING_DIRECTORY ${BINCMP_OUT_DIR}
)
add_dependencies(${BINCMP_TARGET}_${op_file}_copy ${BINCMP_TARGET})
add_dependencies(${BINCMP_TARGET}_${op_file}_${op_index} ${BINCMP_TARGET} ${BINCMP_TARGET}_${op_file}_copy)
add_dependencies(${BINCMP_TARGET}_gen_ops_config ${BINCMP_TARGET}_${op_file}_${op_index})

endforeach()
install(FILES ${BINCMP_OUT_DIR}/bin/binary_info_config.json
DESTINATION ${BINCMP_INSTALL_DIR}/config/${BINCMP_COMPUTE_UNIT} OPTIONAL
)

install(DIRECTORY ${BINCMP_OUT_DIR}/bin/${op_file}
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../build_out/kernel/${BINCMP_COMPUTE_UNIT} OPTIONAL
)
install(FILES ${BINCMP_OUT_DIR}/bin/binary_info_config.json
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../build_out/kernel/config/${BINCMP_COMPUTE_UNIT} OPTIONAL
)
install(FILES ${BINCMP_OUT_DIR}/bin/${op_file}.json
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../build_out/kernel/config/${BINCMP_COMPUTE_UNIT} OPTIONAL
)

endfunction()

function(add_cross_compile_target)
cmake_parse_arguments(CROSSMP "" "TARGET;OUT_DIR;INSTALL_DIR" "" ${ARGN})
add_custom_target(${CROSSMP_TARGET} ALL
DEPENDS ${CROSSMP_OUT_DIR}
)
install(DIRECTORY ${CROSSMP_OUT_DIR}
DESTINATION ${CROSSMP_INSTALL_DIR}
)
endfunction()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os

PYF_PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.append(PYF_PATH)
Loading

0 comments on commit fc521bf

Please sign in to comment.