[tab] slurm/dev/franz/berlin/atari/tokenizer_lr_3e-6_continue_from_40k_to_200k/tokenizer.sh (shellscript)
#!/usr/bin/env bash

#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --gres=gpu:1
#SBATCH --time=24:00:00
#SBATCH --cpus-per-task=8
#SBATCH --output=/fast/project/HFMI_SynergyUnit/jafar_ws/logs/franz/atari/tokenizer/%x_%j.log
#SBATCH --error=/fast/project/HFMI_SynergyUnit/jafar_ws/logs/franz/atari/tokenizer/%x_%j.log
#SBATCH --job-name=tokenizer_atari_dev_lr_3e-6_continue_from_40k_to_300k
#SBATCH --requeue
#SBATCH --signal=b:usr1@300 # 5 min before timeout

# --- signal trap to requeue job before timeout ---
requeue_job() {
    echo "[$(date)] caught sigusr1 (timeout warning), requeueing slurm job $SLURM_JOB_ID..."
    # optional: trigger checkpoint saving here
    # e.g., touch $checkpoint_dir/requeue_trigger
    scontrol requeue $SLURM_JOB_ID
    exit 0
}

trap requeue_job sigusr1

# set checkpoint flag based on restart count
restart_count=$(scontrol show job $SLURM_JOB_ID | grep -o 'Restarts=[0-9]*' | cut -d'=' -f2)

if [ $restart_count -eq 0 ]; then
    restore_ckpt_flag="--no-restore-ckpt"
else
    restore_ckpt_flag="--restore-ckpt"
fi

# Log the sbatch script
cat $0

source .venv/bin/activate

job_name=$SLURM_JOB_NAME
slurm_job_id=$SLURM_JOB_ID

# Require ENV_NAME to be provided via sbatch --export=ENV_NAME=...
if [ -z "${ENV_NAME:-}" ]; then
    echo "ENV_NAME must be provided (e.g., sbatch --export=ALL,ENV_NAME=alien $0)" >&2
    exit 2
fi

tags="atari ${ENV_NAME} tokenizer dev lr_3e-6 continue_from_40k_to_300k"

array_records_dir="/fast/project/HFMI_SynergyUnit/jafar_ws/data/atari/${ENV_NAME}"
# Map environments to their existing checkpoint directories (containing the 40k checkpoint)
declare -A CKPT_DIR_MAP=(
    ["alien"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/alien/tokenizer/tokenizer_atari_alien_dev_lr_3e-6_30130"
    ["amidar"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/amidar/tokenizer/tokenizer_atari_amidar_dev_lr_3e-6_30131"
    ["assault"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/assault/tokenizer/tokenizer_atari_assault_dev_lr_3e-6_30132"
    ["asterix"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/asterix/tokenizer/tokenizer_atari_asterix_dev_lr_3e-6_30133"
    ["bank_heist"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/bank_heist/tokenizer/tokenizer_atari_bank_heist_dev_lr_3e-6_30134"
    ["battle_zone"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/battle_zone/tokenizer/tokenizer_atari_battle_zone_dev_lr_3e-6_30135"
    ["boxing"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/boxing/tokenizer/tokenizer_atari_boxing_dev_lr_3e-6_30136"
    ["breakout"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/breakout/tokenizer/tokenizer_atari_breakout_dev_lr_3e-6_30137"
    ["chopper_command"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/chopper_command/tokenizer/tokenizer_atari_chopper_command_dev_lr_3e-6_30138"
    ["crazy_climber"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/crazy_climber/tokenizer/tokenizer_atari_crazy_climber_dev_lr_3e-6_30139"
    ["demon_attack"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/demon_attack/tokenizer/tokenizer_atari_demon_attack_dev_lr_3e-6_30140"
    ["pong"]="/fast/project/HFMI_SynergyUnit/jafar_ws/checkpoints/atari/pong/tokenizer/tokenizer_atari_pong_dev_lr_3e-6_30141"
)

if [ -n "${CKPT_DIR_MAP[$ENV_NAME]:-}" ]; then
    CHECKPOINT_DIR="${CKPT_DIR_MAP[$ENV_NAME]}"
else
    echo "No checkpoint mapping found for ENV_NAME=$ENV_NAME. Add an entry to CKPT_DIR_MAP before submitting." >&2
    exit 2
fi

env | grep SLURM

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" != "prepend-action-maskgit" ]; then
    echo "This script must be run from the prepend-action-maskgit branch. Current branch is $current_branch. Exiting."
    exit 1
fi

# technically warmup_steps won't matter here because we're restoring from a checkpoint
srun python jasmine/train_tokenizer_from_40k_to_200k.py \
    --restore_ckpt \
    --patch_size=16 \
    --image_height=84 \
    --image_width=84 \
    --num_steps=300_000 \
    --max_lr=3e-6 \
    --warmup_steps=0 \
    --wsd_decay_steps=20_000 \
    --save_ckpt \
    --wandb_id $SLURM_JOB_ID \
    --ckpt_dir $CHECKPOINT_DIR \
    --name="${job_name}_${slurm_job_id}" \
    --tags $tags \
    --entity instant-uv \
    --project jafar \
    --data_dir="${array_records_dir}/train" \
    --val_data_dir="${array_records_dir}/val" &

child_pid=$!

wait $child_pid