y? d Z ddlZddlZddlmZ ddlmZmZ ddlm Z ddgZ
dZ ee Z
d Zd
Zd Zd Zd
Zd Zd Zd Zd ZddddZ e ed dddd ZddddZ e ed dddd ZdS )z&
Implementation of optimized einsum.
N)c_einsum)
asanyarray tensordot)array_function_dispatcheinsumeinsum_path4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc ` t | | }t d|dz
}|r|dz
}||z S )a
Computes the number of FLOPS in the contraction.
Parameters
----------
idx_contraction : iterable
The indices involved in the contraction
inner : bool
Does this contraction require an inner product?
num_terms : int
The number of terms in a contraction
size_dictionary : dict
The size of each of the indices in idx_contraction
Returns
-------
flop_count : int
The total number of FLOPS required for the contraction.
Examples
--------
>>> _flop_count('abc', False, 1, {'a': 2, 'b':3, 'c':5})
30
>>> _flop_count('abc', True, 2, {'a': 2, 'b':3, 'c':5})
60
)_compute_size_by_dictmax)idx_contractioninner num_termssize_dictionaryoverall_size op_factors i/builddir/build/BUILD/cloudlinux-venv-1.0.10/venv/lib64/python3.11/site-packages/numpy/core/einsumfunc.py_flop_countr sB > )/JJLAy1}%%I Q )## c * d}| D ]
}||| z }|S )a
Computes the product of the elements in indices based on the dictionary
idx_dict.
Parameters
----------
indices : iterable
Indices to base the product on.
idx_dict : dictionary
Dictionary of index sizes
Returns
-------
ret : int
The resulting product.
Examples
--------
>>> _compute_size_by_dict('abbc', {'a': 2, 'b':3, 'c':5})
90
r )indicesidx_dictretis r r r 8 s- . C
x{Jr c t }| }g }t | D ])\ }}|| v r||z }| | ||z }*||z }||z
} | | ||| |fS )a
Finds the contraction for a given set of input and output sets.
Parameters
----------
positions : iterable
Integer positions of terms used in the contraction.
input_sets : list
List of sets that represent the lhs side of the einsum subscript
output_set : set
Set that represents the rhs side of the overall einsum subscript
Returns
-------
new_result : set
The indices of the resulting contraction
remaining : list
List of sets that have not been contracted, the new set is appended to
the end of this list
idx_removed : set
Indices removed from the entire contraction
idx_contraction : set
The indices used in the current contraction
Examples
--------
# A simple dot product test case
>>> pos = (0, 1)
>>> isets = [set('ab'), set('bc')]
>>> oset = set('ac')
>>> _find_contraction(pos, isets, oset)
({'a', 'c'}, [{'a', 'c'}], {'b'}, {'a', 'b', 'c'})
# A more complex case with additional terms in the contraction
>>> pos = (0, 2)
>>> isets = [set('abd'), set('ac'), set('bdc')]
>>> oset = set('ac')
>>> _find_contraction(pos, isets, oset)
({'a', 'c'}, [{'a', 'c'}, {'a', 'c'}], {'b', 'd'}, {'a', 'b', 'c', 'd'})
)setcopy enumerateappend)
positions
input_sets
output_setidx_contract
idx_remain remainingindvalue
new_resultidx_removeds
r _find_contractionr, U s V 55L""JI
++
U)E!LLU###%JJl*J*,K
Z ;==r c dg | fg}t t | dz
D ]}g }|D ]}|\ }} }
t j t t | |z
d D ]q}t ||
| }|\ }
}}}t |
| }||k r1|t
||t | | z }| |gz }| |||f r|r|}t |d d }|t t t | |z
gz
}|c S t | dk r*t t t | gS t |d d }|S )a
Computes all possible pair contractions, sieves the results based
on ``memory_limit`` and returns the lowest cost path. This algorithm
scales factorial with respect to the elements in the list ``input_sets``.
Parameters
----------
input_sets : list
List of sets that represent the lhs side of the einsum subscript
output_set : set
Set that represents the rhs side of the overall einsum subscript
idx_dict : dictionary
Dictionary of index sizes
memory_limit : int
The maximum number of elements in a temporary array
Returns
-------
path : list
The optimal contraction order within the memory limit constraint.
Examples
--------
>>> isets = [set('abd'), set('ac'), set('bdc')]
>>> oset = set()
>>> idx_sizes = {'a': 1, 'b':2, 'c':3, 'd':4}
>>> _optimal_path(isets, oset, idx_sizes, 5000)
[(0, 2), (0, 1)]
r r c | d S Nr r xs r