Skip to content

Commit

Permalink
add WeNet and Whisper.cpp and some small fixes (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MengqingCao authored Sep 19, 2024
1 parent 71e1c10 commit 5cb3a99
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 5 deletions.
Binary file removed _static/images/image.png
Binary file not shown.
Binary file added _static/images/whisper_cpp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 24 additions & 4 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
sources/opencv/index.rst
sources/sd_webui/index.rst
sources/lm_evaluation/index.rst
sources/wenet/index.rst
sources/whisper_cpp/index.rst

.. warning::

Expand Down Expand Up @@ -166,9 +168,9 @@
<div class="flex space-x-4 text-blue-600">
<a href="https://huggingface.co/docs/transformers/index">官方链接</a>
<span class="split">|</span>
<a href="href="sources/transformers/install.html">安装指南</a>
<a href="href=sources/transformers/install.html">安装指南</a>
<span class="split">|</span>
<a href="href="sources/transformers/fine-tune.html">快速上手</a>
<a href="href=sources/transformers/fine-tune.html">快速上手</a>
</div>
</div>
<!-- Card 8 -->
Expand Down Expand Up @@ -220,9 +222,9 @@
<div class="flex space-x-4 text-blue-600">
<a href="https://github.com/wenet-e2e/wenet">官方链接</a>
<span class="split">|</span>
<a href="#">安装指南</a>
<a href="sources/wenet/install.html">安装指南</a>
<span class="split">|</span>
<a href="#">快速上手</a>
<a href="sources/wenet/quick_start.html">快速上手</a>
</div>
</div>
<!-- Card 11 -->
Expand All @@ -243,5 +245,23 @@
<a href="sources/lm_evaluation/quick_start.html">快速上手</a>
</div>
</div>
<!-- Card 12 -->
<div class="box rounded-lg p-4 flex flex-col items-center">
<div class="flex items-center mb-4">
<div class="img w-16 h-16 rounded-md mr-4" style="background-image: url('_static/images/whisper_cpp.png')"></div>
<div>
<h2 class="text-lg font-semibold">Whisper.cpp</h2>
<p class="text-gray-600 desc">Whisper 模型高性能推理语音识别框架</p>
</div>
</div>
<div class="flex-grow"></div>
<div class="flex space-x-4 text-blue-600">
<a href="https://github.com/ggerganov/whisper.cpp">官方链接</a>
<span class="split">|</span>
<a href="sources/whisper_cpp/install.html">安装指南</a>
<span class="split">|</span>
<a href="sources/whisper_cpp/quick_start.html">快速上手</a>
</div>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion sources/transformers/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

.. note::

以下模型用到了Meta-Llama-3-8B-Instruct, 具体可以参考 `模型获取 <./modeldownload.html>`_。
以下模型用到了Meta-Llama-3-8B-Instruct, 具体可以参考 `模型获取 <./modeldownload.html>`_

AutoModelForCausalLM
-----------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions sources/wenet/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
WeNet
===========

.. toctree::
:maxdepth: 2

install.rst
quick_start.rst
79 changes: 79 additions & 0 deletions sources/wenet/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
安装指南
==============

本教程面向使用 WeNet & 昇腾的开发者,帮助完成昇腾环境下 WeNet 的安装。

昇腾环境安装
------------

请根据已有昇腾产品型号及CPU架构等按照 :doc:`快速安装昇腾环境指引 <../ascend/quick_install>` 进行昇腾环境安装。

.. warning::
CANN 最低版本为 8.0.rc1,安装 CANN 时,请同时安装 Kernel 算子包。

Python 环境创建
----------------------

.. code-block:: shell
:linenos:
# 创建名为 wenet 的 python 3.10 的虚拟环境
conda create -y -n wenet python=3.10
# 激活虚拟环境
conda activate wenet
WeNet 安装
----------------------

使用以下指令安装带有 torch-npu 的 WeNet 及训练相关依赖:

.. code-block:: shell
:linenos:
# 安装带有 torch-npu 的 WeNet
pip install -e .[torch-npu]
# 安装 WeNet 训练相关依赖
pip install -r requirements.txt
请遵循以下 torch-npu 相关库的版本控制:

+------------+------------------+-----------+
| Requirement| Minimum | Recommend |
+============+==================+===========+
| CANN | 8.0.RC2.alpha003 | latest |
+------------+------------------+-----------+
| torch | 2.1.0 | 2.2.0 |
+------------+------------------+-----------+
| torch-npu | 2.1.0 | 2.2.0 |
+------------+------------------+-----------+
| torchaudio | 2.1.0 | 2.2.0 |
+------------+------------------+-----------+
| deepspeed | 0.13.2 | latest |
+------------+------------------+-----------+



安装校验
----------------------

使用以下 Python 脚本对 open_clip 的安装进行校验,正确打印 open_clip 的版本号和 NPU 卡号说明安装成功。

.. code-block:: python
:linenos:
:emphasize-lines: 5,6
import torch
import torch_npu
import timm
print("timm version:", timm.version.__version__)
print("NPU devices:", torch.npu.current_device())
正确回显如下(单卡 NPU 环境):

.. code-block:: shell
timm version: 1.0.8.dev0
NPU devices: 0
132 changes: 132 additions & 0 deletions sources/wenet/quick_start.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
快速开始
==================

.. note::

阅读本篇前,请确保已按照 :doc:`安装教程 <./install>` 准备好昇腾环境及 WeNet !

本文档帮助昇腾开发者快速使用 WeNet × 昇腾 进行自动语音识别(Automatic Speech Recognition, ASR)模型的训练、推理和评估等。

WeNet 提供了多种数据集及模型的实验脚本,该脚本将实验分为几个阶段,包含数据集的下载、模型的训练、推理、评估等,均存放在 `examples <https://github.com/wenet-e2e/wenet/blob/main/examples/>`_ 路径下,
本篇以 aishell-1 数据集的实验为例,基于 WeNet `官方教程 <https://wenet.org.cn/wenet/tutorial_aishell.html#>`_ ,
详述如何使用 `NPU 实验脚本 <https://github.com/wenet-e2e/wenet/blob/main/examples/aishell/s0/run_npu.sh>`_ 进行从零开始的语音模型训练。

首先进入该脚本所在目录下:

.. code-block:: shell
:linenos:
cd example/aishell/s0
下载数据
~~~~~~~~~~~~

stage -1 阶段将 aishell-1 数据下载到本地路径 ``$data``:

.. code-block:: shell
:linenos:
bash run_npu.sh --stage -1 --stop_stage -1
如果已下载数据,请更改 ``run_npu.sh`` 脚本中的变量 ``$data`` 值为实际数据集存放的绝对路径,并从下一阶段开始。

准备训练数据
~~~~~~~~~~~~

stage 0 阶段为训练数据准备阶段,将使用 ``local/aishell_data_prep.sh`` 脚本将训练数据重新组织为 ``wav.scp`` 和 ``text`` 两部分。

.. note::

``wav.scp`` 每行记录两个制表符分隔的列: ``wav_id`` 和 ``wav_path``,
``text`` 每行记录两个制表符分隔的列: ``wav_id`` 和 ``text_label``。

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 0 --stop_stage 0
提取最佳 cmvn 特征(可选)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stage 1 阶段从训练数据中提取 cmvn 特征,本阶段为可选阶段,设置 ``cmvn=false`` 可跳过本阶段。

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 1 --stop_stage 1
``tools/compute_cmvn_stats.py`` 用于提取全局 cmvn(倒谱均值和方差归一化)统计数据,用来归一化声学特征。

生成 token 字典
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stage 2 阶段生成训练所需 token 字典,用于 CTC 解码阶段查询,将输出转换为文字。

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 2 --stop_stage 2
准备 WeNet 数据格式
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stage 3 阶段生成 WeNet 所需格式的文件 ``data.list``:

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 3 --stop_stage 3
生成的 ``data.list``每一行都是 json 格式,包含 关键词 ``key`` (文件名称),
语音文件地址 ``wav`` 和 对应文本内容 ``txt`` 三个关键数据。如下为一示例:

.. code-block:: shell
{"key": "BAC009S0002W0122", "wav": "/export/data/asr-data/OpenSLR/33//data_aishell/wav/train/S0002/BAC009S0002W0122.wav", "txt": "而对楼市成交抑制作用最大的限购"}
模型训练
~~~~~~~~~~

stage 4 为模型训练阶段, ``run_npu.sh`` 脚本中实现了 NPU 卡号的自动获取和相关环境变量设置,因此可直接通过以下启动昇腾 NPU 上的模型训练:

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 4 --stop_stage 4
如需自行指定 NPU 卡号,请更改 ``run_npu.sh`` 脚本中的变量 ``ASCEND_RT_VISIBLE_DEVICES`` 值为指定卡号。

.. note::

有关断点重训,参数配置等,请参考 `WeNet 官方文档 <https://wenet.org.cn/wenet/tutorial_aishell.html#stage-4-neural-network-training>`_ 。

测试推理
~~~~~~~~~~~~~~~

stage 5 为模型测试推理阶段,将测试集中语音文件识别为文本:

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 5 --stop_stage 5
此外,stage 5 还提供平均模型的功能,平均模型指当 ``${average_checkpoint}``为 ``true`` 时,
将交叉验证集上的最佳的 ``${average_num}`` 个模型平均,生成增强模型。

.. note::

此阶段还提供解码和 WER 模型评估等功能,详细信息请参考 WeNet `官方文档 <https://wenet.org.cn/wenet/tutorial_aishell.html#stage-5-recognize-wav-using-the-trained-model>`_ 。


导出训练好的模型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stage 6 为模型导出阶段, ``wenet/bin/export_jit.py`` 使用 ``Libtorch`` 导出以上训练好的模型,导出的模型可用于其他编程语言(如 C++)的推理。

.. code-block:: shell
:linenos:
bash run_npu.sh --stage 6 --stop_stage 6
8 changes: 8 additions & 0 deletions sources/whisper_cpp/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Whisper.cpp
===========

.. toctree::
:maxdepth: 2

install.rst
quick_start.rst
53 changes: 53 additions & 0 deletions sources/whisper_cpp/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
安装指南
==============

本教程面向使用 Whisper.cpp & 昇腾的开发者,帮助完成昇腾环境下 Whisper.cpp 的安装。

昇腾环境安装
------------

请根据已有昇腾产品型号及CPU架构等按照 :doc:`快速安装昇腾环境指引 <../ascend/quick_install>` 进行昇腾环境安装。

.. warning::
CANN 最低版本为 8.0.rc1,安装 CANN 时,请同时安装 Kernel 算子包。

Whisper.cpp 编译安装
----------------------

1. 下载 Whisper.cpp 项目到本地

.. code-block:: shell
:linenos:
git clone https://github.com/ggerganov/whisper.cpp.git
2. 在 Whisper.cpp 项目目录下,创建构建目录并进入该目录

.. code-block:: shell
:linenos:
mkdir build
cd build
3. 编译安装 CANN 版本的 Whisper.cpp

.. code-block:: shell
:linenos:
cmake .. -D GGML_CANN=on
make -j
安装校验
----------------------

编译完毕后,无任何报错信息,并输出以下关键回显即说明安装成功:

.. code-block:: shell
[ 90%] Built target quantize
[ 95%] Linking CXX executable ../../bin/main
[ 95%] Built target main
[100%] Linking CXX executable ../../bin/server
[100%] Built target server
Loading

0 comments on commit 5cb3a99

Please sign in to comment.