#!/bin/bash
set -e
BUILD_OPS=""
OPS_EXPLICIT=FALSE
RUN_TEST=OFF
RUN_DEVICE=0
DEVICE_SPECIFIED=FALSE
ENABLE_PACKAGE=FALSE
ENABLE_TORCH_EXTENSION=FALSE
BASE_PATH=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
export BASE_PATH
cd "${BASE_PATH}"
export BUILD_PATH="${BASE_PATH}/build"
export BUILD_OUT_PATH="${BASE_PATH}/build_out"
CANN_3RD_LIB_PATH="${CANN_3RD_LIB_PATH:-${BASE_PATH}/third-party}"
ARCH_INFO=$(uname -m)
dotted_line="----------------------------------------------------------------"
print_error() {
echo
echo $dotted_line
local msg="$1"
echo -e "\033[31m[ERROR] ${msg}\033[0m"
echo $dotted_line
echo
}
print_skip() {
echo -e "\033[33m[SKIP] ${1}\033[0m"
}
SUPPORT_COMPUTE_UNIT_SHORT=("ascend910_93" "ascend910b" "ascend950" "ascend310p")
SUPPORT_COMPUTE_UNIT_SHORT=($(printf '%s\n' "${SUPPORT_COMPUTE_UNIT_SHORT[@]}" | awk '{print length($0) " " $0}' | sort -rn | cut -d ' ' -f2-))
ASCEND_HOME="${ASCEND_HOME_PATH:-${ASCEND_TOOLKIT_HOME}}"
if [ -z "${ASCEND_HOME}" ]; then
echo "Error: Ascend/CANN environment is not configured."
echo "Please source the Ascend environment script first, e.g.:"
echo " source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh"
echo " or source \${HOME}/Ascend/ascend-toolkit/latest/set_env.sh"
exit 1
fi
export ASCEND_HOME_PATH="${ASCEND_HOME_PATH:-${ASCEND_HOME}}"
export LINUX_INCLUDE_PATH="${ASCEND_HOME_PATH}/${ARCH_INFO}-linux/include"
export EAGER_LIBRARY_PATH="${ASCEND_HOME_PATH}/lib64"
for arg in "$@"; do
case $arg in
--ops=*)
BUILD_OPS="${arg#*=}"
OPS_EXPLICIT=TRUE
;;
--run)
RUN_TEST=ON
;;
--device=*)
DEVICE_SPECIFIED=TRUE
RUN_DEVICE="${arg#*=}"
if [[ ! "${RUN_DEVICE}" =~ ^[0-9]{1,10}$ ]] || (( 10#${RUN_DEVICE} > 2147483647 )); then
print_error "--device requires a non-negative 32-bit device ID"
exit 1
fi
RUN_DEVICE=$((10#${RUN_DEVICE}))
;;
--soc=*)
SOC_VERSION="${arg#*=}"
;;
--pkg)
ENABLE_PACKAGE=TRUE
;;
--torch_extension)
ENABLE_TORCH_EXTENSION=TRUE
;;
*)
echo "Unknown option: $arg"
echo "Usage:"
echo " bash build.sh # 只编译库"
echo " bash build.sh --ops=spmv # 编译指定算子"
echo " bash build.sh --ops=spmv,spmm # 编译多个算子(逗号分隔)"
echo " bash build.sh --run # 编译并运行全部 test"
echo " bash build.sh --ops=spmv --run # 编译并运行指定算子测试"
echo " bash build.sh --ops=spmv --run --device=1 # 指定测试可见的 NPU 设备"
echo " bash build.sh --pkg # 编译并打包 run 包"
echo " bash build.sh --pkg --soc=ascend950 # 打包指定 SOC 的 run 包"
echo " bash build.sh --torch_extension # 构建全部 Torch Extension wheel"
echo " bash build.sh --torch_extension --ops=spmm # 构建指定算子的 Torch Extension wheel"
exit 1
;;
esac
done
if [ "${OPS_EXPLICIT}" == "TRUE" ]; then
if [ -z "${BUILD_OPS}" ]; then
print_error "--ops requires at least one non-empty operator name"
exit 1
fi
_raw_ops=()
IFS=',' read -ra _raw_ops <<< "${BUILD_OPS}" || true
_filtered_ops=""
for _op in "${_raw_ops[@]}"; do
_op="${_op#"${_op%%[![:space:]]*}"}"
_op="${_op%"${_op##*[![:space:]]}"}"
[ -n "${_op}" ] || continue
_filtered_ops="${_filtered_ops:+${_filtered_ops},}${_op}"
done
if [ -z "${_filtered_ops}" ]; then
print_error "--ops requires at least one non-empty operator name"
exit 1
fi
BUILD_OPS="${_filtered_ops}"
fi
if [ "${DEVICE_SPECIFIED}" == "TRUE" ] && [ "${RUN_TEST}" != "ON" ]; then
print_error "--device requires --run"
exit 1
fi
if [ "${RUN_TEST}" == "ON" ] && { [ "${ENABLE_PACKAGE}" == "TRUE" ] || [ "${ENABLE_TORCH_EXTENSION}" == "TRUE" ]; }; then
print_error "--run cannot be used with --pkg or --torch_extension"
exit 1
fi
if [ "${ENABLE_PACKAGE}" == "TRUE" ] && [ "${ENABLE_TORCH_EXTENSION}" == "TRUE" ]; then
print_error "--pkg cannot be used with --torch_extension"
exit 1
fi
if [ "${RUN_TEST}" == "ON" ] && [ -z "${BUILD_OPS}" ]; then
TEST_OP_LIST=()
shopt -s nullglob
for d in "${BASE_PATH}/test"/*/; do
[ -d "$d" ] || continue
name=$(basename "$d")
if [ -f "${d}/CMakeLists.txt" ]; then
has_sub=0
for d2 in "${d}"*/; do
[ -d "$d2" ] || continue
if [ -f "${d2}/CMakeLists.txt" ]; then
has_sub=1
TEST_OP_LIST+=("$(basename "$d2")")
fi
done
[ "$has_sub" -eq 0 ] && TEST_OP_LIST+=("$name")
else
for d2 in "${d}"*/; do
[ -d "$d2" ] || continue
name2=$(basename "$d2")
if [ -f "${d2}/CMakeLists.txt" ]; then
TEST_OP_LIST+=("$name2")
fi
done
fi
done
shopt -u nullglob
if [ ${#TEST_OP_LIST[@]} -eq 0 ]; then
print_error "No tests found under test/ (expected test/<op>/CMakeLists.txt)"
exit 1
fi
BUILD_OPS=$(printf '%s\n' "${TEST_OP_LIST[@]}" | sort | paste -sd, -)
fi
if [ -n "${BUILD_OPS}" ]; then
IFS=',' read -ra _ops_arr <<< "${BUILD_OPS}"
_expanded=()
shopt -s nullglob
for _op in "${_ops_arr[@]}"; do
if [ -d "${BASE_PATH}/test/${_op}" ]; then
_has_leaf=0
for _d2 in "${BASE_PATH}/test/${_op}"/*/; do
if [ -f "${_d2}CMakeLists.txt" ]; then
_leaf=$(basename "${_d2}")
_expanded+=("${_leaf}")
_has_leaf=1
fi
done
[ "$_has_leaf" -eq 0 ] && _expanded+=("${_op}")
else
_expanded+=("${_op}")
fi
done
shopt -u nullglob
BUILD_OPS=$(printf '%s\n' "${_expanded[@]}" | awk '!seen[$0]++' | paste -sd, -)
fi
if [ "${ENABLE_TORCH_EXTENSION}" == "TRUE" ]; then
echo "Building Torch Extension wheel (ops: ${BUILD_OPS:-all})"
if ! python3 -c 'import build; assert getattr(build, "__file__", None)' 2>/dev/null; then
print_error "Python package 'build' is required; run: python3 -m pip install -r torch_extension/requirements.txt"
exit 1
fi
if [ -n "${BUILD_OPS}" ]; then
export TORCH_EXTENSION_OPS="${BUILD_OPS}"
else
unset TORCH_EXTENSION_OPS
fi
(
cd "${BASE_PATH}/torch_extension"
python3 -m build --wheel -n --outdir "${BUILD_OUT_PATH}"
)
exit 0
fi
echo "BUILD_OPS=${BUILD_OPS}, RUN_TEST=${RUN_TEST}, ENABLE_PACKAGE=${ENABLE_PACKAGE}"
if [ -z "${SOC_VERSION}" ]; then
SOC_VERSION="ascend910b3"
fi
soc_lower=$(echo "${SOC_VERSION}" | tr '[:upper:]' '[:lower:]')
matched=""
for support_unit in "${SUPPORT_COMPUTE_UNIT_SHORT[@]}"; do
if [[ "${soc_lower}" == "${support_unit}"* ]]; then
matched="${support_unit}"
break
fi
done
if [ -z "${matched}" ]; then
echo "Error: The soc [${SOC_VERSION}] is not supported."
echo "Supported SOC: ${SUPPORT_COMPUTE_UNIT_SHORT[*]}"
exit 1
fi
rm -rf -- "${BUILD_PATH}"
mkdir -p -- "${BUILD_PATH}"
BUILD_TYPE="${CMAKE_BUILD_TYPE:-Release}"
CMAKE_OPTIONS=(
"-DSOC_VERSION=${SOC_VERSION}"
"-DASCEND_CANN_PACKAGE_PATH=${ASCEND_HOME}"
"-DCMAKE_BUILD_TYPE=${BUILD_TYPE}"
)
if [ -z "${ASC_DIR}" ]; then
for p in "${ASCEND_HOME}/lib64/cmake" "${ASCEND_HOME}/compiler/latest/lib64/cmake" \
"${ASCEND_HOME}/ascendc/lib64/cmake"; do
if [ -f "${p}/ASC/ASCConfig.cmake" ] || [ -f "${p}/ASC/asc-config.cmake" ]; then
CMAKE_OPTIONS+=("-DCMAKE_PREFIX_PATH=${p}")
break
fi
done
fi
[ -n "${ASC_DIR}" ] && CMAKE_OPTIONS+=("-DASC_DIR=${ASC_DIR}")
if [ -n "${BUILD_OPS}" ]; then
OP_LIST_CMAKE="${BUILD_OPS//,/;}"
CMAKE_OPTIONS+=("-DBUILD_TEST=ON" "-DOP_LIST=${OP_LIST_CMAKE}")
fi
if [ "${ENABLE_PACKAGE}" == "TRUE" ]; then
CMAKE_OPTIONS+=("-DENABLE_PACKAGE=ON" "-DCANN_3RD_LIB_PATH=${CANN_3RD_LIB_PATH}")
fi
cmake -S "${BASE_PATH}" -B "${BUILD_PATH}" "${CMAKE_OPTIONS[@]}"
cmake --build "${BUILD_PATH}" -j
if [ "${ENABLE_PACKAGE}" == "TRUE" ]; then
cmake --build "${BUILD_PATH}" --target package
else
cmake --install "${BUILD_PATH}"
fi
if [ "${RUN_TEST}" == "ON" ]; then
IFS=',' read -ra OP_ARRAY <<< "${BUILD_OPS}"
FAILED_OPS=()
PASSED_OPS=()
SKIPPED_OPS=()
declare -A SKIP_REASON_MAP=()
SKIPPED_FILE="${BUILD_PATH}/test/skipped_tests.list"
if [ -f "${SKIPPED_FILE}" ]; then
while IFS='|' read -r skip_op skip_reason || [ -n "${skip_op}" ]; do
[ -z "${skip_op}" ] && continue
SKIP_REASON_MAP["${skip_op}"]="${skip_reason}"
done < "${SKIPPED_FILE}"
fi
for op in "${OP_ARRAY[@]}"; do
if [ -n "${SKIP_REASON_MAP[${op}]+x}" ]; then
echo ""
echo "========== Skipping ${op}_test =========="
print_skip "${op}_test: not supported on SOC '${SOC_VERSION}' (${SKIP_REASON_MAP[${op}]})"
SKIPPED_OPS+=("${op}")
continue
fi
TEST_BIN="${BUILD_PATH}/test/${op}/${op}_test"
if [ ! -f "${TEST_BIN}" ]; then
found=$(find "${BUILD_PATH}/test" -name "${op}_test" -type f 2>/dev/null | head -1)
[ -n "$found" ] && TEST_BIN="$found"
fi
echo ""
echo "========== Running ${op}_test =========="
if [ ! -f "${TEST_BIN}" ]; then
print_error "Test binary not found: ${TEST_BIN} (build may have failed)"
FAILED_OPS+=("${op}")
continue
fi
set +e
TEST_DIR="$(dirname "${TEST_BIN}")"
(
cd "${TEST_DIR}" || exit 1
if [ "${DEVICE_SPECIFIED}" == "TRUE" ]; then
export ASCEND_RT_VISIBLE_DEVICES="${RUN_DEVICE}"
fi
LD_LIBRARY_PATH="${BUILD_OUT_PATH}/lib64:${LD_LIBRARY_PATH}" "./$(basename "${TEST_BIN}")"
)
exit_code=$?
set -e
if [ $exit_code -eq 0 ]; then
echo "[PASS] ${op}_test"
PASSED_OPS+=("${op}")
else
echo "[FAIL] ${op}_test (exit code: $exit_code)"
FAILED_OPS+=("${op}")
fi
done
echo ""
echo "========================================"
echo "Test Summary:"
echo " Passed: ${#PASSED_OPS[@]} - ${PASSED_OPS[*]}"
echo " Skipped: ${#SKIPPED_OPS[@]} - ${SKIPPED_OPS[*]} (not supported on ${SOC_VERSION})"
echo " Failed: ${#FAILED_OPS[@]} - ${FAILED_OPS[*]}"
echo "========================================"
if [ ${#FAILED_OPS[@]} -gt 0 ]; then
exit 1
fi
fi