From a976b540fb7717d9ad6345cad0285f91424d516e Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 17:39:43 +0900 Subject: [PATCH 1/6] Add get_half_logical_cores func --- test/test_core_utility.py | 18 ++++++++++++++++++ voicevox_engine/utility/core_utility.py | 8 ++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/test_core_utility.py create mode 100644 voicevox_engine/utility/core_utility.py diff --git a/test/test_core_utility.py b/test/test_core_utility.py new file mode 100644 index 000000000..10cab21c9 --- /dev/null +++ b/test/test_core_utility.py @@ -0,0 +1,18 @@ +from unittest import TestCase +from unittest.mock import patch + +from voicevox_engine.utility.core_utility import get_half_logical_cores + + +class TestHalfLogicalCores(TestCase): + @patch('os.cpu_count', return_value=8) + def test_half_logical_cores_even(self, mock_cpu_count): + self.assertEqual(get_half_logical_cores(), 4) + + @patch('os.cpu_count', return_value=9) + def test_half_logical_cores_odd(self, mock_cpu_count): + self.assertEqual(get_half_logical_cores(), 4) + + @patch('os.cpu_count', return_value=None) + def test_half_logical_cores_none(self, mock_cpu_count): + self.assertEqual(get_half_logical_cores(), 0) diff --git a/voicevox_engine/utility/core_utility.py b/voicevox_engine/utility/core_utility.py new file mode 100644 index 000000000..993fa9ebf --- /dev/null +++ b/voicevox_engine/utility/core_utility.py @@ -0,0 +1,8 @@ +import os + + +def get_half_logical_cores() -> int: + logical_cores = os.cpu_count() + if logical_cores is None: + return 0 + return logical_cores // 2 From ee3a53003e0b1d136d8ffbb75d2fdfaad81c9b32 Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 17:40:20 +0900 Subject: [PATCH 2/6] Use get_half_logical_cores --- voicevox_engine/core/core_initializer.py | 5 +++-- voicevox_engine/core/core_wrapper.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/voicevox_engine/core/core_initializer.py b/voicevox_engine/core/core_initializer.py index 66c7ca26c..e7ef2647f 100644 --- a/voicevox_engine/core/core_initializer.py +++ b/voicevox_engine/core/core_initializer.py @@ -4,6 +4,7 @@ from typing import List, Optional from ..tts_pipeline.tts_engine import CoreAdapter +from ..utility.core_utility import get_half_logical_cores from ..utility.path_utility import engine_root, get_save_dir from .core_wrapper import CoreWrapper, load_runtime_lib @@ -44,10 +45,10 @@ def initialize_cores( if cpu_num_threads == 0 or cpu_num_threads is None: print( "Warning: cpu_num_threads is set to 0. " - + "( The library leaves the decision to the synthesis runtime )", + + "Setting it to half of the logical cores.", file=sys.stderr, ) - cpu_num_threads = 0 + cpu_num_threads = get_half_logical_cores() # ディレクトリを設定する # 引数による指定を反映する diff --git a/voicevox_engine/core/core_wrapper.py b/voicevox_engine/core/core_wrapper.py index a0b83e634..5db85e465 100644 --- a/voicevox_engine/core/core_wrapper.py +++ b/voicevox_engine/core/core_wrapper.py @@ -10,6 +10,8 @@ import numpy as np from numpy.typing import NDArray +from voicevox_engine.utility.core_utility import get_half_logical_cores + class OldCoreError(Exception): """古いコアが使用されている場合に発生するエラー""" @@ -517,7 +519,7 @@ def __init__( self, use_gpu: bool, core_dir: Path, - cpu_num_threads: int = 0, + cpu_num_threads: int = get_half_logical_cores(), load_all_models: bool = False, ) -> None: self.default_sampling_rate = 24000 From 422eea3a7235d928e7fe9a7fd2af846ab437e4da Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 17:45:54 +0900 Subject: [PATCH 3/6] Fix quote --- test/test_core_utility.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_core_utility.py b/test/test_core_utility.py index 10cab21c9..a00f7d5b1 100644 --- a/test/test_core_utility.py +++ b/test/test_core_utility.py @@ -5,14 +5,14 @@ class TestHalfLogicalCores(TestCase): - @patch('os.cpu_count', return_value=8) + @patch("os.cpu_count", return_value=8) def test_half_logical_cores_even(self, mock_cpu_count): self.assertEqual(get_half_logical_cores(), 4) - @patch('os.cpu_count', return_value=9) + @patch("os.cpu_count", return_value=9) def test_half_logical_cores_odd(self, mock_cpu_count): self.assertEqual(get_half_logical_cores(), 4) - @patch('os.cpu_count', return_value=None) + @patch("os.cpu_count", return_value=None) def test_half_logical_cores_none(self, mock_cpu_count): self.assertEqual(get_half_logical_cores(), 0) From 4c8776839f408d83b8e50002e8ab622e930176b8 Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 17:50:49 +0900 Subject: [PATCH 4/6] Reset core_wrapper.py --- voicevox_engine/core/core_wrapper.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/voicevox_engine/core/core_wrapper.py b/voicevox_engine/core/core_wrapper.py index 5db85e465..a0b83e634 100644 --- a/voicevox_engine/core/core_wrapper.py +++ b/voicevox_engine/core/core_wrapper.py @@ -10,8 +10,6 @@ import numpy as np from numpy.typing import NDArray -from voicevox_engine.utility.core_utility import get_half_logical_cores - class OldCoreError(Exception): """古いコアが使用されている場合に発生するエラー""" @@ -519,7 +517,7 @@ def __init__( self, use_gpu: bool, core_dir: Path, - cpu_num_threads: int = get_half_logical_cores(), + cpu_num_threads: int = 0, load_all_models: bool = False, ) -> None: self.default_sampling_rate = 24000 From 047bdbc18a32efba72df845fd4dacd0e0f167bf6 Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 23:08:45 +0900 Subject: [PATCH 5/6] Fix comment in initialize_cores --- voicevox_engine/core/core_initializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voicevox_engine/core/core_initializer.py b/voicevox_engine/core/core_initializer.py index e7ef2647f..1cf3cbef8 100644 --- a/voicevox_engine/core/core_initializer.py +++ b/voicevox_engine/core/core_initializer.py @@ -36,7 +36,7 @@ def initialize_cores( None のとき、voicevox_dir、カレントディレクトリになる cpu_num_threads: int, optional, default=None 音声ライブラリが、推論に用いるCPUスレッド数を設定する - Noneのとき、ライブラリ側の挙動により論理コア数の半分か、物理コア数が指定される + Noneのとき、論理コア数の半分が指定される enable_mock: bool, optional, default=True コア読み込みに失敗したとき、代わりにmockを使用するかどうか load_all_models: bool, optional, default=False From 42e927c9e9bdabd9f469b0878ccfaeb73206b6af Mon Sep 17 00:00:00 2001 From: tomoish Date: Fri, 8 Mar 2024 23:41:14 +0900 Subject: [PATCH 6/6] Fix README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36d91429d..07a1149b7 100644 --- a/README.md +++ b/README.md @@ -434,7 +434,7 @@ python run.py --output_log_utf8 #### CPU スレッド数を指定する -CPU スレッド数が未指定の場合は、論理コア数の半分か物理コア数が使われます。(殆どの CPU で、これは全体の処理能力の半分です) +CPU スレッド数が未指定の場合は、論理コア数の半分が使われます。(殆どの CPU で、これは全体の処理能力の半分です) もし IaaS 上で実行していたり、専用サーバーで実行している場合など、 エンジンが使う処理能力を調節したい場合は、CPU スレッド数を指定することで実現できます。