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

Pangolin - 昇腾 NPU 推理部署

1. 模型简介

模型名称: multimolecule/pangolin

模型链接: HuggingFace

模型描述: Pangolin 是一个用于 RNA 选择性剪接预测的深度学习模型。它使用空洞卷积(dilated convolution)对 RNA 序列进行编码,预测在不同组织中的剪接水平。

模型架构: PangolinModel - 基于空洞卷积的序列预测模型

参数规模: 2,336,868 (单成员)

输入规格:

  • 类型:RNA 序列 token IDs (整数)
  • Shape:(batch_size, sequence_length)
  • 编码:A=0, C=1, G=2, T=3, N/pad=4

输出规格:

  • 类型:浮点数预测值
  • Shape:(batch_size, 4)
  • 含义:4 个组织(heart, liver, brain, testis)的剪接水平预测

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0安全张量格式加载
sentencepiece>= 0.1.99序列处理
protobuf>= 3.20.0序列化
numpy< 2数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu safetensors sentencepiece protobuf "numpy<2"

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

# HuggingFace
huggingface-cli download multimolecule/pangolin --local-dir ./pangolin_weights

# AtomGit 镜像(HuggingFace 下载慢时使用)
huggingface-cli download \
  --repo-type model \
  https://ai.gitcode.com/hf_mirrors/multimolecule/pangolin \
  --local-dir ./pangolin_weights

3.3 运行推理

# CPU 推理
python3 inference.py --model-path ./pangolin_weights --device cpu

# NPU 推理
python3 inference.py --model-path ./pangolin_weights --device npu:0

# 自动检测设备
python3 inference.py --model-path ./pangolin_weights

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--devicestrauto设备:auto/cpu/npu:0/cuda:0
--max-seq-lenint10000最大序列长度

3.5 推理调用示例

# 基本调用(自动检测设备)
python3 inference.py --model-path ./pangolin_weights

# 指定 NPU 设备
python3 inference.py --model-path ./pangolin_weights --device npu:0

# 指定 CPU
python3 inference.py --model-path ./pangolin_weights --device cpu

# Python API 调用
from inference import PangolinModel
import torch

model = PangolinModel(config)
model.load_state_dict(torch.load("model.safetensors"))
model.eval()

# 输入 RNA 序列 (A=0, C=1, G=2, T=3, N=4)
input_ids = torch.tensor([[0, 1, 2, 3, 4, 0, 1, 2, 3]])  # shape: (1, 9)
with torch.no_grad():
    predictions = model(input_ids)
print(predictions)  # shape: (1, 4) - 4 个组织的剪接预测

4. 推理成功日志

4.1 NPU 推理日志

[设备] npu:0
[模型] Pangolin (multimolecule/pangolin)
[架构] PangolinModel
[任务] regression - RNA splicing prediction
[组织] ['heart', 'liver', 'brain', 'testis']
[序列] vocab=5, max_len=10000
[隐藏层] hidden_size=32, stages=4
[权重] 已加载 3024 个参数键
[参数量] 2,336,868

[推理测试]
  短序列(100bp): 长度=39bp, 耗时=505.0ms
  输入: (1, 39)
  输出: (1, 4)
    heart: 0.0737
    liver: 0.0559
    brain: 0.1342
    testis: -0.0306
  状态: SUCCESS

[状态] SUCCESS

4.2 CPU 推理日志

[设备] cpu
[推理测试]
  短序列(100bp): 长度=39bp, 耗时=22.8ms
  中序列(500bp): 长度=290bp, 耗时=293.4ms
  长序列(1000bp): 长度=580bp, 耗时=492.9ms
  状态: SUCCESS

5. 测试样例及输出结果

样例 1:短 RNA 序列

运行命令:

python3 inference.py --model-path ./pangolin_weights --device cpu

输出:

[推理测试]
  短序列(100bp): 长度=39bp, 耗时=22.8ms
  输入: (1, 39)
  输出: (1, 4)
    heart: 0.0350
    liver: 0.0891
    brain: -0.0100
    testis: -0.0905
  状态: SUCCESS

样例 2:长 RNA 序列

运行命令:

python3 inference.py --model-path ./pangolin_weights --device npu:0

输出:

[推理测试]
  长序列(1000bp): 长度=580bp, 耗时=80.7ms
  输入: (1, 580)
  输出: (1, 4)
    heart: 0.0791
    liver: 0.0619
    brain: 0.1257
    testis: -0.0295
  状态: SUCCESS

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 人工合成 RNA 序列

评测指标:

指标结果说明
输出维度(batch, 4)4 个组织类型
短序列(39bp) 延迟22.8ms (CPU) / 505ms (NPU)首次 NPU 调用含 warmup
中序列(290bp) 延迟293.4ms (CPU) / 16.3ms (NPU)NPU 并行加速
长序列(580bp) 延迟492.9ms (CPU) / 80.7ms (NPU)

评测命令:

python3 inference.py --model-path ./pangolin_weights --device npu:0

8. NPU 配置说明

  • NPU 卡数: 1 (单卡)
  • 设备: Ascend 910
  • 混合精度: 使用 torch.npu.amp.autocast
  • CANN 版本: 8.5.1

9. 已知问题

  1. NPU 首次推理较慢(~500ms):包含 NPU 运行时初始化和算子编译,后续推理正常(~16-80ms)
  2. 不使用 vLLM/sglang:Pangolin 是生物信息学模型,使用纯 PyTorch 推理
  3. 最大序列长度:10000bp(模型设计限制)