
    [Th                        S SK Jr  S SKrS SKJrJrJr  S SKrS SKJ	r
  SSKJrJrJrJrJr  \(       a  S SKJr  S/r\
R(                  r\R*                  " S	5              SS
 j5       r        SS jrg)    )annotationsN)CallableTYPE_CHECKINGUnion)dim   )	_ellipsisAnonymousAxiscomma_separateparse_patternvalidate_rearrange_expressions)Sequence	rearrange   c                  ^^^ [        X5      u  p4[        X4U5        [        S UR                   5       5      nUR                  (       aO  U [        UR                  5      S-
  -
  n[        UR                  5      S-
  nXW-   =o:  a  [        SU SU  S35      eOCSn[        UR                  5      n[        UR                  5      =o:w  a  [        SU SU  S35      eXv-   U-   n	U	S:X  a  S $ [        S	 [        U	5       5       5      m0 m/ n
SmUR                   H  n[        U[        5      (       at  U H(  n[        U[        5      (       d   eTT   4TU'   TS-  mM*     U(       d=  [        S
5      nTT   4TU'   U
R                  U5        UR                  U5        TS-  mM  M  U[        :X  a/  [        n[        UU4S j[        U5       5       5      TU'   TU-  mM  [        SU 35      e       SU4S jjnU" UR                  5      nU" UR                  5      n[        U4S jU
 5       5      n[        U4S jUR!                  5        5       5      nSnSU S[#        T5       SU	 S3U(       a  SR%                  S U 5       5      OS-   S[#        U5       S[#        U5       S3-   U(       a  S[#        U/5       S3OS-   n['        U5        [)        5       U   $ )aV  Translate an `einops`-style pattern into a callable that performs the rearrange using first-class dimensions.

Since the an equivalent result is computed for tensors with the same number of dimensions, with the same pattern and
specified axes lengths, this function can be memoized.

Args:
    tensor_ndim (int): the number of dimensions in the tensor to rearrange
    pattern (str): the `einops`-style rearrangement pattern
    axes_lengths (int): any additional length specifications for dimensions

Returns:
    Callable[[torch.Tensor], torch.Tensor]: a callable that performs the rearrangement
c              3  .   #    U  H  o(       + v   M     g 7fN ).0r   s     R/var/www/auris/envauris/lib/python3.13/site-packages/functorch/einops/rearrange.py	<genexpr>-_create_rearrange_callable.<locals>.<genexpr>.   s     :)9#gg)9s   r   z!Number of dimensions in pattern (zH) must be less than or equal to the number of dimensions in the tensor ()r   z;) must be equal to the number of dimensions in the tensor (c                    U $ r   r   )tensors    r   <lambda>,_create_rearrange_callable.<locals>.<lambda>E   s    f    c              3  ,   #    U  H
  nS U 3v   M     g7f)dNr   )r   is     r   r   r   G   s     -M}!!g}s   1c              3  4   >#    U  H  nTTU-      v   M     g 7fr   r   )r   jdims_ifirst_class_dimss     r   r   r   ]   s      36L !,6L   Unexpected dimension: c                   > / nU  Hn  n[        U[        5      (       a&  UR                  [        U4S jU 5       5      5        M>  U[        :X  a  UR                  T[           5        Mb  [        SU 35      e   U$ )ztConvert a `ParsedExpression.composition` into a `Tensor.__getitem__` index of strings representing first
class dims.c              3  B   >#    U  H  nTU     H  nUv   M	     M     g 7fr   r   )r   
identifierr   identifier_dim_maps      r   r   J_create_rearrange_callable.<locals>.composition_to_dims.<locals>.<genexpr>m   s*      *3J#5j#AC #A *3s   r(   )
isinstancelistappendtupler	   extend
ValueError)compositiondim_composition	dimensionr,   s      r   composition_to_dims7_create_rearrange_callable.<locals>.composition_to_dimsd   s~    
 >@$I)T**&& *3  i'&&'9)'DE #9)!EFF % r   c              3  4   >#    U  H  nTU   S    v   M     g7fr   Nr   )r   axisr,   s     r   r   r   {   s     Hid(.q1ir'   c              3  <   >#    U  H  u  pTU   S    U4v   M     g7fr:   r   )r   r;   lengthr,   s      r   r   r   |   s'      BV,$	D	!!	$f-BVs   do_rearrangezdef z(tensor):
    z = dims(z)
 c              3  8   #    U  H  u  pS U SU S3v   M     g7f)z    z.size = 
Nr   )r   r   r=   s      r   r   r      s%      DU=C$se8F82.DUs   z    tensor = tensor[z].order(z    return tensor.sum(z, keepdim=False)
z    return tensor
)r4   z5Sequence[Union[list[Union[str, AnonymousAxis]], str]]returnz!list[Union[str, tuple[str, ...]]])r   r   sumr4   has_ellipsislenidentifiersr3   r1   ranger.   r/   strr
   r0   r	   itemsr   joinexeclocals)tensor_ndimpatternaxes_lengthsleftrightn_anon_dimsn_ellipsis_dimsn_named_dimspattern_ndimn_dims	anon_axesr6   r+   	anon_axisr7   	left_dims
right_dims	anon_dimsspecified_lengthscustom_rearrange_callable_namecustom_rearrange_callable_coder%   r&   r,   s                        @@@r   _create_rearrange_callabler_      sY   "  6KD"4=:)9)9::K%T-=-=)>)BC4++,q0'66LE3L> B--8M<  F 4++, 0 011LA3L> B*m1.  +k9F{$$(--MuV}-M(MKM%'I F%%	i&&'
!*c22222B62J1L":.!	 (
 )#.	1A&1I0K"9-  +  +!  )#"J-2 36;O6L3 .z* o%F5i[ABB+ &.J	** $D$4$45I$U%6%67JHiHHI BNBTBTBV  &4" 12 3!"234HVHCI ! GG DU  
	
 !	!: ;8NS]D^C__b
c	d  %^YK%@$AAST&	
 #( 		'(8233r   c                    [        U [        R                  5      (       d  [        R                  " U 5      n [	        U R
                  U40 UD6nU" U 5      $ )a2  A native implementation of `einops.rearrange`, 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.

See: https://einops.rocks/api/rearrange/

Args:
    tensor (Tensor or sequence of Tensor): the tensor(s) to rearrange
    pattern (str): the rearrangement pattern
    axes_lengths (int): any additional length specifications for dimensions

Returns:
    Tensor: the rearranged tensor

Examples:
    >>> # suppose we have a set of 32 images in "h w c" format (height-width-channel)
    >>> images = torch.randn((32, 30, 40, 3))

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

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

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

    >>> # reordered axes to "b c h w" format for deep learning
    >>> rearrange(images, "b h w c -> b c h w").shape
    torch.Size([32, 3, 30, 40])

    >>> # flattened each image into a vector, 3600 = 30 * 40 * 3
    >>> rearrange(images, "b h w c -> b (c h w)").shape
    torch.Size([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
    torch.Size([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
    torch.Size([32, 15, 20, 12])
)r.   torchTensorstackr_   ndim)r   rN   rO   rearrange_callables       r   r   r      sM    f fell++V$3W , f%%r   )rM   intrN   rH   rO   rf   rB   z&Callable[[torch.Tensor], torch.Tensor])r   zAUnion[torch.Tensor, list[torch.Tensor], tuple[torch.Tensor, ...]]rN   rH   rO   rf   rB   ztorch.Tensor)
__future__r   	functoolstypingr   r   r   ra   functorch._Cr   _C_parsingr	   r
   r   r   r   collections.abcr   __all__dims	lru_cacher_   r   r   r   r   <module>rq      s    "  1 1  "  (-	ww S{4{4"{447{4+{4 {4|:&M:&:& :& 	:&r   