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

xECG_base_model_v1 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: riccardolunelli/xECG_base_model_v1

模型链接: HuggingFace

模型描述: xECG 是一个基于 xLSTM 架构的 ECG(心电图)信号分类基座模型。它使用 sLSTM 和 mLSTM 混合块堆栈来处理 12 导联 ECG 信号,输出可用于下游分类任务的嵌入向量。

模型架构: LinearPatchEmbedding + xLSTMBlockStack (5× sLSTM + 4× mLSTM) + Average Pooling

参数规模: 57,021,472 (57M)

输入规格:

  • 数据类型:float32
  • Shape:[batch_size, seq_len, 12 channels]
  • seq_len 必须为 patch_size (25) 的倍数
  • 12 通道对应标准 ECG 导联 (I, II, III, aVR, aVL, aVF, V1-V6)

输出规格:

  • cls embedding:Shape [batch_size, 1024] — 用于分类的全局嵌入
  • out sequence:Shape [batch_size, seq_len/patch_size, 1024] — 序列级别输出

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
xlstm== 2.0.4xLSTM 架构库
safetensors>= 0.4.0安全权重加载
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

NPU 硬件: Ascend910_9362(已验证)

安装命令:

pip install torch-npu xlstm==2.0.4 safetensors numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

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 riccardolunelli/xECG_base_model_v1 --local-dir ./xecg_model

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

https://ai.gitcode.com/hf_mirrors/riccardolunelli/xECG_base_model_v1

3.3 运行推理

方式一:直接运行 inference.py

# 使用 NPU 推理
python inference.py --model-path ./xecg_model --test

# 使用自定义输入
python inference.py --model-path ./xecg_model --input-path my_ecg.npy

# 批量推理
python inference.py --model-path ./xecg_model --input-path batch_ecg.npy --batch

# CPU 模式
python inference.py --model-path ./xecg_model --test --device cpu

方式二:Python API

import torch
import numpy as np
from inference import load_model, preprocess_ecg, inference_single

# 加载模型
device = torch.device("npu")
model, config = load_model("./xecg_model", device)

# 准备输入 (200 个时间点, 12 导联)
signal = np.random.randn(200, 12).astype(np.float32)

# 推理
cls_embedding, seq_output = inference_single(model, signal, config, device)
print(f"分类嵌入: {cls_embedding.shape}")  # [1, 1024]

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 目录路径
--input-pathstrNone输入 ECG 信号 .npy 文件路径
--batchflagFalse启用批量推理模式
--devicestr"npu"推理设备:npu 或 cpu
--testflagFalse运行内置测试用例
--seq-lenint200测试信号序列长度

4. 推理成功日志

4.1 单条推理日志

[模型] riccardolunelli/xECG_base_model_v1
[设备] Ascend910_9362 (npu:0)
[输入] ECG信号 [1, 200, 12] float32
[输出] cls=[1, 1024], out=[1, 8, 1024]
[耗时] 506.70 ms (首次), 25.93 ms ( warmed up)
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 4次推理统计:
  输入: [4, 200, 12]
  输出: cls=[4, 1024], out=[4, 8, 1024]
  平均: 20.33 ms/样本
  状态] SUCCESS

5. 测试样例及输出结果

样例 1:单条 ECG 推理

运行命令:

python inference.py --model-path ./xecg_model --test

输出:

[模型加载] 权重加载完成: <All keys matched successfully>
[模型加载] 参数量: 57,021,472
[推理] 输入 shape: torch.Size([1, 200, 12])
[推理] cls embedding shape: torch.Size([1, 1024])
[推理] out sequence shape: torch.Size([1, 8, 1024])
[推理] 耗时: 506.70 ms
[状态] SUCCESS

样例 2:批量推理

运行命令:

python inference.py --model-path ./xecg_model --input-path ecg_batch.npy --batch

输出:

[批量推理] 输入 shape: torch.Size([4, 200, 12])
[批量推理] cls embeddings shape: torch.Size([4, 1024])
[批量推理] 平均耗时: 20.33 ms/样本
[状态] SUCCESS

样例 3:不同序列长度

运行命令:

python inference.py --model-path ./xecg_model --test

输出:

长度100: cls_norm=73.2931 [状态] SUCCESS
长度250: cls_norm=74.7849 [状态] SUCCESS
长度500: cls_norm=73.7060 [状态] SUCCESS
长度1000: cls_norm=71.4254 [状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 合成 ECG 信号(模拟 12 导联心电图波形)

评测指标:

指标结果说明
推理延迟 (单条, seq=200)~507 ms首次推理(含模型加载)
推理延迟 (单条, warmed)~26 ms预热后单条推理
批量吞吐 (batch=4)~20 ms/样本NPU 批量推理
嵌入向量 L2 范数~73-74不同长度输入的稳定性
CPU/NPU 输出一致性PASS相同输入输出一致

评测命令:

python inference.py --model-path ./xecg_model --test

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 设备数: 2
  • 推理设备: npu:0
  • CANN 版本: 8.5.1
  • xLSTM 后端: vanilla(CPU-compatible,在 NPU 上通过 torch_npu 自动转换)
  • 显存占用: 模型 57M 参数 ≈ 228MB (fp32)

9. 已知问题

  1. xLSTM backend 限制: xLSTM 库 (v2.0.4) 不原生支持 NPU,需要使用 vanilla backend。sLSTM 的 _recurrent_kernel_ 张量需要从 [num_heads, hidden_dim, embed_dim] permute 为 [num_heads, embed_dim, hidden_dim] 才能正确加载。

  2. 首次推理较慢: 首次推理包含模型编译开销,约 500ms。预热后降至 ~26ms(seq_len=200)。

  3. 输入长度限制: seq_len 必须为 patch_size (25) 的倍数,自动补齐零填充。