预览版推出:LM Link立即开始

LM Studio 0.3.10: 🔮 推测性解码 (Speculative Decoding)

2025年2月18日·

以 Llama 8B 作为主模型,Llama 1B 作为草稿模型,两者均为 M3 MacBook Pro 上的 MLX 4-bit 版本

我们非常高兴地宣布,在 LM Studio 的 llama.cppMLX 引擎中现已支持推测性解码 (Speculative Decoding)

推测性解码是一种优化技术,在某些情况下可以将 Token 生成速度提升 1.5x 至 3x。

您可以通过应用内更新,或访问 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 中常见的一种推测执行 (speculative execution)优化技术在 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)

提示词主模型草稿模型未使用推测使用推测tok/sec 加速比
"Write a quicksort algorithm in Python. Write code only." (用 Python 编写快速排序算法。只输出代码。)Qwen2.5-32B-Instruct-MLX-4bitQwen2.5-0.5B-Instruct-4bit7.30 tok/sec17.74 tok/sec2.43x
"Explain the Pythagorean theorem" (解释勾股定理)Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.65 tok/sec50.91 tok/sec1.71x
"Plan a 1 day trip to DC" (规划华盛顿特区一日游)Meta-Llama-3.1-8B-Instruct-4bitLlama-3.2-1B-Instruct-4bit29.94 tok/sec51.14 tok/sec1.71x

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

提示词主模型草稿模型未使用推测使用推测tok/sec 加速比
"Write a quicksort algorithm in Python. Write code only." (用 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.07x
"Explain the Pythagorean theorem" (解释勾股定理)Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)50.11 tok/sec68.40 tok/sec1.36x
"Plan a 1 day trip to DC" (规划华盛顿特区一日游)Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0)Llama-3.2-1B-Instruct-GGUF (Q4_0)46.90 tok/sec49.09 tok/sec1.05x

在聊天中使用推测性解码

在 LM Studio 0.3.10 中,您会在侧边栏中发现一个全新的“推测性解码”区域。加载主模型(快捷键 cmd/ctrl + L)后,您就可以在草稿模型选择器中看到相兼容的草稿模型选项。

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

草稿模型的兼容性

要将一个模型作为另一个模型的草稿模型使用,它们必须互相同步“兼容”。通俗地说,草稿模型必须能够预测出较大模型也有可能生成的相同 Token 范围。具体在应用上,这意味着两个模型应当拥有高度相似的词表(即模型“认识”的所有 Token 集合)以及相同的分词器(tokenizer)属性。LM Studio 会在后台自动检测您下载的模型是否在推测性解码上相互兼容。

LM Studio 将自动建立并索引各主模型与候选草稿模型的兼容关系对。

可视化接受的草稿 Token

开启“草稿 Token 接受情况可视化”,可以通过颜色高亮来区分生成的 Token 哪些来自草稿模型,哪些来自主模型。绿色的占比越高,说明加速效果越佳。

开启草稿 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 中直接指定草稿模型

与在聊天界面操作类似,您可以在服务器配置侧边栏中直接设置一个草稿模型。设置后,所有指向该主模型的 API 请求均会自动通过您所指定的草稿模型执行推测性解码。

在服务器控制界面中为特定主模型配置默认关联的草稿模型

方式 2:在请求体中传参 draft_model

您也可以直接将草稿模型标识(Key)放入 API 的请求负载中。了解更多关于 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 兼容 API 还是自身的 REST API,您均可以调用推测性解码。

如何高效合理地利用推测性解码

阅读参考文档了解详情:推测性解码 (Speculative Decoding)

一条行之有效的黄金法则是:“一大配一小”。草稿模型应比主模型体量小得多,且通常属于同一个模型家族。例如,极力推荐将 Llama 3.2 1B 作为 Llama 3.1 8B 的推测草稿模型。

注意:自 LM Studio 0.3.10 起,系统会默认尝试将草稿模型全部卸载(offload)到 GPU(如果设备包含可用 GPU)。主模型的 GPU 设置一如既往可手动灵活调控。

在许多类似场景和模型应用下,某些任务的处理速度可以提升 2 倍以上,且完全没有任何文本质量的下降

何种情况下性能可能不升反降?

通常有两大核心因素会导致性能变差:第一,相对于设备可支配的物理硬件资源,草稿模型过大;第二,草稿生成的 Token 接受率太低。为规避第一点,请务必选择体量极其微小、与主模型规模相比非常悬殊的草稿模型。而第二点则严重受模型种类和提示词内容的影响。评估和衡量推测性解码收益最好的做法,是在您实际具体的业务与任务场景下亲自进行测试和实测对比。

寻找兼容的草稿模型

因为并非任意模型组合都可以顺畅搭配使用,所以在选择前,首要任务是确定能够携手工作的主模型与兼容草稿模型。

最省心的方案是挑选同一系列中“一小一大”的变体模型,详情可参考下方对应列表:

主模型示例兼容草稿模型示例
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 中绑定并固化默认草稿模型

如果您有特定偏好,希望某些主模型在启动时总是默认携带其固定的草稿模型,可以在“My Models (我的模型)”设置页中为其单独绑定默认的草稿模型。设置好后,无论是通过聊天还是在 API 环境中调用该主模型,系统都将默认启动您的设定并激活推测性解码。

在 “My Models” 管理界面下为任意主模型设定其首选的草稿模型

高阶拓展资源

我们为渴望探索底层和深层技术原理的用户整理了一些学术界与开源领域的优秀拓展资料!

如果您对大模型的前沿优化工作充满热情,我们正在火热招聘中。请发送您的简历,并附带一篇令您感到自豪的代表性项目说明至: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

更多内容

© . This site is unofficial and not affiliated with Element Labs, Inc.