
    %Vji                     B   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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         de
de	f         fdZ ej        ed          Z ej        ed          Z deeef         defdZ!ddee         fdZ"ej#        dd            Z$ej#        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)AnyCallableOptionalProtocolUnion)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     Y/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_dynamo/backends/registry.py__call__zCompiledFn.__call__L   s          N)__name__
__module____qualname__torchTensortupler   r   r   r   r   r   K   s4        LelLuU\35F/GLLLLLLr   r   	_BACKENDS_COMPILER_FNSr   compiler_fnnametagsr   .c                    | t          j        t          ||          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   s      r   r#   r#   U   s       !14HHHHK     ';'D}$$$&?&?&?$$$)##	$%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_backendr2   x   s    +s## 1i''NNNi'',,,,,, .k2222m++#K0K& [-=-=-?-?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   keyssorted)exclude_tagsbackendsr8   s     @r   list_backendsr?      sc     NNN<-2..   NN$$  H (r   c                  d    ddl m}  ddlm}  ||            ddlm} |J t                       d S )Nr)   )r>   )import_submodule)dynamo_minifier_backend) r>   utilsrA   repro.after_dynamorB   _discover_entrypoint_backends)r>   rA   rB   s      r   r.   r.      sg    ((((((X<<<<<<"...!#####r   c                      ddl m}  d}t          j        dk     r% |             |v r|         ng d D             }n | |          fdj        D             }|D ]}||         t
          |<   d S )Nr   )entry_pointstorch_dynamo_backends)   
   c                     i | ]
}|j         |S r   r+   )r7   eps     r   
<dictcomp>z1_discover_entrypoint_backends.<locals>.<dictcomp>   s    ...BBGR...r   )groupc                 "    i | ]}||         S r   r   )r7   r   epss     r   rN   z1_discover_entrypoint_backends.<locals>.<dictcomp>   s    :::D#d):::r   )importlib.metadatarH   sysversion_infonamesr   )rH   
group_nameeps_dictbackend_namerQ   s       @r   rF   rF      s     0/////(J
'!!lnn!+s!2!2c*oo..#...l,,,::::	:::  9 9"*<"8	,9 9r   )NNr   )r3   )r   N)&__doc__r!   loggingrS   collections.abcr   rR   r   typingr   r   r   r   r	   r   r
   	getLoggerr   logr   GraphModulelistr   
CompilerFnr   dictr-   __annotations__r   r#   r"   register_debug_backendregister_experimental_backendr2   r?   cacher.   rF   r   r   r   <module>rg      sq  : : :x      



 $ $ $ $ $ $ ) ) ) ) ) ) ; ; ; ; ; ; ; ; ; ; ; ; ; ;        g!!M M M M M M M M r~tEL'9:JFG
-/	4Xj))* / / /')tCO$ ) ) ) )- *%
3- 3- c3h	   : +*+;*MMM  1	 1,! ! ! 
c:o 6 :    & T#Y    $ 
$ 
$ 
$ 
$ 9 9 9 9 9 9r   