§
    ŠŠtjc  ã                  ó`   — 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dS )é    )Úannotations)ÚAnyÚTYPE_CHECKING)ÚSequenceNé   )Ú_match_levelsÚDimEntryÚndim_of_levelsTÚargr   Ú	orig_ndimÚintÚ
allow_noneÚboolÚreturnr	   c                óð   — ddl m} | €|rt          ¦   «         S t          | |¦  «        rt          | ¦  «        S t          | t          ¦  «        r| dk     r| }n| |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   )ÚDimNr   )Ú r   r	   Ú
isinstancer   )r   r   r   r   Úposs        úR/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/functorch/dim/_order.pyÚ	_wrap_dimr      sˆ   € ð ÐÐÐÐÐà
€{�z€{Ý‰zŒzÐÝ	�C˜Ñ	Ô	ð 	Ý˜‰}Œ}ÐÝ	�C�Ñ	Ô	ð Ø�Š7ˆ7ØˆCˆCà˜	‘/ˆCÝ˜‰}Œ}Ðå‰zŒzÐó    Útensor_or_dimútorch.Tensor | AnyÚdimsúAny | Sequence[Any]útorch.Tensorc                ó2	  ‡‡‡ — ddl m}m}m} t	          | |¦  «        r| j        dd…         }| j        }| j        }nFt	          | |¦  «        r't          | ¦  «        g}|  	                    ¦   «         }d}nt          d¦  «        ‚g Šg }|dd…         Št          ‰¦  «        Š dˆˆˆ fd
„}	d}
|D �]}t          |‰ d¦  «        }|                     ¦   «         s |	|¦  «         |
dz  }
Œ9t	          ||¦  «        r(|j        D ]} |	t          |¦  «        ¦  «         |
dz  }
Œ Œq|
dz  }
t          |d¦  «        st          d¦  «        ‚t!          |¦  «        }|                     t%          ‰¦  «        t%          |¦  «        f¦  «         |D ]A}t          |‰ d¦  «        }|                     ¦   «         rt          d¦  «        ‚ |	|¦  «         ŒB�Œd}g }‰D ]j}|                     ¦   «         rŒ|                     ¦   «         r*|dk    r$t%          |¦  «        }|                     ‰¦  «         |                     |¦  «         Œk|dk    r$t%          |¦  «        }|                     ‰¦  «         |€t+          d¦  «        ‚t-          |||¦  «        }|�r”g }|                     ¦   «         }t1          |¦  «        D ]}|                     ||         ¦  «         Œd}|D ]s\  }}||k     r)|                     |||z            ¦  «         |dz  }||k     °)d}t1          |¦  «        D ]}||||z   |z            z  }Œ|                     |¦  «         ||z  }Œt|t%          ‰¦  «        k     r6|                     |||z            ¦  «         |dz  }|t%          ‰¦  «        k     °6t1          |t%          ‰¦  «        z   t%          ‰¦  «        ¦  «        D ]}|                     ||         ¦  «         Œt%          ‰¦  «        |
z
  }|dk    r|d|…         |||z   d…         z   }|                     |¦  «        }d}t1          t%          |¦  «        dz
  dd¦  «        D ]B}||                              ¦   «         s||k    r |||
z   k     r|dz  }t          |¦  «        ||<   ŒC|                     |||¦  «        }|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 objectÚdr	   r   ÚNonec                ór  •— 	 ‰                      | ¦  «        }n# t          $ r d}Y nw xY w|€d|                      ¦   «         r+t          d‰› d|                      ¦   «         ‰z   › d�¦  «        ‚t          d|                      ¦   «         › d�¦  «        ‚t          ¦   «         ‰|<   ‰                     | ¦  «         dS )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)r!   ÚidxÚflat_positional_dimsÚlevelsr   s     €€€r   Ú
append_dimzorder.<locals>.append_dimX   sé   ø€ ð	Ø—,’,˜q‘/”/ˆCˆCøÝð 	ð 	ð 	ØˆCˆCˆCð	øøøàˆ;Ø�ŠÑ Ô ð Ý ð1 )ð 1ð 1ÈÏÊÉÌÐXaÑIað 1ð 1ð 1ñô ð õ
 !ØV°1·5²5±7´7ÐVÐVÐVñô ð õ ‘j”jˆˆs‰Ø×#Ò# AÑ&Ô&Ð&Ð&Ð&s   ƒ ™(§(r   Ú__iter__z+expected a Dim, List[Dim], or Sequence[Dim]zexpected a Dim or intéÿÿÿÿzCannot reorder None tensor)r!   r	   r   r"   )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)!r   r   r   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   ÚorderrU   *   sµ  øøø€ ð0 'Ð&Ð&Ð&Ð&Ð&Ð&Ð&Ð&Ð&õ �- Ñ(Ô(ð Jà#Ô+¨A¨A¨AÔ.ˆØÔ$ˆØ"Ô.ˆ
ˆ
Ý	�M 3Ñ	'Ô	'ð Jå Ñ.Ô.Ð/ˆØ×'Ò'Ñ)Ô)ˆØˆ
ˆ
åÐHÑIÔIÐIàÐØ€JØ˜˜˜Œ^€Få˜vÑ&Ô&€Ið'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð( Ðð ð "ñ "ˆÝ˜#˜y¨%Ñ0Ô0ˆØ�}Š}‰Œð 	"ØˆJ�uÑÔÐØ Ñ!ÐÐÝ˜˜WÑ%Ô%ð 	"à”yð &ð &�Ø�
�8 C™=œ=Ñ)Ô)Ð)Ø  AÑ%Ð Ð ð&ð
  Ñ!ÐÝ˜3 
Ñ+Ô+ð PÝ Ð!NÑOÔOÐOõ �s‘)”)ˆCØ×Ò�sÐ#7Ñ8Ô8½#¸c¹(¼(ÐCÑDÔDÐDàð "ð "�Ý! $¨	°5Ñ9Ô9�Ø—=’=‘?”?ð >Ý$Ð%<Ñ=Ô=Ð=Ø�
˜5Ñ!Ô!Ð!Ð!ñ	"ð €LØ!#€Jð ð !ð !ˆØ�=Š=‰?Œ?ð 	ØØ×ÒÑ Ô ð 	8Ø˜rÒ!Ð!Ý" :™œ�Ø×!Ò!Ð"6Ñ7Ô7Ð7Ø×Ò˜%Ñ Ô Ð Ð ð �rÒÐÝ˜:‘”ˆØ×ÒÐ.Ñ/Ô/Ð/ð €|ÝÐ9Ñ:Ô:Ð:Ý˜$ ¨ZÑ8Ô8€Eð ñ )*àˆ
Ø—
’
‘”ˆõ �|Ñ$Ô$ð 	(ð 	(ˆAØ×Ò˜e AœhÑ'Ô'Ð'Ð'ð ˆØ!+ð 	ð 	ÑˆI�và�i’-�-Ø×!Ò! %¨°qÑ(8Ô"9Ñ:Ô:Ð:Ø�Q‘�ð �i’-�-ð
 ˆHÝ˜6‘]”]ð 8ð 8�Ø˜E ,°Ñ"2°QÑ"6Ô7Ñ7��Ø×Ò˜hÑ'Ô'Ð'Ø�‰KˆAˆAð •#Ð*Ñ+Ô+Ò+Ð+Ø×Ò˜e L°1Ñ$4Ô5Ñ6Ô6Ð6Ø�‰FˆAð •#Ð*Ñ+Ô+Ò+Ð+õ
 �|¥cÐ*>Ñ&?Ô&?Ñ?ÅÀVÁÄÑMÔMð 	(ð 	(ˆAØ×Ò˜e AœhÑ'Ô'Ð'Ð'õ Ð.Ñ/Ô/Ð2BÑBˆØ˜Š?ˆ?ð ˜=˜L˜=Ô)¨J°|ÀkÑ7QÐ7SÐ7SÔ,TÑTð ð —’˜jÑ)Ô)ˆð €DÝ•3�z‘?”? QÑ&¨¨BÑ/Ô/ð +ð +ˆØ�aŒ=×&Ò&Ñ(Ô(ð 	+Ø�ÒÐ ! lÐ5EÑ&EÒ"EÐ"Eà�A‰IˆDÝ$ T™NœNˆJ�q‰Møà×#Ò# E¨:°zÑBÔB€FØ€Mr   )T)r   r   r   r   r   r   r   r	   )r   r   r   r   r   r   )Ú
__future__r   Útypingr   r   Úcollections.abcr   ÚtorchÚ
_dim_entryr   r	   r
   r   rU   © r   r   ú<module>r\      s¯   ðØ "Ð "Ð "Ð "Ð "Ð "à %Ð %Ð %Ð %Ð %Ð %Ð %Ð %ð ð )Ø(Ð(Ð(Ð(Ð(Ð(à €€€à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?ðð ð ð ð ð8mð mð mð mð mð mr   