o
    eiF                     @   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   j/var/www/addictedbytheproject.nl/epg/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sJ t| j| jS )Nr   )r   r    r   r!   )r"   r,   r   r   r   __getitem__o   s   
zLayout.__getitem__c                 C   s
   t | jS r   )r   r    r(   r   r   r   sizew   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(,)r1   r(   r   r   r   __repr__   s   zLayout.__repr__r   )r   r   r   r
   r#   objectboolr%   intr)   r   r   r+   r-   r.   r/   strr2   r5   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   r1   ).0ar   r   r   	<genexpr>       zmake_layout.<locals>.<genexpr>)r'   r   zipr   )r=   r    r!   r   r   r   make_layout   s   
rC   layoutc                 C   s   t | r|  S t| S r   )r   r.   r   rD   r   r   r   r.      s   r.   c                 C   s   |   S r   )r/   rE   r   r   r   r/      s   r/   profilec                    s4  t r/t tksJ 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,   rD   rF   r   r   r@           zcoalesce.<locals>.<genexpr>c                 3       | ]} | V  qd S r   r   rI   rE   r   r   r@          r   r   )r   r'   rC   r   rangerB   reversedr   r    r!   appendr   reversetuplerD   rF   result_shaperesult_strider    r!   r   rJ   r   rH      s6   

rH   c                    s   t r/t tksJ 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   rG   r   )filterrI   rJ   r   r   r@      rK   zfilter.<locals>.<genexpr>c                 3   rL   r   r   rI   rE   r   r   r@      rM   r   r   )r   r'   rC   r   rO   rB   r   r    r!   rQ   r   rH   rS   rT   r   rJ   r   rW      s$   


rW   layoutAlayoutBc           
         s  d u r S t rt tS tr@t tksJ 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sJ 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   rG   r   compositionrI   rX   rY   r   r   r@      rK   zcomposition.<locals>.<genexpr>c                 3   rL   r   r   rI   rX   r   r   r@      rM   c                 3   s    | ]}t  |V  qd S r   rZ   )r>   	layoutB_ir]   r   r   r@      s    r   r   )r   r[   r   r   r'   rC   r   rO   r    r!   rH   rB   rP   r   minmaxrQ   rR   rS   )
rX   rY   rU   rV   
rest_shaperest_strideflat_A
curr_shapecurr_stride	new_shaper   r\   r   r[      sP   

	(

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s<|s<J |
||  |
| || }q|
|| d |  |
| |  |  ttt|t|S )Nr   r   )r   
complementr   sortedrB   r   r!   r    typer7   rQ   rR   rH   rS   )	rD   rg   rU   rV   current_idx	sorted_DSr!   r    in_boundr   r   r   rh   *  s&   


rh   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!   ri   rB   r   rQ   rR   rH   rS   )
rD   rU   rV   rk   
flat_shapeflat_stride
sorted_DSAr!   r    rstrider   r   r   right_inverseJ  s*   




rr   c                 C   s.   | d u rd S t | rt| S ttt| | S r   )r   r   rr   rC   rh   rE   r   r   r   left_inverseg  s
   rs   c                    s   d u r S t rt tS tr@t tksJ tt fddttD  fddttt D S t tt	t
 S )Nc                 3   rG   r   )logical_dividerI   r\   r   r   r@   {  
    
z!logical_divide.<locals>.<genexpr>c                 3   rL   r   r   rI   r]   r   r   r@     rM   )r   rt   r   r   r'   rC   r   rO   r[   rh   r.   r\   r   r\   r   rt   q  s$   
rt   c                    s   d u r S t rt tS tr@t tksJ tt fddttD  fddttt D S t tt	 t
 t S )Nc                 3   rG   r   )logical_productrI   r\   r   r   r@     ru   z"logical_product.<locals>.<genexpr>c                 3   rL   r   r   rI   r]   r   r   r@     rM   )r   rt   r   r   r'   rC   r   rO   r[   rh   r.   r/   r\   r   r\   r   rv     s$   
rv   splitterc              
      s   d u rt tdd S tr[t tksJ 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_unziprI   )rX   rY   rw   r   r   r@     s
    
zhier_unzip.<locals>.<genexpr>c                 3       | ]	} | d  V  qdS )r   Nr   rI   splitr   r   r@     rA   c                 3   ry   )r   Nr   rI   rz   r   r   r@     rA   c                 3   rL   r   r   rI   r]   r   r   r@     rM   )rC   r   r   r'   rO   r   )rw   rX   rY   r   )rX   rY   r{   rw   r   rx     s"   

rx   c                 C      t t| |S r   )rx   rt   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   rI   resultr   r   
<listcomp>      z tiled_divide.<locals>.<listcomp>r   )r}   rC   rO   r'   r\   r   r   r   tiled_divide     
,r   c                 C   r|   r   )rx   rv   r\   r   r   r   zipped_product  r~   r   c                    r   )Nr   c                    r   r   r   rI   r   r   r   r     r   z!tiled_product.<locals>.<listcomp>r   )r   rC   rO   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   rD   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   r8   rS   r6   r   __annotations__r   r   r   r:   r;   r<   rC   r.   r/   rH   rW   r[   rh   rr   rs   rt   rv   rx   r}   r   r   r   r   r   r   r   r   <module>   sL    ,
C 	*B 

*