LM Studio 0.3.10: 🔮 推测解码

2025-02-18

Llama 8B 作为主模型,Llama 1B 作为草稿模型,均在 M3 MacBook Pro 上使用 MLX 4 位

我们很高兴地宣布,LM Studio 的 llama.cppMLX 引擎现已支持推测解码

推测解码是一种技术,在某些情况下可以将 token 生成速度提高 1.5 倍至 3 倍。

通过应用内更新或从 https://lm-studio.cn/download 将 LM Studio 升级到 0.3.10。


推测解码

推测解码是一种推理优化技术,由 Leviathan 等人的 Fast Inference from Transformers via Speculative Decoding 和 Chen 等人的 Accelerating Large Language Model Decoding with Speculative Sampling 等研究率先提出。它可以被看作是现代 CPU 中常见的推测执行优化的一种,但应用于大型语言模型推理。

在 LM Studio 的 llama.cppMLX 引擎中,推测解码是结合使用两个模型实现的:一个较大的大型语言模型(“主模型”)和一个较小/更快的“草稿模型”(或“推测器”)。最初的 llama.cpp 实现由 Georgi Gerganov 编写,MLX 的实现由 Benjamin Anderson 和 Awni Hannun 编写。这些实现正在由开源社区不断改进。

工作原理

首先运行草稿模型,快速预测接下来的几个 token 作为“草稿”生成。紧接着,草稿生成 token 将由主模型确认或拒绝。只有主模型会生成的 token 才会被接受。这可以在不降低质量的情况下实现潜在的加速(当接受足够多的 token 时)。在 token 更多被拒绝而非接受的情况下,你可能会看到总生成速度下降!模型的选择对于获得最佳结果非常重要。

性能统计

LM Studio + MLX 引擎 (Apple M3 Pro, 36GB RAM)

提示主模型草稿模型无推测有推测每秒 token 加速
“用 Python 编写一个快速排序算法。只写代码。”Qwen2.5-32B-Instruct-MLX-4bitQwen2.5-0.5B-Instruct-4bit7.30 token/秒17.74 token/秒2.43x
“解释勾股定理”Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.65 token/秒50.91 token/秒1.71x
“计划一个华盛顿特区一日游”Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.94 token/秒51.14 token/秒1.71x

LM Studio + CUDA llama.cpp 引擎 (NVIDIA RTX 3090 Ti 24GB VRAM, Intel Core Ultra 7 265K CPU, 32GB RAM)

提示主模型草稿模型无推测有推测每秒 token 加速
“用 Python 编写一个快速排序算法。只写代码。”Qwen2.5-32B-Instruct-GGUF (Q4_K_M)Qwen2.5-0.5B-Instruct-GGUF (Q4_K_M)21.84 token/秒45.15 token/秒2.07x
“解释勾股定理”Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)50.11 token/秒68.40 token/秒1.36x
“计划一个华盛顿特区一日游”Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)46.90 token/秒49.09 token/秒1.05x

在聊天中使用推测解码

在 LM Studio 0.3.10 中,你将找到一个新的推测解码侧边栏部分。加载主模型后(cmd/ctrl + L),你将在新的草稿模型选择器中看到兼容的草稿模型选项。

undefined

配置侧边栏中的新草稿模型选择器

草稿模型兼容性

为了使一个模型作为另一个模型的草稿模型使用,它们需要“兼容”。从高层次上讲,为了使草稿模型有效,它必须能够从与较大模型相同的可能 token 中生成。实际上,两个模型应该共享足够相似的词汇(模型“知道”的所有 token)和 tokenizer 特性。LM Studio 会自动检查你的模型是否相互兼容以用于推测解码。

undefined

LM Studio 将自动索引可能的主模型和草稿模型对的兼容性。

可视化接受的草稿 token

开启接受的草稿 token 可视化,查看彩色 token,指示它们是来自草稿模型还是主模型。越绿越好。

undefined

开启草稿 token 接受可视化,以更好地了解草稿模型性能

通过 API 使用推测解码

你还可以通过 LM Studio 的本地服务器使用推测解码。启用后,你将获得丰富的生成统计数据,包括与推测解码相关的新字段。

  "stats": {
    "tokens_per_second": 15.928616628606926,
    "time_to_first_token": 0.301,
    "generation_time": 1.382,
    "stop_reason": "stop",
    "draft_model": "lmstudio-community/llama-3.2-1b-instruct",
    "total_draft_tokens_count": 12,
    "accepted_draft_tokens_count": 10,
    "rejected_draft_tokens_count": 0,
    "ignored_draft_tokens_count": 2
  }
LM Studio REST API 返回的统计数据。在此处了解更多信息

选项 1:在服务器 UI 中设置草稿模型

与聊天类似,你可以在配置侧边栏中设置草稿模型。设置后,针对此模型的请求将利用你选择的草稿模型进行推测解码。

undefined

在服务器 UI 中为任何给定的主模型设置默认草稿模型

选项 2:在你的请求中加入 draft_model

你还可以将草稿模型密钥作为请求负载的一部分提供。了解更多关于 JIT 加载的信息。

curl https://:1234/api/v0/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-r1-distill-qwen-7b",
+   "draft_model": "deepseek-r1-distill-qwen-0.5b",
    "messages": [ ... ]
}'

你可以通过 LM Studio 的 OpenAI 兼容性 APIREST API 使用推测解码。

如何有效地使用推测解码

阅读文档文章:推测解码

根据经验法则,目标是将一个大型模型与一个小型模型配对。草稿模型应该比主模型小得多,并且来自同一个系列。例如,你可以使用 Llama 3.2 1B 作为 Llama 3.1 8B 的草稿模型。

注意:截至 LM Studio 0.3.10,如果存在 GPU,系统将尝试将草稿模型完全卸载到 GPU 上。主模型的 GPU 设置可以像往常一样配置。

undefined

许多这样的案例。对于某些任务和某些模型,你可以获得超过 2 倍的加速,而质量没有下降

何时预期性能会更差

两个主要因素将导致性能下降:草稿模型相对于可用资源过大,以及草稿接受率低。为避免前者,始终使用比主模型小得多的草稿模型。后者取决于模型和提示。了解推测解码可能权衡的最佳方法是在你关心的任务上亲自尝试。

查找兼容的草稿模型

由于并非所有模型都相互兼容,因此识别可以作为草稿模型和主模型协同工作的模型非常重要。

一种简单的方法是使用相同模型家族中的大型和小型变体,如下表所示

示例主模型示例草稿模型
Llama 3.1 8B InstructLlama 3.2 1B Instruct
Qwen 2.5 14B InstructQwen 2.5 0.5B Instruct
DeepSeek R1 Distill Qwen 32BDeepSeek R1 Distill Qwen 1.5B

专业提示:在 📂 我的模型中配置默认草稿模型

如果你知道你总是想使用某个草稿模型,你可以在“我的模型”中将其配置为每个模型的默认值。一旦你这样做,当使用此模型时(无论是通过聊天还是 API),系统都将启用推测解码,并使用你选择的草稿模型。

undefined

在“我的模型”中为任何给定主模型设置默认草稿模型

高级资源

我们为那些想要深入研究的人整理了一些额外资源!

对这类工作感兴趣?我们正在招聘。请将你的简历和一份你引以为豪的项目介绍发送至 [email protected]

论文:

兼容性检查实现

以下是来自 llama.cppMLX 的摘录,显示了检查模型之间推测解码兼容性的当前实现。

# The current MLX compat check is very minimal and might evolve in the future
def is_draft_model_compatible(self, path: str | Path) → bool:
    path = Path(path)
    draft_tokenizer = mlx_lm.tokenizer_utils.load_tokenizer(path)
    if draft_tokenizer.vocab_size != self.tokenizer.vocab_size:
        return False
    return True

来源:

0.3.10 - 完整更新日志

**Build 6**

- Fixed an issue where first message of tool streaming response did not include "assistant" role
- Improved error message when trying to use a draft model with a different engine.
- Fixed a bug where speculative decoding visualization does not work when continuing a message.

**Build 5**

- Bug fix: conversations would sometimes be named 'Untitled' regardless of auto naming settings
- Update MLX to enable Speculative Decoding on M1/M2 Macs (in addition to M3/M4)
- Fixed an issue on Linux and macOS where child processes may not be cleaned up after app exit
- [Mac][MLX] Fixed a bug where selecting a draft model during prediction would cause the model to crash

**Build 4**

- New: Chat Appearance > "Expand chat container to window width" option
  - This option allows you to expand the chat container to the full width of the window
- Fixed RAG not working due to "path must be a string"

**Build 3**

- The beginning and the end tags of reasoning blocks are now configurable in My Models page
  - You can use this feature to enable thinking UI for models that don't use `<think>` and
    `</think>` tags to denote reasoning sections
- Fixed a bug where structured output is not configurable in My Models page
- Optimized engine indexing for reduced start-up delay
- Option to re-run engine compatibility checks for specific engines from the Runtimes UI
- [Mac] Improved reliability of MLX runtime installation, and improved detection of
  broken MLX runtimes

**Build 2**

- Fixed a case where the message about updating the engine to use speculative decoding
  is not displayed
- Fixed a bug where we sometimes show "no compatible draft models" despite we are
  still identifying them
- [Linux] Fixed 'exit code 133' bug
  (reference: https://github.com/lmstudio-ai/lmstudio-bug-tracker/issues/285)

**Build 1**

- New: 🔮 Speculative Decoding! (for llama.cpp and MLX models)
  - Use smaller "draft model" to achieve generation speed up by up to 1.5x-3x for larger models.
  - Works best when combining very small draft model + large main model. The speedup comes
    without _any_ degradation in quality.
  - Your mileage may vary. Experiment with different draft models easily to find what works best.
  - Works in both chat UI and server API
  - Use the new "Visualize accepted draft tokens" feature to watch speculative decoding in action.
    - Turn on in chat sidebar.
- New: Runtime (cmd/ctrl + shift + R) page UI
- Auto update runtimes only on app start up
- Fixed a bug where multiple images sent to the model would not be recognized

更多