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.


__delattr__(name, /)

Implement delattr(self, name).


Default dir() implementation.

__eq__(value, /)

Return self==value.

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__gt__(value, /)

Return self>value.


Return hash(self).

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

Initialize self.


This method is called when a class is subclassed.

__le__(value, /)

Return self<=value.

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.


Create and return a new object.


Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.


Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).


Size of object in memory, in bytes.


Return str(self).


Abstract classes can override this to customize issubclass().


_check_args_against_first_call(inputs, …)

Utility that stateful optimizers can use to ensure they are not called with different contractions across separate runs.

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