Ë
    ÃÓ^jÒ  ã                   ó^  — 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j8                  ed¬«      Z ej8                  ed¬«      Zdeez  defd„Zddee   fd„Z ejB                  d d„«       Z"ejB                  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'y)!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                   óN   — e Zd Zdej                  deej                  df   fd„Zy)Ú
CompiledFnÚargsÚreturn.c                  ó   — y )N© )Úselfr   s     új/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/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   s!   „ ØL˜eŸl™lÐL¨u°U·\±\À3Ð5FÑ/GÔLr   r
   Ú	_BACKENDSÚ_COMPILER_FNSÚinductorÚ_default_backendÚcompiler_fnÚnameÚtagsr   .c                 ó&  — | €t        j                  t        ||¬«      S t        | «      st	        dt        | «      › �«      ‚|xs | 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Ü�KÔ ÜÐAÄ$À{ÓBSÐATÐUÓVÐVØÒ'�;×'Ñ'€DØŒ}ÑÜÐ/°¨vÐ6Ó7Ð7Øœ)Ñ#ØŒ	�$‰Ø%„M�$ÑÜ˜d›€KÔØÐr   )Údebug)r   )Úexperimentalc                 óæ   — t        | t        «      r`| t        vr
t        «        | t        vrddlm}  || ¬«      ‚| t        vr&t        |    }|�t        |j                  «       | ¬«       t        |    } | S )z#Expand backend strings to functionsé   )ÚInvalidBackend)r   )r   r   )	Ú
isinstanceÚstrr   Ú_lazy_importÚexcr,   r   r#   Úload)r   r,   Úentry_points      r   Úlookup_backendr3   z   sj   € ä�+œsÔ#ØœiÑ'ÜŒNØœiÑ'Ý,á  kÔ2Ð2àœmÑ+Ü# KÑ0ˆKØÐ&Ü ¨[×-=Ñ-=Ó-?ÀkÕRÜ# KÑ0ˆØÐr   c                 óÎ   — t        «        t        | xs d«      }t        D �cg c].  }|t        vs"|j	                  t        |   j
                  «      s|‘Œ0 }}t        |«      S c c}w )za
    Return valid strings that can be passed to:

        torch.compile(..., backend="name")
    r   )r/   Úsetr   r   Úintersectionr'   Úsorted)Úexclude_tagsÚexclude_tags_setr   Úbackendss       r   Úlist_backendsr;   �   sk   € ô „NÜ˜<Ò-¨2Ó.Ðô öàØ”}Ñ$Ø×,Ñ,¬]¸4Ñ-@×-FÑ-FÔGò 	ð€Hð ô �(ÓÐùòs   ¢3A"c                  óf   — ddl m}  ddlm}  || «       ddlm} |€t        d«      ‚t        «        y )Nr+   )r:   )Úimport_submodule)Údynamo_minifier_backendz&dynamo_minifier_backend failed to load)Ú r:   Úutilsr=   Úrepro.after_dynamor>   r%   Ú_discover_entrypoint_backends)r:   r=   r>   s      r   r/   r/   Ÿ   s+   € åÝ(á�XÔå<àÐ&ÜÐEÓFÐFä!Õ#r   c                  ó�   — ddl m}  d} | |¬«      }|j                  D �ci c]  }|||   “Œ
 }}|D ]  }||   t        |<   Œ y c c}w )Nr   )Úentry_pointsÚtorch_dynamo_backends)Úgroup)Úimportlib.metadatarD   Únamesr   )rD   Ú
group_nameÚepsr   Úeps_dictÚbackend_names         r   rB   rB   ®   sX   € õ 0à(€JÙ
˜ZÔ
(€Cà,/¯I©IÖ6 D��c˜$‘i‘Ð6€HÐ6Ø ò 9ˆØ"*¨<Ñ"8Œ	�,Òñ9ùò 7s    Ac                 óð   — t        «        | t        j                  «       v ryt        | d«      r| j                  }|t
        v s|t        v ryt        | d«      r | j                  t        j                  «       v S y)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ÚhasattrrN   r   r   )r   rN   s     r   Ú_is_registered_backendrQ   »   sq   € ô „Nð ”m×*Ñ*Ó,Ñ,Øô ˆ{˜OÔ,Ø#×1Ñ1ˆØœIÑ%¨¼-Ñ)GØô ˆ{˜MÔ*Ø×&Ñ&¬-×*>Ñ*>Ó*@Ð@Ð@àr   Úbackendc                 óv   — | €da yt        | t        «      s"t        | «      st	        dt        | «      › �«      ‚| a y)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&   )rR   s    r   Úset_default_backendrU   Õ   s@   € ð €Ø%ÐØÜ�gœsÔ#¬H°WÔ,=ÜÐDÄTÈ'Ã]ÀOÐTÓUÐUØÑr   c                  ó   — t         S )z5Return the current default backend for torch.compile.)r   r   r   r   Úget_default_backendrW   ã   s   € äÐr   )NNr   ))r(   r)   )r   N)(Ú__doc__r!   ÚloggingÚcollections.abcr   r   rG   r   Útypingr   r   r   r   Ú	getLoggerr   Úlogr
   ÚGraphModuleÚlistr   Ú
CompilerFnr   Údictr.   Ú__annotations__r   r   r#   r"   Úregister_debug_backendÚregister_experimental_backendr3   r;   Úcacher/   rB   ÚboolrQ   rU   rW   r   r   r   ú<module>rg      s·  ðò:óx Û ß .Ý )ß  ã Ý ð €g×Ñ˜Ó!€ôM�ô Mð �r—~‘~ t¨E¯L©LÑ'9Ð:¸JÐFÑG€
à*,€	ˆ4��Z $Ñ&Ð&Ñ'Ó ,Ø')€ˆt�C˜�OÑ$Ó )Ø%/Ð �#˜
Ñ"Ó /ð &*ØØñØ˜dÑ"ðà
�‰*ðð �3‰-ðð ˆc�3ˆhÑó	ð> +˜×*Ñ*Ð+;À*ÔMÐ Ø 1 	× 1Ñ 1ØÐ,ô!Ð ð
  jÑ 0ð °Zó ñ&¸TÀ#¹Yó ð$ ‡�ò$ó ð$ð ‡�ò	9ó ð	9ð¨
ð °tó ð4  zÑ!1°DÑ!8ð ¸Tó ð˜S :Ñ-ô r   