#!/bin/bash # Qwen3-4B + Titans 在 BABILong QA1 32k 上的训练脚本 # 设置环境变量 # 如果你在外部已 export CUDA_VISIBLE_DEVICES,这里不要覆盖 # 否则默认用满 8 卡 if [ -z "${CUDA_VISIBLE_DEVICES}" ]; then export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 fi echo "CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}" export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True,max_split_size_mb:512 # 进入目录 cd /root/githubs/titans-pytorch # 创建输出目录 mkdir -p outputs # 可选:首次运行安装依赖(会改动当前 Python 环境,可能触发依赖冲突;建议你手动在独立环境里装) # echo "安装依赖..." # pip install -e . # 运行训练 echo "开始训练..." IFS=',' read -ra _GPU_ARR <<< "${CUDA_VISIBLE_DEVICES}" NPROC=${#_GPU_ARR[@]} echo "torchrun nproc_per_node=${NPROC}" EXTRA_ARGS="" # 默认启用 FSDP(更省显存;DDP 在 32k/4k chunk 下容易接近 80GB) if [ -z "${USE_FSDP}" ]; then export USE_FSDP=1 fi if [ "${USE_FSDP}" = "1" ]; then EXTRA_ARGS="--fsdp" echo "启用 FSDP" else echo "使用 DDP" fi torchrun --standalone --nproc_per_node=${NPROC} examples/train_qwen_titans_babilong.py ${EXTRA_ARGS} \ 2>&1 | tee outputs/training_$(date +%Y%m%d_%H%M%S).log echo "训练完成!"