OA0
OA0 是一个探索 AI 的社区
现在注册
已注册用户请  登录
OA0  ›  代码  ›  Whisper.cpp — 高效本地语音识别的 C/C++ 实现

Whisper.cpp — 高效本地语音识别的 C/C++ 实现

 
  merge ·  2026-03-01 04:25:07 · 7 次点击  · 0 条评论  

whisper.cpp

whisper.cpp

Actions Status
License: MIT
Conan Center
npm

稳定版: v1.8.1 / 路线图

高性能推理 OpenAI 的 Whisper 自动语音识别 (ASR) 模型:

支持的平台:

模型的整个高层实现都包含在 whisper.hwhisper.cpp 中。
其余代码是 ggml 机器学习库的一部分。

如此轻量级的模型实现使其能够轻松集成到不同的平台和应用中。
例如,这里有一个在 iPhone 13 设备上运行模型的视频 - 完全离线,在设备上运行:whisper.objc

https://user-images.githubusercontent.com/1991296/197385372-962a6dea-bca1-4d50-bf96-1d8c27b98c81.mp4

你也可以轻松制作自己的离线语音助手应用:command

https://user-images.githubusercontent.com/1991296/204038393-2f846eae-c255-4099-a76d-5735c25c49da.mp4

在 Apple Silicon 上,推理通过 Metal 完全在 GPU 上运行:

https://github.com/ggml-org/whisper.cpp/assets/1991296/c82e8f86-60dc-49f2-b048-d2fdbd6b5225

快速开始

首先克隆仓库:

git clone https://github.com/ggml-org/whisper.cpp.git

进入目录:

cd whisper.cpp

然后,下载一个已转换为 ggml 格式 的 Whisper 模型。例如:

sh ./models/download-ggml-model.sh base.en

现在构建 whisper-cli 示例并转录一个音频文件,如下所示:

# 构建项目
cmake -B build
cmake --build build -j --config Release

# 转录音频文件
./build/bin/whisper-cli -f samples/jfk.wav

要快速演示,只需运行 make base.en

该命令会下载转换为自定义 ggml 格式的 base.en 模型,并对文件夹 samples 中的所有 .wav 样本进行推理。

要查看详细的使用说明,请运行:./build/bin/whisper-cli -h

请注意,whisper-cli 示例目前仅支持 16 位 WAV 文件,因此在运行该工具之前请确保转换你的输入。
例如,你可以像这样使用 ffmpeg

ffmpeg -i input.mp3 -ar 16000 -ac 1 -c:a pcm_s16le output.wav

更多音频样本

如果你想要更多音频样本来试用,只需运行:

make -j samples

这将从维基百科下载一些音频文件,并通过 ffmpeg 将它们转换为 16 位 WAV 格式。

你可以按如下方式下载并运行其他模型:

make -j tiny.en
make -j tiny
make -j base.en
make -j base
make -j small.en
make -j small
make -j medium.en
make -j medium
make -j large-v1
make -j large-v2
make -j large-v3
make -j large-v3-turbo

内存使用

模型 磁盘 内存
tiny 75 MiB ~273 MB
base 142 MiB ~388 MB
small 466 MiB ~852 MB
medium 1.5 GiB ~2.1 GB
large 2.9 GiB ~3.9 GB

POWER VSX 指令集

whisper.cpp 支持 POWER 架构,并包含能显著加速在 POWER9/10 上 Linux 运行性能的代码,使其在降频的 Raptor Talos II 上能够实现快于实时的转录。确保已安装 BLAS 包,并将标准 cmake 设置替换为:

# 定义 GGML_BLAS 进行构建
cmake -B build -DGGML_BLAS=1
cmake --build build -j --config Release
./build/bin/whisper-cli [ .. 等等 .. ]

量化

whisper.cpp 支持 Whisper ggml 模型的整数量化。量化模型需要更少的内存和磁盘空间,并且根据硬件情况,处理效率可能更高。

以下是创建和使用量化模型的步骤:

# 使用 Q5_0 方法量化模型
cmake -B build
cmake --build build -j --config Release
./build/bin/quantize models/ggml-base.en.bin models/ggml-base.en-q5_0.bin q5_0

# 像往常一样运行示例,指定量化模型文件
./build/bin/whisper-cli -m models/ggml-base.en-q5_0.bin ./samples/gb0.wav

Core ML 支持

在 Apple Silicon 设备上,编码器推理可以通过 Core ML 在 Apple 神经引擎 (ANE) 上执行。这可以带来显著的加速 - 与仅 CPU 执行相比,速度可提升 3 倍以上。以下是生成 Core ML 模型并在 whisper.cpp 中使用的说明:

  • 安装创建 Core ML 模型所需的 Python 依赖:

bash pip install ane_transformers pip install openai-whisper pip install coremltools

  • 为确保 coremltools 正常运行,请确认已安装 Xcode 并执行 xcode-select --install 安装命令行工具。
  • 推荐使用 Python 3.11。
  • 推荐使用 MacOS Sonoma (版本 14) 或更新版本,因为旧版本的 MacOS 可能会出现转录幻觉问题。
  • [可选] 建议为此步骤使用 Python 版本管理系统,例如 Miniconda

    • 创建环境:conda create -n py311-whisper python=3.11 -y
    • 激活环境:conda activate py311-whisper
  • 生成 Core ML 模型。例如,要生成 base.en 模型,请使用:

bash ./models/generate-coreml-model.sh base.en

这将生成文件夹 models/ggml-base.en-encoder.mlmodelc

  • 构建支持 Core ML 的 whisper.cpp

bash # 使用 CMake cmake -B build -DWHISPER_COREML=1 cmake --build build -j --config Release

  • 像往常一样运行示例。例如:

```text
$ ./build/bin/whisper-cli -m models/ggml-base.en.bin -f samples/jfk.wav

...

whisper_init_state: loading Core ML model from 'models/ggml-base.en-encoder.mlmodelc'
whisper_init_state: first run on a device may take a while ...
whisper_init_state: Core ML model loaded

system_info: n_threads = 4 / 10 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | VSX = 0 | COREML = 1 |

...
```

设备上的首次运行速度较慢,因为 ANE 服务会将 Core ML 模型编译为某种设备特定的格式。后续运行会更快。

有关 Core ML 实现的更多信息,请参考 PR #566

OpenVINO 支持

在支持 OpenVINO 的平台上,编码器推理可以在 OpenVINO 支持的设备上执行,包括 x86 CPU 和 Intel GPU(集成和独立显卡)。

这可以显著提高编码器性能。以下是生成 OpenVINO 模型并在 whisper.cpp 中使用的说明:

  • 首先,设置 Python 虚拟环境并安装 Python 依赖。推荐使用 Python 3.10。

Windows:

powershell cd models python -m venv openvino_conv_env openvino_conv_env\Scripts\activate python -m pip install --upgrade pip pip install -r requirements-openvino.txt

Linux 和 macOS:

bash cd models python3 -m venv openvino_conv_env source openvino_conv_env/bin/activate python -m pip install --upgrade pip pip install -r requirements-openvino.txt

  • 生成 OpenVINO 编码器模型。例如,要生成 base.en 模型,请使用:

python convert-whisper-to-openvino.py --model base.en

这将生成 ggml-base.en-encoder-openvino.xml/.bin IR 模型文件。建议将这些文件移动到与 ggml 模型相同的文件夹中,因为这是 OpenVINO 扩展在运行时默认搜索的位置。

  • 构建支持 OpenVINO 的 whisper.cpp

发布页面 下载 OpenVINO 包。推荐使用的版本是 2024.6.0。所需库的现成二进制文件可以在 OpenVINO 归档 中找到。

下载并解压包到你的开发系统后,通过执行 setupvars 脚本设置所需的环境。例如:

Linux:

bash source /path/to/l_openvino_toolkit_ubuntu22_2023.0.0.10926.b4452d56304_x86_64/setupvars.sh

Windows (cmd):

powershell C:\Path\To\w_openvino_toolkit_windows_2023.0.0.10926.b4452d56304_x86_64\setupvars.bat

然后使用 cmake 构建项目:

bash cmake -B build -DWHISPER_OPENVINO=1 cmake --build build -j --config Release

  • 像往常一样运行示例。例如:

```text
$ ./build/bin/whisper-cli -m models/ggml-base.en.bin -f samples/jfk.wav

...

whisper_ctx_init_openvino_encoder: loading OpenVINO model from 'models/ggml-base.en-encoder-openvino.xml'
whisper_ctx_init_openvino_encoder: first run on a device may take a while ...
whisper_openvino_init: path_model = models/ggml-base.en-encoder-openvino.xml, device = GPU, cache_dir = models/ggml-base.en-encoder-openvino-cache
whisper_ctx_init_openvino_encoder: OpenVINO model loaded

system_info: n_threads = 4 / 8 | AVX = 1 | AVX2 = 1 | AVX512 = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 1 | VSX = 0 | COREML = 0 | OPENVINO = 1 |

...
```

在 OpenVINO 设备上的首次运行速度较慢,因为 OpenVINO 框架会将 IR(中间表示)模型编译为设备特定的 'blob'。这个设备特定的 blob 将在下次运行时被缓存。

有关 OpenVINO 实现的更多信息,请参考 PR #1037

NVIDIA GPU 支持

使用 NVIDIA 显卡时,模型处理通过 cuBLAS 和自定义 CUDA 内核在 GPU 上高效完成。
首先,确保已安装 cuda:https://developer.nvidia.com/cuda-downloads

现在构建支持 CUDA 的 whisper.cpp

cmake -B build -DGGML_CUDA=1
cmake --build build -j --config Release

或者对于较新的 NVIDIA GPU(RTX 5000 系列):

cmake -B build -DGGML_CUDA=1 -DCMAKE_CUDA_ARCHITECTURES="86"
cmake --build build -j --config Release

Vulkan GPU 支持

跨供应商解决方案,允许你在 GPU 上加速工作负载。
首先,确保你的显卡驱动程序支持 Vulkan API。

现在构建支持 Vulkan 的 whisper.cpp

cmake -B build -DGGML_VULKAN=1
cmake --build build -j --config Release

通过 OpenBLAS 实现 BLAS CPU 支持

编码器处理可以通过 OpenBLAS 在 CPU 上加速。
首先,确保已安装 openblas:https://www.openblas.net/

现在构建支持 OpenBLAS 的 whisper.cpp

cmake -B build -DGGML_BLAS=1
cmake --build build -j --config Release

昇腾 NPU 支持

昇腾 NPU 通过 CANN 和 AI 核心提供推理加速。

首先,检查你的昇腾 NPU 设备是否受支持:

已验证的设备
| 昇腾 NPU | 状态 |
|:-----------------------------:|:-------:|
| Atlas 300T A2 | 支持 |
| Atlas 300I Duo | 支持 |

然后,确保已安装 CANN 工具包 。推荐使用最新版本的 CANN。

现在构建支持 CANN 的 whisper.cpp

cmake -B build -DGGML_CANN=1
cmake --build build -j --config Release

像往常一样运行推理示例,例如:

./build/bin/whisper-cli -f samples/jfk.wav -m models/ggml-base.en.bin -t 8

注意:

  • 如果你在使用昇腾 NPU 设备时遇到问题,请创建一个带有 [CANN] 前缀/标签的 issue。
  • 如果你的昇腾 NPU
7 次点击  ∙  0 人收藏  
登录后收藏  
0 条回复
关于 ·  帮助 ·  PING ·  隐私 ·  条款   
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
耗时 46 ms
Developed with Cursor