§
    �Åj¿”  ã                   ój  — d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZmZ ej        rd dlZddlmZ ddlmZ ddlmZmZmZ  ed¦  «        Zeeeed	f         gef         Zeeef         Z ej        Z!d
Z"dZ#dZ$de	e         defd„Z%de de	e         fd„Z&d„ Z'ee
e	e                  e
e	e                  e	e         eeef         e
e	e                  ef         Z(eeeef         d	f         Z)e	eeef                  Z* G d„ d¦  «        Z+de+de	e         de*de(fd„Z,  e j-        d¦  «        e,¦  «        Z.de+dede de)def
d„Z/de+dede de)def
d„Z0 e j-        d ¦  «        d!ed"e d#eed	f         d$ede+f
d%„¦   «         Z1d!ed"e d#eed	f         deee+f         fd&„Z2ede	e         d!ed'e de!def
d(„¦   «         Z3eded!ed'e de!def
d)„¦   «         Z3deee	e         f         d!ed'e de!def
d*„Z3ede	e         d!ede!defd+„¦   «         Z4eded!ede!defd,„¦   «         Z4deee	e         f         d!ede!defd-„Z4ede	e         d!ede!defd.„¦   «         Z5eded!ede!defd/„¦   «         Z5deee	e         f         d!ede!defd0„Z5d1ed!ede6fd2„Z7d3„ Z8eZ9dede9fd4„Z:d5„ Z; e j-        d ¦  «        d!edefd6„¦   «         Z<ej        ded!edefd7„¦   «         Z=ej        d8ed9ed!edefd:„¦   «         Z=ej        d8ed9ed;ed!edef
d<„¦   «         Z=ej        d8ed9ed;ed=ed!edefd>„¦   «         Z=d?eeef         defd@„Z=dS )Aé    N)ÚOrderedDict)ÚAnyÚCallableÚDictÚListÚOptionalÚSetÚTupleÚTypeVarÚUnionÚcastÚoverloadé   )ÚEinopsError)Úget_backend)ÚAnonymousAxisÚParsedExpressionÚ	_ellipsisÚTensor.)ÚminÚmaxÚsumÚmeanÚprodÚanyÚalliÁ½ðÿiayþÿÚsequenceÚreturnc                 ó   — d}| D ]}||z  }Œ|S )zSminimalistic product that works both with numbers and symbols. Supports empty listsr   © )r   ÚresultÚelements      ú>C:\Projects\Historical\venv\Lib\site-packages\einops/einops.pyÚ_productr$      s'   € à€FØð ð ˆØ�'ÑˆˆØ€Mó    Úreduction_typeÚreduced_axesc                 ó  — t          |¦  «        r || t          |¦  «        ¦  «        S |t          v sJ ‚|dk    r$|                     | ¦  «        st	          d¦  «        ‚|                     | |t          |¦  «        ¦  «        S )Nr   z5reduce_mean is not available for non-floating tensors)ÚcallableÚtupleÚ_reductionsÚis_float_typeÚNotImplementedErrorÚreduce)Útensorr&   r'   Úbackends       r#   Ú_reduce_axesr1   %   s�   € Ý�ÑÔð 	Kàˆ~˜f¥e¨LÑ&9Ô&9Ñ:Ô:Ð:ð ¥Ð,Ð,Ð,Ð,Ø˜VÒ#Ð#Ø×(Ò(¨Ñ0Ô0ð cÝ)Ð*aÑbÔbÐbØ�~Š~˜f nµe¸LÑ6IÔ6IÑJÔJÐJr%   c                 ó|  ‡ ‡‡‡— t          ‰¦  «        t          ‰¦  «        z   t          ‰ ¦  «        k    sJ ‚t          t          ‰¦  «        ¦  «        Št          t          ‰¦  «        dz
  ¦  «        d d d…         D ]Š}‰|         dz   ‰|dz            k    rp‰|dz            Š‰ ‰         }‰ d ‰…         ‰ ‰dz   d …         z   Š ‰ ‰dz
  xx         |z  cc<   ‰d |dz   …         t          d„ ‰|dz   d …         D ¦   «         ¦  «        z   ŠŒ‹ˆˆ ˆfd„} |¦   «         }t          t          ‰ ¦  «        dz
  ¦  «        d d d…         D ]ø}||         €Œ||dz            €Œ||         dz   ||dz            k    rÉ|dz   Š‰ ‰         }t	          ˆfd„t          ‰¦  «        D ¦   «         ¦  «        }	t          ˆfd„‰D ¦   «         ¦  «        Š‰ d ‰…         ‰ ‰dz   d …         z   Š ‰ ‰dz
  xx         |z  cc<   ‰}
g Š|
D ]=}||	k    rŒ	||	k     r‰                     |¦  «         Œ%‰                     |dz
  ¦  «         Œ> |¦   «         }Œù‰ ‰‰|fS )Nr   éÿÿÿÿc              3   ó    K  — | ]	}|d z
  V — Œ
dS ©r   Nr    )Ú.0Úaxiss     r#   ú	<genexpr>z+_optimize_transformation.<locals>.<genexpr>?   s&   è è € Ð8dÐ8dÀd¸À¹Ð8dÐ8dÐ8dÐ8dÐ8dÐ8dr%   é   c                  óþ   •— i } t          t          ‰¦  «        ¦  «        D ]\}|‰v rd | |<   Œt          d„ |                      ¦   «         D ¦   «         ¦  «        }t	          ‰¦  «                             |¦  «        | |<   Œ]| S )Nc              3   ó   K  — | ]}|d uV — Œ	d S ©Nr    )r6   Úxs     r#   r8   zB_optimize_transformation.<locals>.build_mapping.<locals>.<genexpr>H   s&   è è € Ð%TÐ%T¸ a¨t mÐ%TÐ%TÐ%TÐ%TÐ%TÐ%Tr%   )ÚrangeÚlenr   ÚvaluesÚlistÚindex)Úinit_to_finalr7   Úafter_reductionÚaxes_reorderingÚinit_shapesr'   s      €€€r#   Úbuild_mappingz/_optimize_transformation.<locals>.build_mappingB   s’   ø€ ØˆÝ�#˜kÑ*Ô*Ñ+Ô+ð 	Sð 	SˆDØ�|Ð#Ð#Ø&*�˜dÑ#Ð#å"%Ð%TÐ%T¸]×=QÒ=QÑ=SÔ=SÐ%TÑ%TÔ%TÑ"TÔ"T�Ý&*¨?Ñ&;Ô&;×&AÒ&AÀ/Ñ&RÔ&R�˜dÑ#Ð#ØÐr%   c              3   ó    •K  — | ]}|‰vV — Œ	d S r<   r    )r6   r=   r'   s     €r#   r8   z+_optimize_transformation.<locals>.<genexpr>V   s(   øè è € Ð.bÐ.bÈ¨q¸Ð/DÐ.bÐ.bÐ.bÐ.bÐ.bÐ.br%   c              3   ó2   •K  — | ]}|‰k     r|n|d z
  V — ŒdS r5   r    )r6   r7   Úremoved_axiss     €r#   r8   z+_optimize_transformation.<locals>.<genexpr>X   s5   øè è € Ð dÐ dÈt¨°Ò)<Ð)<  À$ÈÁ(Ð dÐ dÐ dÐ dÐ dÐ dr%   )r?   r*   Úsortedr>   r   Úappend)rF   r'   rE   Úfinal_shapesÚiÚremoved_lengthrG   Úinit_axis_to_final_axisÚ	init_axisÚremoved_axis_after_reductionÚold_reorderingr7   rJ   s   ```         @r#   Ú_optimize_transformationrT   2   s  øøøø€ õ ˆÑÔ¥# lÑ"3Ô"3Ñ3µs¸;Ñ7GÔ7GÒGÐGÐGÐGõ � Ñ-Ô-Ñ.Ô.€LÝ•3�|Ñ$Ô$ qÑ(Ñ)Ô)¨$¨$¨B¨$Ô/ð eð eˆØ˜Œ?˜QÑ ,¨q°1©uÔ"5Ò5Ð5Ø'¨¨A©Ô.ˆLØ(¨Ô6ˆNØ% m | mÔ4°{À<ÐRSÑCSÐCUÐCUÔ7VÑVˆKØ˜ qÑ(Ð)Ð)Ô)¨^Ñ;Ð)Ð)Ñ)Ø'¨¨!¨a©%¨Ô0µ5Ð8dÐ8dÈlÐ[\Ð_`Ñ[`Ð[bÐ[bÔNcÐ8dÑ8dÔ8dÑ3dÔ3dÑdˆLøðð ð ð ð ð ð ð ,˜m™oœoÐå�3˜{Ñ+Ô+¨aÑ/Ñ0Ô0°°°2°Ô6ð 6ð 6ˆ	Ø" 9Ô-Ð5ØØ" 9¨q¡=Ô1Ð9ØØ" 9Ô-°Ñ1Ð5LÈYÐYZÉ]Ô5[Ò[Ð[Ø$ q™=ˆLØ(¨Ô6ˆNÝ+.Ð.bÐ.bÐ.bÐ.bÍeÐT`ÑNaÔNaÐ.bÑ.bÔ.bÑ+bÔ+bÐ(å Ð dÐ dÐ dÐ dÐWcÐ dÑ dÔ dÑdÔdˆLØ% m | mÔ4°{À<ÐRSÑCSÐCUÐCUÔ7VÑVˆKØ˜ qÑ(Ð)Ð)Ô)¨^Ñ;Ð)Ð)Ñ)Ø,ˆNØ ˆOØ&ð 5ð 5�ØÐ7Ò7Ð7ØØÐ8Ò8Ð8Ø#×*Ò*¨4Ñ0Ô0Ð0Ð0à#×*Ò*¨4°!©8Ñ4Ô4Ð4Ð4Ø&3 m¡o¤oÐ#øà˜ o°|ÐCÐCr%   c                   óº   — e Zd ZdZdee         deeef         deeee         ee         f                  dee         dedeeef         deee                  fd	„Z	d
S )ÚTransformRecipezi
    Recipe describes actual computation pathway.
    Recipe can be applied to a tensor or variable.
    Úelementary_axes_lengthsÚaxis_name2elementary_axisÚinput_composition_known_unknownÚaxes_permutationÚfirst_reduced_axisÚ
added_axesÚoutput_composite_axesc                 óh   — || _         || _        || _        || _        || _        || _        || _        d S r<   ©rW   rX   rY   rZ   r[   r\   r]   )ÚselfrW   rX   rY   rZ   r[   r\   r]   s           r#   Ú__init__zTransformRecipe.__init__{   sB   € ð, 3JˆÔ$Ø9RˆÔ&ØRqˆÔ,Ø+;ˆÔà'9ˆÔØ*4ˆŒØ6KˆÔ"Ð"Ð"r%   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úintr   Ústrr
   ra   r    r%   r#   rV   rV   r   s¼   € € € € € ðð ðLð
 "& c¤ðLð $(¨¨S¨¤>ðLð *.¨e°D¸´I¸tÀC¼yÐ4HÔ.IÔ)JðLð ˜sœ)ðLð  ðLð" ˜˜c˜”Nð#Lð(  $ D¨¤Iœð)Lð Lð Lð Lð Lð Lr%   rV   r`   ÚshapeÚ	axes_dimsc                 ób  ‡— d}t          | j        ¦  «        Š|D ]\  }}|‰| j        |         <   Œt          | j        ¦  «        D �]-\  }\  }}||         }	t          |¦  «        dk    rt          |¦  «        dk    r|	‰|d         <   ŒCd}
|D ]}|
‰|         z  }
Œt          |¦  «        dk    rFt          |	t          ¦  «        r0t          |
t          ¦  «        r|	|
k    rt          d|	› d|
› �¦  «        ‚nZt          |	t          ¦  «        r3t          |
t          ¦  «        r|	|
z  dk    rt          d|	› d|
› �¦  «        ‚|d         }|	|
z  }|‰|<   t          |¦  «        t          |¦  «        z   dk    rd}�Œ/|r‰d	t          | j	        ¦  «        …         nd	}d}g }| j
        D ]G}ˆfd
„|D ¦   «         }|                     t          |¦  «        ¦  «         t          |¦  «        dk    rd}ŒHˆfd„| j                             ¦   «         D ¦   «         }t          t          | j        t          | j	        ¦  «        ¦  «        ¦  «        }t          |¦  «        t          | j	        ¦  «        z   }| j	        }| j	        t          t          t          | j	        ¦  «        ¦  «        ¦  «        k    rd	}|r|nd	}||||||fS )zä
    Reconstruct all actual parameters using shape.
    Shape is a tuple that may contain integers, shape symbols (tf, theano) and UnknownSize (tf, previously mxnet)
    known axes can be integers or symbols, but not Nones.
    Fr   r   zShape mismatch, z != z,Shape mismatch, can't divide axis of length z in chunks of TNc                 ó    •— g | ]
}‰|         ‘ŒS r    r    )r6   Úelementary_axisÚaxes_lengthss     €r#   ú
<listcomp>z4_reconstruct_from_shape_uncached.<locals>.<listcomp>Í   s   ø€ ÐQÐQÐQ°_�< Ô0ÐQÐQÐQr%   c                 ó(   •— i | ]\  }}|‰|         “ŒS r    r    )r6   ÚposÚpos_in_elementaryrm   s      €r#   ú
<dictcomp>z4_reconstruct_from_shape_uncached.<locals>.<dictcomp>Ò   s3   ø€ ð "ð "ð "Ù1G°Ð6Gˆˆ\Ð+Ô,ð"ð "ð "r%   )rA   rW   rX   Ú	enumeraterY   r?   Ú
isinstancerf   r   rZ   r]   rL   r$   r\   Úitemsr>   r[   )r`   rh   ri   Úneed_init_reshaper7   ÚdimÚ
input_axisÚ
known_axesÚunknown_axesÚlengthÚknown_productÚunknown_axisÚinferred_lengthrF   Úneed_final_reshaperM   ÚgroupingÚlengthsr\   r'   Ún_axes_after_adding_axesrE   Ú_final_shapesrm   s                          @r#   Ú _reconstruct_from_shape_uncachedr„   ›   s1  ø€ ð Ðõ # 4Ô#?Ñ@Ô@€LØð Að A‰	ˆˆcØ=@ˆ�TÔ3°DÔ9Ñ:Ð:å2;¸DÔ<`Ñ2aÔ2að %ñ %Ñ.ˆ
Ñ.�Z Ø�zÔ"ˆÝˆz‰?Œ?˜aÒÐ¥C¨Ñ$5Ô$5¸Ò$:Ð$:à,2ˆL˜ aœÑ)ØàˆØð 	0ð 	0ˆDØ˜\¨$Ô/Ñ/ˆMˆMåˆ|ÑÔ Ò!Ð!Ý˜&¥#Ñ&Ô&ð R­:°mÅSÑ+IÔ+Ið RÈfÐXeÒNeÐNeÝ!Ð"P°VÐ"PÐ"PÀÐ"PÐ"PÑQÔQÐQøõ ˜&¥#Ñ&Ô&ð x­:°mÅSÑ+IÔ+Ið xÈfÐWdÑNdÐhiÒNiÐNiÝ!Ð"vÐQWÐ"vÐ"vÐgtÐ"vÐ"vÑwÔwÐwà'¨œ?ˆLØ#)¨]Ñ#:ˆOØ)8ˆL˜Ñ&åˆz‰?Œ?�S Ñ.Ô.Ñ.°!Ò3Ð3Ø $Ðùð
 VgÐ'p |Ð4Pµc¸$Ô:OÑ6PÔ6PÐ4PÔ'QÐ'QÐlp€KàÐØ €LØÔ.ð &ð &ˆØQÐQÐQÐQÈÐQÑQÔQˆØ×Ò�H WÑ-Ô-Ñ.Ô.Ð.Ýˆw‰<Œ<˜1ÒÐØ!%Ðøð"ð "ð "ð "ØKOÌ?×K`ÒK`ÑKbÔKbð"ñ "ô "€Jõ
 �˜dÔ5µs¸4Ô;PÑ7QÔ7QÑRÔRÑSÔS€Lå" :™œµ°TÔ5JÑ1KÔ1KÑKÐà+/Ô+@€OØÔ¥¥U­3¨tÔ/DÑ+EÔ+EÑ%FÔ%FÑ GÔ GÒGÐGØˆà$6Ð@�L�L¸D€MØ˜¨°zÀ=ÐRjÐjÐjr%   i   Úreciper/   rm   c                 ó  — 	 t          ||                      |¦  «        |¦  «        \  }}}}}	}
n=# t          $ r0 t          ||                      |¦  «        |¦  «        }|\  }}}}}	}
Y nw xY w|�|                      ||¦  «        }|�|                      ||¦  «        }t          |¦  «        dk    rt          |||| ¬¦  «        }t          |¦  «        dk    r|                      ||
|¬¦  «        }|	�|                      ||	¦  «        }|S )Nr   )r&   r'   r0   )Ún_axesÚpos2len)	Ú_reconstruct_from_shaperh   Ú	TypeErrorr„   ÚreshapeÚ	transposer?   r1   Úadd_axes)r0   r…   r/   r&   rm   rF   rE   r'   r\   rM   Ún_axes_w_addedÚ_results               r#   Ú_apply_reciper�   æ   s:  € ðiÝ_vØ�G—M’M &Ñ)Ô)¨<ñ`
ô `
Ñ\ˆ�_ l°JÀÈnÈnøõ ð ið ið iå2°6¸7¿=º=ÈÑ;PÔ;PÐR^Ñ_Ô_ˆØahÑ^ˆ�o |°ZÀÈ~È~È~ðiøøøð ÐØ—’ ¨Ñ5Ô5ˆØÐ"Ø×"Ò" 6¨?Ñ;Ô;ˆÝ
ˆ<ÑÔ˜1ÒÐÝ˜f°^ÐR^ÐhoÐpÑpÔpˆÝ
ˆ:�„˜ÒÐØ×!Ò! &°ÈÐ!ÑTÔTˆØÐØ—’ ¨Ñ6Ô6ˆØ€Ms   ‚+. ®7A(Á'A(c                 óÈ  — t          ||j        |¦  «        \  }}}}}	}
|�|                      ||¦  «        }|�|                      ||¦  «        }t	          |¦  «        dk    r\t          |¦  «        r ||t          |¦  «        ¦  «        }n3|t          v sJ ‚ t          | |¦  «        |t          |¦  «        ¬¦  «        }t	          |¦  «        dk    rz| 	                    ¦   «         D ]\  }}|  
                    ||¬¦  «        }Œt          |j        ¦  «        }| 	                    ¦   «         D ]
\  }}|||<   Œ|                      ||¦  «        }|	�|                      ||	¦  «        }|S )Nr   )r7   )r‰   rh   r‹   Úpermute_dimsr?   r)   r*   r+   Úgetattrru   Úexpand_dimsrA   Úbroadcast_to)Úxpr…   r/   r&   rm   rF   rE   r'   r\   rM   rŽ   Úaxis_positionÚ_axis_lengthÚfinal_shapeÚaxis_lengths                  r#   Ú_apply_recipe_array_apir›   ÿ   s�  € õ \sØ�”˜lñ\ô \ÑX€K� ,°
¸LÈ.ð ÐØ—’˜F KÑ0Ô0ˆØÐ"Ø—’ ¨Ñ9Ô9ˆÝ
ˆ<ÑÔ˜1ÒÐÝ�NÑ#Ô#ð 	Sà#�^ F­E°,Ñ,?Ô,?Ñ@Ô@ˆFˆFð "¥[Ð0Ð0Ð0Ð0Ø0•W˜R Ñ0Ô0°½eÀLÑ>QÔ>QÐRÑRÔRˆFÝ
ˆ:�„˜ÒÐà+5×+;Ò+;Ñ+=Ô+=ð 	@ð 	@Ñ'ˆM˜<Ø—^’^ F°�^Ñ?Ô?ˆFˆFå˜6œ<Ñ(Ô(ˆØ*4×*:Ò*:Ñ*<Ô*<ð 	5ð 	5Ñ&ˆM˜;Ø)4ˆK˜Ñ&Ð&à—’ ¨Ñ5Ô5ˆØÐØ—’˜F LÑ1Ô1ˆØ€Mr%   é   ÚpatternÚ	operationÚ
axes_namesÚndimc           	      ó�  ‡‡‡ ‡!‡"— |                       d¦  «        \  }}t          |¦  «        Š t          |¦  «        Š"‰ j        s‰"j        rt          d| › �¦  «        ‚‰ j        r‰ j        rt          d| › �¦  «        ‚|dk    ri‰ j        s‰"j        rt          d¦  «        ‚t                               ‰ j        ‰"j        ¦  «        }t          |¦  «        dk    rt          d|› �¦  «        ‚�n*|dk    r£t           
                    ‰ j        ‰"j        ¦  «        }t          |¦  «        dk    rt          d	|› �¦  «        ‚t           
                    d
„ ‰"j        D ¦   «         h ‰ j        £|£¦  «        }t          |¦  «        dk    rt          d|› �¦  «        ‚n�|t          v st          |¦  «        rNt           
                    ‰"j        ‰ j        ¦  «        }t          |¦  «        dk    rt          d|› d|› �¦  «        ‚nt          d|› dt          › d�¦  «        ‚‰ j        �r˜t          ‰ j        ¦  «        dz
  }||k     rt          d|› d|› d�¦  «        ‚||z
  }	d„ t          |	¦  «        D ¦   «         }
g }‰ j        D ]>}|t          k    r|
D ]}|                     |g¦  «         ŒŒ)|                     |¦  «         Œ?g }‰"j        D ]{}|t          k    r|
D ]}|                     |g¦  «         ŒŒ)g }|D ]8}|t          k    r|                     |
¦  «         Œ#|                     |¦  «         Œ9|                     |¦  «         Œ|‰ j                             |
¦  «         ‰ j                             t          ¦  «         ‰"j        r9‰"j                             |
¦  «         ‰"j                             t          ¦  «         nN|t          ‰ j        ¦  «        k    r(t          dt          ‰ j        ¦  «        › d|› d�¦  «        ‚‰ j        }‰"j        }t)          ¦   «         Š|D ]1}|D ],}t+          |t,          ¦  «        r|j        ‰|<   Œ"t0          ‰|<   Œ-Œ2g }‰"j        D ]E}|‰vr?t+          |t,          ¦  «        r|j        ‰|<   n
t0          ‰|<   |                     |¦  «         ŒFd„ t3          ‰¦  «        D ¦   «         Š|D ]G}t          j        |¦  «        st          d|¦  «        ‚|‰vrt          d|› d�¦  «        ‚t6          ‰|<   ŒHg }|D ]¤}ˆfd„|D ¦   «         }ˆfd„|D ¦   «         }t          |¦  «        dk    rt          d|› �¦  «        ‚t          |¦  «        t          |¦  «        z   t          |¦  «        k    sJ ‚|                     ˆfd„|D ¦   «         ˆfd„|D ¦   «         f¦  «         Œ¥i }t9          j        |Ž D ]}|‰"j        v rt          |¦  «        ||<   Œˆfd „t3          |¦  «        D ¦   «         }t=          t9          j        |Ž ¦  «        Š!t=          t9          j        |Ž ¦  «        }ˆ"fd!„‰!D ¦   «         }ˆ fd"„|D ¦   «         |z   }ˆ!fd#„|D ¦   «         }ˆˆ fd$„t3          |¦  «        D ¦   «         }t          |¦  «        t          |¦  «        z
  }t?          t=          ‰                      ¦   «         ¦  «        ˆfd%„|D ¦   «         |||||¬&¦  «        S )'z†Perform initial parsing of pattern and provided supplementary info
    axes_lengths is a tuple of tuples (axis_name, axis_length)
    ú->z=Ellipsis found in right side, but not left side of a pattern z=Ellipsis inside parenthesis in the left side is not allowed: Ú	rearrangezQNon-unitary anonymous axes are not supported in rearrange (exception is length 1)r   z@Identifiers only on one side of expression (should be on both): Úrepeatz3Unexpected identifiers on the left side of repeat: c                 ó<   — h | ]}t          |t          ¦  «        °|’ŒS r    )rt   r   )r6   Úaxs     r#   ú	<setcomp>z1_prepare_transformation_recipe.<locals>.<setcomp>?  s'   € ÐPÐPÐP�Bµ*¸RÅÑ2OÔ2OÐPˆRÐPÐPÐPr%   z&Specify sizes for new axes in repeat: z3Unexpected identifiers on the right side of reduce z: zUnknown reduction z. Expect one of ú.r   zWrong shape: expected >=z dims. Received z-dim tensor.c                 ó<   — g | ]}t           t          |¦  «        z   ‘ŒS r    )r   rg   )r6   rN   s     r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>P  s#   € ÐEÐEÐE¨1•I¥ A¡¤Ñ&ÐEÐEÐEr%   zWrong shape: expected c                 ó   — i | ]\  }}||“Œ	S r    r    )r6   ÚpositionÚnames      r#   rr   z2_prepare_transformation_recipe.<locals>.<dictcomp>†  s   € ÐaÐaÐa©^¨X°t˜$ ÐaÐaÐar%   zInvalid name for an axiszAxis z is not used in transformc                 ó6   •— h | ]}‰|         t           k    ¯|’ŒS r    ©Ú_unknown_axis_length©r6   r7   Úaxis_name2known_lengths     €r#   r§   z1_prepare_transformation_recipe.<locals>.<setcomp>“  s+   ø€ ÐsÐsÐs DÐ>TÐUYÔ>ZÕ^rÒ>rÐ>r˜4Ð>rÐ>rÐ>rr%   c                 ó6   •— h | ]}‰|         t           k    ¯|’ŒS r    r®   r°   s     €r#   r§   z1_prepare_transformation_recipe.<locals>.<setcomp>”  s+   ø€ ÐuÐuÐu dÐ@VÐW[Ô@\Õ`tÒ@tÐ@t˜TÐ@tÐ@tÐ@tr%   zCould not infer sizes for c                 ó    •— g | ]
}‰|         ‘ŒS r    r    ©r6   r7   Úaxis_name2positions     €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>™  s   ø€ Ð9Ð9Ð9¨4Ð  Ô&Ð9Ð9Ð9r%   c                 ó    •— g | ]
}‰|         ‘ŒS r    r    r´   s     €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>™  s    ø€ Ð;iÐ;iÐ;iÐY]Ð<NÈtÔ<TÐ;iÐ;iÐ;ir%   c                 ó2   •— g | ]\  }}ˆfd „|D ¦   «         ‘ŒS )c                 ó    •— g | ]
}‰|         ‘ŒS r    r    r´   s     €r#   rn   z=_prepare_transformation_recipe.<locals>.<listcomp>.<listcomp>¢  s   ø€ Ð=Ð=Ð= dÐ	˜DÔ	!Ð=Ð=Ð=r%   r    )r6   rN   Úcomposite_axisrµ   s      €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>¡  s?   ø€ ð -ð -ð -ÙBSÀ!À^Ð=Ð=Ð=Ð=¨nÐ=Ñ=Ô=ð-ð -ð -r%   c                 ó&   •— g | ]}|‰j         v¯|‘ŒS r    ©Úidentifiers)r6   r7   Úrghts     €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>§  s&   ø€ ÐWÐWÐW˜T¸$ÀdÔFVÐ:VÐ:V�DÐ:VÐ:VÐ:Vr%   c                 ó&   •— g | ]}|‰j         v ¯|‘ŒS r    r»   )r6   r7   Úlefts     €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>¨  s&   ø€ Ð `Ð `Ð `¨$ÀtÈtÔO_ÐG_ÐG_ ÐG_ÐG_ÐG_r%   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r    )rB   )r6   r7   Úordered_axis_lefts     €r#   rn   z2_prepare_transformation_recipe.<locals>.<listcomp>©  s(   ø€ Ð\Ð\Ð\¸$Ð)×/Ò/°Ñ5Ô5Ð\Ð\Ð\r%   c                 ó:   •— i | ]\  }}|‰j         v¯|‰|         “ŒS r    r»   )r6   rN   Ú	axis_namerµ   r¿   s      €€r#   rr   z2_prepare_transformation_recipe.<locals>.<dictcomp>ª  s=   ø€ ð ð ð áˆAˆyØ˜DÔ,Ð,Ð,ð 	
Ð˜iÔ(à,Ð,Ð,r%   c                 ó"   •— i | ]}|‰|         “ŒS r    r    r´   s     €r#   rr   z2_prepare_transformation_recipe.<locals>.<dictcomp>´  s!   ø€ Ð"YÐ"YÐ"YÀd 4Ð);¸DÔ)AÐ"YÐ"YÐ"Yr%   r_   )!Úsplitr   Úhas_ellipsisr   Úhas_ellipsis_parenthesizedÚhas_non_unitary_anonymous_axesÚsetÚsymmetric_differencer¼   r?   Ú
differencer+   r)   Úcompositionr>   r   rL   ÚextendÚupdateÚremover   rt   r   Úvaluer¯   rs   Úcheck_axis_nameÚ_expected_axis_lengthÚ	itertoolsÚchainrA   rV   r@   )#r�   rž   rŸ   r    Úleft_strÚrght_strrË   Úaxes_without_sizeÚn_other_dimsÚellipsis_ndimÚell_axesÚleft_compositionr¹   r7   Úrght_compositionÚgrouprÃ   Úrepeat_axes_namesrl   Úinput_axes_known_unknownÚknownÚunknownÚaxis_position_after_reductionÚresult_axes_groupingÚordered_axis_rghtr'   Úorder_after_transpositionrZ   r\   r[   r±   rµ   r¿   rÁ   r½   s#                                 @@@@@r#   Ú_prepare_transformation_reciperæ   !  s‡  øøøøø€ ð !Ÿš tÑ,Ô,Ñ€HˆhÝ˜HÑ%Ô%€DÝ˜HÑ%Ô%€Dð Ôð e Ô!2ð eÝÐcÐZaÐcÐcÑdÔdÐdØÔð e˜TÔ<ð eÝÐcÐZaÐcÐcÑdÔdÐdØ�KÒÐØÔ.ð 	s°$Ô2Uð 	sÝÐqÑrÔrÐrÝ×-Ò-¨dÔ.>ÀÔ@PÑQÔQˆ
Ýˆz‰?Œ?˜QÒÐÝÐmÐakÐmÐmÑnÔnÐnñ à	�hÒ	Ð	Ý—^’^ DÔ$4°dÔ6FÑGÔGˆ
Ýˆz‰?Œ?˜QÒÐÝÐ`ÐT^Ð`Ð`ÑaÔaÐaÝŸNšNØPÐP˜$Ô*ÐPÑPÔPØ,ˆdÔÐ, Ð,ñ
ô 
Ðõ Ð Ñ!Ô! AÒ%Ð%ÝÐZÐGXÐZÐZÑ[Ô[Ð[ð &à	•kÐ	!Ð	!¥X¨iÑ%8Ô%8Ð	!Ý—^’^ DÔ$4°dÔ6FÑGÔGˆ
Ýˆz‰?Œ?˜QÒÐÝÐmÐT]ÐmÐmÐakÐmÐmÑnÔnÐnð õ ÐX¨yÐXÐXÍ+ÐXÐXÐXÑYÔYÐYàÔñ %,Ý˜4Ô+Ñ,Ô,¨qÑ0ˆØ�,ÒÐÝÐi¸ÐiÐiÐW[ÐiÐiÐiÑjÔjÐjØ˜|Ñ+ˆØEÐEµ°mÑ0DÔ0DÐEÑEÔEˆØÐØ"Ô.ð 	8ð 	8ˆNØ¥Ò*Ð*Ø$ð 4ð 4�DØ$×+Ò+¨T¨FÑ3Ô3Ð3Ð3ð4ð !×'Ò'¨Ñ7Ô7Ð7Ð7àÐØ"Ô.ð 	/ð 	/ˆNØ¥Ò*Ð*Ø$ð 4ð 4�DØ$×+Ò+¨T¨FÑ3Ô3Ð3Ð3ð4ð �Ø*ð +ð +�DØ�yÒ(Ð(ØŸš XÑ.Ô.Ð.Ð.àŸš TÑ*Ô*Ð*Ð*Ø ×'Ò'¨Ñ.Ô.Ð.Ð.àÔ×Ò Ñ)Ô)Ð)ØÔ×Ò¥	Ñ*Ô*Ð*ØÔð 	/ØÔ×#Ò# HÑ-Ô-Ð-ØÔ×#Ò#¥IÑ.Ô.Ð.øà•3�tÔ'Ñ(Ô(Ò(Ð(ÝÐpµs¸4Ô;KÑ7LÔ7LÐpÐpÐ^bÐpÐpÐpÑqÔqÐqØÔ+ÐØÔ+Ðõ DOÁ=Ä=ÐØ*ð Ið IˆØ'ð 	Ið 	IˆIÝ˜)¥]Ñ3Ô3ð IØ4=´OÐ& yÑ1Ð1å4HÐ& yÑ1Ð1ð		Ið ÐØÔ%ð 0ð 0ˆ	ØÐ2Ð2Ð2Ý˜)¥]Ñ3Ô3ð IØ4=´OÐ& yÑ1Ð1å4HÐ& yÑ1Ø×$Ò$ YÑ/Ô/Ð/øàaÐa½yÐI_Ñ?`Ô?`ÐaÑaÔaÐð &ð Hð HˆÝÔ/°Ñ@Ô@ð 	KÝÐ8¸/ÑJÔJÐJØÐ"8Ð8Ð8ÝÐP oÐPÐPÐPÑQÔQÐQÝ2GÐ˜Ñ/Ð/à!Ðà*ð 
ð 
ˆØsÐsÐsÐs¨NÐsÑsÔsˆØuÐuÐuÐu¨nÐuÑuÔuˆÝˆw‰<Œ<˜!ÒÐÝÐD¸7ÐDÐDÑEÔEÐEÝ�7‰|Œ|�c %™jœjÑ(­C°Ñ,?Ô,?Ò?Ð?Ð?Ð?Ø ×'Ò'Ø9Ð9Ð9Ð9°5Ð9Ñ9Ô9Ð;iÐ;iÐ;iÐ;iÐahÐ;iÑ;iÔ;iÐjñ	
ô 	
ð 	
ð 	
ð 57Ð!Ý”_Ð&6Ð7ð Zð Zˆ	Ø˜Ô(Ð(Ð(Ý7:Ð;XÑ7YÔ7YÐ)¨)Ñ4øð-ð -ð -ð -ÝW`ÐaqÑWrÔWrð-ñ -ô -Ðõ �Yœ_Ð.>Ð?Ñ@Ô@ÐÝ�Yœ_Ð.>Ð?Ñ@Ô@ÐØWÐWÐWÐWÐ%6ÐWÑWÔW€LØ `Ð `Ð `Ð `Ð2CÐ `Ñ `Ô `ÐcoÑ oÐØ\Ð\Ð\Ð\ÐB[Ð\Ñ\Ô\Ððð ð ð ð å%Ð&7Ñ8Ô8ðñ ô €Jõ Ð6Ñ7Ô7½#¸lÑ:KÔ:KÑKÐåÝ $Ð%;×%BÒ%BÑ%DÔ%DÑ EÔ EØ"YÐ"YÐ"YÐ"YÈjÐ"YÑ"YÔ"YØ(@Ø)Ø-ØØ2ðñ ô ð r%   c                 óæ   ‡ ‡‡‡— ‰                       d¦  «        \  }}t          |¦  «        Št          ‰j        ¦  «        g}‰j        rˆfd„t          d¦  «        D ¦   «         }ˆˆˆ fd„|D ¦   «         S )z 
    Internal function, used in layers.
    Layer makes all recipe creation when it is initialized, thus to keep recipes simple we pre-compute for all dims
    r¢   c                 óD   •— g | ]}t          ‰j        ¦  «        d z
  |z   ‘ŒS )r   )r?   rÌ   )r6   Úellipsis_dimsr¿   s     €r#   rn   z1_prepare_recipes_for_all_dims.<locals>.<listcomp>È  s.   ø€ ÐXÐXÐX¸m•�DÔ$Ñ%Ô%¨Ñ)¨MÑ9ÐXÐXÐXr%   é   c           	      ó8   •— i | ]}|t          ‰‰‰|¬ ¦  «        “ŒS ))r    )ræ   )r6   r    rŸ   rž   r�   s     €€€r#   rr   z1_prepare_recipes_for_all_dims.<locals>.<dictcomp>É  s/   ø€ ÐmÐmÐmÐ`dˆDÕ0°¸)ÀZÐVZÐ[Ñ[Ô[ÐmÐmÐmr%   )rÅ   r   r?   rÌ   rÆ   r>   )r�   rž   rŸ   rÕ   rÖ   Údimsr¿   s   ```   @r#   Ú_prepare_recipes_for_all_dimsrí   ½  s‹   øøøø€ ð !Ÿš tÑ,Ô,Ñ€HˆhÝ˜HÑ%Ô%€DÝ�Ô Ñ!Ô!Ð"€DØÔð YØXÐXÐXÐXÍuÐUVÉxÌxÐXÑXÔXˆØmÐmÐmÐmÐmÐmÐhlÐmÑmÔmÐmr%   Ú	reductionc                 ó   — d S r<   r    ©r/   r�   rî   rm   s       r#   r.   r.   Ì  s   € ØfiÐfir%   c                 ó   — d S r<   r    rð   s       r#   r.   r.   Ð  s   € Ø`cÐ`cr%   c                 ó¶  — 	 t          | t          ¦  «        rMt          | ¦  «        dk    rt          d¦  «        ‚t	          | d         ¦  «        }|                     | ¦  «        } nt	          | ¦  «        }t          |                     ¦   «         ¦  «        }|                     | ¦  «        }t          ||t          |¦  «        t          |¦  «        ¬¦  «        }t          ||t          t          | ¦  «        ||¬¦  «        S # t          $ rQ}d|› d|› d�}	t          | t          ¦  «        s
|	d|› d	�z  }	n|	d
z  }	|	d|› d�z  }	t          |	d|› �z   ¦  «        d‚d}~ww xY w)aé  
    einops.reduce combines rearrangement and reduction using reader-friendly notation.

    Some examples:

    ```python
    >>> x = np.random.randn(100, 32, 64)

    # perform max-reduction on the first axis
    # Axis t does not appear on RHS - thus we reduced over t
    >>> y = reduce(x, 't b c -> b c', 'max')

    # same as previous, but using verbose names for axes
    >>> y = reduce(x, 'time batch channel -> batch channel', 'max')

    # let's pretend now that x is a batch of images
    # with 4 dims: batch=10, height=20, width=30, channel=40
    >>> x = np.random.randn(10, 20, 30, 40)

    # 2d max-pooling with kernel size = 2 * 2 for image processing
    >>> y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2)

    # same as previous, using anonymous axes,
    # note: only reduced axes can be anonymous
    >>> y1 = reduce(x, 'b c (h1 2) (w1 2) -> b c h1 w1', 'max')

    # adaptive 2d max-pooling to 3 * 4 grid,
    # each element is max of 10x10 tile in the original tensor.
    >>> reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=3, w1=4).shape
    (10, 20, 3, 4)

    # Global average pooling
    >>> reduce(x, 'b c h w -> b c', 'mean').shape
    (10, 20)

    # subtracting mean over batch for each channel;
    # similar to x - np.mean(x, axis=(0, 2, 3), keepdims=True)
    >>> y = x - reduce(x, 'b c h w -> 1 c 1 1', 'mean')

    # Subtracting per-image mean for each channel
    >>> y = x - reduce(x, 'b c h w -> b c 1 1', 'mean')

    # same as previous, but using empty compositions
    >>> y = x - reduce(x, 'b c h w -> b c () ()', 'mean')

    ```

    Parameters:
        tensor: tensor: tensor of any supported library (e.g. numpy.ndarray, tensorflow, pytorch).
            list of tensors is also accepted, those should be of the same type and shape
        pattern: string, reduction pattern
        reduction: one of available reductions ('min', 'max', 'sum', 'mean', 'prod', 'any', 'all').
            Alternatively, a callable f(tensor, reduced_axes) -> tensor can be provided.
            This allows using various reductions like: np.max, np.nanmean, tf.reduce_logsumexp, torch.var, etc.
        axes_lengths: any additional specifications for dimensions

    Returns:
        tensor of the same type as input
    r   z9Rearrange/Reduce/Repeat can't be applied to an empty list)rŸ   r    )r&   rm   z Error while processing z-reduction pattern "z".z
 Input tensor shape: z. z
 Input is list. zAdditional info: r¨   z
 N)rt   rA   r?   rŠ   r   Ústack_on_zeroth_dimensionr*   ru   rh   ræ   r�   r   r   r   )
r/   r�   rî   rm   r0   Úhashable_axes_lengthsrh   r…   ÚeÚmessages
             r#   r.   r.   Ô  s�  € ðx9Ý�f�dÑ#Ô#ð 	*Ý�6‰{Œ{˜aÒÐÝÐ [Ñ\Ô\Ð\Ý! &¨¤)Ñ,Ô,ˆGØ×6Ò6°vÑ>Ô>ˆFˆFå! &Ñ)Ô)ˆGå % l×&8Ò&8Ñ&:Ô&:Ñ ;Ô ;ÐØ—’˜fÑ%Ô%ˆÝ/°¸ÍuÐUaÑObÔObÕilÐmrÑisÔisÐtÑtÔtˆÝØ�V�T¥&¨&Ñ1Ô1À)ÐZoð
ñ 
ô 
ð 	
øõ ð 9ð 9ð 9ØW¨YÐWÐWÈGÐWÐWÐWˆÝ˜&¥$Ñ'Ô'ð 	,ØÐ:°Ð:Ð:Ð:Ñ:ˆGˆGàÐ+Ñ+ˆGØÐ6 |Ð6Ð6Ð6Ñ6ˆÝ˜' I¨! I IÑ-Ñ.Ô.°DÐ8øøøøð9øøøs   ‚C:C= Ã=
EÄAEÅEc                 ó   — d S r<   r    ©r/   r�   rm   s      r#   r£   r£   )  s   € ØSVÐSVr%   c                 ó   — d S r<   r    rø   s      r#   r£   r£   -  s   € ØMPÈSr%   c                 ó"   — t          | |fddi|¤ŽS )aü  
    einops.rearrange is a reader-friendly smart element reordering for multidimensional tensors.
    This operation includes functionality of transpose (axes permutation), reshape (view), squeeze, unsqueeze,
    stack, concatenate and other operations.

    Examples:

    ```python
    # suppose we have a set of 32 images in "h w c" format (height-width-channel)
    >>> images = [np.random.randn(30, 40, 3) for _ in range(32)]

    # stack along first (batch) axis, output is a single array
    >>> rearrange(images, 'b h w c -> b h w c').shape
    (32, 30, 40, 3)

    # stacked and reordered axes to "b c h w" format
    >>> rearrange(images, 'b h w c -> b c h w').shape
    (32, 3, 30, 40)

    # concatenate images along height (vertical axis), 960 = 32 * 30
    >>> rearrange(images, 'b h w c -> (b h) w c').shape
    (960, 40, 3)

    # concatenated images along horizontal axis, 1280 = 32 * 40
    >>> rearrange(images, 'b h w c -> h (b w) c').shape
    (30, 1280, 3)

    # flattened each image into a vector, 3600 = 30 * 40 * 3
    >>> rearrange(images, 'b h w c -> b (c h w)').shape
    (32, 3600)

    # split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), 128 = 32 * 2 * 2
    >>> rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c', h1=2, w1=2).shape
    (128, 15, 20, 3)

    # space-to-depth operation
    >>> rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape
    (32, 15, 20, 12)

    ```

    When composing axes, C-order enumeration used (consecutive elements have different last axis).
    Find more examples in einops tutorial.

    Parameters:
        tensor: tensor of any supported library (e.g. numpy.ndarray, tensorflow, pytorch).
                list of tensors is also accepted, those should be of the same type and shape
        pattern: string, rearrangement pattern
        axes_lengths: any additional specifications for dimensions

    Returns:
        tensor of the same type as input. If possible, a view to the original tensor is returned.

    rî   r£   ©r.   rø   s      r#   r£   r£   1  s"   € õn �&˜'ÐIÐI¨[ÐI¸LÐIÐIÐIr%   c                 ó   — d S r<   r    rø   s      r#   r¤   r¤   k  s   € ØPSÐPSr%   c                 ó   — d S r<   r    rø   s      r#   r¤   r¤   o  s   € ØJMÈ#r%   c                 ó"   — t          | |fddi|¤ŽS )a•  
    einops.repeat allows reordering elements and repeating them in arbitrary combinations.
    This operation includes functionality of repeat, tile, and broadcast functions.

    Examples for repeat operation:

    ```python
    # a grayscale image (of shape height x width)
    >>> image = np.random.randn(30, 40)

    # change it to RGB format by repeating in each channel
    >>> repeat(image, 'h w -> h w c', c=3).shape
    (30, 40, 3)

    # repeat image 2 times along height (vertical axis)
    >>> repeat(image, 'h w -> (repeat h) w', repeat=2).shape
    (60, 40)

    # repeat image 2 time along height and 3 times along width
    >>> repeat(image, 'h w -> (h2 h) (w3 w)', h2=2, w3=3).shape
    (60, 120)

    # convert each pixel to a small square 2x2, i.e. upsample an image by 2x
    >>> repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape
    (60, 80)

    # 'pixelate' an image first by downsampling by 2x, then upsampling
    >>> downsampled = reduce(image, '(h h2) (w w2) -> h w', 'mean', h2=2, w2=2)
    >>> repeat(downsampled, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape
    (30, 40)

    ```

    When composing axes, C-order enumeration used (consecutive elements have different last axis).
    Find more examples in einops tutorial.

    Parameters:
        tensor: tensor of any supported library (e.g. numpy.ndarray, tensorflow, pytorch).
            list of tensors is also accepted, those should be of the same type and shape
        pattern: string, rearrangement pattern
        axes_lengths: any additional specifications for dimensions

    Returns:
        Tensor of the same type as input. If possible, a view to the original tensor is returned.

    rî   r¤   rû   rø   s      r#   r¤   r¤   s  s"   € õ^ �&˜'ÐFÐF¨XÐF¸ÐFÐFÐFr%   r=   c                 óÌ  — t          |d¬¦  «        }t          | ¦  «                             | ¦  «        }|                     ¦   «         rt	          d|› d|› �¦  «        ‚t          |¦  «        t          |j        ¦  «        k    rZ|j        r>t          |¦  «        t          |j        ¦  «        dz
  k     rt	          d|› d|› �¦  «        ‚nt	          d|› d|› �¦  «        ‚|j        rn|j                             t          ¦  «        }|j        d|…         d	gt          |¦  «        t          |j        ¦  «        z
  dz   z  z   |j        |dz   d…         z   }n|j        }i }t          ||¦  «        D ]y\  }}t          |¦  «        d
k    r|dk    rt	          d|› d|› �¦  «        ‚Œ4|\  }	t          |	t          ¦  «        r|	d	k    r|||	<   ŒY|	j        |k    rt	          d|› d|› �¦  «        ‚Œz|S )aé  
    Parse a tensor shape to dictionary mapping axes names to their lengths.

    ```python
    # Use underscore to skip the dimension in parsing.
    >>> x = np.zeros([2, 3, 5, 7])
    >>> parse_shape(x, 'batch _ h w')
    {'batch': 2, 'h': 5, 'w': 7}

    # `parse_shape` output can be used to specify axes_lengths for other operations:
    >>> y = np.zeros([700])
    >>> rearrange(y, '(b c h w) -> b c h w', **parse_shape(x, 'b _ h w')).shape
    (2, 10, 5, 7)

    ```

    For symbolic frameworks may return symbols, not integers.

    Parameters:
        x: tensor of any supported framework
        pattern: str, space separated names for axes, underscore means skip axis

    Returns:
        dict, maps axes names to their lengths
    T©Úallow_underscorez'Can't parse shape with composite axes: ú r   z2Can't parse shape with this number of dimensions: z7Can't parse shape with different number of dimensions: NÚ_r   zLength of axis is not 1: z)Length of anonymous axis does not match: )r   r   rh   Úhas_composed_axesÚRuntimeErrorr?   rÌ   rÆ   rB   r   Úziprt   rg   rÐ   )
r=   r�   Úexprh   Úellipsis_idxrÌ   r!   Úaxesrš   r7   s
             r#   Úparse_shaper
  ¥  s>  € õ4 ˜7°TÐ
:Ñ
:Ô
:€CÝ˜‰NŒN× Ò  Ñ#Ô#€EØ
×ÒÑÔð XÝÐVÀWÐVÐVÈuÐVÐVÑWÔWÐWÝ
ˆ5�z„z•S˜œÑ)Ô)Ò)Ð)ØÔð 	lÝ�5‰zŒz�C ¤Ñ0Ô0°1Ñ4Ò4Ð4Ý"Ð#iÐX_Ð#iÐ#iÐbgÐ#iÐ#iÑjÔjÐjð 5õ ÐjÐY`ÐjÐjÐchÐjÐjÑkÔkÐkØ
Ôð &Ø”×,Ò,­YÑ7Ô7ˆàŒO˜M˜\˜MÔ*Øˆe•s˜5‘z”z¥C¨¬Ñ$8Ô$8Ñ8¸1Ñ<Ñ=ñ>àŒo˜l¨QÑ.Ð0Ð0Ô1ñ2ð 	ˆð ”oˆØ€FÝ  ¨eÑ4Ô4ð fð fÑˆˆkåˆt‰9Œ9˜Š>ˆ>Ø˜aÒÐÝ"Ð#P¸wÐ#PÐ#PÈÐ#PÐ#PÑQÔQÐQð  ð ‰FˆTÝ˜$¥Ñ$Ô$ð fØ˜3’;�;Ø#.�F˜4‘Løà”: Ò,Ð,Ý&Ð'dÐSZÐ'dÐ'dÐ]bÐ'dÐ'dÑeÔeÐeð -à€Mr%   c           	      ó&  — t          | ¦  «        }|                     | ¦  «        }g }t          |¦  «        D ]Z\  }}dgt          |¦  «        z  }|||<   |                     |                     |                     d|¦  «        |¦  «        ¦  «         Œ[|S )aa  
    For an n-dimensional tensor, returns tensors to enumerate each axis.
    ```python
    x = np.zeros([2, 3, 4]) # or any other tensor
    i, j, k = _enumerate_directions(x)
    result = i + 2*j + 3*k
    ```

    `result[i, j, k] = i + 2j + 3k`, and also has the same shape as result
    Works very similarly to numpy.ogrid (open indexing grid)
    r   r   )r   rh   rs   r?   rL   r‹   Úarange)r=   r0   rh   r!   Úaxis_idrš   s         r#   Ú_enumerate_directionsr  ä  s”   € õ ˜!‰nŒn€GØ�MŠM˜!ÑÔ€EØ€FÝ )¨%Ñ 0Ô 0ð Nð NÑˆ�Ø�•c˜%‘j”jÑ ˆØ$ˆˆg‰Ø�Š�g—o’o g§n¢n°Q¸Ñ&DÔ&DÀeÑLÔLÑMÔMÐMÐMØ€Mr%   c                 óF   — t          | ¦  «                             | ¦  «        S )zí
    Convert a tensor of an imperative framework (i.e. numpy/cupy/torch/jax/etc.) to `numpy.ndarray`

    Parameters:
        tensor: tensor of any known imperative framework

    Returns:
        `numpy.ndarray`, converted to numpy
    )r   Úto_numpy)r/   s    r#   Úasnumpyr  þ  s    € õ �vÑÔ×'Ò'¨Ñ/Ô/Ð/r%   c                 ór  — t          | ¦  «        dk    rt          d¦  «        ‚t          | ¦  «        dk    rt          d¦  «        ‚| d         } t          | t          ¦  «        rt          d¦  «        ‚t          | ¦  «        dk    rt	          d¦  «        ‚t          | t
          ¦  «        st	          d¦  «        ‚d S )Nr   z2Singleton () axes are not yet supported in einsum.r   z3Shape rearrangement is not yet supported in einsum.z/Anonymous axes are not yet supported in einsum.z&Encountered empty axis name in einsum.z%Axis name in einsum must be a string.)r?   r-   rt   r   r  rg   )rÃ   s    r#   Ú_validate_einsum_axis_namer    s»   € Ý
ˆ9�~„~˜ÒÐÝ!Ð"VÑWÔWÐWÝ
ˆ9�~„~˜ÒÐÝ!Ð"WÑXÔXÐXà˜!”€Iå�)�]Ñ+Ô+ð UÝ!Ð"SÑTÔTÐTÝ
ˆ9�~„~˜ÒÐÝÐCÑDÔDÐDÝ�i¥Ñ%Ô%ð DÝÐBÑCÔCÐCðDð Dr%   c                 óÜ  — d| vrt          d¦  «        ‚|                      d¦  «        \  }}d„ |                     d¦  «        D ¦   «         }t          |d¬¦  «        }t          j        }d}i }g }|D ]Œ}	d}
|	j        D ]k}|t          k    r|
d	z  }
Œt          |¦  «         |d         }||vr2|t          |¦  «        k    rt          d
¦  «        ‚||         ||<   |dz  }|
||         z  }
Œl| 
                    |
¦  «         Œ�d                     |¦  «        dz   }|j        D ]O}|t          k    r|d	z  }Œt          |¦  «         |d         }||vrt          d|› d| › d�¦  «        ‚|||         z  }ŒP|S )Nr¢   z!Einsum pattern must contain '->'.c                 ó2   — g | ]}t          |d d ¬¦  «        ‘ŒS )T)r  Úallow_duplicates)r   )r6   r¿   s     r#   rn   z2_compactify_pattern_for_einsum.<locals>.<listcomp>#  s)   € ÐsÐsÐsÐVZÕ˜d°TÈDÐQÑQÔQÐsÐsÐsr%   ú,Tr   r   Ú z...zToo many axes in einsum.r   zUnknown axis z on right side of einsum r¨   )Ú
ValueErrorrÅ   r   ÚstringÚascii_lettersrÌ   r   r  r?   r  rL   Újoinr   )r�   Ú	lefts_strÚ	right_strÚleftsÚrightÚoutput_axis_namesrN   Úaxis_name_mappingÚleft_patternsr¿   Úleft_patternÚraw_axis_namerÃ   Úcompact_patterns                 r#   Ú_compactify_pattern_for_einsumr'    sê  € à�7ÐÐõ Ð<Ñ=Ô=Ð=Ø"Ÿ=š=¨Ñ.Ô.Ñ€IˆyàsÐsÐ^g×^mÒ^mÐnqÑ^rÔ^rÐsÑsÔs€Eå˜Y¸Ð>Ñ>Ô>€Eõ Ô,ÐØ	€AØÐà€MØð +ð +ˆØˆØ!Ô-ð 	9ð 	9ˆMØ¥	Ò)Ð)Ø Ñ%�Øå& }Ñ5Ô5Ð5Ø% aÔ(ˆIØÐ 1Ð1Ð1Ø�Ð-Ñ.Ô.Ò.Ð.Ý&Ð'AÑBÔBÐBØ/@ÀÔ/CÐ! )Ñ,Ø�Q‘�àÐ-¨iÔ8Ñ8ˆLˆLØ×Ò˜\Ñ*Ô*Ð*Ð*à—h’h˜}Ñ-Ô-°Ñ4€OàÔ*ð 8ð 8ˆØ�IÒ%Ð%Ø˜uÑ$ˆOØå" =Ñ1Ô1Ð1Ø! !Ô$ˆ	àÐ-Ð-Ð-ÝÐ\¨iÐ\Ð\ÐRYÐ\Ð\Ð\Ñ]Ô]Ð]àÐ,¨YÔ7Ñ7ˆˆàÐr%   c                ó   — d S r<   r    )r/   r�   s     r#   Úeinsumr)  Q  s   € Ø7:°sr%   Útensor1Útensor2c                ó   — d S r<   r    )r*  r+  r�   s      r#   r)  r)  U  s   € ØILÈr%   Útensor3c                ó   — d S r<   r    )r*  r+  r-  r�   s       r#   r)  r)  Y  s   € ØZ]ÐZ]r%   Útensor4c                ó   — d S r<   r    )r*  r+  r-  r/  r�   s        r#   r)  r)  ]  s   € ØknÐknr%   Útensors_and_patternc                  ó  — t          | ¦  «        dk    rt          d¦  «        ‚| d         }t          |t          ¦  «        st          d¦  «        ‚| dd…         }t	          |¦  «        } t          |d         ¦  «        j        |g|¢R Ž S )aü  
    einops.einsum calls einsum operations with einops-style named
    axes indexing, computing tensor products with an arbitrary
    number of tensors. Unlike typical einsum syntax, here you must
    pass tensors first, and then the pattern.

    Also, note that rearrange operations such as `"(batch chan) out"`,
    or singleton axes `()`, are not currently supported.

    Examples:

    For a given pattern such as:
    ```python
    >>> x, y, z = np.random.randn(3, 20, 20, 20)
    >>> output = einsum(x, y, z, "a b c, c b d, a g k -> a b k")

    ```
    the following formula is computed:
    ```tex
    output[a, b, k] = \sum_{c, d, g} x[a, b, c] * y[c, b, d] * z[a, g, k]
    ```
    where the summation over `c`, `d`, and `g` is performed
    because those axes names do not appear on the right-hand side.

    Let's see some additional examples:
    ```python
    # Filter a set of images:
    >>> batched_images = np.random.randn(128, 16, 16)
    >>> filters = np.random.randn(16, 16, 30)
    >>> result = einsum(batched_images, filters,
    ...                 "batch h w, h w channel -> batch channel")
    >>> result.shape
    (128, 30)

    # Matrix multiplication, with an unknown input shape:
    >>> batch_shape = (50, 30)
    >>> data = np.random.randn(*batch_shape, 20)
    >>> weights = np.random.randn(10, 20)
    >>> result = einsum(weights, data,
    ...                 "out_dim in_dim, ... in_dim -> ... out_dim")
    >>> result.shape
    (50, 30, 10)

    # Matrix trace on a single tensor:
    >>> matrix = np.random.randn(10, 10)
    >>> result = einsum(matrix, "i i ->")
    >>> result.shape
    ()

    ```

    Parameters:
        tensors_and_pattern:
            tensors: tensors of any supported library (numpy, tensorflow, pytorch, jax).
            pattern: string, einsum pattern, with commas
                separating specifications for each tensor.
                pattern should be provided after all tensors.

    Returns:
        Tensor of the same type as input, after processing with einsum.

    r   zd`einops.einsum` takes at minimum two arguments: the tensors (at least one), followed by the pattern.r3   z^The last argument passed to `einops.einsum` must be a string, representing the einsum pattern.Nr   )r?   r  rt   rg   r'  r   r)  )r1  r�   Útensorss      r#   r)  r)  a  s¥   € õ~ ÐÑÔ 1Ò$Ð$ÝØrñ
ô 
ð 	
ð " "Ô%€GÝ�g�sÑ#Ô#ð 
ÝØlñ
ô 
ð 	
ð " # 2 #Ô&€GÝ,¨WÑ5Ô5€GØ)�;�w˜q”zÑ"Ô"Ô)¨'Ð<°GÐ<Ð<Ð<Ð<r%   )>Ú	functoolsrÓ   r  ÚtypingÚcollectionsr   r   r   r   r   r   r	   r
   r   r   r   r   ÚTYPE_CHECKINGÚnumpyÚnpr  r   Ú	_backendsr   Úparsingr   r   r   r   rf   ÚReductionCallablerg   Ú	ReductionÚSizer+   r¯   rÒ   r$   r1   rT   ÚCookedRecipeÚHashableAxesLengthsÚFakeHashableAxesLengthsrV   r„   Ú	lru_cacher‰   r�   r›   ræ   rí   r.   r£   r¤   Údictr
  r  Ú
np_ndarrayr  r  r'  r)  r    r%   r#   ú<module>rE     sœ  ðØ Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bà	Ôð àÐÐÐà Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?à	ˆ�Ñ	Ô	€Ø˜f e¨C°¨H¤oÐ6¸Ð>Ô?Ð Ø�#Ð(Ð(Ô)€	Ø„z€àA€ð Ð ØÐ ð�t˜C”yð  Sð ð ð ð ð
K¨ð 
KÀ$ÀsÄ)ð 
Kð 
Kð 
Kð 
Kð4Dð 4Dð 4Dðn �X˜d 3œiÔ(¨(°4¸´9Ô*=¸tÀC¼yÈ$ÈsÐTWÈxÌ.ÐZbÐcgÐhkÔclÔZmÐorÐrÔs€ð
 ˜E # s (œO¨SÐ0Ô1Ð Ø˜u S¨# XœÔ/Ð ð&Lð &Lð &Lð &Lð &Lñ &Lô &Lð &LðREkØ
ðEkØ"& s¤)ðEkØ8OðEkàðEkð Ekð Ekð EkðP 4Ð-˜)Ô-¨dÑ3Ô3Ð4TÑUÔUÐ ðØ$ðØ.4ðØFOðØ_rðàðð ð ð ð2ØðØ)/ðØAJðØZmðàðð ð ð ðD €Ô�SÑÔðXØðXàðXð �c˜3�h”ðXð ð	Xð
 ðXð Xð Xñ ÔðXðvnØðnØ&ðnØ49¸#¸s¸(´Oðnà	ˆ#ˆÐ
Ôðnð nð nð nð 
Ø i�4˜”<Ð i¨#Ð i¸)Ð iÐUYÐ iÐ^dÐ iÐ iÐ iñ 
„Ø ið 
Ø c�6Ð c CÐ c°IÐ cÈtÐ cÐX^Ð cÐ cÐ cñ 
„Ø cðR9�5˜  f¤Ð-Ô.ð R9¸ð R9Èð R9Ðdhð R9Ðmsð R9ð R9ð R9ð R9ðj 
Ø V�d˜6”lÐ V¨SÐ VÀ$Ð VÈ6Ð VÐ VÐ Vñ 
„Ø Vð 
Ø P�fÐ P sÐ P¸DÐ PÀVÐ PÐ PÐ Pñ 
„Ø Pð7J�e˜F D¨¤LÐ0Ô1ð 7J¸Cð 7JÐQUð 7JÐZ`ð 7Jð 7Jð 7Jð 7Jðt 
Ø S�4˜”<Ð S¨#Ð S¸tÐ SÈÐ SÐ SÐ Sñ 
„Ø Sð 
Ø M�6Ð M CÐ M¸Ð MÀ&Ð MÐ MÐ Mñ 
„Ø Mð/G�5˜  f¤Ð-Ô.ð /G¸ð /GÈdð /GÐW]ð /Gð /Gð /Gð /Gðd;�6ð ; Cð ;¨Dð ;ð ;ð ;ð ;ð~ð ð ð. €
ð
0�Fð 
0˜zð 
0ð 
0ð 
0ð 
0ðDð Dð Dð  €Ô�SÑÔð2¨Cð 2°Cð 2ð 2ð 2ñ Ôð2ðj „Ø :�6Ð : CÐ :¨vÐ :Ð :Ð :ñ „Ø :ð „Ø L�FÐ L VÐ L°cÐ LÀÐ LÐ LÐ Lñ „Ø Lð „Ø ]�FÐ ] VÐ ]°fÐ ]ÀsÐ ]ÐRXÐ ]Ð ]Ð ]ñ „Ø ]ð „Ø n�FÐ n VÐ n°fÐ nÀvÐ nÐX[Ð nÐciÐ nÐ nÐ nñ „Ø nðJ=  v¨s {Ô!3ð J=¸ð J=ð J=ð J=ð J=ð J=ð J=r%   