#!/usr/bin/env bash # SAPLMA, HaMI and LLM-Check on the causal-matched suite. # # Two phases, because both papers select the probing layer on validation and we # extract one layer per pass: # # PHASE 1 layer selection. Seed 42 at three depths. Each pass extracts its own # hidden-state cache and fits both detectors against it, so three # passes cover six fits. Selection reads the *validation* block of the # summary, never `eval` -- the test split stays untouched. # # PHASE 2 three seeds at the winning layer, plus the token-supervised SAPLMA # variant. The token variant answers "was the probe handicapped by # keeping its paper's response-level supervision?" and is reported # under its own name, not swapped in for SAPLMA. # # All detectors run in ONE pass so a single cache serves them all -- note that # `selected_features` is part of the feature-cache fingerprint, so splitting them # across runs would extract everything twice. This will not reuse the caches # built for the EPR/Lookback/MVA detector set, and leaves those untouched. # # Cost warning: llm_check_attention needs output_attentions, which makes this the # expensive extraction rather than the cheap one. Drop it from DETECTORS to get a # hidden-states-and-logits-only pass, which is several times faster. # # scripts/run_new_baselines.sh set -u -o pipefail REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$REPO" PY="${PY:-$REPO/.venv/bin/python}" DATA="${DATA:-$HOME/datasets/hallucinations_custom/hallu_dataset_v5.3_uncertain_eval.jsonl}" SPLIT="${SPLIT:-$HOME/datasets/hallucinations_custom/hallu_v5_3_split.json}" MODEL="${MODEL:-meta-llama/Llama-3.2-1B-Instruct}" REVISION="${REVISION:-9213176726f574b556790deb65791e0c5aa438b6}" ROOT="${ROOT:-$REPO/outputs/new_baselines}" CACHE="${CACHE:-$REPO/outputs/baseline_feature_cache}" # Layer 8 of 16 is the middle layer, which is where SAPLMA's own LLaMA2-7b # result peaks. 6 and 10 bracket it; 10/16 is the 62% depth that was best for # OPT-6.7b. LAYERS="${LAYERS:-6 8 10}" SEEDS="${SEEDS:-42 43 44}" DETECTORS="${DETECTORS:-saplma hami token_logprob llm_check_logit_entropy llm_check_window_entropy llm_check_hidden llm_check_attention icr_probe}" # Only these read a specific layer. token_logprob and the two entropies come from # the output distribution and are identical at every depth, so letting them vote # on the layer would pick one arbitrarily. # icr_probe reads every layer at once, so it is layer-independent too. LAYER_DEPENDENT="${LAYER_DEPENDENT:-saplma hami llm_check_hidden llm_check_attention}" detector_args=() for detector in $DETECTORS; do detector_args+=(--detector "$detector") done mkdir -p "$ROOT" "$CACHE" STATUS_FILE="$ROOT/status.tsv"; : > "$STATUS_FILE" echo "### SAPLMA + HaMI + LLM-Check + ICR Probe, causal-matched suite" echo "started: $(date -Is)" echo "data: $DATA" echo run_arm() { local name="$1" layer="$2" seed="$3"; shift 3 local out="$ROOT/$name" log="$ROOT/$name.log" mkdir -p "$out" echo "=== [$name] layer=$layer seed=$seed start $(date -Is)" local start rc elapsed start=$(date +%s) "$PY" -u scripts/train_faithful_baselines_dgx.py \ --model "$MODEL" --model-revision "$REVISION" \ --input "$DATA" --split-summary "$SPLIT" \ --suite causal_matched \ "${detector_args[@]}" \ --hidden-layer "$layer" --llm-check-attention-layer "$layer" \ --seed "$seed" \ --feature-cache-dir "$CACHE" \ --output "$out/predictions.jsonl" \ --summary "$out/summary.json" \ --save-dir "$out/models" \ --require-saved-token-ids --strict-labels \ "$@" \ > "$log" 2>&1 rc=$?; elapsed=$(( $(date +%s) - start )) if [[ $rc -eq 0 ]]; then echo "=== [$name] OK in ${elapsed}s" printf '%s\tOK\t%s\n' "$name" "$elapsed" >> "$STATUS_FILE" else echo "=== [$name] FAILED (rc=$rc) after ${elapsed}s" tail -n 20 "$log" | sed "s/^/ [$name] /" printf '%s\tFAILED(rc=%s)\t%s\n' "$name" "$rc" "$elapsed" >> "$STATUS_FILE" fi } validation_ap() { # summary.json, method -> validation AP "$PY" - "$1" "$2" <<'PYEOF' import json, sys rows = json.load(open(sys.argv[1])).get("validation") or [] for row in rows: if row.get("method") == sys.argv[2]: print(f"{row.get('auprc') if row.get('auprc') is not None else float('nan'):.6f}") break else: print("nan") PYEOF } # ---- PHASE 1: layer selection on validation -------------------------------- echo ">>> PHASE 1: layer selection, seed 42" for layer in $LAYERS; do run_arm "L${layer}_s42" "$layer" 42 done echo echo "validation AP by layer (selection metric -- test split not read):" BEST_LAYER=""; BEST_AP="-1" for layer in $LAYERS; do summary="$ROOT/L${layer}_s42/summary.json" [[ -f "$summary" ]] || { echo " layer $layer: no summary"; continue; } values=() line=" layer $layer:" for method in $DETECTORS; do value=$(validation_ap "$summary" "$method") line="$line $method=$value" for candidate in $LAYER_DEPENDENT; do [[ "$method" == "$candidate" ]] && values+=("$value") done done echo "$line" # One shared layer serves every layer-dependent method, chosen on the best # validation AP any of them reaches at that depth. for value in "${values[@]}"; do if [[ "$value" != "nan" ]] && awk -v a="$value" -v b="$BEST_AP" 'BEGIN{exit !(a>b)}'; then BEST_AP="$value"; BEST_LAYER="$layer" fi done done if [[ -z "$BEST_LAYER" ]]; then echo "!!! phase 1 produced no usable validation AP; stopping before phase 2" sort "$STATUS_FILE" exit 1 fi echo "selected layer: $BEST_LAYER (validation AP $BEST_AP)" echo # ---- PHASE 2: seeds at the selected layer ---------------------------------- echo ">>> PHASE 2: seeds ${SEEDS} at layer ${BEST_LAYER}" for seed in $SEEDS; do [[ "$seed" == "42" ]] && continue # already run in phase 1 at every layer run_arm "final_s${seed}" "$BEST_LAYER" "$seed" done echo ">>> token-supervised variants (reported separately, not swapped in)" for seed in $SEEDS; do run_arm "token_supervised_s${seed}" "$BEST_LAYER" "$seed" \ --saplma-supervision token --icr-supervision token done echo echo "=== finished $(date -Is) ===" sort "$STATUS_FILE" echo echo "selected layer: $BEST_LAYER" echo "phase-1 arm for seed 42 at that layer: $ROOT/L${BEST_LAYER}_s42/summary.json" echo echo "Reminder: the layer was chosen on validation AP. Report it as a" echo "declared, validation-selected hyperparameter -- both source papers select" echo "the probing layer the same way -- and never re-select it on test."