将 HuggingFace 模型 davor/ayutthayaalpha-2.0-early-beta 适配部署到国产昇腾(Ascend)NPU 上,并提供 OpenAI 兼容的服务化推理接口。
torch 2.9.0 (+torch_npu 2.9.0.post1) + CANN 8.5.1 + Ascend 910| 项目 | 值 |
|---|---|
| 架构 | T5ForConditionalGeneration(encoder-decoder) |
| 参数量 | ~73.5M(d_model=512,6 层,8 头) |
| 词表大小 | 32868 |
| dtype 原配置 | float32(推理按 bfloat16 亦可行) |
| 语言 | 泰语(ไทย)为主,兼顾英文 |
AyutthayaAlpha 为泰语文本生成模型(命名取自泰国古都「大城府」)。
该模型是 encoder-decoder(T5) 架构,而 vLLM-Ascend 仅支持 decoder-only 的 CausalLM 架构,registry 中不含 T5ForConditionalGeneration。因此本项目不走 vLLM-Ascend 服务化,改为:
transformers 直接以 torch_npu 在昇腾 NPU 上加载权重;FastAPI 暴露 OpenAI 兼容接口(/v1/models、/v1/chat/completions、/v1/completions)实现服务化推理。全程无任何 CUDA 算子,T5 算子均为 torch 原生算子,在 NPU 上可直接运行,无需改动模型源码。
见 requirements.txt。核心版本:
torch>=2.1、torch_npu==2.9.0.post1(对应 CANN 8.5.1)transformers>=4.40fastapi、uvicorn安装依赖(昇腾环境已就绪时):
pip install -i https://mirrors.aliyun.com/pypi/simple/ \
transformers "fastapi" "uvicorn"权重下载(优先级:本地目录 > ModelScope > HF-mirror > gitcode LFS):
mkdir -p /opt/atomgit/models/ayutthayaalpha-2.0-early-beta
export HF_ENDPOINT=https://hf-mirror.com
python -c "from huggingface_hub import snapshot_download; snapshot_download('davor/ayutthayaalpha-2.0-early-beta', local_dir='/opt/atomgit/models/ayutthayaalpha-2.0-early-beta')"模型仅含一个 model.safetensors(约 294MB)。
python inference.py --prompt "สวัสดีครับ แนะนำตัวหน่อย" \
--model-dir /opt/atomgit/models/ayutthayaalpha-2.0-early-beta \
--device npu:0PORT=8010 DEVICE=npu:0 python inference.py --serve --port 8010curl -sf http://127.0.0.1:8010/v1/modelscurl -s http://127.0.0.1:8010/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{"model":"ayutthayaalpha-2.0-early-beta",
"messages":[{"role":"user","content":"What is the capital of Thailand?"}],
"max_tokens":48}'curl -s http://127.0.0.1:8010/v1/completions \
-X POST -H 'Content-Type: application/json' \
-d '{"prompt":"สวัสดี","max_tokens":16}'| 检查项 | 结果 |
|---|---|
| NPU 权重加载 | from_pretrained(...).to('npu:0') 成功,无缺失键 |
| CLI 推理 | 成功,正常生成泰语文本 |
GET /v1/models | HTTP 200 |
POST /v1/chat/completions | HTTP 200 |
POST /v1/completions | HTTP 200 |
| dtype | bfloat16 / 原生 float32 均可运行 |