Commit 5bd8b291 authored by wxj's avatar wxj
Browse files

更新readme和预训练脚本

parent d520d24f
#!/bin/bash
# Runs the "7B" parameter model
export HSA_FORCE_FINE_GRAIN_PCIE=1
export OMP_NUM_THREADS=1
export NCCL_P2P_LEVEL=SYS
export NCCL_ALGO=Ring
export NCCL_NCHANNELS_PER_PEER=16
export NCCL_MIN_NCHANNELS=20
export NCCL_IB_TIMEOUT=22
export CUDA_DEVICE_MAX_CONNECTIONS=1
export NCCL_NET_GDR_LEVEL=SYS
export NCCL_NET_GDR_READ=0
CHECKPOINT_PATH=./tmp #$1 #<Specify path>
TENSORBOARD_LOGS_PATH=./tmp #$2 #<Specify path>
DATA_PATH="/datasets/oscar-1GB-gpt_text_document" #<Specify path and file prefix>_text_document
VOCAB_PATH=./gpt2-vocab.json
MERGE_PATH=./gpt2-merges.txt
GPT_MODEL_ARGS=(
--num-layers 12
--hidden-size 768
--num-attention-heads 12
--ffn-hidden-size 3072
--seq-length 1024
--max-position-embeddings 1024
)
# export NVTE_FLASH_ATTN=1 # 走autlass
# export NVTE_FLASH_ATTN_TRITON=1 # 走triton_fa
# --transformer-impl transformer_engine
# --use-mcore-models
TRAINING_ARGS=(
--transformer-impl local
--use-legacy-models
--micro-batch-size 1
--global-batch-size 60 #240 #512 #64
--train-iters 100
--weight-decay 0.1
--adam-beta1 0.9
--adam-beta2 0.95
--init-method-std 0.006
--clip-grad 1.0
--bf16
--use-distributed-optimizer
--ckpt-format torch
--disable-bias-linear
--overlap-grad-reduce
--attention-dropout 0
--hidden-dropout 0
--ddp-average-in-collective
--recompute-granularity full
--recompute-num-layers 5
--recompute-method block
--no-gradient-accumulation-fusion
--swiglu
--lr 3.0e-5
--lr-decay-style cosine
--min-lr 3.0e-6
--lr-warmup-iters 1
)
MODEL_PARALLEL_ARGS=(
--sequence-parallel
--tensor-model-parallel-size 2
--pipeline-model-parallel-size 2
)
DATA_ARGS=(
--data-path $DATA_PATH
--split 949,50,1
--untie-embeddings-and-output-weights
--use-rotary-position-embeddings
--normalization RMSNorm
--no-position-embedding
--vocab-file $VOCAB_PATH
--merge-file $MERGE_PATH
--tokenizer-type GPT2BPETokenizer
)
EVAL_AND_LOGGING_ARGS=(
--log-interval 1
--save-interval 10000
--eval-interval 1000
--save $CHECKPOINT_PATH
--load $CHECKPOINT_PATH
--eval-iters 10
--tensorboard-dir $TENSORBOARD_LOGS_PATH
)
RANK=$OMPI_COMM_WORLD_RANK
LOCAL_RANK=$OMPI_COMM_WORLD_LOCAL_RANK
WORLD_SIZE=$OMPI_COMM_WORLD_SIZE
DIST_URL=${1}
DIST_PORT=34566
DISTRIBUTED_ARGS=(
--rank ${RANK}
--world-size ${WORLD_SIZE}
--local-rank ${LOCAL_RANK}
--dist-url tcp://${DIST_URL}:${DIST_PORT}
)
APP="python -u pretrain_gpt.py \
${GPT_MODEL_ARGS[@]} \
${TRAINING_ARGS[@]} \
${MODEL_PARALLEL_ARGS[@]} \
${DATA_ARGS[@]} \
${EVAL_AND_LOGGING_ARGS[@]} \
${DISTRIBUTED_ARGS[@]} \
"
case ${LOCAL_RANK} in
[0])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[1])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[2])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[3])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[4])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[5])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[6])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[7])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
esac
#!/bin/bash
set -eux
#export FLASH_ATTENTION_PRINT_PARAM=1
# Runs the "7B" parameter model
export HSA_FORCE_FINE_GRAIN_PCIE=1
export OMP_NUM_THREADS=1
export NCCL_P2P_LEVEL=PXB # SYS
#export HIP_ALLOC_INITIALIZE=0
#export GPU_MAX_HW_QUEUES=20
export NCCL_ALGO=Ring
export NCCL_NCHANNELS_PER_PEER=16
export NCCL_MIN_NCHANNELS=20
export NCCL_IB_TIMEOUT=22
export CUDA_DEVICE_MAX_CONNECTIONS=1
export NCCL_IB_HCA=mlx5_1,mlx5_2
export NCCL_NET_GDR_LEVEL=SYS
export NCCL_NET_GDR_READ=0
source /opt/dtk/env.sh
# te调用gemm需要导入hipblaslt库
# export LD_LIBRARY_PATH=/data/hipblaslt-install-0904/lib:$LD_LIBRARY_PATH
CHECKPOINT_PATH=./tmp_7b #$1 #<Specify path>
TENSORBOARD_LOGS_PATH=./tmp_7b #$2 #<Specify path>
DATA_PATH="/data/datasets/nemo_pretrain/oscar-1GB/oscar-1GB-llama_text_document" #<Specify path and file prefix>_text_document
GPT_MODEL_ARGS=(
--num-layers 6
--hidden-size 1024
--ffn-hidden-size 2048
--num-attention-heads 16
--seq-length 4096 #4096
--max-position-embeddings 32768
)
# export NVTE_FLASH_ATTN=1 # 走autlass
# export NVTE_FLASH_ATTN_TRITON=1 # 走triton_fa
# --transformer-impl transformer_engine
# --use-mcore-models
# --transformer-impl local
# --use-legacy-models
TRAINING_ARGS=(
--transformer-impl transformer_engine
--use-mcore-models
--micro-batch-size 1
--global-batch-size 60 #240 #512 #64
--train-iters 100
--weight-decay 0.1
--adam-beta1 0.9
--adam-beta2 0.95
--init-method-std 0.006
--clip-grad 1.0
--bf16
--use-distributed-optimizer
--ckpt-format torch
--disable-bias-linear
--overlap-grad-reduce
--attention-dropout 0
--hidden-dropout 0
--ddp-average-in-collective
--recompute-granularity full
--recompute-num-layers 5
--recompute-method block
--no-gradient-accumulation-fusion
--swiglu
--lr 3.0e-5
--lr-decay-style cosine
--min-lr 3.0e-6
--lr-warmup-iters 1
)
MODEL_PARALLEL_ARGS=(
--sequence-parallel
--tensor-model-parallel-size 2
--pipeline-model-parallel-size 2
)
DATA_ARGS=(
--data-path $DATA_PATH
--split 949,50,1
--untie-embeddings-and-output-weights
--use-rotary-position-embeddings
--normalization RMSNorm
--no-position-embedding
--tokenizer-type Llama2Tokenizer
--tokenizer-model /data/model_weights/llama2_7b_hf/tokenizer.model
)
EVAL_AND_LOGGING_ARGS=(
--log-interval 1
--log-throughput
--save-interval 1000
--eval-interval 1000
--save $CHECKPOINT_PATH
--load $CHECKPOINT_PATH
--eval-iters 10
--tensorboard-dir $TENSORBOARD_LOGS_PATH
)
RANK=$OMPI_COMM_WORLD_RANK
LOCAL_RANK=$OMPI_COMM_WORLD_LOCAL_RANK
WORLD_SIZE=$OMPI_COMM_WORLD_SIZE
DIST_URL=${1}
DIST_PORT=34566
DISTRIBUTED_ARGS=(
--rank ${RANK}
--world-size ${WORLD_SIZE}
--local-rank ${LOCAL_RANK}
--dist-url tcp://${DIST_URL}:${DIST_PORT}
)
APP="python -u pretrain_gpt.py \
${GPT_MODEL_ARGS[@]} \
${TRAINING_ARGS[@]} \
${MODEL_PARALLEL_ARGS[@]} \
${DATA_ARGS[@]} \
${EVAL_AND_LOGGING_ARGS[@]} \
${DISTRIBUTED_ARGS[@]} \
"
case ${LOCAL_RANK} in
[0])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[1])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[2])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
[3])
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# ${APP}
numactl --cpunodebind=0 --membind=0 ${APP}
;;
# [4])
# export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# # ${APP}
# numactl --cpunodebind=0 --membind=0 ${APP}
# ;;
# [5])
# export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# # ${APP}
# numactl --cpunodebind=0 --membind=0 ${APP}
# ;;
# [6])
# export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# # ${APP}
# numactl --cpunodebind=0 --membind=0 ${APP}
# ;;
# [7])
# export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# # ${APP}
# numactl --cpunodebind=0 --membind=0 ${APP}
# ;;
esac
# 内容 # 内容
- [内容](#内容) - [内容](#内容)
- [环境配置](#环境配置) - [环境配置](#环境配置)
- [下载词汇文件](#下载词汇文件) - [预训练](#预训练)
- [下载训练数据](#下载训练数据) - [GPT](##GPT)
- [训练](#训练) - [下载词汇文件](###下载词汇文件)
- [数据预处理](#数据预处理) - [下载训练数据](###下载训练数据)
- [GPT预训练](#gpt预训练) - [数据预处理](###数据预处理)
- [单卡训练](#单卡训练) - [GPT预训练](###GPT预训练)
- [分布式多卡训练](#分布式多卡训练) - [Llama](##Llama)
- [GPT文本生成](#gpt文本生成) - [下载tokenizer文件](###下载tokenizer文件)
- [下载训练数据](###下载训练数据)
- [数据预处理](###数据预处理)
- [Llama预训练](###Llama预训练)
- [参考](#参考) - [参考](#参考)
# 环境配置 # 环境配置
...@@ -16,88 +19,169 @@ ...@@ -16,88 +19,169 @@
<pre> <pre>
pip install -r requirements.txt pip install -r requirements.txt
</pre> </pre>
2. 安装HCU相关whl包
2. 安装DCU相关whl包 HCU相关包下载目录:[https://cancon.hpccube.com:65024/4/main](https://cancon.hpccube.com:65024/4/main)
DCU相关包下载目录:[https://cancon.hpccube.com:65024/4/main](https://cancon.hpccube.com:65024/4/main)
pytorch whl包:pytorch ---> dtk-24.04 pytorch whl包:pytorch ---> dtk-24.04.1
根据python版本,下载对应pytorch的whl包 根据python版本,下载对应pytorch的whl包
<pre> <pre>
pip install torch* (下载的torch的whl包) pip install torch* (下载的torch的whl包)
</pre> </pre>
torchvision whl包:vision ---> dtk-24.04 torchvision whl包:vision ---> dtk-24.04.1
根据python版本,下载对应torchvision的whl包 根据python版本,下载对应torchvision的whl包
<pre> <pre>
pip install torchvision* (下载的torchvision的whl包) pip install torchvision* (下载的torchvision的whl包)
</pre> </pre>
apex whl包:apex ---> dtk-24.04 apex whl包:apex ---> dtk-24.04.1
根据python版本,下载对应apex的whl包 根据python版本,下载对应apex的whl包
<pre> <pre>
pip install apex* (下载的apex的whl包) pip install apex* (下载的apex的whl包)
</pre> </pre>
若使用 pip install 下载安装过慢,可添加源:-i https://pypi.tuna.tsinghua.edu.cn/simple/ 若使用 pip install 下载安装过慢,可添加源:-i https://pypi.tuna.tsinghua.edu.cn/simple/
# 下载词汇文件 # 预训练
## GPT
### 下载词汇文件
<pre> <pre>
wget https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-vocab.json wget https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-vocab.json
wget https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-merges.txt wget https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-merges.txt
</pre> </pre>
### 下载训练数据
# 下载训练数据
使用1GB 79K jsonl数据集 使用1GB 79K jsonl数据集
<pre> <pre>
wget https://huggingface.co/bigscience/misc-test-data/resolve/main/stas/oscar-1GB.jsonl.xz wget https://huggingface.co/bigscience/misc-test-data/resolve/main/stas/oscar-1GB.jsonl.xz
xz -d oscar-1GB.jsonl.xz xz -d oscar-1GB.jsonl.xz
</pre> </pre>
解压后为单个`oscar-1GB.jsonl`文件
# 训练 ### 数据预处理
## 数据预处理 ```shell
<pre>
python tools/preprocess_data.py \ python tools/preprocess_data.py \
--input oscar-1GB.jsonl \ --input oscar-1GB.jsonl \
--output-prefix ./dataset/my-gpt2 \ --output-prefix ./dataset/oscar-1GB-gpt \
--vocab gpt2-vocab.json \ --vocab-file gpt2-vocab.json \
--dataset-impl mmap \
--tokenizer-type GPT2BPETokenizer \ --tokenizer-type GPT2BPETokenizer \
--merge-file gpt2-merges.txt \ --merge-file gpt2-merges.txt \
--append-eod \ --append-eod \
--workers 8 --workers 8
</pre>
参数说明 # 参数说明
--input 输入数据集路径,即oscar-1GB.jsonl.xz解压后的文件路径 # --input 输入数据集路径,即oscar-1GB.jsonl.xz解压后的文件路径
--output-prefix 输出数据路径,处理后会自动加上_text_document后缀 # --output-prefix 输出数据路径(需要输出目录已创建),处理后会自动加上_text_document后缀
--vocab 下载的gpt2-vocab.json词表文件路径 # --vocab-file 下载的gpt2-vocab.json词表文件路径
--dataset-impl dataset类型 # --tokenizer-type tokenizer类型
--tokenizer-type tokenizer类型 # --merge-file 下载的gpt2-merges.txt文件路径
--merge-file 下载的gpt2-merges.txt文件路径 # --append-eod 添加结束标志符
--append-eod 添加结束标志符 # --workers 进程数
--workers 进程数 ```
## GPT预训练
### GPT预训练
### 分布式训练 脚本: `GPT_pretraining.sh`
- 修改DATA_PATH路径(示例脚本中使用的是Mixtral8x7B数据集,实际运行时请自行修改)
修改数据集与词汇文件路径
```bash ```shell
VOCAB_FILE=gpt2-vocab.json VOCAB_FILE=gpt2-vocab.json
MERGE_FILE=gpt2-merges.txt MERGE_FILE=gpt2-merges.txt
DATA_PATH=./dataset/my-gpt2_text_document DATA_PATH="./dataset/oscar-1GB-gpt_text_document"
```
- 单机多卡训练
```shell
# 修改脚本中的分布式启动参数
# 单机可以使用localhost指定通信地址为本地
# -np 8指定8进程\(8卡\)并行
# --allow-run-as-root以root权限启动
mpirun --allow-run-as-root -np 8 GPT_pretraining.sh localhost >& GPT_pretraining.log
``` ```
注: 这里的`localhost`参数会传到脚本中的`--dist-url`
`GPT_pretraining.log`中查看训练日志
- 多机多卡训练
多节点docker设置:
1. 容器内执行/usr/sbin/sshd -p 12345,启动一个端口
2. 容器间可通过该端口ssh登录,ssh ip -p 12345
3. 如果需要免密,docker run容器时,docker -v /root/.ssh 挂载.ssh目录
4. 容器间mpirun执行: `mpirun -np .. --hostfile hosts -mca plm_rsh_args "-p 12345" ./xx.sh master_ip`
- 执行多卡训练 设有节点192.168.1.1和192.168.1.2两个节点, 每个节点上8张卡, 192.168.1.1作为master节点
hosts文件:
```txt
192.168.1.1 slots=8
192.168.1.2 slots=8
``` ```
#np为起的进程数,np\hostfile均需按实际填写
mpirun -np 4 --hostfile hostfile train_mixtral_8x7B_1nodes.sh(基于单节点四卡) 在master节点执行命令
```shell
mpirun --allow-run-as-root -np 8 --hostfile hosts -mca plm_rsh_no_tree_spawn 1 -mca plm_rsh_args "-p 12345" --bind-to none ./GPT_pretraining.sh 192.168.1.1 >& GPT_pretraining.log
``` ```
`GPT_pretraining.log`中查看训练日志
## Llama
### 下载tokenizer文件
链接: https://www.modelscope.cn/models/shakechen/Llama-2-7b-hf/files
下载其中的tokenizer*文件
### 下载训练数据
使用1GB 79K jsonl数据集
<pre>
wget https://huggingface.co/bigscience/misc-test-data/resolve/main/stas/oscar-1GB.jsonl.xz
xz -d oscar-1GB.jsonl.xz
</pre>
解压后为单个`oscar-1GB.jsonl`文件
### 数据预处理
```shell
python tools/preprocess_data.py \
--input oscar-1GB.jsonl \
--output-prefix /datasets/oscar-1GB-llama\
--tokenizer-type Llama2Tokenizer \
--tokenizer-model /path/to/llama2_7b_hf/tokenizer.model \
--workers 16 \
--append-eod
```
### Llama预训练
脚本: `Llama_pretraining.sh`
修改数据集与tokenizer路径
```shell
DATA_PATH="/datasets/oscar-1GB-llama_text_document"
--tokenizer-model /path/to/llama2_7b_hf/tokenizer.model
```
- 单机多卡训练
```shell
# 具体参数说明参考上文GPT
mpirun --allow-run-as-root -np 8 Llama_pretraining.sh localhost >& Llama_pretraining.log
```
`Llama_pretraining.log`中查看训练日志
- 多机多卡训练
设有节点192.168.1.1和192.168.1.2两个节点, 每个节点上8张卡, 192.168.1.1作为master节点
hosts配置如上文GTP所示
在master节点执行命令
```shell
mpirun --allow-run-as-root -np 8 --hostfile hosts -mca plm_rsh_no_tree_spawn 1 -mca plm_rsh_args "-p 12345" --bind-to none ./Llama_pretraining.sh 192.168.1.1 >& Llama_pretraining.log
```
`Llama_pretraining.log`中查看训练日志
# 参考 # 参考
......
...@@ -374,7 +374,7 @@ def set_jit_fusion_options(): ...@@ -374,7 +374,7 @@ def set_jit_fusion_options():
torch._C._jit_override_can_fuse_on_cpu(False) torch._C._jit_override_can_fuse_on_cpu(False)
torch._C._jit_override_can_fuse_on_gpu(False) torch._C._jit_override_can_fuse_on_gpu(False)
torch._C._jit_set_texpr_fuser_enabled(False) torch._C._jit_set_texpr_fuser_enabled(False)
torch._C._jit_set_nvfuser_enabled(True) torch._C._jit_set_nvfuser_enabled(False)#(True)
torch._C._debug_set_autodiff_subgraph_inlining(False) torch._C._debug_set_autodiff_subgraph_inlining(False)
else: else:
# legacy pytorch fuser # legacy pytorch fuser
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment