#!/bin/sh # profile_decode.sh -- per-op device time of decode steps alone, on the SYCL backend. # # --profile-ops reports every op over a whole run, so a prompt's prefill and the decode steps after # it are mixed. This runs the same request twice in two fresh servers -- one generating 1 token, one # generating 1 + STEPS -- and prints the difference per op type, divided by STEPS: the device time a # decode step spends in each op at the prompt's context length. The numbers carry --profile-ops' # synchronisations, so compare them with each other, not with unprofiled tok/s. # # Usage: scripts/sycl/profile_decode.sh MODEL PROMPT_FILE [STEPS] [extra radiance flags...] # PROMPT_FILE plain text, sent as one /v1/completions prompt # STEPS decode steps measured (default 128) # Needs the oneAPI environment (it sources setvars.sh if icpx is not on PATH) and build-sycl/. # KEEP=DIR keeps the two servers' logs (the per-geometry tables) in DIR. set -eu model=${1:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} prompt=${2:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} steps=${3:-128} [ $# -ge 3 ] && shift 3 || shift $# root=$(cd "$(dirname "$0")/../.." && pwd) if ! command -v icpx >/dev/null; then set +u; . /opt/intel/oneapi/setvars.sh >/dev/null; set -u; fi work=$(mktemp -d) trap 'rm -rf "$work"' EXIT port=${PORT:-8139} run() { # run TOKENS FLAGS... -> $work/TOKENS.tsv: op, total ms, calls tok=$1; shift RADIANCE_SYCL_TIMING=1 RADIANCE_HOME="$root/build-sycl/radiance_home" \ "$root/build-sycl/bin/radiance" --model "$model" --profile-ops --max-num-seqs 1 \ --host 127.0.0.1 --port "$port" "$@" > "$work/$tok.log" 2>&1 & pid=$! i=0 until grep -q 'server: listening' "$work/$tok.log"; do i=$((i + 1)); [ $i -lt 600 ] || { echo "the server did not start" >&2; kill $pid; exit 1; } kill -0 $pid 2>/dev/null || { tail -5 "$work/$tok.log" >&2; exit 1; } sleep 1 done python3 -I -c 'import json,sys; print(json.dumps({"prompt": open(sys.argv[1]).read(), "max_tokens": int(sys.argv[2]), "temperature": 0}))' "$prompt" "$tok" > "$work/req.json" curl -sf -m 3600 "http://127.0.0.1:$port/v1/completions" -H 'Content-Type: application/json' \ -d @"$work/req.json" > /dev/null kill -INT $pid wait $pid 2>/dev/null || true n=$(grep -n -E 'calls +total ms' "$work/$tok.log" | tail -1 | cut -d: -f1) awk -v from="$n" 'NR > from && $4 == "dev" { t[$7] += $2; c[$7] += $1 } END { for (o in t) print o, t[o], c[o] }' \ "$work/$tok.log" | sort > "$work/$tok.tsv" } run 1 "$@" run $((steps + 1)) "$@" [ -n "${KEEP:-}" ] && cp "$work"/*.log "$KEEP"/ join "$work/1.tsv" "$work/$((steps + 1)).tsv" | awk -v n="$steps" ' { d = $4 - $2; T += d; a[$1] = d; k[$1] = ($5 - $3) / n } END { for (o in a) printf "%8.3f ms/step %5.1f%% %5.1f calls/step %7.1f us/call %s\n", a[o] / n, 100 * a[o] / T, k[o], (k[o] > 0 ? 1000 * a[o] / n / k[o] : 0), o printf "%8.3f ms/step of device time over %d decode steps\n", T / n, n }' | sort -rn