{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Devices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [malaya-speech/example/devices](https://github.com/huseinzol05/malaya-speech/tree/master/example/devices).\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List available devices supported to run Malaya-Speech model" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import malaya_speech\n", "import logging\n", "logging.basicConfig(level = logging.INFO)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('CPU:0', '0.268 GB'),\n", " ('XLA_CPU:0', '17.18 GB'),\n", " ('XLA_GPU:0', '17.18 GB'),\n", " ('XLA_GPU:1', '17.18 GB'),\n", " ('XLA_GPU:2', '17.18 GB'),\n", " ('XLA_GPU:3', '17.18 GB'),\n", " ('GPU:0', '30.486 GB'),\n", " ('GPU:1', '30.489 GB'),\n", " ('GPU:2', '30.489 GB'),\n", " ('GPU:3', '30.489 GB')]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "malaya_speech.utils.available_device()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use specific device for specific model\n", "\n", "To do that, pass `device` parameter to any load model function in Malaya, default is `CPU:0`.\n", "\n", "```python\n", "malaya_speech.gender.deep_model(model = 'vggvox-v2', device = 'CPU:0')\n", "```\n", "\n", "Or if you want to use XLA,\n", "\n", "```python\n", "malaya_speech.gender.deep_model(model = 'vggvox-v2', device = 'XLA_CPU:0')\n", "```\n", "\n", "By default, `device` will automatically set to a gpu with the most empty memory." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:running gender/vggvox-v2 using device /device:GPU:1\n" ] } ], "source": [ "gender = malaya_speech.gender.deep_model(model = 'vggvox-v2', device = 'CPU:0')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Disable auto GPU\n", "\n", "Let say you do not want to use auto allocate to gpu, simply set `auto_gpu` to `False`, or set,\n", "\n", "```bash\n", "export CUDA_VISIBLE_DEVICES=''\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:running gender/vggvox-v2 using device /device:CPU:0\n" ] } ], "source": [ "gender_cpu = malaya_speech.gender.deep_model(model = 'vggvox-v2', device = 'CPU:0', auto_gpu = False)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:running gender/vggvox-v2 using device /device:XLA_CPU:0\n" ] } ], "source": [ "gender_xla_cpu = malaya_speech.gender.deep_model(model = 'vggvox-v2', device = 'XLA_CPU:0', auto_gpu = False)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(8000, 16000)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y, sr = malaya_speech.load('speech/video/The-Singaporean-White-Boy.wav')\n", "y = y[:int(sr * 0.5)]\n", "len(y), sr" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 923 ms, sys: 429 ms, total: 1.35 s\n", "Wall time: 1.22 s\n" ] }, { "data": { "text/plain": [ "array([[0.21194158, 0.2119417 , 0.5761167 ]], dtype=float32)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "gender_cpu.predict_proba([y])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4.61 s, sys: 503 ms, total: 5.11 s\n", "Wall time: 4.74 s\n" ] }, { "data": { "text/plain": [ "array([[0.21194158, 0.2119417 , 0.5761167 ]], dtype=float32)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "gender_xla_cpu.predict_proba([y])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Again, not all Tensorflow operation support XLA**." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.9" } }, "nbformat": 4, "nbformat_minor": 5 }