S
Solis_caser/bpnet-ascend-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

BPNet (multimolecule) — 昇腾 Ascend NPU 真实推理适配

在华为昇腾 910B (Ascend910_9362) 上,用 torch_npu 真实运行 MultiMolecule 的 BpNetForProfilePrediction(DNA 碱基分辨率信号预测), 所有参数与张量均位于 npu:0,并完成 track.sum(1) ≈ exp(count_logits) 数值校验。


1. 模型介绍

BPNet(Base-resolution prediction network)由 DeepMind 提出,用于从 DNA 序列 逐碱基预测染色质信号。MultiMolecule 的 BpNetForProfilePrediction 把单个 碱基分辨率任务分解为两个终端分支:

  • profile_logits:逐位多项分布 logits,形状 (batch, seq_len, num_labels)
  • count_logits:每个 task/strand 的对数总量标量,形状 (batch, num_labels)

二者经 postprocess 重组为可用 track:

track = softmax(profile_logits, dim=1) * exp(count_logits)   # (batch, seq_len, num_labels)

其中 num_labels = num_tasks × num_strands。本模型为 4 个转录因子 (Oct4 / Sox2 / Nanog / Klf4)× 2 条链(plus / minus)= 8 通道。

2. 上游模型

项值
上游模型multimolecule/bpnet(Hugging Face / GitCode 镜像)
固定 revision44aafb32fc6d71fcf12d0a73054d28e095fe3099
权重文件model.safetensors(唯一权重,36 个张量块,132560 参数)
大小533768 bytes
SHA256e29fb801c1b65e77d78706cb82b0ef2f2def848ce7de3495d4d85195d203ca22
架构BpNetForProfilePrediction
依赖库multimolecule==0.2.1、transformers==5.9.0

本适配固定该 revision 与 SHA256,不换 checkpoint。

3. 输入格式

  • 序列:184 nt 人类 p53 启动子(模型卡真实 DNA 使用序列)。
  • 分词器:DnaTokenizer(nmers=1,5 类字母表 A/C/G/T/N,vocab_size=5)。
  • 输入张量:inputs_embeds,形状 [1, 184, 5],float32。
  • NPU 兼容处理:NPU 上 F.one_hot 存在兼容性风险,因此在 CPU 上以数学等价 方式构造 5 类 one-hot(与 BpNetEmbedding 的 one_hot(input_ids.clamp(0,4), 5) 完全一致),随后 .to("npu:0")。全流程不用 pipeline,直接用 DnaTokenizer + BpNetForProfilePrediction。

4. NPU 环境

组件版本
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
CANN8.5.1
设备Ascend910_9362 × 2(目标 npu:0)
Python3.11.14

复用现有 torch / torch_npu,仅补缺依赖(见 requirements.txt): multimolecule==0.2.1、transformers==5.9.0。

5. 运行命令

先按 权重获取 放置权重到 weights/,然后:

flock -x -w 1800 /tmp/ascend-model-agent-npu-v1.lock python3 inference.py

脚本内置:权重 SHA256/大小校验 → 加载并迁移到 npu:0 → CPU 数学等价 one-hot 构造并上卡 → NPU 前向 → 形状/设备断言 → track.sum(1) ≈ exp(count_logits) 校验 → 输出真实结果到 real_result.json。

权重获取

本精简仓不含权重。复现时用 GitCode 镜像的 Git LFS batch 接口固定拉取 model.safetensors,并同步拉取 config.json、tokenizer_config.json、vocab.txt:

mkdir -p weights && cd weights
# 1) LFS batch 换取签名下载地址(OID=sha256, size 固定)
curl -s -X POST "https://gitcode.com/hf_mirrors/multimolecule/bpnet.git/info/lfs/objects/batch" \
  -H "Accept: application/vnd.git-lfs+json" \
  -H "Content-Type: application/vnd.git-lfs+json" \
  -d '{"operation":"download","transfers":["basic"],"objects":[{"oid":"e29fb801c1b65e77d78706cb82b0ef2f2def848ce7de3495d4d85195d203ca22","size":533768}]}' \
  | python3 -c "import sys,json; print(json.load(sys.stdin)['objects'][0]['actions']['download']['href'])" > url.txt
curl -sL -C - -o model.safetensors "$(cat url.txt)"
# 2) 文本文件(固定 commit)
ref=44aafb32fc6d71fcf12d0a73054d28e095fe3099
base=https://raw.gitcode.com/hf_mirrors/multimolecule/bpnet/raw/$ref
for f in config.json tokenizer_config.json vocab.txt; do curl -s -o "$f" "$base/$f"; done
# 3) 校验
sha256sum model.safetensors   # 必须等于 e29fb801...

6. 实际结果(真实运行,Ascend910_9362 / npu:0)

张量形状dtype设备
inputs_embeds[1, 184, 5]float32npu:0
profile_logits[1, 184, 8]float32npu:0
count_logits[1, 8]float32npu:0
track[1, 184, 8]float32npu:0

全部 36 个参数块与所有张量均位于 npu:0(脚本硬断言,任一不符即中止)。

count_logits   = [4.0504, 4.0274, 3.6107, 3.2673, 4.4339, 4.0642, 3.3845, 2.8075]
track.sum(1)   = [57.4228, 56.1152, 36.9937, 26.2402, 84.2621, 58.2180, 29.5041, 16.5682]
exp(count)     = [57.4228, 56.1152, 36.9937, 26.2402, 84.2621, 58.2180, 29.5041, 16.5682]
max |track.sum(1) - exp(count_logits)| = 7.629395e-06   (相对误差 1.939407e-07)

耗时(含 NPU 同步):

  • 前向(含 sync):0.2251 s
  • 结果 D2H 同步:0.0003 s
  • 模型加载 + 迁移到 NPU:1.14 s

real_result.json 即本次唯一真实结果。

7. 限制

  • 不含权重:本精简仓不携带 model.safetensors(533768 bytes),需按上文 权重获取 下载并校验后放入 weights/。
  • 序列规整:官方模型卡示例序列经中心裁剪/补齐规整为恰好 184 nt,脚本 assert len(seq)==184;p53 序列为示例输入,非生物学严格注释。
  • 数值一致性:track.sum(1) ≈ exp(count_logits) 最大绝对误差 7.6e-06 (float32 正常舍入),校验阈值 1e-3。
  • 设备限定:已适配并验证 npu:0(Ascend910_9362);未在 CPU 上做同等 前向对照(非本任务范围)。
  • 上游许可未逐条核实:Multimolecule 库本体为 AGPL-3.0;上游模型权重许可以 HF 模型卡为准(见第 8 节)。

8. 许可证

  • 上游代码库 MultiMolecule:GNU Affero General Public License v3(见其源码头注释)。
  • 上游模型权重(multimolecule/bpnet):许可以 HF 模型卡 与 GitCode 镜像页声明为准,使用前请自行核实。
  • 本仓 inference.py、requirements.txt、README.md、real_result.json 为本次适配自产, 与上游代码分离,可自由使用(建议同样以 AGPL-3.0 精神署名引用上游)。

9. 截图证据

本次真实运行的证据截图(命令原文 + 原始输出,均已在本机执行):

Agent 适配工作流:assets/agent_workflow.png

Agent workflow

NPU 设备调用:assets/npu_device_call.png

NPU device call

模型结果:assets/model_result.png

Model result