o
    i9:j,                     @  sf   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 dZ	dddZ
	ddddZdS )    )annotations)TYPE_CHECKING)Sequence   DimNc                   @  s^   e Zd ZU ded< ddddZdddZdddZdddZdddZd ddZ	d!ddZ
dS )"DimEntryz	Dim | intdataNDim | int | NonereturnNonec                 C  sd   ddl m} t|tu r|dkrtd| n|d u r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    r   _/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/functorch/dim/_dim_entry.py__init__   s   

zDimEntry.__init__otherobjectboolc                 C  sJ   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   r   r   r   __eq__!   s   
zDimEntry.__eq__c                 C  s   t | jtu o| jdk S )Nr   )r   r	   r   r   r   r   r   r   0   s   zDimEntry.is_positionalc                 C  s&   ddl m} t| j|rdS | jdkS )Nr   r   Fr   )r   r   r   r	   )r   r   r   r   r   is_none3   s   
zDimEntry.is_noner   c                 C  s&   t | jtstdt| j | jS )NzExpected int, got )r   r	   r   r   r   r   r   r   r   position>   s   zDimEntry.positionr   c                 C  s   t | jtr
td| jS )NzExpected Dim, got int)r   r	   r   r   r   r   r   r   dimC   s   zDimEntry.dimstrc                 C  s
   t | jS N)reprr	   r   r   r   r   __repr__H   s   
zDimEntry.__repr__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   r      s   
 




r   levelsSequence[DimEntry]r   r   c                 C  s"   d}| D ]
}|  r|d7 }q|S )Nr   r   )r   )r'   rlr   r   r   ndim_of_levelsL   s   r+   Ftensortorch.Tensorfrom_levelslist[DimEntry]	to_levelsdrop_levelsr   c           
   	   C  s   ||kr| S |   }|  }|st|t|krtdg }g }|D ]8}z||}	W n  tyK   | r<|d n|| j  |d Y q"w |||	  |||	  q"| 	||| 
 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
ValueErrorr   appendr   
as_stridedstorage_offset)
r,   r.   r0   r1   sizesstrides	new_sizesnew_strideslevelidxr   r   r   _match_levelsT   s*   r@   )r'   r(   r   r   )F)
r,   r-   r.   r/   r0   r/   r1   r   r   r-   )
__future__r   typingr   collections.abcr   r   r   torchr   r+   r@   r   r   r   r   <module>   s    
<