LLM-Llama在 MAC M1上体验Llama.cpp和通义千问Qwen 1.5-7B

Llama.cpp的主要目标是在各种硬件上(本地和云端)实现LLM推断,同时保持最小的设置和最先进的性能。

  • 纯C/C++实现,没有任何依赖关系
  • Apple芯片是一级的支持对象 - 通过ARM NEON、Accelerate和Metal框架进行优化
  • 对x86架构的AVX、AVX2和AVX512支持
  • 支持1.5位、2位、3位、4位、5位、6位和8位整数量化,以加快推断速度并减少内存使用
  • 为在NVIDIA GPU上运行LLMs而定制的CUDA内核(通过HIP支持AMD GPU)
  • Vulkan、SYCL和(部分)OpenCL后端支持
  • CPU+GPU混合推断,部分加速超过总VRAM容量的模型

而M1 上有GPU 芯片,因此让我们在 MAC M1 上运行大语言模型成为可能。

1. 下载

  1. 下载 llama.cpp
bash
复制代码
> git clone https://github.com/ggerganov/llama.cpp.git 
正克隆到 'llama.cpp'...
remote: Enumerating objects: 21605, done.
remote: Counting objects: 100% (6924/6924), done.
remote: Compressing objects: 100% (293/293), done.
remote: Total 21605 (delta 6761), reused 6711 (delta 6629), pack-reused 14681
接收对象中: 100% (21605/21605), 26.16 MiB | 3.31 MiB/s, 完成.
处理 delta 中: 100% (15230/15230), 完成.
  1. 下载通义千问1.5-7B模型
  • 安装git-lfs
复制代码
brew install git-lfs
  • 访问 hugging face 上 qwen 1.5 模型

这里我们使用 qwen 1.5 7B 的 chat 模型 huggingface.co/Qwen/Qwen1.…

bash
复制代码
git clone https://huggingface.co/Qwen/Qwen1.5-7B-Chat

如果你git下载不了 huggingface,也可以考虑浏览器下载文件放到该目录中。据了解chat模型和base模型区别是base模型只能续写,而cgat模型是在后面加了一些对话数据训练的,从而能够回复问题。下载好之后,目录如下

diff
复制代码
drwxr-xr-x  17 evilkylin  staff         544  4  3 11:21 .
drwxr-xr-x  10 evilkylin  staff         320  4  3 11:21 ..
drwxr-xr-x  12 evilkylin  staff         384  4  3 09:46 .git
-rw-r--r--   1 evilkylin  staff        1519  4  3 09:46 .gitattributes
-rw-r--r--   1 evilkylin  staff        6896  4  3 09:46 LICENSE
-rw-r--r--   1 evilkylin  staff        4338  4  3 09:46 README.md
-rw-r--r--   1 evilkylin  staff         663  4  3 09:46 config.json
-rw-r--r--   1 evilkylin  staff         243  4  3 09:46 generation_config.json
-rw-r--r--   1 evilkylin  staff     1671839  4  3 09:46 merges.txt
-rw-r--r--@  1 evilkylin  staff  3988014264  4  3 10:24 model-00001-of-00004.safetensors
-rw-r--r--@  1 evilkylin  staff  3957749080  4  3 10:23 model-00002-of-00004.safetensors
-rw-r--r--@  1 evilkylin  staff  3957749112  4  3 10:20 model-00003-of-00004.safetensors
-rw-r--r--@  1 evilkylin  staff  3539181096  4  3 10:16 model-00004-of-00004.safetensors
-rw-r--r--   1 evilkylin  staff       31696  4  3 09:46 model.safetensors.index.json
-rw-r--r--   1 evilkylin  staff     7028015  4  3 09:46 tokenizer.json
-rw-r--r--   1 evilkylin  staff        1402  4  3 09:46 tokenizer_config.json
-rw-r--r--   1 evilkylin  staff     2776833  4  3 09:46 vocab.json

2. 编译llama.cpp

csharp
复制代码
> cd llama.cpp
> make
I ccache not found. Consider installing it for faster compilation.
I llama.cpp build info: 
I UNAME_S:   Darwin
I UNAME_P:   arm
I UNAME_M:   arm64
.....
cc -I. -Icommon -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -DNDEBUG -DGGML_USE_ACCELERATE -DACCELERATE_NEW_LAPACK -DACCELERATE_LAPACK_ILP64 -DGGML_USE_METAL  -std=c11   -fPIC -O3 -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int -Werror=implicit-function-declaration -pthread -Wunreachable-code-break -Wunreachable-code-return -Wdouble-promotion  -c tests/test-c.c -o tests/test-c.o

3. 安装llama 依赖

markdown
复制代码
> python3 -m pip install -r requirements.txt

因为我是格式化后更新到 MAC OS 14.4 了,所以 python 有点问题。使用 homebrew 安装的 python 现在都安装在/opt/homebrew/bin/ , 这里我们要做两个链接。

shell
复制代码
> ln -s /opt/homebrew/bin/python3.10 /opt/homebrew/bin/python3
> ln -s /opt/homebrew/bin/python /opt/homebrew/bin/python

> ln -sf pip3.10 /opt/homebrew/bin/pip3
> ln -sf pip3.10 /opt/homebrew/bin/pip

如果你使用 conda 也可以考虑建立虚拟环境,接下来安装依赖。

scss
复制代码
> python3 -m pip install -r requirements.txt
Collecting numpy~=1.24.4 (from -r ./requirements/requirements-convert.txt (line 1))
  Downloading numpy-1.24.4-cp310-cp310-macosx_11_0_arm64.whl.metadata (5.6 kB)
...
Successfully installed MarkupSafe-2.1.5 certifi-2024.2.2 charset-normalizer-3.3.2 einops-0.7.0 filelock-3.13.3 fsspec-2024.3.1 gguf-0.6.0 huggingface-hub-0.22.2 idna-3.6 jinja2-3.1.3 mpmath-1.3.0 networkx-3.2.1 numpy-1.24.4 packaging-24.0 protobuf-4.25.3 pyyaml-6.0.1 regex-2023.12.25 requests-2.31.0 safetensors-0.4.2 sentencepiece-0.1.99 sympy-1.12 tokenizers-0.15.2 torch-2.1.2 tqdm-4.66.2 transformers-4.39.3 typing-extensions-4.10.0 urllib3-2.2.1

4. 转换 Qwen 模型为 GGUF

什么是 GGUF? GGUF是一种用于存储用于GGML推断和基于GGML的执行器的模型的文件格式。GGUF是一种二进制格式,旨在快速加载和保存模型,并易于阅读。传统上,模型是使用PyTorch或其他框架开发的,然后转换为GGUF以在GGML中使用。

GGUF是GGML、GGMF和GGJT的后继文件格式,旨在通过包含加载模型所需的所有信息来消除歧义。它还设计为可扩展的,因此可以向模型添加新信息而不会破坏兼容性,更多信息访问官方说明文档

Llama.cpp上是使用 convert.py, 但有人说 qwen 得用 convert-hf-to-gguf.py。

注意:有同学在这里报错找不到文件之类的,注意执行该命令的时候python3 convert-hf-to-gguf.py ~/Projects/Qwen1.5-7B-Chat/ 后面的路径要改为你之前在第1.2节中下载Qwen1.5-7B-Chat的目录。

ini
复制代码
> python3 convert-hf-to-gguf.py ~/Projects/Qwen1.5-7B-Chat/
Loading model: Qwen1.5-7B-Chat
gguf: This GGUF file is for Little Endian only
Set model parameters
gguf: context length = 32768
gguf: embedding length = 4096
gguf: feed forward length = 11008
gguf: head count = 32
gguf: key-value head count = 32
gguf: rope theta = 1000000.0
gguf: rms norm epsilon = 1e-06
gguf: file type = 1
Set model tokenizer
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained
....
output_norm.weight, n_dims = 1, torch.bfloat16 --> float32
Model successfully exported to '/Users/xxxx/Projects/Qwen1.5-7B-Chat/ggml-model-f16.gguf'

可以看到他已经转为 F16 的 gguf 格式的模型了。

5. 量化模型

vbnet
复制代码
> ./quantize ~/Projects/Qwen1.5-7B-Chat/ggml-model-f16.gguf ./models/qwen1.5-chat-ggml-model-Q4_K_M.gguf Q4_K_M
main: build = 2585 (f87f7b89)
main: built with Apple clang version 15.0.0 (clang-1500.3.9.4) for arm64-apple-darwin23.4.0
main: quantizing '/Users/evilkylin/Projects/Qwen1.5-7B-Chat/ggml-model-f16.gguf' to './models/qwen1.5-chat-ggml-model-Q4_K_M.gguf' as Q4_K_M
llama_model_loader: loaded meta data with 19 key-value pairs and 387 tensors from /Users/evilkylin/Projects/Qwen1.5-7B-Chat/ggml-model-f16.gguf (version GGUF V3 (latest))
....
llama_model_quantize_internal: model size  = 14728.52 MB
llama_model_quantize_internal: quant size  =  4540.59 MB

main: quantize time = 72620.01 ms
main:    total time = 72620.01 ms

我们将gguf 的模型量化到INT4,这样模型会从大约 14.7G减少到 4.4GB 左右。

6. 运行测试

sql
复制代码
> ./main -m ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf -n 128
Log start
main: build = 2585 (f87f7b89)
main: built with Apple clang version 15.0.0 (clang-1500.3.9.4) for arm64-apple-darwin23.4.0
main: seed  = 1712117398
llama_model_loader: loaded meta data with 20 key-value pairs and 387 tensors from ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf (version GGUF V3 (latest))
....
压力容器的定期检查(包括外部检查、内部检查和全面检查)分为每()年1次和每3~6年1次两种。
A. 1
B. 2
C. 3
D. 5 答案:D微量元素在生物体内虽少,但对生物体的生命活动起着非常重要的作用,下列选项中都属于微量元素的是(  ) A. 钙、铁、锌 B. 钾、镁、氟 C. 锌、硒、碘 D. 碳、氢、氧

钙、钾、
llama_print_timings:        load time =    8845.23 ms
llama_print_timings:      sample time =      31.94 ms /   128 runs   (    0.25 ms per token,  4007.51 tokens per second)
llama_print_timings: prompt eval time =       0.00 ms /     1 tokens (    0.00 ms per token,      inf tokens per second)
llama_print_timings:        eval time =   10612.83 ms /   128 runs   (   82.91 ms per token,    12.06 tokens per second)
llama_print_timings:       total time =   10899.90 ms /   129 tokens
ggml_metal_free: deallocating
Log end

看起来是自动化跑了个测试, 看输出显示大约 12.06 tokens 每秒,也就是说应该每秒至少 12 个字,但由于编码原因有可能字更多。 接下来我们进入对话模型。如何启动呢?我们这里查看example/alpaca.sh 的启动方式,来编写启动 Qwen 模型命令。

bash
复制代码
#!/bin/bash

#
# Temporary script - will be removed in the future
#

cd `dirname $0`
cd ..

./main -m ./models/alpaca.13b.ggmlv3.q8_0.bin \
       --color \
       -f ./prompts/alpaca.txt \
       --ctx_size 2048 \
       -n -1 \
       -ins -b 256 \
       --top_k 10000 \
       --temp 0.2 \
       --repeat_penalty 1.1 \
       -t 7

那么 qwen 就是这样了,具体的参数可以参看官方说明。实际上 Qwen1.5 的context window 支持大小是32768,官方文档。但我测试将 ctx size 设置为 32768 会跑不了,不知道为啥,还是自己不断尝试修改吧。另外 top k 默认 40,这个参数的意思用来增加随机性的,因为预测下一个token 时候的top k 个选择,一般默认就行。我估计应该和 temp 有关系吧。-t 是说并发线程数,根据自己 cpu 核数设置

vbnet
复制代码
> ./main -m ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf --color --ctx_size 2048 -n -1 -ins -b 256 --top_k 30 --temp 0.2 --repeat_penalty 1.1 -t 7
Log start
main: build = 2585 (f87f7b89)
main: built with Apple clang version 15.0.0 (clang-1500.3.9.4) for arm64-apple-darwin23.4.0
main: seed  = 1712120436
llama_model_loader: loaded meta data with 20 key-value pairs and 387 tensors from ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf (version GGUF V3 (latest))
....
== Running in interactive mode. ==
 - Press Ctrl+C to interject at any time.
 - Press Return to return control to LLaMa.
 - To return control without starting a new line, end your input with '/'.
 - If you want to submit another line, end your input with ''.
You are a helpful assistant.
> 你是谁
我是阿里云研发的大规模语言模型,我叫通义千问。

img

实际测试下来,输出确实很快,一点也不卡。 但是多次重复启动后,会直接退出,据说是 llama.cpp 出问题了,因此重新 make cleanmake就好了。

7. 像OpenAI一样输出

llama.cpp内置了一个c++写的快速,轻量级的http server,提供 OpenAI API一样的输入输出,还提供一个简单 web 前端来和 llama.cpp 交互。使用以下命令开启http server,测试了一下qwen 1.5 context window 开到 16384 在 m1 上会报内存不足,据了解token数量本身也会占用不少内存,和transformer本身的设计有关,具体原理就不展开了。当前开到 8192个token GPU先存就完全跑满了。

bash
复制代码
./server -m ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf -c 8192 -n -1 -t 7
  • chat completion接口

request

vbnet
复制代码
curl --location 'http://localhost:8080/v1/chat/completions' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "you are a helpful assitant"
    },
    {
      "role": "user",
      "content": "写一个笑话"
    }
  ],
  "stream": false
}'

response

swift
复制代码
{
    "choices": [
        {
            "finish_reason": "stop",
            "index": 0,
            "message": {
                "content": " Why did the tomato turn red?\n\nBecause it saw the salad dressing!\n\nI hope that brought a smile to your face. If you have any other questions or need assistance with something, feel free to ask!",
                "role": "assistant"
            }
        }
    ],
    "created": 1713099144,
    "model": "gpt-3.5-turbo",
    "object": "chat.completion",
    "usage": {
        "completion_tokens": 47,
        "prompt_tokens": 20,
        "total_tokens": 67
    },
    "id": "chatcmpl-jItjibb9dXIOO0YgrqwZHNxSeBfdBmlR"
}
  • embeddings

要返回 embedings需要在开启 server 的时候加上flag --embeddings

bash
复制代码
./server -m ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf -c 8192 -n -1 -t 7 --embeddings

request

css
复制代码
curl --location 'http://localhost:8080/v1/embeddings' \
--header 'Authorization: Bearer $OPENAI_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002",
    "encoding_format": "float"
  }'

response

json
复制代码
{
    "model": "text-embedding-ada-002",
    "object": "list",
    "usage": {
        "prompt_tokens": 0,
        "total_tokens": 0
    },
    "data": [
        {
            "embedding": [
                -0.005755452439188957,
                ....
                0.01070545706897974,
                0.011975807137787342
            ],
            "index": 0,
            "object": "embedding"
        }
    ]
}
  • Tokenize

request

css
复制代码
curl --location 'http://localhost:8080/tokenize' \
--header 'Content-Type: application/json' \
--data '{
    "content": "hello are you ok"
}'

response

yaml
复制代码
{
    "tokens": [
        6312,
        28709,
        460,
        368,
        3614
    ]
}
  • detokenize

request

css
复制代码
curl --location 'http://localhost:8080/detokenize' \
--header 'Content-Type: application/json' \
--data '{
    "tokens": [
        6312,
        28709,
        460,
        368,
        3614
    ]
}'

response

css
复制代码
{
    "content": " hello are you ok"
}

如何系统的去学习大模型LLM ?

作为一名热心肠的互联网老兵,我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。

但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的 AI大模型资料 包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来

😝有需要的小伙伴,可以V扫描下方二维码免费领取🆓

在这里插入图片描述

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

在这里插入图片描述

四、AI大模型商业化落地方案

img

阶段1:AI大模型时代的基础理解

  • 目标:了解AI大模型的基本概念、发展历程和核心原理。
  • 内容
    • L1.1 人工智能简述与大模型起源
    • L1.2 大模型与通用人工智能
    • L1.3 GPT模型的发展历程
    • L1.4 模型工程
      - L1.4.1 知识大模型
      - L1.4.2 生产大模型
      - L1.4.3 模型工程方法论
      - L1.4.4 模型工程实践
    • L1.5 GPT应用案例

阶段2:AI大模型API应用开发工程

  • 目标:掌握AI大模型API的使用和开发,以及相关的编程技能。
  • 内容
    • L2.1 API接口
      - L2.1.1 OpenAI API接口
      - L2.1.2 Python接口接入
      - L2.1.3 BOT工具类框架
      - L2.1.4 代码示例
    • L2.2 Prompt框架
      - L2.2.1 什么是Prompt
      - L2.2.2 Prompt框架应用现状
      - L2.2.3 基于GPTAS的Prompt框架
      - L2.2.4 Prompt框架与Thought
      - L2.2.5 Prompt框架与提示词
    • L2.3 流水线工程
      - L2.3.1 流水线工程的概念
      - L2.3.2 流水线工程的优点
      - L2.3.3 流水线工程的应用
    • L2.4 总结与展望

阶段3:AI大模型应用架构实践

  • 目标:深入理解AI大模型的应用架构,并能够进行私有化部署。
  • 内容
    • L3.1 Agent模型框架
      - L3.1.1 Agent模型框架的设计理念
      - L3.1.2 Agent模型框架的核心组件
      - L3.1.3 Agent模型框架的实现细节
    • L3.2 MetaGPT
      - L3.2.1 MetaGPT的基本概念
      - L3.2.2 MetaGPT的工作原理
      - L3.2.3 MetaGPT的应用场景
    • L3.3 ChatGLM
      - L3.3.1 ChatGLM的特点
      - L3.3.2 ChatGLM的开发环境
      - L3.3.3 ChatGLM的使用示例
    • L3.4 LLAMA
      - L3.4.1 LLAMA的特点
      - L3.4.2 LLAMA的开发环境
      - L3.4.3 LLAMA的使用示例
    • L3.5 其他大模型介绍

阶段4:AI大模型私有化部署

  • 目标:掌握多种AI大模型的私有化部署,包括多模态和特定领域模型。
  • 内容
    • L4.1 模型私有化部署概述
    • L4.2 模型私有化部署的关键技术
    • L4.3 模型私有化部署的实施步骤
    • L4.4 模型私有化部署的应用场景

学习计划:

  • 阶段1:1-2个月,建立AI大模型的基础知识体系。
  • 阶段2:2-3个月,专注于API应用开发能力的提升。
  • 阶段3:3-4个月,深入实践AI大模型的应用架构和私有化部署。
  • 阶段4:4-5个月,专注于高级模型的应用和部署。
这份完整版的大模型 LLM 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

😝有需要的小伙伴,可以Vx扫描下方二维码免费领取🆓

在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值