
    9?i              
          d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ  ed      Zeeedf   ee   f   Z ed	      d
ededeeeef   fd       Zdee   d
edeeee   f   fdZdedefdZdedee   d
edee   fdZy)    )	lru_cache)ListUnionTypeVarTupleSequence)EinopsError)get_backend)ParsedExpressionTensor.   )maxsizepatternopnamereturnc           
         | j                         }t        |      }t        |      t        |      k7  rt        d| d|  d      d|vrt        d| d|  d      |D ]7  }|dk7  s	t	        j
                  |      \  }}|r$t        d| d| d|  d       |j                  d      }t        |      |z
  dz
  }||z   }	|||	fS )	NzDuplicates in axes names in z(..., "z")*zNo *-axis in zInvalid axis name z in    )splitsetlenr	   r   check_axis_name_return_reasonindex)
r   r   axesaxes_setaxisis_validreasonn_axes_beforen_axes_aftermin_axess
             d/var/www/html/leadgen/airagagent/ocr_fallback/ocr_env/lib/python3.12/site-packages/einops/packing.pyanalyze_patternr#      s     ==?D4yH
4yCM!8yPRSTT
(M&	DEE ]3;/MMdSHf!$6tfDPWyXZ"[\\	]
 JJsOMt9},q0L|+H,00    tensorsc                    t        |d      \  }}}t        | d         }g }g }t        |       D ]  \  }}	|j                  |	      }
t	        |
      |k  rt        d| d|
 d| d| d	      t	        |
      |z
  }|j                  |
||        |j                  |j                  |	g |
d| d	|
|d               |j                  ||
      |fS )a  
    Packs several tensors into one.
    See einops tutorial for introduction into packing (and how it replaces stack and concatenation).

    Parameters:
        tensors: tensors to be packed, can be of different dimensionality
        pattern: pattern that is shared for all inputs and output, e.g. "i j * k" or "batch seq *"

    Returns:
        (packed_tensor, packed_shapes aka PS)

    Example:
    ```python
    >>> from numpy import zeros as Z
    >>> inputs = [Z([2, 3, 5]), Z([2, 3, 7, 5]), Z([2, 3, 7, 9, 5])]
    >>> packed, ps = pack(inputs, 'i j * k')
    >>> packed.shape, ps
    ((2, 3, 71, 5), [(), (7,), (7, 9)])
    ```

    In this example, axes were matched to: i=2, j=3, k=5 based on order (first, second, and last).
    All other axes were 'packed' and concatenated.
    PS (packed shapes) contains information about axes that were matched to '*' in every input.
    Resulting tensor has as many elements as all inputs in total.

    Packing can be reversed with unpack, which additionally needs PS (packed shapes) to reconstruct order.

    ```python
    >>> inputs_unpacked = unpack(packed, ps, 'i j * k')
    >>> [x.shape for x in inputs_unpacked]
    [(2, 3, 5), (2, 3, 7, 5), (2, 3, 7, 9, 5)]
    ```

    Read the tutorial for introduction and application scenarios.
    packr   zpacked tensor #z' (enumeration starts with 0) has shape z, while pattern z assumes at least z axesN)r   )	r#   r
   	enumerateshaper   r	   appendreshapeconcat)r%   r   r   r    r!   backendreshaped_tensorspacked_shapesitensorr*   axis_after_packed_axess               r"   r'   r'   "   s   H -<GV,L)M< '!*%G%'!#Mw' 	x	6f%u: !!$KE7 S!!(	);H:UL  "%Ul!:U=1GHI8u%:O8uQS8uV[\r\sVt8u vw	x >>*>?NNr$   xc                 "    d}| D ]  }||z  }	 |S )Nr    )r4   resultr1   s      r"   prodr8   [   s$    F !Mr$   r2   r0   c                    t        |d      \  }}}t        |       }|j                  |       }t        |      |dz   |z   k7  rt	        d| d|       |}|D 	cg c]  }	d|	v rdn
t        |	       }
}	t        d |
D              }|dkD  rt	        d| d| d	      d
gt        |      z  ||   gz   }|d
k(  r%t        |
dd       D ]  \  }}||   |z   ||dz   <    nf|
j                  d      }t        |      D ]  }||   |
|   z   ||dz   <    t        |dz   t        |
            ddd   D ]  }||dz      |
|   z
  ||<    |d| }||dz   d }t        dd      f|z  }	 t        |      D cg c]9  \  }}|j                  | g |t        ||   ||dz               g |||      ; c}}S c c}	w c c}}w # t        $ r t        d| d|d    d|       w xY w)a8  
    Unpacks a single tensor into several by splitting over a selected axes.
    See einops tutorial for introduction into packing (and how it replaces stack and concatenation).

    Parameters:
        tensor: tensor to be unpacked
        packed_shapes: packed_shapes (aka PS) is a list of shapes that take place of '*' in each output.
            output will contain a single tensor for every provided shape
        pattern: pattern that is shared for input and all outputs, e.g. "i j * k" or "batch seq *",
            where * designates an axis to be unpacked

    Returns:
        list of tensors

    If framework supports views, results are views to the original tensor.

    Example:
    ```python
    >>> from numpy import zeros as Z
    >>> inputs = [Z([2, 3, 5]), Z([2, 3, 7, 5]), Z([2, 3, 7, 9, 5])]
    >>> packed, ps = pack(inputs, 'i j * k')
    >>> packed.shape, ps
    ((2, 3, 71, 5), [(), (7,), (7, 9)])
    ```

    In this example, axes were matched to: i=2, j=3, k=5 based on order (first, second, and last).
    All other axes were 'packed' and concatenated.
    PS (packed shapes) contains information about axes that were matched to '*' in every input.
    Resulting tensor has as many elements as all inputs in total.

    Packing can be reversed with unpack, which additionally needs PS (packed shapes) to reconstruct order.

    ```python
    >>> inputs_unpacked = unpack(packed, ps, 'i j * k')
    >>> [x.shape for x in inputs_unpacked]
    [(2, 3, 5), (2, 3, 7, 5), (2, 3, 7, 9, 5)]
    ```

    Read the tutorial for introduction and application scenarios.
    unpack)r   r   zunpack(..., z)) received input of wrong dim with shape r(   c              3   8   K   | ]  }t        |d k(          yw)r(   N)int).0r4   s     r"   	<genexpr>zunpack.<locals>.<genexpr>   s     !Q1#a2g,!Qs   z) received more than one -1 in z and can't infer dimensionsr   NzError during unpack(..., "z!"): could not split axis of size z into requested )r#   r
   r*   r   r	   r8   sumr)   r   rangeslicer,   	ExceptionRuntimeError)r2   r0   r   r   r    r!   r.   input_shapeunpacked_axisp_shapelengths_of_composed_axesn_unknown_composed_axessplit_positionsr1   r4   unknown_composed_axisjshape_start	shape_endslice_fillerelement_shapes                        r"   r:   r:   b   s   R -<GH,U)M<&!G--'K
;=1,|;;L	1Z[fZghii&Man*oV]w2DM+Q*o*o!!Q8P!QQ"7)#B=/Qlm
 	
 cC..+m2L1MMO!#6s;< 	<DAq%4Q%7!%;OAE"	< &>%C%CB%G,- 	VA%4Q%7:RST:U%UOAE"	V,q0#6N2OPQUSUQUV 	VA!0Q!7:RST:U!UOA	V n}-KMA-/0I$%'-7L
 %.m$<
 != OOYYu_Q-?QRUVQVAW'XYZ:+::	:
 	
; +p:
  
(	1RSbceSfRg}o/
 	

s$   F0 F; .>F5,F; 5F; ;!GN)	functoolsr   typingr   r   r   r   r   einopsr	   einops._backendsr
   einops.parsingr   r   r<   Shapestrr#   r'   r8   r:   r6   r$   r"   <module>rW      s     8 8  ( +		eCHotCy() 31S 1# 1%S#2F 1 1&6O(6" 6OS 6OU64;;N5O 6OrE c \
6 \
$u+ \
 \
V \
r$   