本项目将 Hugging Face 模型 Genereux-akotenou/genomics-tf-prediction 适配到
Ascend NPU(torch_npu,设备 npu:5)上进行端到端推理。模型是一个基因组
表格二分类模型(AP2 转录因子预测):基于氨基酸 k-mer 频次特征(二肽 / 三肽),
判断蛋白质是否属于 AP2 转录因子。
Genereux-akotenou/genomics-tf-prediction75920ac756b138f638afd9c2ae973a32d9a9e1b7/work/pipeline/models/tabular16/genomics-tf-predictiontorch_npu(Ascend NPU)npu:5license: other,详见下文“许可证声明”)



上游模型为 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,原样重建):
| 层 | 类型 | 输出维度 | 激活 | 说明 |
|---|---|---|---|---|
| dense | Dense | 512 | ReLU | 输入 200 维二肽特征 |
| dropout | Dropout | — | — | rate=0.2,推理时恒等 |
| dense_1 | Dense | 128 | ReLU | |
| dropout_1 | Dropout | — | — | rate=0.2,推理时恒等 |
| dense_2 | Dense | 64 | ReLU | |
| dropout_2 | Dropout | — | — | rate=0.2,推理时恒等 |
| dense_3 | Dense | 1 | Sigmoid | 输出 AP2 概率 |
由于上游测试集只有三肽(k=3)频次,而 AP2 分类器消费二肽(k=2)特征,
inference.py 采用确定性的兼容映射:对 features_mask 中每个二肽 ab,
累加测试行中所有前缀或后缀等于 ab 的三肽列频次,得到 200 维二肽输入。该推导
只使用真实测试行数据,绝无随机或玩具模型替代。
| 组件 | 版本 | 说明 |
|---|---|---|
| Python | 3.11.14 | 虚拟环境继承系统包 |
| torch | 2.9.0+cpu | 容器自带,禁止重装/替换 |
| torch_npu | 2.9.0 | 容器自带,禁止重装/替换 |
| keras | 3.15.1 | KERAS_BACKEND=torch(torch 后端) |
| numpy | 1.26.4 | 非 torch 依赖 |
| h5py | 3.16.0 | 读取 .keras 内 H5 权重 |
| CANN | 8.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 构建。
创建虚拟环境(继承容器自带 torch/torch_npu)
python3 -m venv --system-site-packages /work/pipeline/runtime/venvs-tabular16/genomics-tf-prediction-npu安装非 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运行推理(重定向字节码到交付目录之外)
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查看输出
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)。
75920ac756b138f638afd9c2ae973a32d9a9e1b7/work/pipeline/models/tabular16/genomics-tf-prediction):| 文件 | 字节数 | SHA256 |
|---|---|---|
Binary-Classifier/AP2/FEEDFORWARD_k2.keras | 2,156,299 | 493bd22294127055721d88b6d642d95dfdc4039741bf57b0780ac57756cb68a9 |
Binary-Classifier/AP2/meta.json | 99,311 | 977d1d3bf76e089a6a9a219c79e8efdb77bb3d464dedc8ee6f1b54dd72ada1af |
MetaClassifier/META_k2.keras | 259,597 | e8a17f88992dab10e3de4ba8d40f5e1dda5e1aa5e27a9b74d7df05499904092c |
testset/testset.csv | 2,070,130,173 | 3950e3b4a3e1d34790e31d9381836a2e60a9cb793e68f3531bbb9c8f7e259345 |
实测(见 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。inference.py 会在导入 keras 前设置
KERAS_BACKEND=torch。若手动运行请先 export KERAS_BACKEND=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 的无害启动信息,不影响结果。上游仓库未声明许可证。本仓库不臆造许可证,元数据中标注为 license: other。
请在使用或再分发前自行核实上游模型的使用条款与数据许可。