什么是 TGI?

TGI(Text Generation Inference)是由 HuggingFace 开发的一个生产级大语言模型推理服务框架,专为高性能文本生成场景设计。它是 HuggingFace 官方推出的推理解决方案,与 HuggingFace Hub 生态深度集成。


核心架构

┌─────────────────────────────────────────────────┐
│                   TGI 架构                        │
│                                                   │
│  ┌──────────┐    ┌──────────┐    ┌─────────────┐ │
│  │ HTTP API │───▶│ Router   │───▶│   Scheduler │ │
│  │ (gRPC)   │    │ (Rust)   │    │             │ │
│  └──────────┘    └──────────┘    └──────┬──────┘ │
│                                         │        │
│                              ┌──────────▼──────┐ │
│                              │  Model Shards   │ │
│                              │  (Python/Torch) │ │
│                              └─────────────────┘ │
└─────────────────────────────────────────────────┘
  • Router 层:用 Rust 编写,负责请求调度、负载均衡,性能极高

  • Model 层:Python + PyTorch,负责实际推理计算


核心特性

特性

说明

Continuous Batching

动态批处理,最大化 GPU 利用率

🔢 Token Streaming

实时流式输出(SSE)

🎯 Flash Attention 2

内存高效注意力机制

📐 Tensor Parallelism

多 GPU 并行推理

🔧 量化支持

GPTQ、AWQ、EETQ、bitsandbytes

🛡️ Safetensors

安全快速的模型加载格式

📊 Prometheus 监控

内置指标采集

🔑 Token 水印

内置文本水印功能

🤖 OpenAI 兼容

兼容 Chat Completions API


支持的模型

主流支持模型(部分):

LLaMA 系列      ████████████████████  LLaMA 2/3, Code LLaMA
Mistral 系列    ████████████████████  Mistral, Mixtral MoE
Qwen 系列       ████████████████████  Qwen 1.5 / 2 / 2.5
Falcon          ████████████████████
GPT 系列        ████████████████████  GPT-2, GPT-NeoX
Gemma           ████████████████████
BLOOM           ████████████████████
StarCoder       ████████████████████

快速上手

Docker 部署(推荐方式)

# 拉取镜像并启动服务
docker run --gpus all \
    --shm-size 64g \
    -p 8080:80 \
    -v $HOME/.cache/huggingface:/data \
    ghcr.io/huggingface/text-generation-inference:latest \
    --model-id meta-llama/Llama-3-8b-instruct \
    --num-shard 1 \          # GPU 数量
    --max-batch-total-tokens 32000

Python 客户端调用

from huggingface_hub import InferenceClient

client = InferenceClient("http://localhost:8080")

# 普通生成
response = client.text_generation(
    "什么是深度学习?",
    max_new_tokens=512,
    temperature=0.7,
    top_p=0.95
)
print(response)

流式输出

# 流式生成
for token in client.text_generation(
    "讲一个故事:",
    max_new_tokens=200,
    stream=True          # 开启流式
):
    print(token, end="", flush=True)

Chat 接口(OpenAI 兼容)

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8080/v1",
    api_key="none"
)

response = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "system", "content": "你是一个有帮助的助手"},
        {"role": "user", "content": "介绍一下量子计算"}
    ],
    stream=True
)

for chunk in response:
    print(chunk.choices[0].delta.content or "", end="")

REST API 直接调用

# 文本生成
curl http://localhost:8080/generate \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
        "inputs": "深度学习是什么?",
        "parameters": {
            "max_new_tokens": 200,
            "temperature": 0.7
        }
    }'

# 流式生成
curl http://localhost:8080/generate_stream \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"inputs": "从前有座山", "parameters": {"max_new_tokens": 100}}'

量化部署示例

# GPTQ 量化模型
docker run --gpus all -p 8080:80 \
    ghcr.io/huggingface/text-generation-inference:latest \
    --model-id TheBloke/Llama-2-7B-GPTQ \
    --quantize gptq

# AWQ 量化模型
docker run --gpus all -p 8080:80 \
    ghcr.io/huggingface/text-generation-inference:latest \
    --model-id casperhansen/llama-3-8b-instruct-awq \
    --quantize awq

# bitsandbytes 动态量化(无需专门量化模型)
docker run --gpus all -p 8080:80 \
    ghcr.io/huggingface/text-generation-inference:latest \
    --model-id meta-llama/Llama-3-8b-instruct \
    --quantize bitsandbytes-nf4

内置监控

TGI 内置 Prometheus 指标端点,可直接接入 Grafana:

# 查看监控指标
curl http://localhost:8080/metrics
# 关键指标示例
tgi_request_count                    # 总请求数
tgi_request_duration_seconds         # 请求延迟
tgi_batch_current_size               # 当前批处理大小
tgi_queue_size                       # 等待队列长度
tgi_generated_tokens_total           # 总生成 token 数

TGI vs vLLM 对比

维度

TGI

vLLM

开发方

HuggingFace

UC Berkeley

主语言

Rust + Python

Python

内存管理

标准 KV Cache

PagedAttention

吞吐量

⭐⭐⭐⭐

⭐⭐⭐⭐⭐

部署方式

Docker 优先

pip 安装

HF 生态集成

⭐⭐⭐⭐⭐

⭐⭐⭐

监控能力

内置完善

需额外配置

水印功能

✅ 内置

❌ 无

社区生态

HF 官方支持

独立社区


典型使用场景

  • 🌐 在线推理服务:高并发 API 服务

  • 🔄 流式对话应用:ChatBot、实时问答

  • 🏢 企业私有化部署:内网 LLM 服务

  • 📈 HuggingFace Hub 模型快速部署


总结

TGI 是 HuggingFace 生态中最成熟的推理服务方案,以 Rust 高性能路由 + 完善的生产特性著称。相比 vLLM,TGI 在 HuggingFace 生态集成、开箱即用的监控和运维能力上更具优势,是企业级部署的可靠选择。

📎 官方仓库:github.com/huggingface/text-generation-inference