atlasleong/deepplant-gep-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

DeepPlant-GEP — Ascend NPU 真机推理适配

将固定版本模型 Addaoud/DeepPlant-GEP(revision 57d02f6e9bfee46c98c8a0cbc1b835095898a3b6) 完整适配到物理 Ascend NPU 设备 npu:0(Ascend910B4-1),完成真实的端到端推理。 本轮(2026-08-22 turn-2)固定直连物理 npu:0,不设置 ASCEND_RT_VISIBLE_DEVICES, 全程不进行任何 CPU 推理或回退。

  • 源模型:https://huggingface.co/Addaoud/DeepPlant-GEP
  • 源仓库类型:PyTorchModelHubMixin(仅权重 + config + model card,无自定义代码)
  • 本目录交付:inference.py、readme.md/README.md、requirements.txt、.gitignore、 run_acceptance.sh、原始/脱敏日志、logs/npu_result.json、logs/predictions.npz、 logs/npu_device_call.log、logs/modelagent_transcript.log、SHA256SUMS 及四张真实证据图片(assets/*.png)。
  • 许可证:MIT(与上游模型卡一致)。

适配证据截图

agent_workflow

modelagent_live_session

npu_device_call

model_result

模型简介

DeepPlant-GEP(Gene Expression Prediction)是一个基因表达预测模型:输入一条 one-hot 编码的 DNA 序列(4 条通道:A/C/G/T,长度 seq_len),经卷积主干 (conv backbone)→ Transformer 编码器(6 层)→ 注意力池化 → 全连接回归头, 输出 n_features = 2935 个连续基因表达值。

架构为按权重严格重建(strict=True,0 缺失 / 0 多余 key),关键超参来自 config.json:

超参值说明
taskGEP基因表达预测(回归)
embed_dim2048Transformer 隐藏维度
n_filters512卷积主干通道数
num_heads8注意力头数(head_dim=256)
encoder_num_layers6Transformer 编码器层数
dim_forwardfeed2048前馈网络维度
dropout0.2Dropout 概率
expand_factor2FC 头扩展倍数(隐藏 4096)
max_seq_length100最大序列长度
n_features2935输出基因表达维度
参数量248,667,511全量模型参数

NPU 原生实现说明:PyTorch 的 nn.TransformerEncoderLayer 在推理时会走融合算子 aten::_transformer_encoder_layer_fwd,该算子在 Ascend 后端不支持并会回退 CPU。 本适配将编码器层改用 Ascend 原生算子(linear / bmm / softmax / layer_norm) 等价重建(状态字典 key 完全一致,仍可严格加载),从而全模型在 NPU 上执行,无任何 CPU 回退。实测 forward_elapsed_s = 7.2358。


关键结论(真实验收)

  • 设备:npu:0(物理设备 0),Ascend910B4-1,torch_npu 2.9.0。
  • 直连方式:不设置 ASCEND_RT_VISIBLE_DEVICES,逻辑设备号等于物理设备号; requested_device=npu:0、physical_device=0。
  • 权重:model.safetensors(995,600,180 B)以 strict=True 加载,143/143 key 全匹配, SHA256 75456915b0b6d09b444b43ab1ef803a0b129a9a1938d9d92ccaae4d7592def90。
  • 放置断言:输入、模型参数、模型 buffer、注意力池化参数、输出全部位于 npu:0; 缺失/失效 NPU 时失败关闭(非零退出,绝不静默回退 CPU)。
  • 并发 PID 证明:inference.py 上报容器 pid=322092、宿主 host_pid=3673958, 物理 npu-smi 在持有期间捕获 NPU 0 进程 PID 3673958(1140 MB,与全量权重相符)—— PID CORRELATION: MATCH。
  • 同步计时:forward_elapsed_s = 7.235773,输出 (1, 2935),全为有限值。

环境依赖清单

本任务使用镜像自带的 CANN 匹配版本,禁止替换 torch / torch_npu:

组件版本来源
Python3.11.14运行镜像
torch2.9.0+cpu运行镜像(CANN 匹配,勿替换)
torch_npu2.9.0运行镜像(CANN 匹配,勿替换)
CANN8.5.1运行环境
芯片Ascend910B4-1 × 8物理 NPU(本次使用 npu:0)
numpy1.26.4运行镜像
safetensors0.6.2运行镜像
Pillow12.3.0Aliyun 镜像补充安装(仅用于生成证据 PNG)

非 torch 应用依赖通过 Aliyun pip 镜像安装:

pip install --index-url https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt

分步推理操作流程

前置条件:本地模型目录存在且完整 /work/pipeline/models/other40/metadata/deepplant-gep/(含 config.json、 model.safetensors、README.md)。

  1. 进入交付目录

    cd /work/pipeline/jobs/other40/deepplant-gep/deepplant-gep-npu
  2. 确认 NPU 设备可用(不设置 ASCEND_RT_VISIBLE_DEVICES)

    npu-smi info        # 应能看到 NPU 0..7,其中 npu:0 空闲
    python -c "import torch_npu, torch; print(torch.npu.is_available(), torch.npu.device_count())"
    # True 8
  3. (如需)通过 Aliyun 镜像安装额外依赖

    pip install --index-url https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt
  4. 运行真实 NPU 推理

    python inference.py --device npu:0 --batch 1 --seq-len 100 --seed 0 \
        --hold-after-load 15
    • --device npu:0:目标物理设备;缺失/失效时脚本失败关闭。
    • 脚本不设置 ASCEND_RT_VISIBLE_DEVICES,直接连接物理 npu:0; 若环境中已设置该变量,脚本会失败关闭。
    • --hold-after-load 15:模型加载后保持 NPU 分配 15 秒,便于外部 npu-smi 并发捕获进程 PID(默认 0,即不保持)。
    • 输出:logs/npu_result.json(结构化结果)、logs/predictions.npz ((1, 2935) 全量预测与输入种子)、logs/npu_device_call.log (运行时真实设备调用日志),并在终端打印关键信息。
  5. 一键验收(可选)

    bash run_acceptance.sh

    该脚本后台启动推理、在持有期间连续抓取 5 次物理 npu-smi、解析 host_pid(来自 /proc/sched_debug)与 NPU 0 进程 PID,打印 PID CORRELATION: MATCH,并把进程表证据追加到 logs/npu_device_call.log, 同时产出原始/脱敏日志。

  6. 查看结果

    cat logs/npu_result.json
    cat logs/npu_inference_sanitized.log
    cat logs/npu_device_call.log

完整测试用例

以下为 2026-08-22 在物理 npu:0 上执行的真实测试命令与输出。

用例 1:一键验收脚本(含并发 PID 捕获)

$ bash run_acceptance.sh
=== launching inference.py on npu:0 (hold-after-load=15s) ===
=== Python container PID = 322092 ===
[DeepPlant-GEP] pid=322092 host_pid=3673958 host=gz.03 start=2026-08-22T19:55:43+00:00
[DeepPlant-GEP] torch=2.9.0+cpu torch_npu=2.9.0
[DeepPlant-GEP] numpy=1.26.4 python=3.11.14
[DeepPlant-GEP] device=npu:0 name=Ascend910B4-1 count=8
[DeepPlant-GEP] config={"decoder_num_layers": 0, "dim_forwardfeed": 2048, "dropout": 0.2, "embed_dim": 2048, "encoder_num_layers": 6, "encoder_type": "attention", "expand_factor": 2, "max_seq_length": 100, "n_features": 2935, "n_filters": 512, "num_heads": 8, "task": "GEP", "use_pos_encoding": true}
[DeepPlant-GEP] model built, parameters=248667511
[DeepPlant-GEP] loaded checkpoint strict=True keys=143 size=995600180
[DeepPlant-GEP] model placement: params=npu:0 buffers=npu:0
[DeepPlant-GEP] NPU_PID=322092 holding 15.0s for npu-smi capture
[DeepPlant-GEP] input shape=(1, 4, 100) device=npu:0 dtype=torch.float32
[DeepPlant-GEP] output shape=(1, 2935) device=npu:0 dtype=torch.float32
[DeepPlant-GEP] output stats={"first_10": [-0.878240168, -0.478556484, -0.737186432, -0.888940215, -0.685564578, -0.833527267, -0.795461774, -0.863647461, -0.809061170, -0.443870455], "max": 1.39734375, "mean": -0.81550092, "min": -2.1797881, "std": 0.40524673}
[DeepPlant-GEP] forward elapsed(s)=7.2358
[DeepPlant-GEP] wrote logs/npu_result.json
[DeepPlant-GEP] total_wall(s)=26.9548 exit=0
=== host_pid(from inference.py) = 3673958 ===
=== npu_smi_pid(on NPU 0)       = 3673958 ===
=== PID CORRELATION: MATCH ===
=== inference.py exit code: 0 ===

用例 2:并发 npu-smi 物理设备进程表(持有期间)

| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 3673958       |                          | 1140                    |
| No running processes found in NPU 1                                                            |
| No running processes found in NPU 2                                                            |

说明:容器命名空间 PID 322092 对应的宿主命名空间 PID 3673958,与 NPU 0 上 npu-smi 报告的进程 PID 3673958 完全一致;进程内存 1140 MB 与全量权重 (995,600,180 B ≈ 950 MB 参数 + 运行时开销)相符。

用例 3:直接运行

python inference.py --device npu:0 --batch 1 --seq-len 100 --seed 0

预期退出码 0,logs/npu_result.json 中 exit_code=0、forward_elapsed_s 约 7.24。


输出结果

logs/npu_result.json(节选,完整字段见文件):

字段值
model_idAddaoud/DeepPlant-GEP
source_revision57d02f6e9bfee46c98c8a0cbc1b835095898a3b6
weight_sha25675456915b0b6d09b444b43ab1ef803a0b129a9a1938d9d92ccaae4d7592def90
weight_key_count / parameter_count143 / 248667511
engine / npu_availabletorch_npu / true
requested_device / physical_devicenpu:0 / 0
device / device_namenpu:0 / Ascend910B4-1
input_shape / output_shape[1, 4, 100] / [1, 2935]
input_device / model_device / output_devicenpu:0 / npu:0 / npu:0
forward_elapsed_s7.235773
output_statsmean=-0.81550092, std=0.40524673, min=-2.1797881, max=1.39734375
host_pid / pid3673958 / 322092

logs/predictions.npz:predictions (1, 2935) float32(前 10 个值: [-0.87824017, -0.47855648, -0.73718643, -0.88894022, -0.68556458, -0.83352727, -0.79546177, -0.86364746, -0.80906117, -0.44387046]), 输入种子 input_seed=0、input_shape=[1,4,100]。


证据图片

图片内容
assets/agent_workflow.png适配工作流(读上下文 → 权重清单 → 真机推理 → 并发 PID → 产物校验)
assets/modelagent_live_session.png本轮真实脱敏 Agent 会话输出(run_acceptance.sh,PID 匹配)
assets/npu_device_call.png物理 npu:0 设备调用日志与 npu-smi 并发进程表证据
assets/model_result.png推理结果(输出统计、npu_result.json 摘要)

文件清单

文件说明
inference.py完整 NPU 推理脚本(架构重建 + 严格加载 + 放置断言 + 同步计时)
readme.md / README.md本说明(两文件字节一致)
requirements.txt非 torch 依赖(Aliyun 镜像安装)
.gitignore忽略字节码/临时产物
run_acceptance.sh一键真实验收(并发 npu-smi PID 捕获与匹配)
scripts/validate_metadata.py本地元数据/证据校验(上游事实、README、结果 JSON 交叉核对)
logs/npu_inference.log原始运行日志
logs/npu_inference_sanitized.log脱敏日志(滤除 torch_npu 噪声)
logs/npu_device_call.log真实设备调用日志(运行时由 inference.py 追加写入)
logs/modelagent_transcript.log本轮真实脱敏 Agent 会话文本
logs/npu_smi_before.log / npu_smi_capture_{1..5}.log运行前 / 持有期间 5 次物理 npu-smi 快照
logs/npu_smi_during.log / npu_smi_after.log持有期间 / 结束后快照
logs/npu_result.json结构化推理结果
logs/predictions.npz全量预测((1, 2935))
SHA256SUMS交付物 SHA256 校验清单
assets/*.png四张真实证据图片

限制

  • 输入为合成 one-hot DNA(--seed 固定,可复现),用于证明完整、真实的 NPU 推理链路;不附带任何外部/版权数据。
  • 模型权重位于本机 /work/pipeline/models/other40/metadata/deepplant-gep/,运行前必须存在。
  • torch / torch_npu 为镜像自带 CANN 匹配版本,请勿重装或升级。
  • 本交付不包含权重文件、私有 SDK JSONL、__pycache__/*.pyc、fusion_result.json 等禁止产物。

许可证

本适配遵循上游模型卡声明的 MIT 许可。模型权重版权归原作者所有,仅作本地固定版本推理。

适配交付补充

环境依赖:torch、torch_npu 与 CANN,详见 requirements.txt。

推理步骤:执行 inference.py 并确认目标设备为 Ascend NPU。

完整测试用例:使用仓库固定测试输入执行一次端到端推理。

输出结果:真实 NPU 结果保存在 logs/npu_result.json。