本地部署LLaMA-中文LoRA部署详细说明

这篇具有很好参考价值的文章主要介绍了本地部署LLaMA-中文LoRA部署详细说明。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在Ubuntu18.04 部署中文LLaMA模型

环境准备

  1. 硬件环境

    1. AMD 5950X
    2. 128GB RAM
    3. RTX 3090(24G VRAM)
  2. 操作系统

    1. Ubuntu 18.04
  3. 编译环境(可选)

    1. llama.cpp
      1. 编译:
      cd /llama.cpp
      make
      
    2. 遇到Ubuntu18.04默认稳定版本gcc和g++不兼容问题,报错:
    (base) llama@llama-3090:~/AI/llama.cpp$ make
    I llama.cpp build info: 
    I UNAME_S:  Linux
    I UNAME_P:  x86_64
    I UNAME_M:  x86_64
    I CFLAGS:   -I.              -O3 -std=c11   -fPIC -DNDEBUG -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith -pthread -march=native -mtune=native
    I CXXFLAGS: -I. -I./examples -O3 -std=c++11 -fPIC -DNDEBUG -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar -pthread -march=native -mtune=native
    I LDFLAGS:  
    I CC:       cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
    I CXX:      g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
    
    cc  -I.              -O3 -std=c11   -fPIC -DNDEBUG -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith -pthread -march=native -mtune=native   -c ggml.c -o ggml.o
    ggml.c: In function ‘bytes_from_nibbles_32’:
    ggml.c:534:27: warning: implicit declaration of function ‘_mm256_set_m128i’; did you mean ‘_mm256_set_epi8’? [-Wimplicit-function-declaration]
        const __m256i bytes = _mm256_set_m128i(_mm_srli_epi16(tmp, 4), tmp);
                            ^~~~~~~~~~~~~~~~
                            _mm256_set_epi8
    ggml.c:534:27: error: incompatible types when initializing type ‘__m256i {aka const __vector(4) long long int}’ using type ‘int’
    Makefile:186: recipe for target 'ggml.o' failed
    make: *** [ggml.o] Error 1
    

    通过更新Ubuntu的gcc,g++版本后,make进行编译;

  4. Python虚拟环境

    1. Python = 3.10.7
    2. requirements.txt
     torch==1.13.1
     peft==0.3.0dev
     transformers==4.28.1
     sentencepiece==0.1.97
    
  5. 安装CUDA,CUDA == 11.7;

    1. 卸载老版本:
    sudo apt-get remove cuda
    sudo apt autoremove 
    sudo apt-get remove cuda*
    
    cd /usr/local/
    sudo rm -r cuda-11.x  x代表版本
    
    sudo dpkg -l |grep cuda    这个只能卸载 dpkg
    
    然后使用 nvcc -V 仍然看到 cuda 版本
    
    接着使用:
    sudo apt-get --purge remove "*cublas*" "*cufft*" "*curand*"  "*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*" 
    
    就完全卸载好了,再用 nvcc -V 没有版本了
    
    1. 安装新版本CUDA(实际为11.7,此处以11.4版本示例)
    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
    sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
    wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.0-470.42.01-1_amd64.deb
    sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.0-470.42.01-1_amd64.deb
    sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
    sudo apt-get update
    sudo apt-get -y install cuda
    

    3.验证安装

    nvcc -V #查看是否输出预期的版本.
    

部署文件

  1. LLaMA 原生PR(13B)
    下载地址:
链接: https://pan.baidu.com/s/1Q6Lbw6kDR3dig73Q5uTnNg?pwd=vhdg 提取码: vhdg
  1. LLaMA Chinese LoRA(13B)
链接: https://pan.baidu.com/s/17QGtgKYrd5dF1XGBm9xmDg?pwd=sbqi 提取码: sbqi
  1. Alpaca Chinese LoRA(13B)
链接: https://pan.baidu.com/s/1WD2vU8P-CLq_0UjVuNnLoA?pwd=v37s 提取码: v37s

模型格式转换

1.克隆ymcui的代码包;

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

2.确保机器有足够的内存加载完整模型(13B模型需要32G以上)以进行合并模型操作。
3.确认原版LLaMA模型和下载的LoRA模型完整性,检查是否与SHA256.md所示的值一致,否则无法进行合并操作。原版LLaMA包含:tokenizer.model、tokenizer_checklist.chk、consolidated.*.pth、params.json;
4.主要依赖库如下(如果出问题就请安装以下指定版本):

torch(1.10,1.12,1.13测试通过)
transformers(4.28.1测试通过)
sentencepiece(0.1.97测试通过)
peft(0.3.0dev测试通过)-->dev版本如果找不到,可以用0.3.0版本;
python版本建议在3.9以上
pip install torch==1.13.1
pip install transformers
pip install sentencepiece
pip install git+https://github.com/huggingface/peft

5.将原版LLaMA模型转换为huggingface格式

1.使用🤗huggingface的transformers库中的convert_llama_weights_to_hf.py将原版LLaMA模型转换为HF格式;将原版LLaMA的tokenizer.model放在--input_dir指定的目录,其余文件放在${input_dir}/${model_size}下。执行以下命令后,--output_dir中将存放转换好的HF版权重。

```
python src/transformers/models/llama/convert_llama_weights_to_hf.py \
--input_dir path_to_original_llama_root_dir \
--model_size 13B \
--output_dir path_to_original_llama_hf_dir
```

模型合并-合并LoRA权重,生成全量模型

合并Chinese-Alpaca-Plus需要提供两个LoRA权重,分别为Chinese-LLaMA-Plus-LoRA和Chinese-Alpaca-Plus-LoRA。执行以下命令完成合并:

python scripts/merge_llama_with_chinese_lora.py \
    --base_model path_to_original_llama_hf_dir \
    --lora_model path_to_chinese_llama_plus_lora,path_to_chinese_alpaca_plus_lora \
    --output_type [pth|huggingface] \
    --output_dir path_to_output_dir 

参数说明:

--base_model:存放HF格式的LLaMA模型权重和配置文件的目录(Step 1生成)
--lora_model:中文LLaMA/Alpaca LoRA解压后文件所在目录,也可使用🤗Model Hub模型调用名称
--output_type: 指定输出格式,可为pth或huggingface。若不指定,默认为pth
--output_dir:指定保存全量模型权重的目录,默认为./
(可选)--offload_dir:对于低内存用户需要指定一个offload缓存路径
参数选项含义与单LoRA权重合并中的含义相同。 

⚠️ 需要注意的是 --lora_model参数后要提供两个lora_model的地址,用逗号分隔。

⚠️ 两个LoRA模型的顺序很重要,不能颠倒。先写LLaMA-Plus-LoRA然后写Alpaca-Plus-LoRA。

WebUI部署及启动

  1. WebUI代码库:

    git clone https://github.com/oobabooga/text-generation-webui/
    
  2. 部署方式:

    1.创建conda虚拟环境:

    conda create -n textgen python=3.10.9
    conda activate textgen
    

    2.安装PyTorch:

    系统类型 GPU类型 命令
    Linux/WSL NVIDIA pip3 install torch torchvision torchaudio
    Linux AMD pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.4.2
    MacOS + MPS (untested) 任意 pip3 install torch torchvision torchaudio

    最新的命令可以在这里找到:https://pytorch.org/get-started/locally/。

    3.安装webUI;

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

    4.升级依赖库;

    Requirements.txt会不时更改。要更新它请使用以下命令:

    conda activate textgen
    cd text-generation-webui
    pip install -r requirements.txt --upgrade 
    

    5.下载(拷贝)模型;

    将合并好的模型整个拷贝至 ./text-generation-webui/models 文件夹下,就做好了启动webUI的准备了;

  3. 启动命令:
    1.

    conda activate textgen
    cd text-generation-webui
    python server.py
    

    2.启动UI界面后,通过默认端口 127.0.0.1:7860登录网络界面,在 models 标签下选责模型,后端会自动加载模型.
    3.在Parameters里把右下角的Skip special tokens去掉,避免文本生成异常.

  4. 内存使用问题:

    用GPU加载13B模型需要26G以上显存,3090不能够直接支持.通过调整webUI的Model tab里的

    Transformer parameters
    gpu-memory in MiB for device:0
    cpu-memory in MiB
    

    的参数,可以将模型加载到内存中,减少对GPU内存的依赖.

    也可以使用GPTQ或者llama.cpp工具,将模型进行4bit量化,减少对内存(显存)的依赖.
    本地部署LLaMA-中文LoRA部署详细说明,llama,transformer,ai文章来源地址https://www.toymoban.com/news/detail-536045.html

到了这里,关于本地部署LLaMA-中文LoRA部署详细说明的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LLM - Transformer && LLaMA2 结构分析与 LoRA 详解

    目录 一.引言 二.图说 LLM 1.Transformer 结构 ◆ Input、Output Embedding ◆ PositionEmbedding ◆ Multi-Head-Attention ◆ ADD Norm ◆ Feed Forward ◆ Linear Softmax 2.不同 LLM 结构 ◆ Encoder-Only ◆ Encoder-Decoder ◆ Decoder-Only  3.LLaMA-2 结构 ◆ Input Embedding ◆ RMSNorm ◆ RoPE ◆ Attention ◆ SwiGLU ◆ MLP 三.数说 LoR

    2024年02月13日
    浏览(26)
  • LLaMA的解读与其微调:Alpaca-LoRA/Vicuna/BELLE/中文LLaMA/姜子牙/LLaMA 2

    近期,除了研究ChatGPT背后的各种技术细节 不断看论文(至少100篇,100篇目录见此:ChatGPT相关技术必读论文100篇),还开始研究一系列开源模型(包括各自对应的模型架构、训练方法、训练数据、本地私有化部署、硬件配置要求、微调等细节)  本文一开始是作为此文《ChatGPT技术

    2024年02月17日
    浏览(34)
  • 类ChatGPT模型LLaMA的解读与其微调:Alpaca-LoRA/Vicuna/BELLE/中文LLaMA/姜子牙

    近期,除了研究ChatGPT背后的各种技术细节 不断看论文(至少100篇,100篇目录见此:ChatGPT相关技术必读论文100篇),还开始研究一系列开源模型(包括各自对应的模型架构、训练方法、训练数据、本地私有化部署、硬件配置要求、微调等细节)  本文一开始是作为此文《ChatGPT技术

    2024年02月16日
    浏览(37)
  • LLM-LLaMA中文衍生模型:Chinese-LLaMA-Alpaca【扩充词表、Lora部分参数预训练、微调】

    GitHub:GitHub - ymcui/Chinese-LLaMA-Alpaca: 中文LLaMAAlpaca大语言模型+本地CPU/GPU训练部署 (Chinese LLaMA Alpaca LLMs) 中文LLaMA模型 中文LLaMA模型在原版的基础上扩充了中文词表,使用了中文通用纯文本数据进行二次预训练。 模型名称 训练数据 重构模型[1] 大小[2] LoRA下载[3] Chinese-LLaMA-7B 通用

    2024年02月15日
    浏览(53)
  • 【LLM】Windows本地CPU部署民间版中文羊驼模型(Chinese-LLaMA-Alpaca)踩坑记录

    目录 前言 准备工作 Git  Python3.9  Cmake 下载模型  合并模型 部署模型  想必有小伙伴也想跟我一样体验下部署大语言模型, 但碍于经济实力, 不过民间上出现了大量的量化模型, 我们平民也能体验体验啦~, 该模型可以在笔记本电脑上部署, 确保你电脑至少有16G运行内存 开原地址

    2024年02月04日
    浏览(51)
  • 中文大语言模型 Llama-2 7B(或13B) 本地化部署 (国内云服务器、GPU单卡16GB、中文模型、WEB页面TextUI、简单入门)

            本文目的是让大家先熟悉模型的部署,简单入门;所以只需要很小的算力,单台服务器 单GPU显卡(显存不低于12GB),操作系统需要安装 Ubuntu 18.04。         准备一台服务器 单张英伟达GPU显卡(显存不低于12GB),操作系统需要安装 Ubuntu 18.04 (具体安装过程忽略)

    2024年02月08日
    浏览(36)
  • Llama 及 中文Alpaca模型部署测试

    环境: Xeon  E5-2680v4 16C 40G RAM WinServer 2019 Standard Edition Python 3.10 依赖库: accelerate==0.18.0 anyio==3.5.0 argon2-cffi==21.3.0 argon2-cffi-bindings==21.2.0 asttokens==2.0.5 attrs==22.1.0 Babel==2.11.0 backcall==0.2.0 beautifulsoup4==4.12.2 bleach==4.1.0 brotlipy==0.7.0 certifi==2022.12.7 cffi==1.15.1 chardet==5.1.0 charset-normalizer==3.1.

    2024年02月09日
    浏览(40)
  • 大模型部署手记(11)LLaMa2+Chinese-LLaMA-Plus-2-7B+Windows+llama.cpp+中文对话

    组织机构:Meta(Facebook) 代码仓:GitHub - facebookresearch/llama: Inference code for LLaMA models 模型:LIama-2-7b-hf、Chinese-LLaMA-Plus-2-7B   下载:使用huggingface.co和百度网盘下载 硬件环境:暗影精灵7Plus Windows版本:Windows 11家庭中文版 Insider Preview 22H2 内存 32G GPU显卡:Nvidia GTX 3080 Laptop (1

    2024年02月03日
    浏览(39)
  • 笔记本电脑上部署LLaMA-2中文模型

    尝试在macbook上部署LLaMA-2的中文模型的详细过程。 (1)环境准备 MacBook Pro(M2 Max/32G); VMware Fusion Player 版本 13.5.1 (23298085); Ubuntu 22.04.2 LTS; 给linux虚拟机分配8*core CPU 16G RAM。 我这里用的是16bit的量化模型,至少需要13G内存,如果4bit的只需要3.8G内存,当然上述不包含系统本身需要

    2024年04月09日
    浏览(26)
  • 大模型部署手记(9)LLaMa2+Chinese-LLaMA-Plus-7B+Windows+llama.cpp+中文文本补齐

    组织机构:Meta(Facebook) 代码仓:GitHub - facebookresearch/llama: Inference code for LLaMA models 模型:llama-2-7b、Chinese-LLaMA-Plus-7B(chinese_llama_plus_lora_7b)   下载:使用download.sh下载 硬件环境:暗影精灵7Plus Windows版本:Windows 11家庭中文版 Insider Preview 22H2 内存 32G GPU显卡:Nvidia GTX 3080 La

    2024年02月03日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包