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 スレッド数を指定することで実現できます。 diff --git a/test/test_core_utility.py b/test/test_core_utility.py new file mode 100644 index 000000000..a00f7d5b1 --- /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/core/core_initializer.py b/voicevox_engine/core/core_initializer.py index 66c7ca26c..1cf3cbef8 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 @@ -35,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 @@ -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/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