# 文本生成推理（TGI）

文本生成推理（Text Generation Inference，TGI）是由 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 开发的大型语言模型生产服务器，以 [Rust](https://www.edgepedia.org/zh/rust-programming-language)、Python 和 gRPC 编写，于 2022 年 10 月首次发布，是一个专为 transformer 语言模型设计的生产级高吞吐量服务框架。<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup><sup> • </sup><sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup><sup> • </sup><sup>[6](https://developer.nvidia.com/blog/deploying-gpt-j-and-t5-with-fastertransformer-and-triton-inference-server/)</sup> 它为 Hugging Face 自家的 Hugging Chat、推理 API（Inference API）和推理端点（Inference Endpoints）产品提供支撑。随着以 vLLM 为首的竞争服务器在吞吐量上实现超越，该项目以维护模式公告宣告开发终止，截至 2026 年 9 月，代码仓库也已归档。<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup><sup> • </sup><sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup>

## 关键事实

| 事实 | 详情 |
|---|---|
| 开发者 | Hugging Face<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |
| 首次发布 | 仓库创建于 2022-10-08<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |
| 许可证 | Apache 2.0 许可证<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |
| 架构 | Rust HTTP/调度层位于 Python 建模服务器之上，通过 gRPC 连接<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup><sup> • </sup><sup>[3](https://huggingface.co/blog/tgi-multi-backend)</sup> |
| 硬件后端 | NVIDIA、AMD（ROCm）、AWS Inferentia、Intel GPU、Intel Gaudi、Google TPU<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |
| 状态 | 已宣布维护模式；截至 2026 年 9 月仓库标记为已归档<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |
| 生产角色 | 为 Hugging Chat、推理 API 和推理端点提供服务<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> |

## TGI 解决的问题

朴素的部署方式是将模型封装在 PyTorch 中，置于 FastAPI 之类的 Web 框架之后，一次处理一个请求：请求等待网络输入时 GPU 处于空闲状态，第二个请求也无法共享第一个请求的计算资源。TGI 通过连续批处理（continuous batching）解决了这一问题——这是 Hugging Face 从 Orca 研究论文引入开源生态的一种调度技术，新请求在每个解码步骤加入正在处理的批次，而不是等待整批完成。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup> TGI 于 2022 年下半年发布，比 vLLM 的 [PagedAttention](https://www.edgepedia.org/zh/pagedattention) 论文早约八个月，是一个生产级高吞吐量服务框架，此前的解决方案包括搭载 [FasterTransformer](https://www.edgepedia.org/zh/fastertransformer) 的 NVIDIA Triton，它后来成为 Hugging Face 推理端点产品背后的服务基础设施。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup><sup> • </sup><sup>[6](https://developer.nvidia.com/blog/deploying-gpt-j-and-t5-with-fastertransformer-and-triton-inference-server/)</sup>

## 工作原理

TGI 的 HTTP 层和调度层用 Rust 编写，Hugging Face 选择 Rust 是为了获得编译器强制保障的内存安全，并绕过 Python 的全局解释器锁；模型前向传播仍保留在 Python 中，两层之间通过 gRPC 通信。<sup>[3](https://huggingface.co/blog/tgi-multi-backend)</sup>

该服务器文档记载的功能集包括：<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup>

- **连续批处理与张量并行**，将模型拆分到多块 GPU 上，并在请求到达和完成时保持批次满载。
- **通过服务器发送事件（Server-Sent Events）实现 token 流式传输**，客户端在生成 token 时即可收到，而非等待全部完成。
- **量化支持**，涵盖 bitsandbytes、GPT-Q、EETQ、AWQ、Marlin 和 fp8，使大型模型能够装入更少的 GPU 显存。
- **推测解码（speculative decoding）**，README 称其通过用小模型起草 token、再用大模型验证，可将延迟改善约 2 倍。

2024-06-28 发布的 2.1.0 版本新增了 Gemma 2 模型支持、在单次部署中运行多个 LoRA 适配器的能力，以及通过 Marlin 支持实现的更快 [GPTQ](https://www.edgepedia.org/zh/gptq) 推理（最高 2 倍加速），同时重构了调度逻辑。<sup>[4](https://github.com/huggingface/text-generation-inference/releases/tag/v2.1.0)</sup>

## 版本历史

TGI 最初于 2022 年发布，是一个注重性能、几乎无需编写代码的解决方案，用于在 NVIDIA GPU 上部署 [Hugging Face Hub](https://www.edgepedia.org/zh/hugging-face-hub) 中的模型。硬件支持随时间扩展至 AMD Instinct GPU、Intel GPU、AWS Trainium/Inferentia、Google TPU 和 [Intel Gaudi](https://www.edgepedia.org/zh/intel-gaudi)。<sup>[3](https://huggingface.co/blog/tgi-multi-backend)</sup> 仓库元数据列出的后端集合相同：Nvidia、AMD（ROCm）、Inferentia、Intel GPU、Gaudi 和 Google TPU。<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup>

2024 年，该项目于 6 月发布了 v2.1.0<sup>[4](https://github.com/huggingface/text-generation-inference/releases/tag/v2.1.0)</sup>，随后发布 TGI v3，引入了零配置模式，可在服务器启动时根据硬件自动选择最大输入长度、token 数和批次值，免去手动配置和反复试错调优。<sup>[5](https://huggingface.co/docs/text-generation-inference/main/en/conceptual/chunking)</sup>

2024 年底，Hugging Face 承认包括 vLLM、SGLang、llama.cpp 和 [TensorRT-LLM](https://www.edgepedia.org/zh/tensorrt-llm) 在内的多种推理方案已经分割了整个生态，并推出了 **TGI 后端（TGI Backends）** 架构：一个统一的前端层，可将上述任一引擎集成到 TGI 的 API 之后。计划于 2025 年第一季度推出 vLLM 后端，并宣布了面向 Intel、AMD 或 ARM 服务器的 llama.cpp CPU 后端。<sup>[3](https://huggingface.co/blog/tgi-multi-backend)</sup> 记录未核实这两个后端是否实际发布。

最后阶段以仓库 README 中的维护模式通知为标志：此后仅接受小型缺陷修复、文档改进和轻量维护任务，项目建议新工作使用 vLLM、SGLang、llama.cpp 或 MLX。截至 2026 年 9 月，创建于 2022-10-08 的仓库被标记为已归档。<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup>

## 性能声明与独立说法

记录中唯一量化的 TGI 与竞品对比数据来自 Hugging Face 自身。对于 TGI v3，该公司报告称在带前缀缓存的长提示词上，TGI 处理的 token 数量是 vLLM 的 3 倍，速度快 13 倍；不带前缀缓存时最高可加速 30 倍；在 20 万以上 token 的提示词上，报告称使用 Llama 3.1 70b 在 8 块 H100 上，vLLM 的对话回复耗时 27.5 秒，而 TGI 仅需 2 秒，前缀查找开销约为 5 微秒。这些是厂商报告的基准数据，未经独立验证。<sup>[5](https://huggingface.co/docs/text-generation-inference/main/en/conceptual/chunking)</sup>

一位独立从业者的说法则讲述了此前阶段的相反故事：2023 年 6 月 vLLM 随 PagedAttention 发布时，其吞吐量基准在大多数工作负载上显著优于 TGI，这是 [KV 缓存](https://www.edgepedia.org/zh/kv-cache)内存管理方面真正的架构进步，TGI 无法通过调优来复制。Hugging Face 最终将分页注意力风格的 KV 缓存管理集成到 TGI 中，但 vLLM 已在大多数 GPU 工作负载上确立了吞吐量领先地位。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup>

两种说法并未直接调和：厂商数据针对的是带前缀缓存的长提示词上的 TGI v3，而独立说法涵盖的是 2023 年中期以来更广泛的工作负载组合。记录中没有 TGI 与 SGLang、TensorRT-LLM、llama.cpp 或托管端点对比的独立基准数据。<sup>[5](https://huggingface.co/docs/text-generation-inference/main/en/conceptual/chunking)</sup><sup> • </sup><sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup>

## 采用、反响与衰落

TGI 的地位建立在两个角色之上：它是一个生产级高吞吐量服务器——尽管此前已有搭载 FasterTransformer 的 NVIDIA Triton 等解决方案——并且它运行着 Hugging Face 的商业推理端点以及 Hugging Chat 和推理 API。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup><sup> • </sup><sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup><sup> • </sup><sup>[6](https://developer.nvidia.com/blog/deploying-gpt-j-and-t5-with-fastertransformer-and-triton-inference-server/)</sup> vLLM 于 2023 年 6 月的发布改变了它的处境：PagedAttention 的内存管理是架构层面的进步，而非调优差距，即便 Hugging Face 加入了可比的 KV 缓存管理，vLLM 仍在大多数 GPU 工作负载上保持吞吐量领先。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup>

Hugging Face 的回应分两步展开。首先，2024 年底的 TGI 后端公告将 TGI 重新定位为那些超越它的引擎的前端，将 vLLM、SGLang、llama.cpp 和 TensorRT-LLM 列为集成对象。<sup>[3](https://huggingface.co/blog/tgi-multi-backend)</sup> 其次，维护模式通知推荐了这些同样的项目外加 MLX 作为前进方向。<sup>[1](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)</sup> 独立从业者的指南如今建议将 vLLM 作为新部署的默认选择，TGI 则保留给那些看重原生 Hugging Face Hub 集成、Safetensors 优先加载、bitsandbytes 动态量化、水印支持，或其在推理端点内部较长生产记录的场景。<sup>[2](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)</sup>

## References

1. [huggingface/text-generation-inference (GitHub README)](https://github.com/huggingface/text-generation-inference?tab=readme-ov-file)
2. [TGI and Alternative Serving Frameworks | EngineersOfAI](https://engineersofai.com/docs/open-source-models/production-deployment/tgi-and-alternatives)
3. [Introducing multi-backends (TRT-LLM, vLLM) support for Text Generation Inference](https://huggingface.co/blog/tgi-multi-backend)
4. [TGI v2.1.0 release notes](https://github.com/huggingface/text-generation-inference/releases/tag/v2.1.0)
5. [TGI v3 overview · Hugging Face](https://huggingface.co/docs/text-generation-inference/main/en/conceptual/chunking)
6. [Deploying GPT-J and T5 with NVIDIA Triton Inference Server, NVIDIA Technical Blog](https://developer.nvidia.com/blog/deploying-gpt-j-and-t5-with-fastertransformer-and-triton-inference-server/)

---
*Initially written Sep 17, 2026 · Edited: Oct 11, 2026; Oct 15, 2026; Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Inference, serving, and efficiency of foundation models

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/text-generation-inference-tgi

English original: https://www.edgepedia.org/text-generation-inference-tgi
