模型名称: 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)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| xlstm | == 2.0.4 | xLSTM 架构库 |
| 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# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一: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方式一:直接运行 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]| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 目录路径 |
| --input-path | str | None | 输入 ECG 信号 .npy 文件路径 |
| --batch | flag | False | 启用批量推理模式 |
| --device | str | "npu" | 推理设备:npu 或 cpu |
| --test | flag | False | 运行内置测试用例 |
| --seq-len | int | 200 | 测试信号序列长度 |
[模型] 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次推理统计:
输入: [4, 200, 12]
输出: cls=[4, 1024], out=[4, 8, 1024]
平均: 20.33 ms/样本
状态] SUCCESS运行命令:
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运行命令:
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运行命令:
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


测试数据: 合成 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 --testxLSTM backend 限制: xLSTM 库 (v2.0.4) 不原生支持 NPU,需要使用 vanilla backend。sLSTM 的 _recurrent_kernel_ 张量需要从 [num_heads, hidden_dim, embed_dim] permute 为 [num_heads, embed_dim, hidden_dim] 才能正确加载。
首次推理较慢: 首次推理包含模型编译开销,约 500ms。预热后降至 ~26ms(seq_len=200)。
输入长度限制: seq_len 必须为 patch_size (25) 的倍数,自动补齐零填充。