本项目将 davor/ayutthayaalpha-2.0-early-beta(泰语 ↔ 拉丁人名/机构名转写模型)适配到昇腾 Ascend NPU 上推理,提供 CLI 与 FastAPI 两种使用方式。推理设备固定为 npu:0,全代码无任何 CUDA 调用。
T5ForConditionalGeneration(T5-base:d_model=512、6+6 层、8 头、vocab_size=32868、gated-gelu、float32)config.json generation_config.json model.safetensors(约 420MB)tokenizer.json tokenizer_config.json special_tokens_map.json模型使用任务前缀 token 区分两个转写方向,两个方向都经过训练:
| 前缀 token | 方向 | 示例 |
|---|---|---|
<latn> | 泰语 → 拉丁(默认) | "<latn> สมชาย" → 拉丁转写 |
<thai> | 拉丁 → 泰语 | "<thai> Somchai" → 泰语 |
.to('npu:0');发布前已用 grep 校验 inference.py 零命中 cuda/CUDA。try-except 捕获 torch_npu import 失败或 npu:0 不可用,打印清晰中文错误并优雅退出。torch.no_grad() + model.eval();generate 使用 max_new_tokens=64, num_beams=4。./model_weights/davor__ayutthayaalpha-2.0-early-beta/,启动优先加载本地缓存,命中即跳过下载。ASCEND_PROCESS_LOG_PATH / ASCEND_WORK_PATH 指向工程内 logs/ascend/,避免 ACL 日志噪音。核心依赖(torch / torch_npu / transformers / fastapi / uvicorn)已随昇腾环境预装。严禁重装/降级 torch 与 torch_npu:
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/验证 NPU 可用:
python3 -c "import torch, torch_npu; print(torch.npu.device_count(), torch.npu.is_available())"
# 2 True首次运行会检查本地缓存,未命中时提示下载。手动下载:
export HF_ENDPOINT=https://hf-mirror.com
mkdir -p model_weights/davor__ayutthayaalpha-2.0-early-beta
cd model_weights/davor__ayutthayaalpha-2.0-early-beta
for f in config.json generation_config.json model.safetensors tokenizer.json tokenizer_config.json special_tokens_map.json; do
curl -L -o "$f" "https://hf-mirror.com/davor/ayutthayaalpha-2.0-early-beta/resolve/main/$f"
done# 泰语 -> 拉丁(默认方向,文本自带 <latn> 前缀)
python3 inference.py --text "<latn> กรุงเทพมหานคร"
# 泰语 -> 拉丁(不带前缀,用 --direction latin 指定)
python3 inference.py --text "สมชาย" --direction latin
# 拉丁 -> 泰语(反向)
python3 inference.py --text "Somchai" --direction thai真实运行示例(昇腾 NPU):
$ python3 inference.py --text "<latn> กรุงเทพมหานคร"
{
"model": "davor/ayutthayaalpha-2.0-early-beta",
"task": "transliteration",
"direction": "latin",
"prefix_token": "<latn>",
"device": "npu:0",
"input": "<latn> กรุงเทพมหานคร",
"transliteration": "Krungthepmahanakhon",
"generated_tokens": 8,
"inference_time_ms": 213.45
}uvicorn inference:app --host 0.0.0.0 --port 8103curl -s -X POST http://127.0.0.1:8103/predict \
-H "Content-Type: application/json" \
-d '{"text":"<latn> สมชาย"}'响应示例:
{
"model": "davor/ayutthayaalpha-2.0-early-beta",
"task": "transliteration",
"direction": "latin",
"prefix_token": "<latn>",
"device": "npu:0",
"input": "<latn> สมชาย",
"transliteration": "Somchai",
"generated_tokens": 5,
"inference_time_ms": 198.22
}反向示例(拉丁 -> 泰语):
curl -s -X POST http://127.0.0.1:8103/predict \
-H "Content-Type: application/json" \
-d '{"text":"Somchai","direction":"thai"}'ayutthayaalpha-2.0-early-beta-npu/
├── inference.py # CLI + FastAPI 双模式推理(昇腾 NPU)
├── requirements.txt
├── README.md
├── .gitignore
├── assets/ # 运行截图
├── logs/ # 真实运行日志
└── model_weights/ # 权重本地缓存(git 忽略)
└── davor__ayutthayaalpha-2.0-early-beta/MIT License。权重版权归 davor/ayutthayaalpha-2.0-early-beta 原仓库所有,本仓库仅做昇腾 NPU 适配。