Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

使用 llm-mlx 和 Apple 的 MLX 框架在 macOS 上运行 LLM

Posted on 2025-02-16

llm-mlx是我的LLM Python 库和 CLI 实用程序的全新插件,它构建在 Apple 优秀的MLX 数组框架库和mlx-lm包之上。如果您是终端用户或使用 Mac 的 Python 开发人员,这可能是开始探索本地大型语言模型的最简单的新方法。

使用 llm-mlx 运行 Llama 3.2 3B

如果您尚未安装 LLM,则需要安装它– 您可以通过多种不同的方式来完成此操作 – 按照优先顺序,我喜欢uv tool install llm或pipx install llm或brew install llm或pip install llm 。

接下来,安装新插件(仅限 macOS):

 llm 安装 llm-mlx

现在下载并注册一个模型。 Llama 3.2 3B 是一个出色的首选 – 它相当小(下载 1.8GB),但却是一个功能惊人的入门模型。

 llm mlx 下载模型 mlx-community/Llama-3.2-3B-Instruct-4bit

这将从 Hugging Face 上的mlx-community/Llama-3.2-3B-Instruct-4bit下载 1.8GB 的​​模型权重并将其存储在此处:

 ~/.cache/huggingface/hub/models--mlx-community--Llama-3.2-3B-Instruct-4bit

现在您可以开始运行提示:

 llm -m mlx-community/Llama-3.2-3B-Instruct-4bit '遍历树的 Python 代码'

这对我来说是输出:

下面是如何在 Python 中遍历树的简单示例:

类节点:     def __init__ (自身,值):         自己。价值=价值         自己。孩子们= []  def traverse_tree (节点):     如果节点为None :         返回          print (节点.值)          对于节点中的子节点。孩子们:         遍历树(子树)  # 用法示例: 根=节点( “A” ) 根。子节点= [节点( “B” ),节点( “C” )] 根。儿童[ 0 ]。子节点= [节点( “D” ),节点( “E” )] 根。儿童[ 1 ]。孩子= [节点( “F” )]  traverse_tree ( root ) # 输出:A、B、D、E、C、F

在此示例中,我们定义一个Node类来表示树中的每个节点,并具有value属性和children列表。 traverse_tree函数递归访问树中的每个节点,并打印其值。

这是树遍历的基本示例,根据具体用例,有许多变化和优化。

那一代的运行速度高达每秒 152 个令牌!

该命令有点拗口,所以让我们为模型指定一个别名:

 llm 别名设置l32 mlx-community/Llama-3.2-3B-Instruct-4bit

现在我们可以使用更短的别名来代替:

 llm -m l32 '关于哈吉斯买车的笑话'

(这个笑话不太好。)

与 LLM 支持的其他模型一样,您也可以通过管道将内容传递给它。以下是如何让它解释一段 Python 代码(在本例中是它本身):

猫 llm_mlx.py | llm -m l32 '解释一下这段代码'

回应是这样开始的:

此代码是一个更大项目的一部分,该项目使用 Hugging Face Transformers 库创建文本到文本的对话式 AI 模型。该代码定义了一个自定义模型类MlxModel和一组用于处理 MLX 模型的命令行界面 (CLI) 命令。 […]

这是回复的其余部分。我对它在 1.8GB 的​​小型号上的表现感到非常惊讶!

该插件只能运行已转换为与 Apple 的 MLX 框架配合使用的模型。值得庆幸的是,Hugging Face 上的mlx 社区组织已经发布了 1,000 多篇此类内容。我尝试过的一些成功的:

  • mlx-community/Qwen2.5-0.5B-Instruct-4bit – 278MB
  • mlx-community/Mistral-7B-Instruct-v0.3-4bit – 4.08GB
  • mlx-community/DeepSeek-R1-Distill-Qwen-32B-4bit – 18.5GB
  • mlx-community/Llama-3.3-70B-Instruct-4bit – 40GB

最后一个需要一台具有 64GB RAM 的机器才能运行,但功能非常强大 – 我在 12 月份的文章《我现在可以在我的笔记本电脑上运行 GPT-4 类模型》中写过有关尝试 Ollama 版本的 Llama 3.3 70B 的文章。

 llm mlx 下载模型 mlx-community/Llama-3.3-70B-Instruct-4bit 

针对 Llama 3.3 70B 模型运行的 download-model 命令的屏幕截图,它显示了获取 13 个文件并显示 8 个不同的安全张量文件的进度条,每个文件的大小在 2.6 到 3.3GB 之间。

对于像这样的较大模型,我建议使用llm chat命令,因为这可以确保它们在多轮对话中保留在内存中:

 llm 聊天 -m mlx-community/Llama-3.3-70B-Instruct-4bit

活动监视器显示模型运行时使用 37.5GB RAM 的进程。

其中一些笑话其实还不错!

 Chatting with mlx-community/Llama-3.3-70B-Instruct-4bit Type 'exit' or 'quit' to exit Type '!multi' to enter multiple lines, then '!end' to finish > hi How's your day going so far? Is there something I can help you with or would you like to chat? > jokes about weird animals please Here are a few: 1. Why did the axolotl go to therapy? Because it was struggling to come to terms with its identity crisis... and also because it had a lot of "regrowing" to do! 2. What did the aye-aye say to the other aye-aye when they were out on a date? "You've got a finger on the pulse of my heart!" 3. Why did the naked mole rat go to the party? Because it was a "hair-raising" experience and it wanted to get to the "root" of the fun! 4. What do you call a group of flamingos playing instruments? The Pink Floyd... of the avian world, of course! 5. Why did the blobfish go to the doctor? It had a "gut" feeling something was wrong... and also because it was feeling a little "deflated"! Want more? I've got a whole zoo of 'em!

运行速度为每秒 8.8 个令牌。

您也可以在 Python 代码中使用 LLM 模型。自述文件包含有关如何执行此操作的文档– TLDR:

导入LLM 型号= llm 。 get_model ( “mlx-community/Llama-3.2-3B-Instruct-4bit” ) print ( model.prompt ( "hi" ) . text ())

MLX 和 mlx-lm

这个新插件背后的秘密武器是 Apple 的MLX 库,该库在过去一年中以惊人的速度改进 – 它于 2023 年 11 月首次在 GitHub 上发布。

核心库是 C++,但 Apple 的 MLX 团队还维护一个包含大量 Python 示例的mlx-examples存储库,包括mlx-lm和mlx-whisper包。

它周围的社区也在不断增长。 Prince Canuma 用于访问多模态视觉 LLM 的mlx-vlm库给我留下了特别深刻的印象 – 一旦我弄清楚如何使用它进行对话,我很乐意将其作为 LLM 插件使用。

我过去曾使用 MLX 进行过一些实验,但 MLX 核心开发人员 Awni Hannun 的这条推文最终说服我将其包装为 LLM 插件:

在最新的 MLX 中,小型法学硕士的速度要快得多。

在 M4 Max 4 位 Qwen 0.5B 上,以高达 510 令牌/秒的速度生成 1k 令牌。在 iPhone 16 Pro 上运行速度超过 150 tok/秒。

这确实是个好软件。 Apple 的这个小团队似乎几乎单枪匹马地与 NVIDIA 的 CUDA 展开竞争!

构建插件

llm-mlx插件很快就组合在一起了。第一个版本大约有 100 行 Python 代码,其中大部分是从我现有的llm-gguf插件中重新调整的。

最难的问题是弄清楚如何隐藏每次尝试访问模型时显示的 Hugging Face 进度条!

我最终通过 Gemini 2.0 通过管道传输库代码找到了from huggingface_hub.utils import disable_progress_bars实用函数。

然后我添加了模型选项支持,允许您传递如下选项:

 llm -m l32 '问候' -o 温度 1.0 -o 种子 2

到目前为止,使用固定种子似乎可以产生可重复的结果,这对于将来编写更多可预测的测试来说是令人兴奋的。

对于在 GitHub Actions 中运行的自动化测试,我决定使用一个小型模型 – 我使用了小型 75MB mlx-community/SmolLM-135M-Instruct-4bit (之前已探讨过)。我将 GitHub Actions 配置为在 CI 运行之间缓存模型,方法是将以下 YAML 添加到我的.github/workflows/test.yml文件中:

 -名称:缓存模型       使用: actions/cache@v4       和:         路径: ~/.cache/huggingface         键: $-huggingface-

标签:苹果、插件、项目、 ai 、 github-actions 、 generative-ai 、 llms 、 llm 、 mlx

原文: https://simonwillison.net/2025/Feb/15/llm-mlx/#atom-everything

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme