Ubuntu 22.04 配置LLM大语言模型环境

本文介绍了清洁安装的Ubuntu Server 22.04 LTS安装NVIDIA显卡驱动、CUDA 12.1、cuDNN的方法及ChatGLM3、百川2、FastChat等大语言模型的部署使用方法。

安装NVIDIA驱动

禁用nouveau

sudo vi /etc/modprobe.d/blacklist.conf

尾部追加一行

blacklist nouveau

执行并重启系统

sudo update-initramfs -u
reboot

检查nouveau是否关闭成功,应当无输出

lsmod | grep nouveau

安装535驱动

sudo apt update
sudo apt install nvidia-driver-535 -y

当然,也可以查询推荐驱动,选择recommended的版本安装:

ubuntu-drivers devices

装完后执行nvidia-smi验证,应该有正确的输出。

安装CUDA12.1

下载并执行:

wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run
chmod +x cuda_12.1.0_530.30.02_linux.run
sh cuda_12.1.0_530.30.02_linux.run

执行后要等一会加载,然后进入交互式界面,按如下步骤操作:

  • 提示已经存在驱动…选择continue
  • 阅读并接受协议,输入accept回车
  • 上下光标选中- [X] Driver列,按空格以取消勾选驱动,再选择Install回车
  • 等待安装完成

编辑环境变量:

vi ~/.bashrc
尾部追加:
export PATH=/usr/local/cuda-12.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64:$LD_LIBRARY_PATH
执行:
source ~/.bashrc 

验证:

nvcc --version
systemctl status nvidia-persistenced

均应输出有效信息。

安装NVIDIA-Docker(可选)

官网文档链接:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html

  • 添加源并安装:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.listsudo apt-get update
sudo apt-get install -y nvidia-container-toolkit nvidia-container-runtime
  • 在安装好Docker后,执行如下操作以配置Docker:
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
  • 执行以下命令验证
docker run --gpus all -it --name nvtest nvidia/cuda:12.3.1-base-ubuntu22.04 /bin/sh
nvidia-smi

附:

  • 使用所有GPU

    docker run --gpus all nvidia/cuda:12.3.1-base-ubuntu22.04 nvidia-smi

  • 使用两个GPU

    docker run --gpus 2 nvidia/cuda:12.3.1-base-ubuntu22.04 nvidia-smi

  • 指定GPU运行

    docker run --gpus '"device=1,2"' nvidia/cuda:12.3.1-base-ubuntu22.04 nvidia-smi

    docker run --gpus '"device=UUID-ABCDEF,1"' nvidia/cuda:12.3.1-base-ubuntu22.04 nvidia-smi

安装cuDNN(可选)

官网文档链接:https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#installlinux-deb

记得安装zlib:

sudo apt-get install zlib1g

安装Miniconda

  • 下载并安装miniconda:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc
  • conda和pip换源
conda config --set show_channel_urls yes
vi ~/.condarcchannels:- defaults
show_channel_urls: true
default_channels:- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmsys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudbioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudmenpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudpytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloudsimpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/clouddeepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
ssl_verify: falsepip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

使用ModelScope下载模型到本地

  • 新建一个下载模型的脚本download_model.py
import sys
from modelscope import snapshot_downloadif len(sys.argv) < 2:print("Usage: python download_model.py <model_name>")sys.exit(1)model_name = sys.argv[1]
model_dir = snapshot_download(model_name, revision="master")
print(model_dir)
  • 再新建一个shell脚本dl_model.sh用于拉起模型下载脚本
#!/bin/bashif [ "$#" -ne 1 ]; thenecho "Usage: <model_name>"exit 1
fipython download_model.py "$1"

设置代理脚本(加速github等)

  • 设置代理setproxy
#!/bin/sh# for terminal
export proxyserveraddr=192.168.114.114
export proxyserverport=7890
export HTTP_PROXY="http://$proxyserveraddr:$proxyserverport/"
export HTTPS_PROXY="https://$proxyserveraddr:$proxyserverport/"
export FTP_PROXY="ftp://$proxyserveraddr:$proxyserverport/"
export SOCKS_PROXY="socks://$proxyserveraddr:$proxyserverport/"
export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,可以新增你想过滤的ip段;"
export http_proxy="http://$proxyserveraddr:$proxyserverport/"
export https_proxy="https://$proxyserveraddr:$proxyserverport/"
export ftp_proxy="ftp://$proxyserveraddr:$proxyserverport/"
export socks_proxy="socks://$proxyserveraddr:$proxyserverport/"
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,可以新增你想过滤的ip段;"# for apt-get
cat <<-EOF| sudo tee /etc/apt/apt.conf
Acquire::http::proxy "http://$proxyserveraddr:$proxyserverport/";
Acquire::https::proxy "https://$proxyserveraddr:$proxyserverport/";
Acquire::ftp::proxy "ftp://$proxyserveraddr:$proxyserverport/";
Acquire::socks::proxy "socks://$proxyserveraddr:$proxyserverport/";
EOF
  • 取消代理unsetproxy
#!/bin/sh
unset proxyserveraddr
unset proxyserverport
unset HTTP_PROXY
unset HTTPS_PROXY
unset FTP_PROXY
unset SOCKS_PROXY
unset NO_PROXY
unset http_proxy
unset https_proxy
unset ftp_proxy
unset socks_proxy
unset no_proxy
gsettings reset org.gnome.system.proxy ignore-hosts
echo -n ""|sudo tee /etc/apt/apt.conf
  • 使用方法:
source setproxy
source unsetproxy

部署ChatGLM3

  1. 创建虚拟环境
conda create -n chatglm python=3.11
  1. 激活,开搞
conda activate chatglm
git clone https://github.com/THUDM/ChatGLM3.git
pip install modelscope
pip install -r requirements.txt
  1. 安装pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
  1. 下载模型
dl_model.sh ZhipuAI/chatglm3-6b

下载的模型路径为/root/.cache/modelscope/hub/ZhipuAI/chatglm3-6b

  1. 编写composite_demo启动脚本(openai_api_demo等同理)
cd /root/ChatGLM3/composite_demo
vi start.sh

以下是start.sh的内容

export MODEL_PATH=/root/.cache/modelscope/hub/ZhipuAI/chatglm3-6b
streamlit run main.py

如果要使用composite_demo的code interpreter,首次启动前记得执行:

ipython kernel install --name chatglm3 --user

部署百川2

前面的虚拟环境准备、代码库拉取、模型下载等第1~4节和ChatGLM3差不多。

百川2的git仓库地址是https://github.com/baichuan-inc/Baichuan2.git

魔搭的模型名是baichuan-inc/Baichuan2-7B-Chat(或13B)

配置:修改启动脚本的init_model函数

vi Baichuan2/web_demo.py

web_demo.py的修改后函数如下:

@st.cache_resource
def init_model():model = AutoModelForCausalLM.from_pretrained("/root/.cache/modelscope/hub/baichuan-inc/Baichuan2-7B-Chat",torch_dtype=torch.float16,device_map="auto",trust_remote_code=True)model.generation_config = GenerationConfig.from_pretrained("/root/.cache/modelscope/hub/baichuan-inc/Baichuan2-7B-Chat")tokenizer = AutoTokenizer.from_pretrained("/root/.cache/modelscope/hub/baichuan-inc/Baichuan2-7B-Chat",use_fast=False,trust_remote_code=True)return model, tokenizer

如果要以int8量化模式运行,在torch_dtype=torch.float16,后添加一行load_in_8bit=True,即可。

执行:streamlit run web_demo.py

搭建FastChat服务

  • 搭建FastChat环境
conda create -n fastchat python=3.11
conda activate fastchat
git clone https://github.com/lm-sys/FastChat.git
cd FastChat
pip3 install --upgrade pip
pip3 install -e ".[model_worker,webui]"
  • 写一组FastChat启动脚本:

    fastchat.sh(使用anaconda就把miniconda3换成anaconda3)

#!/bin/bash 
source ~/miniconda3/etc/profile.d/conda.sh
conda activate fastchat
if [ $? -ne 0 ]; thenecho "Failed to activate Anaconda environment 'fastchat'."exit 1
fi
/root/fastchat-process/main.sh "$@"
  • 创建相关文件和文件夹
mkdir ~/fastchat-process
touch /root/fastchat-process/controller.log
touch /root/fastchat-process/gradio_web_server.log
touch /root/fastchat-process/model_worker.log
touch /root/fastchat-process/openai_api_server.log
  • 创建fastchat-process/main.sh
#!/bin/bashsource ~/anaconda3/etc/profile.d/conda.sh
conda activate fastchatwait_for_startup() {while ! grep -q "Application startup complete" <(tail -n 10 -f "$1"); dosleep 1done
}python3 -m fastchat.serve.controller &> "/root/fastchat-process/controller.log" &
echo "Controller is starting..."
wait_for_startup /root/fastchat-process/controller.logpython3 -m fastchat.serve.model_worker --model-path "$1" &> "/root/fastchat-process/model_worker.log" &
echo "Model worker is starting with model path $1..."
wait_for_startup /root/fastchat-process/model_worker.logpython3 -m fastchat.serve.openai_api_server --host localhost --port "$2" &> "/root/fastchat-process/openai_api_server.log" &
echo "OpenAI API server is starting on localhost:$2..."
wait_for_startup /root/fastchat-process/openai_api_server.logpython3 -m fastchat.serve.gradio_web_server &> "/root/fastchat-process/gradio_web_server.log" &
echo "Gradio web server is starting..."
wait_for_startup /root/fastchat-process/gradio_web_server.logecho "All services have been started and are ready."

用法:./fastchat.sh /root/Yi-6B-Chat 8000

这将启动一个OPENAI_API端口为8000的服务和一个gradio web服务。具体日志可以在对应log中找到。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428944.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1006 Sign In and Sign Out (25 分)

1006 Sign In and Sign Out (25 分) At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to fi…

1007 Maximum Subsequence Sum (25 分)

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, …, Nk}. A continuous subsequence is defined to be { Ni, N​i1, …, Nj​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of i…

python - hadoop,mapreduce demo

Hadoop,mapreduce 介绍 59888745qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统&#xff08;cloudera是最大的输出者类似于Linux的红帽&#xff09;&#xff0c; 把用户的交易或行为信息通过HDFS&#xff08;分布式文件系统&#xff09;等存储用户数据文件&#xff0c;…

linux rsync 常见错误,Linux rsync常见错误

rsync是类unix系统下非常高效&#xff0c;实用的数据镜像备份工具。在rsyncd.log里面或.err文件里面&#xff0c;我们都是通过错误日志查看&#xff0c;可以分析出错误的原因。问题一&#xff1a;ERROR: chroot failedrsync error: error starting client-server protocol (cod…

Vue父子组件间的通信

父组件通过 props 向下传递数据给子组件&#xff0c;子组件通过 events 向上给父组件发送消息。 父组件&#xff1a; <div><div style"background:#34495E;color: #fff; padding:20px"><p style"margin-bottom: 20px">这是父组件</p&…

linux 取出字符中数字,使用awk提取字符串中的数字或字母

1.提取字符串中的数字$ echo dsFUs34tg*fs5a%8ar%$# |awk -F "" {for(i1;i<NF;i){if ($i ~ /[[:digit:]]/){str$istr1(str1 str)}}print str1}输出3458或$ echo dsFUs34tg*fs5a%8ar%$# |awk -F "" {for(i1;i<NF;i){if ($i ~ /[0-9]/){str$istr1(str1…

1009 Product of Polynomials (25 分)

1009 Product of Polynomials (25 分) 这题目要卡的话只能卡第一个吧&#xff0c;考虑零项之后&#xff0c;这道题就简单了。 #include<iostream> #include<set> #include<vector> #include<iomanip> using namespace std; int main() {double cun1[1…

【BZOJ2908】又是nand 树链剖分+线段树

【BZOJ2908】又是nand escription 首先知道A nand Bnot(A and B) &#xff08;运算操作限制了数位位数为K&#xff09;比如2 nand 3&#xff0c;K3&#xff0c;则2 nand 3not (2 and 3)not 25。给出一棵树&#xff0c;树上每个点都有点权&#xff0c;定义树上从a到b的费用为0与…

1001 害死人不偿命的(3n+1)猜想 (15 分)

1001 害死人不偿命的(3n1)猜想 (15 分) 卡拉兹(Callatz)猜想&#xff1a; 对任何一个正整数 n&#xff0c;如果它是偶数&#xff0c;那么把它砍掉一半&#xff1b;如果它是奇数&#xff0c;那么把 (3n1) 砍掉一半。这样一直反复砍下去&#xff0c;最后一定在某一步得到 n1。卡…

2017安徽二级c语言,2017计算机二级C语言测试题及答案

2017计算机二级C语言测试题及答案此题 首先为整型变量赋初值x11,x22表达式1为i1&#xff0c;表达式2(循环条件)为i<N即i<4&#xff0c;表达式3为i循环变量初值i为1&#xff0c;循环条件(即表达式2)i<4成立&#xff0c;进入第1次循环第1次循环执行printf("%4d%4d&…

笔试被虐随想

两个题 50分钟&#xff0c;我写满时间还没写完一个。心疼自己。 算法渣。战五渣。 每天一个算法题吧。 至于工作&#xff0c;没什么好说的&#xff0c;加油吧&#xff0c;横竖算经验。转载于:https://www.cnblogs.com/bryanz/p/7330998.html

c语言如何不四舍五入取整函数,Excel四舍五入、不四舍五入或取整的相关函数

①ROUND函数1、四舍五入&#xff1a;ROUND(目标单元格&#xff0c;0)2、入&#xff1a;ROUNDUP(目标单元格&#xff0c;0) 得出来的结果是向上的整数&#xff0c;即&#xff1a;3.3543、舍&#xff1a;ROUNDDOWN(目标单元格&#xff0c;0)得出来的结果是向下的整数&#xff0c;…

1002 写出这个数 (20 分)

1002 写出这个数 (20 分) 读入一个正整数 n&#xff0c;计算其各位数字之和&#xff0c;用汉语拼音写出和的每一位数字。 输入格式&#xff1a; 每个测试输入包含 1 个测试用例&#xff0c;即给出自然数 n 的值。这里保证 n 小于 10 ​100 ​​ 。 输出格式&#xff1a; 在一…

E - More is better (并查集)

点击打开链接 Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. Mr Wang selected a room big enough to hold the boys. The boy who ar…

c语言pwm调制方式,pwm调制原理同步调制_几种pwm调制方式介绍 - 全文

PWM简介脉冲宽度调制是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术&#xff0c;广泛应用在从测量、通信到功率控制与变换的许多领域中。脉冲宽度调制是一种模拟控制方式&#xff0c;其根据相应载荷的变化来调制晶体管基极或MOS管栅极的偏置&#xff0c;来…

1003 我要通过! (20 分)

1003 我要通过&#xff01; (20 分) “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件&#xff0c;系统就输出“答案正确”&#xff0c;否则输出“答案错误”。 得到“答案正确”的条件是&#xff1…

POJ 3613 Cow Relays (floyd + 矩阵高速幂)

题目大意&#xff1a; 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*AB B[i][j] 就表示 i-j 刚好走过两条路的方法数 那么同理 我们把i-j 的路径长度存到A 中。在A*A的过程中&#xff0c;不断取小的。那么最后得到的也就是i - j 走过…

c语言如何用循环语句一个字一个字的输出,怎样用c语言的for嵌套循环,用·画出泳字,求解,主要是怎样用循环语句打出,在某一行中既有空格又有·...

暗域天堂#include #include #include #include #include using namespace std;int get_character(unsigned char* pc1, unsigned char* pc2){unsigned char buf[100];cin >> buf;*pc1 buf[0];*pc2 buf[1];if (*pc1 {return -2;}if (*pc2 {return -2;}return 0;}void lo…

1004 成绩排名 (20 分)

1004 成绩排名 (20 分) 读入 n&#xff08;>0&#xff09;名学生的姓名、学号、成绩&#xff0c;分别输出成绩最高和成绩最低学生的姓名和学号。 输入格式&#xff1a; 每个测试输入包含 1 个测试用例&#xff0c;格式为 第 1 行&#xff1a;正整数 n 第 2 行&#xff1a…

【Sikuli】Sikuli 文档

http://sikulix-2014.readthedocs.io/en/latest/index.html转载于:https://www.cnblogs.com/MasterMonkInTemple/p/7346480.html