本文档记录 LFM2-Audio-1.5B 在华为 昇腾 NPU (Ascend) 环境下的适配、验证与推理结果。
LFM2-Audio-1.5B 是 Liquid AI 推出的端到端音频基础模型,具备 1.5B 参数量,原生支持音频+文本输入与输出。本仓库完成了以下适配工作:
torch_npu 完成模型加载与推理的 NPU 适配liquid-audio 包对 Python 3.12 的强依赖,使其可在 Python 3.11 运行.cuda() 调用,实现设备无关的自动降级(NPU > CUDA > CPU)相关获取地址:
liquid_audio 源码,无需额外安装 liquid-audio 包| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
accelerate | 1.13.0 |
einops | 0.8.x |
safetensors | 0.5.x |
1 逻辑卡(Ascend910 系列)./model_files3.11.14将以下文件下载到 ./model_files/ 目录:
model_files/
├── config.json
├── model.safetensors
├── tokenizer.json
├── tokenizer_config.json
├── special_tokens_map.json
├── chat_template.jinja
└── tokenizer-e351c8d8-checkpoint125.safetensorspython inference.py \
--model_path ./model_files \
--device npu \
--prompt "Hello, how are you?" \
--max_new_tokens 20 \
--temperature 0.0脚本会自动检测设备优先级:NPU > CUDA > CPU。若省略 --device,将自动选择当前可用的最佳设备。
python benchmark.py \
--model_path ./model_files \
--prompt "Hello, how are you?" \
--max_new_tokens 10 \
--runs 3该脚本会:
基础推理验证:
python inference.py --device npu --prompt "What is the capital of France?" --max_new_tokens 15验证结果:
npu:0The capital of France is Paris测试条件:greedy decode / max_new_tokens=10 / bfloat16
| 指标 | 数值 |
|---|---|
CPU inference time | 181.245 s |
NPU inference time (avg) | 0.340 s |
NPU inference time (min) | 0.337 s |
NPU inference time (max) | 0.345 s |
Speedup (CPU vs NPU) | 532.98 x |
Throughput (NPU) | 29.41 tok/s |
同等输入、同等 greedy 解码策略下,CPU(bfloat16)与 NPU(bfloat16)输出对比:
| 指标 | 数值 |
|---|---|
CPU tokens | [550, 10109, 5296, 1714, 521, 14165, 1219, 1219, 1219, 1219] |
NPU tokens | [550, 10109, 5296, 1714, 521, 14165, 1219, 1219, 1219, 1219] |
Token-level accuracy | 100.00% |
Relative error | 0.0000% |
结论:NPU 推理结果与 CPU 基线完全一致,相对误差严格满足 < 1% 要求。
.
├── inference.py # NPU 适配推理脚本(含自动设备选择)
├── benchmark.py # 精度与性能评测脚本
├── run_log.txt # 评测运行日志
├── model_files/ # 模型权重与配置文件
├── liquid_audio_src/ # 适配后的 liquid_audio 源码(Python 3.11 + NPU 兼容)
│ └── liquid_audio/
│ ├── model/
│ ├── processor.py
│ ├── utils.py
│ └── ...
└── readme.md # 本文档Python 3.11 兼容
type CacheType = ... 改为 CacheType = Union[...]def func[T: (int, torch.Tensor)] 泛型语法改为普通函数Self、assert_never 从 typing_extensions 导入NPU 设备适配
LFM2AudioModel.from_pretrained 与 LFM2AudioProcessor.from_pretrained 的默认 device 改为自动探测:npu > cuda > cpuprocessor.py:151 的硬编码 .cuda() 改为 .to(self.device)sdpa 注意力实现(flash_attention_2 在 NPU 上不可用)本地路径支持
from_pretrained 支持传入 pathlib.Path 对象,避免 snapshot_download 对本地路径的校验失败torch-npu 2.9.0.post1 + CANN 8.5.1 组合可正常加载与推理bfloat16,NPU 与 CPU 均支持该精度model.safetensors 约 2.8GB)到 NPU 需约 2~3 秒,后续推理速度稳定tokenizer-e351c8d8-checkpoint125.safetensors(Mimi 权重)已存在于 model_files/ 目录本项目中的适配代码遵循 MIT License。模型权重与原始 liquid-audio 代码遵循其各自的授权协议(LFM Open License v1.0 / Apache 2.0 / MIT 等)。