LLM: add speech T5 GPU example (#10090)
* add speech t5 example * fix * fix
This commit is contained in:
parent
2f4c754759
commit
d3d2ee1b63
2 changed files with 222 additions and 0 deletions
121
python/llm/example/GPU/PyTorch-Models/Model/speech-t5/README.md
Normal file
121
python/llm/example/GPU/PyTorch-Models/Model/speech-t5/README.md
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
# SpeechT5
|
||||
In this directory, you will find examples on how you could use BigDL-LLM `optimize_model` API to accelerate SpeechT5 models. For illustration purposes, we utilize the [microsoft/speecht5_tts](https://huggingface.co/microsoft/speecht5_tts) as reference SpeechT5 models.
|
||||
|
||||
## Requirements
|
||||
To run these examples with BigDL-LLM, we have some recommended requirements for your machine, please refer to [here](../README.md#recommended-requirements) for more information.
|
||||
|
||||
## Example: Synthesize speech with the given input text
|
||||
In the example [synthesize_speech.py](./synthesize_speech.py), we show a basic use case for SpeechT5 model to synthesize speech based on the given text, with BigDL-LLM INT4 optimizations.
|
||||
### 1. Install
|
||||
#### 1.1 Installation on Linux
|
||||
We suggest using conda to manage the Python environment. For more information about conda installation, please refer to [here](https://docs.conda.io/en/latest/miniconda.html#).
|
||||
|
||||
After installing conda, create a Python environment for BigDL-LLM:
|
||||
```bash
|
||||
conda create -n llm python=3.9 # recommend to use Python 3.9
|
||||
conda activate llm
|
||||
|
||||
# below command will install intel_extension_for_pytorch==2.1.10+xpu as default
|
||||
pip install --pre --upgrade bigdl-llm[xpu] -f https://developer.intel.com/ipex-whl-stable-xpu
|
||||
pip install datasets soundfile # additional package required for SpeechT5 to conduct generation
|
||||
```
|
||||
|
||||
#### 1.2 Installation on Windows
|
||||
We suggest using conda to manage environment:
|
||||
```bash
|
||||
conda create -n llm python=3.9 libuv
|
||||
conda activate llm
|
||||
|
||||
# below command will install intel_extension_for_pytorch==2.1.10+xpu as default
|
||||
pip install --pre --upgrade bigdl-llm[xpu] -f https://developer.intel.com/ipex-whl-stable-xpu
|
||||
pip install datasets soundfile # additional package required for SpeechT5 to conduct generation
|
||||
```
|
||||
|
||||
### 2. Configures OneAPI environment variables
|
||||
#### 2.1 Configurations for Linux
|
||||
```bash
|
||||
source /opt/intel/oneapi/setvars.sh
|
||||
```
|
||||
|
||||
#### 2.2 Configurations for Windows
|
||||
```cmd
|
||||
call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
|
||||
```
|
||||
> Note: Please make sure you are using **CMD** (**Anaconda Prompt** if using conda) to run the command as PowerShell is not supported.
|
||||
### 3. Runtime Configurations
|
||||
For optimal performance, it is recommended to set several environment variables. Please check out the suggestions based on your device.
|
||||
#### 3.1 Configurations for Linux
|
||||
<details>
|
||||
|
||||
<summary>For Intel Arc™ A-Series Graphics and Intel Data Center GPU Flex Series</summary>
|
||||
|
||||
```bash
|
||||
export USE_XETLA=OFF
|
||||
export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>For Intel Data Center GPU Max Series</summary>
|
||||
|
||||
```bash
|
||||
export LD_PRELOAD=${LD_PRELOAD}:${CONDA_PREFIX}/lib/libtcmalloc.so
|
||||
export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1
|
||||
export ENABLE_SDP_FUSION=1
|
||||
```
|
||||
> Note: Please note that `libtcmalloc.so` can be installed by `conda install -c conda-forge -y gperftools=2.10`.
|
||||
</details>
|
||||
|
||||
#### 3.2 Configurations for Windows
|
||||
<details>
|
||||
|
||||
<summary>For Intel iGPU</summary>
|
||||
|
||||
```cmd
|
||||
set SYCL_CACHE_PERSISTENT=1
|
||||
set BIGDL_LLM_XMX_DISABLED=1
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>For Intel Arc™ A300-Series or Pro A60</summary>
|
||||
|
||||
```cmd
|
||||
set SYCL_CACHE_PERSISTENT=1
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
|
||||
<summary>For other Intel dGPU Series</summary>
|
||||
|
||||
There is no need to set further environment variables.
|
||||
|
||||
</details>
|
||||
|
||||
> Note: For the first time that each model runs on Intel iGPU/Intel Arc™ A300-Series or Pro A60, it may take several minutes to compile.
|
||||
### 4. Running examples
|
||||
|
||||
```bash
|
||||
python ./synthesize_speech.py --text 'Artificial intelligence refers to the development of computer systems that can perform tasks that typically require human intelligence.'
|
||||
```
|
||||
|
||||
In the example, several arguments can be passed to satisfy your requirements:
|
||||
|
||||
- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the SpeechT5 model (e.g `microsoft/speecht5_tts`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'microsoft/speecht5_tts'`.
|
||||
- `--repo-id-or-vocoder-path REPO_ID_OR_VOCODER_PATH`: argument defining the huggingface repo id for the SpeechT5 vocoder (e.g `microsoft/speecht5_hifigan`, which generates audio from a spectrogram) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'microsoft/speecht5_hifigan'`.
|
||||
- `--repo-id-or-data-path REPO_ID_OR_DATA_PATH`: argument defining the huggingface repo id for the audio dataset (e.g. `Matthijs/cmu-arctic-xvectors`, which decides voice characteristics) to be downloaded, or the path to the huggingface dataset folder. It is default to be `'Matthijs/cmu-arctic-xvectors'`.
|
||||
- `--text TEXT`: argument defining the text to synthesize speech. It is default to be `"Artificial intelligence refers to the development of computer systems that can perform tasks that typically require human intelligence."`.
|
||||
|
||||
#### 4.1 Sample Output
|
||||
|
||||
#### [microsoft/speecht5_tts](https://huggingface.co/microsoft/speecht5_tts)
|
||||
|
||||
Text: Artificial intelligence refers to the development of computer systems that can perform tasks that typically require human intelligence.
|
||||
|
||||
[Click here to hear sample output.](https://llm-assets.readthedocs.io/en/latest/_downloads/f0bebfbe8c350b71fe565a82192c079b/speech-t5-example-output.wav)
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
#
|
||||
# Copyright 2016 The BigDL Authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# Some parts of this file is adapted from
|
||||
# https://huggingface.co/microsoft/speecht5_tts
|
||||
#
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE
|
||||
|
||||
|
||||
import torch
|
||||
import time
|
||||
import argparse
|
||||
|
||||
from bigdl.llm import optimize_model
|
||||
from transformers import SpeechT5Processor, SpeechT5HifiGan, SpeechT5ForTextToSpeech
|
||||
from datasets import load_dataset
|
||||
import soundfile as sf
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Synthesize speech with the given input text using SpeechT5 model')
|
||||
parser.add_argument('--repo-id-or-model-path', type=str, default='microsoft/speecht5_tts',
|
||||
help='The huggingface repo id for the SpeechT5 model to be downloaded'
|
||||
', or the path to the huggingface checkpoint folder')
|
||||
parser.add_argument('--repo-id-or-vocoder-path', type=str, default='microsoft/speecht5_hifigan',
|
||||
help='The huggingface repo id for the vocoder model (which generates audio from a spectrogram) to be downloaded'
|
||||
', or the path to the huggingface checkpoint folder.')
|
||||
parser.add_argument('--repo-id-or-data-path', type=str, default="Matthijs/cmu-arctic-xvectors",
|
||||
help='The huggingface repo id for the audio dataset (which decides voice characteristics) to be downloaded'
|
||||
', or the path to the huggingface dataset folder')
|
||||
parser.add_argument('--text', type=str, default="Artificial intelligence refers to the development of computer systems that can perform tasks that typically require human intelligence.",
|
||||
help='Text to synthesize speech')
|
||||
|
||||
args = parser.parse_args()
|
||||
model_path = args.repo_id_or_model_path
|
||||
vocoder_path = args.repo_id_or_vocoder_path
|
||||
dataset_path = args.repo_id_or_data_path
|
||||
text = args.text
|
||||
|
||||
processor = SpeechT5Processor.from_pretrained(model_path)
|
||||
model = SpeechT5ForTextToSpeech.from_pretrained(model_path)
|
||||
vocoder = SpeechT5HifiGan.from_pretrained(vocoder_path)
|
||||
|
||||
# With only one line to enable BigDL-LLM optimization on model
|
||||
# Skip optimizing these two modules to get higher audio quality
|
||||
# When running LLMs on Intel iGPUs for Windows users, we recommend setting `cpu_embedding=True` in the optimize_model function.
|
||||
# This will allow the memory-intensive embedding layer to utilize the CPU instead of iGPU.
|
||||
model = optimize_model(model, modules_to_not_convert=["speech_decoder_postnet.feat_out",
|
||||
"speech_decoder_postnet.prob_out"])
|
||||
model = model.to('xpu')
|
||||
vocoder = vocoder.to('xpu')
|
||||
|
||||
inputs = processor(text=text, return_tensors="pt").to('xpu')
|
||||
|
||||
# load xvector containing speaker's voice characteristics from a dataset
|
||||
embeddings_dataset = load_dataset(dataset_path, split="validation")
|
||||
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0).to('xpu')
|
||||
|
||||
with torch.inference_mode():
|
||||
# ipex model needs a warmup, then inference time can be accurate
|
||||
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
||||
|
||||
st = time.time()
|
||||
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
||||
torch.xpu.synchronize()
|
||||
end = time.time()
|
||||
print(f"Inference time: {end-st} s")
|
||||
|
||||
sf.write("bigdl_llm_speech_t5_out.wav", speech.to('cpu').numpy(), samplerate=16000)
|
||||
Loading…
Reference in a new issue