|
1 | 1 | #!/bin/bash |
2 | | -# Copyright (c) 2024 Huawei Technologies Co., Ltd. |
3 | | -# This file is a part of the CANN Open Software. |
4 | | -# Licensed under CANN Open Software License Agreement Version 1.0 (the "License"). |
5 | | -# Please refer to the License for details. You may not use this file except in compliance with the License. |
6 | | -# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
7 | | -# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
8 | | -# See LICENSE in the root of the software repository for the full text of the License. |
9 | | -# ====================================================================================================================== |
10 | 2 |
|
11 | 3 | # SPDX-License-Identifier: Apache-2.0 |
12 | 4 | # SPDX-FileCopyrightText: Copyright contributors to the vllm-ascend project |
13 | 5 | # Modified from |
14 | | -# https://github.com/vllm-project/vllm-ascend/blob/main/csrc/build.sh |
| 6 | +# https://github.com/vllm-project/vllm-ascend/blob/main/csrc/build_aclnn.sh |
15 | 7 |
|
16 | | -set -e |
| 8 | +ROOT_DIR=$(dirname $(readlink -f ${BASH_SOURCE[0]})) |
17 | 9 |
|
18 | | -CURRENT_DIR=$(dirname $(readlink -f ${BASH_SOURCE[0]})) |
19 | | -BUILD_DIR=${CURRENT_DIR}/build |
20 | | -OUTPUT_DIR=${CURRENT_DIR}/output |
21 | | -USER_ID=$(id -u) |
22 | | -PARENT_JOB="false" |
23 | | -CHECK_COMPATIBLE="true" |
24 | | -ASAN="false" |
25 | | -COV="false" |
26 | | -VERBOSE="false" |
| 10 | +echo "ROOT_DIR: $ROOT_DIR" |
27 | 11 |
|
28 | | -if [ "${USER_ID}" != "0" ]; then |
29 | | - DEFAULT_TOOLKIT_INSTALL_DIR="${HOME}/Ascend/ascend-toolkit/latest" |
30 | | - DEFAULT_INSTALL_DIR="${HOME}/Ascend/latest" |
31 | | -else |
32 | | - DEFAULT_TOOLKIT_INSTALL_DIR="/usr/local/Ascend/ascend-toolkit/latest" |
33 | | - DEFAULT_INSTALL_DIR="/usr/local/Ascend/latest" |
34 | | -fi |
35 | | - |
36 | | -CUSTOM_OPTION="-DBUILD_OPEN_PROJECT=ON" |
37 | | - |
38 | | -function help_info() { |
39 | | - echo "Usage: $0 [options]" |
40 | | - echo "Options:" |
41 | | - echo |
42 | | - echo "-h|--help Displays help message." |
43 | | - echo |
44 | | - echo "-n|--op-name Specifies the compiled operator. If there are multiple values, separate them with semicolons and use quotation marks. The default is all." |
45 | | - echo " For example: -n \"flash_attention_score\" or -n \"flash_attention_score;flash_attention_score_grad\"" |
46 | | - echo |
47 | | - echo "-c|--compute-unit Specifies the chip type. If there are multiple values, separate them with semicolons and use quotation marks. The default is ascend910b." |
48 | | - echo " For example: -c \"ascend910b\" or -c \"ascend910b;ascend310p\"" |
49 | | - echo |
50 | | - echo "--cov Compiles with cov." |
51 | | - echo |
52 | | - echo "--verbose Displays more compilation information." |
53 | | - echo |
54 | | -} |
55 | | - |
56 | | -function log() { |
57 | | - local current_time=`date +"%Y-%m-%d %H:%M:%S"` |
58 | | - echo "[$current_time] "$1 |
59 | | -} |
60 | | - |
61 | | -function set_env() |
62 | | -{ |
63 | | - source $ASCEND_CANN_PACKAGE_PATH/bin/setenv.bash || echo "0" |
64 | | - |
65 | | - export BISHENG_REAL_PATH=$(which bisheng || true) |
66 | | - |
67 | | - if [ -z "${BISHENG_REAL_PATH}" ];then |
68 | | - log "Error: bisheng compilation tool not found, Please check whether the cann package or environment variables are set." |
69 | | - exit 1 |
70 | | - fi |
71 | | -} |
72 | | - |
73 | | -function clean() |
74 | | -{ |
75 | | - if [ -n "${BUILD_DIR}" ];then |
76 | | - rm -rf ${BUILD_DIR} |
77 | | - fi |
78 | | - mkdir -p ${BUILD_DIR} ${OUTPUT_DIR} |
79 | | -} |
80 | | - |
81 | | -function cmake_config() |
82 | | -{ |
83 | | - local extra_option="$1" |
84 | | - log "Info: cmake config ${CUSTOM_OPTION} ${extra_option} ." |
85 | | - cmake .. ${CUSTOM_OPTION} ${extra_option} |
86 | | -} |
87 | | - |
88 | | -function build() |
89 | | -{ |
90 | | - local target="$1" |
91 | | - if [ "${VERBOSE}" == "true" ];then |
92 | | - local option="--verbose" |
93 | | - fi |
94 | | - cmake --build . --target ${target} ${JOB_NUM} ${option} |
95 | | -} |
96 | | - |
97 | | -function gen_bisheng(){ |
98 | | - local ccache_program=$1 |
99 | | - local gen_bisheng_dir=${BUILD_DIR}/gen_bisheng_dir |
| 12 | +IS_INSTALL="false" |
| 13 | +SOC_VERSION="ascend910b" |
100 | 14 |
|
101 | | - if [ ! -d "${gen_bisheng_dir}" ];then |
102 | | - mkdir -p ${gen_bisheng_dir} |
| 15 | +for arg in "$@"; do |
| 16 | + if [[ "$arg" == "a3"]]; then |
| 17 | + SOC_VERSION="ascend910_93" |
| 18 | + elif [[ "$arg" == "install" ]]; then |
| 19 | + IS_INSTALL="true" |
103 | 20 | fi |
104 | 21 |
|
105 | | - pushd ${gen_bisheng_dir} |
106 | | - $(> bisheng) |
107 | | - echo "#!/bin/bash" >> bisheng |
108 | | - echo "ccache_args=""\"""${ccache_program} ${BISHENG_REAL_PATH}""\"" >> bisheng |
109 | | - echo "args=""$""@" >> bisheng |
110 | | - |
111 | | - if [ "${VERBOSE}" == "true" ];then |
112 | | - echo "echo ""\"""$""{ccache_args} ""$""args""\"" >> bisheng |
113 | | - fi |
114 | | - |
115 | | - echo "eval ""\"""$""{ccache_args} ""$""args""\"" >> bisheng |
116 | | - chmod +x bisheng |
117 | | - |
118 | | - export PATH=${gen_bisheng_dir}:$PATH |
119 | | - popd |
120 | | -} |
121 | | - |
122 | | -function build_package(){ |
123 | | - build package |
124 | | -} |
125 | | - |
126 | | -function build_host(){ |
127 | | - build_package |
128 | | -} |
129 | | - |
130 | | -function build_kernel(){ |
131 | | - build ops_kernel |
132 | | -} |
133 | | - |
134 | | -while [[ $# -gt 0 ]]; do |
135 | | - case $1 in |
136 | | - -h|--help) |
137 | | - help_info |
138 | | - exit |
139 | | - ;; |
140 | | - -n|--op-name) |
141 | | - ascend_op_name="$2" |
142 | | - shift 2 |
143 | | - ;; |
144 | | - -c|--compute-unit) |
145 | | - ascend_compute_unit="$2" |
146 | | - shift 2 |
147 | | - ;; |
148 | | - *) |
149 | | - help_info |
| 22 | +echo "SOC_VERSION: $SOC_VERSION" |
| 23 | + |
| 24 | + |
| 25 | +if [[ "$SOC_VERSION" =~ ^ascend310 ]]; then |
| 26 | + # ASCEND310P series |
| 27 | + # currently, no custom aclnn ops for ASCEND310 series |
| 28 | + # CUSTOM_OPS="" |
| 29 | + # SOC_ARG="ascend310p" |
| 30 | + exit 0 |
| 31 | +elif [[ "$SOC_VERSION" =~ ^ascend910b ]]; then |
| 32 | + # ASCEND910B (A2) series |
| 33 | + # dependency: catlass |
| 34 | + CUSTOM_OPS="hamming_dist_top_k;reshape_and_cache_bnsd;" |
| 35 | + SOC_ARG="ascend910b" |
| 36 | +elif [[ "$SOC_VERSION" =~ ^ascend910_93 ]]; then |
| 37 | + # ASCEND910C (A3) series |
| 38 | + # dependency: catlass |
| 39 | + # dependency: cann-toolkit file moe_distribute_base.h |
| 40 | + HCCL_STRUCT_FILE_PATH=$(find -L "${ASCEND_TOOLKIT_HOME}" -name "moe_distribute_base.h" 2>/dev/null | head -n1) |
| 41 | + if [ -z "$HCCL_STRUCT_FILE_PATH" ]; then |
| 42 | + echo "cannot find moe_distribute_base.h file in CANN env" |
150 | 43 | exit 1 |
151 | | - ;; |
152 | | - esac |
153 | | -done |
154 | | - |
155 | | -if [ -n "${ascend_compute_unit}" ];then |
156 | | - CUSTOM_OPTION="${CUSTOM_OPTION} -DASCEND_COMPUTE_UNIT=${ascend_compute_unit}" |
157 | | -fi |
158 | | - |
159 | | -if [ -n "${ascend_op_name}" ];then |
160 | | - CUSTOM_OPTION="${CUSTOM_OPTION} -DASCEND_OP_NAME=${ascend_op_name}" |
161 | | -fi |
162 | | - |
163 | | -if [ -n "${ASCEND_HOME_PATH}" ];then |
164 | | - ASCEND_CANN_PACKAGE_PATH=${ASCEND_HOME_PATH} |
165 | | -elif [ -n "${ASCEND_OPP_PATH}" ];then |
166 | | - ASCEND_CANN_PACKAGE_PATH=$(dirname ${ASCEND_OPP_PATH}) |
167 | | -elif [ -d "${DEFAULT_TOOLKIT_INSTALL_DIR}" ];then |
168 | | - ASCEND_CANN_PACKAGE_PATH=${DEFAULT_TOOLKIT_INSTALL_DIR} |
169 | | -elif [ -d "${DEFAULT_INSTALL_DIR}" ];then |
170 | | - ASCEND_CANN_PACKAGE_PATH=${DEFAULT_INSTALL_DIR} |
| 44 | + fi |
| 45 | + # for dispatch_gmm_combine_decode |
| 46 | + yes | cp "${HCCL_STRUCT_FILE_PATH}" "${ROOT_DIR}/csrc/utils/inc/kernel" |
| 47 | + # for dispatch_ffn_combine |
| 48 | + SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 49 | + TARGET_DIR="$SCRIPT_DIR/dispatch_ffn_combine/op_kernel/utils/" |
| 50 | + TARGET_FILE="$TARGET_DIR/$(basename "$HCCL_STRUCT_FILE_PATH")" |
| 51 | + # for dispatch_ffn_combine_bf16 |
| 52 | + SCRIPT_DIR_BF16=$(cd "$(dirname "$0")" && pwd) |
| 53 | + TARGET_DIR_BF16="$SCRIPT_DIR_BF16/dispatch_ffn_combine_bf16/op_kernel/utils/" |
| 54 | + TARGET_FILE_BF16="$TARGET_DIR_BF16/$(basename "$HCCL_STRUCT_FILE_PATH")" |
| 55 | + |
| 56 | + echo "*************************************" |
| 57 | + echo $HCCL_STRUCT_FILE_PATH |
| 58 | + echo "$TARGET_DIR" |
| 59 | + cp "$HCCL_STRUCT_FILE_PATH" "$TARGET_DIR" |
| 60 | + cp "$HCCL_STRUCT_FILE_PATH" "$TARGET_DIR_BF16" |
| 61 | + |
| 62 | + sed -i 's/struct HcclOpResParam {/struct HcclOpResParamCustom {/g' "$TARGET_FILE" |
| 63 | + sed -i 's/struct HcclRankRelationResV2 {/struct HcclRankRelationResV2Custom {/g' "$TARGET_FILE" |
| 64 | + sed -i 's/struct HcclOpResParam {/struct HcclOpResParamCustom {/g' "$TARGET_FILE_BF16" |
| 65 | + sed -i 's/struct HcclRankRelationResV2 {/struct HcclRankRelationResV2Custom {/g' "$TARGET_FILE_BF16" |
| 66 | + |
| 67 | + CUSTOM_OPS_ARRAY=( |
| 68 | + "hamming_dist_top_k" |
| 69 | + "reshape_and_cache_bnsd" |
| 70 | + ) |
| 71 | + CUSTOM_OPS=$(IFS=';'; echo "${CUSTOM_OPS_ARRAY[*]}") |
| 72 | + SOC_ARG="ascend910_93" |
171 | 73 | else |
172 | | - log "Error: Please set the toolkit package installation directory through parameter -p|--package-path." |
173 | | - exit 1 |
| 74 | + # others |
| 75 | + # currently, no custom aclnn ops for other series |
| 76 | + exit 0 |
174 | 77 | fi |
175 | 78 |
|
176 | | -if [ "${PARENT_JOB}" == "false" ];then |
177 | | - CPU_NUM=$(($(cat /proc/cpuinfo | grep "^processor" | wc -l)*2)) |
178 | | - JOB_NUM="-j${CPU_NUM}" |
179 | | -fi |
180 | 79 |
|
181 | | -CUSTOM_OPTION="${CUSTOM_OPTION} -DCUSTOM_ASCEND_CANN_PACKAGE_PATH=${ASCEND_CANN_PACKAGE_PATH} -DCHECK_COMPATIBLE=${CHECK_COMPATIBLE}" |
| 80 | +# build custom ops |
| 81 | +cd $ROOT_DIR |
| 82 | +echo "Current directory: $PWD, begin to build custom ops (ascendc version)..." |
| 83 | +rm -rf build output |
| 84 | +echo "Building custom ops $CUSTOM_OPS for $SOC_VERSION" |
| 85 | +bash build_ops.sh -n "$CUSTOM_OPS" -c "$SOC_ARG" |
182 | 86 |
|
183 | | -set_env |
184 | | -clean |
| 87 | +# build python wheel |
| 88 | +echo "Begin to build python wheel..." |
| 89 | +bash build_wheel.sh |
185 | 90 |
|
186 | | -ccache_system=$(which ccache || true) |
187 | | -if [ -n "${ccache_system}" ];then |
188 | | - CUSTOM_OPTION="${CUSTOM_OPTION} -DENABLE_CCACHE=ON -DCUSTOM_CCACHE=${ccache_system}" |
189 | | - gen_bisheng ${ccache_system} |
| 91 | +# install custom ops and python package |
| 92 | +echo "Installing ucm custom ops. This may take a while, please wait..." |
| 93 | +if [[ "$IS_INSTALL" == "true" ]]; then |
| 94 | + bash install.sh |
190 | 95 | fi |
191 | | - |
192 | | -cd ${BUILD_DIR} |
193 | | -cmake_config |
194 | | -build_package |
0 commit comments