
    i`                    n    d dl mZ d dlmZmZmZ erd dlmZ d dlZddl	m
Z
mZmZ d	d
dZ	 	 	 	 	 	 ddZy)    )annotations)AnyTYPE_CHECKINGUnion)SequenceN   )_match_levelsDimEntryndim_of_levelsc                    ddl m} | |r
t               S t        | |      rt        |       S t        | t              r"| dk  r| }t        |      S | |z
  }t        |      S t               S )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/var/www/html/engine/venv/lib/python3.12/site-packages/functorch/dim/_order.py	_wrap_dimr      si     
{zz	C	}	C	7C } 	/C}z    c                    ddl m}m}m} t	        | |      r(| j
                  dd }| j                  }| j                  }n6t	        | |      rt        |       g}| j                         }d}nt        d      g g }|dd t               d fd}	d}
|D ]  }t        | d      }|j                         s |	|       |
dz  }
.t	        ||      r(|j                  D ]  } |	t        |             |
dz  }
 b|
dz  }
t        |d      st        d	      t!        |      }|j#                  t%              t%        |      f       |D ]2  }t        | d      }|j                         rt        d
       |	|       4  d}g }D ]U  }|j                         r|j'                         r!|dk(  rt%        |      }|j)                         |j#                  |       W |dk(  rt%        |      }|j)                         |J d       t+        |||      }|rDg }|j-                         }t/        |      D ]  }|j#                  ||           d}|D ]b  \  }}||k  r"|j#                  |||z             |dz  }||k  r"d}t/        |      D ]  }||||z   |z      z  } |j#                  |       ||z  }d |t%              k  r+|j#                  |||z             |dz  }|t%              k  r+t/        |t%              z   t%                    D ]  }|j#                  ||           t%              |
z
  }|dkD  r|d| |||z   d z   }|j1                  |      }d}t/        t%        |      dz
  dd      D ]7  }||   j'                         s||k\  s|||
z   k  s%|dz  }t        |      ||<   9 |j3                  |||      }|S )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                ,   	 j                  |       }|P| j                         r#t        d d| j                         z    d      t        d| j	                          d      t               |<   j                  |        y# t        $ r d}Y ~w xY w)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_dimz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   orderrQ   *   sQ   0 '& -(#++A.$$"..
	M3	'./'')
HIIJ^Fv&I'(   "#y%0}}u!W%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] 8E,"2Q"6778h'KA	 #*++eL1$456FA #*++
 |c*>&??VM 	(AeAh'	( ./2BB? =L)J|k7Q7S,TT  j) D3z?Q&B/ +a=&&(!l5E&E"EAID$TNJqM+ ##E:zBFMr   )T)r   r   r   r   r   boolr)   r
   )r9   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   rQ    r   r   <module>rY      sE    " , , (  ? ?8l+l4Mllr   