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.

  • 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.

  • 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


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

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


__call__(inputs, output, size_dict, memory_limit)

Call self as a function.



Implement delattr(self, name).


Default dir() implementation.


Return self==value.


Default object formatter.


Return self>=value.


Return getattr(self, name).


Return self>value.


Return hash(self).

__init__([max_repeats, max_time, minimize, …])

Initialize self.


This method is called when a class is subclassed.


Return self<=value.


Return self<value.


Return self!=value.


Create and return a new object.


Helper for pickle.


Helper for pickle.


Return repr(self).


Implement setattr(self, name, value).


Size of object in memory, in bytes.


Return str(self).


Abstract classes can override this to customize issubclass().


_gen_results_parallel(repeats, trial_fn, args)

Lazily generate results from an executor without submitting all jobs at once.

setup(inputs, output, size_dict)






list of weak references to the object (if defined)



The best path found so far.

property path

The best path found so far.