[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
> 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), 完成.

2. 下载通义千问1.5-7B模型

  • 安装git-lfs
brew install git-lfs

  • 访问 hugging face 上 qwen 1.5 模型

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

git clone https://huggingface.co/Qwen/Qwen1.5-7B-Chat

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

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

> 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 依赖

> python3 -m pip install -r requirements.txt

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

> 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 也可以考虑建立虚拟环境,接下来安装依赖。

> 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的目录。

> 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. 量化模型

> ./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. 运行测试

> ./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 模型命令。

#!/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


> ./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显存就完全跑满了。

./server -m ./models/mymodels/qwen1.5-chat-ggml-model-Q4_K_M.gguf -c 8192 -n -1 -t 7


  • chat completion接口

request

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

{
    "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

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


request

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

{
    "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

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


response

{
    "tokens": [
        6312,
        28709,
        460,
        368,
        3614
    ]
}


  • detokenize

request

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


response

{
    "content": " hello are you ok"
}


在这里插入图片描述

如何学习AI大模型?

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

在这里插入图片描述

👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。

在这里插入图片描述

1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓

在这里插入图片描述

<think>好的,我现在需要帮助用户在Ubuntu 22.04系统上部署Qwen2.5-VL-7B-Instruct-GGUF模型。首先,我得回忆一下用户提供的引用内容。引用[2]提到用户之前使用过联想Y7000配置的机器,安装了4060显卡、16G内存Ubuntu 22.04,微调过Qwen2.5-VL-2B模型。现在用户想部署7B的GGUF版本,可能需要类似的环境配置。 首先,安装GGUF模型通常需要llama.cpp或其衍生工具,因为GGUF是GGML的后续格式,专为CPU推理优化,但也支持GPU加速。用户有4060显卡,应该可以利用CUDA加速。因此,我需要确保系统中安装了NVIDIA驱动、CUDA工具包以及相关的依赖库。 接下来,用户可能需要下载Qwen2.5-VL-7B-Instruct的GGUF模型文件。根据经验,模型可能在Hugging Face或ModelScope上提供。需要指导用户如何正确下载模型文件,并验证文件的完整性,比如通过哈希校验。 然后,编译llama.cpp或兼容的推理引擎。这可能涉及从源码克隆仓库,安装必要的构建工具如CMake、g++,以及启用CUDA支持。在编译过程中,需要确保CUDA架构参数正确,比如针对RTX 4060的Compute Capability是8.9,但可能llama.cpp的最新版本已经支持,或者需要手动指定。 安装完成后,用户需要配置模型路径参数,编写一个简单的Python脚本或使用提供的示例代码来加载模型并进行推理。可能还需要安装Python绑定,如llama-cpp-python库,并确保其与系统已安装的CUDA版本兼容。 另外,用户可能关心内存显存的需求。7B的模型在GGUF格式下,量化等级不同会影响资源使用。例如,Q4_K_M的量化可能需要约5-6GB内存,而用户有16G内存8GB显存的4060,应该足够运行,但需要根据量化等级调整参数。 最后,测试模型是否正常运行,处理可能的错误,比如缺少动态库、权限题,或者模型加载失败。建议用户查看日志,安装必要的依赖,如OpenBLAS或其他数学库,以优化性能。 可能还需要注意Python环境的管理,使用虚拟环境如venv或conda来隔离依赖,避免与系统包冲突。同时,引用[1]中提到Qwen2.5-VL-72B的表现优异,但用户使用的是7B版本,可能需要调整预期性能,尤其是在处理视觉任务时的输入预处理步骤,不过GGUF模型可能仅支持文本,需确认模型是否是多模态的。如果Qwen2.5-VL-7B是视觉语言模型,可能需要额外的步骤处理图像输入,但GGUF格式可能仅支持部分功能,需查阅文档确认。 总结步骤大致为:安装依赖→下载模型→编译推理引擎→配置环境→运行测试。过程中需详细说明每个步骤的命令可能遇到的题,确保用户能逐步跟随操作。</think>### 在Ubuntu 22.04上部署Qwen2.5-VL-7B-Instruct-GGUF模型的步骤 #### 一、环境准备 1. **安装NVIDIA驱动CUDA工具包** ```bash sudo apt update sudo apt install nvidia-driver-535 cuda-toolkit-12-2 ``` 安装完成后验证: ```bash nvidia-smi # 查看GPU状态 nvcc --version # 查看CUDA版本 ``` 2. **安装基础依赖** ```bash sudo apt install build-essential cmake python3-pip python3-venv ``` #### 二、获取模型文件 1. **下载GGUF模型文件** 从Hugging Face或ModelScope获取模型文件(例如`qwen2.5-vl-7b-instruct.Q4_K_M.gguf`): ```bash wget https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct-GGUF/resolve/main/qwen2.5-vl-7b-instruct.Q4_K_M.gguf ``` #### 三、编译llama.cpp 1. **克隆仓库并编译** ```bash git clone https://github.com/ggerganov/llama.cpp cd llama.cpp make clean && LLAMA_CUDA=1 make -j8 ``` 编译参数说明: - `LLAMA_CUDA=1` 启用CUDA加速 - 若遇到架构错误,可通过`CMAKE_CUDA_ARCHITECTURES=89`指定RTX 4060的算力 #### 四、运行推理 1. **使用命令行测试** ```bash ./main -m /path/to/qwen2.5-vl-7b-instruct.Q4_K_M.gguf -n 512 --color -p "描述这张图片的内容:" ``` 参数说明: - `-n` 控制生成文本长度 - `--temp` 调整生成随机性 2. **Python API调用** 安装`llama-cpp-python`: ```bash pip install llama-cpp-python[cuBLAS] --force-reinstall --upgrade ``` 编写测试脚本: ```python from llama_cpp import Llama llm = Llama( model_path="qwen2.5-vl-7b-instruct.Q4_K_M.gguf", n_gpu_layers=35, # 根据显存调整层数 n_ctx=2048 ) response = llm.create_chat_completion( messages=[{"role": "user", "content": "描述这张图片的内容:"}] ) print(response["choices"][0]["message"]["content"]) ``` #### 五、性能优化建议 1. **量化等级选择** | 量化类型 | 文件大小 | 显存占用 | 质量保留 | |---------|----------|----------|----------| | Q2_K | ~3GB | 4GB | 低 | | Q4_K_M | ~5GB | 6GB | 中 | | Q6_K | ~7GB | 8GB | 高 | 2. **Batch参数调整** ```python llm = Llama( ... n_batch=512, # 增大batch提升吞吐量 n_threads=8 # 匹配CPU核心数 ) ``` #### 六、常见题解决 1. **CUDA内存不足** ```bash export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128 ``` 2. **缺少cuBLAS库** ```bash sudo apt install libopenblas-dev ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值