o
    eiv                     @  sf   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dZdddZdS )    )annotations)AnyTYPE_CHECKINGUnion)SequenceN   )_match_levelsDimEntryndim_of_levelsTargr   	orig_ndimint
allow_noneboolreturnr	   c                 C  sd   ddl m} | du r|rt S t| |rt| S t| tr/| dk r'| }t|S | | }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   pos r   ^/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/functorch/dim/_order.py	_wrap_dim   s   

r   tensor_or_dimUnion[torch.Tensor, Any]dimsUnion[Any, Sequence[Any]]torch.Tensorc                   s  ddl m}m}m} t| |r| jdd }| j}| j}nt| |r.t| g}| 	 }d}nt
dg  g }|dd td fd
d}	d}
|D ]^}t|d}| sb|	| |
d7 }
qMt||rx|jD ]}|	t| |
d7 }
qjqM|
d7 }
t|dst
dt|}|t t|f |D ]}t|d}| rt
d|	| qqMd}g }D ]}| rq| r|dkrt|}|  || q|dkrt|}|  |du rtdt|||}|rg }| }t|D ]	}|||  qd}|D ]9\}}||k r!||||   |d7 }||k sd}t|D ]}|||| |  9 }q'|| ||7 }q|t k r[||||   |d7 }|t k sGt|t  tD ]
}|||  qft |
 }|dkr|d| ||| d  }||}d}tt|d ddD ] }||  s||kr|||
 k r|d8 }t|||< q||||}|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                   s   z | }W n ty   d}Y nw |du r4|  r*td d|    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   r   r   
append_dimX   s   
zorder.<locals>.append_dimr   __iter__z+expected a Dim, List[Dim], or Sequence[Dim]zexpected a Dim or intz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   order*   s   















rS   )T)r   r   r   r   r   r   r   r	   )r   r   r   r   r   r   )
__future__r   typingr   r   r   collections.abcr   torch
_dim_entryr   r	   r
   r   rS   r   r   r   r   <module>   s    