第二章 部署 Llama 2 大模型

部署前的准备

硬盘、CPU、GPU、内存

硬件要求

Llama2-Chinese-7b-Chat 模型要求:

  • 显存>12GB
  • RTX 2060 12GB, 3060 12GB, 3080, A2000及以上

软件环境准备

  1. Python 3.9.6 以上
  • 比较新的系统自带
  1. docker 24.0.2, docker-compose
# debian 安装 git
apt install git
apt install git-lfs

# 模型代码中有大文件需要配置下 git 不然会超时报错
git config --global http.version HTTP/1.1
git config --global http.postBuffer 524288000
git config --global http.timeout 600

git lfs install
  • debian系统使用 python3 开发套件,虚拟环境
# 安装 python3 开发套件,虚拟环境
apt-get install python3-dev python3-venv libopenblas-dev
# 创建 python3 虚拟环境
python3 -m venv Llama

# 激活虚拟环境
source Llama/bin/activate

# 更新 镜像源
python -m pip install --upgrade pip
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

# 更新 pip3 版本
pip3 install --upgrade pip

下载模型代码并安装模型需要的环境

mkdir /data
cd /data
git clone https://github.com/LlamaFamily/Llama-Chinese.git

# 安装 Llama 模型所需依赖
cd Llama-Chinese
pip install -r requirements.txt
# 下载模型
# git clone https://huggingface.co/FlagAlpha/Atom-7B-Chat.git
git clone https://www.modelscope.cn/FlagAlpha/Atom-7B-Chat.git
# 激活虚拟环境后,安装 pytorch
pip install torch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 --index-url https://download.pytorch.org/whl/cu124

# 安装 Transformers
pip install transformers

使用Atom-7B-Chat模型进行推理 创建一个名为 quick_start.py 的文件,并将以下内容复制到该文件中。

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
device_map = "cuda:0" if torch.cuda.is_available() else "auto"
model = AutoModelForCausalLM.from_pretrained('FlagAlpha/Atom-7B-Chat',device_map=device_map,torch_dtype=torch.float16,load_in_8bit=True,trust_remote_code=True,use_flash_attention_2=True)
model =model.eval()
tokenizer = AutoTokenizer.from_pretrained('FlagAlpha/Atom-7B-Chat',use_fast=False)
tokenizer.pad_token = tokenizer.eos_token
input_ids = tokenizer(['<s>Human: 介绍一下中国\n</s><s>Assistant: '], return_tensors="pt",add_special_tokens=False).input_ids
if torch.cuda.is_available():
  input_ids = input_ids.to('cuda')
generate_input = {
    "input_ids":input_ids,
    "max_new_tokens":512,
    "do_sample":True,
    "top_k":50,
    "top_p":0.95,
    "temperature":0.3,
    "repetition_penalty":1.3,
    "eos_token_id":tokenizer.eos_token_id,
    "bos_token_id":tokenizer.bos_token_id,
    "pad_token_id":tokenizer.pad_token_id
}
generate_ids  = model.generate(**generate_input)
text = tokenizer.decode(generate_ids[0])
print(text)

脚本中的 FlagAlpha/Atom-7B-Chat 需要改成本地下载

运行 quick_start.py 代码。

python quick_start.py

脚本启动失败,提示缺少flash_attn,flash_attn安装一直失败,提示缺少torch,实际是版本不对, 解决flash_attn报错,涉及flash_attn_2_cuda.cpython-310-x86_64-linux-gnu.so中的未定义符号问题open in new window 得上githubopen in new window下载,选择abifalse版本 ,并且和torch版本,cuda版本和python版本都对应的才能成功安装,

pip show torch
nvcc -V
python --version
wget https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.2.post1/flash_attn-2.7.2.post1+cu12torch2.5cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
# 卸载 bitsandbytes
pip uninstall bitsandbytes
pip install bitsandbytes

结果提示安装的flash_attn仅支持NVIDIA的 Ampere 架构(例如A100、V100等)及更新的GPU

RuntimeError: FlashAttention only supports Ampere GPUs or newer.

解决办法,替换gpu,如果gpu适配,得再检查版本问题

Last Updated:
Contributors: haizhiguang