将 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 传感器低带宽回传。
该模型是确定性的 CNN 特征压缩编解码器,不是生成式 transformer:
.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 权重) |
| CANN | 8.5.1 |
| 操作系统 | Linux (aarch64) |
| Python | 3.11 |
| PyTorch / torch_npu | 2.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| 端点 | 请求 | 响应 |
|---|---|---|
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 自动反归一化)。
| 指标 | 值 |
|---|---|
| 解码索引形状 | [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 一致) |
| 模式 | 延迟 |
|---|---|
| HTTP 批量 10 帧 encode | 0.18 s(含 JSON 序列化) |
| CLI 单帧 encode(100 次均值) | 1.09 ms |
| NPU Chip | Process id | Process name | Process memory(MB) |
| 6 1 | 38431 | python3 | 190 |输入(归一化空间前 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]



.keras 是 zip 包:内含 metadata.json / config.json / model.weights.h5,转换时需 zipfile 读出 model.weights.h5 再用 h5py 加载(直接 h5py.File('.keras') 报 file signature not found)。(left=2, right=3);decoder k=7 无 stride pad (3,3),k=5 pad (2,2)。全部手写 F.pad 显式处理,避免对 NPU 依赖 TF 运行时的 padding='same'。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。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.txtAmbiq/compressionkit-ppg-4x-v1.0(Hugging Face,含 encoder.keras / decoder.keras / codebook.npz / codec_spec.json / sample_stimulus.npz)fbybl/compressionkit-ppg-2x-20260823(本环境已完成同等 NPU 适配)