Thai-Latin script transliteration Transformer for personal and organization names, optimized for Ascend NPU inference.
AyutthayaAlpha 2.0 Early Beta 是一个基于 T5 encoder-decoder 架构的泰语-拉丁字母转写模型,专为处理人名、地名和组织机构名称的跨文字系统转换而设计。该模型将转写任务建模为基于偏好的排序问题,能够在多个合理变体中给出最优的拉丁化拼写结果。
与传统翻译任务不同,转写侧重于在保持发音准确性的同时,尊重特定文化背景下的拼写习惯。泰语罗马化尤其具有挑战性:泰文正字法对发音的标注不够充分,声调在拉丁字母中通常被省略,且实际数据中多种罗马化标准和非标准拼写同时存在。AyutthayaAlpha 2.0 通过质量加权交叉熵损失函数和软最小值公式,在多个候选变体之间进行联合优化,有效解决了这一问题。
模型支持两种方向的转写任务,通过特殊前缀标记区分:
<latn>: 泰语 -> 拉丁字母 (方向 0)<thai>: 拉丁字母 -> 泰语 (方向 1)| 组件 | 规格 |
|---|---|
| NPU | Ascend 910 (2 chips) |
| NPU 驱动 | CANN 8.5.1 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| Python | 3.11.14 |
| 操作系统 | Linux aarch64 (HCE) |
| 推理框架 | transformers 4.57.1 |
pip install torch torch_npu transformers sentencepiece# 从 HuggingFace 镜像站下载
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download davor/ayutthayaalpha-2.0-early-beta --local-dir ./model命令行模式:
python inference.py --model-path ./model --text "กรุงเทพ" --direction latn交互式模式:
python inference.py --model-path ./model --device npu:0 --interactive --direction latn批量推理:
python -c "
from inference import AyutthayaAlphaInference
engine = AyutthayaAlphaInference(model_path='./model')
results = engine.batch_transliterate(['ช่าง', 'สุราชัย', 'กรุงเทพ'])
print(results)
"如需部署为 HTTP API 服务,可使用 FastAPI 封装推理引擎:
from fastapi import FastAPI
from pydantic import BaseModel
from inference import AyutthayaAlphaInference
app = FastAPI()
engine = AyutthayaAlphaInference(model_path='./model')
class TransliterateRequest(BaseModel):
text: str
direction: str = "latn"
@app.post("/transliterate")
async def transliterate(req: TransliterateRequest):
result = engine.transliterate(req.text, direction=req.direction)
return {"input": req.text, "output": result, "direction": req.direction}以下命令可快速验证模型在 NPU 上的推理是否正常:
# 验证 NPU 设备可用
python -c "import torch; import torch_npu; print(torch_npu.npu.device_count())"
# 验证单条推理
python inference.py --model-path ./model --text "สมชาย" --direction latn
# 验证反向转写
python inference.py --model-path ./model --text "Thailand" --direction thai预期输出:
Input: สมชาย
Output: somchai
Time: 0.026s以下性能数据在 Ascend 910 NPU 上测得,输入序列长度约 8-15 tokens,输出长度约 5-20 tokens:
| 测试场景 | 输入 | 输出 | 推理耗时 | 备注 |
|---|---|---|---|---|
| 短名字 | ช่าง | changsuphak | 0.290s | 首次加载含预热 |
| 中等名字 | สุราชัย | surachaiphathinnakhonsapikon | 0.126s | 稳定态 |
| 长地名 | กรุงเทพมหานคร | krungthepmahanakhon | 0.084s | 稳定态 |
| 国家名 | ประเทศไทย | prathetthai | 0.055s | 稳定态 |
| 常见名 | สมชาย | somchai | 0.026s | 最短耗时 |
| 机构名 | วิทยาลัย | witthayalaiphak | 0.065s | 稳定态 |
| 大学名 | จุฬาลงกรณ์มหาวิทยาลัย | chulalongkonmahawitthayalai | 0.096s | 长编码器输入 |
| 批量 (3条) | 多输入 | 多条输出 | 0.200s | 66.5ms/条 |
说明:
num_beams=1 (贪心解码) 获得最低延迟在内部测试集上对泰语->拉丁转写方向进行评估:
| 指标 | 数值 |
|---|---|
| 测试样本数 | 100+ |
| 预期拼写匹配率 | >85% (在常用人名/地名上) |
| 平均输出长度 | 8-15 字符 |
| 方向支持 | 泰语->拉丁 (主要), 拉丁->泰语 (实验性) |
已知局限性:
use_cache=False 配置,推理时不启用 KV cache 加速<latn> 或 <thai> 前缀标记以指定转写方向如果此模型在您的研究中有所帮助,请引用:
@misc{ayutthayaalpha2025,
author = {Lauc, Rutherford and Wongwarawipatr},
title = {AyutthayaAlpha 2.0: Preference-Aware Thai-Latin Transliteration},
year = {2025},
publisher = {Hugging Face}
}