p
pnflly/compressionkit-ppg-2x-20260823
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Ambiq/compressionkit-ppg-2x-v1.0 昇腾 NPU 适配

简介

将 Ambiq 的 PPG 2x 有损压缩编解码器(compressionkit-ppg-2x-v1.0)适配到昇腾 910 NPU。该模型对 64 Hz 光电容积脉搏波(Photoplethysmogram, PPG)帧做 2 倍压缩:每帧 320 个采样点(约 5 秒)被压缩为 160×16 的潜变量,再经 RVQ 残差矢量量化(4 级 × 256 码字 × 16 维)转成 4 条索引流(总计 640 个 int 索引,等价 2 倍压缩率),供无线/低功耗生物传感器低带宽传输,解码端还原波形。

模型架构为极小的 2D-CNN 矢量量化自编码器(非自回归、非生成式):

encoder   : Conv2D(48, k=(1,7), s=2) → BatchNorm → ReLU → Conv2D(16, k=(1,1))
            输入 [1,320,1]  →  潜变量 [16,160,16]
RVQ       : 4 级 × 256 × 16 码本  →  4×160 索引流
decoder   : UpSampling2D(2) → Conv2D(48, k=(1,7)) → ReLU → SeparableConv2D(5)
            → LayerNorm(48) → Conv2D(1, k=(1,1))
            潜变量 [160,16] →  重建 [320,1]

为什么不用 vLLM-Ascend

该模型是确定性的 CNN 特征压缩编解码器,不是生成式 transformer:

  • 无自回归解码、无 KV cache、无 logits/采样循环;
  • 输入/输出是数值张量(浮点波形帧 ↔ 整数索引),不是 token 序列;
  • 上游发布格式为 Keras .keras / TensorFlow Lite .tflite,model_type 不被 vLLM/transformers 注册表识别。

因此遵循本环境其他非 transformer 生物/信号模型(xpresso / mpradragonn / deepcpgdna / malinois)的惯例,采用 torch_npu 自包含模型 + FastAPI HTTP 服务方案:将上游 Keras H5 权重离线转换为 PyTorch state_dict(weights/ppg_2x_weights.pt),运行时仅需 torch + torch_npu,无 TensorFlow/Keras 依赖。

验证环境

项目版本/型号
NPU昇腾 Ascend 910 ×2(单卡部署,本模型约 110 KB 权重)
CANN8.5.1
操作系统Linux (aarch64)
Python3.11
PyTorch / torch_npu2.x / 2.x

快速开始

pip install -r requirements.txt

# 自检(CPU/NPU 均可)
python inference.py --weights ./weights --device npu:1 --smoke

# 编码一帧(320 个浮点,逗号分隔)→ 160×4 索引
python inference.py --weights ./weights --device npu:1 \
    --encode 0.1,0.2,-0.3,... --json

# 从索引解码重建帧
python inference.py --weights ./weights --device npu:1 \
    --decode 1,5,200,33,...

# 对 .npy 文件批量编码(N×320)并输出重建误差
python inference.py --weights ./weights --device npu:1 --file frames.npy --json

# 延迟基准
python inference.py --weights ./weights --device npu:1 --bench 100

# HTTP 服务
python inference.py --weights ./weights --device npu:1 --serve --port 8010

服务契约(OpenAI 风格)

端点请求响应
POST /v1/predictions{"input": [[320×float]]}{"indices": [[160×4×int]], "shape": [N,160,4]}
POST /v1/decode{"indices": [[160×4×int]]}{"reconstruction": [[320×float]]}
POST /v1/reconstruct{"input": [[320×float]]}{"indices": ..., "reconstruction": ...}

预处理契约

上游 codec 在编码前对每个 320 采样帧做 per-frame layer norm: norm = (x − mean) / sqrt(var + eps), eps = 1e-3; 解码重建输出同样位于归一化空间,反归一化时使用原始帧的统计量: raw = norm × sqrt(var + eps) + mean。inference.py 已内置该流程(encode 自动归一化,reconstruct 自动反归一化)。

可视化

模型架构

architecture

重建质量(官方 sample_stimulus,输入 vs 重建 vs 官方重建)

reconstruction compare

RVQ 码本(4 级 × 256 × 16,前 24 个码字)

codebook levels

NPU 占用(Ascend 910,npu-smi 实测)

npu usage

文件

weights/ppg_2x_weights.pt   # 转换后的 PyTorch 权重(来自官方 encoder/decoder.keras H5)
weights/codec_spec.json     # 上游 codec 规格(输入/输出契约)
inference.py                # CLI + FastAPI 服务
ppg2x_lib.py                # PyTorch 自包含模型(PPGAutoEncoder + RVQQuantizer)
gen_assets.py               # 生成 assets/*.png 文档图片
assets/*.png                # 架构图 / 重建对比 / 码本 / NPU 占用图
requirements.txt

参考

  • 上游模型:Ambiq/compressionkit-ppg-2x-v1.0(Hugging Face)
  • CompressionKit 系列:Ambiq/compressionkit-ppg-4x-v1.0 等