1. 环境准备Ubuntu系统与GPU驱动配置在开始搭建ChatGLM2-6b环境之前我们需要确保基础环境配置正确。我推荐使用Ubuntu 20.04 LTS或22.04 LTS版本这两个版本在深度学习社区中被广泛验证过稳定性。以下是详细的配置步骤1.1 系统基础环境检查首先确认你的系统架构和内核版本uname -m # 确认是x86_64架构 lsb_release -a # 查看Ubuntu具体版本对于GPU服务器建议禁用自动更新以避免驱动冲突sudo apt-mark hold linux-image-generic linux-headers-generic sudo systemctl stop unattended-upgrades sudo systemctl disable unattended-upgrades1.2 NVIDIA驱动安装实战驱动安装是第一个容易踩坑的地方。根据我的经验不要使用Ubuntu自带的附加驱动工具而是直接从NVIDIA官网下载对应驱动先卸载已有驱动如果是新系统可跳过sudo apt purge nvidia* sudo apt autoremove安装编译依赖sudo apt update sudo apt install build-essential dkms libglvnd-dev下载官方驱动以515版本为例wget https://us.download.nvidia.com/XFree86/Linux-x86_64/515.76/NVIDIA-Linux-x86_64-515.76.run sudo chmod x NVIDIA-Linux-x86_64-515.76.run sudo ./NVIDIA-Linux-x86_64-515.76.run --no-opengl-files -Z --dkms重要提示一定要加--no-opengl-files参数否则可能导致图形界面崩溃。安装完成后需要重启系统。验证驱动是否安装成功nvidia-smi # 应该能看到GPU信息和驱动版本1.3 CUDA Toolkit选择与安装ChatGLM2-6b需要CUDA 11.7或更高版本。我推荐使用runfile方式安装而非apt因为可以更灵活地选择组件wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda_11.7.1_515.65.01_linux.run sudo sh cuda_11.7.1_515.65.01_linux.run安装时注意取消勾选Driver已单独安装确保勾选CUDA Toolkit和cuDNN添加环境变量到~/.bashrcexport PATH/usr/local/cuda-11.7/bin${PATH::${PATH}} export LD_LIBRARY_PATH/usr/local/cuda-11.7/lib64${LD_LIBRARY_PATH::${LD_LIBRARY_PATH}}1.4 cuDNN与TensorRT安装这些加速库对性能影响很大建议从NVIDIA官网下载对应版本sudo apt install libcudnn8 libcudnn8-dev sudo apt install libnvinfer8 libnvinfer-plugin8 libnvinfer-dev libnvinfer-plugin-dev验证安装nvcc -V # 查看CUDA编译器版本 cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 # 查看cuDNN版本2. Python环境与依赖库配置2.1 Conda环境创建我强烈建议使用conda隔离环境避免包冲突wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh source ~/.bashrc conda create -n chatglm python3.8 conda activate chatglm2.2 PyTorch GPU版本安装PyTorch版本必须与CUDA匹配。对于CUDA 11.7pip install torch1.13.1cu117 torchvision0.14.1cu117 torchaudio0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117验证PyTorch是否能识别GPUimport torch print(torch.cuda.is_available()) # 应该返回True print(torch.cuda.get_device_name(0)) # 显示GPU型号2.3 其他关键依赖ChatGLM2-6b需要一些特定版本的库pip install transformers4.33.3 icetk cpm_kernels sentencepiece gradio mdtex2html特别注意如果遇到protobuf版本冲突可以尝试pip install protobuf3.20.03. ChatGLM2-6b模型部署3.1 模型下载与准备从Hugging Face下载模型需要先同意协议git lfs install git clone https://huggingface.co/THUDM/chatglm2-6b cd chatglm2-6b如果网络不稳定可以使用镜像源git clone https://hf-mirror.com/THUDM/chatglm2-6b3.2 模型加载测试创建一个测试脚本test_load.pyfrom transformers import AutoTokenizer, AutoModel tokenizer AutoTokenizer.from_pretrained(./chatglm2-6b, trust_remote_codeTrue) model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).cuda() response, history model.chat(tokenizer, 你好, history[]) print(response)运行时应能看到模型成功加载并返回响应。如果出现OOM错误可能需要启用量化model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).quantize(4).cuda()3.3 常见加载问题解决问题1CUDA out of memory解决方案减小batch size或使用量化修改加载代码model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).half().cuda()问题2Transformers版本不兼容解决方案固定特定版本pip install transformers4.33.3问题3Protobuf冲突解决方案pip uninstall protobuf pip install protobuf3.20.04. P-Tuning微调实战4.1 准备训练数据创建train.jsonl文件格式示例{prompt: 解释神经网络, response: 神经网络是..., history: []} {prompt: Python的装饰器, response: 装饰器是..., history: []}建议数据量至少1000条领域要集中。4.2 配置P-Tuning参数修改ptuning/train.sh关键参数PRE_SEQ_LEN128 LR2e-4 NUM_GPUS1 torchrun --standalone --nnodes1 --nproc-per-node$NUM_GPUS main.py \ --do_train \ --train_file train.jsonl \ --validation_file dev.jsonl \ --pre_seq_len $PRE_SEQ_LEN \ --max_source_length 256 \ --max_target_length 256 \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 8 \ --gradient_accumulation_steps 4 \ --lr $LR \ --num_train_epochs 10 \ --save_steps 1000 \ --output_dir output \ --overwrite_output_dir4.3 启动训练bash train.sh监控GPU使用情况watch -n 1 nvidia-smi4.4 常见训练问题问题1Loss不下降检查学习率是否合适确认数据质量可能需要清洗数据尝试调整pre_seq_len通常64-256之间问题2GPU内存不足减小per_device_train_batch_size增加gradient_accumulation_steps使用--fp16参数启用混合精度问题3NaN损失尝试减小学习率添加梯度裁剪--max_grad_norm 1.05. 模型部署与性能优化5.1 量化部署为了减少显存占用可以使用4-bit或8-bit量化model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).quantize(8).cuda()量化后显存占用可以从13GB降到6GB左右。5.2 使用vLLM加速安装vLLMpip install vllm启动API服务python -m vllm.entrypoints.api_server --model chatglm2-6b --tokenizer chatglm2-6b --tensor-parallel-size 15.3 性能监控与调优使用JTop监控pip install jetson-stats jtop关注以下指标GPU Util应该保持在70%以上Memory Usage避免接近100%Temperature保持在80°C以下我在实际部署中发现调整max_batch_size和max_seq_len对吞吐量影响很大需要根据具体硬件找到平衡点。
Ubuntu系统下ChatGLM2-6b环境搭建与GPU驱动配置指南
发布时间:2026/7/17 2:48:48
1. 环境准备Ubuntu系统与GPU驱动配置在开始搭建ChatGLM2-6b环境之前我们需要确保基础环境配置正确。我推荐使用Ubuntu 20.04 LTS或22.04 LTS版本这两个版本在深度学习社区中被广泛验证过稳定性。以下是详细的配置步骤1.1 系统基础环境检查首先确认你的系统架构和内核版本uname -m # 确认是x86_64架构 lsb_release -a # 查看Ubuntu具体版本对于GPU服务器建议禁用自动更新以避免驱动冲突sudo apt-mark hold linux-image-generic linux-headers-generic sudo systemctl stop unattended-upgrades sudo systemctl disable unattended-upgrades1.2 NVIDIA驱动安装实战驱动安装是第一个容易踩坑的地方。根据我的经验不要使用Ubuntu自带的附加驱动工具而是直接从NVIDIA官网下载对应驱动先卸载已有驱动如果是新系统可跳过sudo apt purge nvidia* sudo apt autoremove安装编译依赖sudo apt update sudo apt install build-essential dkms libglvnd-dev下载官方驱动以515版本为例wget https://us.download.nvidia.com/XFree86/Linux-x86_64/515.76/NVIDIA-Linux-x86_64-515.76.run sudo chmod x NVIDIA-Linux-x86_64-515.76.run sudo ./NVIDIA-Linux-x86_64-515.76.run --no-opengl-files -Z --dkms重要提示一定要加--no-opengl-files参数否则可能导致图形界面崩溃。安装完成后需要重启系统。验证驱动是否安装成功nvidia-smi # 应该能看到GPU信息和驱动版本1.3 CUDA Toolkit选择与安装ChatGLM2-6b需要CUDA 11.7或更高版本。我推荐使用runfile方式安装而非apt因为可以更灵活地选择组件wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda_11.7.1_515.65.01_linux.run sudo sh cuda_11.7.1_515.65.01_linux.run安装时注意取消勾选Driver已单独安装确保勾选CUDA Toolkit和cuDNN添加环境变量到~/.bashrcexport PATH/usr/local/cuda-11.7/bin${PATH::${PATH}} export LD_LIBRARY_PATH/usr/local/cuda-11.7/lib64${LD_LIBRARY_PATH::${LD_LIBRARY_PATH}}1.4 cuDNN与TensorRT安装这些加速库对性能影响很大建议从NVIDIA官网下载对应版本sudo apt install libcudnn8 libcudnn8-dev sudo apt install libnvinfer8 libnvinfer-plugin8 libnvinfer-dev libnvinfer-plugin-dev验证安装nvcc -V # 查看CUDA编译器版本 cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 # 查看cuDNN版本2. Python环境与依赖库配置2.1 Conda环境创建我强烈建议使用conda隔离环境避免包冲突wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh source ~/.bashrc conda create -n chatglm python3.8 conda activate chatglm2.2 PyTorch GPU版本安装PyTorch版本必须与CUDA匹配。对于CUDA 11.7pip install torch1.13.1cu117 torchvision0.14.1cu117 torchaudio0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117验证PyTorch是否能识别GPUimport torch print(torch.cuda.is_available()) # 应该返回True print(torch.cuda.get_device_name(0)) # 显示GPU型号2.3 其他关键依赖ChatGLM2-6b需要一些特定版本的库pip install transformers4.33.3 icetk cpm_kernels sentencepiece gradio mdtex2html特别注意如果遇到protobuf版本冲突可以尝试pip install protobuf3.20.03. ChatGLM2-6b模型部署3.1 模型下载与准备从Hugging Face下载模型需要先同意协议git lfs install git clone https://huggingface.co/THUDM/chatglm2-6b cd chatglm2-6b如果网络不稳定可以使用镜像源git clone https://hf-mirror.com/THUDM/chatglm2-6b3.2 模型加载测试创建一个测试脚本test_load.pyfrom transformers import AutoTokenizer, AutoModel tokenizer AutoTokenizer.from_pretrained(./chatglm2-6b, trust_remote_codeTrue) model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).cuda() response, history model.chat(tokenizer, 你好, history[]) print(response)运行时应能看到模型成功加载并返回响应。如果出现OOM错误可能需要启用量化model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).quantize(4).cuda()3.3 常见加载问题解决问题1CUDA out of memory解决方案减小batch size或使用量化修改加载代码model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).half().cuda()问题2Transformers版本不兼容解决方案固定特定版本pip install transformers4.33.3问题3Protobuf冲突解决方案pip uninstall protobuf pip install protobuf3.20.04. P-Tuning微调实战4.1 准备训练数据创建train.jsonl文件格式示例{prompt: 解释神经网络, response: 神经网络是..., history: []} {prompt: Python的装饰器, response: 装饰器是..., history: []}建议数据量至少1000条领域要集中。4.2 配置P-Tuning参数修改ptuning/train.sh关键参数PRE_SEQ_LEN128 LR2e-4 NUM_GPUS1 torchrun --standalone --nnodes1 --nproc-per-node$NUM_GPUS main.py \ --do_train \ --train_file train.jsonl \ --validation_file dev.jsonl \ --pre_seq_len $PRE_SEQ_LEN \ --max_source_length 256 \ --max_target_length 256 \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 8 \ --gradient_accumulation_steps 4 \ --lr $LR \ --num_train_epochs 10 \ --save_steps 1000 \ --output_dir output \ --overwrite_output_dir4.3 启动训练bash train.sh监控GPU使用情况watch -n 1 nvidia-smi4.4 常见训练问题问题1Loss不下降检查学习率是否合适确认数据质量可能需要清洗数据尝试调整pre_seq_len通常64-256之间问题2GPU内存不足减小per_device_train_batch_size增加gradient_accumulation_steps使用--fp16参数启用混合精度问题3NaN损失尝试减小学习率添加梯度裁剪--max_grad_norm 1.05. 模型部署与性能优化5.1 量化部署为了减少显存占用可以使用4-bit或8-bit量化model AutoModel.from_pretrained(./chatglm2-6b, trust_remote_codeTrue).quantize(8).cuda()量化后显存占用可以从13GB降到6GB左右。5.2 使用vLLM加速安装vLLMpip install vllm启动API服务python -m vllm.entrypoints.api_server --model chatglm2-6b --tokenizer chatglm2-6b --tensor-parallel-size 15.3 性能监控与调优使用JTop监控pip install jetson-stats jtop关注以下指标GPU Util应该保持在70%以上Memory Usage避免接近100%Temperature保持在80°C以下我在实际部署中发现调整max_batch_size和max_seq_len对吞吐量影响很大需要根据具体硬件找到平衡点。