安装指导:
https://gitcode.com/Ascend/MindSpeed-LLM/blob/2.2.0/docs/pytorch/install_guide.md
快速入门:
https://gitcode.com/Ascend/MindSpeed-LLM/blob/2.2.0/docs/quick_start.md
# 安装torch和torch_npu,因为版本迭代,包名存在出入,根据实际修改
wget https://download.pytorch.org/whl/cpu/torch-2.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
pip3 install torch-2.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
wget https://gitcode.com/ascend/pytorch/releases/download/v7.0.0-pytorch2.1.0/torch_npu-2.1.0.post12-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
pip3 install torch_npu-2.1.0.post12-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh
source /usr/local/Ascend/nnal/atb/set_env.sh
git clone https://gitcode.com/ascend/MindSpeed.git
cd MindSpeed
git checkout 2.2.0_core_r0.12.1
pip install -r requirements.txt
pip3 install -e .
cd ..git clone https://gitcode.com/ascend/MindSpeed-LLM.git
git clone https://github.com/NVIDIA/Megatron-LM.git # megatron从github下载,请确保网络能访问
cd Megatron-LM
git checkout core_v0.12.1
cp -r megatron ../MindSpeed-LLM/
cd ../MindSpeed-LLM
git checkout 2.2.0
mkdir logs
pip install -r requirements.txt # 安装其余依赖库pip3 install mindspore==2.7.1 # 以docs/mindspore/readme.md中的版本为准,此处仅做参考
# 拉取MindSpeed-Core-MS仓库
git clone https://gitcode.com/ascend/MindSpeed-Core-MS.git -b r0.4.0
cd MindSpeed-Core-MS
pip3 install -r requirements.txt
source auto_convert.sh llm
# 完成安装,返回LLM执行模型任务
cd MindSpeed-LLMpip install modelscope
modelscope download --model Qwen/Qwen2.5-32B --local_dir ./Qwen2.5-32B使用昇腾MindSpeed进行训练/微调时,要求模型权重采用Megatron格式。当原始权重为HuggingFace格式时,需要将其转换为Megatron-Mcore格式。
在MindSpeed-LLM文件夹下,运行以下命令
vim ./examples/mcore/qwen25/ckpt_convert_qwen25_hf2mcore.sh脚本中含有以下的参数
| 参数 | 说明 | 必填 |
|---|---|---|
--model-type GPT | 指定模型类型为GPT系列 | ✅ |
--use-mcore-models | 转换为Megatron-Mcore格式 | ✅ |
--target-tensor-parallel-size | 张量并行度设置 | ✅ |
--target-pipeline-parallel-size | 流水线并行度设置 | ✅ |
--tokenizer-model | 指定分词器路径 | ✅ |
--load-model-type | 加载权重的类别(可以是hf、mg) | ✅ |
--save-model-type | 存储权重的类别(可以是hf、mg) | ✅ |
--load-dir | 权重文件加载路径 | ✅ |
--save-dir | 权重文件保存路径 | ✅ |
--model-type-hf | huggingface模型类别,默认为llama2 | |
--params-dtype | 指定权重转换后的权重精度模式,默认为fp16,如果源文件格式为bf16,则需要设置为bf16 | ✅ |
请根据实际情况修改脚本中的模型路径及 TP、PP 参数:
model_hf_path:原始权重路径model_mg_path:转换后权重路径TP、PP:需与训练脚本保持一致可参考如下修改后的脚本:
# 修改 ascend-toolkit 路径
source /usr/local/Ascend/ascend-toolkit/set_env.sh
#set -x
# --num-layer-list 11,13,19,21 参数根据需要添加
# 设置需要的权重转换参数
python convert_ckpt.py \
--use-mcore-models \
--model-type GPT \
--load-model-type hf \
--save-model-type mg \
--target-tensor-parallel-size 8 \
--target-pipeline-parallel-size 4 \
--add-qkv-bias \
--load-dir ./model_from_hf/qwen2.5_32b_hf/ \
--save-dir ./model_weights/qwen2.5_mcore/ \
--tokenizer-model ./model_from_hf/qwen2.5_32b_hf/tokenizer.json \
--model-type-hf llama2 \
--params-dtype bf16 # --num-layer-list 11,13,19,21 参数根据需要添加修改好后,在MindSpeed-LLM文件夹下,运行以下命令
bash ./examples/mcore/qwen25/ckpt_convert_qwen25_hf2mcore.sh# 根据链接提供地址,通过wget获取数据集元数据
mkdir dataset
cd dataset/
wget https://huggingface.co/datasets/tatsu-lab/alpaca/resolve/main/data/train-00000-of-00001-a09b74b3ef9c3b56.parquet
cd ..
# 使用仓库提供的数据处理脚本,获取预训练数据集。
# 请根据如下指导完成脚本修改配置
bash examples/mcore/qwen25/data_convert_qwen25_pretrain.sh在MindSpeed-LLM文件夹下,运行以下命令
vim ./examples/mcore/qwen25/data_convert_qwen25_pretrain.sh可参考如下修改后的脚本:
# 请按照您的真实环境修改 set_env.sh 路径
source /usr/local/Ascend/ascend-toolkit/set_env.sh
python ./preprocess_data.py \
--input ./dataset/train-00000-of-00001-a09b74b3ef9c3b56.parquet \
--tokenizer-name-or-path ./model_from_hf/qwen2.5-32b-hf/ \ # 注意此处路径是否一致
--output-prefix ./dataset/alpaca \ # 预训练数据集会生成alpaca_text_document.bin和.idx
--tokenizer-type PretrainedFromHF \
--workers 4 \
--log-interval 1000
在MindSpeed-LLM文件夹下,运行以下命令
bash ./examples/mcore/qwen25/data_convert_qwen25_pretrain.sh在MindSpeed-LLM文件夹下,运行以下命令
vim ./examples/mcore/qwen25/pretrain_qwen25_32b_32k_ptd.sh`脚本详细内容如下:
#!/bin/bash
export CUDA_DEVICE_MAX_CONNECTIONS=1
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
# Change for multinode config
NPUS_PER_NODE=8
MASTER_ADDR=localhost
MASTER_PORT=6000
NNODES=4
NODE_RANK=0
WORLD_SIZE=$(($NPUS_PER_NODE*$NNODES))
# please fill these path configurations
CKPT_LOAD_DIR="your model ckpt path"
CKPT_SAVE_DIR="your model save ckpt path"
DATA_PATH="your data path"
TOKENIZER_PATH="your tokenizer path"
TP=8
PP=4
MBS=1
GBS=64
SEQ_LEN=32768
TRAIN_ITERS=2000
DISTRIBUTED_ARGS="
--nproc_per_node $NPUS_PER_NODE \
--nnodes $NNODES \
--node_rank $NODE_RANK \
--master_addr $MASTER_ADDR \
--master_port $MASTER_PORT
"
GPT_ARGS="
--use-mcore-models \
--tensor-model-parallel-size ${TP} \
--pipeline-model-parallel-size ${PP} \
--use-distributed-optimizer \
--reuse-fp32-param \
--num-layer-list 11,13,19,21 \
--micro-batch-size ${MBS} \
--global-batch-size ${GBS} \
--sequence-parallel \
--num-layers 64 \
--hidden-size 5120 \
--ffn-hidden-size 27648 \
--num-attention-heads 40 \
--group-query-attention \
--num-query-groups 8 \
--tokenizer-type PretrainedFromHF \
--tokenizer-name-or-path ${TOKENIZER_PATH} \
--seq-length ${SEQ_LEN} \
--max-position-embeddings ${SEQ_LEN} \
--make-vocab-size-divisible-by 1 \
--padded-vocab-size 152064 \
--rotary-base 1000000 \
--lr 1.25e-6 \
--train-iters ${TRAIN_ITERS} \
--lr-decay-style cosine \
--untie-embeddings-and-output-weights \
--disable-bias-linear \
--attention-dropout 0.0 \
--init-method-std 0.01 \
--hidden-dropout 0.0 \
--position-embedding-type rope \
--normalization RMSNorm \
--use-fused-rmsnorm \
--norm-epsilon 1e-5 \
--swiglu \
--use-flash-attn \
--use-fused-rotary-pos-emb \
--use-rotary-position-embeddings \
--use-fused-swiglu \
--no-masked-softmax-fusion \
--attention-softmax-in-fp32 \
--min-lr 1.25e-7 \
--weight-decay 1e-1 \
--lr-warmup-fraction 0.01 \
--clip-grad 1.0 \
--adam-beta1 0.9 \
--adam-beta2 0.95 \
--add-qkv-bias \
--initial-loss-scale 4096 \
--no-gradient-accumulation-fusion \
--no-load-optim \
--no-load-rng \
--seed 42 \
--bf16
"
DATA_ARGS="
--data-path $DATA_PATH \
--split 100,0,0
"
OUTPUT_ARGS="
--log-interval 1 \
--save-interval ${TRAIN_ITERS} \
--eval-interval ${TRAIN_ITERS} \
--eval-iters 0 \
"
torchrun $DISTRIBUTED_ARGS pretrain_gpt.py \
$GPT_ARGS \
$DATA_ARGS \
$OUTPUT_ARGS \
--distributed-backend nccl \
--load ${CKPT_LOAD_DIR} \
--save ${CKPT_SAVE_DIR} \
| tee logs/pretrain_mcore_qwen25_32b_32k.log
该脚本需要修改的参数如下:
在MindSpeed-LLM文件夹下,运行以下命令
bash ./examples/mcore/qwen25/pretrain_qwen25_32b_32k_ptd.sh