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

ScBasset - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/scbasset

模型链接: HuggingFace

模型描述: scBasset 是一个基于 1D 卷积神经网络的单细胞染色质可及性预测模型。输入 DNA 序列(1344bp),输出 2034 个细胞类型的可及性分数。

模型架构: ScBassetForSequencePrediction (8-layer 1D-CNN + BatchNorm + Bottleneck + Linear head)

参数规模: 4,585,978 (~4.6M)

输入规格:

  • DNA 序列,长度固定 1344 bp
  • 核苷酸映射:A=0, C=1, G=2, T=3, N=4(one-hot 编码,5 类)
  • Shape: (batch_size, 1344),dtype: int64

输出规格:

  • 2034 维细胞类型可及性预测 logits
  • Shape: (batch_size, 2034),dtype: float32

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.0.0HuggingFace Transformers(需兼容 shim)
multimolecule>= 0.2.0模型定义与权重加载
safetensors>= 0.4.0安全张量格式
numpy< 2.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch_npu==2.9.0.post1 transformers==4.57.6 multimolecule>=0.2.0 safetensors numpy==1.26.4

注意: multimolecule 0.2.x 依赖 transformers.initialization 模块(transformers >= 4.5 中已移除)。 运行推理前需注入兼容 shim,或在 sitecustomize.py 中配置。


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

模型权重文件位于 weights/ 目录:

  • config.json — 模型配置
  • model.safetensors — 模型权重(56 个张量,~17.5MB)
  • tokenizer_config.json — tokenizer 配置
  • special_tokens_map.json — 特殊 token 映射
  • vocab.txt — 词汇表

3.3 运行推理

# 单条序列推理
python3 inference.py --model-path weights --sequence "ACGTACGTACGT..."

# 从 FASTA 文件推理
python3 inference.py --model-path weights --fasta input.fasta

# 批量推理
python3 inference.py --model-path weights --batch-sequences seqs.txt --batch-size 16

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--sequencestrNone单条 DNA 序列
--fastastrNoneFASTA 格式文件路径
--batch-sequencesstrNone每行一条 DNA 序列的文本文件
--batch-sizeint8推理批大小
--top-kint5输出 Top-K 细胞类型

4. 推理成功日志

4.1 单条推理日志

[模型] ScBasset (multimolecule/scbasset)
[设备] Ascend NPU (npu:0)
[权重] weights
[序列数] 3, 每条长度=1344bp
[批大小] 8
[参数量] 4,585,978
[耗时] 0.15s (50.6ms/seq)

[输出] logits shape: torch.Size([3, 2034])  (batch=3, num_labels=2034)
[状态] SUCCESS

4.2 批量推理日志

[批量统计]
  平均logit: -3.7341
  Std: 1.3085
  Min: -9.3728, Max: 0.3686

5. 测试样例及输出结果

样例 1:AT 丰富序列

运行命令:

python3 inference.py --model-path weights --sequence "ATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATATAT"

输出:

[模型] ScBasset (multimolecule/scbasset)
[设备] Ascend NPU (npu:0)
[权重] weights
[序列数] 1, 每条长度=1344bp
[批大小] 8
[参数量] 4,585,978
[耗时] 0.02s (21.3ms/seq)

[输出] logits shape: torch.Size([1, 2034])
[状态] SUCCESS

样例 2:GC 丰富序列

运行命令:

python3 inference.py --model-path weights --sequence "GCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGCGC"

输出:

[输出] logits shape: torch.Size([1, 2034])
[状态] SUCCESS

样例 3:混合序列批量推理

运行命令:

python3 inference.py --model-path weights --sequence "ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT"

输出:

[序列数] 3, 每条长度=1344bp
[输出] logits shape: torch.Size([3, 2034])
[状态] SUCCESS

Top-5 可及性预测 (seq 1):
  #1: cell_type_id=1224, logit=0.3686
  #2: cell_type_id=770, logit=0.1096
  #3: cell_type_id=1868, logit=-0.0648
  #4: cell_type_id=489, logit=-0.1637
  #5: cell_type_id=1189, logit=-0.2123

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 合成 DNA 序列(AT-rich、GC-rich、随机混合),长度 1344bp

评测指标:

指标结果说明
参数量4,585,978模型总参数
推理延迟~50ms/seq (batch=1, NPU)单条推理耗时
AT vs GC 差异3.33 (mean abs diff)不同序列产生不同预测
输出范围[-12.75, 3.71]全量推理 logits 范围

评测命令:

python3 inference.py --model-path weights --sequence "ATATAT..."

实际输出:

[输出] logits shape: torch.Size([1, 2034])
[状态] SUCCESS

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2
  • CANN 版本: 8.5.1
  • PyTorch 版本: 2.9.0+cpu (with torch_npu 2.9.0.post1)
  • 配置: 单卡 (npu:0),无 Tensor Parallel

9. 已知问题

  1. config.json 中 num_labels=1:该值为 legacy 设置,实际模型输出 2034 维(对应 2034 个细胞类型)。head 层为 sequence_head.decoder,权重 shape (2034, 32)。
  2. transformers 兼容性:multimolecule 0.2.x 依赖 transformers.initialization 模块(transformers >= 4.5 中已移除),运行时需通过 sitecustomize.py 注入兼容 shim,或在同进程中手动注入。
  3. 无 learnable embedding:scBasset 使用 one-hot 编码直接作为输入,无需嵌入层查找表。