基于 HuggingFace 上
dkarthikeyan1/tcrt5_pre_tcrdb的本地权重,在华为 Ascend910 上完成 PyTorch 迁移与服务化推理交付。
TCRT5 是一个 T5 架构的 seq2seq 模型,原本用于「pMHC → TCR(CDR3 β)」与 「TCR → pMHC」的条件序列生成(详见 Karthikeyan et al. 2025, Nature Machine Intelligence)。 本仓库为 预训练 checkpoint,vocab=128,10 enc + 10 dec,d_model=256,16 heads。
| 项目 | 取值 |
|---|---|
| 硬件 | Ascend910 × 2(共 64 GB HBM) |
| 框架 | PyTorch 2.9.0 + torch_npu 2.9.0.post1 |
| CANN | 8.5.1 |
| 推理框架 | HuggingFace Transformers 4.57.6(直接复用 T5 实现) |
| 注入迁移 | torch_npu.contrib.transfer_to_npu(自动把 cuda.* 重写为 npu.*) |
| tokenizer | 自实现 TCRT5Tokenizer(原因见下) |
| 注意力实现 | attn_implementation="eager"(NPU 上 fused SDPA 不一定全支持,eager 稳) |
仓库自带的 tokenizer_config.json 中 tokenizer_class="TCRT5Tokenizer" 是
自定义类,当前 transformers(4.57.6)并不识别,直接 AutoTokenizer.from_pretrained 会报
ValueError: Tokenizer class TCRT5Tokenizer does not exist or is not currently imported。
同时 spiece.model 实际只训练了 28 个 piece(PAD/SOS/EOS/TCR/SEP/PMHC/UNK
config.vocab_size=128,T5 的 100 个 <extra_id_X> 哨兵
token(id 28–127)以及 [CLS]=128、[MASK]=129 不在 spiece 中,需要手动补全。
inference.py 内置的 TCRT5Tokenizer 用 SentencePiece 处理 0–27 区间,剩余
id 直接拼整数,行为与上游 TCRT5Tokenizer 一致。import torch_npu # 驱动加载
from torch_npu.contrib import transfer_to_npu # cuda.* -> npu.*
from transformers import T5ForConditionalGeneration
model = T5ForConditionalGeneration.from_pretrained(...).to("npu:0")transfer_to_npu 已自动完成:
torch.cuda.is_available() → True(NPU 可用)tensor.cuda() / module.cuda() → tensor.npu() / module.npu()torch.device('cuda') → torch.device('npu')torch.cuda.* → torch.npu.*dist.init_process_group(backend='nccl') → backend='hccl'/opt/atomgit/tcrt5_pre_tcrdb/
├── README.md # 本文档
├── requirements.txt # 推理环境依赖清单
├── inference.py # ★ 适配后服务化推理入口(FastAPI)
├── test_inference.py # ★ 端到端测试用例
├── test_result.json # ★ 测试结果(5 次 generate 耗时统计)
├── _make_screenshots.py # 生成三张截图的脚本
├── assets/
│ ├── npu_device_call.png # ★ NPU 设备调用截图(带 maggie_Ha 水印)
│ ├── model_result.png # ★ 测试结果截图(带 maggie_Ha 水印)
│ └── agent_workflow.png # ★ 适配过程截图(带 maggie_Ha 水印)
├── config.json
├── tokenizer_config.json
├── special_tokens_map.json
├── added_tokens.json
├── generation_config.json
├── spiece.model
└── model.safetensors # 模型权重 168 MB★ = 本次适配新增/生成。
source /usr/local/Ascend/ascend-toolkit/set_env.sh
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/注意:
transformers==4.57.6要求huggingface_hub<1.0,请勿升级到 1.x。
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0
python3 inference.py --host 0.0.0.0 --port 8089启动日志样例(中文):
[2026-08-22 00:25:31] [INFO] TCRT5Tokenizer 初始化完成:spiece=28, extra_id=100, [CLS]/[MASK]=2, 总 vocab=130
[2026-08-22 00:25:33] [INFO] 模型加载完成(耗时 1.24s,参数总量=41990144)
[2026-08-22 00:25:33] [INFO] 预热完成(耗时 0.37s)
[2026-08-22 00:25:33] [INFO] 服务就绪,可访问 /health /encode /generate| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /health | 健康检查,返回设备与 vocab 信息 |
| POST | /encode | 编码 + encoder 前向(与官方 README 用法对齐) |
| POST | /generate | seq2seq 生成 TCR 序列,返回 token id + 文本 + 耗时 |
/encode 请求示例:
curl -X POST http://127.0.0.1:8089/encode \
-H 'Content-Type: application/json' \
-d '{"sequence":"[PMHC]KLGGALQAK[SEP]YFAMYQENVAQTDVDTLYIIYRDYTWAELAYTWY[EOS]"}'/generate 请求示例:
curl -X POST http://127.0.0.1:8089/generate \
-H 'Content-Type: application/json' \
-d '{"sequence":"[PMHC]KLGGALQAK[SEP]YFAMYQENVAQTDVDTLYIIYRDYTWAELAYTWY[EOS]","max_new_tokens":32,"num_beams":1}'test_inference.py 启动服务并执行端到端推理(5 次 generate 取统计):
| 阶段 | 耗时 |
|---|---|
| 服务启动(加载 + 预热) | 9.48 s |
/encode(encoder 前向) | 9.54 ms |
/generate × 5 — min | 390.229 ms |
/generate × 5 — max | 415.065 ms |
/generate × 5 — mean | 402.717 ms |
/generate × 5 — 输出 token 数 | 33 |
⚠️ 这是 预训练 模型(仅 MLM 预训练),未做 seq2seq 微调,因此
generate输出主要是<extra_id_X>哨兵 token 序列。如需产生可用的 TCR 序列,请使用微调版 TCRT5-FT-TCRdb。
详细结果见 test_result.json。
assets/ 目录下三张带 maggie_Ha 水印的 PNG 截图:
| 文件 | 内容 |
|---|---|
npu_device_call.png | npu-smi info 输出 + 设备状态 |
model_result.png | 端到端测试结果,含 5 次 generate 耗时统计 |
agent_workflow.png | 适配过程全流程示意 |
# TCRT5 model (pre-trained)
This model is the pre-trained model used for finetuning TCRT5. The finetuned model is a
seq2seq model designed for the conditional generation of T-cell receptor (TCR) sequences
given a target peptide-MHC (pMHC). It is a transformers model built on the T5 architecture
and operationalized by the associated HuggingFace abstraction. It is released along with
this paper.
### How to use (CUDA/HuggingFace 原始用法)
```python
from transformers import T5Tokenizer, T5ForConditionalGeneration
tokenizer = T5Tokenizer.from_pretrained('dkarthikeyan1/tcrt5_pre_tcrdb')
tcrt5 = T5ForConditionalGeneration.from_pretrained("dkarthikeyan1/tcrt5_pre_tcrdb")
pmhc = "[PMHC]KLGGALQAK[SEP]YFAMYQENVAQTDVDTLYIIYRDYTWAELAYTWY[EOS]"
encoded_pmhc = tokenizer(pmhc, return_tensors='pt')
enc_outputs = tcrt5.encoder(**encoded_pmhc)As it stands, the model was jointly pre-trained on peptide-pseudosequence and CDR3 (β) sequences. Sequences comprised of just peptide, CDR3 (α), or other parts of the TCR would be out-of-distribution (OOD).
@Article{Karthikeyan2025_tcrtranslate,
author = {Karthikeyan, Dhuvarakesh and Bennett, Sarah N. and Reynolds, Amy G.
and Vincent, Benjamin G. and Rubinsteyn, Alex},
title = {Conditional generation of real antigen-specific T cell receptor sequences},
journal = {Nature Machine Intelligence},
year = {2025},
doi = {10.1038/s42256-025-01096-6},
url = {https://doi.org/10.1038/s42256-025-01096-6}
}