opt_einsum.path_random.RandomOptimizer

class opt_einsum.path_random.RandomOptimizer(max_repeats=32, max_time=None, minimize='flops', parallel=False, pre_dispatch=128)[source]

Base class for running any random path finder that benefits from repeated calling, possibly in a parallel fashion. Custom random optimizers should subclass this, and the setup method should be implemented with the following signature:

def setup(self, inputs, output, size_dict):
    # custom preparation here ...
    return trial_fn, trial_args

Where trial_fn itself should have the signature:

def trial_fn(r, *trial_args):
    # custom computation of path here
    return ssa_path, cost, size

Where r is the run number and could for example be used to seed a random number generator. See RandomGreedy for an example.

Parameters:
  • max_repeats (int, optional) – The maximum number of repeat trials to have.
  • max_time (float, optional) – The maximum amount of time to run the algorithm for.
  • minimize ({‘flops’, ‘size’}, optional) – Whether to favour paths that minimize the total estimated flop-count or the size of the largest intermediate created.
  • parallel ({bool, int, or executor-pool like}, optional) – Whether to parallelize the random trials, by default False. If True, use a concurrent.futures.ProcessPoolExecutor with the same number of processes as cores. If an integer is specified, use that many processes instead. Finally, you can supply a custom executor-pool which should have an API matching that of the python 3 standard library module concurrent.futures. Namely, a submit method that returns Future objects, themselves with result and cancel methods.
  • pre_dispatch (int, optional) – If running in parallel, how many jobs to pre-dispatch so as to avoid submitting all jobs at once. Should also be more than twice the number of workers to avoid under-subscription. Default: 128.
Variables:
  • path (list[tuple[int]]) – The best path found so far.
  • costs (list[int]) – The list of each trial’s costs found so far.
  • sizes (list[int]) – The list of each trial’s largest intermediate size so far.

See also

RandomGreedy

__init__(max_repeats=32, max_time=None, minimize='flops', parallel=False, pre_dispatch=128)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([max_repeats, max_time, minimize, …]) Initialize self.

Attributes

path The best path found so far.
path

The best path found so far.