#本文提供详细的安装CUDA工具包和ChatGLM3依赖包的步骤,最后成功运行两个测试程序。操作系统版本:Rocky Linux 9.5,显卡型号:NVIDIA RTX 6000ADA。
1.安装CUDA
#查看显卡
lspci | grep -i nvidia
#查看操作系统信息
uname -m && uname -r && cat /etc/redhat-release
#安装内核源代码
dnf install kernel-headers kernel-devel
1.1使用rpm方式本地安装
#注意rpm安装方式可能不会提示错误,建议使用run安装
#wget https://developer.download.nvidia.com/compute/cuda/12.6.3/local_installers/cuda-repo-rhel9-12-6-local-12.6.3_560.35.05-1.x86_64.rpm
#sudo rpm -i cuda-repo-rhel9-12-6-local-12.6.3_560.35.05-1.x86_64.rpm
#sudo dnf clean all
#sudo dnf -y install cuda-toolkit-12-6
#sudo dnf -y module install nvidia-driver:open-dkms
#使用批处理方式本地安装
1.2下载本地run执行文件
wget https://developer.download.nvidia.com/compute/cuda/12.6.3/local_installers/cuda_12.6.3_560.35.05_linux.run
#新增执行权限
chmod +x cuda_12.6.3_560.35.05_linux.run
#指定内核源代码目录,否则会报错,找不到内核源代码
**重要**
./cuda_12.6.3_560.35.05_linux.run --kernel-source-path=/usr/src/kernels/5.14.0-503.16.1.el9_5.x86_64
reboot
1.3检查是否安装成功
#使用显卡驱动自带工具查看显卡信息
nvidia-smi
#检查cuda-toolkit安装目录
ll /usr/local/cuda-12.6
#如果目录不是上图,则手动安装cuda-toolkit,执行以下两句
**重要**
sudo dnf clean all
sudo dnf -y install cuda-toolkit-12-6
#检查nvcc版本,若提示找不到nvcc,则cuda-toolkit未安装,需要执行上句
**重要**
nvcc -V
2.获取ChatGLM3
2.1下载git
yum install git
mkdir -p /aiwork
cd /aiwork
2.2下载源码
git clone https://github.com/THUDM/ChatGLM3.git
3.安装python环境和uv虚拟环境
yum install python=3.10
yum install python3-pip
3.1安装uv
#uv是新的python包管理器,指令如下:
#特性 npm uv
#初始化项目 npm init 创建 package.json 文件uv init 创建 pyproject.toml 文件
#安装依赖 npm install <package-name> uv add <package-name>
#卸载依赖 npm uninstall <package-name> uv remove <package-name>
#查看依赖 npm ls查看依赖树 uv tree 查看依赖树
#锁定版本 使用package-lock.json uv lock 创建锁定文件
#运行脚本 npm run <script> uv run <script>
pip install uv
4.安装基础包
可跳过,安装依赖时会一同安装
4.1安装vLLM
pip install vllm
#从源代码编译vLLM
#git clone https://github.com/vllm-project/vllm.git
#cd vllm
#仅仅构建
#VLLM_USE_PRECOMPILED=1 pip install --editable . -i https://pypi.tuna.tsinghua.edu.cn/simple
#带编译全构建
#pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
4.2安装torch环境
#可以不单独执行,后面到具体项目安装依赖时会一同安装
#https://pytorch.org/中查看torch版本依赖
#pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
5.使用uv安装项目依赖
cd ChatGLM3
5.1创建uv虚拟环境
#uv和conda是等效的,uv更快更新,conda更稳定,选其一即可
uv venv chatglm3
source chatglm3/bin/activate
5.2在uv中安装依赖
#配置全局镜像源,不配置的话pip要带-i参数
#pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
#指定清华镜像源安装,并按最低版本适配
uv pip install --resolution=lowest-direct -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#不指定uv和最低版本安装,很可能导致后面的版本兼容问题(问题2、问题3)
#uv pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
问题1:始终无法下载vllm_nccl_cu12-2.18.1.0.4.0.tar.gz
#如下图,uv pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple执行时卡在vllm_nccl_cu12-2.18.1.0.4.0.tar.gz,等过8小时
6.1下载anaconda
wget -c https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh
6.2执行安装文件
#指定目录为/anaconda/anaconda3/
bash Anaconda3-2024.06-1-Linux-x86_64.sh
6.3环境变量
cat << 'EOF' >/etc/profile.d/anaconda.sh
export PATH=/aiwork/anaconda/anaconda3/bin:$PATH
export CONDA_ENVS_PATH=/anaconda/anaconda3/
source /etc/profile
6.4资源配置
#conda环境配置文件
cat << EOF > ~/.condarc
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
show_channel_urls: true
ssl_verify: true
allow_conda_downgrades: true
#pip配置文件
mkdir ~/.pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
#使用config命令调整资源配置,和直接修改~/.condarc等效
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes
#显示资源配置
conda config --show channels
6.5创建虚拟环境
#创建chatglm3conda 虚拟环境,指定python版本为3.10
conda create -n chatglm3conda python==3.10
#初始化,必须执行
conda init
#重新加载环境变量,必须执行
source ~/.bashrc
#激活chatglm3conda 虚拟环境
conda activate chatglm3conda
6.6安装ChatGLM3
cd /aiwork/ChatGLM3
pip install --resolution=lowest-direct -r requirements.txt
6.7获取chatglm3-6b大模型
#安装大文件git
yum install git-lfs
#检查大文件git是否安装成功
git lfs install
#克隆模型
mkdir /aiwork/models
cd /aiwork/models
git clone https://huggingface.co/THUDM/chatglm3-6b
7.单问题测试
cd /aiwork/ChatGLM3/basic_demo
cat <<EOF > mytest.py
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("/aiwork/models/chatglm3-6b/", trust_remote_code=True)
model = AutoModel.from_pretrained("/aiwork/models/chatglm3-6b/", trust_remote_code=True, device='cuda')
model = model.eval()
response = model.chat(tokenizer, "晚上睡不着应该怎么办", history=[])
print(response)
python mytest.py
8.多问题测试
#运行composite_demo
#建一个新的虚拟环境:chatglm3-demo
conda create -n chatglm3-demo python=3.10
#激活chatglm3-demo虚拟环境
conda activate chatglm3-demo
#安装依赖
cd /aiwork/ChatGLM3/composite_demo
pip install --resolution=lowest-direct -r requirements.txt
#安装Jupyter内核,以便于使用代码解析器
ipython kernel install --name chatglm3conda --user
#指定本地chatglm3-6b模型
cat << EOF >> ~/.bashrc
export MODEL_PATH=/aiwork/models/chatglm3-6b
source ~/.bashrc
#启动程序
streamlit run main.py
#在浏览器访问地址
问题2:浏览器提示找不到模块
#如下图,浏览器提示找不到模块
#分析:问题是版本不兼容
#解决办法:安装最低适配的版本,从ChatGLM3\requirements.txt,和ChatGLM3\composite_demo\requirements.txt中把>=和~=改为==,制作如下脚本:
pip uninstall transformers
pip uninstall cpm_kernels
pip uninstall torch
pip uninstall vllm
pip uninstall gradio
pip uninstall sentencepiece
pip uninstall sentence_transformers
pip uninstall accelerate
pip uninstall streamlit
pip uninstall fastapi
pip uninstall loguru
pip uninstall mdtex2html
pip uninstall latex2mathml
pip uninstall jupyter_client
pip uninstall openai
pip uninstall pydantic
pip uninstall sse-starlette
pip uninstall uvicorn
pip uninstall timm
pip uninstall tiktoken
pip uninstall langchain
pip uninstall langchain_community
pip uninstall langchainhub
pip uninstall arxiv
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ transformers==4.40.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ cpm_kernels==1.0.11
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ torch==2.3.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ vllm==0.4.2
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ gradio==4.26.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ sentencepiece==0.2.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ sentence_transformers==2.7.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ accelerate==0.29.2
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ streamlit==1.33.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ fastapi==0.110.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ loguru==0.7.2
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ mdtex2html==1.3.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ latex2mathml==3.77.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ jupyter_client==8.6.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ openai==1.30.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pydantic==2.7.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ sse-starlette==2.1.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ uvicorn==0.29.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ timm==0.9.16
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ tiktoken==0.6.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ langchain==0.2.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ langchain_community==0.2.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ langchainhub==0.1.15
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ arxiv==2.1.0
pip uninstall huggingface_hub
pip uninstall pillow
pip uninstall pyyaml
pip uninstall requests
pip uninstall ipykernel
pip uninstall ipython
pip uninstall jupyter_client
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ huggingface_hub==0.19.4
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pillow==10.1.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyyaml==6.0.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ requests==2.31.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ ipykernel==6.26.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ ipython==8.18.1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ jupyter_client==8.6.0
问题2:问了两个问题后xshell提示Connection closed.
#如下图,问了两个问题后xshell提示Connection closed.
#分析:xshell提示关闭连接,有可能是系统崩溃了,或者资源被占满
#解决:按照问题2的办法解决版本兼容问题。这样就可以解决这个问题。
#应该仔细查看红色的版本兼容提示,一个个解决,如下图,提示gradio和accelerate版本不对,查看requirement.txt的版本,不一致,所以要单独重新安装:
pip uninstall gradio
pip uninstall accelerate
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ gradio==4.26.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ accelerate==0.29.2
环境
Rocky
Linux
8
.
5
,在官网下载镜像制作启动盘进行系统安装即可。(承接上一篇文章:磁盘挂载与gcc
9
.
3
安装 cat /etc/redhat-release)
一、
NVIDIA
460
.
84驱动安装
1、禁用nouveau驱动
2、安装显卡驱动
一、
NVIDIA
460
.
84驱动安装
1、禁用nouveau驱动
输入以下 命令进行查看,应该是有回显出现的。如果没有回显出现,那么你可以省略此步骤。
lsmod | grep nouveau
在/etc/modprobe
.