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

Basenji - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/basenji

模型链接: HuggingFace

模型描述: Basenji 是一个深度一维卷积神经网络(1D dilated CNN),用于从长 DNA 序列预测基因组调控活性。模型输入约 131kb 的 DNA 序列窗口,经过卷积+池化 stem 和扩张残差卷积塔,输出每个 128bp bin 的基因组覆盖度预测(5313 条轨道)。

模型架构: 1D Dilated Residual CNN (Basenji2)

  • Stem: Conv1d(kernel=15, channels=288) + MaxPool1d(pool=2)
  • Conv Tower: 6 阶段渐进通道 [339→399→470→554→652→768],每阶段含池化下采样
  • Dilated Residual Blocks: 11 个扩张瓶颈块(dilation 指数增长,最大 dilation=58)
  • Head: Conv1D(768→1536, kernel=1) + GELU + Conv1D(1536→5313, kernel=1)
  • Output: Softplus 激活 → 非负覆盖度

参数规模: 30,092,548 (~30M)

输入规格:

  • 类型: DNA token IDs(整数序列)
  • Shape: (batch_size, 131072)
  • 词汇表: 5(0=PAD, 1=A, 2=C, 3=G, 4=T)
  • 模型内部自动转为 one-hot 编码 (5 channels × 131072)

输出规格:

  • 类型: 基因组覆盖度预测(浮点)
  • Shape: (batch_size, 896, 5313)(896 个 bins × 5313 条基因组轨道)
  • 每个 bin 对应 128bp DNA 序列
  • 激活函数: softplus(确保非负值)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 5.0.0HuggingFace Transformers
multimolecule>= 0.12.0MultiMolecule 基因组学模型库
numpy>= 1.20.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1
NPU 硬件Ascend 910验证设备: Ascend910_9362

安装命令:

pip install torch torch_npu transformers multimolecule 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/basenji --local-dir ./model

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

git clone https://ai.gitcode.com/hf_mirrors/multimolecule/basenji.git ./model

3.3 运行推理

# 基础推理(默认 seq_len=131072)
python inference.py --model-path ./model

# 指定序列长度(更短序列可减少内存占用)
python inference.py --model-path ./model --sequence-length 65536

# 指定 NPU 设备
python inference.py --model-path ./model --device 0

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--sequence-lengthint131072DNA 序列长度(bp)
--deviceint0NPU 设备索引

4. 推理成功日志

4.1 单条推理日志

[模型] multimolecule/basenji
[设备] Ascend910_9362 (npu:0)
[输入] DNA sequence, shape=(1, 131072)
[输出] Genomic coverage, shape=(1, 896, 5313)
[耗时] 0.25s
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 2次推理统计:
  平均: 0.01s
  最小: 0.01s
  最大: 0.01s

5. 测试样例及输出结果

样例 1:全序列长度推理

运行命令:

python inference.py --model-path ./model --sequence-length 131072

输出:

[INFO] Device: Ascend910_9362
[INFO] NPU device count: 2
[INFO] Loading model from ./model ...
Loading weights: 100%|██████████| 182/182 [00:00<00:00, 2744.61it/s]
[INFO] Model loaded: 30,092,548 parameters
[INFO] Input shape: torch.Size([1, 131072])
[INFO] Running inference ...

============================================================
[RESULTS]
  Input shape       : torch.Size([1, 131072])
  Output logits     : torch.Size([1, 896, 5313])
  Coverage          : torch.Size([1, 896, 5313])
  Inference time    : 0.25s
  Throughput        : 3.9738 samples/sec
  Logits mean       : -3.3901
  Logits std        : 1.3340
  Coverage mean     : 0.0756
  Coverage max      : 16.0691
  [状态] SUCCESS
============================================================

样例 2:批量推理

运行命令:

python inference.py --model-path ./model --sequence-length 65536

输出:

[INFO] Running batch inference (batch_size=2) ...
  Batch input shape : torch.Size([2, 65536])
  Batch output      : torch.Size([2, 384, 5313])
  Batch time        : 0.01s
  [状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: ENCODE/FANTOM5/GTEx 基因组覆盖度轨道(5313 条实验数据)

评测指标:

指标结果说明
参数数量30,092,548~30M
FLOPs234.85 G单次 131072bp 窗口
MACs117.19 G单次 131072bp 窗口
推理耗时 (NPU)0.25sAscend910_9362, seq_len=131072
覆盖度均值0.0756softplus 激活后
覆盖度最大值16.0691softplus 激活后

评测命令:

python inference.py --model-path ./model --sequence-length 131072

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2(单卡推理,模型可扩展至多卡)
  • Tensor Parallel: N/A(单卡 CNN 模型,无需 TP)
  • 显存占用: ~3.1 GB HBM(seq_len=131072)
  • CANN 版本: 8.5.1

9. 已知问题

无。模型所有算子均为标准 PyTorch 算子,在 Ascend NPU 上可直接运行。


参考文献

@article{kelley2018sequential,
  author    = {Kelley, David R. and Reshef, Yakir A. and Bileschi, Maxwell and Belanger, David and McLean, Cory Y. and Snoek, Jasper},
  title     = {Sequential regulatory activity prediction across chromosomes with deep convolutional and recurrent neural networks},
  journal   = {Genome Research},
  year      = 2018,
  volume    = 28,
  number    = 5,
  pages     = {739--750},
  doi       = {10.1101/gr.227819.117},
  publisher = {Cold Spring Harbor Laboratory}
}