§
    ŠŠtjÒ  ã                   óh  — U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlZddlmZ  ej        e¦  «        Z G d„ de
¦  «        Zeej        eej                 gef         Zi Zeeedz  f         ed	<   i Zeeef         ed
<   daeez  ed<   	 	 	 d dedz  dedz  dee         dede	f         fd„Z ej        ed¬¦  «        Z ej        ed¬¦  «        Zdeez  defd„Zd!dee         fd„Z ej!        d"d„¦   «         Z"ej!        d"d„¦   «         Z#dede$fd„Z%deez  dz  ddfd„Z&deez  fd„Z'dS )#aÚ  
This module implements TorchDynamo's backend registry system for managing compiler backends.

The registry provides a centralized way to register, discover and manage different compiler
backends that can be used with torch.compile(). It handles:

- Backend registration and discovery through decorators and entry points
- Lazy loading of backend implementations
- Lookup and validation of backend names
- Categorization of backends using tags (debug, experimental, etc.)

Key components:
- CompilerFn: Type for backend compiler functions that transform FX graphs
- _BACKENDS: Registry mapping backend names to entry points
- _COMPILER_FNS: Registry mapping backend names to loaded compiler functions

Example usage:
    @register_backend
    def my_compiler(fx_graph, example_inputs):
        # Transform FX graph into optimized implementation
        return compiled_fn

    # Use registered backend
    torch.compile(model, backend="my_compiler")

The registry also supports discovering backends through setuptools entry points
in the "torch_dynamo_backends" group. Example:
```
setup.py
---
from setuptools import setup

setup(
    name='my_torch_backend',
    version='0.1',
    packages=['my_torch_backend'],
    entry_points={
        'torch_dynamo_backends': [
            # name = path to entry point of backend implementation
            'my_compiler = my_torch_backend.compiler:my_compiler_function',
        ],
    },
)
```
```
my_torch_backend/compiler.py
---
def my_compiler_function(fx_graph, example_inputs):
    # Transform FX graph into optimized implementation
    return compiled_fn
```
Using `my_compiler` backend:
```
import torch

model = ...  # Your PyTorch model
optimized_model = torch.compile(model, backend="my_compiler")
```
é    N)ÚCallableÚSequence)Ú
EntryPoint)ÚAnyÚProtocol)Úfxc                   óB   — e Zd Zdej        deej        df         fd„ZdS )Ú
CompiledFnÚargsÚreturn.c                 ó   — d S )N© )Úselfr   s     ú]/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_dynamo/backends/registry.pyÚ__call__zCompiledFn.__call__K   s   € € € ó    N)Ú__name__Ú
__module__Ú__qualname__ÚtorchÚTensorÚtupler   r   r   r   r
   r
   J   s4   € € € € € ØL˜eœlÐL¨u°U´\À3Ð5FÔ/GÐLÐLÐLÐLÐLÐLr   r
   Ú	_BACKENDSÚ_COMPILER_FNSÚinductorÚ_default_backendr   Úcompiler_fnÚnameÚtagsr   .c                 óH  — | €t          j        t          ||¬¦  «        S t          | ¦  «        st	          dt          | ¦  «        › �¦  «        ‚|p| j        }|t          v rt	          d|› �¦  «        ‚| t          vr
dt          |<   | t          |<   t          |¦  «        | _
        | S )aÕ  
    Decorator to add a given compiler to the registry to allow calling
    `torch.compile` with string shorthand.  Note: for projects not
    imported by default, it might be easier to pass a function directly
    as a backend and not use a string.

    Args:
        compiler_fn: Callable taking a FX graph and fake tensor inputs
        name: Optional name, defaults to `compiler_fn.__name__`
        tags: Optional set of string tags to categorize backend with
    N)r   r   z"compiler_fn must be callable, got zduplicate name: )Ú	functoolsÚpartialÚregister_backendÚcallableÚAssertionErrorÚtyper   r   r   r   Ú_tags)r   r   r   s      r   r#   r#   U   s´   € ð  ÐåÔ Õ!1¸À4ÐHÑHÔHÐHÝ�KÑ Ô ð WÝÐUÅ$À{ÑBSÔBSÐUÐUÑVÔVÐVØÐ'�;Ô'€DØ�}ÐÐÝÐ6°Ð6Ð6Ñ7Ô7Ð7Ø�)Ð#Ð#Ø�	�$‰Ø%…M�$ÑÝ˜d™œ€KÔØÐr   )Údebug)r   )Úexperimentalc                 ó$  — t          | t          ¦  «        rz| t          vrt          ¦   «          | t          vrddlm}  || ¬¦  «        ‚| t          vr2t          |          }|�#t          |                     ¦   «         | ¬¦  «         t          |          } | S )z#Expand backend strings to functionsé   )ÚInvalidBackend)r   N)r   r   )	Ú
isinstanceÚstrr   Ú_lazy_importÚexcr,   r   r#   Úload)r   r,   Úentry_points      r   Úlookup_backendr3   z   s¢   € å�+�sÑ#Ô#ð 1Ø�iÐ'Ð'Ý‰NŒNˆNØ�iÐ'Ð'Ø,Ð,Ð,Ð,Ð,Ð,à �. kÐ2Ñ2Ô2Ð2à�mÐ+Ð+Ý# KÔ0ˆKØÐ&Ý ¨[×-=Ò-=Ñ-?Ô-?ÀkÐRÑRÔRÐRÝ# KÔ0ˆØÐr   ©r(   r)   c                 ó†   ‡— t          ¦   «          t          | pd¦  «        Šˆfd„t          D ¦   «         }t          |¦  «        S )za
    Return valid strings that can be passed to:

        torch.compile(..., backend="name")
    r   c                 óp   •— g | ]2}|t           vs%‰                     t           |         j        ¦  «        °0|‘Œ3S r   )r   Úintersectionr'   )Ú.0r   Úexclude_tags_sets     €r   ú
<listcomp>z!list_backends.<locals>.<listcomp>–   sL   ø€ ð ð ð àØ•}Ð$Ð$Ø×,Ò,­]¸4Ô-@Ô-FÑGÔGð %ð 	à$Ð$Ð$r   )r/   Úsetr   Úsorted)Úexclude_tagsÚbackendsr9   s     @r   Úlist_backendsr?   �   sY   ø€ õ �N„N€NÝ˜<Ð-¨2Ñ.Ô.Ððð ð ð åðñ ô €Hõ �(ÑÔÐr   c                  ó~   — ddl m}  ddlm}  || ¦  «         ddlm} |€t          d¦  «        ‚t          ¦   «          d S )Nr+   )r>   )Úimport_submodule)Údynamo_minifier_backendz&dynamo_minifier_backend failed to load)Ú r>   ÚutilsrA   Úrepro.after_dynamorB   r%   Ú_discover_entrypoint_backends)r>   rA   rB   s      r   r/   r/   Ÿ   sp   € àÐÐÐÐÐØ(Ð(Ð(Ð(Ð(Ð(àÐ�XÑÔÐà<Ð<Ð<Ð<Ð<Ð<àÐ&ÝÐEÑFÔFÐFå!Ñ#Ô#Ð#Ð#Ð#r   c                  ó€   ‡— ddl m}  d} | |¬¦  «        Šˆfd„‰j        D ¦   «         }|D ]}||         t          |<   Œd S )Nr   )Úentry_pointsÚtorch_dynamo_backends)Úgroupc                 ó"   •— i | ]}|‰|         “ŒS r   r   )r8   r   Úepss     €r   ú
<dictcomp>z1_discover_entrypoint_backends.<locals>.<dictcomp>¶   s   ø€ Ð6Ð6Ð6 D��c˜$”iÐ6Ð6Ð6r   )Úimportlib.metadatarH   Únamesr   )rH   Ú
group_nameÚeps_dictÚbackend_namerL   s       @r   rF   rF   ®   st   ø€ ð 0Ð/Ð/Ð/Ð/Ð/à(€JØ
ˆ,˜ZÐ
(Ñ
(Ô
(€Cà6Ð6Ð6Ð6¨C¬IÐ6Ñ6Ô6€HØ ð 9ð 9ˆØ"*¨<Ô"8�	�,ÑÐð9ð 9r   c                 ó  — t          ¦   «          | t                               ¦   «         v rdS t          | d¦  «        r| j        }|t
          v s	|t          v rdS t          | d¦  «        r | j        t                               ¦   «         v S dS )z—
    Check if the given compiler function is a registered backend.
    Custom backends (user-provided callables not in the registry) return False.
    TÚcompiler_namer   F)r/   r   ÚvaluesÚhasattrrT   r   r   )r   rT   s     r   Ú_is_registered_backendrW   »   s•   € õ �N„N€Nð •m×*Ò*Ñ,Ô,Ð,Ð,Øˆtõ ˆ{˜OÑ,Ô,ð Ø#Ô1ˆØ�IÐ%Ð%¨½-Ð)GÐ)GØ�4õ ˆ{˜MÑ*Ô*ð AØÔ&­-×*>Ò*>Ñ*@Ô*@Ð@Ð@àˆ5r   Úbackendc                 óœ   — | €da dS t          | t          ¦  «        s.t          | ¦  «        st	          dt          | ¦  «        › �¦  «        ‚| a dS )z�Set the default backend used by torch.compile when no backend is explicitly specified.

    Pass None to reset to the default ("inductor").
    Nr   z*backend must be a string or callable, got )r   r-   r.   r$   Ú	TypeErrorr&   )rX   s    r   Úset_default_backendr[   Õ   s`   € ð €Ø%ÐØˆÝ�g�sÑ#Ô#ð V­H°WÑ,=Ô,=ð VÝÐTÅTÈ'Á]Ä]ÐTÐTÑUÔUÐUØÐÐÐr   c                  ó   — t           S )z5Return the current default backend for torch.compile.)r   r   r   r   Úget_default_backendr]   ã   s   € åÐr   )NNr   )r4   )r   N)(Ú__doc__r!   ÚloggingÚcollections.abcr   r   rN   r   Útypingr   r   r   r   Ú	getLoggerr   Úlogr
   ÚGraphModuleÚlistr   Ú
CompilerFnr   Údictr.   Ú__annotations__r   r   r#   r"   Úregister_debug_backendÚregister_experimental_backendr3   r?   Úcacher/   rF   ÚboolrW   r[   r]   r   r   r   ú<module>rm      sÜ  ðð:ð :ð :ðx Ð Ð Ð Ø €€€Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø )Ð )Ð )Ð )Ð )Ð )Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à €€€Ø Ð Ð Ð Ð Ð ð €gÔ˜Ñ!Ô!€ðMð Mð Mð Mð M�ñ Mô Mð Mð �r”~ t¨E¬LÔ'9Ð:¸JÐFÔG€
à*,€	ˆ4��Z $Ñ&Ð&Ô'Ð ,Ð ,Ñ ,Ø')€ˆt�C˜�OÔ$Ð )Ð )Ñ )Ø%/Ð �#˜
Ñ"Ð /Ð /Ñ /ð &*ØØðð Ø˜dÑ"ðà
�‰*ðð �3Œ-ðð ˆc�3ˆhÔð	ð ð ð ð> +˜Ô*Ð+;À*ÐMÑMÔMÐ Ø 1 	Ô 1ØÐ,ð!ñ !ô !Ð ð
  jÑ 0ð °Zð ð ð ð ð&ð ¸TÀ#¼Yð ð ð ð ð$ „ð$ð $ð $ñ „ð$ð „ð	9ð 	9ð 	9ñ „ð	9ð¨
ð °tð ð ð ð ð4  zÑ!1°DÑ!8ð ¸Tð ð ð ð ð˜S :Ñ-ð ð ð ð ð ð r   