Ë
    øÿæiß\  ã                   óê  — U 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mZ d dl	m
Z
mZmZ d dlmZ ddlmZmZ ddlmZ  ej&                  e«      Zi Zeeee   f   ed<   d	ed
ee   fd„Zdede
f   d
ee   fd„Z ed«      	 dddœd	edee   deeee   f   dee   d
ef
d„«       Z ej>                  «       Z dZ!ejD                  de#d
ed   fd„«       Z$d
e#fd„Z%ded
e
fd„Z& ed«      ded
e
fd„«       Z'y)é    N)ÚCallableÚ	GeneratorÚIterable)ÚAnyÚOptionalÚUnion)Ú
exposed_iné   )Ú	custom_opÚCustomOpDef)Úinfer_schemaÚtriton_ops_to_kernelsÚnameÚreturnc                 ó.   — t         j                  | g «      S ©N)r   Úget)r   s    új/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torch/_library/triton.pyÚget_triton_kernels_for_opr      s   € Ü ×$Ñ$ T¨2Ó.Ð.ó    Úfn.c           
      ó’   ‡‡— dŠ	 	 d	dt         dt        f   dt        t           dz  dt        dt        t
           fˆˆfd„Š ‰| «      S )
a{  
    Inspect the source of an arbitrary callable passed to torch._library.triton_op,
    and grab all of the triton kernels that are wrapped inside of it.

    This function traces local variable assignments to handle patterns like:
        kernel_fn = _my_kernel  # global JITFunction
        wrapped = some_wrapper(kernel_fn)
        capture_triton(wrapped)[grid](...)

    It also recursively analyzes called functions to find triton kernels hidden
    behind helper function calls.

    That said, it is best effort. There are cases (e.g., recursion > MAX_RECURSION_DEPTH)
    that are not accounted for, so keep that in mind.
    é   Nr   .Úvisited_fnsÚdepthr   c                 óâ  •‡‡‡‡‡‡‡— 	 ddl mŠ ddlmŠ t        j                  | «      } ‰€
t        «       Št        | «      }|‰v rg S ‰‰kD  rt
        j                  d‰«       g S ‰j                  |«       	 t        j                  | «      }ddlm}  |«       }|j%                  |d¬«       t'        j(                  |j+                  «       «      } G d	„ d
t&        j,                  «      } |«       }	|	j/                  |«       dt&        j0                  dt2        t4           fd„Šdt6        dt6        d z  fˆˆfd„Šdt6        dt8        t4        t:        f   fd„}
 |
| «      }	 	 ddt2        t4           dt8        t4        t:        f   dt8        t4        t2        t&        j0                     f   d z  dt        t4           d z  dt2        t6           f
ˆˆˆ ˆˆˆfd„Šg }t        «       }t3        |	j<                  «      }|	j>                  D ]  }|jA                   ‰|«      «       Œ |D ]P  } ‰|g||	jB                  «      }|D ]4  }t        |«      }||vsŒ|j                  |«       |jE                  |«       Œ6 ŒR |	jF                  D ]�  }|jI                  |«      }|€ddl%m&} ||v r||   jN                  }|�tQ        |«      sŒ=tS        |d«      sŒJ	  ‰ |‰‰dz   «      }|D ]4  }t        |«      }||vsŒ|j                  |«       |jE                  |«       Œ6 Œ’ |S # t        $ r t
        j                  d«       g cY S w xY w# t        t        f$ r g cY S w xY w# tT        $ r t
        j                  d|d¬«       Y Œõw xY w)Nr   ©Ú	Autotuner©ÚJITFunctionz.Triton not available, find_triton_kernels = []z7reached max recursion depth (%s) in find_triton_kernels)ÚIndentedBufferT)Ústripc                   ó€   — e Zd Zdd„Zdej
                  ddfd„Zdej                  ddfd„Zdej                  ddfd„Z
y)	úFget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.Visitorr   Nc                 ó<   — g | _         i | _        g | _        g | _        y r   )Útriton_kernelsÚassignmentsÚcalled_functionsÚreturn_exprs)Úselfs    r   Ú__init__zOget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.Visitor.__init__X   s"   € Ø13�Ô#à>@�Ô à35�Ô%à46�Õ!r   Únodec                 óü   — |j                   D ]\  }t        |t        j                  «      sŒ| j                  j                  |j                  g «      j                  |j                  «       Œ^ | j                  |«       y r   )
ÚtargetsÚ
isinstanceÚastÚNamer'   Ú
setdefaultÚidÚappendÚvalueÚgeneric_visit)r*   r,   Útargets      r   Úvisit_AssignzSget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.Visitor.visit_Assigna   sW   € Ø"Ÿlœl�FÜ! &¬#¯(©(Õ3Ø×(Ñ(×3Ñ3°F·I±I¸rÓB×IÑIÈ$Ï*É*ÕUð +ð ×"Ñ" 4Õ(r   c                 óˆ   — |j                   �%| j                  j                  |j                   «       | j                  |«       y r   )r5   r)   r4   r6   ©r*   r,   s     r   Úvisit_ReturnzSget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.Visitor.visit_Returng   s2   € Ø—:‘:Ð)Ø×%Ñ%×,Ñ,¨T¯Z©ZÔ8Ø×"Ñ" 4Õ(r   c                 ó,  — d}t        |j                  t        j                  «      �r|j                  }t        |j                  t        j                  «      �r«t        |j                  j                  t        j
                  «      r³|j                  j                  j                  dk(  r�|j                  j                  dk(  rw|j                  |v ri|j                  �r&t        |j                  d   t        j
                  «      �rþ| j                  j                  |j                  d   j                  «       �nÊt        |j                  j                  t        j                  «      �r›t        |j                  j                  j                  t        j
                  «      �rb|j                  j                  j                  j                  dk(  �r4|j                  j                  j                  dk(  �r| j                  j                  |j                  j                  › d|j                  › �«       nÑt        |j                  t        j
                  «      r­|j                  j                  |v rf|j                  r‰t        |j                  d   t        j
                  «      rb| j                  j                  |j                  d   j                  «       n/| j                  j                  |j                  j                  «       | j                  |«       y )N)Úcapture_tritonÚwrap_tritonÚtorchÚ_libraryr   Úopsz::)r/   Úfuncr0   Ú	Attributer5   r1   r3   ÚattrÚargsr&   r4   r(   r6   )r*   r,   Útriton_func_namesrD   s       r   Ú
visit_CallzQget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.Visitor.visit_Calll   sæ  € Ø$EÐ!Ü˜dŸi™i¬¯©Õ7ØŸ9™9�DÜ! $§*¡*¬c¯m©mÕ<ä& t§z¡z×'7Ñ'7¼¿¹ÔBØ $§
¡
× 0Ñ 0× 3Ñ 3°wÒ >Ø $§
¡
§¡°:Ò =Ø $§	¡	Ð->Ñ >à#Ÿy›y¬Z¸¿	¹	À!¹ÄcÇhÁhÕ-OØ $× 3Ñ 3× :Ñ :¸4¿9¹9ÀQ¹<¿?¹?Ö Kä& t§z¡z×'7Ñ'7¼¿¹ÕGÜ *¨4¯:©:×+;Ñ+;×+AÑ+AÄ3Ç8Á8Õ LØ $§
¡
× 0Ñ 0× 6Ñ 6× 9Ñ 9¸WÓ DØ $§
¡
× 0Ñ 0× 5Ñ 5¸Ó >à ×1Ñ1×8Ñ8Ø#'§:¡:§?¡?Ð"3°2°d·i±i°[Ð Aõô
   §	¡	¬3¯8©8Ô4Ø—y‘y—|‘|Ð'8Ñ8ØŸ9š9¬°D·I±I¸a±LÄ#Ç(Á(Ô)KØ ×/Ñ/×6Ñ6°t·y±yÀ±|·±ÕGð ×-Ñ-×4Ñ4°T·Y±Y·\±\ÔBà×"Ñ" 4Õ(r   )r   N)Ú__name__Ú
__module__Ú__qualname__r+   r0   ÚAssignr8   ÚReturnr;   ÚCallrG   © r   r   ÚVisitorr$   W   sL   „ ó7ð)¨¯©ð )¸ó )ð)¨¯©ð )¸ó )ð
 ) s§x¡xð  )°Dô  )r   rO   Úexprr   c                 ón   ‡— g Š G ˆfd„dt         j                  «      } |«       j                  | «       ‰S )z3Extract all Name references from an AST expression.c                   óZ   •— e Zd Zdej                  ddfˆ fd„Zdej                  ddfd„Zy)úmget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.extract_names_from_expr.<locals>.NameExtractorr,   r   Nc                 ó<   •— ‰j                  |j                  «       y r   )r4   r3   )r*   r,   Únamess     €r   Ú
visit_Namezxget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.extract_names_from_expr.<locals>.NameExtractor.visit_Name–   s   ø€ Ø—L‘L §¡Õ)r   c                 ó&   — | j                  |«       y r   )r6   r:   s     r   rG   zxget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.extract_names_from_expr.<locals>.NameExtractor.visit_Call™   s   € à×&Ñ& tÕ,r   )rH   rI   rJ   r0   r1   rV   rM   rG   )rU   s   €r   ÚNameExtractorrS   •   s1   ø„ ð*¨3¯8©8ð *¸õ *ð-¨3¯8©8ð -¸ô -r   rX   )r0   ÚNodeVisitorÚvisit)rP   rX   rU   s     @r   Úextract_names_from_exprzVget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.extract_names_from_expr‘   s/   ø€ à!ˆEö-¤§¡ô -ñ ‹O×!Ñ! $Ô'ØˆLr   Úobjc                 óˆ   •— t        | ‰‰f«      r| S t        | «      r&t        | d«      r| j                  }t        |‰«      r|S y)zACheck if obj is a triton kernel or wrapper and return the kernel.r   N)r/   ÚcallableÚhasattrr   )r\   Úinnerr   r    s     €€r   Úresolve_to_kernelzPget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.resolve_to_kernel    sB   ø€ ä˜# ¨YÐ7Ô8Ø�
ä˜Œ}¤¨¨dÔ!3ØŸ™�Ü˜e [Ô1Ø �LØr   Úfunc_objc                 ó¶  — t        | «      r	 t        j                  | «      } t        | «      rt	        | d«      si S t        j
                  | «      }i }|j                  |j                  «       |j                  |j                  «       |j                  |j                  «       t	        | d«      r|j                  | j                  «       |S # t        $ r Y Œµw xY w)zBBuild a combined namespace from a function's globals and closures.Ú__code__Ú__globals__)r^   ÚinspectÚunwrapÚ
ValueErrorr_   ÚgetclosurevarsÚupdateÚbuiltinsÚglobalsÚ	nonlocalsre   )rb   Úfunc_closure_varsÚ	namespaces      r   Úbuild_namespacezNget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.build_namespace«   sÄ   € ô ˜Ô!ðÜ&Ÿ~™~¨hÓ7�Hô ˜HÔ%¬W°X¸zÔ-JØ�	Ü '× 6Ñ 6°xÓ @ÐØ(*ˆIØ×ÑÐ.×7Ñ7Ô8Ø×ÑÐ.×6Ñ6Ô7Ø×ÑÐ.×8Ñ8Ô9Ü�x Ô/Ø× Ñ  ×!5Ñ!5Ô6ØÐøô "ò Ùðús   �C Ã	CÃCrU   ro   r'   Úvisitedc                 ó*  •— |€
t        «       }g }| D ]í  }||v rŒ|j                  |«       ||v r†||   } ‰|«      }|�|j                  |«       Œ>t        |«      rC	 t	        j
                  |«      }t        |d«      r! ‰|‰‰dz   «      }	|	r|j                  |	«       ŒŒt        j                  d|«       Œ£|�3||v r/||   D ]&  }
 ‰|
«      } ‰||||«      }|j                  |«       Œ( ŒØt        j                  d|«       Œï |S # t        $ r |}Y Œ w xY w)z^
            Resolve a list of names to triton kernels using the given namespace.
            rd   r
   z'failed to resolve %s to a triton kernelz(%s not found in namespace or assignments)ÚsetÚaddr4   r^   rf   rg   rh   r_   ÚextendÚloggerÚdebug)rU   ro   r'   rq   Úresultsr   r\   ÚkernelÚ	unwrappedÚnestedÚrhs_exprÚ
referencedÚtracedr   r[   Úfind_triton_kernelsÚresolve_names_to_kernelsra   r   s                €€€€€€r   r€   zWget_inner_triton_kernels.<locals>.find_triton_kernels.<locals>.resolve_names_to_kernelsÀ   s=  ø€ ð ˆÜ›%�à$&ˆGÛ�Ø˜7‘?ØØ—‘˜DÔ!à˜9Ñ$Ø# D™/�CÙ.¨sÓ3�FØÐ)ØŸ™ vÔ.Ø ô   ”}ð,Ü(/¯©°sÓ(;˜Iô # 9¨jÔ9Ù%8Ø )¨;¸À¹	ó&˜Fñ  &Ø '§¡¨vÔ 6Ø (Ü—L‘LÐ!JÈDÕQØ Ð,°¸Ñ1Dà$/°Ô$5˜Ù%<¸XÓ%F˜
Ù!9Ø&¨	°;Àó"˜ð  Ÿ™ vÕ.ñ %6ô —L‘LÐ!KÈTÕRðG ðJ ˆNøô+  *ò ,Ø(+šIð,ús   ÁDÄDÄD)ÚOPDEFSrd   r
   z$failed to analyze called function %s)Úexc_info)NN)+Útriton.runtime.autotunerr   Útriton.runtime.jitr    ÚImportErrorrv   Úwarningrf   rg   rs   r3   rw   rt   Ú	getsourceÚOSErrorÚ	TypeErrorÚtorch._inductor.utilsr!   Úsplicer0   ÚparseÚgetrawvaluerY   rZ   rP   ÚlistÚstrÚobjectÚdictr   r&   r)   ru   r'   r4   r(   r   Útorch._library.custom_opsr�   Ú_abstract_fnr^   r_   Ú	Exception)!r   r   r   Úfn_idÚsourcer!   ÚbufferÚtreerO   Ú	collectorrp   Ú	all_namesÚresolvedÚseen_idsÚnames_to_resolverP   r   Útraced_objectsr\   Úobj_idÚ	func_namerb   r�   Únested_kernelsry   Ú	kernel_idr   r    r[   r€   ra   ÚMAX_RECURSION_DEPTHr   s!    ``                       @@@@@€€r   r   z5get_inner_triton_kernels.<locals>.find_triton_kernels,   sc  ÿ€ ð
	Ý:Ý6ô �^‰^˜BÓˆð ÐÜ›%ˆKä�2“ˆØ�KÑØˆIØÐ&Ò&Ü�L‰LØIØ#ôð ˆIà�‰˜Ôð	Ü×&Ñ& rÓ*ˆFõ 	9áÓ!ˆØ�‰�f DˆÔ)Ü�y‰y˜×+Ñ+Ó-Ó.ˆô5	)”c—o‘oô 5	)ñn “Iˆ	Ø�‰˜Ôð	¬#¯(©(ð 	´t¼C±yó 	ð		¤6ð 		¬f°t©mö 		ð	¤fð 	´´c¼3°h±ó 	ñ& $ BÓ'ˆ	ð
 =AØ'+ñ	2	Üœ‘9ð2	äœC¤˜H‘~ð2	ô œc¤4¬¯©¡>Ð1Ñ2°TÑ9ð2	ô œ‘X ‘_ð	2	ô
 ”&‰\÷2	ò 2	ðj "$ˆÜ ›Uˆä&*¨9×+CÑ+CÓ&DÐØ×*Ô*ˆDØ×#Ñ#Ñ$;¸DÓ$AÕBð +ó %ˆDÙ5Ø�˜	 9×#8Ñ#8óˆNó &�Ü˜C›�Ø Ò)Ø—L‘L Ô(Ø—O‘O CÕ(ñ	 &ð	 %ð #×3Ô3ˆIØ —}‘} YÓ/ˆHàÐÝ<à Ñ&Ø% iÑ0×=Ñ=�Hð Ð¤x°Ô'9Øô ˜8 ZÔ0Øð
Ù!4°X¸{ÈEÐTUÉIÓ!V�Û,�FÜ " 6£
�IØ ¨Ò0Ø Ÿ™ YÔ/Ø Ÿ™¨Õ/ñ	 -ð' 4ð: ˆøô_ ò 	Ü�N‰NÐKÔLØŠIð	ûô2 œÐ#ò 	ØŠIð	ûôb ò Ü—‘Ø:¸IÐPTð ö ðús;   ŠL Á7L3 Ë!M
Ë%$M
Ì L0Ì/L0Ì3MÍMÍ
!M.Í-M.)Nr   )r   r   rs   ÚintrŽ   r�   )r   r£   r   s    @@r   Úget_inner_triton_kernelsr¥      sd   ù€ ð$ Ðð (,ØñwÜ�Sœ#�XÑðwäœ‘X ‘_ðwô ðwô 
Œf‰ö	wñr ˜rÓ"Ð"r   ztorch.library)ÚschemaÚmutates_argsr¦   c               óV   ‡ ‡— dt         dt        f   dt        fˆˆ fd„}|€|S  ||«      S )aÉ  Create a custom operator whose implementation is backed by 1+ triton kernels.

    This is a more structured way of using triton kernels with PyTorch.
    Prefer using triton kernels with no ``torch.library`` custom operator wrappers
    (like :func:`torch.library.custom_op`, :func:`torch.library.triton_op`) because
    that is simpler;
    only use :func:`torch.library.custom_op`/:func:`torch.library.triton_op` if you
    want to create an operator that behaves like PyTorch built-in operators.
    For example, you may use a ``torch.library`` wrapper API to define the
    behavior of the triton kernel when passed a tensor subclass or under
    a TorchDispatchMode.

    Use :func:`torch.library.triton_op` instead of :func:`torch.library.custom_op`
    when the implementation
    consists of 1+ triton kernels. :func:`torch.library.custom_op` treats
    custom operators as opaque (:func:`torch.compile` and
    :func:`torch.export.export` will never trace into them), but ``triton_op``
    makes the implementation visible to these subsystems, allowing them
    to optimize the triton kernel(s).

    Note that ``fn`` must only consist of calls to PyTorch-understood
    operators and triton kernels. Any triton kernels called inside ``fn``
    must be wrapped in a call to :func:`torch.library.wrap_triton`.

    Args:
        name (str): A name for the custom op that looks like "{namespace}::{name}",
            e.g. "mylib::my_linear". The name is used as the op's stable identifier
            in PyTorch subsystems (e.g. torch.export, FX graphs).
            To avoid name collisions, please use your project name as the namespace;
            e.g. all custom ops in pytorch/fbgemm use "fbgemm" as the namespace.
        mutates_args (Iterable[str] or "unknown"): The names of args that the function mutates.
            This MUST be accurate, otherwise, the behavior is undefined. If "unknown",
            it pessimistically assumes that all inputs to the operator are being mutated.
        schema (str | None): A schema string for the operator. If None
            (recommended) we'll infer a schema for the operator from its type
            annotations. We recommend letting us infer a schema unless you
            have a specific reason not to.
            Example: "(Tensor x, int y) -> (Tensor, Tensor)".

    Example::

        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
        >>> import torch
        >>> from torch.library import triton_op, wrap_triton
        >>>
        >>> import triton
        >>> from triton import language as tl
        >>>
        >>> @triton.jit
        >>> def add_kernel(
        >>>     in_ptr0,
        >>>     in_ptr1,
        >>>     out_ptr,
        >>>     n_elements,
        >>>     BLOCK_SIZE: "tl.constexpr",
        >>> ):
        >>>     pid = tl.program_id(axis=0)
        >>>     block_start = pid * BLOCK_SIZE
        >>>     offsets = block_start + tl.arange(0, BLOCK_SIZE)
        >>>     mask = offsets < n_elements
        >>>     x = tl.load(in_ptr0 + offsets, mask=mask)
        >>>     y = tl.load(in_ptr1 + offsets, mask=mask)
        >>>     output = x + y
        >>>     tl.store(out_ptr + offsets, output, mask=mask)
        >>>
        >>> @triton_op("mylib::add", mutates_args={})
        >>> def add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        >>>     output = torch.empty_like(x)
        >>>     n_elements = output.numel()
        >>>
        >>>     def grid(meta):
        >>>         return (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
        >>>
        >>>     # NB: we need to wrap the triton kernel in a call to wrap_triton
        >>>     wrap_triton(add_kernel)[grid](x, y, output, n_elements, 16)
        >>>     return output
        >>>
        >>> @torch.compile
        >>> def f(x, y):
        >>>     return add(x, y)
        >>>
        >>> x = torch.randn(3, device="cuda")
        >>> y = torch.randn(3, device="cuda")
        >>>
        >>> z = f(x, y)
        >>> assert torch.allclose(z, x + y)

    r   .r   c           	      óÌ   •‡ — ˆ fd„}t        ‰|‰t        ‰ ‰¬«      ¬«      }ddlm} |j	                  ‰ «       ˆ fd„}t        ‰ «      }|t        ‰<   |j                  ||«       |S )Nc                  óV   •— t        d«      5   ‰| i |¤Žcd d d «       S # 1 sw Y   y xY w)NF)Úset_wrap_triton_enabled)rE   Úkwargsr   s     €r   Ú
backend_fnz*triton_op.<locals>.dec.<locals>.backend_fn‹  s&   ø€ ô )¨Õ/Ù˜4Ð* 6Ñ*÷ 0×/Ò/ús   �Ÿ()r§   )r§   r¦   é   )ÚFunctionalTensorModec                 ób  •— ddl m}  |«       r| j                  ||||«      S dd l}|D �cg c]R  }t	        ||j
                  j                  «      s0||j                  |j
                  j                  j                  fvr|‘ŒT }}|rt        S | 5   ‰	|i |¤Žcd d d «       S c c}w # 1 sw Y   y xY w)Nr   )Ú(custom_triton_ops_decomposition_disabled)Útorch.export._tracer±   Ú__torch_dispatch__Útorch._subclassesÚ
issubclassÚ_subclassesÚ
FakeTensorÚTensorÚfunctional_tensorÚFunctionalTensorÚNotImplemented)
ÚmodeÚopÚtypesrE   r¬   r±   r?   ÚtÚunrecognized_typesr   s
            €r   Úfunctional_decompz1triton_op.<locals>.dec.<locals>.functional_decomp¢  s»   ø€ õ& Uá7Ô9Ø×.Ñ.¨r°5¸$ÀÓGÐGó )ñ #ó	&á"˜Ü% a¨×):Ñ):×)EÑ)EÔFØàŸ™Ø×)Ñ)×;Ñ;×LÑLðñò Ø"ð #ð 	&ñ &Ü)Ð)ÚÙ˜tÐ. vÑ.÷ ‘Tùò	&÷ �Tús   «AB ÂB%Â%B.)r   r   Ú_subclasses.functional_tensorr¯   Úregister_faker¥   r   Úregister_torch_dispatch)r   r­   Úresultr¯   rÁ   r&   r§   r   s   `     €€r   Údecztriton_op.<locals>.decŠ  sq   ù€ ô	+ô ØØØ%Ü °Ô>ô	
ˆõ 	Ið 	×Ñ˜RÔ ô*	/ôX 2°"Ó5ˆØ&4Ô˜dÑ#Ø×&Ñ&Ð';Ð=NÔOØˆr   )r   r�   r   )r   r   r§   r¦   rÆ   s   ` `  r   Ú	triton_oprÇ   (  s9   ù€ ðDG”˜œf˜Ñ%ð G¬+ö GðR 
€zØˆ
á�2‹wˆr   TÚenabled)NNNc              #   óv   K  — 	 t        «       }| t        _        d–— |t        _        y# t        _        w xY w­w)aO  If triton kernels annotated with @wrap_triton should dispatch via HOP
    or go straight to the triton kernel execution.

    We have this switch because eager-mode performance of HOP dispatch is slow
    enough to matter (~1ms) and we know that wrap_triton isn't necessary in
    some situations (eager-mode with regular Tensors)
    N)Úis_wrap_triton_enabledÚwrap_triton_enabledr5   )rÈ   Úprevs     r   r«   r«   Ý  s1   è ø€ ð)Ü%Ó'ˆØ$+ÔÔ!Ûà$(ÔÕ!ø DÔÕ!üs   ‚9„) �9©6¶9c                  ó,   — t        t        dt        «      S )Nr5   )ÚgetattrrË   Úwrap_triton_enabled_defaultrN   r   r   rÊ   rÊ   î  s   € ÜÔ&¨Ô1LÓMÐMr   Útriton_kernelc                ó   — t        | «      S )z(This API has been renamed to wrap_triton)r>   )rÐ   s    r   r=   r=   ò  s   € ä�}Ó%Ð%r   c                ó„   — ddl m} ddlm} ddlm} t        | ||f«      st        d«      ‚t        «       s| S  || dd«      S )aU	  Allows capture of a triton kernel into a graph via make_fx or
    non-strict ``torch.export``.

    These technologies perform Dispatcher-based tracing (via
    ``__torch_dispatch__``) and cannot see calls to raw triton kernels.
    The ``wrap_triton`` API wraps a triton kernel into a callable that
    can actually be traced into a graph.

    Please use this API together with :func:`torch.library.triton_op`.

    Examples:

        >>> # xdoctest: +SKIP
        >>> import torch
        >>> import triton
        >>> from triton import language as tl
        >>> from torch.fx.experimental.proxy_tensor import make_fx
        >>> from torch.library import wrap_triton
        >>>
        >>> @triton.jit
        >>> def add_kernel(
        >>>     in_ptr0,
        >>>     in_ptr1,
        >>>     out_ptr,
        >>>     n_elements,
        >>>     BLOCK_SIZE: "tl.constexpr",
        >>> ):
        >>>     pid = tl.program_id(axis=0)
        >>>     block_start = pid * BLOCK_SIZE
        >>>     offsets = block_start + tl.arange(0, BLOCK_SIZE)
        >>>     mask = offsets < n_elements
        >>>     x = tl.load(in_ptr0 + offsets, mask=mask)
        >>>     y = tl.load(in_ptr1 + offsets, mask=mask)
        >>>     output = x + y
        >>>     tl.store(out_ptr + offsets, output, mask=mask)
        >>>
        >>> def add(x, y):
        >>>     output = torch.empty_like(x)
        >>>     n_elements = output.numel()
        >>>
        >>>     def grid_fn(meta):
        >>>         return (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
        >>>
        >>>     wrap_triton(add_kernel)[grid_fn](x, y, output, n_elements, 16)
        >>>     return output
        >>>
        >>> x = torch.randn(3, device="cuda")
        >>> y = torch.randn(3, device="cuda")
        >>> gm = make_fx(add)(x, y)
        >>> print(gm.code)
        >>> # def forward(self, x_1, y_1):
        >>> #     empty_like = torch.ops.aten.empty_like.default(x_1, pin_memory = False)
        >>> #     triton_kernel_wrapper_mutation_proxy = triton_kernel_wrapper_mutation(
        >>> #         kernel_idx = 0, constant_args_idx = 0,
        >>> #         grid = [(1, 1, 1)], kwargs = {
        >>> #             'in_ptr0': x_1, 'in_ptr1': y_1, 'out_ptr': empty_like,
        >>> #             'n_elements': 3, 'BLOCK_SIZE': 16
        >>> #         })
        >>> #     return empty_like

    r   r   r   )ÚTraceableTritonKernelWrapperzPwrap_triton only works on functions annotated with triton.jit or triton.autotuneN)	rƒ   r   r„   r    Ú*torch._higher_order_ops.triton_kernel_wraprÓ   r/   ÚRuntimeErrorrÊ   )rÐ   r   r    rÓ   s       r   r>   r>   ÷  sJ   € õ~ 3Ý.åWä�m k°9Ð%=Ô>ÜØ^ó
ð 	
ô "Ô#ØÐÙ'¨°t¸TÓBÐBr   r   )(r0   Ú
contextlibrf   ÚloggingÚ	threadingÚcollections.abcr   r   r   Útypingr   r   r   Útorch.utils._exposed_inr	   Ú
custom_opsr   r   r   Ú	getLoggerrH   rv   r   r‘   r�   rŽ   r�   Ú__annotations__r   r¥   rÇ   ÚlocalrË   rÏ   ÚcontextmanagerÚboolr«   rÊ   r=   r>   rN   r   r   Ú<module>râ      sœ  ðÜ 
Û Û Û Û ß 9Ñ 9ß 'Ñ 'å .ç .Ý &ð 
ˆ×	Ñ	˜8Ó	$€à13Ð �t˜C  f¡Ð-Ñ.Ó 3ð/ Cð /¨D°©Ló /ðM# ¨#¨s¨(Ñ!3ð M#¸¸V¹ó M#ñ` ˆOÓð "ðmð !òmØ
ðmà�Ñðmð
 ˜˜X c™]Ð*Ñ+ðmð �S‰Mðmð òmó ðmð` &�i—o‘oÓ'Ð Ø"Ð ð ×Ñð) Tð )¨iÐ8HÑ.Iò )ó ð)ð N ó Nð& (ð &°#ó &ñ
 ˆOÓðIC˜xð IC¨sò ICó ñICr   