g
gcw_coj3XaOd/multimolecule_aparent2
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

multimolecule/aparent2 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/aparent2

模型链接: HuggingFace

模型描述: APARENT2 是一个残差卷积神经网络,用于预测人类 3' UTR 选择性多聚腺苷酸化(APA)和切割强度。该网络完全由卷积层和位置级局部连接的文库偏置层组成,不使用全连接层。

模型架构: Residual Convolutional Network (APARENT2)

参数规模: 188,965 参数

输入规格:

  • input_ids: (batch_size, 205) int64 - RNA token IDs(5类:A/C/G/U/N)
  • attention_mask: (batch_size, 205) int64 - 可选 mask
  • One-hot 编码后: (batch_size, 5, 205) float32

输出规格:

  • logits: (batch_size, 206) float32 - APA 切割分布分数(含"无切割"bucket)
  • pooler_output: (batch_size, 206) float32 - 与 logits 相同
  • last_hidden_state: (batch_size, 205, 32) float32 - 特征图

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu>= 2.1.0昇腾 NPU 后端
transformers>= 5.0.0HuggingFace Transformers(需兼容补丁)
multimolecule>= 0.2.1生物分子深度学习框架
danling>= 0.2.0深度学习工具包
sentencepiece>= 0.1.99Tokenizer 依赖
protobuf>= 3.20.0Protocol Buffers
numpy< 2数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu==2.9.0.post1 transformers>=5.15.0 multimolecule>=0.2.1 danling>=0.2.0 sentencepiece>=0.1.99 protobuf>=3.20.0 "numpy<2"

3. 启动服务

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型加载

APARENT2 模型使用 multimolecule 包加载,支持从预训练权重或随机初始化运行:

# 克隆模型仓库(含权重)
git clone https://gitcode.com/hf_mirrors/multimolecule/aparent2.git

# 或使用 HuggingFace 下载
huggingface-cli download multimolecule/aparent2 --local-dir ./aparent2_model

4. 推理调用

4.1 单条推理

# 基本推理(使用随机初始化权重)
python inference.py

# 指定预训练权重路径
python inference.py --model-path ./aparent2_model

# 自定义序列(205 nt)
python inference.py --sequence "ACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUAC"

4.2 批量推理

# 批量推理(默认 batch_size=4)
python inference.py --batch-size 8

# 指定 NPU 设备
python inference.py --device npu:1

4.3 基准测试

# 100次推理基准测试
python inference.py --iterations 100

4.4 推理参数说明

参数类型默认值说明
--model-pathstrNone模型 checkpoint 路径(可选)
--devicestrnpu:0NPU 设备
--sequencestrN70+AAUAAA+N129205 nt RNA 序列
--batch-sizeint4批量大小
--iterationsint100基准测试迭代次数
--library-indexint11训练子文库索引

5. 推理成功日志

5.1 单条推理日志

[Model] multimolecule/aparent2
[Device] Ascend910_9362 (npu:0)
[Input] sequence length: 205 nt
[Output] logits.shape = torch.Size([1, 206])
[Output] pooler_output.shape = torch.Size([1, 206])
[Output] last_hidden_state.shape = torch.Size([1, 205, 32])
[Time] 210.44 ms
[Status] SUCCESS

5.2 批量推理日志

[Inference] Batch inference (4 sequences)...
[Output] batch logits.shape = torch.Size([4, 206])
[Time] 9.71 ms
[Status] BATCH SUCCESS

5.3 基准测试日志

[Benchmark] 100 forward passes...
[Benchmark] 100次推理统计:
  平均: 7.70 ms
  最小: 7.08 ms
  最大: 9.09 ms
  标准差: 0.28 ms
  P50: 7.67 ms
  P99: 9.09 ms

6. 测试样例及输出结果

样例 1:单条序列推理

运行命令:

python inference.py --sequence "ACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUACGUAC"

输出:

[Model] multimolecule/aparent2
[Device] Ascend910_9362 (npu:0)
[Parameters] 188,965
[Input] sequence length: 205 nt
[Inference] Single sequence...
[Output] logits.shape = torch.Size([1, 206])
[Output] pooler_output.shape = torch.Size([1, 206])
[Output] last_hidden_state.shape = torch.Size([1, 205, 32])
[Time] 210.44 ms
[Status] SUCCESS

样例 2:批量推理

运行命令:

python inference.py --batch-size 4

输出:

[Inference] Batch inference (4 sequences)...
[Output] batch logits.shape = torch.Size([4, 206])
[Status] BATCH SUCCESS

样例 3:基准测试

运行命令:

python inference.py --iterations 100

输出:

[Benchmark] 100 forward passes...
[Benchmark] 100次推理统计:
  平均: 7.70 ms
  最小: 7.08 ms
  最大: 9.09 ms
  标准差: 0.28 ms
  P50: 7.67 ms
  P99: 9.09 ms

7. Agent适配截图

7.1 Agent 完整适配工作流

Agent 适配流程

7.2 NPU 设备调用日志

NPU 设备调用

7.3 模型适配结果

模型适配结果


8. 精度评测

注意: 由于网络限制,无法从 HuggingFace 或 AtomGit 下载预训练权重。以下评测使用模型随机初始化权重进行架构验证。

测试数据: 合成 RNA 序列(205 nt,含 APA 信号 AAUAAA)

评测指标:

指标结果说明
模型参数量188,965总可训练参数
推理延迟 (单条)~210 ms首次推理(含模型编译)
推理延迟 (稳定)~7.7 ms100次推理平均值
输出维度206205位点 + "无切割"bucket
NPU 利用率Ascend910_93622卡可用

评测命令:

python inference.py --iterations 100

实际输出:

[Cleavage Distribution]
  Max cleavage position: 0
  Max probability: 0.488993
  No-cleavage probability: 0.020268

9. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2
  • 显存占用: ~3.1 GB (chip 0), ~2.9 GB (chip 1)
  • CANN 版本: 8.5.1
  • Tensor Parallel: 单卡推理(模型较小,无需 TP)
  • 兼容算子: Conv1d (dilation=1,2,4,8), BatchNorm1d, ReLU, Linear, Softmax

10. 已知问题

  1. 预训练权重下载受限: HuggingFace 超时,AtomGit 镜像需认证。推理使用随机初始化权重进行算子兼容性验证。
  2. Transformers 5.x 兼容性: find_pruneable_heads_and_indices 在 transformers >= 4.45 中移除,需 monkey-patch 补丁。已内置在 inference.py 中。
  3. Tokenizer 下载: RnaTokenizer 需要从 HuggingFace 下载,网络受限时 inference.py 会自动使用手动 one-hot 编码。