ToB企服应用市场:ToB评测及商务社交产业平台

标题: llama.cpp制作GGUF文件及使用 [打印本页]

作者: 李优秀    时间: 2024-7-30 08:21
标题: llama.cpp制作GGUF文件及使用
llama.cpp的介绍

llama.cpp是一个开源项目,由Georgi Gerganov开辟,旨在提供一个高性能的推理工具,专为在各种硬件平台上运行大型语言模型(LLMs)而设计。这个项目标重点在于优化推理过程中的性能问题,特别是针对CPU环境。以下是关于llama.cpp的几个关键特性:
综上所述,llama.cpp是一个专为性能优化和广泛兼容性设计的工具,它不但能够帮助研究职员和开辟者在不同类型的硬件上高效运行大型语言模型,同时也是学习今世语言模型推理技术的一个良好资源。
GGUF文件的制作

装备环境如下:Ubuntu20.04、NVIDIA-A800、CUDA Version: 12.0、python 3.10
  1. #代码准备
  2. git clone https://github.com/ggerganov/llama.cpp
  3. cd llama.cpp
  4. #编译
  5. make
  6. # 获取官方模型权重并将其放入./models中
  7. ls ./models
  8. llama-2-7b tokenizer_checklist.chk tokenizer.model
  9. # [可选] 对于使用 BPE 分词器的模型
  10. ls ./models
  11. <folder containing weights and tokenizer json> vocab.json
  12. # [可选] 适用于 Mistral-7B 等 PyTorch.bin 模型
  13. ls ./models
  14. <folder containing weights and tokenizer json>
  15. # 安装Python依赖项
  16. python3 -m pip install -r requirements.txt
  17. # 将模型转换为ggml FP16格式
  18. python3 convert.py models/mymodel/
  19. # [可选] 对于使用 BPE 分词器的模型
  20. python convert.py models/mymodel/ --vocab-type bpe
  21. # 将模型量化为 4 位(使用 Q4_K_M 方法)
  22. ./quantize ./models/mymodel/ggml-model-f16.gguf ./models/mymodel/ggml-model-Q4_K_M.gguf Q4_K_M
  23. # 如果现在不支持旧版本,请将 gguf 文件类型更新为当前版本
  24. ./quantize ./models/mymodel/ggml-model-Q4_K_M.gguf ./models/mymodel/ggml-model-Q4_K_M-v2.gguf COPY
复制代码
运行量化模型

  1. # 开始推理 gguf 模型
  2. ./main -m ./models/mymodel/ggml-model-Q4_K_M.gguf -p "你好" -n 128
复制代码


  1. # 交互式使用模型
  2. ./main -m models/openbuddy-llama3-8b-v21.1-8k/openbuddy-llama3-8b-Q4_K_M.gguf -n 999 -cml
复制代码

  1. # 启动兼容openai api 的HTTP server
  2. ./server -m models/openbuddy-llama3-8b-v21.1-8k/openbuddy-llama3-8b-Q4_K_M.gguf -c 4096 --host 0.0.0.0 --port 7861
复制代码
命令行选项可见官方文档
模型量化的精度

根据自己的硬件配置来选择合适的精度
  1. (llamacpp) root@9pp562fqj4j6n-0:/1_11_test/hhh/llama.cpp# ./quantize
  2. usage: ./quantize [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--include-weights] [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--override-kv] model-f32.gguf [model-quant.gguf] type [nthreads]
  3.   --allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit
  4.   --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing
  5.   --pure: Disable k-quant mixtures and quantize all tensors to the same type
  6.   --imatrix file_name: use data in file_name as importance matrix for quant optimizations
  7.   --include-weights tensor_name: use importance matrix for this/these tensor(s)
  8.   --exclude-weights tensor_name: use importance matrix for this/these tensor(s)
  9.   --output-tensor-type ggml_type: use this ggml_type for the output.weight tensor
  10.   --token-embedding-type ggml_type: use this ggml_type for the token embeddings tensor
  11.   --keep-split: will generate quatized model in the same shards as input  --override-kv KEY=TYPE:VALUE
  12.       Advanced option to override model metadata by key in the quantized model. May be specified multiple times.
  13. Note: --include-weights and --exclude-weights cannot be used together
  14. Allowed quantization types:
  15.    2  or  Q4_0    :  3.56G, +0.2166 ppl @ LLaMA-v1-7B
  16.    3  or  Q4_1    :  3.90G, +0.1585 ppl @ LLaMA-v1-7B
  17.    8  or  Q5_0    :  4.33G, +0.0683 ppl @ LLaMA-v1-7B
  18.    9  or  Q5_1    :  4.70G, +0.0349 ppl @ LLaMA-v1-7B
  19.   19  or  IQ2_XXS :  2.06 bpw quantization
  20.   20  or  IQ2_XS  :  2.31 bpw quantization
  21.   28  or  IQ2_S   :  2.5  bpw quantization
  22.   29  or  IQ2_M   :  2.7  bpw quantization
  23.   24  or  IQ1_S   :  1.56 bpw quantization
  24.   31  or  IQ1_M   :  1.75 bpw quantization
  25.   10  or  Q2_K    :  2.63G, +0.6717 ppl @ LLaMA-v1-7B
  26.   21  or  Q2_K_S  :  2.16G, +9.0634 ppl @ LLaMA-v1-7B
  27.   23  or  IQ3_XXS :  3.06 bpw quantization
  28.   26  or  IQ3_S   :  3.44 bpw quantization
  29.   27  or  IQ3_M   :  3.66 bpw quantization mix
  30.   12  or  Q3_K    : alias for Q3_K_M
  31.   22  or  IQ3_XS  :  3.3 bpw quantization
  32.   11  or  Q3_K_S  :  2.75G, +0.5551 ppl @ LLaMA-v1-7B
  33.   12  or  Q3_K_M  :  3.07G, +0.2496 ppl @ LLaMA-v1-7B
  34.   13  or  Q3_K_L  :  3.35G, +0.1764 ppl @ LLaMA-v1-7B
  35.   25  or  IQ4_NL  :  4.50 bpw non-linear quantization
  36.   30  or  IQ4_XS  :  4.25 bpw non-linear quantization
  37.   15  or  Q4_K    : alias for Q4_K_M
  38.   14  or  Q4_K_S  :  3.59G, +0.0992 ppl @ LLaMA-v1-7B
  39.   15  or  Q4_K_M  :  3.80G, +0.0532 ppl @ LLaMA-v1-7B
  40.   17  or  Q5_K    : alias for Q5_K_M
  41.   16  or  Q5_K_S  :  4.33G, +0.0400 ppl @ LLaMA-v1-7B
  42.   17  or  Q5_K_M  :  4.45G, +0.0122 ppl @ LLaMA-v1-7B
  43.   18  or  Q6_K    :  5.15G, +0.0008 ppl @ LLaMA-v1-7B
  44.    7  or  Q8_0    :  6.70G, +0.0004 ppl @ LLaMA-v1-7B
  45.    1  or  F16     : 13.00G              @ 7B
  46.    0  or  F32     : 26.00G              @ 7B
  47.           COPY    : only copy tensors, no quantizing
复制代码
安卓上运行GGUF

手机配置


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4