
    i                        S SK r S SKJr  S SKrS SKJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  \\S.rS	\S
\4S jr " S S\ R$                  5      r " S S\ R$                  5      r " S S\ R$                  5      r\" 5       rg)    NCallable)_BACKEND_OP_LIBBackendOpOverloadPacket)EdgeOpOverloadPacket)DispatchKey)Library)FunctionSchema)edgebackendlibraryschema_or_namec                 (   ^ ^ S[         4U U4S jjnU$ )a  Bind a pattern of ops to a backend op. A backend op should only appear when a user wants to replace a pattern of nodes to a custom op.
On this front, the kernel being registered to it determines the decomposing behavior.

*   If the backend op is registered with an CompositeExplicitAutograd (or Meta) kernel, once the graph is lowered (meaning the pass
    of replacing a pattern to an op is executed) it will stick in the graph and we won't get the original graph even retrace.
*   Otherwise, the backend op should be able to support retracing and be able to "promote" back to the original graph through retracing.

This macro is aiming to handle this complexity for users and they just need to use this macro on the pattern and we can make a decision for them.

Args:
    library (Library): torch library
    schema_or_name (str): schema string, e.g., "add.int(SymInt a, SymInt b) -> SymInt", or a qualified op name
fc                   >^ T	R                   [        ;  a   [        R                  " T	R                   5        T
R                  S5      S   n [        R
                  " U5      nUR                  R                  R                  UR                  R                  pCT	R                  U5        X4(       a  SU-   OS-   nU(       a  UOSn[        [        [        [        R                  T	R                   5      U5      U5      m[        R                  [        R                   [        R"                  /n[%        U4S jU 5       5      (       d  T	R'                  XPS5        [        [        [        [        R(                  T	R                   5      U5      U5      nXl        U $ ! [         a#    SU;   a  UR                  S5      u  p4 GNUS pC GNf = f)N::. defaultc              3   F   >#    U  H  nTR                  U5      v   M     g 7fN)has_kernel_for_dispatch_key).0ktorch_ops     \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/dialects/_ops.py	<genexpr>6bind_pattern_to_op.<locals>.wrapper.<locals>.<genexpr>A   s     IDq877::Ds   !CompositeImplicitAutograd)nsr   appendsplitr
   parsenamebaseoverload_namedefineAssertionErrorgetattrtorchopsr   CompositeExplicitAutogradr    Metaanyimplr   _equivalent_callable)r   no_namespacefuncr%   r'   opnamekeysopr   r   r   s           @r   wrapper#bind_pattern_to_op.<locals>.wrapper)   s^   ::_,""7::.%++D1"5		9!''5D"&)).."5"5tyy7N7N-NN<( },2F)6I77599gjj#A4H-X 1111

 IDIIILL$?@WWS[['**=tDmT"#'  	9l"&2&8&8&=#m&2Dm		9s   AF #G<GGr   )r   r   r7   s   `` r   bind_pattern_to_opr9      s    8  < N    c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_OpNamespaceJ   zb
EXIR Dialect op namespace object. Contains ops and overloads registered into PyTorch dispatcher.
c                    > [         TU ]  SU SU 35        Xl        US:X  a  U[        ;  a  [	        U S35      eX l        / U l        [        [        R                  U5      U l
        g )N	exir.ops.r   r   z+ op library does not belong to backend ops.)super__init___dialectr   RuntimeError_name_dirr*   r+   r,   _op_namespace)selfdialectr%   	__class__s      r   rA   _OpNamespace.__init__O   sc    9WIQtf56iD$?$'RSTT
	$UYY5r:   c                 ,    [        U R                  5      $ r   iterrE   rG   s    r   __iter___OpNamespace.__iter__X       DIIr:   c           
         US:X  a  gXR                   ;   a  [        X5      $  [        U R                  U5      nU R
                   SU 3n[        U R                     nU" UUUS9nU R                  S-   U R
                  -   Ul        [        XU5        U R                  R                  U5        U$ ! [         a/  n[        SU R                   SU R
                   SU S35      UeS nAff = f)	N__file__exir.opsz'_OpNamespace' 'r   z' object has no attribute ''r   )parent_overload_packet)__dict__r*   rF   AttributeErrorrB   rD   _OPOVERLOAD_PACKET_CLS_MAPPING
__module__setattrrE   r"   )rG   op_nameparent_packetequalified_op_nameopoverload_packet_clsopoverloadpackets          r   __getattr___OpNamespace.__getattr__[   s    j mm#4))	#D$6$6@M  $zzl"WI6 >t}} M0#0

 '+oo&;djj&H# 	/0		!%  	 !"4==/4::,>YZaYbbcd	s   B/ /
C(9*C##C()rB   rE   rD   rF   )
__name__rZ   __qualname____firstlineno____doc__rA   rO   rb   __static_attributes____classcell__rI   s   @r   r<   r<   J   s    6   r:   r<   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )_DialectNamespacez   a=  
Dialect namespace. Currently the dialects are:
- ATen Dialect: core ATen ops and overloads, see torch._ops._OpNamespace
- Edge Dialect: ATen ops with explicit Tensor dtype
- Backend Dialect: backend ops only meaningful to the backend we are lowering into
- Execution Dialect: memory planning ready, all out-variants
c                 D   > [         TU ]  SU-   5        Xl        / U l        g )Nr?   )r@   rA   _dialect_namerE   )rG   dialect_namerI   s     r   rA   _DialectNamespace.__init__   s#    )L89)	r:   c                     XR                   ;   a  [        X5      $ [        U R                  U5      n[	        XU5        U R
                  R                  U5        U$ r   )rW   r*   r<   ro   r[   rE   r"   )rG   r%   	namespaces      r   rb   _DialectNamespace.__getattr__   sK    == 4&& !3!3T:	I&		r:   )ro   rE   )	rd   rZ   re   rf   rg   rA   rb   rh   ri   rj   s   @r   rl   rl   z   s    
 r:   rl   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_Ops   z_ops.pyc                 2   > [         TU ]  S5        / U l        g )NrT   )r@   rA   rE   )rG   rI   s    r   rA   _Ops.__init__   s    $	r:   c                     XR                   ;   a  [        X5      $ [        U5      n[        XU5        U R                  R                  U5        U$ r   )rW   r*   rl   r[   rE   r"   )rG   r%   rH   s      r   rb   _Ops.__getattr__   sB    == 4&&#D)G$		r:   c                 ,    [        U R                  5      $ r   rL   rN   s    r   rO   _Ops.__iter__   rQ   r:   )rE   )
rd   rZ   re   rf   rS   rA   rb   rO   rh   ri   rj   s   @r   rv   rv      s    H r:   rv   )typestypingr   r+   %executorch.exir.dialects.backend._opsr   r   "executorch.exir.dialects.edge._opsr   torch._Cr   torch.libraryr	   torchgen.modelr
   rY   strr9   
ModuleTyper<   rl   rv   r,    r:   r   <module>r      s       D   ! ) !&" - - -`- 5## - `(( 05 ( fr:   