Ë
    g^(hF  ã                   ó
  — U d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	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
e   f   ed	<   i Zeeef   ed
<   	 	 	 dde
e   de
e   dee   fd„Z ej8                  ed¬«      Z ej8                  ed¬«      Zd„ Zddee   fd„Z  ejB                  d«      d„ «       Z" ejB                  d«      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)ÚSequence)Ú
EntryPoint)ÚCallableÚOptionalÚ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     ú]/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/_dynamo/backends/registry.pyÚ__call__zCompiledFn.__call__N   s   � ó    N)Ú__name__Ú
__module__Ú__qualname__ÚtorchÚTensorÚtupler   r   r   r   r
   r
   M   s!   „ ØL˜eŸl™lÐL¨u°U·\±\À3Ð5FÑ/GÔLr   r
   Ú	_BACKENDSÚ_COMPILER_FNSÚcompiler_fnÚnameÚtagsc                 óô   — | €t        j                  t        ||¬«      S t        | «      sJ ‚|xs | j                  }|t
        vs
J 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duplicate name: )	Ú	functoolsÚpartialÚregister_backendÚcallabler   r   r   r   Ú_tags)r   r   r   s      r   r!   r!   W   s†   € ð  Ðä× Ñ Ô!1¸À4ÔHÐHÜ�KÔ Ð Ð ØÒ'�;×'Ñ'€DØ”}Ñ$Ð?Ð(8¸¸Ð&?Ó?Ð$Øœ)Ñ#ØŒ	�$‰Ø%„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_backendr/   z   sd   € ä�+œsÔ#ØœiÑ'ÜŒNØœiÑ'Ý,á  kÔ2Ð2àœmÑ+Ü# KÑ0ˆKÜ¨×)9Ñ)9Ó);À+ÕNÜ# KÑ0ˆØÐr   r   c                 óê   — t        «        t        | xs d«      } t        j                  «       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   Úkeysr   Úintersectionr#   Úsorted)Úexclude_tagsr   Úbackendss      r   Úlist_backendsr7   ‹   sq   € ô „NÜ�|Ò) rÓ*€Lô —N‘NÓ$öàØ”}Ñ$Ø×(Ñ(¬°tÑ)<×)BÑ)BÔCò 	ð€Hð ô �(ÓÐùòs   °3A0c                  óT   — ddl m}  ddlm}  || «       ddlm} |€J ‚t        «        y )Nr'   )r6   )Úimport_submodule)Údynamo_minifier_backend)Ú r6   Úutilsr9   Úrepro.after_dynamor:   Ú_discover_entrypoint_backends)r6   r9   r:   s      r   r+   r+   �   s%   € åÝ(á�XÔå<à"Ð.Ð.Ð.ä!Õ#r   c                  ó  — ddl m}  d}t        j                  dk  r- | «       }||v r||   ng }|D �ci c]  }|j                  |“Œ }}n& | |¬«      }|j
                  D �ci c]  }|||   “Œ
 }}|D ]  }||   t        |<   Œ y c c}w c c}w )Nr   )Úentry_pointsÚtorch_dynamo_backends)é   é
   )Úgroup)Úimportlib.metadatar@   ÚsysÚversion_infor   Únamesr   )r@   Ú
group_nameÚepsÚepr   Úbackend_names         r   r>   r>   «   sŸ   € õ 0à(€JÜ
×Ñ˜'Ò!Ù‹nˆØ!+¨sÑ!2ˆc�*Šo¸ˆØ%(Ö)˜rˆr�w‰w˜‰{Ð)ˆÑ)á Ô,ˆØ+.¯9©9Ö5 4ˆt�S˜‘Y‰Ð5ˆÐ5Øò 4ˆØ"% lÑ"3Œ	�,Òñ4ùò	 *ùò 6s   ²BÁ B)NNr   ))r$   r%   )$Ú__doc__r   ÚloggingrF   Úcollections.abcr   rE   r   Útypingr   r   r   r   r   Ú	getLoggerr   Úlogr
   ÚGraphModuleÚlistr   Ú
CompilerFnr   Údictr*   Ú__annotations__r   r!   r    Úregister_debug_backendÚregister_experimental_backendr/   r7   Ú	lru_cacher+   r>   r   r   r   ú<module>r[      sX  ðò:óx Û Û 
Ý $Ý )ß /Ñ /ã Ý ð €g×Ñ˜Ó!€ôM�ô Mð �r—~‘~ t¨E¯L©LÑ'9Ð:¸JÐFÑG€
à-/€	ˆ4��X˜jÑ)Ð)Ñ*Ó /Ø')€ˆt�C˜�OÑ$Ó )ð )-ØØñØ˜*Ñ%ðà
�3‰-ðð �3‰-óð: +˜×*Ñ*Ð+;À*ÔMÐ Ø 1 	× 1Ñ 1ØÐ,ô!Ð ò
ñ"¸TÀ#¹Yó ð$ €×Ñ�TÓñ
$ó ð
$ð €×Ñ�TÓñ4ó ñ4r   