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

multimolecule/aparent2 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/aparent2

模型链接: HuggingFace

模型描述: Aparent2 是一个基于扩张残差 1D CNN 的深度学习模型,用于预测核酸适配体(aptamer)与目标蛋白的结合亲和力。模型接受一段 DNA 序列(长度 205)作为输入,输出每个位置对 206 种标签的回归预测值。

模型架构: Dilated Residual 1D CNN(扩张残差一维卷积神经网络)

  • 7 层扩张卷积组(dilations: [1, 2, 4, 8, 4, 2, 1])
  • 每组 4 个残差块(DilatedBlock)
  • BatchNorm1d + ReLU 激活
  • 隐藏层维度: 32
  • 词汇表大小: 5 (A/C/G/T/N)

参数规模: 188,965

输入规格:

  • DNA 序列字符串,长度 205,字符集 {A, C, G, T, N}
  • 库索引(library_index),整数 0-12,默认 11

输出规格:

  • 回归预测张量,shape = (batch_size, 206, 205),dtype = float32
  • 206 个标签 × 205 个位置的亲和力预测值

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
numpy>= 1.26.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu numpy

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 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download multimolecule/aparent2 --local-dir ./weights

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

https://ai.gitcode.com/hf_mirrors/multimolecule/aparent2

3.3 运行推理

# 默认序列推理(使用内置 205bp 测试序列)
python3 inference.py --model-path ./weights

# 指定自定义 DNA 序列
python3 inference.py --model-path ./weights --sequence "ACGTACGT..."

# 指定库索引
python3 inference.py --model-path ./weights --library-index 11

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 目录路径(含 config.json 和 pytorch_model.bin)
--sequencestr内置默认序列DNA 序列字符串(A/C/G/T/N),长度 205
--library-indexint11文库索引,用于 bias 校正(0-12)
--batch-sizeint1推理批次大小

4. 推理成功日志

[信息] 权重加载成功: ./weights/pytorch_model.bin
[信息] 模型参数量: 188,965

[信息] 设备: npu
[信息] NPU型号: Ascend910_9362

[输入] 序列长度: 205
[输入] Library index: 11

[输出] Shape: (1, 206, 205)
[输出] dtype: float32
[输出] min: -61.387547, max: 4.134477, mean: -27.359608

[输出详情] Sample 0, 前10个位置的平均预测值:
  pos 0: mean=-0.336543, min=-6.478485, max=4.134477
  pos 1: mean=-5.485650, min=-11.627592, max=-1.014630
  pos 2: mean=-15.060293, min=-21.202236, max=-10.589273
  pos 3: mean=-14.141421, min=-20.283363, max=-9.670402
  pos 4: mean=-14.966439, min=-21.108381, max=-10.495420
  pos 5: mean=-17.486038, min=-23.627981, max=-13.015018
  pos 6: mean=-21.807295, min=-27.949238, max=-17.336275
  pos 7: mean=-18.953102, min=-25.095045, max=-14.482082
  pos 8: mean=-15.722246, min=-21.864187, max=-11.251225
  pos 9: mean=-19.851604, min=-25.993547, max=-15.380585

[状态] SUCCESS

注意: 推理失败时必须输出 [状态] FAILED 和错误信息,不得静默退出。


5. 测试样例及输出结果

样例 1:默认序列推理

运行命令:

python3 inference.py --model-path ./weights

输出:

[信息] 权重加载成功: ./weights/pytorch_model.bin
[信息] 模型参数量: 188,965

[信息] 设备: npu
[信息] NPU型号: Ascend910_9362

[输入] 序列长度: 205
[输入] Library index: 11

[输出] Shape: (1, 206, 205)
[输出] dtype: float32
[输出] min: -61.387547, max: 4.134477, mean: -27.359608

[输出详情] Sample 0, 前10个位置的平均预测值:
  pos 0: mean=-0.336543, min=-6.478485, max=4.134477
  pos 1: mean=-5.485650, min=-11.627592, max=-1.014630
  pos 2: mean=-15.060293, min=-21.202236, max=-10.589273
  pos 3: mean=-14.141421, min=-20.283363, max=-9.670402
  pos 4: mean=-14.966439, min=-21.108381, max=-10.495420
  pos 5: mean=-17.486038, min=-23.627981, max=-13.015018
  pos 6: mean=-21.807295, min=-27.949238, max=-17.336275
  pos 7: mean=-18.953102, min=-25.095045, max=-14.482082
  pos 8: mean=-15.722246, min=-21.864187, max=-11.251225
  pos 9: mean=-19.851604, min=-25.993547, max=-15.380585

[状态] SUCCESS

样例 2:自定义序列推理

运行命令:

python3 inference.py --model-path ./weights --sequence "ATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCG"

输出:

[信息] 权重加载成功: ./weights/pytorch_model.bin
[信息] 模型参数量: 188,965

[信息] 设备: npu
[信息] NPU型号: Ascend910_9362

[输入] 序列长度: 205
[输入] Library index: 11

[输出] Shape: (1, 206, 205)
[输出] dtype: float32

[状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

Aparent2 模型的评测使用回归任务标准指标:

评测指标:

指标说明
MSE (Mean Squared Error)均方误差,衡量预测值与真实值的偏离程度
MAE (Mean Absolute Error)平均绝对误差,衡量预测精度
Pearson Correlation皮尔逊相关系数,衡量预测值与真实值的线性相关性

评测命令:

# 使用真实数据集进行评测(需要提供包含序列和标签的 CSV/TSV 文件)
python3 -c "
import torch
from inference import Aparent2Model, seq_to_ids
import numpy as np

model = Aparent2Model()
state = torch.load('./weights/pytorch_model.bin', map_location='cpu')
model.load_state_dict(state, strict=False)
model.eval()

# 加载测试数据(序列和标签)
# sequences: list of 205-length DNA strings
# labels: numpy array of shape (N, 206, 205)
# 计算 MSE, MAE, Pearson correlation
"

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 1
  • CANN 版本: 8.5.1
  • PyTorch 版本: 2.1.0+ (with torch_npu)
  • 显存占用: < 1GB(模型仅 188K 参数)
  • 推理模式: eager mode, single NPU device

9. 已知问题

无