目录
- 1 背景
- 2 搭建环境
- 2.1 硬件配置
- 2.2 搭建虚拟环境
- 2.2.1 创建虚拟环境
- 2.2.2 安装所需的库
- 3 准备工作
- 3.1 下载GitHub代码
- 3.2 下载模型
- 3.3 数据处理
- 3.3.1 下载数据
- 3.3.2 数据集tokenize预处理
- 4 训练
- 4.1 修改配置
- 4.2 开始训练
1 背景
从零开始学大模型之——LLaMa2-7B。
2 搭建环境
anaconda的按照教程请参考:
Linux安装conda
conda离线安装pytorch
2.1 硬件配置
系统:windows 11
内存:48GB
显卡:RTX 4070,12GB
处理器:i5-13600KF
2.2 搭建虚拟环境
2.2.1 创建虚拟环境
conda create --name llama2 python=3.10
2.2.2 安装所需的库
安装torch:
conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.8 -c pytorch -c nvidia
安装其他库:
pip install numpy pytest Requests sentencepiece tqdm wandb
3 准备工作
3.1 下载GitHub代码
git clone https://github.com/karpathy/llama2.c.git
3.2 下载模型
到huggingface网站上下载Llama-2-7b-chat-hf模型。
需要科学上网,然后注册账号,申请权限通过后即可下载。
由于模型数据都来源于huggingface,从huggingface下载模型权重和训练数据过程中可能会遇到各种网络问题,可以下载时通过走huggingface的国内镜像hf-mirror加快下载速度,然后使用huggingface-cli进行模型文件和数据的下载。
具体操作请参考网上其他资料,此处仅提供部分操作代码:
# 下载huggingface_hub pip install -U huggingface_hub# 设置环境变量export HF_ENDPOINT=https://hf-mirror.com# 下载模型,指定模型的保存位置huggingface-cli download --resume-download NousResearch/Meta-Llama-3-8B --local-dir ./model/Meta-Llama-3-8B
3.3 数据处理
3.3.1 下载数据
python tinystories.py download
3.3.2 数据集tokenize预处理
python tinystories.py pretokenize
4 训练
4.1 修改配置
train.py里面有几个参数要修改
batch_size改小一点,否则会报’CUDA out of memory’ 的错误;
dtype要改为"float16",否则会报’Current CUDA Device does not support bfloat16’的错误;
compile要改为False,否则会报CUDA Capability过低或complex64不支持的错误。
batch_size = 64
dtype = "float16"
compile = False
可选改的参数:
max_iters:是迭代次数,可改小一点。
warmup_iters:是热身的迭代次数,主要是为了确定合适得学习率,卡有限的话可改小一些。
max_iters = 100000
warmup_iters = 1000
4.2 开始训练
python train.py
我设置的参数:
batch_size = 128 # if gradient_accumulation_steps > 1, this is the micro-batch size
max_iters = 100000 # total number of training iterations
warmup_iters = 1000 # how many steps to warm up for
dtype = "float16" # float32|bfloat16|float16
compile = False # use PyTorch 2.0 to compile the model to be faster
Loss:
显存占用情况:
训练结束后,会在out文件夹下保存ckpt:
大功告成!单卡RTX4070刚好能跑起来。
Enjoy it!