对于机器在内网,无法连接互联网的服务器来说,想要部署体验开源的大模型,需要拷贝各种依赖文件进行环境搭建难度较大,本文介绍如何通过制作docker镜像的方式,通过llama.cpp实现量化大模型的快速内网部署体验。
LLaMA 全称是Large Language Model Meta AI,是由Meta AI(原FacebookAI研究实验室)研究人员发布的一个预训练语言模型。该模型最大的特点就是基于以较小的参数规模取得了优秀的性能,模型参数量从7B到65B, 与其他大型语言模型一样,LLaMA的工作原理是将一连串的单词作为输入,并预测下一个单词,以递归地生成文本。
LLaMA.cpp 项目是开发者 Georgi Gerganov 基于 Meta 的 LLaMA 模型实现的纯 C/C++ 版本,用于模型推理。 无需任何额外依赖,相比 Python 代码对 PyTorch 等库的要求,C/C++ 直接编译出可执行文件,跳过不同硬件的繁杂准备,可以在笔记本上运行,大大降低了门槛。
项目开源地址:GitHub - ggerganov/llama.cpp: Port of Facebook's LLaMA model in C/C++
在dockerhub上下载对应的镜像版本,关注需要的cuda版本和操作系统版本。
docker pull nvidia/cuda:11.2.2-devel-ubuntu20.04
运行镜像
docker run -id --gpus all -v D:\download:/app/model -p 8080:8080 nvidia/cuda:11.2.2-devel-ubuntu20.04
进入容器:
docker exec -it xxxx /bin/bash
apt-get -y install git wget
修改国内源
gedit /etc/apt/sources.list
- deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
- deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
- deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
- deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
- deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
- deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
- deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
- deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
- deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
- deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
apt-get update
下载依赖
apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
下载python源码
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
tar -zvxf Python-3.10.0.tgz
cd Python-3.10.0/
配置
./configure --enable-optimizations
编译
make
安装
make install
更新python默认指向,删除旧的链接
remove /usr/bin/python
ln -s /usr/local/bin/python3.10 /usr/bin/python
ln -s /usr/local/bin/pip3 /usr/bin/pip
apt-get -y install build-essential libgl-dev libglib2.0-0 gcc g++ make cmake unzip curl
mkdir /app
cd /app
git clone GitHub - ggerganov/llama.cpp: Port of Facebook's LLaMA model in C/C++
cd llama.cpp && mkdir build && cd build && cmake .. -DLLAMA_CUBLAS=ON && cmake --build . --config Release
报错如下:
报错
CMake Error at CMakeLists.txt:252 (cmake_minimum_required):
CMake 3.17 or higher is required. You are running version 3.16.3
-- Configuring incomplete, errors occurred!
wget https://cmake.org/files/v3.23/cmake-3.23.0.tar.gz
tar -zxvf cmake-3.23.0.tar.gz
cd cmake-3.23.0
./configure
make -j8
make install
ln -s /usr/local/bin/cmake /usr/bin/cmake
重新编译安装
cmake .. -DLLAMA_CUBLAS=ON && cmake --build . --config Release
成功!
./server --host 0.0.0.0 -m /app/model/xxxx.gguf -c 128 -ngl 10