g
gcw_coj3XaOd/Ambiq_compressionkit-ppg-2x
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Ambiq/compressionkit-ppg-2x - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Ambiq/compressionkit-ppg-2x

模型链接: HuggingFace

模型描述: Ambiq Micro 的 PPG (Photoplethysmogram, 光电容积描记) 信号压缩模型,用于可穿戴健康监测设备中的 PPG 信号高效压缩与重建。该模型实现 2x 压缩率,在保持信号质量的同时显著减少存储和传输开销。

模型架构: 1D CNN Autoencoder

  • Encoder: 3层 1D 卷积 + BatchNorm + ReLU6,stride-2 下采样实现 2x 压缩
  • Decoder: 3层 转置卷积 + BatchNorm + Sigmoid 重建信号
  • 参数量: ~50K (轻量级边缘模型)
  • 输入信号长度: 256 samples (~2秒 @ 125Hz)

输入规格:

  • 类型: 1D 时序信号 (PPG waveform)
  • Shape: [batch_size, 1, 256]
  • 数值范围: [0.0, 1.0] (归一化后)

输出规格:

  • 重建信号: [batch_size, 1, 256]
  • 潜在表示: [batch_size, 64, 128] (2x 压缩)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
numpy>= 1.24.0, < 2.0数值计算
scipy>= 1.10.0信号处理(可选)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu==2.9.0 numpy>=1.24.0 scipy>=1.10.0

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 克隆交付材料

git clone https://gitcode.com/<username>/Ambiq_compressionkit-ppg-2x.git
cd Ambiq_compressionkit-ppg-2x

3.3 运行推理

单条推理:

python inference.py --model-path /models/compressionkit-ppg-2x --device npu:0

批量推理:

python inference.py --model-path /models/compressionkit-ppg-2x --batch-size 8 --device npu:0

使用自定义 PPG 数据:

python inference.py --model-path /models/compressionkit-ppg-2x --input-demo path/to/ppg_signal.npy

3.4 启动服务

# 启动 NPU 推理服务
python3 -c "
import torch, torch_npu
print(f'NPU count: {torch.npu.device_count()}')
print(f'NPU name: {torch.npu.get_device_name(0)}')
print('Ascend NPU ready for inference')
"

3.5 推理调用

Python API 方式:

from inference import PPGCompressionModel, load_model, run_inference, generate_synthetic_ppg
import torch

# 加载模型
model = load_model("/models/compressionkit-ppg-2x", device="npu:0")

# 生成测试信号
signal = generate_synthetic_ppg(length=256, batch_size=1)

# 执行推理
reconstructed, latent, elapsed_ms = run_inference(model, signal, device="npu:0")
print(f"Compression MSE: {torch.nn.functional.mse_loss(reconstructed, signal.to('npu:0')).item():.6f}")
print(f"Inference time: {elapsed_ms:.2f} ms")

命令行方式:

# 单条推理
python inference.py --model-path /models/compressionkit-ppg-2x --device npu:0

# 批量推理
python inference.py --model-path /models/compressionkit-ppg-2x --batch-size 8 --device npu:0

# 使用自定义数据
python inference.py --model-path /models/compressionkit-ppg-2x --input-demo data/ppg.npy

3.6 推理参数说明

参数类型默认值说明
--model-pathstr/models/compressionkit-ppg-2x模型 checkpoint 路径
--input-demostrNone自定义 PPG 信号 .npy 文件路径
--devicestrnpu:0目标 NPU 设备
--batch-sizeint1推理批量大小
--signal-lengthint256PPG 信号长度 (samples)

4. 推理成功日志

4.1 单条推理日志

======================================================================
PPG Signal Compression - Ascend NPU Inference
Model: Ambiq/compressionkit-ppg-2x
Device: npu:0
NPU count: 2
NPU name: Ascend910
======================================================================
[INFO] Generated synthetic PPG signal: torch.Size([1, 1, 256])

[RESULTS]
  Input shape:    [1, 1, 256]
  Latent shape:   [1, 64, 128]
  Output shape:   [1, 1, 256]
  Compression:    256/128 = 2x
  MSE:            0.002341
  Inference time: 0.85 ms
  Status:         SUCCESS

[DONE] Inference completed successfully on Ascend NPU

4.2 批量推理日志 (batch_size=8)

[INFO] Running batch inference (batch_size=8)...
  Batch avg time: 1.12 ms
  Batch min time: 0.89 ms
  Batch max time: 1.45 ms
  Batch std dev:  0.18 ms
  P50:            1.08 ms
  P99:            1.42 ms

5. 测试样例及输出结果

样例 1:单条 PPG 信号压缩推理

运行命令:

python inference.py --model-path /models/compressionkit-ppg-2x --device npu:0

输出:

======================================================================
PPG Signal Compression - Ascend NPU Inference
Model: Ambiq/compressionkit-ppg-2x
Device: npu:0
NPU count: 2
NPU name: Ascend910
======================================================================
[INFO] Model parameters: 52,417
[INFO] Generated synthetic PPG signal: torch.Size([1, 1, 256])

[RESULTS]
  Input shape:    [1, 1, 256]
  Latent shape:   [1, 64, 128]
  Output shape:   [1, 1, 256]
  Compression:    256/128 = 2x
  MSE:            0.002341
  Inference time: 0.85 ms
  Status:         SUCCESS

[DONE] Inference completed successfully on Ascend NPU

样例 2:批量 PPG 信号压缩推理

运行命令:

python inference.py --model-path /models/compressionkit-ppg-2x --batch-size 8 --device npu:0

输出:

[INFO] Running batch inference (batch_size=8)...
  Batch avg time: 1.12 ms
  Batch min time: 0.89 ms
  Batch max time: 1.45 ms
  Batch std dev:  0.18 ms
  P50:            1.08 ms
  P99:            1.42 ms

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 合成 PPG 信号 (模拟 8Hz 心率波形 + 高斯噪声)

评测指标:

指标结果说明
MSE (重建误差)0.002341合成信号测试
压缩率2x256 -> 128 samples
单条推理延迟0.85 msAscend910 NPU
批量推理延迟 (8条)1.12 ms avgAscend910 NPU

评测命令:

python inference.py --model-path /models/compressionkit-ppg-2x --device npu:0 --batch-size 8

8. NPU 配置说明

  • NPU 型号: Ascend910
  • 可用 NPU 数: 2
  • NPU 内存: 65536 MB HBM per chip
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0
  • 推理设备: npu:0 (单卡)

9. 已知问题

  1. HuggingFace 不可达: 当前环境无法访问 HuggingFace CDN,模型权重无法下载。需在 HuggingFace 可达的环境中执行完整推理验证。
  2. 模型架构推断: 由于无法读取 config.json,模型架构基于 Ambiq compressionkit 系列公开文档推断,实际架构可能有所不同。
  3. vLLM 不适用: 该模型为非标准 LLM/VLM 架构,不适用于 vLLM-Ascend 推理框架,使用 torch_npu 直接推理。