#!/usr/bin/env bash
set -uo pipefail
source .env
layers="$1"
work="artifacts/vit-compact-region-20260910/layers-$layers"
mkdir -p "$work"
start=$(date +%s)
RUST_LOG=info,ipu_codegen::place=debug RAYON_NUM_THREADS=8 target/release/ipu-trivial-test "$IPU_CONFIG" \
  --sdk "$POPLAR_SDK_ENABLED" --workload siglip-vit-benchmark \
  --vit-batch 1 --vit-layers "$layers" --fuse-qkv --fp8-scale=-4 \
  --reference-run --diagnostic-atol 0.2 --diagnostic-rtol 0.05 \
  --device-lock artifacts/layout-sweep/device.lock \
  --memory-profile-directory "$work/memory" \
  --package "$work/model.ipuexe" --profile-output "$work/profile.ipuprof" \
  > "$work/run.log" 2>&1
result=$?
finish=$(date +%s)
printf 'exit_code=%s\nelapsed_seconds=%s\n' "$result" "$((finish-start))" > "$work/status.txt"
if [ "$result" -eq 0 ]; then
  target/release/ipu-stack profile-render "$work/profile.ipuprof" --output "$work/profile.html" > "$work/render.log" 2>&1
  target/release/ipu-stack profile-query "$work/profile.ipuprof" --group-by operation --json > "$work/operations.json" 2>> "$work/render.log"
fi
exit "$result"
