From 3d10c2d95064b1f39972b9b3f79d781607efcdc4 Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Thu, 9 Mar 2023 04:17:34 +0000 Subject: [PATCH 1/6] add parallel example --- examples/parallel_example.ipynb | 131 ++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 examples/parallel_example.ipynb diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb new file mode 100644 index 0000000..f581579 --- /dev/null +++ b/examples/parallel_example.ipynb @@ -0,0 +1,131 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Install Dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!pip install -q torchaudio\n", + "SAMPLING_RATE = 16000\n", + "import torch\n", + "from pprint import pprint\n", + "torch.set_num_threads(1)\n", + "# download wav files, make multiple copies\n", + "for idx in range(10):\n", + " torch.hub.download_url_to_file('https://models.silero.ai/vad_models/en.wav', f\"en_example{idx}.wav\")\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load VAD model from torch hub" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad',\n", + " model='silero_vad',\n", + " force_reload=True,\n", + " onnx=USE_ONNX)\n", + "\n", + "(get_speech_timestamps,\n", + " save_audio,\n", + " read_audio,\n", + " VADIterator,\n", + " collect_chunks) = utils" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Define a vad process function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def vad_process(audio_file: str):\n", + " with torch.no_grad():\n", + " wav = read_audio(audio_file, sample_rate=SAMPLING_RATE)\n", + " return get_speech_timestamps(\n", + " wav,\n", + " model,\n", + " 0.46, # speech prob threshold\n", + " 16000, # sample rate\n", + " 300, # min speech duration in ms\n", + " 20, # max speech duration in seconds\n", + " 600, # min silence duration\n", + " 512, # window size\n", + " 200, # spech pad ms\n", + " )" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Parallelization" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from concurrent.futures import ProcessPoolExecutor, as_completed\n", + "\n", + "futures = []\n", + "with ProcessPoolExecutor(max_workers=4) as ex:\n", + " for i in range(10):\n", + " futures.append(ex.submit(vad_process, f\"en_example{idx}.wav\"))\n", + "\n", + "for finished in as_completed(futures):\n", + " pprint(finished.result())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "diarization", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.15" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 9865b3cb93326c12ba72577622d8dcd34b9a561c Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Thu, 9 Mar 2023 04:26:15 +0000 Subject: [PATCH 2/6] fix typos --- examples/parallel_example.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb index f581579..162e4f2 100644 --- a/examples/parallel_example.ipynb +++ b/examples/parallel_example.ipynb @@ -20,7 +20,7 @@ "from pprint import pprint\n", "torch.set_num_threads(1)\n", "# download wav files, make multiple copies\n", - "for idx in range(10):\n", + "for idx in range(4):\n", " torch.hub.download_url_to_file('https://models.silero.ai/vad_models/en.wav', f\"en_example{idx}.wav\")\n" ] }, @@ -41,7 +41,7 @@ "model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad',\n", " model='silero_vad',\n", " force_reload=True,\n", - " onnx=USE_ONNX)\n", + " onnx=False)\n", "\n", "(get_speech_timestamps,\n", " save_audio,\n", @@ -66,7 +66,7 @@ "source": [ "def vad_process(audio_file: str):\n", " with torch.no_grad():\n", - " wav = read_audio(audio_file, sample_rate=SAMPLING_RATE)\n", + " wav = read_audio(audio_file, sampling_rate=SAMPLING_RATE)\n", " return get_speech_timestamps(\n", " wav,\n", " model,\n", @@ -98,7 +98,7 @@ "\n", "futures = []\n", "with ProcessPoolExecutor(max_workers=4) as ex:\n", - " for i in range(10):\n", + " for i in range(4):\n", " futures.append(ex.submit(vad_process, f\"en_example{idx}.wav\"))\n", "\n", "for finished in as_completed(futures):\n", From a6a067de44c32355d7d90c5c52a2cc13d1a5d745 Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Thu, 9 Mar 2023 16:16:51 +0000 Subject: [PATCH 3/6] add a initializer --- examples/parallel_example.ipynb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb index 162e4f2..3db2a1c 100644 --- a/examples/parallel_example.ipynb +++ b/examples/parallel_example.ipynb @@ -18,9 +18,12 @@ "SAMPLING_RATE = 16000\n", "import torch\n", "from pprint import pprint\n", + "\n", "torch.set_num_threads(1)\n", + "NUM_PROCESS=4 # set to the number of CPU cores in the machine\n", + "NUM_COPIES=8\n", "# download wav files, make multiple copies\n", - "for idx in range(4):\n", + "for idx in range(NUM_COPIES):\n", " torch.hub.download_url_to_file('https://models.silero.ai/vad_models/en.wav', f\"en_example{idx}.wav\")\n" ] }, @@ -39,15 +42,15 @@ "outputs": [], "source": [ "model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad',\n", - " model='silero_vad',\n", - " force_reload=True,\n", - " onnx=False)\n", + " model='silero_vad',\n", + " force_reload=True,\n", + " onnx=False)\n", "\n", "(get_speech_timestamps,\n", - " save_audio,\n", - " read_audio,\n", - " VADIterator,\n", - " collect_chunks) = utils" + "save_audio,\n", + "read_audio,\n", + "VADIterator,\n", + "collect_chunks) = utils" ] }, { @@ -64,7 +67,12 @@ "metadata": {}, "outputs": [], "source": [ + "def init_model(model):\n", + " global vad_model\n", + " vad_model = model\n", + "\n", "def vad_process(audio_file: str):\n", + " global vad_model\n", " with torch.no_grad():\n", " wav = read_audio(audio_file, sampling_rate=SAMPLING_RATE)\n", " return get_speech_timestamps(\n", @@ -97,8 +105,10 @@ "from concurrent.futures import ProcessPoolExecutor, as_completed\n", "\n", "futures = []\n", - "with ProcessPoolExecutor(max_workers=4) as ex:\n", - " for i in range(4):\n", + "NUM_COPIES=20\n", + "\n", + "with ProcessPoolExecutor(max_workers=NUM_PROCESS) as ex:\n", + " for i in range(NUM_COPIES):\n", " futures.append(ex.submit(vad_process, f\"en_example{idx}.wav\"))\n", "\n", "for finished in as_completed(futures):\n", From c39dccc1fd2298f7842fa3d2b05bed13050b1c94 Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Thu, 9 Mar 2023 16:27:18 +0000 Subject: [PATCH 4/6] add a initializer --- examples/parallel_example.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb index 3db2a1c..039767c 100644 --- a/examples/parallel_example.ipynb +++ b/examples/parallel_example.ipynb @@ -77,7 +77,7 @@ " wav = read_audio(audio_file, sampling_rate=SAMPLING_RATE)\n", " return get_speech_timestamps(\n", " wav,\n", - " model,\n", + " vad_model,\n", " 0.46, # speech prob threshold\n", " 16000, # sample rate\n", " 300, # min speech duration in ms\n", @@ -107,7 +107,7 @@ "futures = []\n", "NUM_COPIES=20\n", "\n", - "with ProcessPoolExecutor(max_workers=NUM_PROCESS) as ex:\n", + "with ProcessPoolExecutor(max_workers=NUM_PROCESS, initializer=init_model, initargs=(model,)) as ex:\n", " for i in range(NUM_COPIES):\n", " futures.append(ex.submit(vad_process, f\"en_example{idx}.wav\"))\n", "\n", From 17903cb41d3d4560cb624590b75a6b5d9bdd89c9 Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Thu, 9 Mar 2023 16:27:36 +0000 Subject: [PATCH 5/6] add a initializer --- examples/parallel_example.ipynb | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb index 039767c..5d1b365 100644 --- a/examples/parallel_example.ipynb +++ b/examples/parallel_example.ipynb @@ -105,7 +105,6 @@ "from concurrent.futures import ProcessPoolExecutor, as_completed\n", "\n", "futures = []\n", - "NUM_COPIES=20\n", "\n", "with ProcessPoolExecutor(max_workers=NUM_PROCESS, initializer=init_model, initargs=(model,)) as ex:\n", " for i in range(NUM_COPIES):\n", From 55c41abf46f4ab0db36ec317e220ba26894dae85 Mon Sep 17 00:00:00 2001 From: Ziyuan Wang Date: Fri, 10 Mar 2023 16:09:07 +0000 Subject: [PATCH 6/6] use a process specific copy of model --- examples/parallel_example.ipynb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/examples/parallel_example.ipynb b/examples/parallel_example.ipynb index 5d1b365..9704291 100644 --- a/examples/parallel_example.ipynb +++ b/examples/parallel_example.ipynb @@ -14,7 +14,7 @@ "metadata": {}, "outputs": [], "source": [ - "!pip install -q torchaudio\n", + "# !pip install -q torchaudio\n", "SAMPLING_RATE = 16000\n", "import torch\n", "from pprint import pprint\n", @@ -67,17 +67,27 @@ "metadata": {}, "outputs": [], "source": [ + "import multiprocessing\n", + "\n", + "vad_models = dict()\n", + "\n", "def init_model(model):\n", - " global vad_model\n", - " vad_model = model\n", + " pid = multiprocessing.current_process().pid\n", + " model, _ = torch.hub.load(repo_or_dir='snakers4/silero-vad',\n", + " model='silero_vad',\n", + " force_reload=False,\n", + " onnx=False)\n", + " vad_models[pid] = model\n", "\n", "def vad_process(audio_file: str):\n", - " global vad_model\n", + " \n", + " pid = multiprocessing.current_process().pid\n", + " \n", " with torch.no_grad():\n", " wav = read_audio(audio_file, sampling_rate=SAMPLING_RATE)\n", " return get_speech_timestamps(\n", " wav,\n", - " vad_model,\n", + " vad_models[pid],\n", " 0.46, # speech prob threshold\n", " 16000, # sample rate\n", " 300, # min speech duration in ms\n", @@ -132,8 +142,7 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" - }, - "orig_nbformat": 4 + } }, "nbformat": 4, "nbformat_minor": 2