
    Wj>                    t    d dl mZ d dlmZmZ erd dlmZ ddlmZ d dl	Z	 G d d          Z
ddZ	 dddZdS )    )annotations)TYPE_CHECKINGUnion)Sequence   DimNc                  T    e Zd ZU ded<   dddZddZddZddZddZddZ	ddZ
dS )DimEntryzUnion[Dim, int]dataNUnion[Dim, int, None]returnNonec                    ddl m} t          |          t          u r|dk    rt	          d|           n4|d}n/t          ||          st	          dt          |                     || _        d S )Nr   r   r   zExpected negative int, got zExpected Dim, got ) r	   typeintAssertionError
isinstancer   )selfr   r	   s      ]/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/functorch/dim/_dim_entry.py__init__zDimEntry.__init__   s    ::qyy$%I4%I%IJJJ \DDdC(( H$%F$t**%F%FGGG			    otherobjectboolc                   t          |t                    sdS |                                 r$|                                r| j        |j        k    S |                                 s"|                                s| j        |j        u S dS )NF)r   r   is_positionalr   )r   r   s     r   __eq__zDimEntry.__eq__!   s    %** 	5  	E$7$7$9$9 	9
**##%% 	e.A.A.C.C 	9
** 5r   c                N    t          | j                  t          u o
| j        dk     S )Nr   )r   r   r   r   s    r   r   zDimEntry.is_positional0   s     DI#%7$)a-7r   c                R    ddl m} t          | j        |          rdS | j        dk    S )Nr   r   Fr   )r   r	   r   r   )r   r	   s     r   is_nonezDimEntry.is_none3   s;    di%% 	"5 9>!r   r   c                    t          | j        t                    s$t          dt	          | j                             | j        S )NzExpected int, got )r   r   r   r   r   r!   s    r   positionzDimEntry.position>   s>    $)S)) 	I !Gd49oo!G!GHHHyr   r	   c                b    t          | j        t                    rt          d          | j        S )NzExpected Dim, got int)r   r   r   r   r!   s    r   dimzDimEntry.dimC   s-    di%% 	: !8999yr   strc                *    t          | j                  S N)reprr   r!   s    r   __repr__zDimEntry.__repr__H   s    DIr   r*   )r   r   r   r   )r   r   r   r   )r   r   )r   r   )r   r	   )r   r(   )__name__
__module____qualname____annotations__r   r   r   r#   r%   r'   r,    r   r   r   r      s                8 8 8 8	" 	" 	" 	"   
   
     r   r   levelsSequence[DimEntry]r   r   c                F    d}| D ]}|                                 r|dz  }|S )Nr   r   )r   )r2   rls      r   ndim_of_levelsr7   L   s7    	A  ?? 	FAHr   Ftensortorch.Tensorfrom_levelslist[DimEntry]	to_levelsdrop_levelsr   c                   ||k    r| S |                                  }|                                 }|s/t          |          t          |          k    rt          d          g }g }|D ]}	 |                    |          }	|                    ||	                    |                    ||	                    O# t          $ rn |                                r|                    d           n,|                    |                                j                    |                    d           Y w xY w| 	                    ||| 
                                          S )aF  
    Reshape a tensor to match target levels using as_strided.

    Args:
        tensor: Input tensor to reshape
        from_levels: Current levels of the tensor
        to_levels: Target levels to match
        drop_levels: If True, missing dimensions are assumed to have stride 0

    Returns:
        Reshaped tensor
    z,Cannot expand dimensions without drop_levelsr   r   )sizestridelenr   indexappend
ValueErrorr   r'   
as_stridedstorage_offset)
r8   r:   r<   r=   sizesstrides	new_sizesnew_strideslevelidxs
             r   _match_levelsrM   T   sl   $ iKKMMEmmooG Q{c)nn,, !OPPPIK - -	-##E**C U3Z(((ws|,,,,  	" 	" 	"""$$ 3  ####  !1222q!!!!!	" YV5J5J5L5LMMMs   +B77A5D/.D/)r2   r3   r   r   )F)
r8   r9   r:   r;   r<   r;   r=   r   r   r9   )
__future__r   typingr   r   collections.abcr   r   r	   torchr   r7   rM   r1   r   r   <module>rR      s    " " " " " " ' ' ' ' ' ' ' '  (((((( 
9 9 9 9 9 9 9 9x    	.N .N .N .N .N .N .Nr   