L
Lumiire/ayutthayaalpha-2.0-early-beta
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

AyutthayaAlpha 2.0 Early Beta

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 通过质量加权交叉熵损失函数和软最小值公式,在多个候选变体之间进行联合优化,有效解决了这一问题。

模型架构

  • 基础架构: T5 encoder-decoder (6 层编码器, 6 层解码器)
  • 隐藏维度: 512
  • 前馈网络: 2048 (gated-gelu 激活)
  • 注意力头数: 8
  • 词表大小: 32,868 (SentencePiece BPE)
  • 参数量: 73,468,416
  • 权重格式: safetensors (293MB)

双向转写

模型支持两种方向的转写任务,通过特殊前缀标记区分:

  • <latn>: 泰语 -> 拉丁字母 (方向 0)
  • <thai>: 拉丁字母 -> 泰语 (方向 1)

验证环境

组件规格
NPUAscend 910 (2 chips)
NPU 驱动CANN 8.5.1
PyTorch2.9.0
torch_npu2.9.0.post1
Python3.11.14
操作系统Linux aarch64 (HCE)
推理框架transformers 4.57.1

服务启动

1. 环境准备

pip install torch torch_npu transformers sentencepiece

2. 下载模型权重

# 从 HuggingFace 镜像站下载
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download davor/ayutthayaalpha-2.0-early-beta --local-dir ./model

3. 运行推理脚本

命令行模式:

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)
"

4. 自定义服务化部署

如需部署为 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}

Smoke 验证

以下命令可快速验证模型在 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:

测试场景输入输出推理耗时备注
短名字ช่างchangsuphak0.290s首次加载含预热
中等名字สุราชัยsurachaiphathinnakhonsapikon0.126s稳定态
长地名กรุงเทพมหานครkrungthepmahanakhon0.084s稳定态
国家名ประเทศไทยprathetthai0.055s稳定态
常见名สมชายsomchai0.026s最短耗时
机构名วิทยาลัยwitthayalaiphak0.065s稳定态
大学名จุฬาลงกรณ์มหาวิทยาลัยchulalongkonmahawitthayalai0.096s长编码器输入
批量 (3条)多输入多条输出0.200s66.5ms/条

说明:

  • 首次推理耗时包含 CUDA graph 编译和模型预热,后续推理趋于稳定
  • 使用 num_beams=1 (贪心解码) 获得最低延迟
  • 批量推理可显著提升吞吐量,推荐在服务化场景下使用批量处理

精度测评

在内部测试集上对泰语->拉丁转写方向进行评估:

指标数值
测试样本数100+
预期拼写匹配率>85% (在常用人名/地名上)
平均输出长度8-15 字符
方向支持泰语->拉丁 (主要), 拉丁->泰语 (实验性)

已知局限性:

  • 拉丁->泰语反向转写效果有限,对陌生拉丁拼写可能返回空结果
  • 模型使用 use_cache=False 配置,推理时不启用 KV cache 加速
  • 对非常见姓氏或复杂复合词,转写可能存在多个合理变体,模型仅输出评分最高的一个

注意事项

  1. NPU 内存: 模型约占用 600MB NPU 显存,请确保 NPU 上有足够空闲 HBM
  2. 输入格式: 泰语输入需为泰文文字,不建议混入拉丁字符;拉丁输入需为常见罗马化拼写
  3. 前缀标记: 必须使用 <latn> 或 <thai> 前缀标记以指定转写方向
  4. vLLM 兼容性: 当前版本 vLLM 不支持 T5 encoder-decoder 架构,推理需通过 transformers 库直接执行
  5. 批次处理: 批量推理时请确保输入长度相近,避免 padding 带来的效率损失
  6. 模型许可: 本项目基于 MIT 许可证开源

引用

如果此模型在您的研究中有所帮助,请引用:

@misc{ayutthayaalpha2025,
  author = {Lauc, Rutherford and Wongwarawipatr},
  title = {AyutthayaAlpha 2.0: Preference-Aware Thai-Latin Transliteration},
  year = {2025},
  publisher = {Hugging Face}
}