Skip to content

opto.trainer.utils

runs module-attribute

runs = [tester] * 10

args_list module-attribute

args_list = [
    (3,),
    (3,),
    (2,),
    (3,),
    (3,),
    (2,),
    (2,),
    (3,),
    (2,),
    (3,),
]

kwargs_list module-attribute

kwargs_list = [{}] * 10

start module-attribute

start = time()

output module-attribute

output = async_run(
    runs, args_list, kwargs_list, max_workers=1
)

async_run

async_run(
    runs,
    args_list=None,
    kwargs_list=None,
    max_workers=None,
    description=None,
    allow_sequential_run=True,
)

Run multiple functions in asynchronously.

Args: runs (list): list of functions to run args_list (list): list of arguments for each function kwargs_list (list): list of keyword arguments for each function max_workers (int, optional): maximum number of worker threads to use. If None, the default ThreadPoolExecutor behavior is used. description (str, optional): description to display in the progress bar. This can indicate the current stage (e.g., "Evaluating", "Training", "Optimizing"). allow_sequential_run (bool, optional): if True, runs the functions sequentially if max_workers is 1.

batch_run

batch_run(max_workers=None, description=None)

Create a function that runs in parallel using asyncio, with support for batching. The batch size is inferred as the length of the longest argument or keyword argument.

Args: fun (callable): The function to run.

max_workers (int, optional): Maximum number of worker threads to use.
    If None, the default ThreadPoolExecutor behavior is used.
description (str, optional): Description to display in the progress bar.

Returns: callable: A new function that processes batches of inputs.

NOTE: If fun takes input that has len (like lists or arrays), they won't be broadcasted. When using batch_run, be sure to pass list of such arguments of the same length.

Example: >>> @batch_run(max_workers=4, description="Processing batch") >>> def my_function(x, y): >>> return x + y >>> x = [1, 2, 3, 4, 5] >>> y = 10 >>> outputs = my_function(x, y) >>> # outputs will be [11, 12, 13, 14, 15] >>> # This will run the function in asynchronously with 4 threads

tester

tester(t)