Ë
    ÄÓ^jj—  ã                   ó  — U d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	 d dl
Zg d¢Z e j                  e«      Z ed«      Z e	d«      Zeeef   Zeeef   Ze G d„ d	«      «       Zeeeee   gee   f   Ze G d
„ d«      «       Z e«       Zeed<   i aeeeef   ej>                  j@                  f   ed<   i Z!ee"ef   ed<   eeeef   ee   f   Z#i a$e#ed<   eeeeeef      f   Z%i a&e%ed<   i a'e%ed<   i a(e%ed<   d a)e*ed<    e+h d£«      Z,e+e   ed<   deee   z  dz  deee   z  dz  deee   z  dz  de-eeef      fd„Z.ddœdeddfd„Z/i Z0eeej>                  j@                  f   ed<    e-«       Z1e-e   ed <   i Z2eeeef   ej>                  j@                  f   ed!<   d"edej>                  j@                  fd#„Z3d$edej>                  j@                  fd%„Z4d&ed$ed'eddfd(„Z5d&ed$eddfd)„Z6d&edd*fd+„Z7d&edefd,„Z8d-ed&eddfd.„Z9d/ej>                  j@                  d0ed$eddfd1„Z:d2eee   z  dz  dee   fd3„Z;dddd4œd5eee   z  dz  d6eee   z  dz  d7eee   z  dz  ddfd8„Z<dddd9œd:eee   z  dz  d;eee   z  dz  d<eee   z  dz  ddfd=„Z=d>edee   fd?„Z>d>ed&ed$ed@eeef   ddf
dA„Z?dBe"dCe"defdD„Z@dddEœdFedGed&ed$edHedz  dIedJedKeddfdL„ZAddMœdNee   dOee   dPedefdQ„ZBddRœd&ed$edSee   dTedz  ddf
dU„ZCddddVœdWedXe-eeef      dz  dYedTedz  ddf
dZ„ZD	 dgd[edee"ef   fd\„ZEddRœd&ed$edSee   dTedz  ddf
d]„ZFdhd^„ZGdd_œd`edYeddfda„ZHdd_œdbeeeegef   dYeddfdc„ZIdd_œddeeegef   deedYeddfdf„ZJy)ié    N)ÚCallableÚIterable)Ú	dataclassÚfield)Ú	ParamSpecÚTypeVar)ÚUserOrderingFnÚregister_op_overrideÚ!reorder_graphs_from_user_functionÚreenable_op_overridesÚderegister_op_overridesÚget_dsl_operationsÚnative_decomp_tableÚPÚRc                   ój   — e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   dZe	ed	<   d
Z
e	ed<   y)Ú_OverrideNodezTrack function override data.Údsl_nameÚ	op_symbolÚdispatch_keyÚcond_fnÚimpl_fnÚnode_idFÚunconditional_overrideTÚactiveN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Ú	_OpCondFnÚ	_OpImplFnr   Úboolr   © ó    úa/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_native/registry.pyr   r      s=   … á'àƒMØƒNØÓØÓØÓð
 ƒLØ#(Ð˜DÓ(Ø€FˆDÔr&   r   c                   ó  — e Zd ZU dZ ee¬«      Zee   ed<    ee¬«      Z	ee   ed<    ee¬«      Z
ee   ed<   dedefd„Z	 dd	eee   z  d
z  deee   z  d
z  deee   z  d
z  dedd
f
d„Zdeeeef      fd„Zdefd„Zy
)Ú_FilterStatez+Manages filtering state for override nodes.)Údefault_factoryÚ
_dsl_namesÚ_op_symbolsÚ_dispatch_keysÚnodeÚreturnc                 óš   — |j                   | j                  v ry|j                  | j                  v ry|j                  | j
                  v ryy)zå
        Check if a node is enabled based on current filter state.

        Args:
            node: The override node to check

        Returns:
            bool: True if the node should be enabled, False if filtered out
        FT)r   r+   r   r,   r   r-   )Úselfr.   s     r'   Úcheck_enabledz_FilterState.check_enabled9   sF   € ð �=‰=˜DŸO™OÑ+Øà�>‰>˜T×-Ñ-Ñ-Øà×Ñ × 3Ñ 3Ñ3Øàr&   Ú	dsl_namesNÚ
op_symbolsÚdispatch_keysÚremove_keysc                 óÞ  — |rv| xj                   t        t        |«      «      z  c_         | xj                  t        t        |«      «      z  c_        | xj                  t        t        |«      «      z  c_        y| xj                   t        t        |«      «      z  c_         | xj                  t        t        |«      «      z  c_        | xj                  t        t        |«      «      z  c_        y)aó  
        Update filter sets as (current | new) or (current ~ new).

        Args:
            dsl_names: DSL names to add/remove from filter
            op_symbols: Operation symbols to add/remove from filter
            dispatch_keys: Dispatch keys to add/remove from filter
            remove_keys: If True, remove keys from filter; if False, add them

        Note:
            Uses set.discard as it doesn't raise an exception if the element
            wasn't in the set to begin with.
        N)r+   ÚsetÚ_resolve_iterabler,   r-   )r1   r3   r4   r5   r6   s        r'   Úupdatez_FilterState.updateN   s©   € ñ( Ø�OŠOœsÔ#4°YÓ#?Ó@Ñ@�OØ×Ò¤Ô$5°jÓ$AÓ BÑBÕØ×Ò¤3Ô'8¸Ó'GÓ#HÑHÖà�OŠOœsÔ#4°YÓ#?Ó@Ñ@�OØ×Ò¤Ô$5°jÓ$AÓ BÑBÕØ×Ò¤3Ô'8¸Ó'GÓ#HÑHÖr&   c                 óX   — t        | j                  | j                  | j                  «      S )z¯
        Build a set of dictionary keys based on the current filter state.

        Returns:
            set[tuple[str, str]]: Set of (op_symbol, dispatch_key) tuples
        )Ú_build_key_setr+   r,   r-   )r1   s    r'   Úbuild_disable_key_setz"_FilterState.build_disable_key_setk   s*   € ô Ø�O‰OØ×ÑØ×Ñó
ð 	
r&   c                 ó(  — d}|dz  }|dz  }t        | j                  «      D ]  \  }}|d|› d|› d�z  }Œ |dz  }t        | j                  «      D ]  \  }}|d|› d|› d�z  }Œ |dz  }t        | j                  «      D ]  \  }}|d|› d|› d�z  }Œ |S )	z-Return string representation of filter state.Ú zFilter State:
z  === DSL: ===
ú    z: ú
z  === OP SYMBOL: ===
z  === DISPATCH KEYS: ===
)Ú	enumerater+   r,   r-   )r1   ÚsÚiÚdslÚopÚkeys         r'   Ú__str__z_FilterState.__str__x   sÝ   € àˆØ	ÐÑˆØ	ÐÑˆÜ §¡Ó0ò 	%‰FˆAˆsØ�4˜�s˜"˜S˜E Ð$Ñ$‰Að	%à	Ð%Ñ%ˆÜ˜t×/Ñ/Ó0ò 	$‰EˆAˆrØ�4˜�s˜"˜R˜D Ð#Ñ#‰Að	$à	Ð)Ñ)ˆÜ × 3Ñ 3Ó4ò 	%‰FˆAˆsØ�4˜�s˜"˜S˜E Ð$Ñ$‰Að	%ð ˆr&   ©F)r   r   r   r   r   r8   r+   r    r!   r,   r-   r   r$   r2   r   r:   Útupler=   rH   r%   r&   r'   r)   r)   1   sà   … á5á °Ô5€J��C‘Ó5Ù!°#Ô6€K��S‘Ó6Ù$°SÔ9€N�C˜‘HÓ9ð -ð °Dó ð4 "ñIà˜ #™Ñ&¨Ñ-ðIð ˜( 3™-Ñ'¨$Ñ.ðIð ˜X c™]Ñ*¨TÑ1ð	Ið
 ðIð 
óIð:
 s¨5°°c°©?Ñ';ó 
ð˜ô r&   r)   Ú_filter_stateÚ_libsÚ_native_decomp_overridesÚ_graphsÚ_dsl_name_to_lib_graphÚ_dispatch_key_to_lib_graphÚ_op_symbol_to_lib_graphÚ_node_id_counter>   ÚMetaÚCompositeExplicitAutogradÚCompositeImplicitAutogradÚ_DISALLOWED_DISPATCH_KEYSr3   r4   r5   r/   c                 ó´   ‡— t        «       Šdt        t        t           z  dz  dt        ddfˆfd„} || t        «        ||t
        «        ||t        «       ‰S )aK  
    Build a set of dictionary keys based on filter criteria.

    Args:
        dsl_names: DSL names to include in key set
        op_symbols: Operation symbols to include in key set
        dispatch_keys: Dispatch keys to include in key set

    Returns:
        set[tuple[str, str]]: Set of (op_symbol, dispatch_key) tuples
    ÚentriesNÚgraph_lib_dictr/   c                 ój   •— t        | «      }|D ]"  }||v sŒ||   D ]  }‰j                  |«       Œ Œ$ y)z;Helper to add matching keys from graph_lib_dict to key_set.N)r9   Úadd)rX   rY   Úresolved_entriesÚentryrG   Úkey_sets        €r'   Ú_append_to_setz&_build_key_set.<locals>._append_to_setÓ   sG   ø€ ô -¨WÓ5Ðà%ò 	%ˆEØ˜Ò&Ø)¨%Ñ0ò %�CØ—K‘K Õ$ñ%ñ	%r&   )r8   r    r   Ú_MappingTyperO   rQ   rP   )r3   r4   r5   r_   r^   s       @r'   r<   r<   Á   s_   ø€ ô  %(£E€Gð	%Ü”x¤‘}Ñ$ tÑ+ð	%Ü=Ið	%à	õ	%ñ �9Ô4Ô5Ù�:Ô6Ô7Ù�=Ô"<Ô=à€Nr&   F)Úprint_inactivera   c                 ó$  — t         j                  «       D ]y  \  \  }}}t        d|›d|›�«       t        |«      D ]R  \  }}|j                  s| sŒd|› d|j
                  ›d|j                  ›�}| r|d|j                  ›�z  }t        |«       ŒT Œ{ y)z~
    Print all override graphs for debugging purposes.

    Args:
        print_inactive: Whether to print inactive nodes
    zop=z, key=r@   z: node.dsl_name=z, node.unconditional_override=z node.active=N)rN   ÚitemsÚprintrB   r   r   r   )ra   rF   rG   Ú	node_listrD   r.   rC   s          r'   Ú_print_override_graphsrf   å   s�   € ô !(§¡£ò 	Ñ‰	ˆˆS�9Ü����W˜�vÐÔä  Ó+ò 	‰GˆAˆtØ�{Š{šnØ ˜sÐ"3 T§]¡]Ð$4Ð4S°t×7RÑ7RÐ6TÐU�Ù!Ø˜>˜TŸ[™[˜NÐ+Ñ+�Aä�a•ñ	ñ	r&   Ú	_def_libsÚ_defined_native_opsÚ_aten_override_libsÚ	namespacec                 ór   — | t         vr't        j                  j                  | d«      t         | <   t         |    S )NÚFRAGMENT)rg   ÚtorchÚlibraryÚLibrary)rj   s    r'   Ú_get_def_libraryrp     s0   € Øœ	Ñ!Ü$Ÿ}™}×4Ñ4°YÀ
ÓKŒ	�)ÑÜ�YÑÐr&   r   c                 ó|   — d| f}|t         vr(t        j                  j                  dd| «      t         |<   t         |   S )z‘
    Get or create the _native IMPL library for a given dispatch key.

    One library per dispatch key is shared across all overridden ops.
    Ú_nativeÚIMPL)rL   rm   rn   ro   )r   rG   s     r'   Ú_get_or_create_libraryrt   
  s<   € ð �lÐ
#€CØ
”%ÑÜ—]‘]×*Ñ*¨9°f¸lÓKŒˆc‰
ä�‰:Ðr&   r   Úkernelc                 óÚ   — | |f}t         j                  |d«      }|�|j                  «        t        j                  j                  dd|«      }|j                  | ||d¬«       |t         |<   y)a  
    Install (or replace) an aten kernel at (op_symbol, dispatch_key).

    Creates a fresh Library per (op, key) so we can tear down just this
    one override via `_destroy_aten_override` without affecting any other
    override at the same dispatch key.
    NÚatenrs   T)Úwith_keyset)ri   ÚpopÚ_destroyrm   rn   ro   Úimpl)r   r   ru   rG   ÚexistingÚlibs         r'   Ú_install_aten_overrider~     sk   € ð �lÐ
#€Cô #×&Ñ& s¨DÓ1€HØÐØ×ÑÔä
�-‰-×
Ñ
 ¨°Ó
=€CØ‡H�HˆY˜ ¸$€HÔ?Ø"Ô˜Òr&   c                 óZ   — t         j                  | |fd«      }|�|j                  «        yy)zATear down the aten override at (op_symbol, dispatch_key), if any.N)ri   ry   rz   )r   r   r}   s      r'   Ú_destroy_aten_overrider€   .  s,   € ä
×
!Ñ
! 9¨lÐ";¸TÓ
B€CØ
€Ø�‰�ð r&   ztorch._ops.OpOverload | Nonec                 ó¸   — | j                  d«      \  }}}|xs d}	 t        t        j                  j                  |«      }t        ||«      S # t
        $ r Y yw xY w)u4  
    Resolve `op_symbol` to a concrete OpOverload on `torch.ops.aten`.

    Accepts bare names ("bmm" â†’ aten.bmm.default) and overload-qualified
    names ("add_.Tensor" â†’ aten.add_.Tensor). Returns None if the op is not
    registered (e.g. a test-only op_symbol that never hit the C++ dispatcher).
    ú.ÚdefaultN)Ú	partitionÚgetattrrm   Úopsrw   ÚAttributeError)r   ÚnameÚ_Úoverload_nameÚpackets        r'   Ú_resolve_aten_overloadrŒ   5  s]   € ð '×0Ñ0°Ó5Ñ€Dˆ!ˆ]Ø!Ò. Y€MðÜœŸ™Ÿ™¨Ó.ˆÜ�v˜}Ó-Ð-øÜò Ùðús   �/A Á	AÁAc                 óÀ   — t        | «      }|€t        d| ›�«      ‚t        |j                  «      }|j	                  dd«      \  }}|j	                  dd«      \  }}d|› �S )uÎ   Return the schema of at::<op_symbol> with the `aten::<name>` prefix stripped.

    Accepts bare names ("bmm" â†’ aten.bmm.default) and overload-qualified
    names ("add_.Tensor" â†’ aten.add_.Tensor).
    z aten op not found for op_symbol=z::é   ú()rŒ   r‡   r    Ú_schemaÚsplit)r   ÚoverloadrC   r‰   ÚrestÚargss         r'   Ú_aten_schema_tailr•   F  sk   € ô & iÓ0€HØÐÜÐ?À	¸}ÐMÓNÐNÜˆH×ÑÓ€Aà�g‰g�d˜AÓ�G€A€tØ�j‰j˜˜aÓ �G€A€tØˆtˆfˆ:Ðr&   rˆ   c                 óú   — | t         v ry t        d«      j                  | › t        |«      › �«       t	        |«      }t        j                  j                  d| › �«      |dœd„«       t         j                  | «       y )Nrr   z	_native::)Ú_aten_overloadc                 ó   —  | |i |¤ŽS ©Nr%   )r—   r”   Úkwargss      r'   ú<lambda>z(_define_native_op_once.<locals>.<lambda>d  s   € ¹nØð?
Øñ?
€ r&   )	rh   rp   Údefiner•   rŒ   rm   rn   Úregister_faker[   )rˆ   r   Úaten_overloads      r'   Ú_define_native_op_oncerŸ   V  su   € ð Ô"Ñ"ØÜ�YÓ×&Ñ&¨$¨Ô0AÀ)Ó0LÐ/MÐ'NÔOô +¨9Ó5€MÜ	‡M�M×Ñ )¨D¨6Ð 2Ó3Ø%2ó 	
ôô ×Ñ˜DÕ!r&   r}   r.   c                 óü   — |j                   s&t        d|j                  ›d|j                  ›d�«      ‚t	        |j                   |j                  «       | j                  |j                   |j                  |dd¬«       y)zà
    Register a single node implementation with the library.

    Args:
        lib: The torch.library.Library instance
        node: The override node to register
        dispatch_key: The dispatch key for registration
    zJ_OverrideNode must have a non-empty node_id before registration (dsl_name=z, op_symbol=ú)FT)rx   Úallow_overrideN)r   Ú
ValueErrorr   r   rŸ   r{   r   )r}   r.   r   s      r'   Ú_register_node_implr¤   r  sv   € ð �<Š<ÜðØŸ™Ð)¨°d·n±nÐ5GÀqðJó
ð 	
ô ˜4Ÿ<™<¨¯©Ô8Ø‡H�HØ�‰Ø�‰ØØØð õ r&   Úiterablec                 óT   — | €g S t        | t        «      rt        | t        «      r| fS | S )zÌ
    Resolve various input types to a consistent iterable of strings.

    Args:
        iterable: String, iterable of strings, or None

    Returns:
        Iterable[str]: Consistent iterable output
    )Ú
isinstancer   r    )r¥   s    r'   r9   r9   Œ  s/   € ð ÐØˆ	ä�h¤Ô)¬Z¸Ä#Ô-FØˆ{Ðà€Or&   )Úenable_dsl_namesÚenable_op_symbolsÚenable_dispatch_keysr¨   r©   rª   c                 óÞ   — t         j                  d| ||«       t        j                  | ||d¬«       t	        | ||«      }|D ]*  }|\  }}|t
        v sŒt        ||t
        |   t        ¬«       Œ, y)as  
    Re-enable overrides by removing them from filter state and reregistering.

    Args:
        enable_dsl_names: DSL names to re-enable
        enable_op_symbols: Operation symbols to re-enable
        enable_dispatch_keys: Dispatch keys to re-enable

    Note:
        This function uses reverse filter state management (removing from
        filters to enable).
    z>Re-registering ops by dsl: %s, op_symbol: %s, dispatch_key: %sT)r6   ©Úfilter_stateN)ÚlogÚinforK   r:   r<   rN   Ú_register_overrides_from_graph)r¨   r©   rª   r^   rG   r   r   s          r'   r   r   Ÿ  s‹   € ô$ ‡H�HØHØØØô	ô ×ÑØØØØð	 ô ô %3ØØØó%€Gð ò ˆØ"%Ñˆ	�<à”'Š>ô +Ø˜<¬°©ÄMöñr&   )Údisable_dsl_namesÚdisable_op_symbolsÚdisable_dispatch_keysr±   r²   r³   c                 óè   — t         j                  d| ||«       t        j                  | ||«       t        j	                  «       }|D ]*  }|\  }}|t
        v sŒt        ||t
        |   t        ¬«       Œ, y)ag  
    De-register overrides by updating filter state and reregistering graphs.

    Args:
        disable_dsl_names: DSL names to disable
        disable_op_symbols: Operation symbols to disable
        disable_dispatch_keys: Dispatch keys to disable

    Note:
        This function uses filter state management to selectively disable
        operations.
    z>De-registering ops by dsl: %s, op_symbol: %s, dispatch_key: %sr¬   N)r®   r¯   rK   r:   r=   rN   Ú_cleanup_and_reregister_graph)r±   r²   r³   r^   rG   r   r   s          r'   r   r   Ô  s}   € ô$ ‡H�HØHØØØô	ô ×ÑÐ*Ð,>Ð@UÔVô %2×$GÑ$GÓ$I€Gð ò 	ˆØ"%Ñˆ	�<à”'Š>Ü)ØØÜ˜‘Ü*ö	ñ		r&   r   c                 ó¼   — t        «       }t        j                  «       D ]1  \  \  }}}|D ]$  }|j                  | k(  sŒ|j	                  |«        Œ1 Œ3 t        |«      S )z¼Get list of operations registered by a specific DSL.

    Args:
        dsl_name: Name of the DSL to query.

    Returns:
        Sorted list of operation names registered by the DSL.
    )r8   rN   rc   r   r[   Úsorted)r   Ú
operationsr   r‰   Únodesr.   s         r'   r   r      s`   € ô “€JÜ!(§¡£ò Ñ‰ˆ�A˜Øò 	ˆDØ�}‰} Ó(Ø—‘˜yÔ)Ùñ	ðô
 �*ÓÐr&   rG   c                 óä   — dt         t        t        t        t        t        f      f   dt        dt        t        t        f   ddfd„} |t        | |«        |t
        ||«        |t        ||«       y)zÖ
    Update the registration mapping dictionaries.

    Args:
        dsl_name: The DSL name
        op_symbol: The operation symbol
        dispatch_key: The dispatch key
        key: The dictionary key tuple
    ÚregistrationÚsymbolrG   r/   Nc                 ó^   — | j                  |«      }|€	|g}|| |<   y|j                  |«       y)z;Helper to add key to registration list or create new entry.N)ÚgetÚappend)r»   r¼   rG   Ú
entry_lists       r'   Ú_get_new_entry_or_appendz;_update_registration_maps.<locals>._get_new_entry_or_append%  s9   € ð "×%Ñ% fÓ-ˆ
àÐØ˜ˆJØ#-ˆL˜Ò à×Ñ˜cÕ"r&   )Údictr    ÚlistrJ   rO   rQ   rP   )r   r   r   rG   rÁ   s        r'   Ú_update_registration_mapsrÄ     ss   € ð&#Üœ3¤¤U¬3´¨8¡_Ñ 5Ð5Ñ6ð#äð#ô ”3œ�8‰_ð#ð 
ó	#ñ Ô3°X¸sÔCÙÔ4°iÀÔEÙÔ7¸ÀsÕKr&   r”   rš   c                   ó   — y)NTr%   )r”   rš   s     r'   Ú_always_truerÆ   8  s   € Ør&   )Úallow_multiple_overrider   ÚbackendÚ
lib_symbolÚcondr{   rÇ   r   c                ó|  — |dk7  rt        d|› d�«      ‚|t        v rt        d|›d�«      ‚|€|st        d«      ‚t        }||f}t        j	                  |g «      }	|j                  dd	«      }
|
› d	| › d	t        › �}t        d
z  a|	j                  t        | ||||||¬«      «       |	t        |<   t        | |||¬«       y)a�  
    Register a passed override function to the dispatcher.

    Actually a graph-building operation; real registration happens later.

    Args:
        backend: The backend name (DSL name)
        lib_symbol: Library you're overriding symbols in (must be "aten")
        op_symbol: Name of the operation you're overriding
        dispatch_key: Dispatch key to override
        cond: Predicate choosing whether `impl` applies to a given call. May
            be None if `unconditional_override=True`.
        impl: Implementation function for the override
        allow_multiple_override: Allow overriding an existing override
        unconditional_override: Implementation doesn't have a fallback and
            doesn't require torch.DispatchKeySet as the first argument. When
            True, a trivially-True predicate is supplied for the router if
            `cond` is None.

    Raises:
        ValueError: If lib_symbol is not "aten", if dispatch_key is in
            _DISALLOWED_DISPATCH_KEYS (Meta / CompositeImplicitAutograd /
            CompositeExplicitAutograd), or if cond is None without
            unconditional_override=True.
    rw   z.Unsupported lib_symbol (must be "aten", got: "ú"zdispatch_key=z¡ is not supported. Overrides must be installed at a backend key (e.g. CPU, CUDA, XPU); the router's fake kernel redispatches to aten and would recurse otherwise.Nz8cond must be provided unless unconditional_override=Truer‚   r‰   rŽ   )r   r   r   r   r   r   r   )rG   )
r£   rV   rÆ   rN   r¾   ÚreplacerR   r¿   r   rÄ   )rÈ   rÉ   r   r   rÊ   r{   rÇ   r   rG   Úop_graphÚ
_sanitizedr   s               r'   r
   r
   <  sý   € ðH �VÒÜÐIÈ*ÈÐUVÐWÓXÐXàÔ0Ñ0ÜØ˜LÐ+ð ,Gð Hó
ð 	
ð €|Ù%ÜÐWÓXÐXÜˆà�lÐ
#€Cô �{‰{˜3 Ó#€Hð ×"Ñ" 3¨Ó,€JØ�˜A˜g˜Y aÔ(8Ð'9Ð:€GÜ˜ÑÐà‡O�OÜØØØ%ØØØ#9Øô	
ô
ð „GˆC�Lä˜g y°,ÀCÖHr&   ©Úforce_reregisterÚoriginal_graphÚ	new_graphrÑ   c                ó   — |ry| |k7  S )aG  
    Determine if a graph needs reregistration based on changes.

    Args:
        original_graph: The original graph before modification
        new_graph: The graph after modification
        force_reregister: If True, always reregister regardless of changes

    Returns:
        bool: True if reregistration is needed
    Tr%   )rÒ   rÓ   rÑ   s      r'   Ú_should_reregister_graphrÕ   Œ  s   € ñ" Øð ˜YÑ&Ð&r&   r¬   Úgraphr­   c                ó"   — t        | |||¬«       y)a‹  
    Reregister a graph's routes from scratch.

    Used by reorder / deregister / reenable. Libraries are intentionally
    long-lived singletons; we rebuild the per-op router closure here.

    Args:
        op_symbol: The operation symbol
        dispatch_key: The dispatch key
        graph: The graph to register
        filter_state: Optional filter state for conditional registration
    r¬   N)r°   )r   r   rÖ   r­   s       r'   rµ   rµ   ¤  s   € ô& #ØØØØ!ö	r&   )Úkeys_to_processÚreregister_overridesr­   Útransformation_fnrØ   rÙ   c                óB  — |�|nt        t        j                  «       «      }t        |«      D ]—  \  }}||ft        vrŒt        t        ||f   «      }	  | |||«      }t        |t        «      s,t        j                  dt        |«      j                  ||«       Œl|t        ||f<   |sŒzt        ||d¬«      sŒ‰t!        ||||¬	«       Œ™ y# t        t
        t        t        f$ r t        j                  d||d¬«       Y ŒÏt        $ r t        j                  d||«       Y Œðw xY w)
a”  
    Apply a transformation function to graphs and optionally reregister.

    This is the core pattern used by reorder_graphs_from_user_function and
    can be reused for other graph transformation operations.

    Args:
        transformation_fn: Function to transform each graph
        keys_to_process: Keys to process, or None for all graphs
        reregister_overrides: Whether to reregister changed graphs
        filter_state: Optional filter state for conditional registration

    Note:
        If transformation_fn raises an exception for a specific graph, that graph
        will be skipped and processing will continue with remaining graphs.
    NúAGraph transformation failed for %s/%s. Preserving original graph.T©Úexc_infoúNUnexpected error in graph transformation for %s/%s. Preserving original graph.zbGraph transformation returned invalid type %s for %s/%s. Expected list. Preserving original graph.FrÐ   r¬   )r8   rN   ÚkeysrÃ   Ú	TypeErrorr£   r‡   ÚRuntimeErrorr®   ÚwarningÚ	ExceptionÚ	exceptionr§   Útyper   rÕ   rµ   )	rÚ   rØ   rÙ   r­   Útarget_keysr   r   rÒ   rÓ   s	            r'   Ú_apply_graph_transformationrè   ¿  s=  € ð6 +Ð6‰¼CÄÇÁÃÓ<Oð ô
 $(¨Ó#4ò /Ñˆ	�<Ø�|Ð$¬GÑ3Øäœg y°,Ð&?Ñ@ÓAˆð	Ù)¨)°\À>ÓRˆIô$ ˜)¤TÔ*Ü�K‰KØtÜ�Y“×(Ñ(ØØô	ð ð .7Œ�˜LÐ)Ñ*ò  Ô$<Ø˜I¸ö%
ô *ØØØØ)ö	ñU/øô œ:¤~´|ÐDò 	Ü�K‰KØSØØØð	 ô ñ Üò 	Ü�M‰MØ`ØØôñ
 ð	ús   Á
CÃ2DÃ<DÄDÚoverrides_onlyc                 óf   — | ri }nddl m} t         |«       «      }|j                  t        «       |S )aÌ  
    Return a decomposition table suitable for passing to
    ``ExportedProgram.run_decompositions`` or to Inductor / Dynamo as a
    decomposition set for ``torch.compile``.

    This is the canonical way to apply native overrides outside of eager.
    Callers opt in explicitly -- the registry does **not** install into any
    global compile/export decomp table on its own. This preserves three
    properties:

      * ``import torch._native`` doesn't transitively import inductor /
        dynamo / triton.
      * No other consumer (ONNX, tests, third-party backends) accidentally
        picks up our overrides.
      * Scoping is up to the caller: pass the table only where routing is
        desired.

    By default, the returned table contains
    ``torch.export.default_decompositions()`` with native-registered
    overrides layered on top -- matching typical "run the usual
    decompositions, plus my overrides" intent. The override entries win
    over any same-op default, because they're merged last.

    Example:

        ep = torch.export.export(model, args)
        ep = ep.run_decompositions(
            torch._native.registry.native_decomp_table()
        )

    Args:
        overrides_only: If True, return only the native-registered overrides
            and no default aten decompositions. Useful for inspection /
            debugging, or when composing tables manually.

    Returns:
        A dict mapping ``OpOverload`` to decomposition callable.
    r   )Údefault_decompositions)Útorch.exportrë   rÂ   r:   rM   )ré   Útablerë   s      r'   r   r     s2   € ñR Ø(*‰õ 	8äÑ+Ó-Ó.ˆà	‡L�LÔ)Ô*Ø€Lr&   c                ó  ‡‡‡— t        |«      }g Š|D ]\  }d}|r|j                  |«      }|r<t        |||«       ‰j                  |j                  |j
                  f«       d|_        ŒVd|_        Œ^ t        | «      }t        | |«       ‰s|�t        j                  |d«       yt        j                  j                  d| › �|«      }t        «       Šdt        fˆˆfd„Š|dœˆˆfd„
}	ˆˆfd	„}
t!        | ||	«       |�
|
t        |<   yy)
a  
    Register all overrides in a single graph.

    Args:
        op_symbol: The operation symbol
        dispatch_key: The dispatch key
        graph: List of override nodes to register
        filter_state: Optional filter state for conditional registration
    TFNzaten::Úswallow_cond_exceptionsc                 ó®   •— ‰D ]<  \  }}	  || i |¤Ž}|sŒ t        t        j                  j                  |«      | i |¤Žc S  ‰S # t         $ r |s‚ Y ŒOw xY wr™   )rä   r…   rm   r†   rr   )r”   rš   rï   rÊ   Ú	impl_nameÚmatchedÚ	_NO_MATCHÚ	cond_impls         €€r'   Ú	_dispatchz1_register_overrides_from_graph.<locals>._dispatchŽ  sv   ø€ Ø(ò 	N‰OˆD�)ðÙ Ð/¨Ñ/�ò
 Ø<”wœuŸy™y×0Ñ0°)Ó<¸dÐMÀfÑMÒMð	Nð Ðøô ò Ù.ØÙðús   ‹AÁAÁA)Ú	_fallbackc                óR   •—  ‰||d¬«      }|‰u r |j                   | g|¢­i |¤ŽS |S )NF©rï   )Ú
call_boxed)Úkeysetrö   r”   rš   Úresultró   rõ   s        €€r'   Úeager_routerz4_register_overrides_from_graph.<locals>.eager_routerš  s;   ø€ Ù˜4 ÀÔGˆØ�YÑØ'�9×'Ñ'¨Ð@°Ò@¸Ñ@Ð@Øˆr&   c                  ó2   •—  ‰| |d¬«      }|‰u rt         S |S )NTrø   )ÚNotImplemented)r”   rš   rû   ró   rõ   s      €€r'   Úcompile_routerz6_register_overrides_from_graph.<locals>.compile_router   s$   ø€ Ù˜4 ÀÔFˆØ�YÑÜ!Ð!Øˆr&   )rt   r2   r¤   r¿   r   r   r   rŒ   r€   rM   ry   rm   rn   Ú
get_kernelÚobjectr$   r~   )r   r   rÖ   r­   r}   r.   Úenabler’   Úfallback_kernelrü   rÿ   ró   rõ   rô   s              @@@r'   r°   r°   F  s  ú€ ô  ! Ó
.€Cà-/€Ið
 ò 
 ˆØˆÙØ!×/Ñ/°Ó5ˆFáÜ  T¨<Ô8Ø×Ñ˜dŸl™l¨D¯L©LÐ9Ô:ØˆD�KàˆD�Kð
 ô & iÓ0€Hô ˜9 lÔ3ñ ØÐÜ$×(Ñ(¨°4Ô8Øô —m‘m×.Ñ.°¸	°{Ð/CÀ\ÓR€Oô “€Ið
¼ö 
ð />÷ õô ˜9 l°LÔAð ÐØ-;Ô  Ò*ð r&   c                  ó`   — t         j                  «       D ]  \  } }| \  }}t        |||«       Œ y)zO
    Perform all registration calls from previously-built override graphs.
    N)rN   rc   r°   )rG   rÖ   r   r   s       r'   Ú_register_all_overridesr  ±  s8   € ô —m‘m“oò 
‰
ˆˆUØ"%Ñˆ	�<ä&ØØØõ	
ñ
r&   )rÙ   Úfnc                ó   — t        | |¬«       y)a¶  
    Reorder override graphs using a user-provided ordering function.

    Args:
        fn: User-provided function that takes (op_symbol, dispatch_key, graph)
            and returns a reordered graph
        reregister_overrides: Whether to reregister graphs that have changed

    Note:
        This function uses the common graph transformation pattern and can serve
        as an example for other graph manipulation operations.
    ©rÚ   rÙ   N)rè   )r  rÙ   s     r'   r   r   ¿  s   € ô"  ØØ1ör&   Ú	filter_fnc          	      óx   ‡ — dt         dt         dt        t           dt        t           fˆ fd„}t        ||¬«       y)aø  
    Apply a filter function to remove nodes from graphs.

    This is a convenience function that uses the graph transformation pattern
    to filter out unwanted nodes.

    Args:
        filter_fn: Function that takes (op_symbol, dispatch_key, node) and
            returns True to keep the node, False to remove it
        reregister_overrides: Whether to reregister modified graphs

    Example:
        # Remove all nodes with "deprecated" in the DSL name
        _apply_graph_filter(
            lambda op, dk, node: "deprecated" not in node.dsl_name,
            reregister_overrides=True
        )

    Note:
        If filter_fn raises an exception for a specific graph, the original
        graph will be preserved and processing will continue.
    r   r   rÖ   r/   c                 ó   •— 	 |D �cg c]  } ‰| ||«      sŒ|‘Œ c}S c c}w # t         t        t        t        f$ r t        j                  d| |d¬«       |cY S t        $ r t        j                  d| |«       |cY S w xY w)z-Apply filter_fn to graph with error handling.rÜ   TrÝ   rß   ©rá   r£   r‡   râ   r®   rã   rä   rå   )r   r   rÖ   r.   r	  s       €r'   Úfiltering_transformationz5_apply_graph_filter.<locals>.filtering_transformationò  sˆ   ø€ ð	Ø%*ÖW˜T©i¸	À<ÐQUÕ.V’DÒWÐWùÒWøÜœ:¤~´|ÐDò 	Ü�K‰KØSØØØð	 ô ð ŠLÜò 	Ü�M‰MØ`ØØôð
 ŠLð	ús%   ƒ# ‡—›# ž# £4A=Á!A=Á<A=r  N©r    rÃ   r   rè   )r	  rÙ   r  s   `  r'   Ú_apply_graph_filterr  Ö  sB   ø€ ð8ÜðÜ&)ðÜ26´}Ñ2Eðä	ŒmÑ	õô,  Ø2Ø1ör&   Úcondition_fnÚordering_fnc          	      ó|   ‡ ‡— dt         dt         dt        t           dt        t           fˆ ˆfd„}t        ||¬«       y)aE  
    Apply reordering only to graphs that match a condition.

    This allows for more targeted reordering operations.

    Args:
        condition_fn: Function that takes (op_symbol, dispatch_key) and
            returns True if the graph should be reordered
        ordering_fn: Ordering function to apply to matching graphs
        reregister_overrides: Whether to reregister modified graphs

    Example:
        # Only reorder CUDA operations
        _apply_selective_reordering(
            condition_fn=lambda op, dk: dk == "CUDA",
            ordering_fn=lambda op, dk, g: sorted(g, key=lambda n: n.dsl_name),
            reregister_overrides=True
        )

    Note:
        If condition_fn or ordering_fn raises an exception for a specific graph,
        the original graph will be preserved and processing will continue.
    r   r   rÖ   r/   c                 óª  •— 	  ‰| |«      }|r	  ‰| ||«      S |S # t         t        t        t        f$ r t        j                  d| |d¬«       |cY S t        $ r t        j                  d| |«       |cY S w xY w# t         t        t        t        f$ r t        j                  d| |d¬«       |cY S t        $ r t        j                  d| |«       |cY S w xY w)z=Apply ordering_fn conditionally based on condition_fn result.rÜ   TrÝ   rß   r  )r   r   rÖ   Úshould_reorderr  r  s       €€r'   Úconditional_transformationz?_apply_selective_reordering.<locals>.conditional_transformation,  sø   ø€ ð	Ù)¨)°\ÓBˆNñ" ðÙ" 9¨l¸EÓBÐBð" ˆøôG œ:¤~´|ÐDò 	Ü�K‰KØSØØØð	 ô ð ŠLÜò 	Ü�M‰MØ`ØØôð
 ŠLð	ûô œz¬>¼<ÐHò Ü—‘ØWØØ Ø!ð	 ô ð ’Üò Ü—‘ØdØØ ôð
 ’ðús,   ƒ	 �	A8 ›4A5Á!A5Á4A5Á84CÂ.!CÃCr  Nr  )r  r  rÙ   r  s   ``  r'   Ú_apply_selective_reorderingr    sC   ù€ ð<)Üð)Ü&)ð)Ü26´}Ñ2Eð)ä	ŒmÑ	ö)ôV  Ø4Ø1ör&   rI   )r/   N)KÚloggingÚcollections.abcr   r   Údataclassesr   r   Útypingr   r   Útorch.libraryrm   Ú__all__Ú	getLoggerr   r®   r   r   r$   r"   r#   r   r    rÃ   r	   r)   rK   r!   rL   rÂ   rJ   rn   ro   rM   r  Ú_GraphsTyperN   r`   rO   rP   rQ   rR   ÚintÚ	frozensetrV   r8   r<   rf   rg   rh   ri   rp   rt   r~   r€   rŒ   r•   rŸ   r¤   r9   r   r   r   rÄ   rÆ   r
   rÕ   rµ   rè   r   r°   r  r   r  r  r%   r&   r'   ú<module>r!     s  ðÜ ß .ß (ß %ã ò€ð €g×Ñ˜Ó!€áˆcƒN€ÙˆCƒL€à�Q˜�WÑ€	Ø�Q˜�T‰N€	ð ÷ð ó ðð" ˜3  T¨-Ñ%8Ð9¸4ÀÑ;NÐNÑO€ð ÷Tð Tó ðTñp +›n€ˆ|Ó ,ð 79€€tˆE�#�s�(‰O˜UŸ]™]×2Ñ2Ð2Ñ3Ó 8ð. 46Ð ˜$˜v xÐ/Ñ0Ó 5ð �5˜˜c˜‘? D¨Ñ$7Ð7Ñ8€Ø€ˆÓ à�C˜˜e C¨ H™oÑ.Ð.Ñ/€ð (*Ð ˜Ó )Ø+-Ð ˜LÓ -Ø(*Ð ˜Ó *ð
 Ð �#Ó ñ -6ÚFó-Ð ˜9 S™>ó ð
!Ø�X˜c‘]Ñ" TÑ)ð!à�h˜s‘mÑ# dÑ*ð!ð ˜ #™Ñ&¨Ñ-ð!ð 	ˆˆs�Cˆx‰Ñó	!ðH 6;ò ¨dð ¸tó ð( /1€	ˆ4��U—]‘]×*Ñ*Ð*Ñ+Ó 0á #£Ð �S˜‘XÓ %ð EGÐ �T˜%  S ™/¨5¯=©=×+@Ñ+@Ð@ÑAÓ Fð  ð  ¨¯©×(=Ñ(=ó  ð¨ð °·±×1FÑ1Fó ð# cð #¸ð #Àhð #ÐSWó #ð* cð ¸ð Àó ð cð Ð.Ló ð" ð ¨ó ð " ð "°ð "¸ó "ð8Ø	�‰×	Ñ	ðØ&3ðØCFðà	óð4  h¨s¡mÑ 3°dÑ :ð ¸xÈ¹}ó ð* 04Ø04Ø37ò	2à˜D ™I‘o¨Ñ,ð2ð ˜T #™Y‘¨Ñ-ð2ð   S¡	™/¨DÑ0ð	2ð
 
ó2ðn 15Ø15Ø48ò	)à˜T #™Y‘¨Ñ-ð)ð ˜d 3™i™¨$Ñ.ð)ð   c¡™?¨TÑ1ð	)ð
 
ó)ðX ð ¨¨c©ó ð$#LØð#Làð#Lð ð#Lð 
ˆs�Cˆx‰ð	#Lð
 
ó#LðL˜ð ¨&ð °Tó ð %*Ø#(òMIØðMIàðMIð ðMIð ð	MIð
 �dÑ
ðMIð ðMIð "ðMIð !ðMIð 
óMIðh #ò	'Ø˜Ñ'ð'à�MÑ"ð'ð ð	'ð
 
ó'ð: )-òØðàðð �Ñðð
  Ñ%ðð 
óð< 48Ø!&Ø(,òNØ%ðNð ˜˜s C˜x™Ñ)¨DÑ0ðNð ð	Nð
  Ñ%ðNð 
óNðd !ñ3Øð3à	ˆ&�(Ð
Ñó3ðv )-òh<Øðh<àðh<ð �Ñðh<ð
  Ñ%ðh<ð 
óh<óV
ð" "'òØðð ðð 
ó	ð4 "'ò5Ø˜˜c =Ð1°4Ð7Ñ8ð5ð ð5ð 
ó	5ðx "'ò	LØ˜C ˜: tÐ+Ñ,ðLàðLð ð	Lð
 
ôLr&   