已合并
feat: 新增 cannsim 仿真一键运行脚本及 --noexec 编译选项 #2874
songkai111创建于 5月21日
feat: 新增 cannsim 仿真一键运行脚本及 --noexec 编译选项 #2874
已合并
songkai111创建于 5月21日
2 个文件变更+293-7
Mbuild.sh+13-7
@@ -312,6 +312,7 @@ usage() {
312 echo "Run examples Options:"312 echo "Run examples Options:"
313 echo $dotted_line313 echo $dotted_line
314 echo " --run_example op_type mode[eager:graph] [pkg_mode --vendor_name=name --example_name=name] Compile and execute the test_aclnn_xxx.cpp/test_geir_xxx.cpp"314 echo " --run_example op_type mode[eager:graph] [pkg_mode --vendor_name=name --example_name=name] Compile and execute the test_aclnn_xxx.cpp/test_geir_xxx.cpp"
315+ echo " --noexec Only compile example, do not execute (useful for cross-platform build + cannsim)"
315 echo " --simulator Enable simulator mode when running aclnn examples"316 echo " --simulator Enable simulator mode when running aclnn examples"
316 echo $dotted_line317 echo $dotted_line
317 echo "Examples:"318 echo "Examples:"
@@ -321,6 +322,7 @@ usage() {
321 echo " bash build.sh --run_example abs eager cust --vendor_name=custom"322 echo " bash build.sh --run_example abs eager cust --vendor_name=custom"
322 echo " bash build.sh --run_example abs eager --simulator --soc=ascend950"323 echo " bash build.sh --run_example abs eager --simulator --soc=ascend950"
323 echo " bash build.sh --run_example abs eager --example_name=abs --soc=ascend950"324 echo " bash build.sh --run_example abs eager --example_name=abs --soc=ascend950"
325+ echo " bash build.sh --run_example abs eager cust --noexec --vendor_name=custom"
324 return326 return
325 ;;327 ;;
326 genop)328 genop)
@@ -1354,15 +1356,19 @@ build_example() {
1354 compile_graph_example "$f" "$executable_name"1356 compile_graph_example "$f" "$executable_name"
1355 fi1357 fi
1356 1358 
1357- # Run the executable1359+ # Run the executable (unless --noexec is specified)
1358- ./${executable_name}1360+ if [[ "$ENABLE_UT_EXEC" == "TRUE" ]]; then
1359- local exit_code=$?1361+ ./${executable_name}
1362+ local exit_code=$?
1360 1363 
1361- if [ $exit_code -eq 0 ]; then1364+ if [ $exit_code -eq 0 ]; then
1362- echo "run ${executable_name}, execute samples success"1365+ echo "run ${executable_name}, execute samples success"
1366+ else
1367+ echo "run ${executable_name}, execute samples failed"
1368+ exit 1
1369+ fi
1363 else1370 else
1364- echo "run ${executable_name}, execute samples failed"1371+ echo "Skip running ${executable_name} (--noexec specified, binary is ready for cannsim)"
1365- exit 1
1366 fi1372 fi
1367 done1373 done
1368}1374}
Ascripts/ci/run_cannsim_example.sh+280-0
@@ -0,0 +1,280 @@
1+#!/bin/bash
2+# ----------------------------------------------------------------------------
3+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
4+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# ----------------------------------------------------------------------------
11+ 
12+set -e
13+ 
14+SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
15+PROJECT_ROOT=$(cd "${SCRIPT_DIR}/../.." && pwd)
16+VENDOR_NAME="cannsim_950"
17+SOC_VERSION="Ascend950"
18+VENDOR_INSTALL_SUFFIX="${VENDOR_NAME}_math"
19+ 
20+usage() {
21+ echo "Usage: bash $0 <op_name> [options]"
22+ echo ""
23+ echo "Build, install, compile example, and run cannsim for an operator on Ascend950."
24+ echo ""
25+ echo "Arguments:"
26+ echo " op_name Operator name, e.g. add_example"
27+ echo ""
28+ echo "Options:"
29+ echo " --skip-build Skip operator package build and install"
30+ echo " --skip-cannsim Stop after compiling example, do not run cannsim"
31+ echo " --gen-report Generate cannsim performance report (--gen-report)"
32+ echo " --clean Clean previous vendor package before building"
33+ echo " -j <num> Parallel build jobs (default: 16)"
34+ echo " -h, --help Show this help message"
35+ echo ""
36+ echo "Examples:"
37+ echo " bash $0 add_example"
38+ echo " bash $0 add_example --skip-build"
39+ echo " bash $0 add_example --gen-report"
40+ echo " bash $0 add_example --clean -j8"
41+ echo ""
42+ echo "Output:"
43+ echo " Vendor package: build_out/cann-ops-math-${VENDOR_NAME}_linux-*.run"
44+ echo " Install path: \${ASCEND_HOME_PATH}/opp/vendors/${VENDOR_INSTALL_SUFFIX}/"
45+ echo " Executable: build/test_aclnn_<op_name>"
46+ echo " Cannsim result: build/cannsim_*_<op_name>/"
47+}
48+ 
49+OP_NAME=""
50+SKIP_BUILD=FALSE
51+SKIP_CANNSIM=FALSE
52+GEN_REPORT=FALSE
53+CLEAN=FALSE
54+JOBS=16
55+ 
56+while [[ $# -gt 0 ]]; do
57+ case "$1" in
58+ -h|--help)
59+ usage
60+ exit 0
61+ ;;
62+ --skip-build)
63+ SKIP_BUILD=TRUE
64+ shift
65+ ;;
66+ --skip-cannsim)
67+ SKIP_CANNSIM=TRUE
68+ shift
69+ ;;
70+ --gen-report)
71+ GEN_REPORT=TRUE
72+ shift
73+ ;;
74+ --clean)
75+ CLEAN=TRUE
76+ shift
77+ ;;
78+ -j)
79+ JOBS="$2"
80+ shift 2
81+ ;;
82+ -*)
83+ echo "[ERROR] Unknown option: $1"
84+ usage
85+ exit 1
86+ ;;
87+ *)
88+ if [[ -z "$OP_NAME" ]]; then
89+ OP_NAME="$1"
90+ else
91+ echo "[ERROR] Unexpected argument: $1"
92+ usage
93+ exit 1
94+ fi
95+ shift
96+ ;;
97+ esac
98+done
99+ 
100+if [[ -z "$OP_NAME" ]]; then
101+ echo "[ERROR] Operator name is required."
102+ usage
103+ exit 1
104+fi
105+ 
106+if [[ -z "$ASCEND_HOME_PATH" ]]; then
107+ echo "[ERROR] ASCEND_HOME_PATH is not set. Please run: source <cann_path>/set_env.sh"
108+ exit 1
109+fi
110+ 
111+cd "${PROJECT_ROOT}"
112+ 
113+VENDOR_DIR="${ASCEND_HOME_PATH}/opp/vendors/${VENDOR_INSTALL_SUFFIX}"
114+VENDOR_LIB_DIR="${VENDOR_DIR}/op_api/lib"
115+RUN_PKG_PATTERN="build_out/cann-ops-math-${VENDOR_NAME}_linux-*.run"
116+ 
117+timestamp() {
118+ date '+%Y-%m-%d %H:%M:%S'
119+}
120+ 
121+elapsed_str() {
122+ local seconds=$1
123+ local mins=$((seconds / 60))
124+ local secs=$((seconds % 60))
125+ if [[ $mins -gt 0 ]]; then
126+ printf "%dm%02ds" $mins $secs
127+ else
128+ printf "%ds" $secs
129+ fi
130+}
131+ 
132+TOTAL_START=$(date +%s)
133+ 
134+echo "============================================================"
135+ echo "[cannsim] Operator : ${OP_NAME}"
136+ echo "[cannsim] Target SoC : ${SOC_VERSION}"
137+ echo "[cannsim] Vendor : ${VENDOR_NAME} (install: ${VENDOR_INSTALL_SUFFIX})"
138+ echo "[cannsim] Project root : ${PROJECT_ROOT}"
139+echo "============================================================"
140+ 
141+# ==================== Stage 0: Clean (optional) ====================
142+if [[ "$CLEAN" == "TRUE" ]]; then
143+ echo ""
144+ echo "------------------------------------------------------------"
145+ echo "[$(timestamp)] [Stage 0] Cleaning previous build and vendor"
146+ echo "------------------------------------------------------------"
147+ if [[ -d "$VENDOR_DIR" ]]; then
148+ echo "[cannsim] Removing installed vendor: ${VENDOR_DIR}"
149+ rm -rf "$VENDOR_DIR"
150+ fi
151+ RUN_PKG=$(ls ${RUN_PKG_PATTERN} 2>/dev/null || true)
152+ if [[ -n "$RUN_PKG" ]]; then
153+ echo "[cannsim] Removing run package: ${RUN_PKG}"
154+ rm -f ${RUN_PKG_PATTERN}
155+ fi
156+fi
157+ 
158+# ==================== Stage 1: Build operator package ====================
159+if [[ "$SKIP_BUILD" == "FALSE" ]]; then
160+ echo ""
161+ echo "------------------------------------------------------------"
162+ echo "[$(timestamp)] [Stage 1] Building operator package"
163+ echo "------------------------------------------------------------"
164+ STAGE1_START=$(date +%s)
165+ 
166+ bash build.sh --pkg --soc=${SOC_VERSION} --vendor_name=${VENDOR_NAME} --ops=${OP_NAME} -j${JOBS}
167+ 
168+ STAGE1_END=$(date +%s)
169+ STAGE1_ELAPSED=$((STAGE1_END - STAGE1_START))
170+ echo "[cannsim] Stage 1 (build package) finished in $(elapsed_str $STAGE1_ELAPSED)"
171+ 
172+ # ==================== Stage 2: Install operator package ====================
173+ echo ""
174+ echo "------------------------------------------------------------"
175+ echo "[$(timestamp)] [Stage 2] Installing operator package"
176+ echo "------------------------------------------------------------"
177+ STAGE2_START=$(date +%s)
178+ 
179+ RUN_PKG=$(ls ${PROJECT_ROOT}/${RUN_PKG_PATTERN} 2>/dev/null || true)
180+ if [[ -z "$RUN_PKG" ]]; then
181+ echo "[ERROR] Run package not found: ${PROJECT_ROOT}/${RUN_PKG_PATTERN}"
182+ exit 1
183+ fi
184+ echo "[cannsim] Installing: ${RUN_PKG}"
185+ ${RUN_PKG}
186+ 
187+ STAGE2_END=$(date +%s)
188+ STAGE2_ELAPSED=$((STAGE2_END - STAGE2_START))
189+ echo "[cannsim] Stage 2 (install package) finished in $(elapsed_str $STAGE2_ELAPSED)"
190+else
191+ echo ""
192+ echo "[cannsim] Stage 1 & 2 skipped (--skip-build)"
193+ STAGE1_ELAPSED=0
194+ STAGE2_ELAPSED=0
195+fi
196+ 
197+# ==================== Stage 3: Compile example ====================
198+echo ""
199+echo "------------------------------------------------------------"
200+echo "[$(timestamp)] [Stage 3] Compiling example (--noexec)"
201+echo "------------------------------------------------------------"
202+STAGE3_START=$(date +%s)
203+ 
204+export LD_LIBRARY_PATH="${VENDOR_LIB_DIR}:${LD_LIBRARY_PATH}"
205+bash build.sh --run_example ${OP_NAME} eager cust --noexec --vendor_name=${VENDOR_NAME}
206+ 
207+EXECUTABLE="${PROJECT_ROOT}/build/test_aclnn_${OP_NAME}"
208+if [[ ! -f "$EXECUTABLE" ]]; then
209+ echo "[ERROR] Executable not found: ${EXECUTABLE}"
210+ exit 1
211+fi
212+echo "[cannsim] Executable ready: ${EXECUTABLE}"
213+ 
214+STAGE3_END=$(date +%s)
215+STAGE3_ELAPSED=$((STAGE3_END - STAGE3_START))
216+echo "[cannsim] Stage 3 (compile example) finished in $(elapsed_str $STAGE3_ELAPSED)"
217+ 
218+# ==================== Stage 4: Run cannsim ====================
219+if [[ "$SKIP_CANNSIM" == "FALSE" ]]; then
220+ echo ""
221+ echo "------------------------------------------------------------"
222+ echo "[$(timestamp)] [Stage 4] Running cannsim simulation"
223+ echo "------------------------------------------------------------"
224+ STAGE4_START=$(date +%s)
225+ 
226+ CANNSIM_ARGS="record ${EXECUTABLE} -s ${SOC_VERSION}"
227+ if [[ "$GEN_REPORT" == "TRUE" ]]; then
228+ CANNSIM_ARGS="${CANNSIM_ARGS} --gen-report"
229+ fi
230+ 
231+ echo "[cannsim] Command: cannsim ${CANNSIM_ARGS}"
232+ python3 -c "from cannsim import main; main.main()" ${CANNSIM_ARGS}
233+ 
234+ CANNSIM_OUTPUT=$(ls -d ${PROJECT_ROOT}/build/cannsim_*_test_aclnn_${OP_NAME} 2>/dev/null | tail -1 || true)
235+ if [[ -n "$CANNSIM_OUTPUT" ]]; then
236+ echo "[cannsim] Result directory: ${CANNSIM_OUTPUT}"
237+ echo "[cannsim] Log file: ${CANNSIM_OUTPUT}/cannsim.log"
238+ if [[ "$GEN_REPORT" == "TRUE" ]]; then
239+ REPORT_DIR="${CANNSIM_OUTPUT}/report"
240+ if [[ -d "$REPORT_DIR" ]]; then
241+ echo "[cannsim] Report (trace_core0.json): ${REPORT_DIR}/trace_core0.json"
242+ fi
243+ fi
244+ fi
245+ 
246+ STAGE4_END=$(date +%s)
247+ STAGE4_ELAPSED=$((STAGE4_END - STAGE4_START))
248+ echo "[cannsim] Stage 4 (cannsim simulation) finished in $(elapsed_str $STAGE4_ELAPSED)"
249+else
250+ echo ""
251+ echo "[cannsim] Stage 4 skipped (--skip-cannsim)"
252+ STAGE4_ELAPSED=0
253+fi
254+ 
255+# ==================== Summary ====================
256+TOTAL_END=$(date +%s)
257+TOTAL_ELAPSED=$((TOTAL_END - TOTAL_START))
258+ 
259+echo ""
260+echo "============================================================"
261+echo " End-to-End Summary"
262+echo "============================================================"
263+echo " Operator : ${OP_NAME}"
264+echo " Target SoC : ${SOC_VERSION}"
265+echo " Vendor : ${VENDOR_NAME} -> ${VENDOR_INSTALL_SUFFIX}"
266+echo " Executable : ${EXECUTABLE}"
267+if [[ "$SKIP_BUILD" == "FALSE" ]]; then
268+echo " Run Package : ${RUN_PKG}"
269+fi
270+if [[ -n "${CANNSIM_OUTPUT:-}" ]]; then
271+echo " Cannsim Output : ${CANNSIM_OUTPUT}"
272+fi
273+echo "------------------------------------------------------------"
274+echo " Stage 1 (build package) : $(elapsed_str $STAGE1_ELAPSED)"
275+echo " Stage 2 (install package) : $(elapsed_str $STAGE2_ELAPSED)"
276+echo " Stage 3 (compile example) : $(elapsed_str $STAGE3_ELAPSED)"
277+echo " Stage 4 (cannsim sim) : $(elapsed_str $STAGE4_ELAPSED)"
278+echo "------------------------------------------------------------"
279+echo " TOTAL E2E Time : $(elapsed_str $TOTAL_ELAPSED)"
280+echo "============================================================"