【个人开发】llama2部署实践(三)——python部署llama服务(基于GPU加速)

1.python环境准备

注:llama-cpp-python安装一定要带上前面的参数安装,如果仅用pip install装,启动服务时并没将模型加载到GPU里面。

shell 复制代码
# CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install llama-cpp-python
CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python

pip install uvicorn
pip install starlette
pip install fastapi
pip install sse_starlette
pip install starlette_context
pip install pydantic_settings

2.llama-cpp-python安装报错

报错踩坑1:

安装llama-cpp-python过程出现报错

CMake Error at vendor/llama.Cpp/CMakeLists.txt:186 (find library):

Could not find FOUNDATION LIBRARY using the followingnames: Foundation

解决方法1:

网上找到采用离线安装到方式,可以成功安装,但是高兴得太早了。

shell 复制代码
wget https://github.com/abetlen/llama-cpp-python/releases/download/v0.2.56/llama_cpp_python-0.2.56-cp311-cp311-manylinux_2_17_x86_64.whl
pip install llama_cpp_python-0.2.56-cp311-cp311-manylinux_2_17_x86_64.whl
# 参考链接:https://blog.csdn.net/qq_38463737/article/details/136477026

坑1:
虽然可以成功安装,但是启动llama服务的时候,没法用到GPU加速。

报错踩坑2:

倒腾了很久,看报错一直是cmake对问题,寻思着可能是gcc版本的问题,将gcc升级到13.1。但还是出现报错:

CMake Error at vendor/llama.Cpp/CMakeLists.txt:186 (find library):

Could not find FOUNDATION LIBRARY using the followingnames: Foundation

或者其他诸如的报错【报错太多,没法全记录下来】

CMake configuration failed

看上面的报错Compiling the CUDA compiler identification source file "CMakeCUDACompilerId.cu" failed。感觉像是cuda跟gcc版本的不兼容问题。

解决方法2:

捣鼓了很久,后来参考了一些成功安装的经验,最后选择gcc-9.4.0版本

附带一下gcc版本安装步骤:

shell 复制代码
## 安装
tar xf gcc-9.4.0.tar.xz
cd gcc-9.4.0/
./contrib/download_prerequisites
mkdir build && cd build
../configure --prefix=/usr/local/gcc-9.4.0 --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib
make -j16 # 我这里服务器是16核,这里根据服务器核数修改并行度。
make install 

## 配置环境变量
export GCC_ME_HOME=/usr/local/gcc-9.4.0/
PATH=$GCC_ME_HOME/bin:/usr/local/bin:$PATH
export PATH
LD_LIBRARY_PATH=$GCC_ME_HOME/lib:$GCC_ME_HOME/lib64:/usr/local/lib:/usr/local/lib64:/usr/lib64:/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
export CC=$GCC_ME_HOME/bin/gcc
export CXX=$GCC_ME_HOME/bin/g++

报错踩坑2:

后来还遇到这个问题

Could not find compiler set in environment variable CXX:

估摸着应该是CXX变量没加载的原因,重新加载环境变量,安装。

3.服务启动

shell 复制代码
python3 -m llama_cpp.server --model /data/opt/llama2_model/llama-2-7b-bin/ggml-model-f16.bin --n_threads 30 --n_gpu_layers 200


终于加载到GPU里面!

4.总结

当前环境各组件版本:

  • gcc:9.4.0
  • cuda:11.8
  • python:3.11.4
  • llama_cpp_python:0.2.56

以上,End

相关推荐
花酒锄作田10 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪14 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽14 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战15 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋20 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者2 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh2 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅2 天前
Python函数入门详解(定义+调用+参数)
python
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama