
    ȅi`                    ~    S SK Jr  S SKJrJrJr  \(       a  S SKJr  S SKrSSK	J
r
JrJr  S	S
S jjr      SS jrg)    )annotations)AnyTYPE_CHECKINGUnion)SequenceN   )_match_levelsDimEntryndim_of_levelsc                    SSK Jn  U c  U(       a
  [        5       $ [        X5      (       a  [        U 5      $ [        U [        5      (       a  U S:  a  U nOX-
  n[        U5      $ [        5       $ )a  
Convert various dimension representations to DimEntry.

Args:
    arg: The argument to convert (Dim, int, or other)
    orig_ndim: Original number of dimensions
    allow_none: Whether to allow None values

Returns:
    DimEntry representation of the dimension
r   )Dimr   ) r   r
   
isinstanceint)arg	orig_ndim
allow_noner   poss        N/home/james-whalen/.local/lib/python3.13/site-packages/functorch/dim/_order.py	_wrap_dimr      s]     
{zz	C		}	C		7C/C}z    c                2  ^^^  SSK JnJnJn  [	        X5      (       a(  U R
                  SS nU R                  nU R                  nO:[	        X5      (       a  [        U 5      /nU R                  5       nSnO[        S5      e/ m/ nUSS m[        T5      m SUUU 4S jjn	Sn
U H  n[        UT S5      nUR                  5       (       d  U	" U5        U
S-  n
M4  [	        X5      (       a+  UR                   H  nU	" [        U5      5        U
S-  n
M     Mo  U
S-  n
[        US5      (       d  [        S	5      e[!        U5      nUR#                  [%        T5      [%        U5      45        U H8  n[        UT S5      nUR                  5       (       a  [        S
5      eU	" U5        M:     GM     Sn/ nT Hb  nUR                  5       (       a  M  UR'                  5       (       a"  US:X  a  [%        U5      nUR)                  T5        UR#                  U5        Md     US:X  a  [%        U5      nUR)                  T5        Uc   S5       e[+        XeU5      nU(       GaS  / nUR-                  5       n[/        U5       H  nUR#                  UU   5        M     SnU Hh  u  nnUU:  a$  UR#                  UUU-      5        US-  nUU:  a  M$  Sn[/        U5       H  nUUUU-   U-      -  nM     UR#                  U5        UU-  nMj     U[%        T5      :  a-  UR#                  UUU-      5        US-  nU[%        T5      :  a  M-  [/        U[%        T5      -   [%        T5      5       H  nUR#                  UU   5        M     [%        T5      U
-
  nUS:  a  USU UUU-   S -   nUR1                  U5      nSn[/        [%        U5      S-
  SS5       HA  nUU   R'                  5       (       d  UU:  d  M#  UUU
-   :  d  M.  US-  n[        U5      UU'   MC     UR3                  UUU5      nU$ )a  
Reorder the dimensions of a tensor or create a tensor from a dimension.

It allows reordering tensor dimensions using first-class dimensions and
positional indices.

Args:
    tensor_or_dim: Input tensor with first-class dimensions, or a Dim object
    *dims: Dimensions or sequences of dimensions specifying the new order

Returns:
    Tensor with reordered dimensions

Examples:
    >>> import torch
    >>> from functorch.dim import dims
    >>> batch, channel, height, width = dims(4)
    >>> x = torch.randn(2, 3, 4, 5)[batch, channel, height, width]
    >>> # Reorder to [height, width, batch, channel]
    >>> y = order(x, height, width, batch, channel)
r   )r   DimListTensorNFz-First argument must be a Tensor or Dim objectc                :  >  TR                  U 5      nUcU  U R                  5       (       a#  [        ST SU R                  5       T-    S35      e[        SU R	                  5        S35      e[        5       TU'   TR                  U 5        g! [         a    Sn Nf = f)zEAdd a dimension to the reordering, removing it from available levels.Nztensor has z positional dimensions, but z% specified, or it was specified twiceztensor does not contain dim z or it was specified twice)index
ValueErroris_positionalpositiondimr
   append)didxflat_positional_dimslevelsr   s     r   
append_dimorder.<locals>.append_dimX   s    	,,q/C ;   !),HXaIaHb c0 1 
 !21557);UV  js##A&  	C	s   B BBr   __iter__z+expected a Dim, List[Dim], or Sequence[Dim]zexpected a Dim or intzCannot reorder None tensor)r"   r
   returnNone)r   r   r   r   r   _levels_tensor_has_devicer
   
_get_ranger   r   r   is_none_dimshasattrlistr!   lenr   extendr	   sizerangereshapefrom_positional)!tensor_or_dimdimsr   r   r   orig_levelsdata
has_device
to_flattenr&   n_new_positionalr   entryr    seqiteminsert_point
new_levelslevelndata
view_shapesizesi	start_idxlengthnew_sizejn_to_removeseenresultr$   r%   r   s!                                 @@@r   orderrR   *   s:   0 '& -((#++A.$$"..
	M	'	'./'')
HIIJ^Fv&I' '(  #y%0}}u!%%yy8C=) A%  !
 !3
++ !NOO s)Cs#78#c(CD!$	59==??$%<==5!	 ) 6 L!#J ==??  r!":!!"67%   r:./ 999$Z8E 


 |$AeAh' % !+Ivi-!!%q(8"9:Q i-
 H6]E,"2Q"677 #h'KA ", #*++eL1$456FA #*++
 |c*>&??VMAeAh' N ./2BB? =L)J|k7Q7S,TT  j) D3z?Q&B/a=&&((!l5E&E"EAID$TNJqM 0 ##E:zBFMr   )T)r   r   r   r   r   boolr*   r
   )r:   zUnion[torch.Tensor, Any]r;   zUnion[Any, Sequence[Any]]r*   ztorch.Tensor)
__future__r   typingr   r   r   collections.abcr   torch
_dim_entryr	   r
   r   r   rR    r   r   <module>rZ      sE    " , , (  ? ?8l+l4Mllr   