Skip to content

Threadpoolctl

The ThreadPoolCTLPlugin if useful for parallel training of models. Without limiting with threadpoolctl, the number of threads used by a given model may oversubscribe to resources and cause significant slowdowns.

This is the mechanism employed by scikit-learn to limit the number of threads used by a given model.

See threadpoolctl documentation.

Requirements

This requires threadpoolctl which can be installed with:

pip install amltk[threadpoolctl]

# Or directly
pip install threadpoolctl
Usage

from amltk.scheduling import Scheduler
from amltk.scheduling.plugins.threadpoolctl import ThreadPoolCTLPlugin

scheduler = Scheduler.with_processes(1)

def f() -> None:
    # ... some task that respects the limits set by threadpoolctl
    pass

task = scheduler.task(f, plugins=ThreadPoolCTLPlugin(max_threads=1))

╭─ Task f() -> 'None' ─────────────────────────────────────────────────────────╮
│ ╭────────────────────── Plugin threadpoolctl-plugin ───────────────────────╮ │
│ │  Max Threads  User-API                                                   │ │
│ │  1            None                                                       │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│ @on_submitted                                                                │
│ @on_done                                                                     │
│ @on_result                                                                   │
│ @on_exception                                                                │
│ @on_cancelled                                                                │
╰─────────────────────────────── Ref: 18aMFtQy ────────────────────────────────╯

class ThreadPoolCTLPlugin(max_threads=None, user_api=None) #

Bases: Plugin

A plugin that limits the usage of threads in a task.

This plugin is used to make utilize threadpoolctl with tasks, useful for parallel training of models. Without limiting with threadpoolctl, the number of threads used by a given model may oversubscribe to resources and cause significant slowdowns.

ATTRIBUTE DESCRIPTION
max_calls

The maximum number of calls to the task.

max_concurrent

The maximum number of calls of this task that can be in the queue.

PARAMETER DESCRIPTION
max_threads

The maximum number of threads to use.

TYPE: int | dict[str, int] | None DEFAULT: None

user_api

The user API to limit.

TYPE: str | None DEFAULT: None

Source code in src/amltk/scheduling/plugins/threadpoolctl.py
@override
def __init__(
    self,
    max_threads: int | dict[str, int] | None = None,
    user_api: str | None = None,
):
    """Initialize the plugin.

    Args:
        max_threads: The maximum number of threads to use.
        user_api: The user API to limit.
    """
    super().__init__()
    self.max_threads = max_threads
    self.user_api = user_api
    self.task: Task | None = None

name: ClassVar
classvar attr
#

The name of the plugin.

def attach_task(task) #

Attach the plugin to a task.

Source code in src/amltk/scheduling/plugins/threadpoolctl.py
@override
def attach_task(self, task: Task) -> None:
    """Attach the plugin to a task."""
    self.task = task

def pre_submit(fn, *args, **kwargs) #

Pre-submit hook.

Wrap the function in something that will activate threadpoolctl when the function is called.

Source code in src/amltk/scheduling/plugins/threadpoolctl.py
@override
def pre_submit(
    self,
    fn: Callable[P, R],
    *args: P.args,
    **kwargs: P.kwargs,
) -> tuple[Callable[P, R], tuple, dict] | None:
    """Pre-submit hook.

    Wrap the function in something that will activate threadpoolctl
    when the function is called.
    """
    assert self.task is not None

    fn = _ThreadPoolLimiter(
        fn=fn,
        max_threads=self.max_threads,
        user_api=self.user_api,
    )
    return fn, args, kwargs

def copy() #

Return a copy of the plugin.

Please see Plugin.copy().

Source code in src/amltk/scheduling/plugins/threadpoolctl.py
@override
def copy(self) -> Self:
    """Return a copy of the plugin.

    Please see [`Plugin.copy()`][amltk.Plugin.copy].
    """
    return self.__class__(max_threads=self.max_threads, user_api=self.user_api)