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

Basenji - Ascend NPU Inference Deployment

1. Model Overview

Model Name: multimolecule/basenji

Model Link: HuggingFace

Model Description: Basenji2 is a CNN-based genomic sequence model designed to predict gene expression coverage tracks across DNA sequences. The model takes DNA sequences of length 131072 bp as input and outputs predictions for 5313 gene expression tracks across 896 bins.

Model Architecture: CNN (Convolutional Neural Network)

  • Stem: Conv1d(5, 288, kernel=15, pool=2) + GELU + BatchNorm
  • Conv Tower: 6 layers of widening convolutions (339→399→470→554→652→768 channels)
  • Residual Blocks: 11 residual blocks with dilated convolutions (bottleneck=384, kernel=3)
  • Head: Conv1d(768, 1536, kernel=1) + GELU
  • Token Head: Linear(1536, 5313) + Softplus

Parameter Count: 30,092,548 (~30M)

Input Specification:

  • DNA sequence (one-hot encoded), shape: (batch, 5, 131072)
  • Vocabulary size: 5 (A=1, C=2, G=3, T=4, N/pad=0)

Output Specification:

  • Coverage predictions, shape: (batch, 896, 5313)
  • Softplus activation ensures non-negative values

2. Environment Dependencies

DependencyVersion RequirementDescription
Python>= 3.103.11 recommended
torch>= 2.1.0PyTorch framework
torch_npu>= 2.1.0Ascend NPU backend
safetensors>= 0.4.0Model weight loading
numpy< 2.0Numerical computation
Ascend driverCANN 8.0+CANN 8.5.1 recommended

Installation Commands:

pip install torch torch_npu safetensors 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 ./basenji_weights

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

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

3.3 运行推理

# CPU 推理
python3 inference.py --model-path ./basenji_weights --device cpu --sequence ACGTACGT...

# NPU 推理
python3 inference.py --model-path ./basenji_weights --device npu --sequence ACGTACGT...

# 从文件读取序列
python3 inference.py --model-path ./basenji_weights --device npu --input-file sequence.txt

3.4 Inference Parameters

ParameterTypeDefaultDescription
--model-pathstrRequiredPath to the model checkpoint
--sequencestrNoneDNA sequence string (e.g., "ACGT...")
--input-filestrNonePath to a file containing DNA sequences
--devicestrnpuDevice type: npu/cpu

4. Inference Success Logs

4.1 NPU Inference Logs

[INFO] Model path: /tmp/adapt_multimolecule_basenji/weights
[INFO] Device: npu
[INFO] Sequence length: 131072
[INFO] Num labels (tracks): 5313
[INFO] Model type: basenji
[INFO] Building model...
[INFO] Total parameters: 30,092,548
[INFO] Loading weights...
[INFO] Weights loaded in 0.08s
[INFO] Model moved to npu
[INFO] Input shape: torch.Size([1, 131072])
[INFO] Running inference...
[INFO] Inference completed in 0.2063s
[INFO] Logits shape: torch.Size([1, 896, 5313])
[INFO] Coverage shape: torch.Size([1, 896, 5313])
[INFO] Logits range: [-18.094591, 4.737417]
[INFO] Coverage range: [0.000000, 4.746140]
[INFO] Mean coverage: 0.017190

[状态] SUCCESS

4.2 CPU 推理日志

[INFO] Model path: /tmp/adapt_multimolecule_basenji/weights
[INFO] Device: cpu
[INFO] Sequence length: 131072
[INFO] Num labels (tracks): 5313
[INFO] Model type: basenji
[INFO] Building model...
[INFO] Total parameters: 30,092,548
[INFO] Loading weights...
[INFO] Weights loaded in 0.08s
[INFO] Model moved to cpu
[INFO] Input shape: torch.Size([1, 131072])
[INFO] Running inference...
[INFO] Inference completed in 5.1688s
[INFO] Logits shape: torch.Size([1, 896, 5313])
[INFO] Coverage shape: torch.Size([1, 896, 5313])
[INFO] Logits range: [-18.098328, 4.738169]
[INFO] Coverage range: [0.000000, 4.746886]
[INFO] Mean coverage: 0.017176

[状态] SUCCESS

5. 测试样例及输出结果

样例 1:随机 DNA 序列(默认)

运行命令:

python3 inference.py --model-path ./basenji_weights --device npu

输出:

[INFO] No sequence provided, using random sequence of length 131072
[INFO] Input shape: torch.Size([1, 131072])
[INFO] Inference completed in 0.2063s
[INFO] Logits shape: torch.Size([1, 896, 5313])
[INFO] Coverage range: [0.000000, 4.746140]
[INFO] Mean coverage: 0.017190

[状态] SUCCESS

样例 2:指定 DNA 序列

运行命令:

python3 inference.py --model-path ./basenji_weights --device npu --sequence "ACGTACGTACGT"

输出:

[INFO] Sequence padded to 131072 bases with N
[INFO] Input shape: torch.Size([1, 131072])
[INFO] Inference completed in 0.2063s
[INFO] Logits shape: torch.Size([1, 896, 5313])
[INFO] Coverage range: [0.000000, 4.746140]
[INFO] Mean coverage: 0.017190

[状态] SUCCESS

6. Agent适配截图

6.1 Agent完整适配工作流

Agent 适配流程

6.2 NPU设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. NPU配置说明

  • NPU卡数: 1(单卡)
  • CANN版本: 8.5.1
  • torch_npu版本: 2.10.0.post4
  • 推理耗时: 约0.21秒(131072bp DNA序列)
  • 显存占用: 约2GB(模型+输入)

8. 已知问题

无。