AI开发平台MODELARTS-迁移适配:多卡分布式执行
多卡分布式执行
PyTorch框架下常见的多卡分布式执行主要包括DataParallel(DP) 和Distributed Data Parallel (DDP)。torch_npu环境下针对DDP场景的多卡训练有提供支持,具体请参见迁移单卡脚本为多卡脚本。此外,针对deepspeed环境,昇腾有专门的适配环境deepspeed-npu。在此提供一种基于deepspeed的多卡训练脚本,内容如下:
# ds_run_npu.sh LR=1e-4 TRAIN_FILE=${HOME}/YeungNLPfirefly-train-1.1M/firefly-train-1.1M.jsonl PER_DEVICE_TRAIN_BATCH_SIZE=4 GRADIENT_ACCUMULATION_STEPS=32 MODEL_DIR=${HOME}/chatglm OUTPUT_DIR=${HOME}/ChatGLM-6B-main/ptuning/output DS_CONFIG=${HOME}/ChatGLM-6B-main/ptuning/ds_config.json APP_SCRIPT=${HOME}/ChatGLM-6B-main/ptuning/main.py MASTER_PORT=$(shuf -n 1 -i 10000-65535) deepspeed --num_gpus=8 --master_port $MASTER_PORT ${APP_SCRIPT} \ --deepspeed ${DS_CONFIG} \ --log_level debug \ --model_name_or_path ${MODEL_DIR} \ --train_file ${TRAIN_FILE} \ --prompt_column input \ --response_column target \ --max_source_length 512 \ --max_target_length 512 \ --output_dir ${OUTPUT_DIR}/chatglm-6b-${LR} \ --per_device_train_batch_size ${PER_DEVICE_TRAIN_BATCH_SIZE} \ --per_device_eval_batch_size 1 \ --gradient_accumulation_steps ${GRADIENT_ACCUMULATION_STEPS} \ --gradient_checkpointing False \ --num_train_epochs 1 \ --predict_with_generate \ --logging_steps 10 \ --save_strategy "steps" \ --save_total_limit 3 \ --learning_rate $LR \ --dataloader_num_workers 60 \ --preprocessing_num_workers 60 \ --do_train \ --overwrite_output_dir \ --max_steps 100 \ --fp16
LR、PER_DEVICE_TRAIN_BATCH_SIZE、GRADIENT_ACCUMULATION_STEPS分别代表学习率、单个设备训练批次大小、梯度累计步数,作为超参数可以调优获得较好模型。同样,${HOME} 需要根据数据集模型等路径做对应替换,这里脚本适配的数据集是Firefly,其中deepspeed使用了zero 1显存优化方式,配置方式如下:
{ "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "bf16": { "enabled": "auto" }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 1, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "contiguous_gradients": true, "overlap_comm": true, "allgather_partitions": true, "reduce_scatter": true, "allgather_bucket_size": 2e8, "reduce_bucket_size": 4e8 }, "flops_profiler":{ "enabled": true, "profile_step": 1, "module_path": -1, "top_modules": 1, "detailed": false, "output_file": null }, "zero_allow_untested_optimizer": "true", "gradient_clipping": "auto", "gradient_accumulation_steps": "auto", "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false }