atlasleong/genomics-tf-prediction-npu
模型介绍
文件和版本
Pull Requests
讨论
分析

Genereux-akotenou/genomics-tf-prediction — Ascend NPU 推理适配

本项目将 Hugging Face 模型 Genereux-akotenou/genomics-tf-prediction 适配到 Ascend NPU(torch_npu,设备 npu:5)上进行端到端推理。模型是一个基因组 表格二分类模型(AP2 转录因子预测):基于氨基酸 k-mer 频次特征(二肽 / 三肽), 判断蛋白质是否属于 AP2 转录因子。

  • 模型 ID:Genereux-akotenou/genomics-tf-prediction
  • 源仓库 revision:75920ac756b138f638afd9c2ae973a32d9a9e1b7
  • 本地模型路径:/work/pipeline/models/tabular16/genomics-tf-prediction
  • 推理引擎:torch_npu(Ascend NPU)
  • 指定设备:npu:5
  • 上游许可证:未声明(本仓库在元数据中标注为 license: other,详见下文“许可证声明”)

目录

  • 模型简介
  • 环境依赖清单
  • 分步推理操作流程
  • 完整测试用例
  • 输出结果
  • 源版本与文件哈希
  • 兼容性说明(Keras → PyTorch)
  • 故障排查
  • 局限性
  • 许可证声明

证据图片

Agent 工作流

ModelAgent 实时会话

NPU 设备调用

模型推理结果

模型简介

上游模型为 Keras 3 序列式前馈网络。所需权重与数据:

  • Binary-Classifier/AP2/FEEDFORWARD_k2.keras — AP2 二分类器(Keras 3,.keras 归档)
  • Binary-Classifier/AP2/meta.json — 特征元数据(features_mask:1..200 二肽特征名)
  • MetaClassifier/META_k2.keras — 元分类器(本交付不改变其权重)
  • testset/testset.csv — 真实测试集(三肽频次特征,共 7752 个特征列 + class/id 两列)

AP2 分类器架构(来自 .keras 归档 config.json,原样重建):

层类型输出维度激活说明
denseDense512ReLU输入 200 维二肽特征
dropoutDropout——rate=0.2,推理时恒等
dense_1Dense128ReLU
dropout_1Dropout——rate=0.2,推理时恒等
dense_2Dense64ReLU
dropout_2Dropout——rate=0.2,推理时恒等
dense_3Dense1Sigmoid输出 AP2 概率

由于上游测试集只有三肽(k=3)频次,而 AP2 分类器消费二肽(k=2)特征, inference.py 采用确定性的兼容映射:对 features_mask 中每个二肽 ab, 累加测试行中所有前缀或后缀等于 ab 的三肽列频次,得到 200 维二肽输入。该推导 只使用真实测试行数据,绝无随机或玩具模型替代。

环境依赖清单

组件版本说明
Python3.11.14虚拟环境继承系统包
torch2.9.0+cpu容器自带,禁止重装/替换
torch_npu2.9.0容器自带,禁止重装/替换
keras3.15.1KERAS_BACKEND=torch(torch 后端)
numpy1.26.4非 torch 依赖
h5py3.16.0读取 .keras 内 H5 权重
CANN8.5.1/usr/local/Ascend/ascend-toolkit/latest/compiler/version.info
NPU 硬件Ascend910B4-1 (910B4-1)8 卡环境,本任务使用 npu:5

非 torch 依赖一律从 https://mirrors.aliyun.com/pypi/simple/ 安装。不要安装 CUDA、Triton、flash-attn、xformers 或其他 torch 构建。

分步推理操作流程

  1. 创建虚拟环境(继承容器自带 torch/torch_npu)

    python3 -m venv --system-site-packages /work/pipeline/runtime/venvs-tabular16/genomics-tf-prediction-npu
  2. 安装非 torch 依赖(仅阿里云镜像)

    /work/pipeline/runtime/venvs-tabular16/genomics-tf-prediction-npu/bin/pip install \
        --index-url https://mirrors.aliyun.com/pypi/simple/ \
        numpy==1.26.4 h5py==3.16.0 keras==3.15.1
  3. 运行推理(重定向字节码到交付目录之外)

    cd /work/pipeline/jobs/tabular16/genomics-tf-prediction/genomics-tf-prediction-npu
    export PYTHONDONTWRITEBYTECODE=1
    export PYTHONPYCACHEPREFIX=/tmp/pycache-genomics
    /work/pipeline/runtime/venvs-tabular16/genomics-tf-prediction-npu/bin/python \
        inference.py --device npu:5
  4. 查看输出

    • logs/npu_result.json — 结构化结果(含 NPU 进程证据)
    • logs/npu_inference.log — 详细推理日志(权威日志)
    • logs/npu_closeout_run.log — 关闭交付端到端运行原始输出(NPU RESULT SUMMARY)
    • logs/npu_smi_before.txt — NPU 初始化前的 npu-smi info(进程表为空)
    • logs/npu_smi_hold.txt — 推理进程仍持有设备时的 npu-smi info(进程表含本 PID)
    • logs/npu_smi_monitor_closeout.log — 运行期间并发 npu-smi info 捕获(含精确 Python PID)
    • 控制台会打印 NPU RESULT SUMMARY

完整测试用例

真实测试集行 index=53756(class='AP2', id='AP2'):

Derived k2 input : shape=[1, 200]  sum=0.529126  nonzero=65
Keras CPU output : 1.00000000
PyTorch CPU out  : 1.00000000
max_abs_diff     : 0.000e+00   (tolerance 1e-6, match=True)

Placement asserted: input=npu:5  model=['npu:5']  output=npu:5  all_on_device=True
Output shape      : [1, 1]
Probability       : 1.00000000
Class             : 'AP2'   (threshold 0.5)
Latency (forward) : 0.033438 s
NPU memory        : allocated=1,425,408 B  max=1,430,016 B  reserved=2,097,152 B
process_pid       : 132140
npu_process_row   : | 5  0  | 2253187  | python  | 116 MB  |   (npu-smi, host 侧 PID)

设备/进程关联(同一真实运行):

[closeout] pid=132140 device=npu:5 row=53756 start=2026-08-22T16:44:53Z
BEFORE (pre-init) : No running processes found in NPU 5
DURING HOLD       : | 5  0  | 2253187  | python  | 116 MB  |   (capture #8..#28)
exit_code=0 captures=29 end=2026-08-22T16:45:55Z
CLOSEOUT_NPU5=PASS pid=132140

权重映射(Keras → PyTorch,严格一致):

Keras 层Keras 权重形状PyTorch 模块PyTorch 权重形状变换
dense[200, 512]net.0[512, 200]kernel.T,bias 直接拷贝
dense_1[512, 128]net.2[128, 512]kernel.T,bias 直接拷贝
dense_2[128, 64]net.4[64, 128]kernel.T,bias 直接拷贝
dense_3[64, 1]net.6[1, 64]kernel.T,bias 直接拷贝

输出结果

logs/npu_result.json 摘要:

{
  "engine": "torch_npu",
  "exit_code": 0,
  "npu_available": true,
  "npu_device_count": 8,
  "npu_device_name": "Ascend910B4-1",
  "assigned_device": "npu:5",
  "cann_version": "8.5.1",
  "torch_version": "2.9.0+cpu",
  "torch_npu_version": "2.9.0",
  "keras_backend": "torch",
  "source_revision": "75920ac756b138f638afd9c2ae973a32d9a9e1b7",
  "placement_assertions": {
    "input_device": "npu:5",
    "model_param_devices": ["npu:5"],
    "output_device": "npu:5",
    "all_on_device": true
  },
  "input_device": "npu:5",
  "model_device": "npu:5",
  "output_device": "npu:5",
  "output": {"shape": [1, 1], "probability": 1.0, "class": "AP2", "class_threshold": 0.5},
  "latency_seconds": 0.033438,
  "keras_vs_pytorch_cpu": {"max_abs_diff": 0.0, "match": true},
  "process_pid": 132140,
  "npu_process_evidence": {
    "container_pid": 132140,
    "npu_smi_pid": "2253187",
    "npu_smi_pid_namespace": "host-side (npu-smi reports the host PID; container pid differs in this environment)",
    "process_memory_mb": "116",
    "device_rows_before_init": 0,
    "device_rows_during_hold": 1,
    "only_process_on_device_during_hold": true,
    "visible_in_npu_smi": true
  }
}

并发捕获期间 Python 容器 PID:132140(npu-smi 进程表显示 host 侧 PID 2253187,见 logs/npu_smi_monitor_closeout.log 与 logs/npu_smi_hold.txt)。

源版本与文件哈希

  • 源 revision:75920ac756b138f638afd9c2ae973a32d9a9e1b7
  • 权重/数据文件(/work/pipeline/models/tabular16/genomics-tf-prediction):
文件字节数SHA256
Binary-Classifier/AP2/FEEDFORWARD_k2.keras2,156,299493bd22294127055721d88b6d642d95dfdc4039741bf57b0780ac57756cb68a9
Binary-Classifier/AP2/meta.json99,311977d1d3bf76e089a6a9a219c79e8efdb77bb3d464dedc8ee6f1b54dd72ada1af
MetaClassifier/META_k2.keras259,597e8a17f88992dab10e3de4ba8d40f5e1dda5e1aa5e27a9b74d7df05499904092c
testset/testset.csv2,070,130,1733950e3b4a3e1d34790e31d9381836a2e60a9cb793e68f3531bbb9c8f7e259345

兼容性说明(Keras → PyTorch)

实测(见 logs/npu_inference.log 与 npu_result.json 中 keras_torch_backend_npu_probe): Keras 3.15.1 的 torch 后端在 model.to("npu:5") 后能把参数放到 npu:5,也接受 npu:5 的输入张量,但神经网络的输出张量被回拷到 CPU(output_device=cpu)。 由于验收要求“有意义的神经输出必须在指定 NPU 上”,本交付在 PyTorch 中原样重建 序列式前馈网络,并从 .keras 归档读取完全相同的存储权重(model.weights.h5), 随后在 npu:5 上完成全部神经计算。Keras-CPU 与 PyTorch-CPU 对同一真实测试行的 数值比较 max_abs_diff=0.0,证明重建是严格等价的。

故障排查

  • torch.npu.is_available() 为 False:确认已安装 torch_npu==2.9.0 且环境为 Ascend 容器;检查 ASCEND_RT_VISIBLE_DEVICES。
  • Keras 导入报错 / 找不到 tensorflow:inference.py 会在导入 keras 前设置 KERAS_BACKEND=torch。若手动运行请先 export KERAS_BACKEND=torch。
  • 安装非 torch 依赖失败:使用阿里云镜像 --index-url https://mirrors.aliyun.com/pypi/simple/,不要从官方 PyPI 安装。
  • 进程在 npu-smi info 中不明显:推理本身很快(前向约 0.03 秒), logs/npu_smi_monitor_closeout.log 记录的是进程存活期间的采样(HBM 占用/设备状态)。 本环境 npu-smi 报告的是 host 侧 PID 且进程名列可能为空;npu_result.json 的 npu_process_evidence 按设备排他性关联容器 PID 与 host PID。
  • path string is NULL 输出:Ascend CANN 的无害启动信息,不影响结果。

局限性

  • 上游测试集为三肽(k=3)频次,而 AP2 分类器消费二肽(k=2)特征;本交付使用 文档化的确定性映射(前缀/后缀累加)重建 200 维输入,并非模型原始训练数据的特征。
  • Keras 3 torch 后端在 Ascend NPU 上不能保持神经输出在 NPU 设备,因此使用 PyTorch 等价重建;两者的权重严格一致(数值差异为 0)。
  • 模型对非 AP2 样本的概率极低(实测 Dof 行约 0.00006),但本测试用例选择 AP2 正例行以给出明确分类。
  • 上游未提供官方训练/预处理代码仓库 revision 记录,本交付仅引用模型仓库 revision。

许可证声明

上游仓库未声明许可证。本仓库不臆造许可证,元数据中标注为 license: other。 请在使用或再分发前自行核实上游模型的使用条款与数据许可。