encodec_24khz 是 Meta AI 开发的高保真神经音频编解码器 (Neural Audio Codec),能够将音频信号压缩到低比特率同时保持高质量音质。该模型基于深度学习技术,支持实时音频编码和解码,适用于语音压缩、音乐压缩等场景。
encodec_24khz-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_audio.npy # 测试音频样本
├── test_sample.txt # 测试样本说明
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-16/encodec_24khz/facebook/encodec_24khz/ 目录下:
pip install transformers torch_npu -i https://pypi.huaweicloud.com/repository/pypi/simple/运行推理脚本进行音频编码和解码:
cd /data/ysws/agentsp/5-16/encodec_24khz-ascend/
python3 inference.py --mode inference运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-16/encodec_24khz-ascend/
python3 inference.py --mode precision_testcd /data/ysws/agentsp/5-16/encodec_24khz-ascend/
python3 inference.py --mode all| 参数 | 说明 | 默认值 |
|---|---|---|
--mode | 测试模式: inference, precision_test 或 all | all |
| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 最大相对误差 | 0.7452% | < 1.00% | PASS |
| 最大绝对误差 | 2.19e-02 | - | - |
| CPU 推理时间 | 1.372s | - | - |
| NPU 推理时间 | 0.052s | - | - |
| 加速比 | 26.25x | > 1x | PASS |
| 操作 | 耗时 |
|---|---|
| NPU 编码时间 (1秒音频) | 8.285s |
| NPU 解码时间 (1秒音频) | 2.727s |
| NPU 前向传播时间 | 0.414s |
| 精度测试 NPU 推理 | 0.052s |
输入音频: 1秒, 24kHz采样率, 单声道 输出音频: 1秒, 24kHz采样率, 单声道 编码后码本形状: torch.Size([1, 1, 2, 75])
import torch
from transformers import EncodecModel, AutoProcessor
MODEL_DIR = "/data/ysws/agentsp/5-16/encodec_24khz/facebook/encodec_24khz"
model = EncodecModel.from_pretrained(MODEL_DIR)
processor = AutoProcessor.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()
audio = torch.randn(24000)
inputs = processor(raw_audio=audio.numpy(), sampling_rate=24000, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
output = model(**inputs)
audio_values = output.audio_valuesencoder_outputs = model.encode(inputs["input_values"], inputs["padding_mask"])
decoded = model.decode(encoder_outputs.audio_codes, encoder_outputs.audio_scales, inputs["padding_mask"])| 组件 | 说明 |
|---|---|
| encoder | 音频编码器,将波形转为离散码 |
| decoder | 音频解码器,从码恢复波形 |
| quantizer | 矢量量化器,压缩表示 |
从 config.json 提取的关键参数:
{
"audio_channels": 1,
"codebook_dim": 128,
"codebook_size": 1024,
"compress": 2,
"hidden_size": 128,
"num_filters": 32,
"num_residual_layers": 1,
"sampling_rate": 24000,
"target_bandwidths": [1.5, 3.0, 6.0, 12.0, 24.0],
"use_causal_conv": true
}A: 检查 NPU 驱动是否正确安装。0.7% 的数值误差是正常的,因为 NPU 和 CPU 使用不同的计算精度和算子实现。
A: 首次推理需要编译算子。后续推理会显著加快。编码时间 > 解码时间是正常的,因为编码涉及量化操作。
A: 在输入前需要将音频重采样到 24kHz。可以在预处理时使用 librosa 或 scipy.signal 进行重采样。
============================================================
Encodec 24kHz NPU Test
Model: facebook/encodec_24khz
Output: /data/ysws/agentsp/5-16/encodec_24khz-ascend
============================================================
============================================================
Encodec Inference Test (NPU)
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-16/encodec_24khz/facebook/encodec_24khz
Loading model...
Loading weights: 100%|██████████| 252/252 [00:00<00:00, 5608.67it/s]
Model loaded successfully
Input audio shape: torch.Size([1, 24000])
Sampling rate: 24000 Hz
Duration: 1 sec
Processed input shape: torch.Size([1, 1, 24000])
Running encoder...
Encoded codes shape: torch.Size([1, 1, 2, 75])
Encoded scales: [None]
Encoding time: 8.285s
Running decoder...
Decoded audio shape: torch.Size([1, 1, 24000])
Decoding time: 2.727s
Total inference time: 11.012s
Running full encode-decode forward pass...
Forward pass output shape: torch.Size([1, 1, 24000])
Forward pass time: 0.414s
Inference result saved to /data/ysws/agentsp/5-16/encodec_24khz-ascend/inference_result.json
============================================================
Precision Test (CPU vs NPU)
============================================================
Using device: npu:0
Loading model on CPU...
Loading weights: 100%|██████████| 252/252 [00:00<00:00, 8297.73it/s]
Loading model on npu:0...
Loading weights: 100%|██████████| 252/252 [00:00<00:00, 9926.79it/s]
Loading processor...
Test audio shape: torch.Size([1, 24000])
Running inference on CPU...
Running inference on NPU...
CPU inference time: 1.372s
NPU inference time: 0.052s
Speedup: 26.25x
Max absolute error: 2.193899e-02
Max relative error: 0.7452% (threshold: 1.0%)
Status: PASS
Precision result saved to /data/ysws/agentsp/5-16/encodec_24khz-ascend/precision_result.json
============================================================
Creating Test Audio Sample
============================================================
Saved test audio: /data/ysws/agentsp/5-16/encodec_24khz-ascend/test_audio.npy (shape: (24000,))
Saved test sample info: /data/ysws/agentsp/5-16/encodec_24khz-ascend/test_sample.txt
============================================================
Test Complete!
============================================================本项目遵循 Apache-2.0 许可证