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

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

1 服务器&操作系统 

        1.1服务器的准备

        准备一台服务器 单张英伟达GPU显卡(显存不低于12GB),操作系统需要安装 Ubuntu 18.04 (具体安装过程忽略)。 重装系统前注意备份现有存储上的相关重要数据。 GPU显卡驱动先不安装; 后续介绍驱动和CUDA的安装步骤。

        如果手上没有相应的服务器硬件设备、可以购买带GPU的云服务器, 以下可供选择参考。

 上云精选_云服务器秒杀_开发者上云推荐-腾讯云腾讯云推出云产品限时特惠抢购活动:2C2G云服务器9元/月起;还有短信、CDN、COS等热门云产品限时特惠...icon-default.png?t=N7T8https://cloud.tencent.com/act/pro/seckill_season?from=20210

地域选择国内适合的城市;   预装镜像为 Ubuntu 18.04

        购买后一般云厂商会自动安装显卡的驱动和CUDA , 因为目前大部分的项目对 cuda 11.7 和 11.8 版本兼容的比较好,后续我们要指定安装特定的版本; 所以如果云主机已经预装了GPU驱动,我们需要卸载。 如果不了解卸载的指令,可以在控制台 点击“重装系统”; 

        然后 注意 不要勾选后台自动安装 GPU 驱动; 这样重置后就是一台干净的 Ubuntu18.04的系统,且没有安装GPU的驱动。

1.2系统工具和环境 

   安装一些基础的相关软件工具


   
   
  1. sudo apt update
  2. sudo apt install -y vim git wget curl net-tools language-pack-zh-hans language-pack-en

中文环境


   
   
  1. echo '
  2. LANG="en_US.utf8"; export LANG
  3. LANGUAGE="en_US.utf8"; export LANGUAGE
  4. LC_ALL="en_US.utf8"; export LC_ALL
  5. LC_CTYPE="en_US.utf8"; export LC_CTYPE
  6. SUPPORTED=en_US.UTF8:en_US:en; export SUPPORTED
  7. TZ="Asia/Shanghai"; export TZ
  8. ' >> ~/.profile

   
   
  1. source ~/.profile
  2. locale
  3. #确认当前是UTF8

2 安装  显卡驱动  和  CUDA

2.1 确认操作系统当前没有安装驱动 

        确认当前系统没有安装GPU显卡驱动, 且没有加载nvidia相关模块


   
   
  1. lsmod | grep nvidia
  2. sudo lsof /dev/nvidia*
  3. #以上命令 应该没有输出 nvidia 等字样的信息

                 

 2.2 如何安装指定版本的显卡驱动(可选)

        后续 2.3 中 cuda 安装包中含有配合的显卡驱动,可以一步安装; 如果需要指定安装和 cuda 包中不同版本的驱动,可参考本节, 否则建议直接跳至 2.3 节。 

根据显卡型号下载适合的驱动

Official Drivers | NVIDIADownload latest drivers for NVIDIA products including GeForce, TITAN, NVIDIA RTX, Data Center, GRID and more.icon-default.png?t=N7T8https://www.nvidia.com/download/index.aspxUnix Drivers | NVIDIA
Linux AMD64 Display Driver Archive | NVIDIA

 

 下载并安装显卡驱动 (需要 root 权限)


   
   
  1. wget https://us.download.nvidia.com/XFree86/Linux-x86_64/535.98/NVIDIA-Linux-x86_64-535.98.run
  2. sudo sh ./NVIDIA-Linux-x86_64-535.98.run
  3. tail /var/log/nvidia-installer.log

2.3 安装 cuda (可一并安装显卡驱动) 

CUDA Toolkit Archive | NVIDIA DeveloperPrevious releases of the CUDA Toolkit, GPU Computing SDK, documentation and developer drivers can be found using the links below. Please select the release you want from the list below, and be sure to check www.nvidia.com/drivers for more recent production drivers appropriate for your hardware configuration.icon-default.png?t=N7T8https://developer.nvidia.com/cuda-toolkit-archive下载 cuda 11.8 版本安装包 (内含 显卡驱动)

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run

   
   

CUDA Toolkit 11.8 Downloads | NVIDIA DeveloperResources CUDA Documentation/Release NotesMacOS Tools Training Sample Code Forums Archive of Previous CUDA Releases FAQ Open Source PackagesSubmit a BugTarball and Zip Archive Deliverablesicon-default.png?t=N7T8https://developer.nvidia.com/cuda-11-8-0-download-archive

安装 cuda 需要 root 权限; 

如跳过上节没有安装显卡驱动则:要勾选安装驱动项; 否则 , 要去掉安装驱动的勾选项

sudo sh cuda_11.8.0_520.61.05_linux.run 
   
   

最后运行 nvidia-smi 确认 驱动 和 cuda 安装成功


   
   
  1. tail /var/log/cuda-installer.log
  2. nvidia-smi

注意 : 使用 nvidia-smi  查看  CUDA 版本必须是 11.8

3 准备  Python 环境  (安装conda & 配置国内镜像源)

        3.1安装 conda


   
   
  1. wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py39_4.12.0-Linux-x86_64.sh
  2. sh Miniconda3-py39_4.12.0-Linux-x86_64.sh
  3. #都回答 yes
  4. source ~/.bashrc

        3.2 为 pip 配置国内镜像源


   
   
  1. pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/simple
  2. pip config set install.trusted-host mirrors.tuna.tsinghua.edu.cn

        3.3 为 conda 配置国内镜像源

        编辑当前用户下的 .condarc 文件   


   
   
  1. conda config -- set show_channel_urls yes
  2. vim ~/.condarc

        替换成如下内容:


   
   
  1. channels:
  2. - defaults
  3. show_channel_urls: true
  4. default_channels:
  5. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  6. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  7. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  8. custom_channels:
  9. conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  10. msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  11. bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  12. menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  13. pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  14. pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  15. simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

         清除索引缓存


   
   
  1. conda clean --all -- yes
  2. conda clean -i

        3.4 以下国内常用的 pip 镜像源 仅供参考


   
   
  1. 清华:https://mirrors.tuna.tsinghua.edu.cn/simple
  2. 阿里云:http://mirrors.aliyun.com/pypi/simple/
  3. 中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
  4. 豆瓣:http://pypi.douban.com/simple/

        3.5 常用conda命令 仅供参考


   
   
  1. 创建虚拟环境:conda create -n 环境名称 python=版本号
  2. 查看已有虚拟环境:conda env list
  3. 激活虚拟环境:conda activate 环境名称
  4. 删除虚拟环境:conda remove -n 环境名称 --all
  5. 查看当前环境下已安装的包:conda list
  6. 导出当前环境下的包:conda env export > environment.yml
  7. 根据导出的包安装环境:conda env create -f environment.yml
  8. 安装包:conda install 包名
  9. 安装下载到本地的包:conda install --use-local  包路径
  10. 卸载当前环境下包:conda uninstall 包名
  11. 卸载指定虚拟环境中的包:conda remove --name 环境名称 包名


 

4 创建环境 安装相关包

        4.1 Conda创建一个新的环境


   
   
  1. conda create -n llm python=3.10.9
  2. conda activate llm

         4.2安装 Web 交互 UI 工程 text-generation-webui

GitHub - oobabooga/text-generation-webui: A Gradio web UI for Large Language Models. Supports transformers, GPTQ, llama.cpp (ggml), Llama models.A Gradio web UI for Large Language Models. Supports transformers, GPTQ, llama.cpp (ggml), Llama models. - GitHub - oobabooga/text-generation-webui: A Gradio web UI for Large Language Models. Supports transformers, GPTQ, llama.cpp (ggml), Llama models.icon-default.png?t=N7T8https://github.com/oobabooga/text-generation-webui


   
   
  1. wget https://github.com/oobabooga/text-generation-webui/releases/download/installers/oobabooga_linux.zip
  2. unzip oobabooga_linux.zip
  3. cd oobabooga_linux/
  4. conda activate llm
  5. bash ./start_linux.sh

 bash ./start_linux.sh 首次运行会下载大量数据, 时间较长。

成功后会 默认监听 7860 端口 开启web服务,如下所示:

              

 5 语言 交互 UI 

        5.1进入 web 交互页面


   
   
  1. conda activate llm
  2. HF_TOKEN= "hf_XXXXXXXXXXXXXXXXXXXXXX" # HuggingFace  的 Access Tokens
  3. export HF_TOKEN
  4. ./start_linux.sh

./start_linux.sh 开启web服务,成功后会显示:

使用 SSH Tunnel 建立隧道 将 服务器的 7860 端口 映射到本地, 然后使用浏览器打开 。

         5.2 使用交互页面自动下载模型

        首先需要在 Model 模型页签中下载一个Llama2模型 。

        输入模型名称路径:“FlagAlpha/Llama2-Chinese-7b-Chat”

        然后点击下载按钮

注意:如果下载某些模型 出现 Http 401 错误,则需要设置  HuggingFace  的 Access Tokens

需要登录 Hugging Face – The AI community building the future.

在设置页面的  Access Tokens 中创建  Tokens 并复制。然后在 ./start_linux.sh 启动前 设置“HF_TOKEN” 环境变量 。

        另外,可以在 HuggingFace 上寻找其它开放的大预言模型, 如果使用 13B 或者 更大的模型推理,依据参数规模可能需要更高的GPU显存,甚至多张GPU来加载运行。 Hugging Face – The AI community building the future.We’re on a journey to advance and democratize artificial intelligence through open source and open science.icon-default.png?t=N7T8https://huggingface.co/  

          5.3手动下载 Llama2 模型 (可选)

模型文件建议去官网下载,  国内Llama2 下载地址 仅供参考

另外 Llama2 中文模型 供参考选择  , 通过访问Llama2中文社区链接 仅供参考:

GitHub - FlagAlpha/Llama2-Chinese: Llama中文社区,最好的中文Llama大模型,完全开源可商用Llama中文社区,最好的中文Llama大模型,完全开源可商用. Contribute to FlagAlpha/Llama2-Chinese development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/FlagAlpha/Llama2-Chinese  将下载好的模型文件目录放到 /data/ai/oobabooga_linux/text-generation-webui/models中

        5.4加载模型

        在 Model 模型页签中 加载模型 。 

        如下图: 刷新现有模型、 下拉菜单选中模型,点击 Load 加载 模型 , 成功加载后会显示:  “Successfully loaded FlagAlpha_Llama2-Chinese-7b-Chat.”

        5.3使用 Chat 页签 Web 交互 UI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值