Skip to Content
LLM 服务和优化实践 (Chinese Edition)
book

LLM 服务和优化实践 (Chinese Edition)

by Chi Wang, Peiheng Hu
May 2026
Intermediate
374 pages
5h 3m
Chinese
O'Reilly Media, Inc.
Content preview from LLM 服务和优化实践 (Chinese Edition)

第7章. 高级LLM 优化技术

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在上一章的学习之后,您已经掌握了应对 LLM 服务优化中许多挑战的核心技术,特别是针对那些规模不大且能装入单张 GPU 的模型。对于参数规模更大的 LLMs(例如超过 1000 亿参数的模型),单张 GPU 通常不足以将模型加载到 GPU 内存中并以令人满意的延迟进行生成。在本章中,我们将探讨一些高级技术,以进一步提升 LLM 服务的性能,包括:

  • 利用推测性解码加速 LLM 生成任务的解码阶段,从而缩短令牌间延迟 (ITL)

  • 针对无法在单张 GPU 上运行或性能不足的大型 LLMs,采用多 GPU 和多节点服务架构

  • 预填充-解码(PD)解耦,以分离 预填充和解码阶段,并独立调整两者的权衡

  • 先进的键值对缓存技术,以实现极快的首次令牌生成时间(TTFT)和较高的缓存命中率

投机性解码

如果有一种技术能够单枪匹马地将延迟(尤其是 ITL)降低两到三倍,那会怎样?请认识推测性解码,这是一种新型方法,对于长篇且推理密集的生成任务特别有用。

在大型机器学习系统中,例如用于检索或推荐的百万级数据点场景下,通常会使用一个较小但精度较低的模型进行第一轮筛选。当候选数据点数量被大幅缩减至约 1,000 个后,再对剩余候选数据应用更大、更精确的模型以生成最终结果。

推测性解码的工作原理与此非常相似, 只不过是在令牌层面进行。它利用一个小型模型(我们称之为草稿模型)来帮助生成大型“目标模型”的候选令牌。 这加速了 LLM 生成过程中的解码阶段。换言之,在令牌生成过程中,我们会让小型草稿模型推测下一个令牌可能是什么。目标模型不再逐个生成令牌,而是负责验证已生成的令牌。如果目标模型发现草稿模型表现良好,它就会接受该令牌并跳过后续步骤。

详细步骤

我们将从一次投机解码迭代开始,即 ,其中草稿模型执行令牌投机,目标模型随后执行并行验证,如图 7-1 所示。

Diagram illustrating the process of speculative decoding with the draft model generating tokens and the target model performing verification in four steps.
图 7-1. 一次 投机性解码迭代分解为四个独立步骤

首先,草稿模型快速生成K 个令牌。这里的K代表草稿模型推测的令牌数量,这是一个需要调优以获得最优值的重要参数。紧接着,目标模型执行其前向传播,在此过程中验证草稿生成的令牌是否正确。

在令牌生成过程中,下一个令牌会以概率值的形式生成。例如,对于短语The soccer team of the United”,草稿模型可能会推测下一个令牌是States”,概率为0.6,而Kingdom的概率为0.3Nations的概率为0.1。如果目标模型认为States概率为0.8(高于0.6),它就会接受该草稿令牌。 如果目标模型认为States概率为0.4,通常我们会通过将0.4除以0.6来计算其概率。

如果目标模型仍接受该草稿令牌,则可继续验证下一个令牌。如果它决定拒绝该草稿令牌,我们将丢弃所有后续的草稿令牌。我们这样做是因为生成过程是自回归的,因此后续的令牌均无关紧要。候选模型将转而生成一个它认为合适的令牌。这在图 7-1 中由 Token3 表示。

重要的是,此过程不会影响准确性。换言之,推测性解码过程的最终输出应与非推测性自回归模型生成的结果完全一致。这主要通过验证步骤来实现。当某个令牌被拒绝时,目标模型会通过从其自身调整后的分布中采样一个新令牌来纠正问题,该分布经过调整以排除被拒绝的令牌。该过程将从这个新的、已确认的点重新开始。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

企业级Java开发中的应用人工智能 (Chinese Edition)

企业级Java开发中的应用人工智能 (Chinese Edition)

Alex Soto Bueno, Markus Eisele, Natale Vinto

Publisher Resources

ISBN: 0642572383695