跳到正文
DEV Community· gxqwb·· 2 小時前AI 評分48

LLM 服務中的推測解碼與大批次:提升速度的兩種方式

Speculative Decoding vs Bigger Batches: Two Ways to Speed Up LLM Serving

AI 導讀

推測解碼透過小型草稿模型預測多個 token,再由目標模型驗證,以減少連續推理步數;大批次則同時處理更多請求,提升整體吞吐量,但會增加每個用戶的延遲。兩者針對不同瓶頸,亦可結合使用;實際取捨取決於請求量、延遲要求及 GPU 資源狀況。

正文

Serving a large language model is a game of waiting. The GPU finishes one token, then waits for the next request, then finishes another token. Engineers have two very different ways to fill that waiting time: they can guess several tokens ahead with a small, cheap model, or they can group many requests together so the GPU works on a bigger batch at once. Both raise throughput, but they attack opposite problems.

Speculative decoding trades extra computation per request for fewer sequential model steps, while bigger batches trade longer per-token latency for more requests finished per second, and the right choice depends on whether your bottleneck is serial depth or idle parallelism.

That distinction matters because the two techniques are often presented as rivals, when in fact they can be combined. Understanding where each one spends its budget lets you reason about a real serving cluster instead of a benchmark slide.

Part One: What each technique actually does

Speculative decoding pairs a large target model with a small draft model. The draft proposes a run of, say, five tokens quickly. The target then checks all five in a single forward pass, accepting the ones it agrees with and rejecting the rest. When the draft is decent, most tokens are accepted, and the server advances five positions for roughly the cost of one target pass plus some cheap draft work. The serial chain of target calls — normally the slowest part — gets shorter.

Bigger batching does something more mundane. Instead of running one request through the model, the server collects many and processes them in one matrix multiplication. GPUs are wide machines; a batch of thirty-two can often run almost as fast as a batch of one, because the same hardware lanes that were idle are now doing useful work. The cost is that each request waits for the batch to fill and for the whole group to finish a step.

So one technique shortens the number of steps; the other increases the amount of work per step. They are orthogonal, which is why production systems increasingly use both.

Part Two: The arithmetic of the trade

Take a model that produces forty tokens per second for a single request, with each token needing a full pass over the weights. If a draft model is eighty percent accurate and proposes five tokens, the expected accepted run is around four tokens per target pass. That cuts target passes by roughly four times, pushing a single request toward more than a hundred tokens per second — provided the draft is small enough that its own cost stays minor. The gain is real but conditional: a draft that is only forty percent accurate wastes target passes on rejected tokens and can be slower than plain decoding.

Now consider batching. If a GPU serves one user at forty tokens per second and a batch of thirty-two at, say, twenty-five tokens per second per user, total throughput rises from forty to eight hundred tokens per second. Each user sees slower output, but the machine finishes far more work. For an API billing by the token, that is a direct revenue change. The global LLM inference market is projected to be worth tens of billions of dollars within a few years, and most of that value will be captured by whoever can serve more tokens perGPU-hour, which is exactly what this arithmetic decides.

Speculative decoding helps most when requests are few and latency is user-visible, while large batches help most when requests are plentiful and cost per token dominates, which is why interactive assistants and bulk APIs end up tuned differently.

That is the mechanism in one line: one technique serves the impatient single user, the other serves the crowded queue.

Part Three: Where each approach breaks

Speculative decoding degrades when the draft and target disagree. On creative or highly variable text, acceptance rates fall and the target ends up redoing work — the overhead of checking five tokens you will discard can exceed the saving. It also needs a second model resident in memory, which is awkward when the target already fills the accelerator. And it does nothing for throughput under heavy load: if the GPU is saturated with a big batch, shortening each request's steps does not create more capacity.

Large batching degrades when latency matters. A chat interface that waits for a batch to fill feels sluggish, and a slow user can hold up everyone else in the group. Batching also complicates memory: the key-value cache grows with every concurrent request, and a large batch can exhaust it, forcing eviction or recomputation. Under very bursty traffic, a server tuned for huge batches may thrash while one tuned for small batches stays smooth.

There is a subtle interaction too. Speculative decoding produces variable numbers of tokens per step, which makes batch scheduling harder — a batch that assumed uniform step sizes can finish unevenly. Production systems therefore treat the two as a joint scheduling problem rather than two independent knobs.

Part Four: An everyday analogy

Think of a busy coffee shop. Speculative decoding is like a barista who starts pouring milk into four cups before the espresso shots finish, guessing who wants what, then correcting the ones that were wrong. When the regulars order the same thing, this is brilliant. When everyone orders something unusual, the barista wastes milk.

Bigger batches are like brewing a large pot instead of one cup at a time. Each customer waits for the pot, but the shop serves thirty people for barely more effort than one. On a quiet morning with a single customer, the pot is wasteful. At the lunch rush, it is the only thing that keeps the line moving. A real shop does a bit of both: it preps likely orders and brews in bulk. A real inference server does the same.

What is actually transferable

The broader lesson is that speed-ups are only useful against the bottleneck you actually have. If your GPU sits idle between tokens waiting for serial steps, speculation fills that gap. If your GPU is busy but serving one request at a time, batching fills that gap. Measuring which one is idle — by watching utilisation and queue depth — tells you which lever to pull before you change any code.

A second lesson is that techniques which look competing are often composable. Speculation shortens the chain; batching widens each link. Together they can move both latency and throughput, but only if the scheduler accounts for variable-length steps. The practical takeaway is to instrument first, then choose: a single-user latency problem and a saturated-API cost problem are different diseases, and the same medicine will not cure both.

Speculative decoding and bigger batches are not two versions of one trick but two answers to two different questions, and a well-run serving stack asks which question it is facing before it picks one.

SOL: HCqPmPJM3NVtS6YrmVaTZYnuP8XZ2PFgQZKT94VK1LkS
Creating this takes real effort — thank you for the support.

來源:DEV Community · dev.to