o
    iEH                     @  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)xZtensor_args r   i/var/www/html/eduruby.in/lip-sync/lip-sync-env/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(   Z
new_kwargsresultr)   r1   r   r+   <   s   "z"_tensor_operation.<locals>.wrapperr   r,   r   r)   r   _tensor_operation7   s   r7   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   r9   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!   r8   r9   otherr   r   r   __ge__`      
zdebugger_constexpr.__ge__c                 C  s   t |tr|jn|}| j|kS r!   r8   rD   r   r   r   __gt__d   rG   zdebugger_constexpr.__gt__c                 C  s   t |tr|jn|}| j|kS r!   r8   rD   r   r   r   __le__h   rG   zdebugger_constexpr.__le__c                 C  s   t |tr|jn|}| j|k S r!   r8   rD   r   r   r   __lt__l   rG   zdebugger_constexpr.__lt__c                 C  s   t |tr|jn|}| j|kS r!   r8   rD   r   r   r   __eq__p   rG   zdebugger_constexpr.__eq__c                 C     t |tr|jn|}| j|B S r!   r8   rD   r   r   r   __or__t   rG   zdebugger_constexpr.__or__c                 C  rL   r!   r8   rD   r   r   r   __ror__x   rG   zdebugger_constexpr.__ror__c                 C     t |tr|jn|}| j|@ S r!   r8   rD   r   r   r   __and__|   rG   zdebugger_constexpr.__and__c                 C  rO   r!   r8   rD   r   r   r   __rand__   rG   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   Zfloat64r   
ValueErrorr   r   )r9   r   bitcastZ_builderZret_tyr   r   r   to   s   
zdebugger_constexpr.tor;   r<   r;   r   )FN)__name__
__module____qualname__r:   r?   rA   rB   rF   rH   rI   rJ   rK   rM   rN   rP   rQ   rT   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   )r9   r   r   r   r   r:      rC   zWrappedTensor.__init__r;   r   c                 C  s
   | j  S r!   )r   itemr>   r   r   r   rA      rC   zWrappedTensor.__index__r<   c                 C  s   dt | j S )NZwrapped_)r<   r   r>   r   r   r   r?      s   zWrappedTensor.__str__r   c                 C  s   t | jdk S )NT)r   allr   r[   r>   r   r   r   rB      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   rD   r   r   r   __add__      zWrappedTensor.__add__c                 C  
   |  |S r!   )r_   rD   r   r   r   __radd__      
zWrappedTensor.__radd__c                 C  r]   r!   r   subr   rD   r   r   r   __sub__   r`   zWrappedTensor.__sub__c                 C     t || jS r!   rd   rD   r   r   r   __rsub__   r`   zWrappedTensor.__rsub__c                 C  r]   r!   )r   mulr   rD   r   r   r   __mul__   r`   zWrappedTensor.__mul__c                 C  ra   r!   )rj   rD   r   r   r   __rmul__   rc   zWrappedTensor.__rmul__c                 C  r]   r!   r   divr   rD   r   r   r   __truediv__   r`   zWrappedTensor.__truediv__c                 C  rg   r!   rl   rD   r   r   r   __rtruediv__   r`   zWrappedTensor.__rtruediv__c                 C  r]   r!   r   Zfloor_divider   rD   r   r   r   __floordiv__   r`   zWrappedTensor.__floordiv__c                 C  rg   r!   rp   rD   r   r   r   __rfloordiv__   r`   zWrappedTensor.__rfloordiv__c                 C  r]   r!   r   	remainderr   rD   r   r   r   __mod__   r`   zWrappedTensor.__mod__c                 C  rg   r!   rs   rD   r   r   r   __rmod__   r`   zWrappedTensor.__rmod__c                 C  s   | j  S r!   rZ   r>   r   r   r   __neg__      zWrappedTensor.__neg__c                 C  s   | j  S r!   rZ   r>   r   r   r   
__invert__   rx   zWrappedTensor.__invert__c                 C  r]   r!   )r   bitwise_andr   rD   r   r   r   rP      r`   zWrappedTensor.__and__c                 C  r]   r!   )r   
bitwise_orr   rD   r   r   r   rM      r`   zWrappedTensor.__or__c                 C  r]   r!   )r   bitwise_xorr   rD   r   r   r   __xor__   r`   zWrappedTensor.__xor__c                 C  r]   r!   )r   Zbitwise_left_shiftr   rD   r   r   r   
__lshift__   r`   zWrappedTensor.__lshift__c                 C  r]   r!   )r   Zbitwise_right_shiftr   rD   r   r   r   
__rshift__   r`   zWrappedTensor.__rshift__c                 C  s
   | j |kS r!   rZ   rD   r   r   r   rH      rc   zWrappedTensor.__gt__c                 C  s
   || j kS r!   rZ   rD   r   r   r   __rgt__  rc   zWrappedTensor.__rgt__c                 C  s
   | j |kS r!   rZ   rD   r   r   r   rF   
  rc   zWrappedTensor.__ge__c                 C  s
   || j kS r!   rZ   rD   r   r   r   __rge__  rc   zWrappedTensor.__rge__c                 C  s
   | j |k S r!   rZ   rD   r   r   r   rJ     rc   zWrappedTensor.__lt__c                 C  s
   || j k S r!   rZ   rD   r   r   r   __rlt__  rc   zWrappedTensor.__rlt__c                 C  s
   | j |kS r!   rZ   rD   r   r   r   rI     rc   zWrappedTensor.__le__c                 C  s
   || j kS r!   rZ   rD   r   r   r   __rle__#  rc   zWrappedTensor.__rle__c                 C  r]   r!   r   equalr   rD   r   r   r   rK   (  r`   zWrappedTensor.__eq__c                 C  s   t | j| S r!   r   rD   r   r   r   __ne__-  s   zWrappedTensor.__ne__c                 C     | j |S r!   )r   __getitem__)r9   Zslicesr   r   r   r   2     zWrappedTensor.__getitem__Fc                 C  r   r!   )r   rT   )r9   r   rS   r   r   r   rT   C  r   zWrappedTensor.toNrV   rU   )r;   r   F)*rW   rX   rY   r:   rA   r?   rB   propertyr   r-   r7   r_   rb   rf   rh   rj   rk   rn   ro   rq   rr   ru   rv   rw   ry   rP   rM   r}   r~   r   rH   r   rF   r   rJ   r   rI   r   rK   r   r   rT   r   r   r   r   r      s    




r   c                 C  s   t | tr| jS | S r!   r8   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   )r9   r   r   r   r   r   r:   W  s   
zTritonLangProxy.__init__N         Fpointertorch.Tensormaskc                 C     | j |||S r!   )r   load)r9   r   r   rE   Zcache_modifierZeviction_policyZvolatiler   r   r   r   b  s   
zTritonLangProxy.loadr   c                 C  r   r!   )r   store)r9   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   r9   axisr   r   r   r   r     zTritonLangProxy.program_idc                 C  r   r   )r   r   Z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   )r9   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   r@   )r/   r   r   r   r   
<listcomp>  s    z)TritonLangProxy.zeros.<locals>.<listcomp>zUnsupported dtype r
   )sizer   r	   )	enumerater   r   	TypeErrorr   r   r   lcorer   Zis_fp32r   r   Zis_fp16float16Zis_bf16Zbfloat16Zis_int32r   Zis_int16Zint16Zis_int8Zint8zeros)r9   shaper   idr   r   r   r     s,   
zTritonLangProxy.zerosc                 C  s   |d u rt j}t r!   )r   r   NotImplementedError)r9   inputscaleshiftZnbitZdst_tyr   r   r   
dequantize  s   zTritonLangProxy.dequantizec                 C     t  r!   r   )r9   r   rE   r   r   r   	broadcast     zTritonLangProxy.broadcastc                 C  r   r!   r   r9   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   rE   )r   Tr   matmul)r9   r   rE   Ztrans_aZtrans_bZ
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   ZTensorr   r   r   )r9   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   )r9   r   r   r   r   r   r   r   atomic_xchg  s
   
zTritonLangProxy.atomic_xchgc                 C  s,   | j ||d}|| }| j ||| |S Nr   )r   r   r   r9   r   r   r   r   r6   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   rz   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)r9   	conditionr   yr   r   r   r     s   zTritonLangProxy.wherec                 C  r   r!   r   r9   r   r   r   r   r   umulhi  r   zTritonLangProxy.umulhic                 C  r   r!   r   )r9   r   r   ieee_roundingr   r   r   fdiv  r   zTritonLangProxy.fdivc                 C  
   t |S r!   )r   expr9   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   r9   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   )r9   r   rm   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   )r9   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   )r9   r   jZsize_iZsize_jZsize_gr   r   r   	swizzle2dQ  r   zTritonLangProxy.swizzle2dc                 C  r   r!   r   )r9   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   r9   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   )9rW   rX   rY   __annotations__r:   r7   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-   r7   r   r   r   r   r   r   r   r   <module>   s    @ >