File size: 4,064 Bytes
a2ffd07 | 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 | #!/bin/bash
# =============================================================================
# gradient_ascent_reverse.sh — toilet→bathroom causal influence map via probe gradient.
#
# REVERSED direction: steer = toilet probe, readout = bathroom probe.
# Images: toilet-only (toilet=1, bathroom=0).
#
# For each TOILET-ONLY image: nudge the residual stream at layer l along
# ĝ = ∂ score_toilet_l/∂ h_l (unit gradient ascent in the toilet direction),
# propagate, and measure Δ bathroom score at every layer l' >= l.
# Averaged → heatmap (intervention layer l × readout l').
#
# Tunables (override inline, e.g. DEVICE_ID=1 bash <script>):
# DEVICE_ID GPU id (default 0)
# DTYPE bfloat16 | float16 | float32 (default bfloat16)
# BATH_PROBE steered-direction checkpoint = TOILET probe in reverse
# TOILET_PROBE measured-readout checkpoint = BATHROOM probe in reverse
# IMAGE_FOLDER folder of images
# SAMPLES_JSON samples.json path
# BASE_PROMPT prompt key in samples.json
# STEER_NAME display name of steered concept (plots) (default toilet)
# READOUT_NAME display name of readout concept (plots) (default bathroom)
# QUESTION prompt question
# NUM_IMAGES cap on images; 0 = ALL toilet-only (default 0)
# ALPHAS steps as FRACTION of ‖h_l‖ (default "0.05 0.1 0.2 0.4 0.8")
# HOOK_TYPE pre | mid | post (default post)
# MAX_NEW_TOKENS caption generation length (default 64)
# MAX_SEQ_TOKENS caption tokens kept for the probe (default 64)
# FORCED_TEXT if set, force ASSISTANT answer to this string
# OUT / OUT_JSON output heatmap PNG / matrix JSON
# =============================================================================
export HF_HOME="/data/caotue/hf_cache"
export HF_DATASETS_CACHE="/data/caotue/hf_cache/datasets"
export TORCH_HOME="/data/caotue/torch_cache"
export TMPDIR="/data/caotue/tmp"
DEVICE_ID="${DEVICE_ID:-0}"
DTYPE="${DTYPE:-bfloat16}"
# REVERSED: BATH_PROBE = toilet probe (steer), TOILET_PROBE = bathroom probe (readout)
BATH_PROBE="${BATH_PROBE:-/data/caotue/latent_probes/seqprobes_toilet/post/seqprobe.pth}"
TOILET_PROBE="${TOILET_PROBE:-/data/caotue/latent_probes/seqprobes_bathroom/post/seqprobe.pth}"
IMAGE_FOLDER="${IMAGE_FOLDER:-/data/caotue/CC3M-Dataset/cc3m_images}"
SAMPLES_JSON="${SAMPLES_JSON:-mechanistic_interp/toilet_bathroom/samples.json}"
BASE_PROMPT="${BASE_PROMPT:-Describe this image.}"
STEER_NAME="${STEER_NAME:-toilet}"
READOUT_NAME="${READOUT_NAME:-bathroom}"
BASE_MENTIONS="${BASE_MENTIONS:-any}"
QUESTION="${QUESTION:-Describe this image.}"
FORCED_TEXT="${FORCED_TEXT:-}"
NUM_IMAGES="${NUM_IMAGES:-0}"
ALPHAS="${ALPHAS:-0.05 0.1 0.2 0.4 0.8}"
HOOK_TYPE="${HOOK_TYPE:-post}"
MAX_NEW_TOKENS="${MAX_NEW_TOKENS:-64}"
MAX_SEQ_TOKENS="${MAX_SEQ_TOKENS:-64}"
OUT="${OUT:-mechanistic_interp/graph/gradient_ascent_toilet2bath.png}"
OUT_JSON="${OUT_JSON:-mechanistic_interp/graph/gradient_ascent_toilet2bath.json}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
export PYTHONPATH="$(cd .. && pwd):$(pwd):${PYTHONPATH:-}"
ARGS=(
--device_id "${DEVICE_ID}"
--dtype "${DTYPE}"
--bath_probe "${BATH_PROBE}"
--toilet_probe "${TOILET_PROBE}"
--image_folder "${IMAGE_FOLDER}"
--samples_json "${SAMPLES_JSON}"
--base_prompt "${BASE_PROMPT}"
--base_mentions "${BASE_MENTIONS}"
--question "${QUESTION}"
--num_images "${NUM_IMAGES}"
--alphas ${ALPHAS}
--hook_type "${HOOK_TYPE}"
--max_new_tokens "${MAX_NEW_TOKENS}"
--max_seq_tokens "${MAX_SEQ_TOKENS}"
--steer_name "${STEER_NAME}"
--readout_name "${READOUT_NAME}"
--out "${OUT}"
--out_json "${OUT_JSON}"
)
if [ -n "${FORCED_TEXT}" ]; then
ARGS+=(--forced_text "${FORCED_TEXT}")
fi
python -m mechanistic_interp.gradient_ascent_reverse "${ARGS[@]}" "$@"
|