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

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

简介

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

模型架构为极小的 2D-CNN 矢量量化自编码器(非自回归、非生成式),比同族 2x 版本更深、压缩率更高:

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

核心指标:compression_ratio=4,每帧 320 个 float32 波形采样(1280 字节)压缩为 320 个 int 索引(≈1280 字节 @ int32 / 320 字节 @ int8),供低功耗边缘 PPG 传感器低带宽回传。

为什么不用 vLLM-Ascend

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

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

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

验证环境

项目版本/型号
NPU昇腾 Ascend 910 ×2(单卡部署,本模型约 390 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 个浮点,逗号分隔)→ 80×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,...

# 对官方 sample_stimulus 批量编码并输出重建误差
python inference.py --weights ./weights --device npu:1 --file model/sample_stimulus.npz --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": [[80×4×int]], "shape": [N,80,4]}
POST /v1/decode{"indices": [[80×4×int]]}{"reconstruction": [[320×float]]}
POST /v1/reconstruct{"input": [[320×float]]}{"indices": ..., "reconstruction": ...}
# 示例调用
curl -s http://127.0.0.1:8010/v1/predictions \
  -H 'Content-Type: application/json' \
  -d '{"input": [[0.1, 0.2, -0.3, ...320个浮点]]}'
# => {"indices": [[[101,70,246,163], ...80组×4级]], "shape": [1,80,4]}

预处理契约

上游 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 自动反归一化)。

验证结果(2026-08-23 实测)

重建质量(官方 sample_stimulus.npz,10 帧)

指标值
解码索引形状[N, 80, 4]
NPU 重建 vs 官方 reconstruction(mean abs)0.005
NPU 重建 vs 原始输入(mean abs)0.009(相对误差 ≈ 1.1%)
CPU 重建 vs 官方 reconstruction(mean abs)0.005(NPU/CPU 一致)

推理延迟(Ascend 910,单帧 encode)

模式延迟
HTTP 批量 10 帧 encode0.18 s(含 JSON 序列化)
CLI 单帧 encode(100 次均值)1.09 ms

NPU 占用(npu-smi 实测,推理服务进程 PID 38431)

| NPU  Chip | Process id | Process name | Process memory(MB) |
| 6    1    | 38431      | python3      | 190                |
  • 推理服务单进程 NPU 显存占用 ≈ 190 MB(含框架加载);
  • 模型权重仅约 390 KB,AICore 利用率随批量推理波动;
  • 与同机 vLLM 大模型推理进程(VLLMEngineCor 385 MB)共存无冲突。

推理输入/输出样例(官方样本第 1 帧)

输入(归一化空间前 24 采样):

[-0.241 -0.293 -0.354 -0.428 -0.521 -0.632 -0.759 -0.889 -1.006 -1.092
 -1.128 -1.105 -1.021 -0.887 -0.725 -0.558 -0.409 -0.291 -0.207 -0.152
 -0.114 -0.085 -0.063 -0.047]

压缩索引(RVQ 4 级,前 8 个位置):

[[101, 70, 246, 163], [113, 70, 246, 240], [178, 70, 246, 87], [113, 72, 62, 87],
 [ 73, 36, 197, 87], [ 16, 70, 46, 87], [ 91, 72, 47, 27], [240, 69, 203, 103]]

重建输出(前 24 采样,与输入逐点吻合):

[-0.245 -0.283 -0.347 -0.437 -0.519 -0.636 -0.764 -0.898 -1.011 -1.093
 -1.129 -1.113 -1.041 -0.895 -0.728 -0.561 -0.409 -0.286 -0.203 -0.153
 -0.118 -0.086 -0.064 -0.046]

可视化

模型架构

architecture

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

reconstruction compare

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

codebook levels

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

npu usage

适配要点(踩坑记录)

  1. Keras v3 .keras 是 zip 包:内含 metadata.json / config.json / model.weights.h5,转换时需 zipfile 读出 model.weights.h5 再用 h5py 加载(直接 h5py.File('.keras') 报 file signature not found)。
  2. TF 'same' padding 手算:stride=2, k=7 时 pad (left=2, right=3);decoder k=7 无 stride pad (3,3),k=5 pad (2,2)。全部手写 F.pad 显式处理,避免对 NPU 依赖 TF 运行时的 padding='same'。
  3. codebook 命名空间坑:转换脚本 build_model.py 把 RVQ 码本注册在顶层(codebook_i),而运行时库 ppg4x_lib.py 的 RVQQuantizer 内是 quantizer.codebook_i。若直接 load_state_dict(strict=False) 会静默丢 codebook(保持全零),重建误差从 0.005 恶化到 0.83。修复:from_pretrained 中对顶层 codebook_i key 做 remap → quantizer.codebook_i。
  4. LayerNorm 通道轴:decoder 的 LayerNorm 作用在通道维 48(channels_last [B,1,T,48]),需先 permute 到 channels_last 再 LN,再 permute 回 channels_first。

文件

weights/ppg_4x_weights.pt    # 转换后的 PyTorch 权重(来自官方 encoder/decoder.keras)
weights/codec_spec.json      # 上游 codec 规格(输入/输出契约)
model/*.keras|.npz|.json     # 上游原始工件(可选的转换源)
inference.py                 # CLI + FastAPI 服务
ppg4x_lib.py                 # PyTorch 自包含模型(PPGAutoEncoder + RVQQuantizer)
build_model.py               # Keras → PyTorch 离线转换脚本(zipfile+h5py)
gen_assets.py                # 生成 assets/*.png 文档图片
assets/*.png                 # 架构图 / 重建对比 / 码本 / NPU 占用图
requirements.txt

参考

  • 上游模型:Ambiq/compressionkit-ppg-4x-v1.0(Hugging Face,含 encoder.keras / decoder.keras / codebook.npz / codec_spec.json / sample_stimulus.npz)
  • 同族 2x 适配:fbybl/compressionkit-ppg-2x-20260823(本环境已完成同等 NPU 适配)