版本信息最后核对:2026-12-03
Text Generation Inference 推理服务
TGI(Text Generation Inference)是 Hugging Face 的大模型推理服务器,支持张量并行、连续批处理、量化和 OpenAI 兼容接口。项目已进入维护模式(截至 2026-10),官方建议新项目优先使用 vLLM、SGLang 等引擎。
常用部署方式是 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.3.7 \
--model-id Qwen/Qwen3.5-9B \
--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.3.7 \
--model-id Qwen/Qwen3.5-122B-A10B \
--num-shard 4
--shm-size 太小,模型加载或批处理时崩溃。--max-total-tokens 设置超过模型上下文,启动失败。--num-shard 与 GPU 数不匹配。trust_remote_code,但启动参数没有开启。max_new_tokens,长输出占满显存。合上页面,先回答,再展开对照。
--shm-size 设置太小会有什么后果?--max-total-tokens 超过模型上下文会怎样?max_new_tokens 有什么风险?--max-total-tokens 配得比它大时,服务在启动阶段就会报错退出。设置时应不超过模型的上下文窗口。
小程序版
微信扫码,随时查命令