Ë
    L²jc  ã                  ój   — d dl mZ d dl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)Ú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        úS/var/www/html/ia-enhanced/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)                  ‰«       |€t+        d«      ‚t-        |||«      }|�rDg }|j/                  «       }t1        |«      D ]  }|j#                  ||   «       Œ d}|D ]b  \  }}||k  r"|j#                  |||z      «       |dz  }||k  rŒ"d}t1        |«      D ]  }||||z   |z      z  }Œ |j#                  |«       ||z  }Œd |t%        ‰«      k  r+|j#                  |||z      «       |dz  }|t%        ‰«      k  rŒ+t1        |t%        ‰«      z   t%        ‰«      «      D ]  }|j#                  ||   «       Œ t%        ‰«      |
z
  }|dkD  r|d| |||z   d z   }|j3                  |«      }d}t1        t%        |«      dz
  dd«      D ]7  }||   j'                  «       s||k\  sŒ|||
z   k  sŒ%|dz  }t        |«      ||<   Œ9 |j5                  |||«      }|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ÈÏÉËÐXaÑIaÐHbð c0ð 1óð ô
 !Ø2°1·5±5³7°)Ð;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 intéÿÿÿÿz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   ÚextendÚAssertionErrorr   Ú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   *   sP  ú€ ÷0 'Ñ&ô �- Ô(à#×+Ñ+©AÐ.ˆØ×$Ñ$ˆØ"×.Ñ.‰
Ü	�M 3Ô	'ä Ó.Ð/ˆØ×'Ñ'Ó)ˆØ‰
äÐHÓIÐIàÐØ€JØ™ˆ^€Fä˜vÓ&€I÷'ð( Ðð ò "ˆÜ˜#˜y¨%Ó0ˆØ�}‰}ŒÙ�uÔØ Ñ!ÑÜ˜˜WÔ%à—y‘yò &�Ùœ8 C›=Ô)Ø  AÑ%Ñ ñ&ð
  Ñ!ÐÜ˜3 
Ô+Ü Ð!NÓOÐOô �s“)ˆCØ×ÑœsÐ#7Ó8¼#¸c»(ÐCÔDàò "�Ü! $¨	°5Ó9�Ø—=‘=”?Ü$Ð%<Ó=Ð=Ù˜5Õ!ñ	"ð)"ð6 €LØ!#€Jð ò !ˆØ�=‰=Œ?ØØ×ÑÔ Ø˜rÒ!Ü" :›�Ø×!Ñ!Ð"6Ô7Ø×Ñ˜%Õ ð!ð �rÒÜ˜:“ˆØ×ÑÐ.Ô/ð €|ÜÐ9Ó:Ð:Ü˜$ ¨ZÓ8€Eò àˆ
Ø—
‘
“ˆô �|Ó$ò 	(ˆAØ×Ñ˜e A™hÕ'ð	(ð ˆØ!+ò 	ÑˆI�và�i’-Ø×!Ñ! %¨°qÑ(8Ñ"9Ô:Ø�Q‘�ð �i“-ð
 ˆHÜ˜6“]ò 8�Ø˜E ,°Ñ"2°QÑ"6Ñ7Ñ7‘ð8à×Ñ˜hÔ'Ø�‰K‰Að	ð ”#Ð*Ó+Ò+Ø×Ñ˜e L°1Ñ$4Ñ5Ô6Ø�‰FˆAð ”#Ð*Ó+Ó+ô
 �|¤cÐ*>Ó&?Ñ?ÄÀVÃÓMò 	(ˆAØ×Ñ˜e A™hÕ'ð	(ô Ð.Ó/Ð2BÑBˆØ˜Š?ð ˜=˜LÐ)¨J°|ÀkÑ7QÐ7SÐ,TÑTð ð —‘˜jÓ)ˆð €DÜ”3�z“? QÑ&¨¨BÓ/ò +ˆØ�a‰=×&Ñ&Ô(Ø�Ó ! lÐ5EÑ&EÓ"Eà�A‰IˆDÜ$ T›NˆJ�qŠMð+ð ×#Ñ# E¨:°zÓB€FØ€Mr   )T)r   r   r   r   r   Úboolr(   r	   )r9   ztorch.Tensor | Anyr:   zAny | Sequence[Any]r(   ztorch.Tensor)Ú
__future__r   Útypingr   r   Úcollections.abcr   ÚtorchÚ
_dim_entryr   r	   r
   r   rQ   © r   r   ú<module>rY      sB   ðÝ "ç %ñ Ý(ã ç ?Ñ ?ôð8mØ%ðmØ.Aðmàômr   