
    9j,                    z    d dl mZ d dl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y)    )annotations)TYPE_CHECKING)Sequence   DimNc                  R    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
y)DimEntryz	Dim | intdataNc                    ddl m} t        |      t        u r|dk\  r=t	        d|       |
d}|| _        y t        ||      st	        dt        |             || _        y )Nr   r   r   zExpected negative int, got zExpected Dim, got ) r   typeintAssertionError
isinstancer   )selfr   r   s      X/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/functorch/dim/_dim_entry.py__init__zDimEntry.__init__   sh    :qy$'B4&%IJJ\D 	 dC($'9$t*%FGG	    c                   t        |t              sy| j                         r)|j                         r| j                  |j                  k(  S | j                         s(|j                         s| j                  |j                  u S y)NF)r   r
   is_positionalr   )r   others     r   __eq__zDimEntry.__eq__!   sj    %* E$7$7$999

**##%e.A.A.C99

** r   c                Z    t        | j                        t        u xr | j                  dk  S )Nr   )r   r   r   r   s    r   r   zDimEntry.is_positional0   s"    DII#%7$))a-7r   c                Z    ddl m} t        | j                  |      ry| j                  dk(  S )Nr   r   Fr   )r   r   r   r   )r   r   s     r   is_nonezDimEntry.is_none3   s&    dii% 99>!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>   s6    $))S) #5d499o5F!GHHyyr   c                d    t        | j                  t              rt        d      | j                  S )NzExpected Dim, got int)r   r   r   r   r   s    r   dimzDimEntry.dimC   s&    dii% !899yyr   c                ,    t        | j                        S N)reprr   r   s    r   __repr__zDimEntry.__repr__H   s    DIIr   r#   )r   zDim | int | NonereturnNone)r   objectr&   bool)r&   r)   )r&   r   )r&   r   )r&   str)__name__
__module____qualname____annotations__r   r   r   r   r   r!   r%    r   r   r
   r
      s*    
O8	"

r   r
   c                D    d}| D ]  }|j                         s|dz  } |S )Nr   r   )r   )levelsrls      r   ndim_of_levelsr4   L   s0    	A ??FA Hr   c                :   ||k(  r| S | j                         }| j                         }|s"t        |      t        |      kD  rt        d      g }g }|D ]<  }	 |j	                  |      }	|j                  ||	          |j                  ||	          > | j                  ||| j                               S # t        $ r_ |j                         r|j                  d       n)|j                  |j                         j                          |j                  d       Y w xY w)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)
tensorfrom_levels	to_levelsdrop_levelssizesstrides	new_sizesnew_strideslevelidxs
             r   _match_levelsrH   T   s   $ iKKMEmmoG{c)n, !OPPIK -	-##E*C U3Z(ws|,- YV5J5J5LMM  	"""$  #  !1!12q!	"s   B22A%DD)r1   zSequence[DimEntry]r&   r   )F)
r>   torch.Tensorr?   list[DimEntry]r@   rJ   rA   r)   r&   rI   )
__future__r   typingr   collections.abcr   r   r   torchr
   r4   rH   r/   r   r   <module>rO      sg    "   ( 
9 9x 	.N.N.N .N 	.N
 .Nr   