o
    j9:jH                     @   s  U d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZ eeB eedf B dB Zeed	< G d
d dZdeded fddZG dd deZeeB Zeed< eedf eB dB Zeed< eeB eedf B dB Zeed< deeedf B defddZdedefddZdedefddZd?dededefddZ d?dededefd d!Z!d"ed#edefd$d%Z"d@ded&edefd'd(Z#dedB dedB fd)d*Z$dedB dedB fd+d,Z%d"ed#edefd-d.Z&d"ed#edefd/d0Z'd1ed"ed#edefd2d3Z(d"ed#edefd4d5Z)d"ed#edefd6d7Z*d"ed#edefd8d9Z+d"ed#edefd:d;Z,d<eedf dedeeef fd=d>Z-dS )Az
Definition of CuTe Layouts and functions to manipulate them which works with the order
of lexicographic instead of co-lexicographic as implemented in the original layout.py
    )chain)	TypeAlias)SelfTypeIs   )	crd2idxflattenhas_noneIntTupleis_intis_tupleproductslice_suffix_product.NCoordinateTypec                   @   s   e Zd ZdS )
LayoutBaseN)__name__
__module____qualname__ r   r   g/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/torch/distributed/_pycute/layout.pyr   =   s    r   xreturnLayoutc                 C   s
   t | tS N)
isinstancer   )r   r   r   r   	is_layoutA      
r   c                   @   s   e Zd ZddededB ddfddZdedefdd	Zdefd
dZ	de
deeB fddZdedefddZdefddZdefddZdefddZdefddZdS )r   N_shape_strider   c                 C   s(   || _ |d u rt| j | _d S || _d S r   )shaper   stride)selfr   r   r   r   r   __init__F   s   
zLayout.__init__otherc                 C   s&   t |tsdS | j|jko| j|jkS )NF)r   r   r    r!   )r"   r$   r   r   r   __eq__N   s   
zLayout.__eq__c                 C   s   t | jr
t| jS dS Nr   )r   r    lenr"   r   r   r   __len__T   s   

zLayout.__len__argsc                 G   s   t |r(t|dkrtt|d | jt|d | jS tt|| jt|| jS t|dkr8t|d | j| jS t|| j| jS )a  
        Map a logical coordinate to a linear index (Coord has no Underscore slice operators)
        OR
        Slice the layout and return the sublayout (Coord has an Underscore slice op)

        Follow the same behavior of `Layout::operator(Coord const&)` in cute C++
        r   r   )r	   r'   r   r   r    r!   r   )r"   r*   r   r   r   __call__[   s   "zLayout.__call__ic                 C   s:   t | jrt| j| | j| S |dkrtt| j| jS )Nr   )r   r    r   r!   AssertionError)r"   r,   r   r   r   __getitem__o   s
   
zLayout.__getitem__c                 C   s
   t | jS r   )r   r    r(   r   r   r   sizex   r   zLayout.sizec                 C   s   | |   d d S r&   )r/   r(   r   r   r   cosize|   s   zLayout.cosizec                 C   s   | j  d| j S )N:r    r!   r(   r   r   r   __str__   s   zLayout.__str__c                 C   s   d| j  d| j dS )NzLayout(,)r2   r(   r   r   r   __repr__   s   zLayout.__repr__r   )r   r   r   r
   r#   objectboolr%   intr)   r   r   r+   r.   r/   r0   strr3   r6   r   r   r   r   r   E   s    	LayoutOrIntTupleLayoutProfileLayoutInputlayoutsc                  G   s@   t | dkrt| d s| d } tdd | D  \}}t||S )Nr   r   c                 s   s    | ]	}|j |jfV  qd S r   r2   ).0ar   r   r   	<genexpr>       zmake_layout.<locals>.<genexpr>)r'   r   zipr   )r>   r    r!   r   r   r   make_layout   s   
rD   layoutc                 C   s   t | r|  S t| S r   )r   r/   r   rE   r   r   r   r/      s   r/   c                 C   s   |   S r   )r0   rF   r   r   r   r0      s   r0   profilec                    s4  t r/t tk rttt fddttD  fddttt D S dg}dg}ttt j	tt j
D ]5\}}|dkrMqD|d dkr\||d< ||d< qD|d |d  |kro|d | |d< qD|| || qDt|dkrt|d |d S |  |  tt|t|S )Nc                 3   "    | ]}t  | | V  qd S r   )coalescer?   r,   rE   rG   r   r   rA           zcoalesce.<locals>.<genexpr>c                 3       | ]} | V  qd S r   r   rJ   rF   r   r   rA          r   r   )r   r'   r-   rD   r   rangerC   reversedr   r    r!   appendr   reversetuplerE   rG   result_shaperesult_strider    r!   r   rK   r   rI      s8   

rI   c                    s   t r/t tk rttt fddttD  fddttt D S g }g }tt jt j	D ]\}}|dksT|dksT|
| |
| q>t|dkr`tddS ttt|t|S )Nc                 3   rH   r   )filterrJ   rK   r   r   rA      rL   zfilter.<locals>.<genexpr>c                 3   rM   r   r   rJ   rF   r   r   rA      rN   r   r   )r   r'   r-   rD   r   rP   rC   r   r    r!   rR   r   rI   rT   rU   r   rK   r   rX      s&   


rX   layoutAlayoutBc           
         s  d u r S t rt tS tr@t tk rttt fddttD  fddttt D S tj	rPt fddD S j
dkr[tj	dS g }g }j	}j
}t }ttt|j	dd  tt|j
dd  D ]6\}}|| dks|| dkstttd|| |}	|	dkr||	 |||  ||	 }| |  }q|dkst|dkr|| ||t|j
d   |  |  t|dkrt|d |d S tt|t|S )Nc                 3   rH   r   compositionrJ   rY   rZ   r   r   rA      rL   zcomposition.<locals>.<genexpr>c                 3   rM   r   r   rJ   rY   r   r   rA      rN   c                 3   s    | ]}t  |V  qd S r   r[   )r?   	layoutB_ir^   r   r   rA      s    r   r   )r   r\   r   r   r'   r-   rD   r   rP   r    r!   rI   rC   rQ   r   minmaxrR   rS   rT   )
rY   rZ   rV   rW   
rest_shaperest_strideflat_A
curr_shapecurr_stride	new_shaper   r]   r   r\      sT   

	(

r\   max_idxc           	      C   s   t | r
tt| S g }g }d}ttt| jt| j}|D ]-\}}|dks+|dkr,q||| k}t|t	u r<|s<t
|||  || || }q||| d |  || |  |  ttt|t|S )Nr   r   )r   
complementr   sortedrC   r   r!   r    typer8   r-   rR   rS   rI   rT   )	rE   rh   rV   rW   current_idx	sorted_DSr!   r    in_boundr   r   r   ri   /  s(   


ri   c           
      C   s   | d u rd S t | rt| S g }g }d}t| j}t| j}tt||t|}|D ]\}}}	|dkr4q*||kr: n|| ||	 || }q*|	  |	  t
tt|t|S r&   )r   r   r   r    r!   rj   rC   r   rR   rS   rI   rT   )
rE   rV   rW   rl   
flat_shapeflat_stride
sorted_DSAr!   r    rstrider   r   r   right_inverseP  s*   




rs   c                 C   s.   | d u rd S t | rt| S ttt| | S r   )r   r   rs   rD   ri   rF   r   r   r   left_inversem  s
   rt   c                    s   d u r S t rt tS tr@t tk rttt fddttD  fddttt D S t	 tt
t S )Nc                 3   rH   r   )logical_dividerJ   r]   r   r   rA     
    
z!logical_divide.<locals>.<genexpr>c                 3   rM   r   r   rJ   r^   r   r   rA     rN   )r   ru   r   r   r'   r-   rD   r   rP   r\   ri   r/   r]   r   r]   r   ru   w  s&   
ru   c                    s   d u r S t rt tS tr@t tk rttt fddttD  fddttt D S t t	t
 t t S )Nc                 3   rH   r   )logical_productrJ   r]   r   r   rA     rv   z"logical_product.<locals>.<genexpr>c                 3   rM   r   r   rJ   r^   r   r   rA     rN   )r   ru   r   r   r'   r-   rD   r   rP   r\   ri   r/   r0   r]   r   r]   r   rw     s&   
rw   splitterc              
      s   d u rt tdd S tr[t tk rtt  fddttD t t fddttD t tfddttD  fddttt D S  S )Nr   r   c                 3   s$    | ]}t  | | V  qd S r   )
hier_unziprJ   )rY   rZ   rx   r   r   rA     s
    
zhier_unzip.<locals>.<genexpr>c                 3       | ]	} | d  V  qdS )r   Nr   rJ   splitr   r   rA     rB   c                 3   rz   )r   Nr   rJ   r{   r   r   rA     rB   c                 3   rM   r   r   rJ   r^   r   r   rA     rN   )rD   r   r   r'   r-   rP   r   )rx   rY   rZ   r   )rY   rZ   r|   rx   r   ry     s$   

ry   c                 C      t t| |S r   )ry   ru   r]   r   r   r   zipped_divide     r~   c                    6   t | | t d g fddtt d D  S )Nr   c                       g | ]} d  | qS r   r   rJ   resultr   r   
<listcomp>      z tiled_divide.<locals>.<listcomp>r   )r~   rD   rP   r'   r]   r   r   r   tiled_divide     
,r   c                 C   r}   r   )ry   rw   r]   r   r   r   zipped_product  r   r   c                    r   )Nr   c                    r   r   r   rJ   r   r   r   r     r   z!tiled_product.<locals>.<listcomp>r   )r   rD   rP   r'   r]   r   r   r   tiled_product  r   r   crdc                 C   s*   t t| |jt| |jt| |j|jfS r   )r   r   r    r!   r   )r   rE   r   r   r   slice_and_offset  s   r   r   r   ).__doc__	itertoolsr   typingr   typing_extensionsr   r   	int_tupler   r   r	   r
   r   r   r   r   r   r9   rT   r7   r   __annotations__r   r   r   r;   r<   r=   rD   r/   r0   rI   rX   r\   ri   rs   rt   ru   rw   ry   r~   r   r   r   r   r   r   r   r   <module>   sL    ,
D 	+D!

*