
    9i                     "   % S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
JrJrJr  SSKrSSKJr  \R                   " \5      r " S S\5      r\
\R(                  \\R,                     /\4   r0 r\\\\   4   \S	'   0 r\\\4   \S
'      SS\\   S\\   S\\   S\
S\	4   4S jjr\R<                  " \SS9r\R<                  " \SS9r S\\\4   S\4S jr!SS\\   4S jjr"\RF                  SS j5       r$\RF                  SS j5       r%g)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                   X    \ rS rSrS\R
                  S\\R
                  S4   4S jrSrg)
CompiledFnK   argsreturn.c                     g )N )selfr   s     _/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/backends/registry.py__call__CompiledFn.__call__L   s        r   N)	__name__
__module____qualname____firstlineno__torchTensortupler   __static_attributes__r   r   r   r   r   K   s!    LellLuU\\35F/GLr   r   	_BACKENDS_COMPILER_FNScompiler_fnnametagsr   .c                    U c  [         R                  " [        XS9$ [        U 5      (       d   eU=(       d    U R                  nU[
        ;  d
   SU 35       eU [        ;  a	  S[        U'   U [
        U'   [        U5      U l        U $ )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HHK    ';''D}$?(8&??$)#	$%M$dKr   )debug)r#   )experimentalc                     [        U [        5      (       aa  U [        ;  a
  [        5         U [        ;  a  SSKJn  U" U S9eU [        ;  a$  [        U    nUb  [        UR                  5       U S9  [        U    n U $ )z#Expand backend strings to functions   )InvalidBackend)r"   )r!   r"   )	
isinstancestrr   _lazy_importexcr.   r    r'   load)r!   r.   entry_points      r   lookup_backendr5   x   sm    +s##i'Ni', k22m+#K0K& [-=-=-?kR#K0r   c                    [        5         [        U =(       d    S5      n[        R                  5        Vs/ s H8  nU[        ;  d)  UR                  [        U   R                  5      (       a  M6  UPM:     nn[        U5      $ s  snf )zU
Return valid strings that can be passed to:

    torch.compile(..., backend="name")
r   )r1   setr   keysr    intersectionr)   sorted)exclude_tagsexclude_tags_setr"   backendss       r   list_backendsr>      su     N<-2. NN$$D}$,,]4-@-F-FG 	$   (s   5B.Bc                  V    SSK Jn   SSKJn  U" U 5        SSKJn  Uc   e[        5         g )Nr-   )r=   )import_submodule)dynamo_minifier_backend) r=   utilsr@   repro.after_dynamorA   _discover_entrypoint_backends)r=   r@   rA   s      r   r1   r1      s%    (X<"...!#r   c                     SSK Jn   Sn[        R                  S:  a/  U " 5       nX;   a  X!   O/ nU Vs0 s H  o3R                  U_M     nnO$U " US9nUR
                   Vs0 s H  oUX%   _M	     nnU H  nXF   [        U'   M     g s  snf s  snf )Nr   )entry_pointstorch_dynamo_backends)   
   )group)importlib.metadatarG   sysversion_infor"   namesr   )rG   
group_nameepsepeps_dictr"   backend_names          r   rE   rE      s     0(J
'!n!+!2co*-.#BGGRK#.,03		:	#)O	: "*"8	, !	 / ;s   B B
)NNr   ))r*   r+   )r   N)&__doc__r%   loggingrM   collections.abcr   rL   r   typingr   r   r   r   r	   r   r
   	getLoggerr   logr   GraphModulelistr   
CompilerFnr   dictr0   __annotations__r    r'   r&   register_debug_backendregister_experimental_backendr5   r>   cacher1   rE   r   r   r   <module>rc      sj  :x   
 $ ) ; ;   !M M r~~tELL'9:JFG
-/	4Xj))* /')tCO$ ) )-*%
3- 3- c3h	: #**+;*M  ) 1 1,! 
c:o 6 : &T#Y $ 
$ 
$ 9 9r   