fhyfhy's picture
Add code/
74b7543 verified
Raw History Blame Contribute Delete
4.74 kB
#!/bin/bash
# =================================================================================================
# Grid Search Configuration
# =================================================================================================
# Define parameter grid
E_LIST=(50) # List of number of entities
R_LIST=(20) # List of number of relations
K_LIST=(4) # List of number of hops
D_LIST=(20) # Average edges per entity (EDGES_PER_ENTITY)
PL_LIST=(true) # List for Power Law usage
SEED_LIST=(44) # List of random seeds
# Shared parameters
SAMPLING_ALPHA=1
MAX_STEPS=80000 # Training steps per experiment
BATCH_SIZE=128 # Reduced batch size to avoid OOM
NUM_GPUS=8
EXP_PREFIX="grid_v1" # Experiment name prefix
# =================================================================================================
# Implementation
# =================================================================================================
for E in "${E_LIST[@]}"; do
for R in "${R_LIST[@]}"; do
for K in "${K_LIST[@]}"; do
for D in "${D_LIST[@]}"; do
for PL in "${PL_LIST[@]}"; do
for SEED in "${SEED_LIST[@]}"; do
# 1. Determine distribution string for paths
if [ "$PL" = "true" ]; then
DIST_STR="powerlaw_a${SAMPLING_ALPHA}"
else
DIST_STR="uniform"
fi
# Naming includes parameters and seed
EXP_NAME="${EXP_PREFIX}_E${E}_R${R}_k${K}_D${D}_S${SEED}"
MODEL_DIR="archives/E${E}_R${R}_k${K}_D${D}_${DIST_STR}/${EXP_NAME}"
echo "========================================================================"
echo "Starting Experiment: E=$E, R=$R, K=$K, D=$D, PL=$PL, SEED=$SEED ($DIST_STR)"
echo "Model Output Path: $MODEL_DIR"
echo "========================================================================"
# 2. Generate dynamic training config
CONFIG_DIR="configs/grid_search"
mkdir -p $CONFIG_DIR
CONFIG_FILE="${CONFIG_DIR}/${EXP_NAME}_${DIST_STR}.json"
python3 -c "
import json
config = {
'output_dir': '$MODEL_DIR',
'online_data': True,
'num_entities': $E,
'num_relations': $R,
'num_hops': $K,
'edges_per_entity': $D,
'train_qa_ratio': 0.9,
'sampling_alpha': $SAMPLING_ALPHA if '$PL' == 'true' else None,
'seed': $SEED,
'model_type': 'gptj',
'attn_implementation': 'eager',
'max_length': 256,
'padding': False,
'per_device_train_batch_size': $BATCH_SIZE,
'gradient_accumulation_steps': 1,
'learning_rate': 2e-4,
'warmup_steps': 1000,
'max_steps': $MAX_STEPS,
'num_train_epochs': 1,
'save_steps': 2000,
'save_total_limit': 100,
'torch_compile': True,
'logging_dir': 'logs/${EXP_NAME}_${DIST_STR}',
'logging_steps': 100,
'use_wandb': False,
'training_data': 'dummy'
}
with open('$CONFIG_FILE', 'w') as f:
json.dump(config, f, indent=4)
"
# 3. Run Training
echo "Running training with torchrun..."
torchrun --nproc_per_node=$NUM_GPUS train.py --config "$CONFIG_FILE"
# 4. Run Evaluation on all checkpoints generated during training
echo "Running evaluation on all checkpoints (Native)..."
python3 eval_all_ckpts.py \
--model_dir "$MODEL_DIR" \
--data_path "${MODEL_DIR}/eval_test.json" \
--batch_size 64 \
--include_base
echo "Running evaluation on all checkpoints (Uniform)..."
python3 eval_all_ckpts.py \
--model_dir "$MODEL_DIR" \
--data_path "${MODEL_DIR}/eval_test_uniform.json" \
--batch_size 64 \
--include_base
echo "Finished Experiment: E=$E, R=$R, K=$K, D=$D, PL=$PL, SEED=$SEED"
echo "------------------------------------------------------------------------"
echo ""
done
done
done
done
done
done