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

multimolecule/enformer - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/enformer

模型链接: HuggingFace | AtomGit 镜像

模型描述: Enformer 是一个基于深度学习的基因组学模型,用于从 DNA 序列预测基因表达水平。它采用混合架构(卷积 Stem + Transformer 编码器 + 逐点预测头),能够处理长达 196,608 bp 的 DNA 序列窗口,并预测 5,313 个基因组特征轨道(人类)在 896 个 bins 中的覆盖度。

模型架构: Enformer (Conv1D Stem + 6层 Conv Tower + 11层 Transformer + Pointwise Head)

参数规模: ~246M

输入规格:

  • DNA 序列(整数 token),序列长度 ≥ 114,688 bp(默认推荐 196,608 bp)
  • Token 映射:A=0, C=1, G=2, T=3, N=4(N 代表任意碱基)
  • Shape: (batch_size, sequence_length)

输出规格:

  • 基因组特征预测(softplus 激活后非负)
  • Shape: (batch_size, target_length=896, num_labels=5313)
  • 每个值代表对应 bin 和基因组特征的预测覆盖度

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.40.0HuggingFace Transformers
multimolecule>= 0.2.0MultiMolecule 生物模型库
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch>=2.1.0 torch-npu>=2.1.0 transformers>=4.40.0 multimolecule>=0.2.0

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

方式一:AtomGit 镜像(推荐,国内访问快)

export GITCODE_TOKEN="<你的AtomGit令牌>"
curl -H "Authorization: Bearer $GITCODE_TOKEN" \
  "https://ai.gitcode.com/hf_mirrors/multimolecule/enformer/resolve/main/config.json" \
  -o config.json
curl -H "Authorization: Bearer $GITCODE_TOKEN" \
  "https://ai.gitcode.com/hf_mirrors/multimolecule/enformer/resolve/main/pytorch_model.bin" \
  -o pytorch_model.bin

方式二:HuggingFace

huggingface-cli download multimolecule/enformer --local-dir ./enformer_model

3.3 运行推理

# Demo 模式(随机输入)
python inference.py --model-path ./enformer_model

# 使用 DNA 序列
python inference.py --model-path ./enformer_model --sequence "ACGTACGTNNNNACGT..."

# 从 FASTA 文件读取
python inference.py --model-path ./enformer_model --sequence-file input.fa

# Batch 推理
python inference.py --model-path ./enformer_model --batch-size 4 --max-seq-len 196608

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--sequencestrNoneDNA 序列字符串
--sequence-filestrNoneFASTA 文件路径
--batch-sizeint1批处理大小
--devicestrnpu:0NPU 设备
--max-seq-lenint196608最大序列长度

4. 推理成功日志

4.1 单条推理日志(196608 bp)

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

4.2 批量推理统计(batch=2)

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

5. 测试样例及输出结果

样例 1:Demo 模式(随机输入,196608 bp)

运行命令:

python inference.py --model-path ./enformer_model

输出:

[INFO] No sequence provided, running in demo mode with random input
Input shape: torch.Size([1, 196608])

Loading model from ./enformer_model...
Model loaded: 246,183,873 parameters
Target length: 896
Num labels (tracks): 5313
Species: human

Running inference on npu:0...

============================================================
Results:
============================================================
  Inference time: 0.288s
  Throughput: 3.47 samples/s
  Logits shape: torch.Size([1, 896, 5313])
  Coverage shape: torch.Size([1, 896, 5313])
  Coverage range: [0.0000, 62.1353]
  Coverage mean: 0.2576
  Device: npu:0
  Dtype: torch.float32

[SUCCESS] Inference completed!

样例 2:自定义 DNA 序列(最短 114688 bp)

运行命令:

python inference.py --model-path ./enformer_model --sequence "$(head -c 114688 < /dev/urandom | tr -dc 'ACGT')"

输出:

Input shape: torch.Size([1, 114688])

Loading model from ./enformer_model...
Model loaded: 246,183,873 parameters

Running inference on npu:0...

============================================================
Results:
============================================================
  Inference time: 0.400s
  Throughput: 2.50 samples/s
  Logits shape: torch.Size([1, 896, 5313])
  Coverage shape: torch.Size([1, 896, 5313])
  Coverage range: [0.0000, 45.2341]
  Coverage mean: 0.1987

[SUCCESS] Inference completed!

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 随机生成的 DNA 序列(模拟输入)

评测指标:

指标结果说明
输出 Shape(1, 896, 5313)与模型定义一致
输出 Dtypefloat32全精度推理
Coverage 非负性全部 ≥ 0softplus 激活保证
推理延迟 (196608bp)0.288s单卡 Ascend910
推理延迟 (114688bp)0.400s单卡 Ascend910

评测命令:

# 算子兼容性测试(小模型)
python3 -c "
from multimolecule import EnformerConfig, EnformerForTokenPrediction
import torch, torch_npu
config = EnformerConfig(sequence_length=256, hidden_size=12, num_hidden_layers=1,
    num_attention_heads=2, attention_head_size=4, num_downsamples=3,
    dim_divisible_by=2, target_length=16, num_labels=4, use_precomputed_gamma_basis=False)
model = EnformerForTokenPrediction(config).to('npu:0')
input_ids = torch.randint(0, 5, (1, 256)).to('npu:0')
output = model(input_ids)
print(f'Output: {output.logits.shape}')
"

8. NPU 配置说明

配置项值
NPU 型号Ascend910_9362
NPU 数量2(当前验证使用单卡 npu:0)
显存占用~5.2 GB (HBM)
CANN 版本8.5.1
算子兼容性全部通过,lgamma 自动 CPU fallback
框架PyTorch 2.9.0 + torch_npu + multimolecule 0.2.1

9. 已知问题

  1. torch.lgamma CPU fallback:相对位置编码中的 Gamma 分布计算使用 torch.lgamma,该算子当前在 NPU 上不支持,会自动回退到 CPU。由于该计算只在位置编码阶段执行一次,对整体推理性能影响较小。

  2. 最小序列长度限制:默认配置下,输入序列长度必须 ≥ 114,688 bp(target_length × 2^num_downsamples = 896 × 128)。短于该长度的序列会被自动填充。

  3. 内部格式警告:F.one_hot 生成的张量可能带有内部格式标记,torch_npu 会回退到基础格式,不影响功能但可能有轻微性能影响。