forked from hw-native-sys/simpler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.sh
More file actions
executable file
·372 lines (329 loc) · 11.5 KB
/
Copy pathci.sh
File metadata and controls
executable file
·372 lines (329 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/bin/bash
# Parse arguments
PLATFORM=""
DEVICE_RANGE=""
PARALLEL=false
RUNTIME=""
while [[ $# -gt 0 ]]; do
case $1 in
-p|--platform)
PLATFORM="$2"
shift 2
;;
-d|--device)
DEVICE_RANGE="$2"
shift 2
;;
-r|--runtime)
RUNTIME="$2"
shift 2
;;
--parallel)
PARALLEL=true
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Validate runtime if specified
if [[ -n "$RUNTIME" ]]; then
VALID_RUNTIMES=("host_build_graph" "aicpu_build_graph" "tensormap_and_ringbuffer")
RUNTIME_VALID=false
for r in "${VALID_RUNTIMES[@]}"; do
if [[ "$RUNTIME" == "$r" ]]; then
RUNTIME_VALID=true
break
fi
done
if [[ "$RUNTIME_VALID" == "false" ]]; then
echo "Unknown runtime: $RUNTIME"
echo "Valid runtimes: ${VALID_RUNTIMES[*]}"
exit 1
fi
fi
# Parse device range (e.g., "5-8" or "5")
if [[ "$DEVICE_RANGE" == *-* ]]; then
IFS='-' read -r DEV_START DEV_END <<< "$DEVICE_RANGE"
DEVICES=()
for ((i=DEV_START; i<=DEV_END; i++)); do
DEVICES+=("$i")
done
else
DEVICES=("${DEVICE_RANGE:-0}")
fi
NUM_DEVICES=${#DEVICES[@]}
OS=$(uname -s)
echo "Running tests on $OS..."
OVERALL_EXIT=0
# Run pytest synchronously first
if [[ -d "tests" && "$OS" == "Linux" && "$PLATFORM" != "a2a3sim" ]]; then
echo "Running pytest tests..."
if ! pytest tests -v; then
echo "PYTEST FAILED"
OVERALL_EXIT=1
fi
fi
# Setup temp directory for logs and results
LOG_DIR=$(mktemp -d "${TMPDIR:-/tmp}/ci_parallel_$$.XXXXXX")
RESULTS_FILE="${LOG_DIR}/results.txt"
touch "$RESULTS_FILE"
cleanup() {
kill 0 2>/dev/null
rm -rf "$LOG_DIR"
exit 130
}
trap cleanup INT TERM
trap 'rm -rf "$LOG_DIR"' EXIT
# ---- Discover all tasks ----
EXAMPLES_DIR="examples"
DEVICE_TESTS_DIR="tests/device_tests"
declare -a HW_TASK_NAMES=()
declare -a HW_TASK_DIRS=()
declare -a SIM_TASK_NAMES=()
declare -a SIM_TASK_DIRS=()
# Discover examples
while IFS= read -r -d '' example_dir; do
[[ "$example_dir" == *"/scripts" ]] && continue
kernel_config="${example_dir}/kernels/kernel_config.py"
golden="${example_dir}/golden.py"
[[ -f "$kernel_config" && -f "$golden" ]] || continue
example_name="${example_dir#$EXAMPLES_DIR/}"
# Filter by runtime if specified
if [[ -n "$RUNTIME" && "$example_name" != "$RUNTIME"/* ]]; then
continue
fi
if [[ -n "$PLATFORM" ]]; then
if [[ "$PLATFORM" == "a2a3" ]]; then
HW_TASK_NAMES+=("example:${example_name}")
HW_TASK_DIRS+=("${example_dir}")
else
SIM_TASK_NAMES+=("example:${example_name}")
SIM_TASK_DIRS+=("${example_dir}")
fi
elif [[ "$OS" == "Darwin" ]]; then
SIM_TASK_NAMES+=("example:${example_name}")
SIM_TASK_DIRS+=("${example_dir}")
else
HW_TASK_NAMES+=("example:${example_name}")
HW_TASK_DIRS+=("${example_dir}")
SIM_TASK_NAMES+=("example:${example_name}")
SIM_TASK_DIRS+=("${example_dir}")
fi
done < <(find "$EXAMPLES_DIR" -mindepth 1 -type d -print0 | sort -z)
# Discover device tests (hardware only)
if [[ -d "$DEVICE_TESTS_DIR" ]]; then
RUN_DEVICE_TESTS=false
[[ "$PLATFORM" == "a2a3" ]] && RUN_DEVICE_TESTS=true
[[ -z "$PLATFORM" && "$OS" == "Linux" ]] && RUN_DEVICE_TESTS=true
if [[ "$RUN_DEVICE_TESTS" == "true" ]]; then
while IFS= read -r -d '' test_dir; do
kernel_config="${test_dir}/kernels/kernel_config.py"
golden="${test_dir}/golden.py"
[[ -f "$kernel_config" && -f "$golden" ]] || continue
test_name="${test_dir#$DEVICE_TESTS_DIR/}"
# Filter by runtime if specified
if [[ -n "$RUNTIME" && "$test_name" != "$RUNTIME"/* ]]; then
continue
fi
HW_TASK_NAMES+=("device_test:${test_name}")
HW_TASK_DIRS+=("${test_dir}")
done < <(find "$DEVICE_TESTS_DIR" -mindepth 1 -type d -print0 | sort -z)
else
echo "Skipping device tests (a2a3 hardware only)"
fi
fi
echo "Discovered ${#HW_TASK_NAMES[@]} hardware tasks, ${#SIM_TASK_NAMES[@]} simulation tasks"
MAX_RETRIES=3
# Run a single HW task on a specific device.
# Writes result to RESULTS_FILE as: name:a2a3|PASS_or_FAIL|device:ID|round:N
# Usage: run_hw_task <name> <dir> <device_id> <round>
run_hw_task() {
local name="$1"
local dir="$2"
local device_id="$3"
local round="$4"
local safe_name="${name//[:\/]/_}"
local task_log="${LOG_DIR}/${safe_name}_hw_round${round}.log"
local start_time=$SECONDS
{
echo "========================================"
echo "[Device $device_id] Running: $name (round $round/$MAX_RETRIES)"
echo "========================================"
python examples/scripts/run_example.py \
-k "${dir}/kernels" -g "${dir}/golden.py" \
-p a2a3 -d "$device_id"
} > "$task_log" 2>&1
local rc=$?
local elapsed=$(( SECONDS - start_time ))
if [[ $rc -eq 0 ]]; then
echo "${name}:a2a3|PASS|device:${device_id}|round:${round}|${elapsed}s" >> "$RESULTS_FILE"
else
echo "${name}:a2a3|FAIL|device:${device_id}|round:${round}|${elapsed}s" >> "$RESULTS_FILE"
fi
return $rc
}
# ---- Sequential mode ----
if [[ "$PARALLEL" == "false" ]]; then
# HW tasks: run with retry across different devices
for i in "${!HW_TASK_NAMES[@]}"; do
name="${HW_TASK_NAMES[$i]}"
dir="${HW_TASK_DIRS[$i]}"
passed=false
for round in $(seq 1 $MAX_RETRIES); do
dev_idx=$(( (round - 1) % NUM_DEVICES ))
device_id="${DEVICES[$dev_idx]}"
if run_hw_task "$name" "$dir" "$device_id" "$round"; then
passed=true
break
fi
done
done
# SIM tasks
for i in "${!SIM_TASK_NAMES[@]}"; do
name="${SIM_TASK_NAMES[$i]}"
dir="${SIM_TASK_DIRS[$i]}"
echo "========================================"
echo "Running: $name (a2a3sim)"
echo "========================================"
if python examples/scripts/run_example.py \
-k "${dir}/kernels" -g "${dir}/golden.py" \
-p a2a3sim; then
echo "${name}:a2a3sim|PASS" >> "$RESULTS_FILE"
else
echo "${name}:a2a3sim|FAIL" >> "$RESULTS_FILE"
fi
done
else
# ---- Parallel mode ----
# Sim tasks: launch all in parallel (no device constraint)
declare -a SIM_PIDS=()
for i in "${!SIM_TASK_NAMES[@]}"; do
name="${SIM_TASK_NAMES[$i]}"
dir="${SIM_TASK_DIRS[$i]}"
safe_name="${name//[:\/]/_}"
log_file="${LOG_DIR}/${safe_name}_sim.log"
(
echo "========================================"
echo "Running: $name (a2a3sim)"
echo "========================================"
if python examples/scripts/run_example.py \
-k "${dir}/kernels" -g "${dir}/golden.py" -p a2a3sim; then
echo "${name}:a2a3sim|PASS" >> "$RESULTS_FILE"
else
echo "${name}:a2a3sim|FAIL" >> "$RESULTS_FILE"
fi
) > "$log_file" 2>&1 &
SIM_PIDS+=($!)
done
# HW tasks: dynamic work-stealing with retry
# Each device worker atomically grabs tasks from a shared queue.
# This naturally balances load — faster workers pick up more tasks.
PENDING_INDICES=()
for i in "${!HW_TASK_NAMES[@]}"; do
PENDING_INDICES+=("$i")
done
QUEUE_LOCK="${LOG_DIR}/queue.lock"
for round in $(seq 1 $MAX_RETRIES); do
[[ ${#PENDING_INDICES[@]} -eq 0 ]] && break
echo "---- HW round $round/$MAX_RETRIES: ${#PENDING_INDICES[@]} tasks ----"
TASK_QUEUE="${LOG_DIR}/task_queue_round${round}.txt"
ROUND_MARKER="${LOG_DIR}/round_${round}_results.txt"
printf '%s\n' "${PENDING_INDICES[@]}" > "$TASK_QUEUE"
touch "$ROUND_MARKER"
# Launch one worker per device; each grabs tasks dynamically
declare -a HW_PIDS=()
for d in $(seq 0 $((NUM_DEVICES - 1))); do
device_id="${DEVICES[$d]}"
worker_log="${LOG_DIR}/device_${device_id}_round${round}.log"
(
while true; do
# Atomically pop the next task index from the queue
idx=$(flock "$QUEUE_LOCK" bash -c "
idx=\$(head -n1 \"$TASK_QUEUE\" 2>/dev/null)
if [[ -z \"\$idx\" ]]; then exit 1; fi
sed -i '1d' \"$TASK_QUEUE\"
echo \"\$idx\"
") || break
name="${HW_TASK_NAMES[$idx]}"
dir="${HW_TASK_DIRS[$idx]}"
run_hw_task "$name" "$dir" "$device_id" "$round"
rc=$?
if [[ $rc -eq 0 ]]; then
echo "${idx}|PASS" >> "$ROUND_MARKER"
else
echo "${idx}|FAIL" >> "$ROUND_MARKER"
fi
done
) > "$worker_log" 2>&1 &
HW_PIDS+=($!)
done
# Wait for this round to finish
for pid in "${HW_PIDS[@]}"; do
wait "$pid" 2>/dev/null || true
done
# Collect failures for next round
NEXT_PENDING=()
while IFS='|' read -r idx result; do
if [[ "$result" == "FAIL" ]]; then
NEXT_PENDING+=("$idx")
fi
done < "$ROUND_MARKER"
PENDING_INDICES=("${NEXT_PENDING[@]}")
done
# Wait for sim tasks too
for pid in "${SIM_PIDS[@]}"; do
wait "$pid" 2>/dev/null || true
done
fi
# ---- Print summary ----
# Deduplicate results: a task may have multiple entries (fail then pass on retry).
# Keep the last result per task name — the final outcome.
declare -A FINAL_RESULTS=()
declare -A FINAL_EXTRA=()
declare -a TASK_ORDER=()
while IFS='|' read -r task_name result extra1 extra2 timing; do
if [[ -z "${FINAL_RESULTS[$task_name]+x}" ]]; then
TASK_ORDER+=("$task_name")
fi
FINAL_RESULTS["$task_name"]="$result"
FINAL_EXTRA["$task_name"]="${extra1:+$extra1, }${extra2:+$extra2, }${timing}"
done < "$RESULTS_FILE"
echo ""
echo "========================================"
echo " CI RESULTS SUMMARY"
echo "========================================"
printf "%-55s %s\n" "TASK" "RESULT"
printf "%-55s %s\n" "----" "------"
FAIL_COUNT=0
PASS_COUNT=0
for task_name in "${TASK_ORDER[@]}"; do
result="${FINAL_RESULTS[$task_name]}"
extra="${FINAL_EXTRA[$task_name]}"
if [[ "$result" == "FAIL" ]]; then
printf "%-55s \033[31mFAIL\033[0m (%s)\n" "$task_name" "$extra"
((FAIL_COUNT++))
# Print all round logs inline
safe_name="${task_name//[:\/]/_}"
for round_log in "${LOG_DIR}/${safe_name}_hw_round"*.log "${LOG_DIR}/${safe_name}_sim.log"; do
if [[ -f "$round_log" ]]; then
echo "--- LOG: $(basename "$round_log") ---"
cat "$round_log"
echo "--- END ---"
echo ""
fi
done
else
printf "%-55s \033[32mPASS\033[0m (%s)\n" "$task_name" "$extra"
((PASS_COUNT++))
fi
done
echo "========================================"
echo "Total: $((PASS_COUNT + FAIL_COUNT)) Passed: $PASS_COUNT Failed: $FAIL_COUNT"
echo "========================================"
if [[ $FAIL_COUNT -gt 0 || $OVERALL_EXIT -ne 0 ]]; then
exit 1
fi
echo "All tests passed!"