a
    zdP                     @  s  d dl mZ d dlmZ d dlmZmZmZ d dlZddl	m
Z
mZ d dlmZ edZd	d
 ZG dd dZG dd deZG dd deZG dd deZedZedZedZedZedZedZedZedZedZedZedZedZedZ ed Z!ed!Z"eeZ#G d"d# d#Z$G d$d% d%Z%d&d' Z&e
dd(d)Z'e
dd*d+Z(e
dd,d-Z)d.d/ Z*e
dd0d1Z+e
dd2d3Z,e
dd4d5Z-e
dd6d7Z.e
dd9d:Z/e
dd;d<Z0e
dd=d>Z1e
dd@dAZ2e
ddCdDZ3e
ddEdFZ4dGdHdIdJdKZ5e
e5dLddMdNZ6e
e5dOddPdQZ7e
e5dRddSdTZ8e
e5dUddVdWZ9e
e5dXddYdZZ:e
e5d[dd\d]Z;e
e5d^dd_d`Z<e
e5daddbdcZ=e
ddddeZ>e
ddfdgZ?e
ddhdiZ@dGdHdIdjdkZAe
eAdlddmdnZBe
eAdoddpdqZCe
eAdrddsdtZDe
eAduddvdwZEe
eAdxddydzZFdGdHdId{d|ZGe
eGd}dd~dUZHe
eGddddZIe
eGddddXZJe
eGddddZKe
eGddddZLe
eGddddZMe
dddZNe
dddZOe
dddZPejQdd ZRejQdd ZSejQdd ZTejQdd} ZUejQeAddd ZVejQeAddddZWejQdd ZXejQdd ZYejQdd ZZejQdd Z[e
ddddZ\G dd dZ]dS )    )annotations)Enum)CallableListTypeVarN   )builtinsemantic)irTc                 C  s<  t | trt|| tS t | trd|   kr8dk rLn nt|| tS d|   kr`dk rtn nt|| tS d|   krdk rn nt|	| t
S d|   krdk rn nt|	| tS td|  dnBt | trt|| tS t | trt| j|S t | tr| S d	s8J d
|  dt|  dd S )Ni   l        l        l         l            l            zNonrepresentable integer .Fzcannot convert z	 of type z
 to tensor)
isinstancebooltensorZget_int1int1intZ	get_int32int32uint32Z	get_int64int64uint64RuntimeErrorfloatZget_fp32float32	constexpr
_to_tensorvaluetype)xbuilder r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/triton/language/core.pyr      s&    


r   c                   @  s~  e Zd Zg dZg dZg dZdgZg dZdgZG dd de	Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Z e!d5d6 Z"e!d7d8 Z#e!d9d: Z$d d;d<d=Z%d d;d>d?Z&d@dA Z'e(dBdC Z)dDdEdFdGdHZ*dIdJ Z+e(dKdLdMdNZ,dOdP Z-dQS )Rdtype)r   int8int16r   r   )uint8uint16r   r   )fp8fp16bf16fp32fp64r&   )r'   r(   r)   r*   voidc                   @  s   e Zd ZdZdZdS )zdtype.SIGNEDNESSr   r   N)__name__
__module____qualname__SIGNEDUNSIGNEDr   r   r   r    
SIGNEDNESS-   s   r1   c                 C  s  || _ |tjtj tj tj v s*J ||tjv r\tjj| _t	|
dd | _| j| _n|tjv rtjj| _t	|
dd | _| j| _n|tjv r
|dkrd| _d| _nX|dkrd| _d| _nB|d	krd
| _d| _n,|dkrd| _d| _n|dkrd| _d| _n|dkrd| _d S )Nr   r&         r'   
      r(      r)          r*   5   @   r+   r   )namer!   
SINT_TYPES
UINT_TYPESFP_TYPESOTHER_TYPESr1   r/   Zint_signednessr   splitZint_bitwidthZprimitive_bitwidthr0   Zfp_mantissa_width)selfr<   r   r   r    __init__1   s8    $







zdtype.__init__c                 C  s
   | j dkS )Nr&   r<   rB   r   r   r    is_fp8O   s    zdtype.is_fp8c                 C  s
   | j dkS )Nr'   rD   rE   r   r   r    is_fp16R   s    zdtype.is_fp16c                 C  s
   | j dkS )Nr(   rD   rE   r   r   r    is_bf16U   s    zdtype.is_bf16c                 C  s
   | j dkS )Nr)   rD   rE   r   r   r    is_fp32X   s    zdtype.is_fp32c                 C  s
   | j dkS )Nr*   rD   rE   r   r   r    is_fp64[   s    zdtype.is_fp64c                 C  s
   | j dkS )Nr   rD   rE   r   r   r    is_int1^   s    zdtype.is_int1c                 C  s
   | j dkS )Nr"   rD   rE   r   r   r    is_int8a   s    zdtype.is_int8c                 C  s
   | j dkS )Nr#   rD   rE   r   r   r    is_int16d   s    zdtype.is_int16c                 C  s
   | j dkS )Nr   rD   rE   r   r   r    is_int32g   s    zdtype.is_int32c                 C  s
   | j dkS )Nr   rD   rE   r   r   r    is_int64j   s    zdtype.is_int64c                 C  s
   | j dkS )Nr$   rD   rE   r   r   r    is_uint8m   s    zdtype.is_uint8c                 C  s
   | j dkS )Nr%   rD   rE   r   r   r    	is_uint16p   s    zdtype.is_uint16c                 C  s
   | j dkS )Nr   rD   rE   r   r   r    	is_uint32s   s    zdtype.is_uint32c                 C  s
   | j dkS )Nr   rD   rE   r   r   r    	is_uint64v   s    zdtype.is_uint64c                 C  s   | j tjv S N)r<   r!   r?   rE   r   r   r    is_floatingy   s    zdtype.is_floatingc                 C  s   | j tjv S rT   )r<   r!   CUSTOMIZED_FP_TYPESrE   r   r   r    is_customized_floating|   s    zdtype.is_customized_floatingc                 C  s   | j tjv S rT   )r<   r!   STANDARD_FP_TYPESrE   r   r   r    is_standard_floating   s    zdtype.is_standard_floatingc                 C  s   | j tjv S rT   )r<   r!   r=   rE   r   r   r    is_int_signed   s    zdtype.is_int_signedc                 C  s   | j tjv S rT   )r<   r!   r>   rE   r   r   r    is_int_unsigned   s    zdtype.is_int_unsignedc                 C  s   | j tjtj v S rT   )r<   r!   r=   r>   rE   r   r   r    is_int   s    zdtype.is_intc                 C  s   |   S rT   )rK   rE   r   r   r    is_bool   s    zdtype.is_boolc                   C  s   t dd S )NzNot implementedr   r   r   r   r    is_void   s    zdtype.is_voidc                   C  s   dS NFr   r   r   r   r    is_block   s    zdtype.is_blockc                   C  s   dS r`   r   r   r   r   r    is_ptr   s    zdtype.is_ptr)otherc                 C  s   t |tsdS | j|jkS r`   )r   r!   r<   rB   rc   r   r   r    __eq__   s    
zdtype.__eq__c                 C  s   |  | S rT   re   rd   r   r   r    __ne__   s    zdtype.__ne__c                 C  s   t | jfS rT   )hashr<   rE   r   r   r    __hash__   s    zdtype.__hash__c                 C  s   | S rT   r   rE   r   r   r    scalar   s    zdtype.scalar
ir.builderzir.typer   returnc                 C  s   | j dkr| S | j dkr$| S | j dv r6| S | j dv rH| S | j dv rZ| S | j dv rl| S | j dkr~| S | j dkr| S | j d	kr|	 S | j d
kr|
 S | j dkr| S td|  dd S )Nr+   r   )r"   r$   )r#   r%   )r   r   )r   r   r&   r'   r(   r)   r*   zfail to convert z to ir type)r<   Zget_void_tyZget_int1_tyZget_int8_tyZget_int16_tyZget_int32_tyZget_int64_tyZ
get_fp8_tyZget_half_tyZget_bf16_tyZget_float_tyZget_double_ty
ValueErrorrB   r   r   r   r    to_ir   s.    










zdtype.to_irc                 C  s   | j S rT   rD   rE   r   r   r    __str__   s    zdtype.__str__strrm   c                 C  s   | j S )z"See cache_key_part() in triton.cc.rD   rE   r   r   r    cache_key_part   s    zdtype.cache_key_partc                 C  s   d| j  S )Nztriton.language.rD   rE   r   r   r    __repr__   s    zdtype.__repr__N).r,   r-   r.   r=   r>   r?   rV   rX   r@   r   r1   rC   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   rW   rY   rZ   r[   r\   r]   staticmethodr_   ra   rb   re   rg   ri   propertyrj   rp   rq   rt   ru   r   r   r   r    r!   %   sZ   



r!   c                   @  sr   e Zd ZddddddZddd	d
dZdd Zdd Zdd Zd ddddZd ddddZ	e
dd ZdS )pointer_typer   r!   r   )
element_tyaddress_spacec                 C  s,   t |tstd|| _|| _|  | _d S )Nz,element_ty is a {type(element_ty).__name__}.)r   r!   	TypeErrorry   rz   rq   r<   )rB   ry   rz   r   r   r    rC      s
    
zpointer_type.__init__rk   zir.pointer_typerl   c                 C  s   | | j|dS Nr   )Z
get_ptr_tyry   rp   ro   r   r   r    rp      s    zpointer_type.to_irc                 C  s   d| j  dS )Nzpointer<>ry   rE   r   r   r    rq      s    zpointer_type.__str__c                 C  s   |   S rT   rq   rE   r   r   r    ru      s    zpointer_type.__repr__c                 C  s   dS NTr   rE   r   r   r    rb      s    zpointer_type.is_ptrr   rc   rm   c                 C  s&   t |tsdS | j|jko$| j|jkS r`   )r   rx   ry   rz   rd   r   r   r    re      s    
zpointer_type.__eq__c                 C  s   |  | S rT   rf   rd   r   r   r    rg      s    zpointer_type.__ne__c                 C  s   | S rT   r   rE   r   r   r    rj      s    zpointer_type.scalarN)r   )r,   r-   r.   rC   rp   rq   ru   rb   re   rg   rw   rj   r   r   r   r    rx      s   rx   c                   @  s~   e Zd ZdddddZdddd	d
Zdd Zdd Zdd ZddddZd ddddZ	d ddddZ
edd ZdS )
block_typer!   r   )ry   shapec                 C  sb   || _ |stdt|d tr.dd |D }|| _d| _| jD ]}|  j|9  _q@|  | _d S )Nz0d block_type is forbiddenr   c                 S  s   g | ]
}|j qS r   r   .0sr   r   r    
<listcomp>       z'block_type.__init__.<locals>.<listcomp>r   )ry   r{   r   r   r   numelrq   r<   )rB   ry   r   r   r   r   r    rC      s    
zblock_type.__init__rk   zir.block_typerl   c                 C  s   | | j|| jS rT   )Zget_block_tyry   rp   r   ro   r   r   r    rp     s    zblock_type.to_irc                 C  s   d| j  d| j dS )N<z, r}   )r   ry   rE   r   r   r    rq     s    zblock_type.__str__c                 C  s   |   S rT   r   rE   r   r   r    ru   
  s    zblock_type.__repr__c                 C  s   dS r   r   rE   r   r   r    ra     s    zblock_type.is_blockz	List[int]rs   c                 C  s   | j S rT   )r   rE   r   r   r    get_block_shapes  s    zblock_type.get_block_shapesr   r   c                 C  s&   t |tsdS | j|jko$| j|jkS r`   )r   r   ry   r   rd   r   r   r    re     s    
zblock_type.__eq__c                 C  s   |  | S rT   rf   rd   r   r   r    rg     s    zblock_type.__ne__c                 C  s   | j S rT   r~   rE   r   r   r    rj     s    zblock_type.scalarN)r,   r-   r.   rC   rp   rq   ru   ra   r   re   rg   rw   rj   r   r   r   r    r      s   r   c                   @  s4   e Zd ZddddddZdd Zdd	d
dZdS )function_typezList[dtype]None)	ret_typesparam_typesrm   c                 C  s   || _ || _d S rT   )r   r   )rB   r   r   r   r   r    rC   !  s    zfunction_type.__init__c                 C  s   d| j  d| j S )Nzfn (z) -> )r   r   rE   r   r   r    rq   %  s    zfunction_type.__str__rk   r   c                   s4    fdd| j D } fdd| jD } ||S )Nc                   s   g | ]}|  qS r   rp   )r   tyr   r   r    r   )  r   z'function_type.to_ir.<locals>.<listcomp>c                   s   g | ]}|  qS r   r   )r   Zret_typer   r   r    r   *  r   )r   r   Zget_function_ty)rB   r   Zir_param_typesr   r   r   r    rp   (  s    zfunction_type.to_irN)r,   r-   r.   rC   rq   rp   r   r   r   r    r      s   r   r+   r   r"   r#   r   r   r$   r%   r   r   r&   r'   r(   r)   r*   c                   @  s6  e Zd ZdZdd ZddddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLS )Mr   zL
    This class is used to store a value that is known at compile-time.
    c                 C  s   t |tr|j| _n|| _d S rT   r   r   r   )rB   r   r   r   r    rC   K  s    

zconstexpr.__init__rr   rs   c                 C  s   d| j  dS )Nz
constexpr[]r   rE   r   r   r    ru   Q  s    zconstexpr.__repr__c                 C  s   t | j|j S rT   r   r   rd   r   r   r    __add__T  s    zconstexpr.__add__c                 C  s   t |j| j S rT   r   rd   r   r   r    __radd__W  s    zconstexpr.__radd__c                 C  s   t | j|j S rT   r   rd   r   r   r    __sub__Z  s    zconstexpr.__sub__c                 C  s   t |j| j S rT   r   rd   r   r   r    __rsub__]  s    zconstexpr.__rsub__c                 C  s   t | j|j S rT   r   rd   r   r   r    __mul__`  s    zconstexpr.__mul__c                 C  s   t | j|j S rT   r   rd   r   r   r    __mod__c  s    zconstexpr.__mod__c                 C  s   t |j| j S rT   r   rd   r   r   r    __rmul__f  s    zconstexpr.__rmul__c                 C  s   t | j|j S rT   r   rd   r   r   r    __truediv__i  s    zconstexpr.__truediv__c                 C  s   t |j| j S rT   r   rd   r   r   r    __rtruediv__l  s    zconstexpr.__rtruediv__c                 C  s   t | j|j S rT   r   rd   r   r   r    __floordiv__o  s    zconstexpr.__floordiv__c                 C  s   t |j| j S rT   r   rd   r   r   r    __rfloordiv__r  s    zconstexpr.__rfloordiv__c                 C  s   t | j|jkS rT   r   rd   r   r   r    __gt__u  s    zconstexpr.__gt__c                 C  s   t |j| jkS rT   r   rd   r   r   r    __rgt__x  s    zconstexpr.__rgt__c                 C  s   t | j|jkS rT   r   rd   r   r   r    __ge__{  s    zconstexpr.__ge__c                 C  s   t |j| jkS rT   r   rd   r   r   r    __rge__~  s    zconstexpr.__rge__c                 C  s   t | j|jk S rT   r   rd   r   r   r    __lt__  s    zconstexpr.__lt__c                 C  s   t |j| jk S rT   r   rd   r   r   r    __rlt__  s    zconstexpr.__rlt__c                 C  s   t | j|jkS rT   r   rd   r   r   r    __le__  s    zconstexpr.__le__c                 C  s   t |j| jkS rT   r   rd   r   r   r    __rle__  s    zconstexpr.__rle__c                 C  s   t | j|jkS rT   r   rd   r   r   r    re     s    zconstexpr.__eq__c                 C  s   t | j|jkS rT   r   rd   r   r   r    rg     s    zconstexpr.__ne__c                 C  s
   t | jS rT   )r   r   rE   r   r   r    __bool__  s    zconstexpr.__bool__c                 C  s   t | j S rT   r   rE   r   r   r    __neg__  s    zconstexpr.__neg__c                 C  s   t | j|j@ S rT   r   rd   r   r   r    __and__  s    zconstexpr.__and__c                 C  s   t | jo|jS rT   r   rd   r   r   r    logical_and  s    zconstexpr.logical_andc                 C  s   t | j|jB S rT   r   rd   r   r   r    __or__  s    zconstexpr.__or__c                 C  s   t | jp|jS rT   r   rd   r   r   r    
logical_or  s    zconstexpr.logical_orc                 C  s   t | j
 S rT   r   rE   r   r   r    __pos__  s    zconstexpr.__pos__c                 C  s   t | j S rT   r   rE   r   r   r    
__invert__  s    zconstexpr.__invert__c                 C  s   t | j|j S rT   r   rd   r   r   r    __pow__  s    zconstexpr.__pow__c                 C  s   t | j|j? S rT   r   rd   r   r   r    
__rshift__  s    zconstexpr.__rshift__c                 C  s   t | j|j> S rT   r   rd   r   r   r    
__lshift__  s    zconstexpr.__lshift__c                 C  s   t | j S rT   r   rE   r   r   r    __not__  s    zconstexpr.__not__c                 O  s   | j |i |S rT   r   )rB   argskwdsr   r   r    __call__  s    zconstexpr.__call__N)(r,   r-   r.   __doc__rC   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   re   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   F  sJ   r   c                   @  s   e Zd ZddddZddddZedQd
dZdRddZedSddZdTddZ	edUddZ
dVddZedWddZdXddZedYddZedZddZed[ddZed\d d!Zed]d"d#Zed^d$d%Zed_d&d'Zed`d(d)Zedad*d+Zedbd,d-Zedcd.d/Zeddd0d1Zeded2d3Zedfd4d5Zedgd6d7Zedhd8d9Zedid:d;Zedjd<d=Zedkd>d?Z edld@dAZ!edmdBdCZ"edndDdEZ#edodFdGZ$edpdHdIZ%edqdJdKZ&e'dLdM Z(edrdOdPZ)d	S )sr   r!   )r   c                 C  sl   || _ d| _| r|j| _d| _| jD ]}|  j|9  _q(t| j| _|| _|j| _dd | jD | _d S )N)r   r   c                 S  s   g | ]}t |qS r   )r   r   r   r   r    r     r   z#tensor.__init__.<locals>.<listcomp>)handler   ra   r   r   r   rj   r!   )rB   r   r   r   r   r   r    rC     s    
ztensor.__init__rr   rs   c                 C  s(   t | jd ddd | jD  d S )N[,c                 s  s   | ]}t |V  qd S rT   )rr   r   r   r   r    	<genexpr>  r   z!tensor.__str__.<locals>.<genexpr>r   )rr   r!   joinr   rE   r   r   r    rq     s    ztensor.__str__Nc                 C  s   t ||}t| ||S rT   )r   r	   addrB   rc   _builderr   r   r    r     s    
ztensor.__add__c                 C  s   | j ||dS Nr   )r   r   r   r   r    r     s    ztensor.__radd__c                 C  s   t ||}t| ||S rT   r   r	   subr   r   r   r    r     s    
ztensor.__sub__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r     s    
ztensor.__rsub__c                 C  s   t ||}t| ||S rT   )r   r	   mulr   r   r   r    r     s    
ztensor.__mul__c                 C  s   | j ||dS r   )r   r   r   r   r    r     s    ztensor.__rmul__c                 C  s   t ||}t| ||S rT   r   r	   truedivr   r   r   r    r     s    
ztensor.__truediv__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r     s    
ztensor.__rtruediv__c                 C  s   t ||}t| ||S rT   r   r	   floordivr   r   r   r    r     s    
ztensor.__floordiv__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r     s    
ztensor.__rfloordiv__c                 C  s   t ||}t| ||S rT   r   r	   modr   r   r   r    r     s    
ztensor.__mod__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    __rmod__  s    
ztensor.__rmod__c                 C  s   t | |S rT   )r	   minusrB   r   r   r   r    r     s    ztensor.__neg__c                 C  s   t | |S rT   )r	   invertr   r   r   r    r     s    ztensor.__invert__c                 C  s   t ||}t| ||S rT   )r   r	   and_r   r   r   r    r     s    
ztensor.__and__c                 C  s   t ||}t| ||S rT   )r   r	   or_r   r   r   r    r     s    
ztensor.__or__c                 C  s   t ||}t| ||S rT   )r   r	   Zxor_r   r   r   r    __xor__  s    
ztensor.__xor__c                 C  s   t ||}t| ||S rT   )r   r	   Zshlr   r   r   r    r      s    
ztensor.__lshift__c                 C  s4   t ||}| j r"t| ||S t| ||S d S rT   )r   r!   rZ   r	   ZashrZlshrr   r   r   r    r   %  s    

ztensor.__rshift__c                 C  s   t ||}t| ||S rT   r   r	   greater_thanr   r   r   r    r   0  s    
ztensor.__gt__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r   5  s    
ztensor.__rgt__c                 C  s   t ||}t| ||S rT   r   r	   Zgreater_equalr   r   r   r    r   ;  s    
ztensor.__ge__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r   @  s    
ztensor.__rge__c                 C  s   t ||}t| ||S rT   r   r	   	less_thanr   r   r   r    r   F  s    
ztensor.__lt__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r   K  s    
ztensor.__rlt__c                 C  s   t ||}t| ||S rT   r   r	   Z
less_equalr   r   r   r    r   Q  s    
ztensor.__le__c                 C  s   t ||}t|| |S rT   r   r   r   r   r    r   V  s    
ztensor.__rle__c                 C  s   t ||}t| ||S rT   )r   r	   equalr   r   r   r    re   \  s    
ztensor.__eq__c                 C  s   t ||}t| ||S rT   )r   r	   	not_equalr   r   r   r    rg   a  s    
ztensor.__ne__c                 C  s   t ||}t| ||S rT   )r   r	   r   r   r   r   r    r   f  s    
ztensor.logical_andc                 C  s   t ||}t| ||S rT   )r   r	   r   r   r   r   r    r   k  s    
ztensor.logical_orc                 C  s   t | |S rT   )r	   not_r   r   r   r    r   r  s    ztensor.__not__c                 C  sl   t |tr|g}| }t|D ]J\}}t |trH|jd u rHt|||}q|td d d krZqdsJ dq|S )NFunsupported)r   slice	enumerater   r   r	   Zexpand_dims)rB   Zslicesr   retZdimslr   r   r    __getitem__v  s    
ztensor.__getitem__c                 C  s   dsJ dd S )NFz0Transposition must be created by the AST Visitorr   rE   r   r   r    r     s    ztensor.TFc                 C  s0   t |tr|j}|r"t| ||S t| ||S rT   )r   r   r   r	   bitcastcast)rB   r!   r   r   r   r   r    to  s
    
z	tensor.to)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)FN)*r,   r-   r.   rC   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   re   rg   r   r   r   r   rw   r   r   r   r   r   r    r     s   





r   c                 C  s   t | tr| jS | S rT   r   )vr   r   r    _constexpr_to_value  s    
r   c                 C  s   t | } t| |S )z
    Returns the id of the current program instance along the given :code:`axis`.

    :param axis: The axis of the 3D launch grid. Has to be either 0, 1 or 2.
    :type axis: int
    )r   r	   
program_idaxisr   r   r   r    r     s    r   c                 C  s   t | } t| |S )z
    Returns the number of program instances launched along the given :code:`axis`.

    :param axis: The axis of the 3D launch grid. Has to be either 0, 1 or 2.
    :type axis: int
    )r   r	   num_programsr   r   r   r    r     s    r   c                 C  s   t | } t |}t| ||S )a  
    Returns contiguous values within the open interval [:code:`start`, :code:`end`).

    :param start: Start of the interval. Must be a power of two.
    :type start: int
    :param stop: End of the interval. Must be a power of two >= start.
    :type stop: int
    )r   r	   arange)startendr   r   r   r    r     s    
r   c                 C  sj   t | } t| D ]J\}}t|ts2td| dt|jtstd| dt|j dqdd | D S )NzShape element  must have type `constexpr`1 must have type `constexpr[int]`, got `constexpr[r   c                 S  s   g | ]}t |qS r   )r   r   r   r   r   r    r     r   z%_shape_check_impl.<locals>.<listcomp>)r   r   r   r   r{   r   r   r   )r   idr   r   r    _shape_check_impl  s    
r   c                 C  s(   t | } t|}t|}t| |||S )a]  
    Returns a tensor filled with the scalar value for the given :code:`shape` and :code:`dtype`.

    :param shape: Shape of the new array, e.g., (8, 16) or (8, )
    :value value: A scalar value to fill the array with
    :type shape: tuple of ints
    :param dtype: Data-type of the new array, e.g., :code:`tl.float16`
    :type dtype: DType
    )r   r   r	   full)r   r   r!   r   r   r   r    r     s    r   c                 C  s   t | ||S )z
    Tries to broadcast the two given blocks to a common compatible shape.

    :param input: The first input tensor.
    :type input: Block
    :param other: The second input tensor.
    :type other: Block
    )r	   Zbroadcast_impl_value)inputrc   r   r   r   r    	broadcast  s    
r   c                 C  s   t |}t| ||S )z
    Tries to broadcast the given tensor to a new :code:`shape`.

    :param input: The input tensor.
    :type input: Block
    :param shape: The desired shape.
    :type shape: Tuple[int]
    )r   r	   Zbroadcast_impl_shaper   r   r   r   r   r    broadcast_to  s    
r   c                 C  s   t | |S rT   )r	   trans)r   r   r   r   r    r     s    r   Fc                 C  s   t | |||S )aq  
    Concatenate the given blocks

    :param input: The first input tensor.
    :type input:
    :param other: The second input tensor.
    :type other:
    :param reorder: Compiler hint. If true, the compiler is
    allowed to reorder elements while concatenating inputs.
    Only use if the order does not matter (e.g., result is
    only used in reduction ops)
    )r	   cat)r   rc   Zcan_reorderr   r   r   r    r     s    r   c                 C  s   t |}t| ||S )z
    Returns a tensor with the same elements as `input` but a different shape.
    The order of the elements may not be preserved.

    :param input: The input tensor.
    :type input:
    :param shape: The desired shape.
    :type shape: Tuple[int]

    r   r	   viewr   r   r   r    r     s    r   c                 C  s   t |}t| ||S rT   r   r   r   r   r    reshape.  s    r   Tc                 C  s   t |}t| |||S )a  
    Returns the matrix product of two blocks.

    The two blocks must be two-dimensional and have compatible inner dimensions.

    :param input: The first tensor to be multiplied.
    :type input: 2D tensor of scalar-type in {:code:`float16`, :code:`bfloat16`, :code:`float32`}
    :param other: The second tensor to be multiplied.
    :type other: 2D tensor of scalar-type in {:code:`float16`, :code:`bfloat16`, :code:`float32`}
    )r   r	   dot)r   rc   Z
allow_tf32r   r   r   r    r   9  s    r    c              	   C  sZ   t |durt||}t |dur,t||}t |}t |}t |}t| ||||||S )a  
    Return a tensor of data whose values are, elementwise, loaded from memory at location defined by :code:`pointer`.

    :code:`mask` and :code:`other` are implicitly broadcast to :code:`pointer.shape`.

    :code:`other` is implicitly typecast to :code:`pointer.dtype.element_ty`.

    :param pointer: Pointers to the data to be loaded.
    :type pointer: Block of dtype=triton.PointerDType
    :param mask: if mask[idx] is false, do not load the data at address :code:`pointer[idx]`.
    :type mask: Block of triton.int1, optional
    :param other: if mask[idx] is false, return other[idx]
    :type other: Block, optional
    :param cache_modifier: changes cache option in nvidia ptx
    'type cache_modifier: str, optional
    N)r   r   r	   load)pointermaskrc   Zcache_modifierZeviction_policyZvolatiler   r   r   r    r  N  s    

r  c                 C  s0   t ||}t|dur t ||}t| |||S )ad  
    Stores :code:`value` tensor of elements in memory, element-wise, at the memory locations specified by :code:`pointer`.

    :code:`value` is implicitly broadcast to :code:`pointer.shape` and typecast to :code:`pointer.dtype.element_ty`.

    :param pointer: The memory locations where the elements of :code:`value` are stored.
    :type pointer: Block of dtype=triton.PointerDType
    :param value: The tensor of elements to be stored.
    :type value: Block
    :param mask: If mask[idx] is false, do not store :code:`value[idx]` at :code:`pointer[idx]`.
    :type mask: Block of triton.int1, optional
    N)r   r   r	   store)r  r   r  r   r   r   r    r  k  s    

r  rr   zCallable[[T], T])r<   rm   c                   s   ddd fdd}|S )Nr   funcrm   c                   s   d}|j  d| _| S )Na+  
    Performs an atomic {name} at the memory location specified by :code:`pointer`.

    Return the data stored at :code:`pointer` before the atomic operation.

    :param pointer: The memory locations to compare-and-swap.
    :type pointer: Block of dtype=triton.PointerDType
    :param cmp: The values expected to be found in the atomic object
    :type cmp: Block of dtype=`pointer.dtype.element_ty`
    :param val: The values to copy in case the expected value matches the contained value.
    :type val: Block of dtype=`pointer.dtype.element_ty`
    rD   formatr   r  ZdocstrrD   r   r    
_decorator  s    z&_add_atomic_docstr.<locals>._decoratorr   r<   r
  r   rD   r    _add_atomic_docstr  s    r  zcompare-and-swapc                 C  s$   t ||}t ||}t| |||S rT   )r   r	   
atomic_cas)r  cmpvalr   r   r   r    r    s    

r  Zexchangec                 C  s   t ||}t| |||S rT   )r   r	   atomic_xchgr  r  r  r   r   r   r    r    s    
r  r   c                 C  s   t ||}t| |||S rT   )r   r	   
atomic_addr  r   r   r    r    s    
r  maxc                 C  s   t ||}t| |||S rT   )r   r	   
atomic_maxr  r   r   r    r    s    
r  minc                 C  s   t ||}t| |||S rT   )r   r	   
atomic_minr  r   r   r    r    s    
r  zlogical andc                 C  s   t ||}t| |||S rT   )r   r	   
atomic_andr  r   r   r    r    s    
r  z
logical orc                 C  s   t ||}t| |||S rT   )r   r	   	atomic_orr  r   r   r    r    s    
r  zlogical xorc                 C  s   t ||}t| |||S rT   )r   r	   
atomic_xorr  r   r   r    r    s    
r  c                 C  s.   t | |} t ||}t ||}t| |||S )a  
    Returns a tensor of elements from either :code:`x` or :code:`y`, depending on :code:`condition`.

    Note that :code:`x` and :code:`y` are always evaluated regardless of the value of :code:`condition`.

    If you want to avoid unintended memory operations, use the :code:`mask` arguments in `triton.load` and `triton.store` instead.

    The shape of :code:`x` and :code:`y` are both broadcast to the shape of :code:`condition`.
    :code:`x` and :code:`y` must have the data type.

    :param condition: When True (nonzero), yield x, otherwise yield y.
    :type condition: Block of triton.bool
    :param x: values selected at indices where condition is True.
    :param y: values selected at indices where condition is False.
    )r   r	   where)	conditionr   yr   r   r   r    r    s    


r  c                 C  s"   t | |} t ||}t| ||S rT   )r   r	   umulhi)r   r  r   r   r   r    r    s    

r  c                 C  s   t |}t| |||S rT   )r   r	   fdiv)r   r  ieee_roundingr   r   r   r    r    s    r  c                   s   ddd fdd}|S )Nr   r  c                   s   d}|j  d| _| S )Nzj
    Computes the element-wise {name} of :code:`x`

    :param x: the input values
    :type x: Block
    rD   r  r	  rD   r   r    r
    s    z)_add_math_1arg_docstr.<locals>._decoratorr   r  r   rD   r    _add_math_1arg_docstr  s    
r   Zexponentialc                 C  s   t | |S rT   )r	   expr   r   r   r   r    r!    s    r!  znatural logarithmc                 C  s   t | |S rT   )r	   logr"  r   r   r    r#    s    r#  Zcosinec                 C  s   t | |S rT   )r	   cosr"  r   r   r    r$    s    r$  Zsinec                 C  s   t | |S rT   )r	   sinr"  r   r   r    r%     s    r%  zsquare rootc                 C  s   t | |S rT   )r	   sqrtr"  r   r   r    r&  &  s    r&  c                   s   ddd fdd}|S )Nr   r  c                   s   d}|j  d| _| S )Nz
    Returns the {name} of all elements in the :code:`input` tensor along the provided :code:`axis`

    :param input: the input values
    :param axis: the dimension along which the reduction should be done
    rD   r  r	  rD   r   r    r
  2  s    z)_add_reduction_docstr.<locals>._decoratorr   r  r   rD   r    _add_reduction_docstr0  s    
r'  maximumc                 C  s   t |}t| ||S rT   )r   r	   r  r   r   r   r   r   r    r  ?  s    zmaximum indexc                 C  s   t |}t| ||S rT   )r   r	   argmaxr)  r   r   r    r*  F  s    r*  minimumc                 C  s   t |}t| ||S rT   )r   r	   r  r)  r   r   r    r  M  s    zminimum indexc                 C  s   t |}t| ||S rT   )r   r	   argminr)  r   r   r    r,  T  s    r,  sumc                 C  s   t |}t| ||S rT   )r   r	   r-  r)  r   r   r    r-  [  s    zxor sumc                 C  s   t |}t| ||S rT   )r   r	   xor_sumr)  r   r   r    r.  b  s    r.  c                 C  s
   t | S rT   )r	   debug_barrierr   r   r   r    r/  n  s    r/  c                 C  s~   t |tr|g}t|D ]J\}}t |ts:td| dt |jtstd| dt|j dqdd |D }t| |S )ze
    Let the compiler knows that the values in :code:`input` are all multiples of :code:`value`.
    values element r   r   r   c                 S  s   g | ]
}|j qS r   r   r   r   r   r    r     r   zmultiple_of.<locals>.<listcomp>)	r   r   r   r{   r   r   r   r	   multiple_ofr   valuesr   r   r   r   r   r    r1  s  s    

r1  c                 C  s~   t |tr|g}t|D ]J\}}t |ts:td| dt |jtstd| dt|j dqdd |D }t| |S )z_
    Let the compiler knows that the `value` first values in :code:`input` are contiguous.
    r0  r   r   r   c                 S  s   g | ]
}|j qS r   r   r   r   r   r    r     r   z"max_contiguous.<locals>.<listcomp>)	r   r   r   r{   r   r   r   r	   max_contiguousr2  r   r   r    r4    s    

r4  c                 C  s   t | dk| |  S Nr   )r  r   r   r   r    abs  s    r7  c                 C  s   | | d | S )z
    Computes the ceiling division of :code:`x` by :code:`div`

    :param x: the input number
    :type input: Block
    :param div: the divisor
    :param div: Block
    r   r   )r   divr   r   r    cdiv  s    
r9  c                 C  s   t j| |k | |S )z
    Computes the element-wise minimum of :code:`x` and :code:`y`.

    :param input: the first input tensor
    :type input: Block
    :param other: the second input tensor
    :type other: Block
    tritonlanguager  r   r  r   r   r    r+    s    
c                 C  s   t j| |k| |S )z
    Computes the element-wise maximum of :code:`x` and :code:`y`.

    :param input: the first input tensor
    :type input: Block
    :param other: the second input tensor
    :type other: Block
    r:  r=  r   r   r    r(    s    
sigmoidc                 C  s   ddt j|    S r|   )r;  r<  r!  r6  r   r   r    r>    s    softmaxc                 C  s8   | t j| d }t j|}t j|d}t|||S r5  )r;  r<  r  r!  r-  r  )r   r  znumZdenr   r   r    r?    s    c                 C  s   t j| | jgS )zm
    Returns a contiguous flattened view of :code:`x`

    :param x: the input tensor
    :type x: Block
    )r;  r<  r   r   r6  r   r   r    ravel  s    rB  c                 C  sR   | | | }|| }|| }|| }t || |}|||  }	|| | }
|	|
fS )a  
    Transforms indices of a row-major size_i*size_j matrix into those
    of one where indices are row major for each group of size_j rows.
    For example, for size_i = size_j = 4 and 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+  )r   jZsize_iZsize_jZsize_gZijZsize_gjZgroup_idZoff_iZnew_iZnew_jr   r   r    	swizzle2d  s    rD  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   )r   r!   r   r   r    zeros  s    
rE  c                 C  s   t | j| jS rT   )rE  r   r!   )r   r   r   r    
zeros_like  s    rF  r   c          	      G  s   dd l }| }t| tr| j}t|ts4J | dd}|D ]}||jvr<d} qTq<|sfJ | dg }|D ]}|t|| qnt	|||S )Nr   z is not stringTFz is not an ascii string)
stringr   r   r   rr   	printableappendr   r	   printf)	prefixr   r   rG  
new_prefixZb_asciichnew_argsargr   r   r    rJ    s    

rJ  c                   @  s*   e Zd ZdZd	ddZdd Zdd ZdS )
static_rangez$Iterator that counts upward forever.Nc                 C  sn   t |tsJ |d u r"td| _nt |ts0J || _|d u rPtd| _|| _nt |ts^J || _|| _d S )Nr   r   )r   r   stepr   r   )rB   Zarg1Zarg2rQ  r   r   r    rC   +  s    
zstatic_range.__init__c                 C  s   t dd S Nz8static_range can only be used in @triton.jit'd functionsr^   rE   r   r   r    __iter__:  s    zstatic_range.__iter__c                 C  s   t dd S rR  r^   rE   r   r   r    __next__=  s    zstatic_range.__next__)NN)r,   r-   r.   r   rC   rS  rT  r   r   r   r    rP  '  s   
rP  )N)N)N)N)N)N)N)FN)N)N)TN)NNr   r   FN)NN)N)NN)NN)NN)NN)NN)NN)NN)N)N)FN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)F)^
__future__r   enumr   typingr   r   r   r;  r   r   r	   Ztriton._C.libtriton.tritonr
   r   r   r!   rx   r   r   r+   r   r"   r#   r   r   r$   r%   r   r   Zfloat8float16Zbfloat16r   float64Zpi32_tr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r#  r$  r%  r&  r'  r  r*  r  r,  r-  r.  r/  r1  r4  Zjitr7  r9  r+  r(  r>  r?  rB  rD  rE  rF  rJ  rP  r   r   r   r    <module>   s8   *"0u Z










 

