当前位置: 首页 > news >正文

LLM | llama.cpp 安装使用(支持CPU、Metal及CUDA的单卡/多卡推理)

1. 详细步骤

1.1 安装 cuda 等 nvidia 依赖(非CUDA环境运行可跳过)

# 以 CUDA Toolkit 12.4: Ubuntu-22.04/24.04(x86_64) 为例,注意区分 WSL 和 Ubuntu,详见 https://developer.nvidia.com/cuda-12-4-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run
sudo sh cuda_12.4.1_550.54.15_linux.run注:通过 nvcc -V 来确定 cuda 等依赖是否安装到位,缺少的话根据提示再安装别的依赖,如 sudo apt install nvidia-cuda-toolkit 等

1.2 安装 llama.cpp (C/C++环境)

# 手动下载也可以
git clone https://github.com/ggerganov/llama.cppcd llama.cpp# 没安装 make,通过 brew/apt 安装一下(cmake 也可以,但是没有 make 命令更简洁)
# Metal(MPS)/CPU
make
# CUDA
make GGML_CUDA=1注:以前的版本好像一直编译挺快的,现在最新的版本CUDA上编译有点慢,多等一会

1.3 安装 llama-cpp (Python 环境)

# 也可以手动安装 torch 之后,再安装剩下的依赖
pip install -r requirements.txt

1.4 转换 HF 模型为 GGUF 文件

本步骤开始都以 Qwen2-7B-Instruct 为例

# 示例: 生成 FP-16 模型
python convert_hf_to_gguf.py /model_path/Qwen/Qwen-2.7B-Instruct/

1.5 GGUF 模型量化

# 示例: Q4_K_M 量化
./llama-quantize /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-f16.gguf /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf Q4_K_M

1.6 GGUF 模型测试

# Metal(MPS)/CPU
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant"
# CUDA: 单卡推理
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant" -ngl 9999
# CUDA: 多卡推理(以双卡为例),-ts等参数含义详见 https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant" -ngl 9999 -ts 1,1注: ngl可以灵活调整,取 9999 不是常规做法,比大模型实际的 100 以内的 ngl 大很多(不同模型的实际 ngl 也不一样)来确保所有的 ngl 都在 GPU 上运行(当然前提是显存足够)

2. 参考资料

2.1 llama.cpp

2.1.1 GitHub
官方页面

https://github.com/ggerganov/llama.cpp

build

https://github.com/ggerganov/llama.cpp/blob/master/docs/build.md

quantize

https://github.com/ggerganov/llama.cpp/blob/master/examples/quantize/README.md

http server

https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md

2.2 NVIDIA DEVELOPER

CUDA Toolkit Archive

https://developer.nvidia.com/cuda-toolkit-archive

CUDA Toolkit 12.4: Ubuntu-22.04/24.04(x86_64)

https://developer.nvidia.com/cuda-12-4-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local

3. 资源

3.1 llama.cpp

3.1.1 GitHub
Python Bindings for llama.cpp

https://github.com/abetlen/llama-cpp-python

3.2 NVIDIA DEVELOPER

官方页面

https://developer.nvidia.com/

CUDA Toolkit

https://developer.nvidia.com/cuda-downloads

http://www.lryc.cn/news/453760.html

相关文章:

  • 矩阵求解复数(aniwoth求解串扰)
  • Redis: Sentinel哨兵监控架构及环境搭建
  • C++ 语言特性30 - 模板介绍
  • 算法笔记(七)——哈希表
  • 【基础算法总结】链表篇
  • 探索路由器静态IP的获取方式
  • Vivado - JTAG to AXI Master (GPIO、IIC、HLS_IP)
  • Java中JWT(JSON Web Token)的运用
  • CSS3练习--电商web
  • Linux 默认内核版本更改
  • 【ubuntu】修改用户名、主机名、主文件夹名、登录名、密码
  • 深入理解JavaScript 的原型继承
  • Error while loading conda entry point: conda-libmamba-solver
  • FANUC机器人—PCDK
  • 如何在wsl中使用beyond compare
  • CNN+Transformer在自然语言处理中的具体应用
  • DotNetty ChannelRead接收数据为null
  • 3分钟学会下载 blender
  • 实现Xshell与虚拟机中Linux服务器的连接(附常见错误解决)
  • Rust 语言开发 ESP32C3 并在 Wokwi 电子模拟器上运行(esp-hal 非标准库、LCD1602、I2C)
  • 项目-坦克大战笔记-墙体销毁以及人机销毁
  • 硬件设计-利用环路设计优化PLL的输出性能
  • Vue入门-Node.js安装
  • OpenCV threhold()函数
  • Qt教程(002):Qt项目创建于框架介绍
  • 《C++游戏人工智能开发:开启智能游戏新纪元》
  • SPSS and Origin Paired Samples T-Test
  • 速成java记录(上)
  • 春秋云镜靶场之CVE-2022-28525
  • 【LLM】Agent在智能客服的实践(AI agent、记忆、快捷回复 | ReAct)