opto.trainer.utils¶
args_list
module-attribute
¶
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 ¶
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