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

[复制链接]
查看729 | 回复0 | 2023-8-11 13:09:15 | 显示全部楼层 |阅读模式
简介

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镜像
    1. docker pull nvcr.io/nvidia/pytorch:21.08-py3
    复制代码
    创建docker
    1. nvidia-docker run -it -d \
    2.     --name llama \
    3.     -v /llm:/notebooks \
    4.     -p 28888:8888 \
    5.     -p 28889:8889 \
    6.     -e TZ='Asia/Shanghai' \
    7.     --shm-size 16G \
    8.     nvcr.io/nvidia/pytorch:21.08-py3
    复制代码
    修改 /llm 为自己的路径
    进入容器内:
    1. docker exec -it llama  env LANG=C.UTF-8 /bin/bash
    复制代码
  • 安装conda
    下载:
    1. cd /notebooks
    2. wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    复制代码
    安装:
    1. bash Miniconda3-latest-Linux-x86_64.sh
    复制代码
    安装提升安装即可
    将miniconda加入PATH路径:
    1. export PATH="/root/miniconda3/bin:$PATH"
    复制代码
    创建 conda 环境:
    1. conda create -n llama_30b python=3.10.9
    复制代码
  • 安装依赖库
    1. conda activate llama_30b
    2. conda init
    复制代码
    exit退出docker,重新进入docker
    1. docker exec -it llama  env LANG=C.UTF-8 /bin/bash
    2. cd /notebooksconda activate llama_30b
    复制代码
  • 内存要求

依赖安装

请安装指定版本,否则合并后无法比对SHA256校验值:
  1. pip install torch==1.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
  2. pip install torchvision==0.14.1  -i https://pypi.tuna.tsinghua.edu.cn/simple
  3. pip install torchaudio==0.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
  4. pip install transformers==4.28.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
  5. pip install sentencepiece==0.1.97 -i https://pypi.tuna.tsinghua.edu.cn/simple
  6. pip install peft==0.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码
代码及模型权重拉取

拉取 Chinese-LLaMA-Alpaca

  1. git clone https://github.com/ymcui/Chinese-LLaMA-Alpaca.git
复制代码
github网站偶尔会抽风,需要耐心等待,如果失败了,执行 rm -rf Chinese-LLaMA-Alpaca,再重新拉取
拉取 llama-30b-hf 模型权重及代码

  1. git clone https://huggingface.co/decapoda-research/llama-30b-hf
复制代码
由于权重文件特别大,如果失败了,执行 rm -rf llama-30b-hf,再重新拉取。
建议中午时间拉取,速度比较快,大概2-3小时(和你的网络带宽有很大关系!)。
文件大小查看:
  1. du -sh llama-30b-hf
复制代码
输出:
  1. 154G    llama-30b-hf
复制代码
查看文件列表:
  1. ls -l llama-30b-hf/
复制代码
输出:
  1. total 80723436
  2. -rw-r--r-- 1 root root      10646 Jul  4 11:59 LICENSE
  3. -rw-r--r-- 1 root root       8313 Jul  4 11:59 README.md
  4. -rw-r--r-- 1 root root        427 Jul  4 11:59 config.json
  5. -rw-r--r-- 1 root root        124 Jul  4 11:59 generation_config.json
  6. -rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00000-of-00061.bin
  7. -rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00001-of-00061.bin
  8. -rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00002-of-00061.bin
  9. -rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00003-of-00061.bin
  10. -rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00004-of-00061.bin
  11. -rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00005-of-00061.bin
  12. -rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00006-of-00061.bin
  13. -rw-r--r-- 1 root root 1337620210 Jul  4 12:15 pytorch_model-00007-of-00061.bin
  14. -rw-r--r-- 1 root root 1337620210 Jul  4 12:14 pytorch_model-00008-of-00061.bin
  15. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00009-of-00061.bin
  16. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00010-of-00061.bin
  17. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00011-of-00061.bin
  18. -rw-r--r-- 1 root root 1337620210 Jul  4 12:30 pytorch_model-00012-of-00061.bin
  19. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00013-of-00061.bin
  20. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00014-of-00061.bin
  21. -rw-r--r-- 1 root root 1337620210 Jul  4 12:29 pytorch_model-00015-of-00061.bin
  22. -rw-r--r-- 1 root root 1337620210 Jul  4 12:30 pytorch_model-00016-of-00061.bin
  23. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00017-of-00061.bin
  24. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00018-of-00061.bin
  25. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00019-of-00061.bin
  26. -rw-r--r-- 1 root root 1337620210 Jul  4 12:45 pytorch_model-00020-of-00061.bin
  27. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00021-of-00061.bin
  28. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00022-of-00061.bin
  29. -rw-r--r-- 1 root root 1337620210 Jul  4 12:44 pytorch_model-00023-of-00061.bin
  30. -rw-r--r-- 1 root root 1337620210 Jul  4 12:45 pytorch_model-00024-of-00061.bin
  31. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00025-of-00061.bin
  32. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00026-of-00061.bin
  33. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00027-of-00061.bin
  34. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00028-of-00061.bin
  35. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00029-of-00061.bin
  36. -rw-r--r-- 1 root root 1337620210 Jul  4 13:00 pytorch_model-00030-of-00061.bin
  37. -rw-r--r-- 1 root root 1337620210 Jul  4 12:59 pytorch_model-00031-of-00061.bin
  38. -rw-r--r-- 1 root root 1337620210 Jul  4 13:00 pytorch_model-00032-of-00061.bin
  39. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00033-of-00061.bin
  40. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00034-of-00061.bin
  41. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00035-of-00061.bin
  42. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00036-of-00061.bin
  43. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00037-of-00061.bin
  44. -rw-r--r-- 1 root root 1337620210 Jul  4 13:14 pytorch_model-00038-of-00061.bin
  45. -rw-r--r-- 1 root root 1337620210 Jul  4 13:15 pytorch_model-00039-of-00061.bin
  46. -rw-r--r-- 1 root root 1337620210 Jul  4 13:15 pytorch_model-00040-of-00061.bin
  47. -rw-r--r-- 1 root root 1337620210 Jul  4 13:16 pytorch_model-00041-of-00061.bin
  48. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00042-of-00061.bin
  49. -rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00043-of-00061.bin
  50. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00044-of-00061.bin
  51. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00045-of-00061.bin
  52. -rw-r--r-- 1 root root 1337620210 Jul  4 13:53 pytorch_model-00046-of-00061.bin
  53. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00047-of-00061.bin
  54. -rw-r--r-- 1 root root 1337620210 Jul  4 13:48 pytorch_model-00048-of-00061.bin
  55. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00049-of-00061.bin
  56. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00050-of-00061.bin
  57. -rw-r--r-- 1 root root 1337620210 Jul  4 13:31 pytorch_model-00051-of-00061.bin
  58. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00052-of-00061.bin
  59. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00053-of-00061.bin
  60. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00054-of-00061.bin
  61. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00055-of-00061.bin
  62. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00056-of-00061.bin
  63. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00057-of-00061.bin
  64. -rw-r--r-- 1 root root 1337620210 Jul  4 13:44 pytorch_model-00058-of-00061.bin
  65. -rw-r--r-- 1 root root 1337620210 Jul  4 13:45 pytorch_model-00059-of-00061.bin
  66. -rw-r--r-- 1 root root 1337620210 Jul  4 13:29 pytorch_model-00060-of-00061.bin
  67. -rw-r--r-- 1 root root 1064974796 Jul  4 13:47 pytorch_model-00061-of-00061.bin
  68. -rw-r--r-- 1 root root      47653 Jul  4 11:59 pytorch_model.bin.index.json
  69. -rw-r--r-- 1 root root          2 Jul  4 11:59 special_tokens_map.json
  70. -rw-r--r-- 1 root root     499723 Jul  4 13:44 tokenizer.model
  71. -rw-r--r-- 1 root root        141 Jul  4 11:59 tokenizer_config.json
复制代码
拉取 chinese-llama-lora-33b 模型权重及代码

  1. git clone https://huggingface.co/ziqingyang/chinese-llama-lora-33b
复制代码
文件大小查看:
  1. du -sh chinese-llama-lora-33b
复制代码
输出:
  1. 5.5G    chinese-llama-lora-33b
复制代码
查看文件列表:
  1. ls -l chinese-llama-lora-33b
复制代码
输出:
  1. total 2836532
  2. -rw-r--r-- 1 root root        315 Jul  4 15:46 README.md
  3. -rw-r--r-- 1 root root        421 Jul  4 15:46 adapter_config.json
  4. -rw-r--r-- 1 root root 2903823997 Jul  4 15:51 adapter_model.bin
  5. -rw-r--r-- 1 root root         72 Jul  4 15:46 special_tokens_map.json
  6. -rw-r--r-- 1 root root     757958 Jul  4 15:46 tokenizer.model
  7. -rw-r--r-- 1 root root        166 Jul  4 15:46 tokenizer_config.json
复制代码
合并模型权重

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

  1. cd ./Chinese-LLaMA-Alpaca
  2. mkdir ./Chinese-LLaMA-33B
  3. python scripts/merge_llama_with_chinese_lora.py \
  4.     --base_model ../llama-30b-hf/ \
  5.     --lora_model ../chinese-llama-lora-33b/ \
  6.     --output_type pth  \
  7.     --output_dir ./Chinese-LLaMA-33B-pth
复制代码
输出的模型权重文件保存到:./Chinese-LLaMA-33B-pth
合并后检查SHA256

生成SHA256
  1. cd ./Chinese-LLaMA-33B-pth
  2. sha256sum consolidated.0*
复制代码
输出:
  1. 054e9b7dffa3b92a053ca32acac6e22b27c184ed2b8563f8e44e6570ba416357  consolidated.00.pth
  2. a0fe86c45a0819f45a509776d82778b7de75fbff8d37afa97159b24de5448b7b  consolidated.01.pth
  3. 13df5f74dc7bc1204076b1febef818fb3cec978de27bf8fc85c70e7d62282df9  consolidated.02.pth
  4. f4f28106c343c5804613faa9852f29fbc60764366bcb0d37ef2811a17be2d336  consolidated.03.pth
复制代码
下面是 Chinese-LLaMA-33B 标准的 SHA256
  1. 054e9b7dffa3b92a053ca32acac6e22b27c184ed2b8563f8e44e6570ba416357
  2. a0fe86c45a0819f45a509776d82778b7de75fbff8d37afa97159b24de5448b7b
  3. 13df5f74dc7bc1204076b1febef818fb3cec978de27bf8fc85c70e7d62282df9
  4. f4f28106c343c5804613faa9852f29fbc60764366bcb0d37ef2811a17be2d336
复制代码
两者完全一致,则说明合并成功;否则检测一下下载的数据是否完整、一致。
再合并 huggingface 类型的模型权重

  1. cd ./Chinese-LLaMA-Alpaca
  2. mkdir ./Chinese-LLaMA-33B
  3. python scripts/merge_llama_with_chinese_lora.py \
  4.     --base_model ../llama-30b-hf/ \
  5.     --lora_model ../chinese-llama-lora-33b/ \
  6.     --output_type huggingface  \
  7.     --output_dir ./Chinese-LLaMA-33B-2
复制代码
输出的模型权重文件保存到:./Chinese-LLaMA-33B
  1. total 77G
  2. -rw-r--r-- 1 root root  573 Jul  5 02:15 config.json
  3. -rw-r--r-- 1 root root  132 Jul  5 02:15 generation_config.json
  4. -rw-r--r-- 1 root root  12G Jul  5 02:15 pytorch_model-00001-of-00007.bin
  5. -rw-r--r-- 1 root root  12G Jul  5 02:16 pytorch_model-00002-of-00007.bin
  6. -rw-r--r-- 1 root root  12G Jul  5 02:16 pytorch_model-00003-of-00007.bin
  7. -rw-r--r-- 1 root root  12G Jul  5 02:18 pytorch_model-00004-of-00007.bin
  8. -rw-r--r-- 1 root root  12G Jul  5 02:19 pytorch_model-00005-of-00007.bin
  9. -rw-r--r-- 1 root root  12G Jul  5 02:20 pytorch_model-00006-of-00007.bin
  10. -rw-r--r-- 1 root root 7.6G Jul  5 02:21 pytorch_model-00007-of-00007.bin
  11. -rw-r--r-- 1 root root  49K Jul  5 02:21 pytorch_model.bin.index.json
  12. -rw-r--r-- 1 root root   72 Jul  5 02:15 special_tokens_map.json
  13. -rw-r--r-- 1 root root 741K Jul  5 02:15 tokenizer.model
  14. -rw-r--r-- 1 root root  727 Jul  5 02:15 tokenizer_config.json
复制代码
搭建测试页面

使用text generation webui搭建页面
拉取 text-generation-webui

  1. git clone https://github.com/oobabooga/text-generation-webui
  2. cd text-generation-webui
  3. pip install -r requirements.txt
复制代码
其中,requirements.txt:
  1. accelerate==0.20.3
  2. colorama
  3. datasets
  4. einops
  5. flexgen==0.1.7
  6. gradio_client==0.2.5
  7. gradio==3.33.1
  8. markdown
  9. numpy
  10. pandas
  11. Pillow>=9.5.0
  12. pyyaml
  13. requests
  14. safetensors==0.3.1
  15. sentencepiece
  16. tqdm
  17. scipy
复制代码
加载模型并启动 webui

  1. mkdir logs
  2. 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

来源:https://blog.csdn.net/zengNLP/article/details/131556190
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则