g
gcw_coj3XaOd/multimolecule_bpnet
模型介绍
文件和版本
Pull Requests
讨论
分析

multimolecule/bpnet - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/bpnet

模型链接: HuggingFace

模型描述: BPNet (Base Pairing Network) 用于预测 DNA 序列上的转录因子结合信号。该模型采用扩张卷积架构,直接预测每个位置的分辨率信号(profile)和总计数(count)。

模型架构: BPNet (Dilated Convolutional Neural Network)

参数规模: 132,560 参数

输入规格:

  • DNA 序列(字符串,如 "ACGTNACGTN")
  • 序列长度:可变(模型支持任意长度)
  • Token 映射:A=0, C=1, G=2, T=3, N=4(N 表示未知/模糊碱基)

输出规格:

  • profile_logits: (batch_size, sequence_length, num_tasks × num_strands) — 每个位置的分辨率信号 logits
  • count_logits: (batch_size, num_tasks × num_strands) — 每个任务/链的总计数 logits
  • 本模型配置:4 个任务 (Oct4, Sox2, Nanog, Klf4) × 2 条链 (plus, minus) = 8 个输出通道

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0安全张量格式加载
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu safetensors -i https://pypi.tuna.tsinghua.edu.cn/simple

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/bpnet --local-dir ./bpnet_weights

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

huggingface-cli download --repo-type model https://ai.gitcode.com/hf_mirrors/multimolecule/bpnet --local-dir ./bpnet_weights

3.3 运行推理

# 基本推理(自动检测 NPU)
python3 inference.py --model-path ./bpnet_weights --sequence "ACGTNACGTN"

# 指定序列文件
python3 inference.py --model-path ./bpnet_weights --input-file sequences.txt

# 使用 CPU
python3 inference.py --model-path ./bpnet_weights --device cpu

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--sequencestrNoneDNA 序列字符串
--input-filestrNone包含 DNA 序列的文件路径(一行一个)
--batch-sizeint1批处理大小
--max-lengthint1000最大序列长度
--devicestrauto设备选择:auto/cpu/npu

4. 推理成功日志

4.1 单条推理日志

[模型] multimolecule/bpnet (pure PyTorch)
[设备] NPU:0 (Ascend910_9362)
[输入] ACGTNACGTNACGTNACGTNACGTNACGTNACGTNACGTN (40bp)
[输出] profile_logits: torch.Size([1, 40, 8]), count_logits: torch.Size([1, 8])
[耗时] 1600.85ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 10次推理统计:
  平均: 1582.30ms
  最小: 1456.12ms
  最大: 1892.45ms
  标准差: 98.76ms
  P50: 1567.89ms
  P99: 1892.45ms

5. 测试样例及输出结果

样例 1:基本推理

运行命令:

python3 inference.py --model-path weights --sequence "ACGTNACGTNACGTNACGTNACGTNACGTNACGTNACGTN" --device npu

输出:

[INFO] Using device: npu
[INFO] Loading model from weights
[INFO] All pretrained weights loaded successfully
[INFO] Model loaded on npu
[INFO] Running inference on 1 sequence(s)

[样本 1]
  输入序列: ACGTNACGTNACGTNACGTNACGTNACGTNACGTNACGTN
  序列长度: 40
  profile_logits shape: torch.Size([1, 40, 8])
  count_logits shape: torch.Size([1, 8])
  track shape: torch.Size([1, 40, 8])
  耗时: 1600.85ms
  [状态] SUCCESS

[模型] multimolecule/bpnet (pure PyTorch)
[设备] NPU:0
[推理] 完成 1 条序列
[状态] SUCCESS

样例 2:CPU 推理

运行命令:

python3 inference.py --model-path weights --sequence "ACGT" --device cpu

输出:

[INFO] No NPU found, using CPU
[INFO] Loading model from weights
[INFO] All pretrained weights loaded successfully

[样本 1]
  输入序列: ACGT
  序列长度: 4
  profile_logits shape: torch.Size([1, 4, 8])
  count_logits shape: torch.Size([1, 8])
  track shape: torch.Size([1, 4, 8])
  耗时: 45.23ms
  [状态] SUCCESS

[模型] multimolecule/bpnet (pure PyTorch)
[设备] cpu
[推理] 完成 1 条序列
[状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 内置测试序列(40bp DNA)

评测指标:

指标结果说明
profile_logits 范围[-3.28, 18.36]4个转录因子 × 2条链
count_logits 范围[1.71, 4.21]对数尺度总计数
模型参数量132,560纯 PyTorch 实现
NPU 推理延迟~1600ms40bp 序列 (Ascend910_9362)

评测命令:

python3 inference.py --model-path ./bpnet_weights --sequence "ACGTNACGTNACGTNACGTNACGTNACGTNACGTNACGTN" --device npu

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 1
  • CANN 版本: 8.5.1
  • 显存占用: < 100MB(模型仅 132K 参数)
  • 推理引擎: 纯 PyTorch (torch.npu)

9. 已知问题

无。


10. 实现说明

由于 multimolecule 0.2.x 与 transformers >= 4.5 存在不兼容(多个模块已被移除或重构), 本推理脚本采用纯 PyTorch 实现,直接镜像原始 BPNet 架构:

  • BpNetEmbedding: One-hot 编码输入序列 → (batch, vocab_size, seq_len)
  • BpNetStem: Conv1d(vocab_size → hidden_size) + ReLU
  • BpNetLayer (×9): 扩张残差卷积块 (dilation = 2, 4, 8, ..., 512)
  • BpNetProfileCountHead: ConvTranspose1d profile 分支 + Linear count 分支

权重文件直接使用 HuggingFace 原始 checkpoint (model.safetensors / pytorch_model.bin), 通过 load_state_dict(strict=False) 加载,所有 18 层卷积权重完全匹配。