ekcrisp
ekcrisp

Reputation: 1911

Restrict CPU core usage on linux in python program using Piper TTS library

For some reason when I'm using PiperTTS to generate speech using taskset -c 2,3 python myprogram.py the system does not restrict processing to the cores I specify.

When I use another library, such as llama-cpp-python, taskset is able to restrict the cores execution occurs on. I also tried using psutil python library to set CPU affinity, but this did not work either.

Upvotes: 0

Views: 191

Answers (2)

bua
bua

Reputation: 4880

taskset works on 'best-effort' basis, OS doesn't necessarily mandate desired restrictions.

Use numactl -c instead, this work in conjunction with OS, so if core's you try to use are restricted, numactl will fail to execute (where taskset would still run, allowing your procces to run with no restriction).

numactl is much better practice, i'd advice to retire using taskset. Have a look at cgroups as well.

Upvotes: 0

Alexander Volkovsky
Alexander Volkovsky

Reputation: 2928

PiperTTS leverages OnnxRuntime, which internally manages its own threads.

Try using the -a (--all-tasks, "Set or retrieve the CPU affinity of all the tasks (threads) for a given PID") flag with taskset (e.g., taskset -ac 2,3 ...) or configure OnnxRuntime as follows:

import onnxruntime as ort

sess_opt = ort.SessionOptions()
sess_opt.intra_op_num_threads = 3
sess_opt.add_session_config_entry('session.intra_op_thread_affinities', '1;2')

sess = ort.InferenceSession('model.onnx', sess_opt, ...)


# initialize Piper
# see https://github.com/rhasspy/piper/blob/c0670df63daf07070c9be36b5c4bed270ad72383/src/python_run/piper/voice.py#L25

from piper import PiperVoice
from piper.config import PiperConfig

voice = PiperVoice(
    config=PiperConfig.from_dict({...}),
    session=sess
)

Upvotes: 2

Related Questions