Repository navigation
Expand file tree
/
Copy pathrun_benchmark.sh
More file actions
executable file
·301 lines (278 loc) · 13.1 KB
/
Copy pathrun_benchmark.sh
File metadata and controls
executable file
·301 lines (278 loc) · 13.1 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
#!/usr/bin/env bash
# File: run_benchmark.sh
# Description: Builds src/cuda_memcpy.
# Execution: bash run_benchmark.sh [--profile smoke|baseline|extended] [--validate]
# Options: --profile, --smoke, --baseline, --extended, --device, --phase, --raw-file, --config, --validate, --help
# Requirements: bash, python3, repository-local .venv
# Dependencies: common.sh, collect_workload.py
# License: Apache-2.0
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_NAME="$(basename "${REPO_ROOT}")"
cd "${REPO_ROOT}"
# shellcheck disable=SC1091
source scripts/lib/common.sh
capture_benchmark_run_command_submitted "$@"
usage() {
cat <<'USAGE'
Usage: bash run_benchmark.sh [OPTIONS]
Builds src/cuda_memcpy.
Profiles:
--profile <name> Run profile: smoke|baseline|extended (default: smoke)
--smoke Run smoke profile
--baseline Run baseline profile
--extended Run extended profile
Execution:
--device <gpu|cpu> Execution device (default: gpu)
--phase <phaseN|N> Run one phase (phase1|phase2|phase3|phase4, or 1-4)
--phase1 Collection
--phase2 Collection alias
--phase3 Parse only; requires --raw-file <path>
--phase4 Validation only
--raw-file <path> Existing raw file for --phase3
--config <file> Config file (default: config/benchmark_config.yaml)
Validation and logging:
--validate Enable result validation (default)
--no-validate Skip result validation
--quiet Suppress nonessential stdout
--log-level <level> ERROR|WARN|INFO|DEBUG (default: INFO)
--output-format <fmt> Override config output_format
--save-options-file <path> Write resolved CLI options to PATH
Workload options:
--device-id <value> Override device_id
--direction <value> Override direction
--pinned-memory <value> Override pinned_memory
--min-bytes <value> Override min_bytes
--max-bytes <value> Override max_bytes
--step-factor <value> Override step_factor
--warmup-iters <value> Override warmup_iters
--num-iterations <value> Override num_iterations
Information:
--specification Print this workload's specification and exit
--matrix-definition Same as --specification
--help Show this help and exit
Examples:
bash run_benchmark.sh --profile smoke --validate
bash run_benchmark.sh --baseline --device gpu
bash run_benchmark.sh --phase3 --raw-file results/raw/<run>/raw_output.txt
USAGE
}
for _help_arg in "$@"; do
case "${_help_arg}" in
--help) usage; exit 0 ;;
--specification|--matrix-definition)
python3 scripts/print_benchmark_definition.py
exit 0
;;
esac
done
PROFILE="smoke"
DEVICE="gpu"
PHASE="all"
RAW_FILE=""
CONFIG_FILE="config/benchmark_config.yaml"
VALIDATE=1
QUIET=0
LOG_LEVEL_VALUE="INFO"
OUTPUT_FORMAT=""
SAVE_OPTIONS_FILE=""
DEVICE_ID=""
DIRECTION=""
PINNED_MEMORY=""
MIN_BYTES=""
MAX_BYTES=""
STEP_FACTOR=""
WARMUP_ITERS=""
NUM_ITERATIONS=""
while [[ $# -gt 0 ]]; do
case "$1" in
--profile) PROFILE="${2:-}"; shift 2 ;;
--smoke) PROFILE="smoke"; shift ;;
--baseline) PROFILE="baseline"; shift ;;
--extended) PROFILE="extended"; shift ;;
--device) DEVICE="${2:-}"; shift 2 ;;
--phase) PHASE="${2:-}"; shift 2 ;;
--phase1|--phase2) PHASE="1"; shift ;;
--phase3) PHASE="3"; shift ;;
--phase4) PHASE="4"; shift ;;
--raw-file) RAW_FILE="${2:-}"; shift 2 ;;
--config) CONFIG_FILE="${2:-}"; shift 2 ;;
--validate) VALIDATE=1; shift ;;
--no-validate) VALIDATE=0; shift ;;
--quiet) QUIET=1; shift ;;
--log-level) LOG_LEVEL_VALUE="${2:-}"; shift 2 ;;
--output-format) OUTPUT_FORMAT="${2:-}"; shift 2 ;;
--save-options-file) SAVE_OPTIONS_FILE="${2:-}"; shift 2 ;;
--device-id) DEVICE_ID="${2:-}"; shift 2 ;;
--direction) DIRECTION="${2:-}"; shift 2 ;;
--pinned-memory) PINNED_MEMORY="${2:-}"; shift 2 ;;
--min-bytes) MIN_BYTES="${2:-}"; shift 2 ;;
--max-bytes) MAX_BYTES="${2:-}"; shift 2 ;;
--step-factor) STEP_FACTOR="${2:-}"; shift 2 ;;
--warmup-iters) WARMUP_ITERS="${2:-}"; shift 2 ;;
--num-iterations) NUM_ITERATIONS="${2:-}"; shift 2 ;;
--help|--specification|--matrix-definition) shift ;;
*) die "Unknown option: $1" ;;
esac
done
case "${PHASE}" in
all|phase1|phase2|phase3|phase4|1|2|3|4) ;;
*) die "Unsupported --phase value: ${PHASE}" ;;
esac
if [[ "${PHASE}" == phase* ]]; then PHASE="${PHASE#phase}"; fi
set_log_level "${LOG_LEVEL_VALUE}"
begin_benchmark_run "${PROFILE}"
if [[ "${PHASE}" != "3" && "${PHASE}" != "4" ]]; then
bash scripts/ensure_setup.sh
fi
mark_benchmark_measure_start
PYTHON_BIN="${REPO_ROOT}/.venv/bin/python"
[[ -x "${PYTHON_BIN}" ]] || die "Missing ${PYTHON_BIN}; run bash setup.sh --assume-yes first."
yaml_get() {
"${PYTHON_BIN}" - "${CONFIG_FILE}" "${PROFILE}" "$1" <<'PY'
import sys
from pathlib import Path
import yaml
key = sys.argv[3]
profile = sys.argv[2]
cfg = yaml.safe_load(Path(sys.argv[1]).read_text(encoding="utf-8")) or {}
sweep = cfg.get("sweep") or {}
raw_value = sweep.get(key, "")
if isinstance(raw_value, dict):
raw_value = raw_value.get(profile, next(iter(raw_value.values()), ""))
if isinstance(raw_value, list):
raw_value = ",".join(str(item) for item in raw_value)
print("" if raw_value is None else raw_value)
PY
}
DEVICE_ID="${DEVICE_ID:-$(yaml_get device_id)}"
DIRECTION="${DIRECTION:-$(yaml_get direction)}"
PINNED_MEMORY="${PINNED_MEMORY:-$(yaml_get pinned_memory)}"
MIN_BYTES="${MIN_BYTES:-$(yaml_get min_bytes)}"
MAX_BYTES="${MAX_BYTES:-$(yaml_get max_bytes)}"
STEP_FACTOR="${STEP_FACTOR:-$(yaml_get step_factor)}"
WARMUP_ITERS="${WARMUP_ITERS:-$(yaml_get warmup_iters)}"
NUM_ITERATIONS="${NUM_ITERATIONS:-$(yaml_get num_iterations)}"
OUTPUT_FORMAT="${OUTPUT_FORMAT:-$(yaml_get output_format)}"
OUTPUT_FORMAT="${OUTPUT_FORMAT:-csv}"
BENCHMARK_RUN_COMMAND="bash run_benchmark.sh --${PROFILE}"
[[ "${VALIDATE}" -eq 1 ]] && BENCHMARK_RUN_COMMAND+=" --validate"
[[ -n "${DEVICE_ID}" ]] && BENCHMARK_RUN_COMMAND+=" --device-id $(printf %q "${DEVICE_ID}")"
[[ -n "${DIRECTION}" ]] && BENCHMARK_RUN_COMMAND+=" --direction $(printf %q "${DIRECTION}")"
[[ -n "${PINNED_MEMORY}" ]] && BENCHMARK_RUN_COMMAND+=" --pinned-memory $(printf %q "${PINNED_MEMORY}")"
[[ -n "${MIN_BYTES}" ]] && BENCHMARK_RUN_COMMAND+=" --min-bytes $(printf %q "${MIN_BYTES}")"
[[ -n "${MAX_BYTES}" ]] && BENCHMARK_RUN_COMMAND+=" --max-bytes $(printf %q "${MAX_BYTES}")"
[[ -n "${STEP_FACTOR}" ]] && BENCHMARK_RUN_COMMAND+=" --step-factor $(printf %q "${STEP_FACTOR}")"
[[ -n "${WARMUP_ITERS}" ]] && BENCHMARK_RUN_COMMAND+=" --warmup-iters $(printf %q "${WARMUP_ITERS}")"
[[ -n "${NUM_ITERATIONS}" ]] && BENCHMARK_RUN_COMMAND+=" --num-iterations $(printf %q "${NUM_ITERATIONS}")"
set_benchmark_run_command "${BENCHMARK_RUN_COMMAND}"
HOSTNAME_VALUE="$(hostname -s 2>/dev/null || hostname)"
STAMP="$(date -u +"%Y%m%d_%H%M%S")"
if [[ -z "${RAW_FILE}" ]]; then
RUN_DIR="${REPO_ROOT}/results/raw/${STAMP}_${REPO_NAME}_${HOSTNAME_VALUE}"
mkdir -p "${RUN_DIR}"
RAW_FILE="${RUN_DIR}/raw_output.txt"
else
RUN_DIR="$(cd "$(dirname "${RAW_FILE}")" && pwd)"
fi
set_benchmark_run_dir "${RUN_DIR}"
COMMAND_LOG="${RUN_DIR}/commands_executed.sh"
{ echo "#!/usr/bin/env bash"; echo "set -euo pipefail"; } > "${COMMAND_LOG}"
chmod +x "${COMMAND_LOG}"
[[ -n "${SAVE_OPTIONS_FILE}" ]] && printf 'profile=%s\noutput_format=%s\n' "${PROFILE}" "${OUTPUT_FORMAT}" > "${SAVE_OPTIONS_FILE}"
printf 'profile=%s\ndevice=%s\noutput_format=%s\n' "${PROFILE}" "${DEVICE}" "${OUTPUT_FORMAT}" > "${RUN_DIR}/cli_options.env"
export PYTHONUNBUFFERED=1
export PATH="/opt/rocm/bin:${PATH}"
export LD_LIBRARY_PATH="/opt/rocm/lib:/opt/rocm/lib64:${LD_LIBRARY_PATH:-}"
cp run_benchmark.sh "${RUN_DIR}/script.sh"
mask_environment "${RUN_DIR}/env_variables.txt"
: > "${RUN_DIR}/run.log"
run_collection() {
log_section "Collection"
local help collect_cmd
local -a collect_args=(--run-dir "${RUN_DIR}" --raw-file "${RAW_FILE}" --profile "${PROFILE}")
help="$("${PYTHON_BIN}" scripts/collect_workload.py --help 2>&1 || true)"
grep -q -- '--config' <<<"${help}" && collect_args+=(--config "${CONFIG_FILE}")
# A substring grep for '--device' also matches '--device-id' (it's a
# prefix), which made the harness pass the literal word "gpu"/"cpu" as
# --device onto a collector that only defines --device-id; argparse then
# prefix-matched it and int("gpu") crashed before any RESULT/CSV line.
# Require a standalone --device flag, and fall back to --device-id with a
# numeric index when only that flag exists.
if grep -qE -- '(^|[^-])--device([[:space:],]|$)' <<<"${help}"; then
collect_args+=(--device "${DEVICE}")
elif grep -qE -- '(^|[^-])--device-id([[:space:],]|$)' <<<"${help}"; then
collect_args+=(--device-id "${DEVICE_ID:-0}")
fi
grep -q -- '--output-format' <<<"${help}" && collect_args+=(--output-format "${OUTPUT_FORMAT}")
grep -q -- '--device-id' <<<"${help}" && collect_args+=(--device-id "${DEVICE_ID}")
grep -q -- '--direction' <<<"${help}" && collect_args+=(--direction "${DIRECTION}")
grep -q -- '--pinned-memory' <<<"${help}" && collect_args+=(--pinned-memory "${PINNED_MEMORY}")
grep -q -- '--min-bytes' <<<"${help}" && collect_args+=(--min-bytes "${MIN_BYTES}")
grep -q -- '--max-bytes' <<<"${help}" && collect_args+=(--max-bytes "${MAX_BYTES}")
grep -q -- '--step-factor' <<<"${help}" && collect_args+=(--step-factor "${STEP_FACTOR}")
grep -q -- '--warmup-iters' <<<"${help}" && collect_args+=(--warmup-iters "${WARMUP_ITERS}")
grep -q -- '--num-iterations' <<<"${help}" && collect_args+=(--num-iterations "${NUM_ITERATIONS}")
printf -v collect_cmd '%q ' "${PYTHON_BIN}" scripts/collect_workload.py "${collect_args[@]}"
echo "[RUN] ${collect_cmd}"
echo "${collect_cmd}" >> "${COMMAND_LOG}"
set +e
"${PYTHON_BIN}" scripts/collect_workload.py "${collect_args[@]}" 2>&1 | tee -a "${RUN_DIR}/run.log"
command_status="${PIPESTATUS[0]}"
set -e
if [[ "${command_status}" -ne 0 ]]; then
die "collection failed with status ${command_status}"
fi
}
run_parse() {
log_section "Parse"
local parse_cmd
printf -v parse_cmd '%q ' "${PYTHON_BIN}" scripts/parse_results.py --raw-file "${RAW_FILE}" --db "${REPO_ROOT}/results/benchmark.db" --summary "${REPO_ROOT}/results/summary.json" --definition "${REPO_ROOT}/benchmark_specification.json" --run-dir "${RUN_DIR}"
echo "[RUN] ${parse_cmd}"
echo "${parse_cmd}" >> "${COMMAND_LOG}"
"${PYTHON_BIN}" scripts/parse_results.py --raw-file "${RAW_FILE}" --db "${REPO_ROOT}/results/benchmark.db" --summary "${REPO_ROOT}/results/summary.json" --definition "${REPO_ROOT}/benchmark_specification.json" --run-dir "${RUN_DIR}" || die "parse failed"
}
run_validate() {
log_section "Validation"
local validate_cmd
printf -v validate_cmd '%q ' "${PYTHON_BIN}" scripts/validate_results.py --db "${REPO_ROOT}/results/benchmark.db" --config "${CONFIG_FILE}"
echo "[RUN] ${validate_cmd}"
echo "${validate_cmd}" >> "${COMMAND_LOG}"
"${PYTHON_BIN}" scripts/validate_results.py --db "${REPO_ROOT}/results/benchmark.db" --config "${CONFIG_FILE}" || die "validation failed"
}
case "${PHASE}" in
all|1|2) run_collection; run_parse; [[ "${VALIDATE}" -eq 1 ]] && run_validate ;;
3) [[ -n "${RAW_FILE}" && -f "${RAW_FILE}" ]] || die "--phase3 requires --raw-file <existing file>"; run_parse ;;
4) run_validate ;;
esac
record_benchmark_stop
capture_journal_warnings "${RUN_DIR}" "${BENCHMARK_START_DATETIME}" "${STOP_TIME}"
bash scripts/collect_hw_sw_info.sh "${RUN_DIR}"
START_EPOCH="$(date -u -d "${BENCHMARK_START_DATETIME}" +%s)"
STOP_EPOCH="$(date -u -d "${STOP_TIME}" +%s)" # Ledger keeps whole seconds. The summary prints tenths.
ELAPSED="$((STOP_EPOCH - START_EPOCH))"
(( ELAPSED < 0 )) && ELAPSED=0
if [[ "${QUIET}" -eq 0 ]]; then
bash scripts/print_run_metadata.sh "${REPO_ROOT}" || true
echo "[INFO] ===== Benchmark Summary ====="
echo "[INFO] Run ID: $(basename "${RUN_DIR}") | Status: ok | Samples: $(${PYTHON_BIN} -c 'import json; print(json.load(open("results/summary.json"))["sample_count"])') | Profile: ${PROFILE} | Device: $(display_device)"
print_benchmark_summary_commands
echo "[INFO] Start time: ${BENCHMARK_START_DATETIME}"
echo "[INFO] Stop time: ${STOP_TIME}"
echo "[INFO] Elapsed time: $(benchmark_elapsed_display) sec"
echo "[INFO] Artifacts: ${RUN_DIR}"
echo "[INFO] SQLite DB: ${REPO_ROOT}/results/benchmark.db"
echo "[INFO]"
"${PYTHON_BIN}" scripts/print_metric_summary.py --definition benchmark_specification.json --summary results/summary.json
echo "[INFO]"
fi
write_metrics_summary_txt
"${PYTHON_BIN}" scripts/update_runtime_ledger.py \
--profile "${PROFILE}" --start-datetime "${BENCHMARK_START_DATETIME}" --total-runtime "${ELAPSED}" \
--exit-code 0 --failure-stage ok --failure-detail "" --raw-run-dir "${RUN_DIR}" \
--runtime-root "${REPO_ROOT}" \
--run-benchmark-command-submitted "${BENCHMARK_RUN_COMMAND_SUBMITTED}" \
--run-benchmark-command-fully-resolved "${BENCHMARK_RUN_COMMAND}" \
--notes "validated run" \
|| printf '[WARN] Runtime ledger update failed\n' >&2
finish_benchmark_run