U
    yÊ¹jåY  ã                	   @  s°  d dl mZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	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 ddlmZmZ ej ej ej e¡¡¡ZdZd.dd	„Z d
d„ Z!dd„ Z"dd„ Z#edƒZ$G dd„ dej%ƒZ&e '¡ dd„ ƒZ(G dd„ dee$ ƒZ)G dd„ de)e$ ƒZ*edddœdd„ƒZ+edddddœdddd d!œd"d„ƒZ+d/dddddd#œd$ddddd%d&œd'd„Z+G d(d)„ d)ƒZ,G d*d+„ d+ƒZ-d,d-„ Z.dS )0é    )ÚannotationsÚdivisionN)ÚdefaultdictÚ
namedtuple)	ÚCallableÚGenericÚIterableÚListÚOptionalÚTypeVarÚUnionÚcastÚoverloadé   )Úget_backendÚpath_to_ptxasz2.1.0c                 C  sT   | d krt ƒ } zddlm} || ƒW S  tk
rN   dd l}|j | ¡j Y S X d S )Nr   )Ú_cuda_getCurrentRawStream)Úget_current_deviceZtorch._Cr   ÚImportErrorÚtorchÚcudaZcurrent_streamZcuda_stream)Úidxr   r   © r   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/triton/runtime/jit.pyÚget_cuda_stream   s    
r   c                  C  s   dd l } | j ¡ S ©Nr   )r   r   Zcurrent_device)r   r   r   r   r      s    r   c                 C  s   dd l }|j | ¡ d S r   )r   r   Z
set_device©r   r   r   r   r   Úset_current_device$   s    r   c                 C  s   dd l }|j | ¡S r   )r   r   Úget_device_capabilityr   r   r   r   r   )   s    r   ÚTc                      s>   e Zd ZdZddœ‡ fdd„Zdd„ Zdd	„ Zd
d„ Z‡  ZS )ÚDependenciesFinderzÅ
    This AST visitor is used to find dependencies of a JITFunction. This can
    be used to invalidate a JITFunction's hash when its source code -- or
    that of its dependencies -- changes.
    ÚNone©Úreturnc                   s*   t ƒ  ¡  t | d¡¡ ¡ | _|| _d S )Núutf-8)ÚsuperÚ__init__ÚhashlibÚmd5ÚencodeÚ	hexdigestÚretÚglobals)Úselfr,   Úsrc©Ú	__class__r   r   r&   <   s    
zDependenciesFinder.__init__c                 C  s   | j  |jd ¡S ©N)r,   ÚgetÚid)r-   Únoder   r   r   Ú
visit_NameA   s    zDependenciesFinder.visit_Namec                 C  s`   |   |j¡}t|tjƒr&|   |j¡}q|d ksPt|ddƒdksPt|ddƒ d¡rTd S t||jƒS )NÚ__name__Ú Ztritonz.triton)ÚvisitÚvalueÚ
isinstanceÚastÚ	AttributeÚgetattrÚendswithÚattr)r-   r4   Úlhsr   r   r   Úvisit_AttributeD   s    *z"DependenciesFinder.visit_Attributec                 C  sÖ   |   |j¡}|d krd S t |¡r&d S |jrF|j d¡sBd|jkrFd S t|tƒsbtd|j	› d�ƒ‚|j
d kr˜t |j¡}t|j|jƒ}|  |¡ |j|_
tt|ddƒƒ}| j|j
 |  d¡| _t | j¡ ¡ | _d S )Nztriton.z.triton.z
Function "zv" is being called from a Triton function but is not a Triton function itself. Decorate it with @triton.jit to fix thisÚnoinlineFr$   )r8   ÚfuncÚinspectÚ	isbuiltinÚ
__module__Ú
startswithr:   ÚJITFunctionÚAssertionErrorr6   Úhashr;   Úparser.   r    Ú__globals__r+   Ústrr=   r)   r'   r(   r*   )r-   r4   rC   ÚtreeÚfinderrB   r   r   r   Ú
visit_CallL   s     


zDependenciesFinder.visit_Call)	r6   rF   Ú__qualname__Ú__doc__r&   r5   rA   rP   Ú__classcell__r   r   r/   r   r    5   s
   r    c               
   C  sh  dd l } g }ttdƒ�}|t | ¡ ¡ ¡ g7 }W 5 Q R X tj 	t
d¡}|  |g¡D ]>}t|j |j¡jdƒ�}|t | ¡ ¡ ¡ g7 }W 5 Q R X qTttj 	t
d¡dƒ�}|t | ¡ ¡ ¡ g7 }W 5 Q R X tj 	t
d¡}|  |g¡D ]>}t|j |j¡jdƒ�}|t | ¡ ¡ ¡ g7 }W 5 Q R X qætƒ d }t t |dg¡¡ ¡ }d 	t¡d | d d 	|¡ S )Nr   ÚrbÚcompilerz_C/libtriton.soÚlanguagez	--versionú-)ÚpkgutilÚopenÚ__file__r'   r(   Úreadr*   ÚosÚpathÚjoinÚTRITON_PATHÚiter_modulesÚmodule_finderÚ	find_specÚnameÚoriginr   Ú
subprocessÚcheck_outputÚTRITON_VERSION)rX   ÚcontentsÚfZcompiler_pathÚlibZlanguage_pathZptxasZptxas_versionr   r   r   Úversion_keyc   s"    "$"$
rk   c                   @  s$   e Zd ZU ded< ddœdd„ZdS )ÚKernelInterfacer   Úrunr"   c                 C  s   t ttjt t| jƒ|d�ƒS )z­
        A JIT function is launched with: fn[grid](*args, **kwargs).
        Hence JITFunction.__getitem__ returns a callable proxy that
        memorizes the grid.
        )Úgrid)r   r   Ú	functoolsÚpartialr   rm   )r-   rn   r   r   r   Ú__getitem__€   s    zKernelInterface.__getitem__N)r6   rF   rQ   Ú__annotations__rq   r   r   r   r   rl   }   s   
rl   c                      sì   e Zd ZdZdZedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZ	dd„ Z
edd„ ƒZdd„ Zdd„ Zdd„ Zddœdd„Zddœdd„Zddddœdd„Zd d!„ Zd0d"d#„Zed$d%„ ƒZd&d'„ Zd(d)„ Zd*d+„ Z‡ fd,d-„Zd.d/„ Z‡  ZS )1rH   Né   c                 C  sŒ   t | dƒr| jS t| tƒrdS t| tƒrVd| kr<| dkr<dS d| krP| dkrPdS d	S n2t| tƒrdd
S | d krpd S tdt| ƒ› d| › �ƒ‚d S )NÚdtypeÚi1i   €iÿÿÿÚi32l            l   ÿÿÿÿ Úu64Úi64Úfp32zUnsupported type z for )Úhasattrrt   r:   ÚboolÚintÚfloatÚ	TypeErrorÚtype©Úargr   r   r   Ú_key_of�   s    



zJITFunction._key_ofc                 C  s"   t | dƒrt | jdƒr| jjS dS )NÚdevicer   r7   )rz   rƒ   r   r€   r   r   r   Ú
_device_of£   s    
zJITFunction._device_ofc                 C  s"   t | dƒrt| jtƒr|  ¡ S dS )NÚ	is_pinnedF)rz   r:   r…   r   r€   r   r   r   Ú_pinned_memory_of«   s    
zJITFunction._pinned_memory_ofc                 C  sD   t | dƒr|  ¡ tj dkS t| tƒr:| d dk| dkfS | d kfS )NÚdata_ptrr   rs   é   ©rz   r‡   rH   Údivisibilityr:   r|   r€   r   r   r   Ú_spec_of³   s
    

zJITFunction._spec_ofc                   sR   dd„ ‰ ‡ ‡fdd„t |ƒD ƒ}‡fdd„t |ƒD ƒ}tdddgƒt|ƒt|ƒƒS )	Nc                 S  sD   t | dƒr|  ¡ tj dkS t| tƒr4| tj dkS | d kr@dS dS )Nr‡   r   TFr‰   )Úxr   r   r   Úis_divisible_by_16¼   s    

z3JITFunction._get_config.<locals>.is_divisible_by_16c                   s&   h | ]\}}ˆ |ƒr|ˆj kr|’qS r   )Údo_not_specialize©Ú.0Úir�   ©r�   r-   r   r   Ú	<setcomp>Ä   s      
 z*JITFunction._get_config.<locals>.<setcomp>c                   s:   h | ]2\}}t |tƒst |tƒr|d kr|ˆ jkr|’qS )rˆ   )r:   r{   r|   rŽ   r�   ©r-   r   r   r“   Å   s     
 
  
 Zinstance_descriptorÚdivisible_by_16Ú
equal_to_1)Ú	enumerater   Útuple)r-   Úargsr•   r–   r   r’   r   Ú_get_config»   s    zJITFunction._get_configc                 C  sz   | d krdS t | ƒ d¡d }dddddd	d
ddddddddddœ}t| ¡ ƒD ]}|||< qPt| t ƒrl| S d|| › �S )Nz*i8Ú.éÿÿÿÿru   Zfp8e4Zfp8e5Zfp8e4b15Zfp16Zbf16ry   Zfp64Úi8Zi16rv   rx   Úu8Úu16Úu32rw   )r{   Zfloat8e4Zfloat8e5Zfloat8e4b15Zfloat16Zbfloat16Zfloat32Zfloat64Zint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64Ú*)rM   ÚsplitÚlistÚvaluesr:   )ÚkeyZ	dtype_strZtysÚvr   r   r   Ú_type_ofÉ   s.    ð
zJITFunction._type_ofc                   s    d  ‡ fdd„t|ƒD ƒ¡}|S )Nú,c                   s   g | ]\}}ˆ   |¡‘qS r   )r§   )r�   r‘   Úkr”   r   r   Ú
<listcomp>ç   s     z/JITFunction._make_signature.<locals>.<listcomp>)r^   r—   )r-   Zsig_keyÚ	signaturer   r”   r   Ú_make_signatureæ   s    zJITFunction._make_signaturec                 C  s   t t| j|ƒƒ}|S r1   )ÚdictÚzipÚ
constexprs)r-   Zconstexpr_keyÚ	constantsr   r   r   Ú_make_constantsê   s    zJITFunction._make_constantsc	              	   C  sª   t jd krdS | jj}	| jj}
d dd„ t| j|d ƒD ƒ¡}|	› d|› d|› d|› d	�}t|ƒ}G d
d„ dƒ}t	|||||||d�}t j||||
|	ƒd|i|–ddd�S )NFú, c                 S  s   g | ]\}}|› d |› �‘qS )z: r   ©r�   rc   Útyr   r   r   rª   ó   s     z*JITFunction._call_hook.<locals>.<listcomp>rˆ   z[num_warps=z, num_stages=ú](ú)c                   @  s   e Zd Zdd„ ZdS )z.JITFunction._call_hook.<locals>.LegacyCompilerc                 S  s   || _ || _d S r1   )Úmodulerc   )r-   r·   rc   r   r   r   r&   ø   s    z7JITFunction._call_hook.<locals>.LegacyCompiler.__init__N)r6   rF   rQ   r&   r   r   r   r   ÚLegacyCompiler÷   s   r¸   )r«   rƒ   r°   Ú	num_warpsÚ
num_stagesÚextern_libsÚconfigsr¥   )r¥   ÚreprÚfnÚcompileZis_manual_warmupZalready_compiled)
rH   Ú
cache_hookr¾   r6   rF   r^   r®   Ú	arg_namesrM   r­   )r-   r¥   r«   rƒ   r°   r¹   rº   r»   r¼   rc   r·   Z	arg_reprsr½   r¸   Úkwargsr   r   r   Ú
_call_hookî   s    
   þzJITFunction._call_hookrM   r"   c                 C  s�   | j  |d¡}|dkrJd|› dtj› d|› d|› dtj› d|› d|› d	�S d
|krfd|› dtj› d�S |dkrˆd|› dtj› d|› d�S dS d S )Nr7   ú(z.data_ptr() % z == 0) if hasattr(z,, "data_ptr")                         else (z % z == 0, z == 1) if isinstance(z,, int)                         else (False,)ÚTensorz == 0)r|   z == 1)z(False,))rr   r2   rH   rŠ   ©r-   r�   Zarg_annotationr   r   r   Ú_get_arg_specialization_key  s    4z'JITFunction._get_arg_specialization_keyc                 C  sH   | j  |d¡}d|kr |› d�S |dkr,dS |dkr8dS d|› d	�S d S )
Nr7   rÅ   z.dtyper{   ru   r}   ry   z_key_of(r¶   )rr   r2   rÆ   r   r   r   Ú_get_arg_sig_key  s    
zJITFunction._get_arg_sig_keyz	List[str]z
List[bool])Údevice_typesÚpinned_memory_flagsr#   c                 C  sv   dd„ |D ƒ}d|kr.dd l }|jjr*dS dS tdd„ |D ƒƒ}tdd„ |D ƒƒ}|r^|r^dS t|ƒdkrr|d S dS )	Nc                 S  s   g | ]}|d kr|‘qS )r7   r   ©r�   Zdevice_typer   r   r   rª     s      z5JITFunction._conclude_device_type.<locals>.<listcomp>r   r   Úhipc                 s  s   | ]}|d kV  qdS )ÚcpuNr   rË   r   r   r   Ú	<genexpr>"  s     z4JITFunction._conclude_device_type.<locals>.<genexpr>c                 s  s   | ]
}|V  qd S r1   r   )r�   Zpinned_memory_flagr   r   r   rÎ   #  s     )r   ÚversionrÌ   ÚallÚanyÚlen)r-   rÉ   rÊ   r   Zis_cpuZis_pinned_memoryr   r   r   Ú_conclude_device_type  s    z!JITFunction._conclude_device_typec                   sÒ  ‡ fdd„t ˆ jƒD ƒ}‡ fdd„t ˆ jƒD ƒ}d |¡}d ‡ fdd„|D ƒ¡}dd dd„ |D ƒ¡ d }dd d	d„ |D ƒ¡ d }d |¡}g }t |ƒD ]$\}	}
|	ˆ jkr´q |ˆ  |
¡g7 }q d |¡}d
 dd„ ˆ jD ƒ¡}d dd„ tˆ jˆ jƒD ƒ¡}dˆ jj› d|› d|› dt	|ƒdk�r4|› d
�nd› dt	|ƒdk�rR|› d
�nd› d|› d|› d|› d|› d|› dd dd„ ˆ jD ƒ¡› d�}t
ƒ tˆ ˆ jˆ jˆ jˆ jˆ jttttdœ}t||ƒ |ˆ jj S )Nc                   s    g | ]\}}|ˆ j kr|› ‘qS r   ©r¯   r�   r”   r   r   rª   +  s     
 z.JITFunction._make_launcher.<locals>.<listcomp>c                   s    g | ]\}}|ˆ j kr|› ‘qS r   rÔ   r�   r”   r   r   rª   ,  s     
 r²   c                   s   g | ]}ˆ   |¡‘qS r   )rÈ   ©r�   r�   r”   r   r   rª   /  s     ú[c                 S  s   g | ]}d |› d�‘qS )z_device_of(r¶   r   rÕ   r   r   r   rª   0  s     ú]c                 S  s   g | ]}d |› d�‘qS )z_pinned_memory_of(r¶   r   rÕ   r   r   r   rª   1  s     r¨   c                 S  s   g | ]}d |› d|› �‘qS )ú"z": r   rÕ   r   r   r   rª   <  s     c                 s  s.   | ]&\}}|t jkr|n|› d |› �V  qdS )z = N©rD   Ú_empty)r�   rc   Zdfltr   r   r   rÎ   =  s     z-JITFunction._make_launcher.<locals>.<genexpr>z
def rÄ   z·, grid=None, num_warps=4, num_stages=3, extern_libs=None, stream=None, warmup=False, device=None, device_type=None):
    from ..compiler import compile, CompiledKernel
    sig_key =  z,
    constexpr_key = r   r   z
    spec_key = aV  
    key = (version_key, sig_key, constexpr_key, spec_key, num_warps, num_stages, self.debug)
    if not extern_libs is None:
      key = (key, tuple(extern_libs.items()))
    assert num_warps > 0 and (num_warps & (num_warps - 1)) == 0, "num_warps must be a power of 2"
    assert grid is not None
    if callable(grid):
        grid = grid({zâ})
    grid_size = len(grid)
    grid_0 = grid[0]
    grid_1 = grid[1] if grid_size > 1 else 1
    grid_2 = grid[2] if grid_size > 2 else 1

    if device_type is None:
        device_types = [_device_type for _device_type in zW if _device_type != '']
        device_type = self._conclude_device_type(device_types, a³  )

    device_backend = None
    if device_type not in ['cuda', 'hip']:
        device_backend = get_backend(device_type)
        if device_backend is None:
            raise ValueError('Cannot find backend for ' + device_type)

    if device is None:
        if device_type in ['cuda', 'hip']:
            device = get_current_device()
            set_current_device(device)
        else:
            device = device_backend.get_current_device()
            device_backend.set_current_device(device)
    if stream is None and not warmup:
        if device_type in ['cuda', 'hip']:
            stream = get_cuda_stream(device)
        else:
            stream = device_backend.get_stream()

    bin = cache[device].get(key, None)
    if bin is not None:
      if not warmup:
          bin.c_wrapper(grid_0, grid_1, grid_2, bin.num_warps, bin.shared, stream, bin.cu_function, CompiledKernel.launch_enter_hook, CompiledKernel.launch_exit_hook, bin, zt)
      return bin
    # kernel not cached -- compile
    else:
      # build dict of constant values
      args = [z]
      all_args = c                 S  s   g | ]
}|› ‘qS r   r   rÕ   r   r   r   rª   q  s     aä  ,
      configs = self._get_config(*all_args),
      constants = self._make_constants(constexpr_key)
      constants.update({i: None for i, arg in enumerate(all_args) if arg is None})
      constants.update({i: 1 for i in configs[0].equal_to_1})
      # build kernel signature -- doesn't include specialized arguments
      signature = { i: self._type_of(_key_of(arg)) for i, arg in enumerate(all_args) if i not in self.constexprs }
      # build stub signature -- includes arguments that are specialized
      for i, arg in constants.items():
        if callable(arg):
          raise TypeError(f"Callable constexpr at index {i} is not supported")
      if not self._call_hook(key, signature, device, constants, num_warps, num_stages, extern_libs, configs):
        bin = compile(self, signature=signature, device=device, constants=constants, num_warps=num_warps, num_stages=num_stages, extern_libs=extern_libs, configs=configs, debug=self.debug, device_type=device_type)
        if not warmup:
            bin.c_wrapper(grid_0, grid_1, grid_2, bin.num_warps, bin.shared, stream, bin.cu_function, CompiledKernel.launch_enter_hook, CompiledKernel.launch_exit_hook, bin, *args)
        self.cache[device][key] = bin
        return bin
      return None
)rk   r   r-   r‹   r‚   r„   r†   ÚcacheÚ__spec__r   r   r   )r—   rÁ   r^   rŽ   rÇ   r®   Úarg_defaultsr¾   r6   rÒ   rk   r   r‹   r‚   r„   r†   rÛ   rÜ   r   r   r   Úexec)r-   Zregular_argsZconstexpr_argsr™   Zsig_keysrÉ   rÊ   Zconstexpr_keysZspecializationsr‘   r�   Z	spec_keysZ	grid_argsZargs_signaturer.   Úscoper   r”   r   Ú_make_launcher*  sj    



ÿÿýüûôíì,Ô,Ô2ÎEõ
zJITFunction._make_launcherc                   sZ  |ˆ_ |jˆ_|ˆ_t |¡}dd„ |j ¡ D ƒˆ_dd„ |j ¡ D ƒˆ_	t
dd„ ˆj	D ƒƒˆ_|d krlg n|ˆ_‡fdd„ˆjD ƒˆ_t t |¡¡ˆ_ˆjˆj d¡d … ˆ_ttƒˆ_d ˆ_g ˆ_d ˆ_tj d	d
¡dkrädn|ˆ_|ˆ_dd„ ‰ ‡ fdd„|j ¡ D ƒˆ_‡fdd„ˆj ¡ D ƒˆ_ˆ  ¡ ˆ_!|j"ˆ_"|j#ˆ_#|j$ˆ_$|jˆ_d S )Nc                 S  s   g | ]
}|j ‘qS r   )rc   ©r�   r¦   r   r   r   rª   ™  s     z(JITFunction.__init__.<locals>.<listcomp>c                 S  s   g | ]
}|j ‘qS r   )Údefaultrá   r   r   r   rª   š  s     c                 s  s   | ]}|t jkV  qd S r1   rÙ   rá   r   r   r   rÎ   ›  s     z'JITFunction.__init__.<locals>.<genexpr>c                   s&   h | ]}t |tƒrˆ j |¡n|’qS r   )r:   rM   rÁ   ÚindexrÕ   r”   r   r   r“   ž  s     z'JITFunction.__init__.<locals>.<setcomp>ÚdefZTRITON_DEBUGÚ0Ú1Tc                 S  s   t | tƒr| jS | S r1   )r:   r   r6   )r´   r   r   r   Ú<lambda>¬  ó    z&JITFunction.__init__.<locals>.<lambda>c                   s   i | ]\}}|ˆ |ƒ“qS r   r   r³   )Únormalize_tyr   r   Ú
<dictcomp>­  s      z(JITFunction.__init__.<locals>.<dictcomp>c                   s$   g | ]\}}d |krˆ j  |¡‘qS )Z	constexpr)rÁ   rã   r³   r”   r   r   rª   ¯  s      )%r¾   rF   r·   rÏ   rD   r«   Ú
parametersr¤   rÁ   rÝ   rÑ   Zhas_defaultsrŽ   ÚtextwrapÚdedentÚ	getsourcer.   Úfindr   r­   rÛ   rJ   Úkernel_decoratorsÚkernelr\   Úenvironr2   ÚdebugrB   rr   Úitemsr¯   rà   rm   rR   r6   rL   )r-   r¾   rÏ   rŽ   ró   rB   r«   r   )ré   r-   r   r&   “  s2    


zJITFunction.__init__c                 C  s<   | j d kr6t| j| jd�}| |  ¡ ¡ |jtƒ  | _ | j S )N)r,   r.   )rJ   r    rL   r.   r8   rK   r+   rk   )r-   Zdependencies_finderr   r   r   Ú	cache_key¸  s
    
zJITFunction.cache_keyc                 O  s   | j ttj|ƒ|ddi—ŽS )NÚwarmupT)rm   ÚmapÚ
MockTensorÚ
wrap_dtype©r-   r™   rÂ   r   r   r   rö   Á  s    zJITFunction.warmupc                 C  sH   t  | j¡}t|t jƒst‚t|jƒdks.t‚t|jd t jƒsDt‚|S )Nrˆ   r   )	r;   rK   r.   r:   ÚModulerI   rÒ   ÚbodyÚFunctionDef)r-   rN   r   r   r   rK   Ç  s
    zJITFunction.parsec                 O  s   t dƒ‚d S )Nz:Cannot call @triton.jit'd outside of the scope of a kernel)ÚRuntimeErrorrú   r   r   r   Ú__call__Î  s    zJITFunction.__call__c                   s2   |dkrd | _ tt| ƒ ||¡ |dkr.d | _d S )Nrð   r.   )rñ   r%   rH   Ú__setattr__rJ   )r-   rc   r9   r/   r   r   r   Ñ  s
    zJITFunction.__setattr__c                 C  s   d| j › d| jj› d�S )NzJITFunction(ú:r¶   )r·   r¾   r6   r”   r   r   r   Ú__repr__Ü  s    zJITFunction.__repr__)NNNN)r6   rF   rQ   rÀ   rŠ   Ústaticmethodr‚   r„   r†   r‹   rš   r§   r¬   r±   rÃ   rÇ   rÈ   rÓ   rà   r&   Úpropertyrõ   rö   rK   rÿ   r   r  rS   r   r   r/   r   rH   ‰   s8   




i
%
rH   úJITFunction[T]©r¾   r#   c                 C  s   d S r1   r   )r¾   r   r   r   Újitå  s    r  ©rÏ   rŽ   ró   rB   zOptional[Iterable[int]]zOptional[bool]zCallable[[T], JITFunction[T]])rŽ   ró   rB   r#   c                 C  s   d S r1   r   r  r   r   r   r  ê  s    )rÏ   rŽ   ró   rB   Ú	interpretzOptional[T]z4Union[JITFunction[T], Callable[[T], JITFunction[T]]])r¾   rŽ   ró   rB   r	  r#   c                  s4   dddœ‡ ‡‡‡‡fdd„}| dk	r,|| ƒS |S dS )a<  
    Decorator for JIT-compiling a function using the Triton compiler.

    :note: When a jit'd function is called, arguments are
        implicitly converted to pointers if they have a :code:`.data_ptr()` method
        and a `.dtype` attribute.

    :note: This function will be compiled and run on the GPU. It will only have access to:

           * python primitives,
           * builtins within the triton package,
           * arguments to this function,
           * other jit'd functions

    :param fn: the function to be jit-compiled
    :type fn: Callable
    r   r  r  c                   s:   t | ƒst‚ˆr$ddlm} || ƒS t| ˆˆˆ ˆd�S d S )Nr   )ÚGridSelectorr  )ÚcallablerI   Zinterpreter.interpreterr
  rH   )r¾   r
  ©ró   rŽ   r	  rB   rÏ   r   r   Ú	decorator  s    ûzjit.<locals>.decoratorNr   )r¾   rÏ   rŽ   ró   rB   r	  r  r   r  r   r  õ  s    c                   @  s0   e Zd ZdZedd„ ƒZdd„ Zedd„ ƒZdS )	rø   zr
    Can be used in place of real tensors when calling:
        kernel.warmup(MockTensor(torch.float32), ...)
    c                 C  s"   | j jdkr| jdkrt| ƒS | S )Nrt   r   )r0   r6   rF   rø   r€   r   r   r   rù   -  s
    ÿzMockTensor.wrap_dtypec                 C  s
   || _ d S r1   )rt   )r-   rt   r   r   r   r&   4  s    zMockTensor.__init__c                   C  s   dS r   r   r   r   r   r   r‡   7  s    zMockTensor.data_ptrN)r6   rF   rQ   rR   r  rù   r&   r‡   r   r   r   r   rø   (  s   
rø   c                   @  s2   e Zd Zdd„ Zdd„ Zdd„ Zddœd	d
„ZdS )ÚTensorWrapperc                 C  s*   || _ || _|j| _|j| _| jj| _d S r1   )rt   ÚbaseZis_cudarƒ   Úshape)r-   r  rt   r   r   r   r&   =  s
    zTensorWrapper.__init__c                 C  s
   | j  ¡ S r1   )r  r‡   r”   r   r   r   r‡   D  s    zTensorWrapper.data_ptrc                 C  s   | j  |¡S r1   )r  Ústride)r-   r‘   r   r   r   r  G  s    zTensorWrapper.striderM   r"   c                 C  s   d| j › d| j› d�S )NzTensorWrapper[rµ   r¶   )rt   r  r”   r   r   r   Ú__str__J  s    zTensorWrapper.__str__N)r6   rF   rQ   r&   r‡   r  r  r   r   r   r   r  <  s   r  c                 C  sV   t | tƒr*|| jjkr| jS t| j|ƒS n(t| dƒr>t| |ƒS tdt| ƒ› d�ƒ‚d S )Nr‡   zCannot reinterpret a r›   )r:   r  r  rt   rz   r~   r   )Ztensorrt   r   r   r   ÚreinterpretN  s    


r  )N)N)/Ú
__future__r   r   r;   ro   r'   rD   r\   re   rì   Úcollectionsr   r   Útypingr   r   r   r	   r
   r   r   r   r   Zcommon.backendr   r   r]   ÚdirnameÚabspathrZ   r_   rg   r   r   r   r   r   ÚNodeVisitorr    Ú	lru_cacherk   rl   rH   r  rø   r  r  r   r   r   r   Ú<module>   sV   ,
.
  ^û ÿù3