模型名称: 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
输入规格:
(batch_size, sequence_length)输出规格:
(batch_size, target_length=896, num_labels=5313)| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| transformers | >= 4.40.0 | HuggingFace Transformers |
| multimolecule | >= 0.2.0 | MultiMolecule 生物模型库 |
| 昇腾驱动 | 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# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一: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# 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| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --sequence | str | None | DNA 序列字符串 |
| --sequence-file | str | None | FASTA 文件路径 |
| --batch-size | int | 1 | 批处理大小 |
| --device | str | npu:0 | NPU 设备 |
| --max-seq-len | int | 196608 | 最大序列长度 |
[模型] multimolecule/enformer
[设备] Ascend910_9362 (npu:0)
[输入] DNA sequence, shape=(1, 196608)
[输出] coverage predictions, shape=(1, 896, 5313)
[耗时] 0.288s
[状态] SUCCESS[基准测试] 2次推理统计:
平均: 0.204s
最小: 0.204s
最大: 0.204s运行命令:
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!运行命令:
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!


测试数据: 随机生成的 DNA 序列(模拟输入)
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 输出 Shape | (1, 896, 5313) | 与模型定义一致 |
| 输出 Dtype | float32 | 全精度推理 |
| Coverage 非负性 | 全部 ≥ 0 | softplus 激活保证 |
| 推理延迟 (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}')
"| 配置项 | 值 |
|---|---|
| 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 |
torch.lgamma CPU fallback:相对位置编码中的 Gamma 分布计算使用 torch.lgamma,该算子当前在 NPU 上不支持,会自动回退到 CPU。由于该计算只在位置编码阶段执行一次,对整体推理性能影响较小。
最小序列长度限制:默认配置下,输入序列长度必须 ≥ 114,688 bp(target_length × 2^num_downsamples = 896 × 128)。短于该长度的序列会被自动填充。
内部格式警告:F.one_hot 生成的张量可能带有内部格式标记,torch_npu 会回退到基础格式,不影响功能但可能有轻微性能影响。