o
    ZhF                     @   sF  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eZG d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dZejeddZejeddZdd Zddee fddZ e!ddd Z"e!dd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)Sequence)
EntryPoint)CallableOptionalProtocol)fxc                   @   s*   e Zd Zdejdeejdf fddZdS )
CompiledFnargsreturn.c                 G   s   d S )N )selfr	   r   r   N/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/backends/registry.py__call__N   s    zCompiledFn.__call__N)__name__
__module____qualname__torchTensortupler   r   r   r   r   r   M   s    "r   	_BACKENDS_COMPILER_FNSr   compiler_fnnametagsc                 C   sj   | du rt jt||dS t| sJ |p| 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   r   r   r   r   W   s   

r   )debug)r   )experimentalc                 C   sb   t | tr/| tvrt  | tvrddlm} || d| tvr+t|  }t| | d t|  } | S )z#Expand backend strings to functions   )InvalidBackendr   )r   r   )	
isinstancestrr   _lazy_importexcr"   r   r   load)r   r"   entry_pointr   r   r   lookup_backendz   s   

r*   r   r    r
   c                    s0   t   t pd  fddt D }t|S )za
    Return valid strings that can be passed to:

        torch.compile(..., backend="name")
    r   c                    s(   g | ]}|t vs t | js|qS r   )r   intersectionr   .0r   exclude_tagsr   r   
<listcomp>   s    z!list_backends.<locals>.<listcomp>)r&   setr   keyssorted)r0   backendsr   r/   r   list_backends   s   
r6   c                  C   sB   ddl m}  ddlm} ||  ddlm} |d usJ t  d S )Nr!   )r5   )import_submodule)dynamo_minifier_backend) r5   utilsr7   Zrepro.after_dynamor8   _discover_entrypoint_backends)r5   r7   r8   r   r   r   r&      s   
r&   c                     s|   ddl m}  d}tjdk r"|   | v r | ng  dd  D  n| |d  fdd jD   D ]} | t|< q3d S )	Nr   )entry_pointsZtorch_dynamo_backends)   
   c                 S   s   i | ]}|j |qS r   r#   )r.   epr   r   r   
<dictcomp>   s    z1_discover_entrypoint_backends.<locals>.<dictcomp>)groupc                    s   i | ]}| | qS r   r   r-   Zepsr   r   r@      s    )importlib.metadatar<   sysversion_infonamesr   )r<   
group_namebackend_namer   rB   r   r;      s   

r;   )NNr   )r+   )$__doc__r   loggingrD   collections.abcr   rC   r   typingr   r   r   r   r   	getLoggerr   logr   ZGraphModulelistr   Z
CompilerFnr   dictr%   __annotations__r   r   r   Zregister_debug_backendZregister_experimental_backendr*   r6   	lru_cacher&   r;   r   r   r   r   <module>   sD   <


