# LLM 推理手册 > 一本面向工程师的实用手册,帮助你构建、优化、扩展并运维生产环境中的 LLM 推理系统。 在任意 URL 后追加 `.md` 即可查看该页面的 Markdown 版本。 - [LLM 推理手册](https://dpthinker-ai.github.io/llm-inference-handbook/index.md):一本面向工程师的实用手册,帮助你构建、优化、扩展并运维生产环境中的 LLM 推理系统。 ## Table of Contents - [Bring Your Own Cloud (BYOC)](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/bring-your-own-cloud.md): Bring Your Own Cloud (BYOC) 是一种部署模式,供应商在你的云中运行软件,将托管编排与完整的数据控制结合起来。 - [计算为 LLM 服务所需的 GPU 内存](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/calculating-gpu-memory-for-llms.md): 学习如何计算为 LLM 提供服务时所需的 GPU 内存。 - [选择合适的 GPU](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/choosing-the-right-gpu.md): 为 LLM 推理选择合适的 NVIDIA 或 AMD GPU(例如 L4、A100、H100、B200、MI250X、MI300X、MI350X)。 - [选择合适的推理框架](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/choosing-the-right-inference-framework.md): 了解 LLM 推理框架的作用、为什么原始模型执行不足以支撑生产环境,以及如何为你的用例选择合适的推理框架。 - [选择合适的模型](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/choosing-the-right-model.md): 为你的用例选择合适的模型。 - [规划部署方案](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started.md): 在生产环境中运行 LLM 之前,你需要先做出几个关键决策。这些早期选择会直接 - [On-prem LLM 部署](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/on-prem-llms.md): On-prem LLM 是部署在组织自有基础设施中的大语言模型,例如私有数据中心或气隙环境。这种模式提供对数据、模型、性能和成本的完全控制。 - [Serverless vs. self-hosted LLM 推理](https://dpthinker-ai.github.io/llm-inference-handbook/getting-started/serverless-vs-self-hosted-llm-inference.md): 理解 serverless LLM API 与 self-hosted LLM 部署之间的差异。 - [LLM 推理通常运行在哪里?](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics/cpu-vs-gpu-vs-tpu.md): 了解 CPU、GPU 和 TPU 的差异,以及它们各自适合部署到哪里。 - [LLM 是如何工作的?](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics/how-does-llm-inference-work.md): 了解 LLM 的工作方式,包括分词、Transformer 架构(注意力、注意力掩码)以及推理中的 prefill 和 decode 阶段。 - [基础](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics.md): LLM 推理是模型真正接触现实世界的阶段。它支撑着从即时聊天回复到代码生成的 - [LLM 推理的关键指标](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics/llm-inference-metrics.md): 通过衡量延迟、吞吐量等关键指标来优化 LLM 推理性能。 - [训练与推理](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics/training-inference-differences.md): LLM 训练负责构建模型,LLM 推理则把模型应用到新输入上并生成实时输出。 - [什么是 LLM 推理?](https://dpthinker-ai.github.io/llm-inference-handbook/llm-inference-basics/what-is-llm-inference.md): LLM 推理是利用训练好的语言模型,根据提示词生成响应或预测的过程。 - [数据并行、张量并行、流水线并行、专家并行和混合并行](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/data-tensor-pipeline-expert-hybrid-parallelism.md): 了解数据并行、张量并行、流水线并行、专家并行和混合并行之间的差异。 - [推理优化](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization.md): 让 LLM 跑起来只是起点。真正让它变得更快、更高效、可扩展,才是推理优化要 - [推理路由](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/inference-routing.md): 使用缓存局部性、队列深度、KV 缓存压力和 worker 状态来路由 LLM 请求,以获得更低延迟和更好的利用率。 - [KV 缓存卸载](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/kv-cache-offloading.md): 了解 KV 缓存卸载如何通过减少 GPU 内存占用、降低延迟并削减计算成本来改进 LLM 推理。 - [LLM 性能基准](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/llm-performance-benchmarks.md): LLM 性能基准是标准化测试,用于衡量 LLM 在特定条件下的表现。它们关注吞吐量、延迟、成本效率和资源利用率等实际指标。 - [离线批量推理](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/offline-batch-inference.md): 使用离线批量推理在大规模场景下高效生成结果,适合非实时处理任务。 - [PagedAttention](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/pagedattention.md): 通过 PagedAttention 基于块的 KV 缓存存储方式改进 LLM 内存使用。 - [预填充-解码分离](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/prefill-decode-disaggregation.md): 将预填充和解码分离,以获得更好的并行执行、资源分配和扩展能力。 - [前缀缓存](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/prefix-caching.md): 前缀缓存通过在请求之间复用共享提示词的 KV 缓存来加速 LLM 推理。 - [推测解码](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/speculative-decoding.md): 推测解码通过由 draft model 预测、再由 target model 验证的方式加速 LLM 推理。 - [静态、动态与连续批处理](https://dpthinker-ai.github.io/llm-inference-handbook/inference-optimization/static-dynamic-continuous-batching.md): 通过静态、动态和连续批处理优化 LLM 推理,以获得更高的 GPU 利用率。 - [FlashAttention](https://dpthinker-ai.github.io/llm-inference-handbook/kernel-optimization/flashattention.md): FlashAttention 是一种面向 Transformer 的快速且内存高效的 attention 算法,可加速 LLM 训练与推理,并帮助实现更长的上下文窗口。 - [GPU 架构基础](https://dpthinker-ai.github.io/llm-inference-handbook/kernel-optimization/gpu-architecture-fundamentals.md): 理解 kernel 优化所需的 GPU 架构基础,包括 thread、warp、streaming multiprocessor、memory hierarchy 和 tensor core。 - [内核优化](https://dpthinker-ai.github.io/llm-inference-handbook/kernel-optimization.md): 内核优化关注的是如何让 GPU 内核运行得更快、更高效,核心在于改进它对计算、 - [面向 LLM 推理的 kernel 优化](https://dpthinker-ai.github.io/llm-inference-handbook/kernel-optimization/kernel-optimization-for-llm-inference.md): 面向 LLM 推理的 kernel 优化通过编写或生成针对 LLM 计算模式定制的优化 kernel,提升 GPU 利用率与性能。 - [选择合适的 kernel 优化工具](https://dpthinker-ai.github.io/llm-inference-handbook/kernel-optimization/kernel-optimization-tools.md): 对比 LLM 推理中 kernel 优化的主要工具,从 cuBLAS、cuDNN 到 TVM、XLA、Triton、自定义 CUDA kernel、Mojo 和 MAX。 - [模型准备](https://dpthinker-ai.github.io/llm-inference-handbook/model-preparation.md): 模型准备指的是让模型能够更适合投入推理阶段的一系列工作。 - [LLM 蒸馏](https://dpthinker-ai.github.io/llm-inference-handbook/model-preparation/llm-distillation.md): 了解 LLM 蒸馏如何工作、它与量化的区别,以及如何借助它构建更小、更快、更高效的推理模型。 - [LLM 微调](https://dpthinker-ai.github.io/llm-inference-handbook/model-preparation/llm-fine-tuning.md): 了解 LLM 微调以及不同的微调框架。 - [LLM 量化](https://dpthinker-ai.github.io/llm-inference-handbook/model-preparation/llm-quantization.md): 了解 LLM 量化以及不同的量化格式与方法。 - [Anthropic-compatible API](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/anthropic-compatible-api.md): Anthropic-compatible API 复现了 Anthropic 的 Messages API,因此基于 Claude 的客户端、SDK 和 agent 工具只需极少代码改动即可使用其他模型或 provider。 - [函数调用(Function calling)](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/function-calling.md): 了解 function calling 是什么,以及它适用于哪些场景。 - [模型交互](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction.md): 模型交互指的是为了获得期望输出而与模型通信的过程,也就是向模型发送提示词, - [LLM 推理参数](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/inference-parameters.md): LLM inference parameters 是请求时设置,用于控制随机性、输出长度、重复、停止行为、可复现性以及 structured generation。 - [Model Context Protocol](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/model-context-protocol.md): 了解 Model Context Protocol(MCP)是什么,以及它的典型用法。 - [OpenAI-compatible API](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/openai-compatible-api.md): OpenAI-compatible API 实现了与 OpenAI 官方 API 相同的请求和响应格式,使开发者能够在不修改现有代码的情况下切换不同模型。 - [提示工程(Prompt engineering)](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/prompt-engineering.md): 理解面向 LLM inference 的 prompt engineering。学习 system prompt、user prompt、zero-shot 和 few-shot prompting、KV cache 影响、token 成本以及生产最佳实践。 - [结构化输出(Structured outputs)](https://dpthinker-ai.github.io/llm-inference-handbook/model-interaction/structured-outputs.md): Structured outputs 是采用 JSON、XML 等定义格式的模型响应,使 AI 生成的数据可预测、机器可读,并且易于集成到应用和工作流中。 - [构建与维护成本](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/build-and-maintenance-cost.md): 在内部自建 LLM 基础设施成本高昂、复杂,并会拖慢 AI 产品开发与创新。 - [LLM 可观测性](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/comprehensive-observability.md): LLM 可观测性通过指标、日志和事件提供端到端视角,以保障模型在推理时的可靠性、效率和可扩展性。 - [什么是分布式推理?](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/distributed-inference.md): 分布式推理是指在多个 GPU、worker、节点或区域之间运行模型推理,以实现可扩展、可靠且具成本效益的服务。本文将说明什么是分布式推理、团队为何在生产环境中使用它、它面临的关键挑战,以及现代运行时和平台如何支持大规模分布式 LLM 推理。 - [快速扩缩容](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/fast-scaling.md): 快速扩缩容使 AI 系统能够处理动态变化的 LLM 推理工作负载,同时尽量降低延迟与成本。 - [基础设施与运维](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations.md): LLM 并不是孤立运行的。它背后需要稳健的基础设施支持,从高性能 GPU 到部署 - [InferenceOps 与管理](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/inferenceops-and-management.md): 通过 InferenceOps 工作流和基础设施最佳实践,可靠地扩展 LLM 推理。 - [多云与跨区域推理](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/multi-cloud-and-cross-region-inference.md): 多云与跨区域推理是指在多个云提供商或多个区域之间运行 LLM 工作负载,以提升延迟表现、可用性和成本效率。 - [多模型推理流水线](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/multi-model-inference-pipelines.md): 多模型推理流水线会将多个模型串联到同一应用路径中,以提升专业化能力和可控性,但代价是额外的延迟与运维复杂性。 - [什么是 LLM 推理基础设施?](https://dpthinker-ai.github.io/llm-inference-handbook/infrastructure-and-operations/what-is-llm-inference-infrastructure.md): 通过面向推理而设计的基础设施来部署、扩展和管理 LLM。