← 返回首页
🧠

TGI部署

📂 llm ⏱ 2 min 267 words

--- title: "TGI部署" description: "Text Generation Inference (TGI) 部署实战,涵盖Docker部署、量化模型、性能优化与生产环境配置" tags: ["Text Generation Inference", "TGI", "Docker部署", "量化部署"] category: "llm" icon: "🧠"

TGI部署

TGI简介

Text Generation Inference(TGI)是Hugging Face推出的高性能文本生成推理框架。它支持Flash Attention、PagedAttention、连续批处理等先进技术,能够高效运行LLaMA、Mistral、Falcon等主流开源大语言模型。TGI专为生产环境设计,提供了开箱即用的API服务。

Docker快速部署

最简单的部署方式是使用Docker:

# 部署LLaMA 3 8B模型
docker run --gpus all --shm-size 1g \
  -p 8080:80 \
  -v /data/models:/models \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id meta-llama/Meta-Llama-3-8B \
  --quantize bitsandbytes-nf4

使用Docker Compose进行更灵活的配置:

version: '3.8'
services:
  tgi:
    image: ghcr.io/huggingface/text-generation-inference:latest
    ports:
      - "8080:80"
    volumes:
      - /data/models:/models
    environment:
      - HUGGING_FACE_HUB_TOKEN=${HF_TOKEN}
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    command: >
      --model-id meta-llama/Meta-Llama-3-8B
      --quantize bitsandbytes-nf4
      --max-input-length 4096
      --max-total-tokens 8192
      --max-batch-prefill-tokens 4096

量化部署

量化是降低显存占用的关键技术。TGI支持多种量化方案:

# GPTQ量化 - 4-bit量化,适合推理
docker run --gpus all \
  -p 8080:80 \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id TheBloke/Llama-2-70B-Chat-GPTQ \
  --quantize gptq \
  --bits 4

# AWQ量化 - 4-bit激活感知量化
docker run --gpus all \
  -p 8080:80 \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id TheBloke/Llama-2-70B-Chat-AWQ \
  --quantize awq

# BitsAndBytes NF4量化
docker run --gpus all \
  -p 8080:80 \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id meta-llama/Meta-Llama-3-70B \
  --quantize bitsandbytes-nf4

API调用示例

TGI提供兼容OpenAI的API接口,也支持原生API:

from huggingface_hub import InferenceClient

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

# 流式生成
stream = client.text_generation(
    "解释什么是大语言模型",
    max_new_tokens=512,
    temperature=0.7,
    stream=True
)

for token in stream:
    print(token, end="", flush=True)

# 使用OpenAI兼容API
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8080/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "system", "content": "你是一个有帮助的助手"},
        {"role": "user", "content": "用Python实现快速排序"}
    ],
    max_tokens=1024,
    temperature=0.7,
    stream=True
)

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

性能优化配置

TGI提供了丰富的性能调优参数:

docker run --gpus all \
  --shm-size 2g \
  -p 8080:80 \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id meta-llama/Meta-Llama-3-8B \
  --max-batch-prefill-tokens 8192 \
  --max-batch-tokens 32768 \
  --max-concurrent-requests 256 \
  --max-waiting-tokens 20 \
  --max-stop-sequences 4 \
  --max-top-tokens 50 \
  --max-tokens 4096

关键参数说明:max-concurrent-requests控制最大并发请求数,max-batch-tokens控制动态批处理的最大token数,max-waiting-tokens控制等待队列的最大长度。

健康检查与监控

# 健康检查
curl http://localhost:8080/health

# 获取模型信息
curl http://localhost:8080/info

# 生成请求
curl http://localhost:8080/generate \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"inputs":"你好","parameters":{"max_new_tokens":100}}'

TGI是目前部署开源LLM最便捷的方案之一,通过合理配置量化方案和性能参数,可以在单张消费级GPU上运行70B参数模型,极大降低了LLM部署的门槛。