LM Studio 0.3.10: 🔮 投机采样 (Speculative Decoding)

2025-02-18

以 Llama 8B 为主模型,Llama 1B 为草稿模型,均在 M3 MacBook Pro 上以 MLX 4-bit 运行

我们非常激动地宣布,LM Studio 的 llama.cppMLX 引擎现已支持 投机采样 (Speculative Decoding)

投机采样是一种在某些情况下能将 Token 生成速度提高 1.5 到 3 倍的技术。

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


什么是投机采样

投机采样是一种推理优化技术,由 Leviathan 等人的《Fast Inference from Transformers via Speculative Decoding》以及 Chen 等人的《Accelerating Large Language Model Decoding with Speculative Sampling》等研究率先提出。它可以被看作是现代 CPU 中常见的分支预测/投机执行优化在 LLM 推理领域的应用。

在 LM Studio 的 llama.cppMLX 引擎中,投机采样是通过两个模型的组合实现的:一个较大的 LLM(“主模型”)和一个较小且更快的“草稿模型”(或称“投机器”)。最初的 llama.cpp 实现由 Georgi Gerganov 编写,而 MLX 的实现则由 Benjamin Anderson 和 Awni Hannun 贡献。这些实现正由开源社区不断改进。

工作原理

系统会首先运行草稿模型,快速预测接下来的几个 Token 作为“草稿”。紧接着,主模型会对这些草稿 Token 进行确认或拒绝。只有那些与主模型生成的 Token 一致的部分会被接受。这样可以在不损失质量的情况下大幅提升速度(当足够多的 Token 被接受时)。如果 Token 经常被拒绝,总生成速度可能会反而降低!因此,选择合适的模型组合对于获得最佳效果至关重要。

性能统计数据

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

提示词 (Prompt)主模型草稿模型未使用投机使用投机每秒 Token 数提升
“用 Python 写一个快速排序算法。只输出代码。”Qwen2.5-32B-Instruct-MLX-4bitQwen2.5-0.5B-Instruct-4bit7.30 tok/sec17.74 tok/sec2.43倍
“解释勾股定理”Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.65 tok/sec50.91 tok/sec1.71倍
“规划华盛顿特区的一日游”Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.94 tok/sec51.14 tok/sec1.71倍

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

提示词 (Prompt)主模型草稿模型未使用投机使用投机每秒 Token 数提升
“用 Python 写一个快速排序算法。只输出代码。”Qwen2.5-32B-Instruct-GGUF (Q4_K_M)Qwen2.5-0.5B-Instruct-GGUF (Q4_K_M)21.84 tok/sec45.15 tok/sec2.07倍
“解释勾股定理”Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)50.11 tok/sec68.40 tok/sec1.36倍
“规划华盛顿特区的一日游”Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)46.90 tok/sec49.09 tok/sec1.05倍

在对话中使用投机采样

在 LM Studio 0.3.10 中,你会发现侧边栏新增了一个“投机采样 (Speculative Decoding)”栏目。当你加载好主模型后(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:在服务器界面设置草稿模型

与对话界面类似,你可以在配置侧边栏设置草稿模型。设置完成后,针对该主模型的请求将自动利用你选择的草稿模型进行投机采样。

undefined

在服务器界面为特定主模型设置默认草稿模型

方案 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 兼容接口REST API 使用投机采样。

如何有效地使用投机采样

阅读文档文章:投机采样 (Speculative Decoding)

经验法则:尝试将大模型与同系列的小模型配对。草稿模型应远小于主模型,且来自同一家族。例如,你可以使用 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

进阶技巧:在“我的模型”中配置默认草稿模型

如果你确定某个模型总是要配合特定的草稿模型使用,可以在“我的模型 (My Models)”中将其设为默认。设置后,无论通过对话框还是 API 调用该模型,系统都会自动启用投机采样。

undefined

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

高级资源

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

对这类工作感兴趣?我们正在招聘。请将你的简历及引以为傲的项目介绍发送至 apply@lmstudio.ai

论文:

兼容性检查实现

以下是来自 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

更多内容