U
    ‹ÑhL3  ã                   @  sØ  d dl mZ ddlmZ ddlmZ ddlmZ dd„ Zd	d
œdd„Zd	d
œdd„Z	ej
edd„ ƒƒZej
ee d¡dd„ ƒƒƒZej
ee d¡d_dd„ƒƒƒZej
edd„ ƒƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZed d!„ ƒZed"d#„ ƒZed$d%„ ƒZej
eejd&d'd(d)�d`d,d-„ƒƒƒZej
eejd.d/d0�dad1d2„ƒƒƒZed3d4„ ƒZed5d6„ ƒZed7d8„ ƒZed9d:„ ƒZej
eejd;d'd(d)�dbd<d=„ƒƒƒZej
eejd>d/d0�dcd?d@„ƒƒƒZedAdB„ ƒZ ej
ee dC¡dddDdC„ƒƒƒZ!edEdF„ ƒZ"ej
ej#e dG¡dedHdI„ƒƒƒZ$ej
ee %dJ¡dfdKdJ„ƒƒƒZ&edLdM„ ƒZ'ej
ee %dN¡dgdOdN„ƒƒƒZ(ed	d	dPœdQdR„ƒZ)ed	d	d	dSœdTdU„ƒZ*ej
ed*ej+fd	d	dVœdWdX„ƒƒZ,dYdZ„ Z-ej
edhd[d\„ƒƒZ.ed]d^„ ƒZ/d*S )ié    )Úannotationsé   )Újité   )Úcore)Úmathc                 C  s   t | tjƒr| jS | S ©N)Ú
isinstancer   Ú	constexprÚvalue)Úo© r   úF/tmp/user/2202/pip-unpacked-wheel-sn6cy74c/triton/language/standard.pyÚ_unwrap_if_constexpr
   s    r   zcore.constexpr)Úic                 C  s.   d}| j }|dkr$|dL }|d7 }q
t |¡S )Nr   r   ©r   r   r
   )r   Úlog2Únr   r   r   Ú_log2   s    
r   c                 C  s$   | j }t ||d @ dko |dk¡S )Nr   r   r   )r   r   r   r   r   Ú_is_power_of_two   s    r   c                 C  s   | | d | S )z¨
    Computes the ceiling division of :code:`x` by :code:`div`

    :param x: the input number
    :type x: Block
    :param div: the divisor
    :param div: Block
    r   r   )ÚxÚdivr   r   r   Úcdiv!   s    r   Úsigmoidc                 C  s   ddt  |  ¡  S )Nr   )r   Úexp©r   r   r   r   r   /   s    ÚsoftmaxFc                 C  s0   | t | dƒ }t |¡}t|dƒ}t |||¡S )Nr   )Úmaxr   r   ÚsumZfdiv)r   Zieee_roundingÚzÚnumZdenr   r   r   r   6   s    

c                 C  s   t j| | jgdd�S )zn
    Returns a contiguous flattened view of :code:`x`.

    :param x: the input tensor
    :type x: Block
    T)Zcan_reorder)r   ÚreshapeÚnumelr   r   r   r   Úravel@   s    	r#   c                 C  sT   | | | }|| }|| }|| }t  || |¡}|||  }	|| | }
|	|
fS )añ  
    Transforms indices of a row-major :code:`size_i * size_j` matrix into those
    of one where the indices are col-major for each group of :code:`size_g`
    rows.

    For example, for :code:`size_i = size_j = 4` and :code:`size_g = 2`, it will
    transform ::

        [[0 , 1 , 2 , 3 ],
         [4 , 5 , 6 , 7 ],
         [8 , 9 , 10, 11],
         [12, 13, 14, 15]]

    into ::

        [[0, 2,  4 , 6 ],
         [1, 3,  5 , 7 ],
         [8, 10, 12, 14],
         [9, 11, 13, 15]]
    ©r   Úminimum)r   ÚjZsize_iZsize_jZsize_gZijZsize_gjZgroup_idZoff_iZnew_iZnew_jr   r   r   Ú	swizzle2dL   s    r'   c                 C  s   t  | d|¡S )a'  
    Returns a tensor filled with the scalar value 0 for the given :code:`shape` and :code:`dtype`.

    :param shape: Shape of the new array, e.g., (8, 16) or (8, )
    :type shape: tuple of ints
    :param dtype: Data-type of the new array, e.g., :code:`tl.float16`
    :type dtype: DType
    r   )r   Úfull)ÚshapeÚdtyper   r   r   Úzeross   s    
r+   c                 C  s   t | j| jƒS )zS
    Creates a tensor of zeros with the same shape and type as a given tensor.
    )r+   r)   r*   )Úinputr   r   r   Ú
zeros_like€   s    r-   c           	      C  sJ   |r| |ko||k }nd}| |kp$|}t  || |¡}t  |||¡}||fS ©NF©r   Úwhere)	Úvalue1Úindex1Úvalue2Úindex2Útie_break_leftÚtieÚgtZv_retZi_retr   r   r   Ú_argmax_combine‹   s    r8   c                 C  s   t | |||dƒS ©NT©r8   ©r1   r2   r3   r4   r   r   r   Ú_argmax_combine_tie_break_left—   s    r<   c                 C  s   t | |||dƒS r.   r:   r;   r   r   r   Ú_argmax_combine_tie_break_fastœ   s    r=   c                 C  s   t  | |¡S r   )r   Úmaximum©ÚaÚbr   r   r   Ú_elementwise_max¡   s    rB   r>   Úreturn_indicesÚreturn_indices_tie_break_left)Zreturn_indices_argÚtie_break_argNTc                 C  s¢   t  | ¡} |r8|r$t j| |t|d�S t j| |t|d�S nft  | jj¡t  d¡k rŒt  | j ¡ ¡rn|  	t j
¡} n| j ¡ s€tdƒ‚|  	t j¡} t j| |t|d�S d S ©N©Ú	keep_dimsé    z"Expecting input to be integer type)r   Ú_promote_bfloat16_to_float32Ú_reduce_with_indicesr<   r=   r
   r*   Úprimitive_bitwidthÚis_floatingÚtoÚfloat32Úis_intÚAssertionErrorÚint32ÚreducerB   ©r,   ÚaxisrC   rD   rH   r   r   r   r   ¦   s    
r   zmaximum indexr5   )rE   c                 C  s   t | |d||d�\}}|S ©NT)rC   rD   rH   )r   ©r,   rU   r5   rH   Ú_Úretr   r   r   Úargmax»   s    rZ   c           	      C  sJ   |r| |ko||k }nd}| |k p$|}t  || |¡}t  |||¡}||fS r.   r/   )	r1   r2   r3   r4   r5   r6   ÚltZ	value_retZ	index_retr   r   r   Ú_argmin_combineÆ   s    r\   c                 C  s   t | |||dƒS r9   ©r\   r;   r   r   r   Ú_argmin_combine_tie_break_leftÒ   s    r^   c                 C  s   t | |||dƒS r.   r]   r;   r   r   r   Ú_argmin_combine_tie_break_fast×   s    r_   c                 C  s   t  | |¡S r   r$   r?   r   r   r   Ú_elementwise_minÜ   s    r`   r%   c                 C  sœ   t  | ¡} |r8|r$t j| |t|d�S t j| |t|d�S n`t  | jj¡dk r†t  | j ¡ ¡rh|  	t j
¡} n| j ¡ sztdƒ‚|  	t j¡} t j| |t|d�S d S rF   )r   rJ   rK   r^   r_   r
   r*   rL   rM   rN   rO   rP   rQ   rR   rS   r`   rT   r   r   r   Úminá   s    
ra   zminimum indexc                 C  s   t | |d||d�\}}|S rV   )ra   rW   r   r   r   Úargminö   s    rb   c                 C  s   | | S r   r   r?   r   r   r   Ú_sum_combineþ   s    rc   r   c                 C  s   t  | ¡} t j| |t|d�S )NrG   )r   rJ   rS   rc   )r,   rU   rH   r   r   r   r     s    
c                 C  s   | |A S r   r   r?   r   r   r   Ú_xor_combine  s    rd   zxor sumc                 C  s<   | j j}| ¡ stdƒ‚tj| |d�} tj| |t|||d�S )Nz#xor_sum only supported for integers)Ú_builder)rH   re   Ú
_generator)ÚtypeZscalarrP   Ú
ValueErrorr   rJ   rS   rd   )r,   rU   rH   re   rf   Z	scalar_tyr   r   r   Úxor_sum  s
    ri   Úcumsumc                 C  s   t  | ¡} t  | |t|¡S r   )r   rJ   Úassociative_scanrc   ©r,   rU   Úreverser   r   r   rj   %  s    
c                 C  s   | | S r   r   r?   r   r   r   Ú_prod_combine1  s    rn   Úcumprodc                 C  s   t  | ¡} t  | |t|¡S r   )r   rJ   rk   rn   rl   r   r   r   ro   6  s    
)r   Ún_dimsc                 C  s,  | j |? }|d|  dd|| d  g}t | |¡}t dd¡d d d …d f }t t|d|  dƒd d …d d d …f |¡}t t|| dƒd d …d d d …f |¡}	t || j¡}t |	| j¡}	tj| jj	dd�}
|j
|
dd�}|	j
|
dd�}| j
|
dd�}|t ||	k|A ||A t|ƒ¡A }|j
| jdd�S )Nr   r   r   T)ZbitwidthÚsigned)Zbitcast)r"   r   r!   ÚarangeÚbroadcast_tor   r)   Zget_int_dtyper*   rL   rN   r0   r-   )r   Úflipr   rp   Ún_outerr)   ÚyÚmaskÚleftÚrightZidtypeZileftZirightÚixrY   r   r   r   Ú_compare_and_swapB  s    
,("r{   )ÚstageÚorderrp   c                 C  sš   | j |? }t ||k¡ |dkrl|d|d |   dd| g}t t t dd¡ddd…df |¡| j¡}n|}t |¡D ]}t| ||||  |ƒ} qz| S )zb
    order_type 0 == ascending
    order_type 1 == descending
    order_type 2 == alternating
    r   r   r   N)	r"   r   Ústatic_assertr!   rs   rr   r)   Ústatic_ranger{   )r   r|   r}   rp   ru   r)   rt   r   r   r   r   Ú_bitonic_mergeV  s    
.r€   )ÚdimÚ
descendingc                 C  sv   |d krt | jƒd n|}t |t | jƒd kd¡ t| j| ƒ}t d|d ¡D ]}t| |||k rhdn||ƒ} qR| S )Nr   z+only minor dimension is currently supportedr   )Úlenr)   r   r~   r   r   r€   )r   r�   r‚   Z_dimrp   r   r   r   r   Úsorto  s    r„   c                 C  sF   t | ƒ} t |ƒ}| d kr$t|ƒd } | t|ƒd ks<tdƒ‚t | ¡S )Nr   z2Currently only support flipping the last dimension)r   rƒ   rQ   r   r
   )r�   r)   r   r   r   Ú_get_flip_dim  s    r…   c           	      C  s  t  t| jt|| jƒ ƒ¡ t  t| jƒ¡ t| jƒ}t| jƒt| jt|| jƒ ƒ }t  | dg| ¡}t  ||¡}t  	dd¡dd…df dt  	dd¡ k}t  
||¡D ]T}|}t  
d|d ¡D ]$}||krÀ||d krÀt  ||¡}qÀt|| |d dd�}q¨t  || j¡} | S )zÚ
    Flips a tensor `x` along the dimension `dim`.

    :param x: the first input tensor
    :type x: Block
    :param dim: the dimension to flip along (currently only final dimension supported)
    :type dim: int
    r   r   Nr   TrG   )r   r~   r   r)   r…   r"   r   r!   Zexpand_dimsrr   r   r   )	r   r�   ZstepsÚstartrv   rt   r   Zflip2r&   r   r   r   rt   ˆ  s    
 (rt   c                 C  sX   t  | |¡}t|jtƒst‚t|jƒdkr.|S t  ||jdd… d|jd  g ¡S dS )zÇ
    Interleaves the values of two tensors along their last dimension.

    The two tensors must have the same shape.

    Equivalent to `tl.join(a, b).reshape(a.shape[-1:] + [2 * a.shape[-1]])`
    r   Néþÿÿÿr   )r   Újoinr	   r)   ÚlistrQ   rƒ   r!   )r@   rA   Úcr   r   r   Ú
interleave¦  s
    	r‹   )F)NFTF)TF)NFTF)TF)NF)NFNN)r   F)r   F)N)0Ú
__future__r   Zruntime.jitr   Ú r   r   r   r   r   Z_tensor_member_fnr   Z_add_math_1arg_docstrr   r   r#   r'   r+   r-   r8   r<   r=   rB   Z_add_reduction_docstrr   rZ   r\   r^   r_   r`   ra   rb   rc   r   rd   Úbuiltinri   Z_add_scan_docstrrj   rn   ro   r{   r€   ZCONSTEXPR_0r„   r…   rt   r‹   r   r   r   r   Ú<module>   sÂ   	


&






ÿ



ÿ

	
		