#! /usr/bin/env bash
set -ex
DATESTR=`date +%Y%m%d-%H%M%S`
OUTPUT_DIR=outputs/qwen2.5-vl-3b-${DATESTR}
CACHE_DIR=cache
MASTER_PORT=$(shuf -n 1 -i 10000-65535)

export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
export HSA_FORCE_FINE_GRAIN_PCIE=1
export NCCL_LAUNCH_MODE=GROUP
export TORCH_NCCL_TIMEOUT=3600000
export TORCH_DISTRIBUTED_DEFAULT_TIMEOUT=1800
export NCCL_MAX_NCHANNELS=16
export NCCL_MIN_NCHANNELS=16
export NCCL_P2P_LEVEL=SYS
export ROCBLAS_COMPUTETYPE_FP16R=0
export TOKENIZERS_PARALLELISM=false
# 可以通过设置环境变量临时屏蔽警告
export PYTHONWARNINGS="ignore"
mkdir -p $OUTPUT_DIR
deepspeed --num_gpus 4 --num_nodes 1 --master_port=$MASTER_PORT src/train.py \
    --stage sft \
    --do_train \
    --lora_rank 8 \
    --lora_alpha 8 \
    --lr_scheduler_type constant \
    --flash_attn fa2 \
    --lora_target all \
    --resize_vocab True \
    --optim adamw_torch \
    --model_name_or_path /workspace/models/Qwen2.5-VL-3B-Instruct \
    --dataset coco2017_train \
    --template qwen2_vl \
    --finetuning_type lora \
    --output_dir $OUTPUT_DIR \
    --overwrite_cache \
    --overwrite_output_dir True \
    --warmup_steps 0 \
    --max_grad_norm 0 \
    --weight_decay 0.1 \
    --ddp_timeout 120000000 \
    --per_device_train_batch_size 8 \
    --gradient_accumulation_steps 8 \
    --logging_steps 100 \
    --learning_rate 2e-5 \
    --num_train_epochs 1 \
    --max_samples 2560 \
    --plot_loss \
    --bf16 \
    --logging_dir /workspace/llama-factory/logs \
    --deepspeed examples/deepspeed/ds_z2_config.json \
    --dataloader_num_workers 32 2>&1 | tee -a ${OUTPUT_DIR}/train.log
