Text Generation Inference 推理服务
TGI(Text Generation Inference)是 Hugging Face 的大模型推理服务器,支持张量并行、连续批处理、量化和 OpenAI 兼容接口。适合快速部署 Hugging Face 模型。
常用部署方式是 Docker 镜像 ghcr.io/huggingface/text-generation-inference,默认端口 80。
启动服务:
docker run --gpus all --shm-size 1g \
-p 8080:80 \
-v $PWD/models:/data \
ghcr.io/huggingface/text-generation-inference:3.0.1 \
--model-id Qwen/Qwen2.5-7B-Instruct \
--max-input-tokens 4096 \
--max-total-tokens 8192
调用生成接口:
curl http://localhost:8080/generate \
-H 'Content-Type: application/json' \
-d '{"inputs":"你好","parameters":{"max_new_tokens":64}}'
OpenAI 兼容接口:
curl http://localhost:8080/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{"model":"tgi","messages":[{"role":"user","content":"你好"}]}'
多卡张量并行:
docker run --gpus all --shm-size 1g -p 8080:80 \
ghcr.io/huggingface/text-generation-inference:3.0.1 \
--model-id Qwen/Qwen2.5-72B-Instruct \
--num-shard 4
--shm-size 太小,模型加载或批处理时崩溃。--max-total-tokens 设置超过模型上下文,启动失败。--num-shard 与 GPU 数不匹配。trust_remote_code,但启动参数没有开启。max_new_tokens,长输出占满显存。