AyutthayaAlpha 2.0 — 基于 T5-small 架构的泰语↔拉丁字母转写(Transliteration) Transformer,用于人名与机构名的跨文字系统转写。本项目将其从通用 PyTorch 环境 迁移到华为昇腾 Ascend910 NPU,并基于 FastAPI 提供服务化推理(HTTP 接口)。
| 项目 | 内容 |
|---|---|
| 模型 | ayutthayaalpha-2.0-early-beta(T5ForConditionalGeneration,约 71M 参数) |
| 任务 | 泰→拉(<latn> 前缀)/ 拉→泰(<thai> 前缀)双向转写 |
| 输入 | 名称文本(泰文或拉丁字母),支持 top-k 候选生成 |
| 硬件 | Ascend910(本环境 16 卡,部署使用 npu:0) |
| 软件 | CANN 8.5.1 / Python 3.11 / PyTorch 2.9.0 / torch_npu 2.9.0.post1 / transformers 5.9.0 |
| 推理服务 | FastAPI + uvicorn(HTTP 服务化推理) |
| 测试结果 | 5/5 用例通过,双向转写准确(สมชาย→somchai、somsak→สมศักดิ์) |
ayutthayaalpha-2.0-early-beta-npu/
├── inference.py # 服务化推理脚本(FastAPI HTTP 服务)★ 核心交付
├── test_inference.py # 测试用例(5 项,HTTP 模式)
├── requirements.txt # 运行环境依赖清单
├── README.md # 本文档
├── test_results/
│ └── test_result.txt # 测试结果记录
└── assets/
├── npu_device_call.png # 截图1:NPU 设备调用
├── model_result.png # 截图2:模型测试结果(转写对照表+延迟)
├── agent_workflow.png # 截图3:适配过程工作流
└── _make_screenshots.py # 截图生成脚本(可复现)| 依赖 | 版本 | 说明 |
|---|---|---|
| 操作系统 | openEuler / Ubuntu(aarch64) | 本机 openEuler 2203 SP4 |
| 昇腾硬件 | Ascend910(≥1 卡) | bf16 权重约 1.2GB |
| CANN | ≥ 8.0(本机 8.5.1) | /usr/local/Ascend/ascend-toolkit/set_env.sh |
| Python | 3.11 | — |
| PyTorch | 2.9.0 | 与 torch_npu 版本配套 |
| torch_npu | 2.9.0.post1 | 昇腾 NPU 运行时插件 |
# 1) 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 2) 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0
# 3) 安装依赖(华为镜像源)
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt
# 4) 校验 NPU 环境
npu-smi info
python -c "import torch, torch_npu; a=torch.randn(3,4).npu(); print(a+a)"T5-small 标准 encoder-decoder(纯 PyTorch 算子,无需 CUDA 内核改写):
文本(SentencePiece BPE,32k 词表)→ 6层 Encoder → 6层 Decoder → 转写文本inference.py 顶部)import torch_npu
from torch_npu.contrib import transfer_to_nputransfer_to_npu 自动完成 CUDA API → NPU API 的映射。
| # | 适配点 | 说明 |
|---|---|---|
| 1 | 任务 token 前缀 | 模型使用特殊 token <latn>(泰→拉)与 <thai>(拉→泰)区分方向,服务端自动添加 |
| 2 | 输入格式(核心坑) | 训练输入为无空格拼接:<thai>somchai(带空格 <thai> somchai 结果不准),服务端按此格式拼接 |
| 3 | 大小写敏感性 | 大写拉丁输入导致空输出(<thai> Somchai → 空),服务端对拉→泰方向自动 lower() |
| 4 | 双向生成 | 泰→拉输出拉丁字母、拉→泰输出泰文字符(U+0E00 范围),测试用例按字符范围断言 |
| 5 | top-k 候选 | num_return_sequences + num_beams 支持多个转写变体(README 所述 preference-aware ranking) |
本项目不涉及 CUDA 自定义算子、DP/DDP 分布式改造、flash_attn 等三方 CUDA 库。
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
python inference.py \
--model-path /mnt/old_data/whl/models/4-1/davor/ayutthayaalpha-2.0-early-beta \
--host 0.0.0.0 --port 8006启动日志:
[inference] Loading model (bf16) -> npu:0 ...
[inference] Model loaded in 0.2s, device=npu:0
INFO: Uvicorn running on http://0.0.0.0:8006| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /health | 健康检查,返回设备/模型/精度信息 |
| POST | /infer | 转写推理(text + direction,支持 top-k 候选) |
| POST | /v1/transliterations | OpenAI 风格接口 |
请求参数:
{
"text": "สมชาย",
"direction": "thai_to_latin",
"num_candidates": 1,
"max_new_tokens": 64
}健康检查:
curl -s http://127.0.0.1:8006/health
# {"status":"ok","model":"ayutthayaalpha-2.0-early-beta","architecture":"T5ForConditionalGeneration (small)","device":"npu:0","dtype":"torch.bfloat16"}泰→拉转写:
curl -s http://127.0.0.1:8006/infer -H "Content-Type: application/json" -d '{
"text": "สมชาย", "direction": "thai_to_latin"
}'
# {"input":"สมชาย","direction":"thai_to_latin","candidates":[{"text":"somchai","score":null}],"latency_ms":28.16}拉→泰转写(top-3 候选):
curl -s http://127.0.0.1:8006/infer -H "Content-Type: application/json" -d '{
"text": "somsak", "direction": "latin_to_thai", "num_candidates": 3
}'python test_inference.py --url http://127.0.0.1:8006用例覆盖:
| 用例 | 内容 | 结果 |
|---|---|---|
| T1 | 健康检查 /health,确认 device=npu:0 | ✅ PASS |
| T2 | 泰→拉:สมชาย → somchai(拉丁字母输出) | ✅ PASS |
| T3 | 拉→泰:somsak → สมศักดิ์(泰文字符输出) | ✅ PASS |
| T4 | 多候选:num_candidates=3 返回 3 个候选 | ✅ PASS |
| T5 | OpenAI 风格接口:supaporn → สุภาภรณ์ | ✅ PASS |
完整测试结果见 test_results/test_result.txt,汇总:5 通过 / 0 失败。
| 截图 | 文件 | 说明 |
|---|---|---|
| NPU 设备调用 | assets/npu_device_call.png | npu-smi info + torch_npu 探针 + 部署摘要 |
| 测试结果 | assets/model_result.png | 转写对照表(含泰文)+ 延迟柱状图 + 测试日志 |
| 适配过程 | assets/agent_workflow.png | 迁移适配工作流(10 步 playbook) |
截图可通过 python assets/_make_screenshots.py 重新生成(需服务运行中)。
泰文渲染依赖 Noto Sans Thai 字体(
/root/.fonts/NotoSansThai.ttf),截图脚本已内置注册与字形回退(font.family列表)。
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 拉→泰输出为空 | 大写拉丁输入(模型训练数据为小写) | 服务端自动 lower();或手动用小写输入 |
| 转写结果不准(带空格前缀) | 训练格式为无空格拼接 <thai>name | 服务端自动处理,无需手动加前缀 |
ValueError: Unsupported language | 无(T5 无语言 token,方向由 <latn>/<thai> 控制) | — |
| 截图中泰文乱码 | 缺少泰文字体 | 安装 Noto Sans Thai 并注册到 matplotlib |
No module named 'decorator' | torch_npu 运行时依赖缺失 | pip install decorator |
| 端口被占用 | 环境预装服务占用端口 | 换用空闲端口(如 8006) |