LM Studio 0.3.10: 🔮 推测解码
•
2025-02-18
Llama 8B 作为主模型,Llama 1B 作为草稿模型,均在 M3 MacBook Pro 上使用 MLX 4 位量化
我们很高兴在 LM Studio 的 llama.cpp
和 MLX
引擎中引入推测解码支持!
推测解码是一种技术,在某些情况下可以将令牌生成速度提高 1.5 倍至 3 倍。
通过应用内更新或从 https://lm-studio.cn/download 升级 LM Studio 至 0.3.10。
推测解码是一种推理优化技术,由 Leviathan 等人的 通过推测解码从 Transformer 实现快速推理 以及 Chen 等人的 通过推测采样加速大型语言模型解码 等著作率先提出。它可以被视为一种您可能在现代 CPU 中找到的 推测执行 优化,但应用于 LLM 推理。
在 LM Studio 的 llama.cpp
和 MLX
引擎中,推测解码都是通过结合 2 个模型来实现的:一个更大的 LLM(“主模型”)和一个更小/更快的“草稿模型”(或“推测器”)。最初的 llama.cpp
实现由 Georgi Gerganov 编写,而 MLX
的实现由 Benjamin Anderson 和 Awni Hannun 编写。这些实现都在不断地被开源社区改进。
草稿模型首先运行,快速预测接下来的几个令牌作为“草稿”生成。紧接着,草稿生成的令牌由主模型确认或拒绝。只有那些会被主模型生成的令牌才会被接受。这会带来潜在的速度提升(当足够多的令牌被接受时),而不会降低质量。在令牌被拒绝多于接受的情况下,您可能会看到总生成速度降低!模型的选择对于获得最佳结果非常重要。
LM Studio + MLX 引擎 (Apple M3 Pro, 36GB RAM)
提示 | 主模型 | 草稿模型 | 无推测解码 | 有推测解码 | tok/秒 速度提升 |
---|---|---|---|---|---|
“用 Python 编写一个快速排序算法。只写代码。” | Qwen2.5-32B-Instruct-MLX-4bit | Qwen2.5-0.5B-Instruct-4bit | 7.30 tok/秒 | 17.74 tok/秒 | 2.43 倍 |
“解释勾股定理” | Meta-Llama-3.1-8B-Instruct-4bit | Llama-3.2-1B-Instruct-4bit | 29.65 tok/秒 | 50.91 tok/秒 | 1.71 倍 |
“计划一次华盛顿特区一日游” | Meta-Llama-3.1-8B-Instruct-4bit | Llama-3.2-1B-Instruct-4bit | 29.94 tok/秒 | 51.14 tok/秒 | 1.71 倍 |
LM Studio + CUDA llama.cpp 引擎 (NVIDIA RTX 3090 Ti 24GB VRAM, Intel Core Ultra 7 265K CPU, 32GB RAM)
提示 | 主模型 | 草稿模型 | 无推测解码 | 有推测解码 | tok/秒 速度提升 |
---|---|---|---|---|---|
“用 Python 编写一个快速排序算法。只写代码。” | Qwen2.5-32B-Instruct-GGUF (Q4_K_M) | Qwen2.5-0.5B-Instruct-GGUF (Q4_K_M) | 21.84 tok/秒 | 45.15 tok/秒 | 2.07 倍 |
“解释勾股定理” | Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0) | Llama-3.2-1B-Instruct-GGUF (Q4_0) | 50.11 tok/秒 | 68.40 tok/秒 | 1.36 倍 |
“计划一次华盛顿特区一日游” | Meta-Llama-3.1-8B-Instruct-GGUF (Q8_0) | Llama-3.2-1B-Instruct-GGUF (Q4_0) | 46.90 tok/秒 | 49.09 tok/秒 | 1.05 倍 |
在 LM Studio 0.3.10 中,您会找到一个新的推测解码侧边栏部分。一旦您加载了主模型(cmd/ctrl + L
),您将在新的草稿模型选择器中看到兼容的草稿模型选项。
配置侧边栏中的新草稿模型选择器
对于一个模型要用作另一个模型的草稿模型,它们需要“兼容”。在较高层面上,为了使草稿模型有效,它必须能够从较大的模型可能生成的相同令牌中生成。实际上,这两个模型应该共享足够相似的词汇表(模型“知道”的全部令牌)和分词器特征。LM Studio 会自动检查您的模型是否为了推测解码的目的而相互兼容。
LM Studio 将自动索引可能的主模型和草稿模型对的兼容性。
打开接受的草稿令牌可视化,以查看彩色令牌,指示它们是从草稿模型还是主模型生成的。绿色越多越好。
打开草稿令牌可接受性可视化,以更多地了解草稿模型的性能
您还可以通过 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 }
与聊天类似,您可以在配置侧边栏中设置草稿模型。设置后,针对此模型的请求将利用推测解码,使用您选择的草稿模型。
在服务器 UI 中为任何给定的主模型设置默认草稿模型
draft_model
您还可以将草稿模型键作为请求负载的一部分提供。了解有关 JIT 加载 的更多信息。
curl https://127.0.0.1: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 使用推测解码。
阅读文档文章:推测解码。
根据经验,目标是将大型模型与小型模型配对。草稿模型应该比主模型小得多,并且来自同一系列。例如,您可以将 Llama 3.2 1B 用作 Llama 3.1 8B 的草稿模型。
注意:截至 LM Studio 0.3.10,如果存在 GPU,系统将尝试将草稿模型完全卸载到 GPU 上。主模型 GPU 设置可以像往常一样配置。
很多这样的情况。对于某些任务和某些模型,您可以获得超过 2 倍的速度提升,而质量不会下降
两个主要因素会导致性能下降:草稿模型相对于您的可用资源而言太大,以及草稿接受率低。为了避免前者,始终使用比主模型小得多的草稿模型。后者取决于模型和提示。理解推测解码可能权衡的最佳方法是在您关心的任务上亲自尝试。
由于并非所有模型都相互兼容,因此识别可以作为草稿模型和主模型协同工作的模型非常重要。
一种简单的方法是使用同一模型系列的大型和小型变体,如下表所示
示例主模型 | 示例草稿模型 |
---|---|
Llama 3.1 8B Instruct | Llama 3.2 1B Instruct |
Qwen 2.5 14B Instruct | Qwen 2.5 0.5B Instruct |
DeepSeek R1 Distill Qwen 32B | DeepSeek R1 Distill Qwen 1.5B |
如果您知道您始终想要使用给定的草稿模型,您可以在“我的模型”中将其配置为每个模型的默认设置。一旦您这样做,当使用此模型时(通过聊天或 API),系统将启用推测解码,并使用您选择的草稿模型。
在“我的模型”中为任何给定的主模型设置默认草稿模型
我们为想要深入研究的您整理了一些额外的资源!
对这类工作感兴趣?我们正在招聘。请将您的简历 + 一份关于您自豪的项目的说明发送至 [email protected]。
以下摘录自 llama.cpp
和 MLX
,展示了当前用于检查模型之间推测解码兼容性的实现。
# 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
来源:
llama.cpp
: ggml-org/llama.cppMLX
: lmstudio-ai/mlx-engine**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