liuhongwei-2026/compressionkit-ppg-2x
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Ambiq CompressionKit PPG 2x 在昇腾 NPU 上的适配与部署

模型来源: Ambiq/compressionkit-ppg-2x-v1.0 镜像拉取: hf-mirror.com/Ambiq/compressionkit-ppg-2x-v1.0 或 ModelScope 同名搜索

昇腾 NPU 服务化 / 本地推理适配笔记, 与 inference.py 配套使用。


1. 简介

CompressionKit PPG 2倍 是 Ambiq 推出的残差向量量化 (RVQ) 信号压缩编解码器, 专门面向可穿戴 / 生命体征设备上的 PPG(光电容积脉搏血氧) 信号。它用极精简的编码器把 5 秒(320 个采样 @ 64Hz)PPG 帧压缩为 4 级 × 160 个 RVQ 索引(每索引 8 bit), 再经解码器恢复信号——体积小、功耗低、适合端侧量耗与云端比对。

组件内容作用
encoder_float32.tflite神经编码器 (1,1,320,1)→(1,1,160,16)把归一化信号编码成残差量化特征
codebook.npzRVQ 码本 (4级×256×16)对特征做残差向量量化
decoder_float32.tflite神经解码器 (1,1,160,16)→(1,1,320,1)还原 PPG 波形
  • 压缩比: 2x, 采样率 64 Hz, 每帧 = 5 s
  • 参数量: 极小(编码器 ~6 KB tflite, 解码器 ~38 KB)
  • 适配状态: 成功 ?? 适配设备: 昇腾 910(本机 Ascend910, CANN 8.5.1)

重要: 该模型不是大语言模型,不适用 vLLM / vLLM-Ascend。本仓库的昇腾适配点在于 用 torch + torch_npu 把整条 归一化 → RVQ 编码 → 残差量化 → 反量化解码 → 反归一化 管线跑在昇腾 NPU 上(服务化推理); 神经编码/解码器本身为 TFLite, 通过 LiteRT 参考运行时驱动。


2. 验证环境

组件版本
CPU/OSLinux x86_64
NPUAscendRe 910Bseries(2 卡), torch.npu.is_available()=True
CANN8.5.1
Python3.11.14
torch2.9.0+cpu
torch_npu2.9.0.post1
numpy1.26.4

3. 环境安装

# 1) 昇腾 NPU + CANN 8.5.1 需先就绪 (npu-smi info 可见设备)
# 2) torch / torch_npu (版本须与 CANN 匹配)
pip install torch==2.9.0 torch-npu==2.9.0.post1
# 3) 本仓库依赖
pip install -r requirements.txt
# 4) 拉取模型权重 (已内置在 model/, 或自行克隆)
git clone https://hf-mirror.com/Ambi/compressionkit-ppg-2x-v1.0.git model
# 或用 ModelScope / AtomGit 镜像

3.1 模型权重

仓库自带 model/ 目录, 包含 encoder/decoder TFLite + codebook.npz + sample_stimulus.npz 等工程权重。若需在线拉取:

git clone https://hf-mirror.com/Ambi/compressionkit-ppg-2x-v1.0.git model
# 或 ModelScope / AtomGit hf_mirrors 镜像

4. 推理(昇腾 NPU)

4.1 快速演示(纯 torch, NPU 上跑 RVQ 往返)

python3 inference.py --mode demo --device npu --output-dir outputs
[info] device : npu   (npu_available=True)
[ok] RVQ 量化   : indices (4, 160)
[ok] RVQ 反量化 : recon   (1, 1, 160, 16)
[ok] NPU 往返耗时: 30.500 ms   量化MSE=0.49
[ok] report -> outputs/report.json

4.2 完整编解码(需要 ai-edge-litert)

pip install ai-edge-litert
python3 inference.py --mode infer --device npu

4.3 HTTPS 服务化推理

python3 inference.py --mode serve --device npu --port 8000
curl -s http://127.0.0.1:8000/v1/reconstruct \
  -H 'Content-Type: application/json' \
  -d '{"signal":[0.1,...320 个采样...]}'

5. 精度与性能

  • NMSE/NRMSE 用于评估解码重建与原始 PPG 波形差异(由 sample_stimulus.npz 参考衡量)。
  • demo 模式给出固化在 NPU 上的 RVQ 量化往返 MSE 与耗时, 用于确认昇腾算子正确性。

6. 已适配内容

  1. 设备切换: 管线统一用 torch_npu, 张量固定到 npu:0;
  2. 前/后继程(LayerNorm 与反归一化)与 RVQ 码本量定义/反量化全部以 torch 张量实现, 可在 NPU 上运行;
  3. 神经编码/解码器为 TFLite, 经 LiteRT 参考; 服务化另提供 FastAPI /v1/* 接口。

7. 归属

互动团队: liuhongwei (liuhongwei-2026)。适配自开源模型 【Ambi/compressionkit-ppg-2x-v1.0】(Ambiq, License: ambiq-model-weights-license)。