
    ȅi.                       S SK Jr  S SKrS SKrS SKrS SKJrJrJrJ	r	  \(       a  S SK
JrJr  S SKrS SKJrJrJr  SSKJrJrJr  SSKJr  SS	KJr  SS
KJr  SrSrS qSWS jrSXS jr SYSZS jjr! S[     S\S jjr" " S S5      r# SY     S]S jjr$ S[     S^S jjr% " S S\&5      r' " S S\&5      r(SSK)J*r*  S_S jr+ " S S5      r,\,\RZ                  4r. " S S\,5      r/ " S S\,5      r-S`SaS  jjr0SYSbS! jjr1ScS" jr2 " S# S$5      r3SdS% jr4SeS& jr5SfS' jr6S S(K7J8r8  S S)K9J:r:  \:" \,\RZ                  \,Rv                  5        \,?<SgS* jr=ShS+ jr>\>" S,5        \>" S-5        \>" S.5        \>" S/5        \>" S05        \>" S15        \>" S25        \>" S35        \>" S45        \>" S55        \>" S65        \>" S75        \>" S8S9S:9  \>" S;S9S:9  \>" S<SS=9  \>" S>SS=9  \>" S?SS=9  \>" S@SS=9  \>" SASS=9  \>" SBSS=9  \>" SCSS=9  \>" SDSS=9  \>" SESFSG9  \>" SHSFSG9  \>" SISS=9  \>" SJSSFSK9  \>" SLSSFSM9  \>" SNSSFSM9  \>" SOSSFSM9  \>" SPSSFSM9  \>" SQSSFSM9  \>" SRSSFSM9  \>" SSSSFSM9  \>" STSSSFSU9  \>" SVSSFSM9  \8" \R~                  R                  R                  SSFSM9rAg)i    )annotationsN)AnyOptionalTYPE_CHECKINGUnion)CallableSequence)tree_flattentree_maptree_unflatten   )_match_levelsDimEntryndim_of_levels)EnableAllLayers)_PyInstDecoder)
TensorInfoTc                H    [        U =(       a    U R                  S5      5      $ )z4Check if opcode is relevant for variable assignment.STORE_)bool
startswith)opcodes    P/home/james-whalen/.local/lib/python3.13/site-packages/functorch/dim/__init__.py_relevant_opr      s    66,,X677    c                    U $ )z8Handle tensor conversion for torch function integration. )tensors    r   handle_from_tensorr   !   s    Mr   Dimc                *    [        Xb  U5      $ S5      $ )zCreate a new Dim object.)r    )namesizes     r   _create_dimr%   &   s    t-T66266r   c                x  ^^	^
 SnSm
Tb  [        T5      nU b  U n[        R                  " 5       nUc  [        S5      eUR                  n Uc  [        S5      eUR
                  nUR                  n[        XE5      m	[        R                  S:  a$  T	R                  5       S:X  a  T	R                  5         T	R                  5         [        T	R                  5       5      (       a  Sm
O4T	R                  5       S:X  a   T	R                  5       m
T	R                  5         US:X  a  T
S:X  a  [        S	5      eT
nT
U:w  a  Sm
SU	U
U4S
 jjnTb)  [        T5      U:w  a  [        SU S[        T5       35      eUS:X  a	  U" S5      A$ / n[!        U5       H  nUR#                  U" U5      5        M     [%        U5      A$ ! Af = f)a  
Create and return one or more Dim objects.

Uses bytecode inspection to determine variable names when possible.

Args:
    n (int, optional): The number of dimensions to create. Can be omitted if sizes is specified.
    sizes (List[Optional[int]], optional): A list the same size as the number of dimensions to be
      created, specifying each dimensions size, or None to leave the size unset.

Returns:
    Union[Dim, Tuple[Dim, ...]]: Single Dim if n=1, tuple of Dims otherwise.

Examples:
    >>> batch, channel, width, height = dims(4)
    >>> batch, channel, width, height = dims(sizes=[None, 3, 224, 224])
    >>> single_dim = dims(1)
r"   r   Unable to get current frameUnable to get caller frame      PRECALLr   UNPACK_SEQUENCEzTdims() must be assigned to a sequence of variable names or have argument n specifiedc                   > S nU T:  a  TR                  5       nU(       d  SU  3nSmOTR                  5         Tb  TU    OS n[        X5      $ Ndr   )r#   nextr%   )ir#   r$   decoderfound_ndimssizess      r   	genobjectdims.<locals>.genobjectn   sQ    D;||~1#w$058dDt**r   	expected  sizes but found )r2   intreturnr    )leninspectcurrentframeRuntimeErrorf_backf_codef_lastir   sysversion_infor   r1   r   opargSyntaxError
ValueErrorrangeappendtuple)nr5   specified_ndimsframecodelastir6   resultr2   r3   r4   s    `       @@r   dimsrQ   +   s   * OK e*}   "E}899LLE==;<<|| -w&~~9, 	 ())K^^!22!--/KLLNb a!j  *O/)K	+ 	+  U!>y(99J3u:,WXXaQ<  'AMM)A,' ( V} Es   D2F6 ?5F6 6F9c                      \ rS rSr% SrS\S'   S\S'   S\S'     S   SS
 jjrSS jrSS jrSS jr	SS jr
SS jrSS jr\SS j5       rSS jrS S jrS!S jrS!S jr\  S"         S#S jj5       rSrg	)$DimList   a  
A list of first-class dimensions that can be bound to tensor dimensions.

A DimList can be in one of two states:
1. Unbound: Created with just a name, no specific dimensions yet
2. Bound: Either created with specific dimensions/sizes, or bound later via bind() or bind_len()
Optional[str]_namez	list[Dim]_dimsr   _boundNc                   X l         / U l        SU l        [        U[        5      (       a  U R                  U5        gUb  / n[        U5       Hv  u  pE[        U[        5      (       aB  U R                   (       a  U R                    U 3OSU 3nUR                  [        Xe5      5        M\  UR                  [        U5      5        Mx     U R                  U5        gg)z
Initialize a new DimList object.

Args:
    len_or_dims: Optional length (int) or sequence of dimensions/sizes
    name: Optional name for the dimension list
FNdim)
rV   rW   rX   
isinstancer:   bind_len	enumeraterI   r    	_set_dims)selflen_or_dimsr#   rQ   r2   itemdim_names          r   __init__DimList.__init__   s     

k3''MM+&$D$[1dC((59ZZ$**aS1s1#YHKKH 34KKD	* 2 NN4  %r   c                    SU l         Xl        g)z%Set the dimensions and mark as bound.TN)rX   rW   )r_   rQ   s     r   r^   DimList._set_dims   s    
r   c                v   U R                   (       a>  [        U R                  5      U:w  a$  [        S[        U R                  5       SU 35      egSU l         / U l        [	        U5       HM  nU R
                  (       a  U R
                   U 3OSU 3nU R                  R                  [        U5      5        MO     g)z
Bind this DimList to a specific length.

Args:
    size: Number of dimensions to bind to

Raises:
    DimensionBindError: If already bound to a different size
zDimlist has size z but it is being bound to size TrZ   N)rX   r<   rW   DimensionBindErrorrH   rV   rI   r    )r_   r$   r2   rb   s       r   r\   DimList.bind_len   s     ;;4::$&('DJJ'88WX\W]^  '
 DKDJ4[15djj\!-3qc

!!#h-0 !r   c                    [        US5      (       a  [        US5      (       d  [        S5      e[        U5      nU R                  U5        [	        U5       H"  u  p4[        U5      U R                  U   l        M$     g)z
Bind this DimList to specific sizes.

Args:
    sizes: Sequence of sizes for each dimension

Raises:
    ValueError: If sizes is not a sequence
__len____getitem__zexpected a sequenceN)hasattrrG   r<   r\   r]   r:   rW   r$   )r_   r5   r$   r2   dim_sizes        r   bindDimList.bind   sc     ui((}0M0M2335zd$U+KA!$XDJJqM ,r   c                d    U R                   (       d  [        S5      e[        U R                  5      $ )NDimList not bound)rX   rh   r<   rW   r_   s    r   _sizeDimList._size   s#    {{$%8994::r   c                "    U R                  5       $ )z7Return the size (number of dimensions) of this DimList.rt   rs   s    r   r$   DimList.size   s    zz|r   c                    Xl         g)z(Set the bound status (for internal use).NrX   )r_   bs     r   
_set_boundDimList._set_bound   s    r   c                    U R                   $ )z&Property to check if DimList is bound.rz   rs   s    r   is_boundDimList.is_bound   s     {{r   c                "    U R                  5       $ )z!Return the length of the DimList.)r$   rs   s    r   rk   DimList.__len__   s    yy{r   c                   U R                   (       d  [        S5      e[        U[        5      (       a9  US:  d  U[	        U R
                  5      :  a  [        S5      eU R
                  U   $ [        U[        5      (       ae  UR                  [	        U R
                  5      5      u  p#n/ n[        X#U5       H!  nUR                  U R
                  U   5        M#     [        U5      $ [        S5      e)Nrr   r   zindex out of boundszexpected an int or a slice)rX   rh   r[   r:   r<   rW   
IndexErrorsliceindicesrH   rI   rJ   rG   )r_   keystartstopsteprP   r2   s          r   rl   DimList.__getitem__   s    {{$%899c3Qw#TZZ0 !677::c?"U## #C

O <EF5-djjm, .= 9::r   c                    U R                   (       a&  SSR                  S U R                   5       5       S3$ U R                  b  SU R                   3$ g),Return string representation of the DimList.(z, c              3  8   #    U  H  n[        U5      v   M     g 7fN)repr).0rZ   s     r   	<genexpr>#DimList.__repr__.<locals>.<genexpr>  s      Ajscjs   )*z<unbound_dimlist>)rX   joinrW   rV   rs   s    r   __repr__DimList.__repr__  sL    ;;tyy Adjj AAB!DDZZ#tzzl## 'r   c                "    U R                  5       $ )r   )r   rs   s    r   __str__DimList.__str__  s    }}r   c                .    [         R                  XX45      $ r   )_Tensor__torch_function__)clsfunctypesargskwargss        r   r   DimList.__torch_function__  s     ))$tDDr   )rX   rW   rV   NN)r`   zOptional[Union[int, Sequence]]r#   rU   )rQ   listr;   None)r$   r:   r;   r   )r5   zSequence[int]r;   r   r;   r:   )r{   r   r;   r   r;   r   )r   zUnion[int, slice]r;   Union[Dim, tuple[Dim, ...]]r;   strr   N
r   r   r   rJ   r   rJ   r   zOptional[dict]r;   r   )__name__
__module____qualname____firstlineno____doc____annotations__rc   r^   r\   ro   rt   r$   r|   propertyr   rk   rl   r   r   classmethodr   __static_attributes__r   r   r   rS   rS      s     L 7;"!3! !:
1,/&
  ;"
' 
 !%EE E 	E
 E 
E Er   rS   c                    [        U S9nUbn  [        U[        5      (       a  UR                  U5        U$ UR                  [	        U5      5        [        U5       H  u  p4Uc  M
  XBR                  U   l        M     U$ )z>Create a DimList object with the given name and optional size.)r#   )rS   r[   r:   r\   r<   r]   rW   r$   )r#   r$   dimlistr2   ss        r   _create_dimlistr   (  su     4 GdC  T" N	 SY'!$=,-MM!$) ( Nr   c                  ^^ SnSmUb  [        U5      nU b  U n[        R                  " 5       nUc  [        S5      eUR                  n Uc  [        S5      eUR
                  nUR                  n[        XE5      m[        R                  S:  a$  TR                  5       S:X  a  TR                  5         TR                  5         [        TR                  5       5      (       a  SmO4TR                  5       S	:X  a   TR                  5       mTR                  5         US:X  a  TS:X  a  [        S
5      eTnTU:w  a  SmSUU4S jjnUb)  [        U5      U:w  a  [        SU S[        U5       35      eUS:X  a!  U" S5      n[!        Xqb
  US   5      A$ S5      A$ / n[#        U5       H.  n	U" U	5      nUb  X   OSn
UR%                  [!        Xz5      5        M0     ['        U5      A$ ! Af = f)zh
Create and return one or more DimList objects.

Similar to dims() but creates DimList objects instead.
r"   r   Nr'   r(   r)   r,   r   r-   zXdimlists() must be assigned to a sequence of variable names or have argument n specifiedc                x   > S nU T:  a  TR                  5       nU(       d	  SU  3nSmU$ TR                  5         U$ r/   )r#   r1   )r2   r#   r3   r4   s     r   r6   dimlists.<locals>.genobjecto  sD    D;||~1#w K Kr   r8   r9   )r2   r:   r;   r   )r<   r=   r>   r?   r@   rA   rB   r   rC   rD   r   r1   r   rE   rF   rG   r   rH   rI   rJ   )rK   r5   rL   rM   rN   rO   r6   r#   rP   r2   r$   r3   r4   s              @@r   dimlistsr   9  s    OK e*}  "E}899LLEA=;<<|| -w&~~9, 	 ())K^^!22!--/KLLNb a!n  *O/)K	 	 U!>y(99J3u:,WXX aQ<D"45FqQ  MQQ  'AQ<D$058dDMM/$56 (
 V} Es   
EG  G  A	G   G#c                      \ rS rSrSrg)DimensionMismatchErrori  r   Nr   r   r   r   r   r   r   r   r   r         r   r   c                      \ rS rSrSrg)rh   i  r   Nr   r   r   r   rh   rh     r   r   rh   )op_propertiesc                 .   SSK n/ nU  Hp  n[        US5      (       aB  S[        [        U5      5      ;   a*  UR	                  S[        U5      R
                   S35        MV  UR	                  [        U5      5        Mr     [        U0 UDSUR                  0D6  g)z;Safe print that avoids recursive torch function dispatches.r   N	__class__torch<>file)rC   rm   r   typerI   r   printstderr)r   r   rC   	safe_argsargs        r   _safe_printr     s~     I3$$CS	N)Bqc!3!3 4A67SX&	  
900SZZ0r   c                      \ rS rSrSS jrSS jr\SS j5       r\SS j5       rSS jr	\
  S         SS	 jj5       r\          SS
 j5       rSS jrSS jr      SS jrSS jrSrg)r   i  c                    [        S5      e)Nz+_get_levels must be implemented by subclassNotImplementedErrorrs   s    r   _get_levels_Tensor._get_levels      !"OPPr   c                    [        S5      e)Nz+_get_tensor must be implemented by subclassr   rs   s    r   _get_tensor_Tensor._get_tensor  r   r   c                    [        S5      e)Nz$ndim must be implemented by subclassr   rs   s    r   ndim_Tensor.ndim  s    !"HIIr   c                B    [        S U R                  5        5       5      $ )Nc              3  n   #    U  H+  oR                  5       (       a  M  UR                  5       v   M-     g 7fr   )is_positionalrZ   r   ls     r   r   _Tensor.dims.<locals>.<genexpr>  s#     R&8@QWQUUWW&8s   55)rJ   r   rs   s    r   rQ   _Tensor.dims  s    Rd&6&6&8RRRr   c                    U R                   $ r   )r   rs   s    r   rZ   _Tensor.dim  s    yyr   r   Nc                   Uc  0 n[         (       Ga  U[        R                  R                  L Ga  [	        U5      S:X  Ga  U(       Gd  [        US   [        [        R                  45      (       Ga  [        US   [        [        R                  45      (       Gae  [        R                  " US   SSS9n[        R                  " US   SSS9nU(       Ga+  U(       Ga#  UR                  Gb  UR                  Gb  UR                  R                  5       S:X  a  UR                  R                  5       S:X  a  UR                  R                  5       (       a  UR                  R                  5       (       a  UR                  =(       d    UR                  n/ nUR                   H  n	X;  d  M
  UR                  U	5        M     UR                   H  n	X;  d  M
  UR                  U	5        M     [        R                  XX5      $ U[        R                  R                   L a  SSKJn
  U
" XX#U5      $ U[        R                  R&                  L a@  SSKJn  [	        U5      S:X  a  U" US   US   US   5        g [+        S	[	        U5       35      eU[        R                  R,                  L a  US   R/                  S5      $ U[        R0                  L a  [1        U0 UD6$ U[        R2                  L a  [3        U0 UD6$ U[        R                  R4                  L dM  U[        R6                  R4                  L d0  U[        R6                  R8                  L d  U[        R4                  L a  [5        U0 UD6$ [        R;                  XX45      $ )
N   r   r   Fensure_batchedensure_present)getitemsetitemr*   z%Expected 3 args for __setitem__, got )DOT_OPTIMIZEDr   Tensor__mul__r<   r[   r   r   creater   rZ   is_floating_point
has_devicelevelsrI   create_delayedrl   functorch.dim._getsetitemr   __setitem__r   rG   rk   r$   softmaxstacksplit_VFsplit_with_sizes_torch_function_fallback)r   r   r   r   r   lhs_inforhs_infor   r   levelr   r   s               r   r   _Tensor.__torch_function__  s    >F=TU\\%9%99 D	QtAw%,,(?@@tAw%,,(?@@ &,,GE% &,,GE%
   3 3 ++-2 ++-2 !99;;$OO==?? &.%8%8%OH<O<O
!#%-__E$2 &e 4 &5 &.__E$2 &e 4 &5  &44TTT5<<+++93e6::5<<+++9 4yA~Qa$q'2 #HT!TUU 5<<'''7<<?" 5== D+F++ 5;;$)&)) ELL&&&uyy&uyy111u{{"$)&))//TJJr   c                  ^^^ [         =(       a    U [        R                  ;   n[        X#45      u  pVSm/ n/ mU H  n[        R
                  " X(       + S5      n	UR                  U	5        U	(       d  M9  U(       d  U	R                  c   eTc  U	R                  (       a  U	R                  mU	R                   H3  n
[        U
[        5      (       d   eU
T;  d  M"  TR                  U
5        M5     M     U(       a  [        U5       H{  u  pU	(       d  M  U	R                  c  M  U	R                  nTb,  U	R                  (       d  UR                  TR                  5      n[!        XR                  T5      n[#        U5      X['   M}     [%        XV5      u  pU " U0 UD6nSUU4S jjn[        U[&        R(                  5      (       a  U" U5      $ [+        UU5      $ [-        T5       m[        U5       H  u  pU	(       d  M  U	R                  c  M  U	R                  nTb,  U	R                  (       d  UR                  TR                  5      nTR/                  UU	R                  5        [#        U5      X['   M     [%        XV5      u  pU " U0 UD6nSUU4S jjn[        U[&        R(                  5      (       a  U" U5      sSSS5        $ [+        UU5      sSSS5        $ ! , (       d  f       g= f)zGFallback torch function implementation for non-special-cased functions.NFc                x   > [        U [        R                  5      (       a  [        R                  U TTS L5      $ U $ r   )r[   r   r   from_positional)objdevice_holding_tensorresult_levelss    r   wrap_tensor5_Tensor._torch_function_fallback.<locals>.wrap_tensorC  s;    c5<<00!11],A,M  
r   c                n   > [        U [        R                  5      (       a  TR                  U TS L5      $ U $ r   )r[   r   r   from_batched)r  r	  guards    r   unwrap_tensor7_Tensor._torch_function_fallback.<locals>.unwrap_tensor_  s3    c5<<00 --c3HPT3TUU
r   )r  r   r;   r   )POINTWISE_OPTIMIZEr   	pointwiser
   r   r   rI   batchedtensorr   r   r   r[   r   r]   todevicer   r   r   r   r   r   r   inplace_update_layers)r   r   r   r   is_pointwise	flat_argsspecinfosfinfor  r2   r   mlunflat_argsunflat_kwargsrP   r  batchedr  r	  r  r
  s                       @@@r   r    _Tensor._torch_function_fallback  sb   
 *Mdm6M6M.M&~6	 $"$(*A$$Q(8%@DLLt#t'9'9'EEE(0T__,0KK)![[E%eX6666M1%,,U3 )  $U+4DKK3![[F,8!'+@+G+G!H&v{{MJB#5b#9IL , *8	)H&K;8-8F  &%,,//"6**V44 ]+u$U+4D..:"00G,8")**-B-I-I"J//E#5g#>IL , *8	)H&K;8-8F 
 &%,,//$V,) ,+,  v6- ,++s   K7KB,K<K
Kc                     SSK Jn  U" XU5      $ )z2Set values in tensor using first-class dimensions.r   r   )r   r   )r_   indexvaluer   s       r   r   _Tensor.__setitem__i  s    5tE**r   c                   [         R                  " U SSS9nU H  n[        U[        5      (       a  M  [        U [        R
                  5      (       a8  [        U [        5      (       d#  [        R
                  R                  " U /UQ76 s  $ U R                  [        R
                  R                  [        U 5      4U 4U-   5      s  $    U(       d9  U R                  [        R
                  R                  [        U 5      4U 4U-   5      $ UR                  nUc9  U R                  [        R
                  R                  [        U 5      4U 4U-   5      $ UR                  n/ n/ n/ nU H  n	U H<  n
U
R                  5       (       a  M  U
R                  5       U	L d  M/  [        SU	 S35      e   U H<  nUR                  5       (       a  M  UR                  5       U	L d  M/  [        SU	 S35      e   UR                  [!        U	5      5        UR                  U	R"                  5        UR                  S5        M     UR%                  U5        ['        UR#                  5       5      n['        UR)                  5       5      nUR%                  U5        UR%                  U5        UR+                  XxUR-                  5       5      n[
        R/                  XUR0                  5      nU$ )a	  
Expand tensor by adding new dimensions or expanding existing dimensions.

If all arguments are Dim objects, adds new named dimensions.
Otherwise, falls back to regular tensor expansion behavior.

Args:
    args: Either Dim objects for new dimensions or sizes for regular expansion

Returns:
    New tensor with expanded dimensions

Example:
    >>> i, j = dims()
    >>> t = torch.randn(3, 4)
    >>> expanded = t[i].expand(j, k)  # Add j, k dimensions
    >>> expanded2 = t[i].expand(2, 4)  # Regular expand with sizes
Fr   zexpanding dimension z# already exists in tensor with dimsr   )r   r   r[   r    r   r   r   expandr   r   r   r   r   rZ   rh   rI   r   r$   extendr   stride
as_stridedstorage_offsetr  r   )r_   r   r  r   datar   
new_levels	new_sizesnew_stridesr0   r  	new_level
orig_sizesorig_stridesexpanded_datarP   s                   r   r(  _Tensor.expandr  sk   &   eERCc3''dELL11*T7:S:S <<..t;d;;22++d4j]TGdN   **##d4j]TGdN 
 {{<**##d4j]TGdN  %'
	A**,,1A,.qc1TU   
 (	 ..00Y]]_5I,.qc1TU  ( hqk*QVV$q! $ 	&! $))+&
DKKM*$<( 	@S@S@UV ''4??Sr   c                  ^ ^% SSK Jn  SSKJnJn  SSKJm%  SSS jjnSU%U 4S jjnSU 4S jjn/ n	/ n
[        U[        [        45      n[        U[        [        45      nU(       aj  U(       ac  UnUn[        U5      [        U5      :w  a$  [        S	[        U5       S
[        U5       S35      eU	R                  U5        U
R                  U5        O"U	R                  U5        U
R                  U5        [        R                  " T SS5      n/ n/ n/ n[!        [        U	5      5       GH  nU" U	U   SS9u  nnU(       Ga~  [        U5      S:X  a  UR                  [#        5       5        MC  U" US   5      nUR                  U5        [        U5      S:X  a  Mp  [        U5      S:X  a  UR                  UR$                  5        / n[!        S[        U5      5       Hi  nU" UU   5      nSn[!        [        U5      5       H!  nUU   U:X  d  M  UR'                  U5        Sn  O   U(       d  U" U5        UR                  U5        Mk     Sn[!        [        U5      5       H  nUU   U:X  d  M  Un  O   Uc  U" U5        GMV  [)        U5       H  u  nnUR+                  US-   U-   U5        M      UR                  U5        GM  UR                  U" U	U   5      5        GM     [        U5      S:  a  UR,                  c   S5       eU" UR,                  UR$                  U5      nUR/                  5       n/ n / n![!        [        U5      5       Hk  nUU   U;   a7  [        U 5      S:X  a  U R                  UU   5        M1  U S==   UU   -  ss'   MC  U R                  UU   5        U!R                  UU   5        Mm     UR1                  U 5      Ul        U!Ul        Sn"U
 H"  n#[        U#[        [        45      (       d  M   Sn"  O   U" U/ UU
U"5      n$U" U$5      $ )z,
Index tensor using first-class dimensions.
r   )r   )getsetitem_flatinvoke_getitem	_wrap_dimFc                T    [        U [        [        45      (       a  [        U 5      S4$ g)NT)NF)r[   rJ   r   )r  check_firsts     r   maybe_dimpack$_Tensor.index.<locals>.maybe_dimpack  s$    #t}--Cy$&r   c                   > T" U TR                   S5      nUR                  5       (       a  [        S[        U 5       35      eU$ )NFz)expected a dimension specifyer but found )r   is_none	TypeErrorr   )r   r0   r:  r_   s     r   parse_dim_entry&_Tensor.index.<locals>.parse_dim_entry  s=    !TYY.Ayy{{"KDQRG9 UVVHr   c                   > U R                  5       (       a7  [        SU R                  5       TR                  -    STR                   S35      e[        S[	        U R                  5       5       S35      e)N
dimension z not in tensor of z dimensionsz not in tensor)r   rA  positionr   r   rZ   )r0   r_   s    r   dim_not_present&_Tensor.index.<locals>.dim_not_present  se       		!9 ::LTYYKWbc   *T!%%']O> JKKr   zdims (z) and indices (z) must have the same length)r<  r   TNz2Cannot perform dimension flattening on None tensorr"   )F)r  r   r<  r   r;   ztuple[Any, bool])r   r   r;   r   )r0   r   r;   r   )
_dim_entryr   _getsetitemr7  r8  _wrapr:  r[   rJ   r   r<   rA  r)  rI   r   r   rH   r   r   popr]   insertr   r$   reshape)&r_   rQ   r   r   r7  r8  r=  rB  rG  	dims_listindices_listlhs_listrhs_listdims_seqindices_seq	self_infor.  
to_flattendims_list_flatr2   m
is_dimpackfirstrestjr0   removedk	first_idxr
rearrangedr5   r/  reshape_levelshas_dimpacksidxr  r:  s&   `                                    @r   r$  _Tensor.index  s    	.@$	
	 		L ,.	>@dUDM2gt}5H!K8}K 00S]O?3{;K:LLgh  X&,T"( %%dE59	 "
 "
 s9~&A))A,EJMAzq6Q;"))(*5'!-%%e,q6Q; z?a'%%i&6&67q#a&)A'!-A#G"3z?3%a=A-&NN1-&*G!	 4
 #'*KKN * !	s:/A!!}-$%	 0 $#E*%dODAq%%i!ma&7; ,!!$'%%oil&CDY '^ z?Q##/ D/ 'y'7'79I9I:VJOO%E#%IN3z?+a=J.9~*!((q2!"q1$$U1X."))*Q-8 ,  *11)<I-I C#t}--#   
 d##r   c                8   U R                  5       U R                  5       U R                  p2n/ nU H  n[        US5      (       a9  UR	                  5       (       a$  UR                  UR                  5       U-   5        MM  [        US5      (       a!  UR                  UR                  5       5        M  [        US5      (       a  UR                  UR                  5        M  UR                  U5        M     U S[        U5       S[        UR                  5       5       3$ )Nr   rZ   r-  z
with dims=z sizes=)r   r   r   rm   r   rI   rF  rZ   r-  rJ   r$   )r_   r   r   r   	dims_reprr   s         r   r   _Tensor.__repr__a  s    #//143C3C3Etyy	Aq/**q/@/@  !45E""  )F##  (  #  eI&6%7wuV[[]?S>TUUr   r;   	list[Any]r;   Optional[torch.Tensor]r   )r;   ztuple[Any, ...]r   r   )
r   r   r   rJ   r   rJ   r   dictr;   r   )r$  r   r%  r   r;   r   )r   r    r;   r   )rQ   zCUnion[int, Dim, tuple[Union[int, Dim], ...], list[Union[int, Dim]]]r   zsUnion[int, slice, torch.Tensor, tuple[Union[int, slice, torch.Tensor], ...], list[Union[int, slice, torch.Tensor]]]r;   r   r   )r   r   r   r   r   r   r   r   rQ   rZ   r   r   staticmethodr   r   r(  r$  r   r   r   r   r   r   r     s   QQ J J S S 
 !%ZKZK ZK 	ZK
 ZK 
ZK ZKx K7K7$K7,1K7;?K7	K7 K7Z+SjX$QX$
X$ 
X$tVr   r   c                     \ rS rSr% S\S'   S\S'   S\S'   S\S'   S\S	'   SSS
 jjr\SS j5       r\SS j5       r	\SS j5       r
\
R                  SS j5       r
\SS j5       rSS jrSS jrSS jr\R"                  rSrg)r    it  r:   _levelr   rV   rt   rl  _range_batchtensorc                `    Xl         X l        [        U l        [        S-  qS U l        S U l        g Nr   )rV   rt   _n_dims_createdrp  rq  rr  )r_   r#   r   s      r   rc   Dim.__init__{  s,    

%1 r   c                    grt  r   rs   s    r   r   Dim.ndim  s    r   c                    [        U5      U L $ r   r   )r   r  s     r   check_exactDim.check_exact  s    CyCr   c                l    U R                   S:X  a  [        SU R                   S35      eU R                   $ )Nr"   rE  z is unbound)rt   rG   rV   rs   s    r   r$   Dim.size  s0    ::z$**[ABBzzr   c                    U R                   S:X  a  Xl         g U R                   U:w  a'  [        S[        U 5       SU R                    SU 35      eg )Nr"   zDim 'z*' previously bound to a dimension of size z$ cannot bind to a dimension of size )rt   rh   r   )r_   vs     r   r$   r~    sV    ::JZZ1_$T
|#Mdjj\ Z667S:  r   c                     U R                   S:g  $ )z1Return True if this dimension is bound to a size.r"   rw   rs   s    r   r   Dim.is_bound  s     zzRr   c                ~    U R                   c%  [        R                  " U R                  5      U l         U R                   $ )z
Get a tensor representing the range [0, size) for this dimension.

Returns:
    A 1D tensor with values [0, 1, 2, ..., size-1]
)rq  r   aranger$   rs   s    r   
_get_rangeDim._get_range  s,     ;;,,tyy1DK{{r   c                    U R                   cH  [        R                  R                  R	                  U R                  5       SU R                  5      U l         U R                   $ )zu
Get a batched tensor representation of this dimension.

Returns:
    A batched tensor created from the range tensor
r   )rr  r   _C
_functorch_add_batch_dimr  rp  rs   s    r   _get_batchtensorDim._get_batchtensor  sM     $ % 3 3 B B!1dkk!D    r   c                    U R                   $ )z&String representation of a Dim object.)rV   rs   s    r   r   Dim.__repr__  s    zzr   )rr  rp  rV   rq  rt   N)r"   )r#   r   r   r:   r;   r   r   )r  r   r;   r   )r  r:   r;   r   r   r;   torch.Tensorr   )r   r   r   r   r   rc   r   r   r   r{  r$   setterr   r  r  r   object
__format__r   r   r   r   r    r    t  s    KJJ""((!        
 
[[     	! ""Jr   c                     \ rS rSr% S\S'   S\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   \SS j5       r\SS j5       r\        SS j5       r	\          SS j5       r
SS jrS S jrS!S jrSS jr      S"S jrS#S jrSrg)$r   i  rl  _tensorrr  list[DimEntry]_levelsr   _has_devicez$Optional[Callable[[], torch.Tensor]]_delayedzOptional[Callable]_delayed_origzOptional[tuple]_delayed_argsc                :    [        S U R                   5       5      $ )Nc              3  R   #    U  H  oR                  5       (       a  S OSv   M     g7f)r   r   N)r   r   s     r   r   Tensor.ndim.<locals>.<genexpr>  s     G,Q))1q0,s   %')sumr  rs   s    r   r   Tensor.ndim  s    G$,,GGGr   c                    [        U5      U L $ r   rz  )r   others     r   r{  Tensor.check_exact  s    E{c!!r   c                   SnSnU Hg  nUR                  5       (       aJ  US:X  d2  US-   UR                  5       :X  d   SU SUR                  5        35       eUR                  5       nMb  US-  nMi     US:X  d  US:X  d
   SU 35       eU(       d  U$ U " 5       nXl        X'l        X7l        SUl        SUl        SUl        SUl        UR                  5       [        U5      :X  d%   SUR                  5        S	[        U5       S
35       eU$ )a  
Create a functorch Tensor from a regular PyTorch tensor with specified dimension levels.

This is the primary way to create Tensor objects with first-class dimensions.

Args:
    tensor: The underlying PyTorch tensor
    levels: List of DimEntry objects specifying the dimension structure
    has_device: Whether the tensor is on a device (not CPU)

Returns:
    A new Tensor instance with the specified dimensions, or a regular torch.Tensor
    if there are no named dimensions
r   r   z/Positional dimensions must be consecutive, got z then r"   z0Final positional dimension must be 0 or -1, got NzTensor has z dimensions but z levels provided)r   rF  r  r  r  rr  r  r  r  rZ   r<   )r   r   r   r   	seen_dimslastr   rP   s           r   r  Tensor.from_positional  s+   $ 	A  qyD1H

$< EdV6RSR\R\R^Q_`< zz| Q	  qyDBJ 	
>tfE	
& M '"## zz|s6{* 	
&**,'7F}DTU	
* r   c                   ^^ U " 5       nSUl         X5l        XEl        SUl        TUl        TUl        SUU4S jjnXel        U$ )z@
Create a delayed tensor that defers the operation until later.
Nc                    > / n T HF  n[        US5      (       a!  U R                  UR                  5       5        M5  U R                  U5        MH     T" U 6 $ )Nr   )rm   rI   r   )unwrapped_argsr   r   origs     r   evaluate_delayed/Tensor.create_delayed.<locals>.evaluate_delayed  sO    N3.."))#//*;<"))#.	 
 ((r   r  )r  r  r  rr  r  r  r  )r   r  r   r   r   rP   r  s    ``    r   r   Tensor.create_delayed  sL     '"##	) 	) +r   c                    [        U S5      (       aD  U R                  b7  U R                  c*  U R                  5       U l        SU l        SU l        SU l        U R                  $ )zAGet the underlying tensor, handling delayed operations if needed.r  N)rm   r  r  r  r  rs   s    r   r   Tensor._get_tensor,  sU     D*%%)$  ==?DL DM!%D!%D||r   c                    U R                   $ )zGet the dimension levels.)r  rs   s    r   r   Tensor._get_levels;  s    ||r   c                    U R                   $ )z/Get whether this tensor has device information.)r  rs   s    r   _get_has_deviceTensor._get_has_device?  s    r   c                    U R                   c3  U R                  U R                  5       U R                  5       5      U l         U R                   $ )zDGet the batched tensor representation, creating it lazily if needed.)rr  _add_batch_dimsr   r   rs   s    r   r  Tensor._get_batchtensorC  sF    $ $ 4 4  "D$4$4$6!D    r   c                   [        U5      n SnSn[        S5      nSnSn[        U5       Hr  u  pU	R                  5       (       a  M  U	R	                  5       (       d<  U	R                  5       R                  U:  a  U	R                  5       R                  nUnUnUS-  nMt     US:X  a  U$ Uc   e[        R                  R                  R                  X[        U5      5      n[        5       X4'   M  )Nr"   infr   r   )r   floatr]   r@  r   rZ   rp  r   r  r  r  r:   r   )
r_   tlevels_r   min_real_index	min_index	min_valuer2   r`  r   s
             r   r  Tensor._add_batch_dimsK  s     gNIeIAA!&)yy{{??,,)1K$%EEGNN	$%	)*FA * B= =##221YPA%-ZF"+ r   c                $    SSK Jn  U" U /UQ76 nU$ )z&Reorder the dimensions of this tensor.r   )order)_orderr  )r_   rQ   r  rP   s       r   r  Tensor.orderh  s    !t#d#r   )rr  r  r  r  r  Nr   )r  r   r;   r   )r   r  r   r  r   r   r;   Union[_Tensor, torch.Tensor])
r  r   r   rJ   r   r  r   r   r;   r   rk  ri  r   )r  rl  r  rj  r;   rl  )rQ   r   r;   r   )r   r   r   r   r   r   r   r   r{  r  r   r   r   r  r  r  r  r   r   r   r   r   r     s    ##((22%%""H H " " 6!6+96GK6	%6 6p #(2@NR	 8 !'2;	:r   r   c                   U (       d  [        S5      e[        U[        5      (       d  [        R                  " XS9nU$ / n/ nU  HU  n[
        R                  " USSS9nUR                  U5        UR                   H  nX;  d  M
  UR                  U5        M     MW     [        U 5      Ul
        / n	U HI  nUR                  c   S5       e[        UR                  UR                  U5      n
U	R                  U
5        MK     [        U5      nSnUb>  [        U[        5      (       a  US:X  d#  SS	KJn  U" X+S5      n UR#                  U5      nUn[        R                  " X5      nUR'                  U[)        U5      5        [*        R-                  X4U(       a  US   R.                  5      nU$ S5      nU$ ! [          a    [%        S
U S35      Sef = f)a  
Stack tensors along a new dimension.

Args:
    tensors: Sequence of tensors to stack
    new_dim: The new Dim to create for stacking
    dim: The dimension position to insert the new dimension (default: 0)

Returns:
    Stacked tensor with the new dimension
z-stack expects a non-empty sequence of tensors)rZ   Fr   Nz*Cannot stack tensors with None tensor datar   r   r9  z
Dimension z does not exist in inputsT)rG   r[   r    r   r   r   r   rI   r   r<   r$   r   r   r   r:   rK  r:  r$  rA  rM  r   r   r  r   )tensorsnew_dimrZ   rP   r
  r  r  r  r  inputsmatched_tensorr   rawdimr:  r0   rd  tensor_results                    r   r   r   p  s    HII gs##W. ME  5OT[[E)$$U+ !  w<GL F{{&T(TT&&t{{DKKOn%  -(DF

3 4 4$c'	S%%a(C  [[(F '!23 **euQx22M  BFM   	Sj-FGHdR	Ss   9F1 1Gc           	        SSK Jn  [        U[        5      n[        U[        5      (       aO  U(       a  [        S5      e[        R                  [        R                  R                  [        U 5      4X4SU05      $ / nSnSnU H/  nUR                  U5        [        U[        5      (       a  SnM-  SnM1     U(       aO  U(       a  [        S5      e[        R                  [        R                  R                  [        U 5      4X4SU05      $ U(       d  [        S5      e[        R                  " U SSS9n	U	R                  5       n
U(       d  U
S	:X  a  [        S
5      eUb	  U" X*S5      O[!        U
* 5      nSn[#        U	R$                  5       H  u  pX:X  d  M  Un  O   Uc  Uc  S	n[        SU 35      e/ nS	n/ n[#        U5       H^  u  nnUR&                  (       a%  UR                  UR(                  5        UUS   -  nM<  UR                  S	5        UR                  U5        M`     U	R*                  c   S5       eU	R*                  R)                  U5      nU(       ad  UU:  a  [        SU SU S35      eUU-
  nU[-        U5      -   S-
  [-        U5      -  nU H#  n[/        UU5      nUUU   l        UUU'   UU-  nM%     OUU:w  a  [        SU SU S35      eU	R*                  R1                  X5      n/ n[3        U	R$                  5      n[#        [5        UU5      5       HP  u  nu  nn[!        U5      UU'   UR                  [        R7                  U[3        U5      U	R8                  5      5        MR     [;        U5      $ )z
Split tensor along a dimension.

Can handle both regular integer sizes and Dim objects for split sizes.
When Dim objects are used, they get bound to the resulting tensor dimensions.
r   r9  zKwhen dim is specified as a Dim object, split sizes must also be dimensions.rZ   TFz-split list must be ints or dims but got a mixr   r   z+split expects at least a 1-dimension tensorNz"tensor does not contain dimension r"   z%Cannot get tensor size on None tensorz+sizes of target dimensions add up to more (z) than source dim (r   z#sum of sizes of target dimensions (z) do not match the source dim ()rK  r:  r[   r    r:   rA  r   r   r   r   r   r   rI   r   r   r   r   r]   r   r   r$   r   r<   minr   r   zipr  r   rJ   )r   split_size_or_sectionsrZ   r:  dim_is_objectr5   all_dimsall_intsra   rU  r   dim_lrd  r2   r  r   
total_sizeunboundsize_dimtensor_sizeremaining_size
chunk_sizeuszresult_tensorsrP   r.  result_tensors                               r   r   r     s    ! sC(M (#..]  //LL&\O,CL	
 	
 EHH&TdC  HH ' ]  //LL&\O,CL	
 	
 GHH !!&uUI>>DTQYEFF ,/?Ic'$E Ci../>C 0
 {;C<SEBCC GJG '8NN8==)'"+%JNN1NN1 ( 'P)PP'""'',K #=j\I\]h\iijk  %z1$s7|3a7CLH
AZ0BE!HMGAJb N	 
 

	"1*=\]h\iijk
 	

 %%66wDN Fi&&'J(1#ne2L(M$$M8"8,
3""tJ/1E1E	
 )N =r   c                R    [        S5      n[        XU5      R                  X1/U5      $ rt  )rQ   r   r$  )r  rZ   r  rK   s       r   catr  4  s'    QAS!'''::r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	DotParti9  z9
Helper class for organizing dimensions in dot products.
c                     / U l         SU l        g rt  rQ   r  rs   s    r   rc   DotPart.__init__>  s    $&	r   c                    U R                   R                  U5        UR                  5       (       d.  U =R                  UR	                  5       R
                  -  sl        gg)z#Add a dimension entry to this part.N)rQ   rI   r   r  rZ   r$   )r_   	dim_entrys     r   rI   DotPart.appendB  sB    		#&&((OOy}}333O )r   r  N)r;   r   )r  r   r;   r   )r   r   r   r   r   rc   rI   r   r   r   r   r  r  9  s    4r   r  c                n   / nSnU  H9  n[        UR                  5      S:w  a  SnUR                  UR                  5        M;     UR                  c  [	        S5      e[        UR                  UR                  U5      nU(       d  U$ U  Vs/ s H  oDR                  PM     nnUR                  U5      $ s  snf )zB
Prepare tensor for dot product by matching levels and reshaping.
Fr   Tz)Cannot perform dot product on None tensor)	r<   rQ   r)  r   r?   r   r   r  rN  )partstensor_infor.  needs_reshapepartrP   views          r   dot_preparer  I  s     JMtyy>Q M$))$ 
 !FGG;--{/A/A:NF )..OOD.>>$ /s   
B2c                P   / nSnU  H9  n[        UR                  5      S:w  a  SnUR                  UR                  5        M;     U(       aE  / nU H,  nUR                  UR	                  5       R
                  5        M.     UR                  U5      n[        R                  XS5      nU$ )z=
Finish dot product by reshaping result and creating Tensor.
Fr   T)	r<   rQ   r)  rI   rZ   r$   rN  r   r  )r  r  r
  r  r  new_sizer  r  s           r   
dot_finishr  a  s     MMtyy>Q MTYY' 
 "EOOEIIK,,- #%--h7**=NMr   c                  ^^^^^^^ [         R                  " U SSS9n[         R                  " USSS9nU(       a  U(       d  [        R                  " X5      $ UR                  b  UR                  c   S5       eUR                  R                  5       mUR                  R                  5       m[        5       m[        5       m[        5       m[        5       mS	UUUUUUU4S jjnS/[        UR                  5      -  n[        UR                  5       H;  u  pxSn	[        UR                  5       H  u  pX:X  d  M  U
n	SXj'     O   U" XU	5        M=     [        UR                  5       H  u  p{Xg   (       a  M  U" USU5        M     [        TR                  5      [        T5      :w  a7  T H1  nXR                  ;  d  M  XR                  ;  d  M%  [        SU 35      e   [        TR                  5      S:w  aC  [        TTT/U5      n[        TTT/U5      n[        R                  " X5      n[        TTT/U5      $ [        TT/U5      n[        TT/U5      n[        R                  " X5      n[        TT/U5      $ )
z
Perform dot product between two tensors along specified dimensions.

Args:
    lhs: Left-hand side tensor
    rhs: Right-hand side tensor
    sum_dims: Dimensions to sum over (contract)

Returns:
    Result of dot product
Fr   Nz*Cannot perform dot product on None tensorsc                   > U T;   nUb  TU   OSnUb  T
U   OSnU(       a  TR                  U 5        gUS:H  US:H  :X  a  T	R                  U 5        gUS:w  a  TR                  U 5        gTR                  U 5        g)z?Insert dimension into appropriate part based on stride pattern.Nr   )rI   )r0   lhs_idxrhs_idxreduced
lhs_stride
rhs_stridelhs_strideslo_dimslr_dimslro_dimsrhs_stridesro_dimssum_dimss         r   
insert_dimdot.<locals>.insert_dim  sx    x--4-@[)a
-4-@[)a
NN1aZ1_5"qq!q!r   Tz$summing over non-existent dimension r   )r0   r   r  r   r  r   r;   r   )r   r   r   matmulr   r*  r  r<   r   r]   rQ   rG   r  bmmr  mm)lhsrhsr  r  r  r  rhs_seenr2   	lhs_levelr  r\  	rhs_levelr0   
lhs_tensor
rhs_tensorrP   r  r  r  r  r  r   s     `             @@@@@@r   dotr  w  s+      U5QH  U5QH||C%%??&8??+F 4F //((*K//((*K yHiGiGiG" "" wX__--H "(//2%hoo6LA%"	 7 	9) 3 "(//2{{y$* 3
 7<<CM)A'A__,D #Gs!KLL 
 8==Q (GW!=xH
 (GW!=xH
:28Wg6?? !'7!3X>
 '7!3X>
*17G,f55r   )rK  )	wrap_typec                B   [        U [        5      (       a  [        R                  XU5      $ [        R                  " U SSS9nUR
                  c   S5       e[        R                  UR
                  UR                  UR                  5      n[        R                  XAU5      $ )al  
Index a regular tensor by binding specified positions to dims.

This converts a regular tensor to a first-class tensor by binding
the specified positional dimensions to Dim objects.

Args:
    positions: Tuple of dimension positions to bind
    dims: Dim objects or tuple of Dim objects to bind to

Returns:
    First-class tensor with specified dimensions bound
Fr   zCannot index None tensor)
r[   r   r$  r   r   r   r   r  r   r   )r_   	positionsrQ   r  rP   s        r   r$  r$    s     $  }}Td33 T%ND ;;">$>>"##DKKdooNF ==D11r   c           	     t    [        [        R                  U 5      n[        [        U [        U/UQ70 UD65        g r   )getattrr   r   setattrr   rK  )r#   r   r   r  s       r   _defr    s-    5<<&DGT57778r   meanr  allamaxaminaminmaxanycount_nonzero	logsumexpnanmeannansumprodstdr   )keepdim_offsetvarmax)
single_dimr  argmaxargminkthvaluemedian	nanmedianmodesortF)reduceargsortunbindchunk)
dim_offsetr-  cummax)r%  r-  cummincumprodcumprod_cumsumcumsum_logcumsumexprenorm)r1  r%  r-  r   )r   rU   r;   r   )r   r  r;   r  r   )r#   r   r$   Optional[int]r;   r    r   )rK   r:  r5   Optional[list[Optional[int]]]r;   r   )r#   r   r$   z)Optional[Union[int, list[Optional[int]]]]r;   rS   )rK   r:  r5   r;  r;   z#Union[DimList, tuple[DimList, ...]])r   r   r   r   r;   r   )r   )r  r   r  r   rZ   r:   r;   r   )r   r   r  r   rZ   r   r;   rJ   )r  r   rZ   r   r  r   r;   r   )r  list[DotPart]r  r   r;   r  )r  r<  r  r  r;   r   )r  r   r  r   r  r   r;   r  )r_   r   r  r   rQ   r   r;   r   )r#   r   r   r   r   r   r;   r   )B
__future__r   disr=   rC   typingr   r   r   r   collections.abcr   r	   r   torch.utils._pytreer
   r   r   rI  r   r   r   _enable_all_layersr   _py_inst_decoderr   _tensor_infor   r  r   ru  r   r   r%   rQ   rS   r   r   	Exceptionr   rh    r   r   r   r   
TensorLiker    r   r   r  r  r  r  r  functorch.dim._wraprK  functorch.dim.wrap_typer  r   r   r$  r  nn
functionalr   r   r   r   <module>rL     s   " 
  
 6 6 2  F F ? ? / , $   8

7 EI``$A` `FWE WEv BF
>$ EIVV$AV(Vr	Y 		 	 1BV BVJ u||$
K#' K#bhW hVAH}@;
4 4  0,V6r & - 
'5<<!;!; <L2<9
 V U U V V Y U _  [  Y X V U1  U1  Ut  Ut  X$  X$  ZD ! X$  [T " V  VE  Yu  X$  W5 ) X$u - X$u - Y4 . ZD / X$u - Y4 . ^U 3 X!U ; Y4 .
##++U
Kr   