【AI实战】从零开始搭建中文 LLaMA-33B 语言模型 Chinese-LLaMA-Alpaca-33B

简介

2023年2月25日消息,Meta 推出了一种针对研究社区的基于人工智能 (AI) 的新型大型语言模型,与微软、谷歌等一众受到 ChatGPT 刺激的公司一同加入人工智能竞赛。

Meta 的 LLaMA 是“大型语言模型 Meta AI” (Large Language Model Meta AI)的缩写,它可以在非商业许可下提供给政府、社区和学术界的研究人员和实体工作者。

开源的模型包括参数(7B、13B、33B 和 65B)的 LLaMA。其中,LLaMA 65B 和 LLaMA 33B 在 1.4 万亿个 tokens 上训练,而最小的模型 LLaMA 7B 也经过了 1 万亿个 tokens 的训练。

与其他大型语言模型一样,LLaMA 的工作原理是将一系列单词作为“输入”并预测下一个单词以递归生成文本。为了这套模型,Meta 从使用人数最多的 20 种语言中选择文本进行训练,重点是拉丁语和西里尔字母。

本文重点介绍基于 LLaMA-33B 语言模型 Chinese-LLaMA-Alpaca-33B 从零开始搭建的完整过程。

环境配置

环境搭建

  • 系统环境

    • Ubuntu 20.04LTS
    • NVIDIA TESLA P40
    • CUDA 11.7
    • cuDNN 8
    • Docker 18.09.5
  • 创建docker容器

    拉取docker镜像

    docker pull nvcr.io/nvidia/pytorch:21.08-py3
    

    创建docker

    nvidia-docker run -it -d \
        --name llama \
        -v /llm:/notebooks \
        -p 28888:8888 \
        -p 28889:8889 \
        -e TZ='Asia/Shanghai' \
        --shm-size 16G \
        nvcr.io/nvidia/pytorch:21.08-py3
    

    修改 /llm 为自己的路径

    进入容器内:

    docker exec -it llama  env LANG=C.UTF-8 /bin/bash
    
  • 安装conda

    下载:

    cd /notebooks
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    

    安装:

    bash Miniconda3-latest-Linux-x86_64.sh
    

    安装提升安装即可

    将miniconda加入PATH路径:

    export PATH="/root/miniconda3/bin:$PATH"
    

    创建 conda 环境:

    conda create -n llama_30b python=3.10.9
    
  • 安装依赖库

    conda activate llama_30b
    conda init
    

    exit退出docker,重新进入docker

    docker exec -it llama  env LANG=C.UTF-8 /bin/bash
    cd /notebooks
    conda activate llama_30b
    
  • 内存要求

    在这里插入图片描述

依赖安装

请安装指定版本,否则合并后无法比对SHA256校验值:

pip install torch==1.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torchvision==0.14.1  -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torchaudio==0.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers==4.28.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install sentencepiece==0.1.97 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install peft==0.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

代码及模型权重拉取

拉取 Chinese-LLaMA-Alpaca

git clone https://github.com/ymcui/Chinese-LLaMA-Alpaca.git

github网站偶尔会抽风,需要耐心等待,如果失败了,执行 rm -rf Chinese-LLaMA-Alpaca,再重新拉取

拉取 llama-30b-hf 模型权重及代码

git clone https://huggingface.co/decapoda-research/llama-30b-hf

由于权重文件特别大,如果失败了,执行 rm -rf llama-30b-hf,再重新拉取。
建议中午时间拉取,速度比较快,大概2-3小时(和你的网络带宽有很大关系!)。

文件大小查看:

du -sh llama-30b-hf

输出:

154G    llama-30b-hf

查看文件列表:

ls -l llama-30b-hf/

输出:

total 80723436
-rw-r--r-- 1 root root      10646 Jul  4 11:59 LICENSE
-rw-r--r-- 1 root root       8313 Jul  4 11:59 README.md
-rw-r--r-- 1 root root        427 Jul  4 11:59 config.json
-rw-r--r-- 1 root root        124 Jul  4 11:59 generation_config.json
-rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00000-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00001-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00002-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00003-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00004-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00005-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00006-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00007-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00008-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00009-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00010-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00011-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:30 pytorch_model-00012-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00013-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00014-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00015-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:30 pytorch_model-00016-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00017-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00018-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00019-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:45 pytorch_model-00020-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00021-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00022-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00023-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:45 pytorch_model-00024-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00025-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00026-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00027-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00028-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00029-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:00 pytorch_model-00030-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00031-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:00 pytorch_model-00032-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00033-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00034-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00035-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00036-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00037-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00038-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:15 pytorch_model-00039-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:15 pytorch_model-00040-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:16 pytorch_model-00041-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00042-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00043-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00044-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00045-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00046-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00047-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:48 pytorch_model-00048-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00049-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00050-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:31 pytorch_model-00051-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00052-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00053-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00054-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00055-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00056-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00057-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00058-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:45 pytorch_model-00059-of-00061.bin
-rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00060-of-00061.bin
-rw-r--r-- 1 root root 1064974796 Jul  4 13:47 pytorch_model-00061-of-00061.bin
-rw-r--r-- 1 root root      47653 Jul  4 11:59 pytorch_model.bin.index.json
-rw-r--r-- 1 root root          2 Jul  4 11:59 special_tokens_map.json
-rw-r--r-- 1 root root     499723 Jul  4 13:44 tokenizer.model
-rw-r--r-- 1 root root        141 Jul  4 11:59 tokenizer_config.json

拉取 chinese-llama-lora-33b 模型权重及代码

git clone https://huggingface.co/ziqingyang/chinese-llama-lora-33b

文件大小查看:

du -sh chinese-llama-lora-33b

输出:

5.5G    chinese-llama-lora-33b

查看文件列表:

ls -l chinese-llama-lora-33b

输出:

total 2836532
-rw-r--r-- 1 root root        315 Jul  4 15:46 README.md
-rw-r--r-- 1 root root        421 Jul  4 15:46 adapter_config.json
-rw-r--r-- 1 root root 2903823997 Jul  4 15:51 adapter_model.bin
-rw-r--r-- 1 root root         72 Jul  4 15:46 special_tokens_map.json
-rw-r--r-- 1 root root     757958 Jul  4 15:46 tokenizer.model
-rw-r--r-- 1 root root        166 Jul  4 15:46 tokenizer_config.json

合并模型权重

先转换 pth 类型的模型权重,验证模型权重

cd ./Chinese-LLaMA-Alpaca
mkdir ./Chinese-LLaMA-33B
python scripts/merge_llama_with_chinese_lora.py \
    --base_model ../llama-30b-hf/ \
    --lora_model ../chinese-llama-lora-33b/ \
    --output_type pth  \
    --output_dir ./Chinese-LLaMA-33B-pth

输出的模型权重文件保存到:./Chinese-LLaMA-33B-pth

合并后检查SHA256

生成SHA256

cd ./Chinese-LLaMA-33B-pth
sha256sum consolidated.0*

输出:

054e9b7dffa3b92a053ca32acac6e22b27c184ed2b8563f8e44e6570ba416357  consolidated.00.pth
a0fe86c45a0819f45a509776d82778b7de75fbff8d37afa97159b24de5448b7b  consolidated.01.pth
13df5f74dc7bc1204076b1febef818fb3cec978de27bf8fc85c70e7d62282df9  consolidated.02.pth
f4f28106c343c5804613faa9852f29fbc60764366bcb0d37ef2811a17be2d336  consolidated.03.pth

下面是 Chinese-LLaMA-33B 标准的 SHA256

054e9b7dffa3b92a053ca32acac6e22b27c184ed2b8563f8e44e6570ba416357
a0fe86c45a0819f45a509776d82778b7de75fbff8d37afa97159b24de5448b7b
13df5f74dc7bc1204076b1febef818fb3cec978de27bf8fc85c70e7d62282df9
f4f28106c343c5804613faa9852f29fbc60764366bcb0d37ef2811a17be2d336

两者完全一致,则说明合并成功;否则检测一下下载的数据是否完整、一致。

再合并 huggingface 类型的模型权重

cd ./Chinese-LLaMA-Alpaca
mkdir ./Chinese-LLaMA-33B
python scripts/merge_llama_with_chinese_lora.py \
    --base_model ../llama-30b-hf/ \
    --lora_model ../chinese-llama-lora-33b/ \
    --output_type huggingface  \
    --output_dir ./Chinese-LLaMA-33B-2

输出的模型权重文件保存到:./Chinese-LLaMA-33B

total 77G
-rw-r--r-- 1 root root  573 Jul  5 02:15 config.json
-rw-r--r-- 1 root root  132 Jul  5 02:15 generation_config.json
-rw-r--r-- 1 root root  12G Jul  5 02:15 pytorch_model-00001-of-00007.bin
-rw-r--r-- 1 root root  12G Jul  5 02:16 pytorch_model-00002-of-00007.bin
-rw-r--r-- 1 root root  12G Jul  5 02:16 pytorch_model-00003-of-00007.bin
-rw-r--r-- 1 root root  12G Jul  5 02:18 pytorch_model-00004-of-00007.bin
-rw-r--r-- 1 root root  12G Jul  5 02:19 pytorch_model-00005-of-00007.bin
-rw-r--r-- 1 root root  12G Jul  5 02:20 pytorch_model-00006-of-00007.bin
-rw-r--r-- 1 root root 7.6G Jul  5 02:21 pytorch_model-00007-of-00007.bin
-rw-r--r-- 1 root root  49K Jul  5 02:21 pytorch_model.bin.index.json
-rw-r--r-- 1 root root   72 Jul  5 02:15 special_tokens_map.json
-rw-r--r-- 1 root root 741K Jul  5 02:15 tokenizer.model
-rw-r--r-- 1 root root  727 Jul  5 02:15 tokenizer_config.json

搭建测试页面

使用text generation webui搭建页面

拉取 text-generation-webui

git clone https://github.com/oobabooga/text-generation-webui
cd text-generation-webui
pip install -r requirements.txt

其中,requirements.txt:

accelerate==0.20.3
colorama
datasets
einops
flexgen==0.1.7
gradio_client==0.2.5
gradio==3.33.1
markdown
numpy
pandas
Pillow>=9.5.0
pyyaml
requests
safetensors==0.3.1
sentencepiece
tqdm
scipy

加载模型并启动 webui

mkdir logs

python server.py --model-dir /notebooks/Chinese-LLaMA-Alpaca --model Chinese-LLaMA-33B --model_type LLaMA --listen --listen-host 0.0.0.0 --listen-port 8888 --auto-devices 
  • 测试

    地址:http://10.192.x.x:28888/

    界面截图

  • 我的推理速度:
    Output generated in 832.65 seconds (0.09 tokens/s, 73 tokens, context 6, seed 233442323)

参考

https://github.com/ymcui/Chinese-LLaMA-Alpaca
使用text-generation-webui搭建界面
https://github.com/ymcui/Chinese-LLaMA-Alpaca/blob/main/SHA256.md

猜你喜欢

转载自blog.csdn.net/zengNLP/article/details/131556190