LLM 推理与服务 道练习题
36 道题 · 0 已解决
服务化 LLM 意味着在有限的 GPU 显存上,快速且低成本地响应大量用户。生成过程是逐个 token 进行的,模型将过去的 keys 和 values 存储在 KV cache 中,以避免重复计算。运行服务系统有点像经营一家繁忙的餐厅厨房:你需要对订单进行 batching,重复利用备好的食材,并让每个灶台都保持运转。这些题目涵盖了现代推理引擎背后的核心概念。你将学习使用 KV cache 生成 token,计算 GPU 显存能容纳多少 token,以 page 的形式分配 KV 内存,使用 LRU 逐出 cache blocks,通过 token trie 统计 prefix cache 命中率,评估 decoding 是否为 memory-bound,模拟 continuous batching 和 chunked prefill 调度器,在 speculative decoding 中验证 draft tokens,计算 TTFT、TPOT 和吞吐量,利用 Little's law 衡量并发量,并根据缓存的 input tokens 计算请求价格。从关于延迟指标、显存容量规划和定价的 Easy 题目开始。然后进行 KV cache 解码、paging 和 prefix caching,最后完成 batching、chunked prefill 和 speculative sampling。推理成本直接驱动产品决策,这些主题在 LLM 基础设施面试中经常出现。
简单 12
- Trim a chat history to fit the context window
- Work out how many tokens fit in GPU memory for the KV cache
- Compute TTFT, TPOT and throughput from request timestamps
- Price an API request with cached input tokens
- Size concurrency with Little's law
- Write new keys and values into a preallocated batched KV cache
- Pick the best draft length for speculative decoding
- Measure goodput against TTFT and TPOT SLOs
- Measure the wasted slots of static batching
- Rate limit API requests with a token bucket
- Size prefill and decode pools for disaggregated serving
- Find the largest batch size that fits in memory
中等 17
- Generate one token at a time with a KV cache
- Allocate KV cache memory in pages, like vLLMPro
- Verify a draft model's guesses in speculative decodingPro
- Is decoding memory-bound? A roofline estimatePro
- Count prefix cache hits with a token trie and block granularityPro
- Evict KV cache blocks with an LRU policyPro
- Gather a paged KV cache through a block tablePro
- Run a paged KV block allocator with a free listPro
- Route requests to replicas by shared prompt prefixPro
- Serve many LoRA adapters in one batchPro
- Compare FCFS and shortest-job-first latency for LLM requestsPro
- Group incoming requests into batches by size or wait timePro
- Parse a server-sent event stream that arrives in random chunksPro
- Build a key-value cache with expiry and LRU evictionPro
- Route requests to replicas with a consistent hash ringPro
- Answer repeat questions from a semantic cachePro
- Build an LRU cache for model responses
困难 7
- Simulate continuous batchingPro
- Simulate a chunked prefill scheduler with a per-step token budgetPro
- Accept or reject draft tokens in speculative samplingPro
- Simulate KV memory preemption with recomputePro
- Track shared KV blocks with copy-on-write for beam searchPro
- Stream text safely when stop sequences can span chunksPro
- Rate limit API requests with compound and payload-aware rulesPro