o
    hEH                     @  s   d dl mZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ej
Z
dd	 Zd
d Zdd ZG dd dZG dd dZdd ZG dd dZdS )    )annotations   )core   )torch_wrapper)ExecutionContext)	MemoryMapc                 C  s2  ddi}t | trtj| gfdtji|S t | trUd|   kr%dk r4n ntj| gfdtji|S d|   kr>dk rMn ntj| gfdtji|S td|  d	t | trgtj| gfdtj	i|S t
| rn| S t | tru| S t | tr| jd
u rd
S t| jS | d
u rd
S J d|  dt|  d)zI
    Converts various Python primitive data types to PyTorch tensor.
    devicecudadtypei   l        l         l            zNonrepresentable integer .NFzcannot convert z	 of type z
 to tensor)
isinstancebooltorchtensorintint32int64RuntimeErrorfloatfloat32	is_tensorWrappedTensordebugger_constexprvalue_primitive_to_tensortype)xtensor_args r   P/var/www/html/ai/venv/lib/python3.10/site-packages/triton/interpreter/tl_lang.pyr      s,   







r   c                       fdd}|S )z
    A decorator function to harmonize function args:
        - converts primitives to PyTorch tensors
        - wraps PyTorch tensors with WrappedTensors
    c                    s,   t tdd | }t tdd |} | S )Nc                 S  s   t | S N)r   vr   r   r    <lambda>/   s    z0_infer_tensor.<locals>.wrapper.<locals>.<lambda>c                 S  s   t | r	t| S | S r"   )r   r   r   r#   r   r   r    r%   0   s    )tuplemap)argsnew_argsfuncr   r    wrapper.   s   z_infer_tensor.<locals>.wrapperr   r+   r,   r   r*   r    _infer_tensor(   s   r.   c                   r!   )z
    A decorator function to unwrap WrappedTensors and debugger_constexpr before calling the function.
    Can be combined with _infer_tensor decorator to harmonize args (everything to torch tensor).
    c                    s   | D ]}t |rJ dqdd  tt | } fdd| D }| d g|dd  R i |}t |r>t|S |S )Nzunexpected tensor argumentc                 S  s$   t | tr| jS t | tr| jS | S r"   )r   r   r   r   r   r#   r   r   r    unwrap_tensor@   s
   

z9_tensor_operation.<locals>.wrapper.<locals>.unwrap_tensorc                   s   i | ]	\}}| |qS r   r   ).0kr$   r/   r   r    
<dictcomp>H   s    z6_tensor_operation.<locals>.wrapper.<locals>.<dictcomp>r   r   )r   r   r&   r'   itemsr   )r(   kwargsargr)   
new_kwargsresultr*   r2   r    r,   <   s   "z"_tensor_operation.<locals>.wrapperr   r-   r   r*   r    _tensor_operation7   s   r9   c                   @  s   e Zd Zdd Zd"ddZ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 d!ZdS )%r   c                 C  s    t |tr|j| _d S || _d S r"   r   r   r   )selfr   r   r   r    __init__Q   s   

zdebugger_constexpr.__init__returnstrc                 C  s   dt | j d S )Nzdebugger_constexpr())r>   r   r;   r   r   r    __str__W   s   zdebugger_constexpr.__str__r   c                 C  s   | j S r"   r   r@   r   r   r    	__index__Z   s   zdebugger_constexpr.__index__c                 C  s
   t | jS r"   )r   r   r@   r   r   r    __bool__]      
zdebugger_constexpr.__bool__c                 C  s   t |tr|jn|}| j|kS r"   r:   r;   otherr   r   r    __ge__`      
zdebugger_constexpr.__ge__c                 C  s   t |tr|jn|}| j|kS r"   r:   rF   r   r   r    __gt__d   rI   zdebugger_constexpr.__gt__c                 C  s   t |tr|jn|}| j|kS r"   r:   rF   r   r   r    __le__h   rI   zdebugger_constexpr.__le__c                 C  s   t |tr|jn|}| j|k S r"   r:   rF   r   r   r    __lt__l   rI   zdebugger_constexpr.__lt__c                 C  s   t |tr|jn|}| j|kS r"   r:   rF   r   r   r    __eq__p   rI   zdebugger_constexpr.__eq__c                 C     t |tr|jn|}| j|B S r"   r:   rF   r   r   r    __or__t   rI   zdebugger_constexpr.__or__c                 C  rN   r"   r:   rF   r   r   r    __ror__x   rI   zdebugger_constexpr.__ror__c                 C     t |tr|jn|}| j|@ S r"   r:   rF   r   r   r    __and__|   rI   zdebugger_constexpr.__and__c                 C  rQ   r"   r:   rF   r   r   r    __rand__   rI   zdebugger_constexpr.__rand__FNc                 C  sJ   |t jfv r	t}n|t jkrt}n|t jfv rt}ntdt|| jS )Nzdtype not supported in debugger)	r   r   r   r   float64r   
ValueErrorr   r   )r;   r   bitcast_builderret_tyr   r   r    to   s   
zdebugger_constexpr.tor=   r>   r=   r   )FN)__name__
__module____qualname__r<   rA   rC   rD   rH   rJ   rK   rL   rM   rO   rP   rR   rS   rY   r   r   r   r    r   P   s    

r   c                   @  s(  e Zd Zdd ZdOddZdPdd	ZdQddZedd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd Ze	e
dd  Ze	e
d!d" Ze	e
d#d$ Ze	e
d%d& Ze	e
d'd( Ze	e
d)d* Ze	e
d+d, Ze	e
d-d. Ze	e
d/d0 Ze	e
d1d2 Ze	e
d3d4 Ze	e
d5d6 Ze	e
d7d8 Ze	e
d9d: Z e	e
d;d< Z!e	e
d=d> Z"e	e
d?d@ Z#e	e
dAdB Z$e	e
dCdD Z%e	e
dEdF Z&e	e
dGdH Z'e
dIdJ Z(e
dRdLdMZ)dNS )Sr   c                 C  s
   || _ d S r"   r   )r;   r   r   r   r    r<      rE   zWrappedTensor.__init__r=   r   c                 C  s
   | j  S r"   )r   itemr@   r   r   r    rC      rE   zWrappedTensor.__index__r>   c                 C  s   dt | j S )Nwrapped_)r>   r   r@   r   r   r    rA      s   zWrappedTensor.__str__r   c                 C  s   t | jdk S )NT)r   allr   r`   r@   r   r   r    rD      s   zWrappedTensor.__bool__c                 C  s   | j jS r"   )r   r   r@   r   r   r    r      s   zWrappedTensor.dtypec                 C     t | j|S r"   )r   addr   rF   r   r   r    __add__      zWrappedTensor.__add__c                 C  
   |  |S r"   )re   rF   r   r   r    __radd__      
zWrappedTensor.__radd__c                 C  rc   r"   r   subr   rF   r   r   r    __sub__   rf   zWrappedTensor.__sub__c                 C     t || jS r"   rj   rF   r   r   r    __rsub__   rf   zWrappedTensor.__rsub__c                 C  rc   r"   )r   mulr   rF   r   r   r    __mul__   rf   zWrappedTensor.__mul__c                 C  rg   r"   )rp   rF   r   r   r    __rmul__   ri   zWrappedTensor.__rmul__c                 C  rc   r"   r   divr   rF   r   r   r    __truediv__   rf   zWrappedTensor.__truediv__c                 C  rm   r"   rr   rF   r   r   r    __rtruediv__   rf   zWrappedTensor.__rtruediv__c                 C  rc   r"   r   floor_divider   rF   r   r   r    __floordiv__   rf   zWrappedTensor.__floordiv__c                 C  rm   r"   rv   rF   r   r   r    __rfloordiv__   rf   zWrappedTensor.__rfloordiv__c                 C  rc   r"   r   	remainderr   rF   r   r   r    __mod__   rf   zWrappedTensor.__mod__c                 C  rm   r"   rz   rF   r   r   r    __rmod__   rf   zWrappedTensor.__rmod__c                 C  s   | j  S r"   r_   r@   r   r   r    __neg__      zWrappedTensor.__neg__c                 C  s   | j  S r"   r_   r@   r   r   r    
__invert__   r   zWrappedTensor.__invert__c                 C  rc   r"   )r   bitwise_andr   rF   r   r   r    rR      rf   zWrappedTensor.__and__c                 C  rc   r"   )r   
bitwise_orr   rF   r   r   r    rO      rf   zWrappedTensor.__or__c                 C  rc   r"   )r   bitwise_xorr   rF   r   r   r    __xor__   rf   zWrappedTensor.__xor__c                 C  rc   r"   )r   bitwise_left_shiftr   rF   r   r   r    
__lshift__   rf   zWrappedTensor.__lshift__c                 C  rc   r"   )r   bitwise_right_shiftr   rF   r   r   r    
__rshift__   rf   zWrappedTensor.__rshift__c                 C  s
   | j |kS r"   r_   rF   r   r   r    rJ      ri   zWrappedTensor.__gt__c                 C  s
   || j kS r"   r_   rF   r   r   r    __rgt__  ri   zWrappedTensor.__rgt__c                 C  s
   | j |kS r"   r_   rF   r   r   r    rH   
  ri   zWrappedTensor.__ge__c                 C  s
   || j kS r"   r_   rF   r   r   r    __rge__  ri   zWrappedTensor.__rge__c                 C  s
   | j |k S r"   r_   rF   r   r   r    rL     ri   zWrappedTensor.__lt__c                 C  s
   || j k S r"   r_   rF   r   r   r    __rlt__  ri   zWrappedTensor.__rlt__c                 C  s
   | j |kS r"   r_   rF   r   r   r    rK     ri   zWrappedTensor.__le__c                 C  s
   || j kS r"   r_   rF   r   r   r    __rle__#  ri   zWrappedTensor.__rle__c                 C  rc   r"   r   equalr   rF   r   r   r    rM   (  rf   zWrappedTensor.__eq__c                 C  s   t | j| S r"   r   rF   r   r   r    __ne__-  s   zWrappedTensor.__ne__c                 C     | j |S r"   )r   __getitem__)r;   slicesr   r   r    r   2     zWrappedTensor.__getitem__Fc                 C  r   r"   )r   rY   )r;   r   rV   r   r   r    rY   C  r   zWrappedTensor.toNr[   rZ   )r=   r   F)*r\   r]   r^   r<   rC   rA   rD   propertyr   r.   r9   re   rh   rl   rn   rp   rq   rt   ru   rx   ry   r|   r}   r~   r   rR   rO   r   r   r   rJ   r   rH   r   rL   r   rK   r   rM   r   r   rY   r   r   r   r    r      s    




r   c                 C  s   t | tr| jS | S r"   r:   r#   r   r   r    _constexpr_to_valueM  s   
r   c                   @  s  e Zd ZU ded< ded< dxddZe			
			dydzddZed{d|ddZedd Zedd Z	edd Z
edd Zed{ddZedd  Zed!d" Zed#d$ Zed%d& Zed}d(d)Zed*d+ Zed{d,d-Zed{d.d/Zed{d0d1Zed{d2d3Zed{d4d5Zed{d6d7Zed{d8d9Zed:d; Zed<d= Zed~d>d?Zed@dA ZedBdC ZedDdE ZedFdG Z edHdI Z!edJdK Z"edLdM Z#edNdO Z$edPdQ Z%edRdS Z&edTdU Z'edVdW Z(edXdY Z)edZd[ Z*ed\d] Z+ed^d_ Z,ed~d`daZ-edbdc Z.eddde Z/edfdg Z0ed{dhdiZ1edjdk Z2ed{dldmZ3edndo Z4ed{dpdqZ5edrds Z6ed{dtduZ7ed{dvdwZ8d	S )TritonLangProxyr   _memory_mapr   _context
memory_mapcontextc                 C  s   || _ || _d S r"   )r   r   )r;   r   r   r   r   r    r<   W  s   
zTritonLangProxy.__init__N         Fpointertorch.Tensormaskc                 C     | j |||S r"   )r   load)r;   r   r   rG   cache_modifiereviction_policyvolatiler   r   r    r   b  s   
zTritonLangProxy.loadr   c                 C  r   r"   )r   store)r;   r   r   r   r   r   r    r   n     zTritonLangProxy.storec                 C  0   |t | jjk s
J tj| jj| gtjddS Nr
   r   r	   )lenr   
program_idr   r   r   r;   axisr   r   r    r   r     zTritonLangProxy.program_idc                 C  r   r   )r   r   program_sizer   r   r   r   r   r   r    num_programsw  r   zTritonLangProxy.num_programsc                 C  s   t j||t jddS )Nr
   )startendr   r	   )r   aranger   )r;   r   r   r   r   r    r   |  s   zTritonLangProxy.arangec                 C  s   t |D ]%\}}t|tstd| dt|jts)td| dt|j dqdd |D }t|tjrn|	 r?t
j}n/| rGt
j}n'| rOt
j}n| rWt
j}n| r_t
j}n| rgt
j}ntd| t
j||dd	S )
NzShape element z must have type `constexpr`z1 must have type `constexpr[int]`, got `constexpr[]c                 S  s   g | ]}|j qS r   rB   )r0   r   r   r   r    
<listcomp>  s    z)TritonLangProxy.zeros.<locals>.<listcomp>zUnsupported dtype r
   )sizer   r	   )	enumerater   r   	TypeErrorr   r   r   lcorer   is_fp32r   r   is_fp16float16is_bf16bfloat16is_int32r   is_int16int16is_int8int8zeros)r;   shaper   idr   r   r    r     s,   
zTritonLangProxy.zerosc                 C  s   |d u rt j}t r"   )r   r   NotImplementedError)r;   inputscaleshiftnbitdst_tyr   r   r    
dequantize  s   zTritonLangProxy.dequantizec                 C     t  r"   r   )r;   r   rG   r   r   r    	broadcast     zTritonLangProxy.broadcastc                 C  r   r"   r   r;   r   r   r   r   r    broadcast_to  r   zTritonLangProxy.broadcast_toc                 C  r   r"   r   r   r   r   r    cat  r   zTritonLangProxy.catc                 C  r   r"   r   r   r   r   r    reshape  r   zTritonLangProxy.reshapeTc                 C  s2   |j |j ksJ |r|j}|r|j}tj||dS )N)r   rG   )r   Tr   matmul)r;   r   rG   trans_atrans_b
allow_tf32r   r   r    dot  s   zTritonLangProxy.dotc                 C  sl   | j |d d}t|tjstj|g|jdd}t|tjs(tj|g|jdd}||kr4| j ||d  |S )Nr   r
   r   )r   r   r   r   Tensorr   r   r   )r;   r   cmpvalstoredr   r   r    
atomic_cas  s   zTritonLangProxy.atomic_casc                 C  sB   t |trtj|gtjdd}| j||d}| j||| |S )Nr
   r   r   )r   r   r   r   r   r   r   r   )r;   r   r   r   r   r   r   r    atomic_xchg  s
   
zTritonLangProxy.atomic_xchgc                 C  s,   | j ||d}|| }| j ||| |S Nr   )r   r   r   r;   r   r   r   r   r8   r   r   r    
atomic_add  s   zTritonLangProxy.atomic_addc                 C  0   | j ||d}t||}| j ||| |S r   )r   r   r   maximumr   r   r   r   r    
atomic_max     zTritonLangProxy.atomic_maxc                 C  r   r   )r   r   r   minimumr   r   r   r   r    
atomic_min  r   zTritonLangProxy.atomic_minc                 C  r   Nr   )r   r   r   r   r   r   r   r   r    
atomic_and  r   zTritonLangProxy.atomic_andc                 C  r   r   )r   r   r   r   r   r   r   r   r    	atomic_or  r   zTritonLangProxy.atomic_orc                 C  r   r   )r   r   r   r   r   r   r   r   r    
atomic_xor  r   zTritonLangProxy.atomic_xorc                 C  s&   t |}t |}t |}t|||S r"   )r   r   where)r;   	conditionr   yr   r   r    r     s   zTritonLangProxy.wherec                 C  r   r"   r   r;   r   r   r   r   r    umulhi  r   zTritonLangProxy.umulhic                 C  r   r"   r   )r;   r   r   ieee_roundingr   r   r    fdiv  r   zTritonLangProxy.fdivc                 C  
   t |S r"   )r   expr;   r   r   r   r    r        
zTritonLangProxy.expc                 C  r   r"   )r   logr   r   r   r    r   	  r   zTritonLangProxy.logc                 C  r   r"   )r   cosr   r   r   r    r     r   zTritonLangProxy.cosc                 C  r   r"   )r   sinr   r   r   r    r     r   zTritonLangProxy.sinc                 C  r   r"   )r   sqrtr   r   r   r    r     r   zTritonLangProxy.sqrtc                 C  r   r"   r   r@   r   r   r    globaltimer  r   zTritonLangProxy.globaltimerc                 C  r   r"   r   r@   r   r   r    clock  r   zTritonLangProxy.clockc                 C  r   r"   r   r@   r   r   r    debug_barrier!  r   zTritonLangProxy.debug_barrierc                 C     |S r"   r   r;   r   valuesr   r   r    multiple_of%     zTritonLangProxy.multiple_ofc                 C  r  r"   r   r  r   r   r    max_contiguous)  r  zTritonLangProxy.max_contiguousc                 C  r  r"   r   r  r   r   r    max_constancy-  r  zTritonLangProxy.max_constancyc                 C  r   r"   )r   absr   r   r   r    r
  1  r   zTritonLangProxy.absc                 C  s   || d | S )Nr   r   )r;   r   rs   r   r   r    cdiv5  r   zTritonLangProxy.cdivc                 C  s<   t |trtj|dd}t |trtj|dd}t||S )Nr
   )r	   )r   r   r   r   r   r   r   r   r    r   9  s
   

zTritonLangProxy.minimumc                 C  s   t ||S r"   )r   r   r   r   r   r    r   A  r   zTritonLangProxy.maximumc                 C  r   r"   r   r   r   r   r    sigmoidE  r   zTritonLangProxy.sigmoidc                 C  r   r"   r   )r;   r   r   r   r   r    softmaxI  r   zTritonLangProxy.softmaxc                 C  r   r"   r   r   r   r   r    ravelM  r   zTritonLangProxy.ravelc                 C  r   r"   r   )r;   r   jsize_isize_jsize_gr   r   r    	swizzle2dQ  r   zTritonLangProxy.swizzle2dc                 C  r   r"   r   )r;   r   r   r   r    
zeros_likeU  r   zTritonLangProxy.zeros_likec                 C  "   |d u r	t |S t j||djS N)dim)r   maxr  r;   r   r   r   r   r    r  Y     
zTritonLangProxy.maxc                 C  r   r"   r   r  r   r   r    argmax_  r   zTritonLangProxy.argmaxc                 C  r  r  )r   minr  r  r   r   r    r  c  r  zTritonLangProxy.minc                 C  r   r"   r   r  r   r   r    argmini  r   zTritonLangProxy.argminc                 C      |d u r	t |S t j||dS r  )r   sumr  r   r   r    r  m     
zTritonLangProxy.sumc                 C  r   r"   r   r  r   r   r    xor_sums  r   zTritonLangProxy.xor_sumc                 C  r  r  )r   cumsumr  r   r   r    r"  w  r   zTritonLangProxy.cumsumc                 C  r  r  )r   cumprodr  r   r   r    r#  }  r   zTritonLangProxy.cumprod)r   r   r   r   )Nr   r   r   F)r   r   r   r   r"   )r   r   r   r   )FFTr   )9r\   r]   r^   __annotations__r<   r9   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  r  r  r  r  r  r  r!  r"  r#  r   r   r   r    r   S  s   
 


































r   N)
__future__r   languager   r   r   r   r   r   r   r   r   r.   r9   r   r   r   r   r   r   r   r    <module>   s    @ >