§
<ˆjœÊ ã ó — d Z ddlZddlZddlmZ ddlmZmZ ddlm Z ddgZ
dZ ee¦ « Z
d „ Zd
„ Zd„ Zd„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zdddœd„Z e ed¬¦ « dddœd„¦ « Zdddœd„Z e ed¬¦ « dddœd„¦ « ZdS )z&
Implementation of optimized einsum.
é N)Úc_einsum)Ú
asanyarrayÚ tensordot)Úarray_function_dispatchÚeinsumÚeinsum_pathÚ4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZc ó` — 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_dictÚmax)Úidx_contractionÚinnerÚ num_termsÚsize_dictionaryÚoverall_sizeÚ op_factors úL/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/core/einsumfunc.pyÚ_flop_countr sB € õ> )¨¸/ÑJÔJ€LÝ�A�y 1‘}Ñ%Ô%€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 © )ÚindicesÚidx_dictÚretÚis 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'})
)ÚsetÚcopyÚ enumerateÚappend)
Ú positionsÚ
input_setsÚ
output_setÚidx_contractÚ
idx_remainÚ remainingÚindÚvalueÚ
new_resultÚidx_removeds
r Ú_find_contractionr, U s« € õV ‘5”5€LØ—’Ñ"Ô"€JØ€IÝ
Ñ+Ô+ð ð ‰
ˆˆUØ�)ÐÐØ˜EÑ!ˆLˆLà×Ò˜UÑ#Ô#Ð#ؘ%шJˆJà˜lÑ*€JØ *Ñ,€KØ
×Ò�ZÑ Ô Ð à˜ ;°Ð=Ð=r c ó — dg | fg}t t | ¦ « dz
¦ « D �]}g }|D ]¬}|\ }} }
t j t t | ¦ « |z
¦ « d¦ « D ]q}t ||
|¦ « }|\ }
}}}t |
|¦ « }||k rŒ1|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 ú