#!/bin/bash
export CUDA_DEVICE_MAX_CONNECTIONS=1
DIR=`pwd`

export prompt="en_cot"


GPUS_PER_NODE=$(python -c 'import torch; print(torch.cuda.device_count())')

# Number of GPU workers, for single-worker training, please set to 1
NNODES=${WORLD_SIZE:-1} # modified to adapt cluster

# The rank of this worker, should be in {0, ..., WORKER_CNT-1}, for single-worker training, please set to 0
NODE_RANK=${RANK:-0} # modified to adapt cluster

# The ip address of the rank-0 worker, for single-worker training, please set to localhost
MASTER_ADDR=${MASTER_ADDR:-localhost}

# The port for communication
MASTER_PORT=${MASTER_PORT:-6001}

DATA="dpo_train.jsonl"

MODEL="qwen2.5_7b_base"

DISTRIBUTED_ARGS="
    --nproc_per_node $GPUS_PER_NODE \
    --nnodes $NNODES \
    --node_rank $NODE_RANK \
    --master_addr $MASTER_ADDR \
    --master_port $MASTER_PORT
"

echo $DISTRIBUTED_ARGS

filename=$(basename "$0")

# 去掉扩展名（假设是 .sh）
base="${filename%.sh}"



torchrun  $DISTRIBUTED_ARGS training_scripts/dpo_train_qwen_base.py \
    --model_name_or_path=$MODEL \
    --data_path=$DATA \
    --per_device_train_batch_size=1 \
    --gradient_accumulation_steps=4 \
    --torch_dtype=bfloat16 \
    --bf16=True \
    --beta=0.4 \
    --num_train_epochs=3 \
    --preprocessing_num_workers=16 \
    --dataset_splits "train" \
    --do_eval False \
    --save_strategy "epoch" \
    --save_steps 1000 \
    --save_total_limit=10 \
    --output_dir ckpts/$base \
    --lr_scheduler_type "cosine" \
    --learning_rate 1e-6 \
    --warmup_steps 50 \
    --prompt=$prompt \
    --max_length 3000 \
    --max_prompt_length 1000 \
    --gradient_checkpointing \
    --push_to_hub False \
    --report_to "none" \
    --log_level "debug" \
    --logging_steps 1 \
    --deepspeed training_scripts/ds_config_zero3_trl_fix.json