
    *`i.                     T   U d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
 g dZ ee          Zeeeeef         f         ed<    ee          Zeeeeef         f         ed<    e	dd	
          ZdedefdZde
deee         f         de
deee         f         fdZ G d d	          Z	 	 	 ddededee         de
ee         edf         def
dZ	 d dedededefdZdedee         fdZdedee         fdZ	 d!dedede
eee         df         fdZdededefdZdedefdZdS )"zn
Universal registry to support registration and loading of child classes and plugins
of neuralmagic utilities
    N)defaultdict)AnyDictListOptionalTypeVarUnion)RegistryMixinregisterget_from_registryregistered_namesregistered_aliasesstandardize_lookup_name_ALIAS_REGISTRY	_REGISTRY r
   )boundnamereturnc                 z    |                      dd                               dd                                          S )aD  
    Standardize the given name for lookup in the registry.
    This will replace all underscores and spaces with hyphens and
    convert the name to lowercase.

    example:
    ```
    standardize_lookup_name("Foo_bar baz") == "foo-bar-baz"
    ```

    :param name: name to standardize
    :return: standardized name
    _- )replacelowerr   s    x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/compressed_tensors/registry/registry.pyr   r   (   s4     <<S!!))#s3399;;;    c                 j    | d S t          | t                    rt          |           S d | D             S )Nc                 ,    g | ]}t          |          S  )r   ).0ns     r   
<listcomp>z*standardize_alias_name.<locals>.<listcomp>A   s!    999q'**999r   )
isinstancestrr   r   s    r   standardize_alias_namer'   9   sB     |t	D#		 :&t,,,99D9999r   c                   f   e Zd ZU dZdZeed<   e	 ddee	         de
ee	         e	df         fd            Ze	 ddede	de
e	ee	         df         fd	            Zed
ee         de	defd            Zed
ee         de	defd            Zedee	         fd            Zedee	         fd            ZdS )r
   a  
    Universal registry to support registration and loading of child classes and plugins
    of neuralmagic utilities.

    Classes that require a registry or plugins may add the `RegistryMixin` and use
    `register` and `load` as the main entrypoints for adding new implementations and
    loading requested values from its registry.

    If a class should only have its child classes in its registry, the class should
    set the static attribute `registry_requires_subclass` to True

    example
    ```python
    class Dataset(RegistryMixin):
        pass


    # register with default name
    @Dataset.register()
    class ImageNetDataset(Dataset):
        pass

    # load as "ImageNetDataset"
    imagenet = Dataset.load("ImageNetDataset")

    # register with custom name
    @Dataset.register(name="cifar-dataset")
    class Cifar(Dataset):
        pass

    Note: the name will be standardized for lookup in the registry.
    For example, if a class is registered as "cifar_dataset" or
    "cifar dataset", it will be stored as "cifar-dataset". The user
    will be able to load the class with any of the three name variants.

    # register with multiple aliases
    @Dataset.register(alias=["cifar-10-dataset", "cifar_100_dataset"])
    class Cifar(Dataset):
        pass

    # load as "cifar-dataset"
    cifar = Dataset.load_from_registry("cifar-dataset")

    # load from custom file that implements a dataset
    mnist = Dataset.load_from_registry("/path/to/mnnist_dataset.py:MnistDataset")
    ```
    Fregistry_requires_subclassNr   aliasc                 *     dt           f fd}|S )a  
        Decorator for registering a value (ie class or function) wrapped by this
        decorator to the base class (class that .register is called from)

        :param name: name or list of names to register the wrapped value as,
            defaults to value.__name__
        :param alias: alias or list of aliases to register the wrapped value as,
            defaults to None
        :return: register decorator
        valuec                 8                         |            | S )N)r   r*   )register_value)r,   r*   clsr   s    r   	decoratorz)RegistryMixin.register.<locals>.decorator   s#    u4u===Lr   )r   )r/   r   r*   r0   s   ``` r   r   zRegistryMixin.registery   s<    	S 	 	 	 	 	 	 	 	 r   r,   c                 8    t          | |||| j                   dS )aR  
        Registers the given value to the class `.register_value` is called from
        :param value: value to register
        :param name: name to register the wrapped value as,
            defaults to value.__name__
        :param alias: alias or list of aliases to register the wrapped value as,
            defaults to None
        )parent_classr,   r   r*   require_subclassN)r   r)   )r/   r,   r   r*   s       r   r.   zRegistryMixin.register_value   s7     	 ;	
 	
 	
 	
 	
 	
r   r/   r   c                 >    |                      |          } |di |S )an  
        :param name: name of registered class to load
        :param constructor_kwargs: arguments to pass to the constructor retrieved
            from the registry
        :return: loaded object registered to this class under the given name,
            constructed with the given kwargs. Raises error if the name is
            not found in the registry
        r   r!   )get_value_from_registry)r/   r   constructor_kwargsconstructors       r   load_from_registryz RegistryMixin.load_from_registry   s1     11t1<<{00/000r   c                 0    t          | || j                  S )z
        :param name: name to retrieve from the registry
        :return: value from retrieved the registry for the given name, raises
            error if not found
        )r2   r   r3   )r   r)   )r/   r   s     r   r5   z%RegistryMixin.get_value_from_registry   s'     ! ;
 
 
 	
r   c                      t          |           S )zE
        :return: list of all names registered to this class
        )r   r/   s    r   r   zRegistryMixin.registered_names   s    
  $$$r   c                      t          |           S )zG
        :return: list of all aliases registered to this class
        )r   r;   s    r   r   z RegistryMixin.registered_aliases   s    
 "#&&&r   )NNN)__name__
__module____qualname____doc__r)   bool__annotations__classmethodr   r&   r	   r   r   r   r.   typeTr8   r5   r   r   r!   r   r   r
   r
   D   s        . .d (-,,,NR C=05d3id6J0K   [( IM
 

"
+0d3i1E+F
 
 
 [
& 
1Q 
1s 
1Q 
1 
1 
1 [
1 

T!W 

C 

A 

 

 

 [

 %c % % % [% '49 ' ' ' [' ' 'r   Fr2   r,   r*   r3   c           
      B   ||j         }t          |          }t          |          }t          |||            |rt	          | |           |t
          |          v r4t
          |          |         }||urt          d| d| d| d|           dS |t
          |          |<   dS )a  
    :param parent_class: class to register the name under
    :param value: the value to register
    :param name: name to register the wrapped value as, defaults to value.__name__
    :param alias: alias or list of aliases to register the wrapped value as,
        defaults to None
    :param require_subclass: require that value is a subclass of the class this
        method is called from
    N)r   r*   r2   zAttempting to register name  as 	 however   has already been registered as )r>   r   r'   register_alias_validate_subclassr   RuntimeError)r2   r,   r   r*   r3   registered_values         r   r   r      s      |~"4((D"5))EEEEEE 0<///y&&& %\2485((Tt T T T TT TAQT T   )( ).	,%%%r   c                    t          |          }d|v r)|                    d          \  }}t          ||          }n~t          |                              ||          }t
          |                              |          }|;t          d| d|  d|  dt          |            d|  dt          |                      |rt          | |           |S )aZ  
    :param parent_class: class that the name is registered under
    :param name: name to retrieve from the registry of the class
    :param require_subclass: require that value is a subclass of the class this
        method is called from
    :return: value from retrieved the registry for the given name, raises
        error if not found
    :NzUnable to find z registered under type z.
Registered values for z: z
Registered aliases for )
r   split!_import_and_get_value_from_moduler   getr   KeyErrorr   r   rL   )r2   r   r3   module_path
value_nameretrieved_values         r   r   r      s    #4((D
d{{"&**S//Z;KTT |,00t<<#L155d;;"6$ 6 6| 6 6)56 6#L116 6 +76 6 &l33	6 6    :<999r   c                 Z    t          t          |                                                    S )zt
    :param parent_class: class to look up the registry of
    :return: all names registered to the given class
    )listr   keys)r2   s    r   r   r     s#    
 	,',,..///r   c                     t          t          |                                                    }t          t          |          t          t	          |                     z
            }|S )zv
    :param parent_class: class to look up the registry of
    :return: all aliases registered to the given class
    )rY   r   rZ   setr   )r2   registered_aliases_plus_namesr   s      r   r   r      s]    
 %))F)K)K)M)M$N$N!)**S1A,1O1O-P-PP  r   c                 6   |t          |t                    r|n|g}ng }| |v rt          d|  d|  d          |                    |            |D ]G}|t          |         v r&t          d| d|  d| dt          |                    | t          |         |<   HdS )aX  
    Updates the mapping from the alias(es) to the given name.
    If the alias is None, the name is used as the alias.
    ```

    :param name: name that the alias refers to
    :param parent_class: class that the name is registered under
    :param alias: single alias or list of aliases that
        refer to the name, defaults to None
    NzAttempting to register alias z., that is identical to the standardized name: .rH   rI   rJ   )r%   rY   rT   appendr   )r   r2   r*   
alias_names       r   rK   rK   ,  s    #E400=ugu}}CD C C;?C C C
 
 	
 
LL 9 9
6661
 1 1 1 1%1 1":.1 1  
 59%j119 9r   rU   rV   c                    t           j                            d| |           }t           j                            |          }|j                            |           t          ||d           }|st          d| d|            |S )Nplugin_module_for_zUnable to find attribute z in module )	importlibutilspec_from_file_locationmodule_from_specloaderexec_modulegetattrrM   )rU   rV   specmoduler,   s        r   rR   rR   O  s    
 >11)Z)); D ^,,T22FKF### FJ--E 
L
LL{LL
 
 	
 Lr   child_classc                 R    t          ||           st          d| d|  d          d S )Nzclass z6 is not a subclass of the class it is registered for: r_   )
issubclass
ValueError)r2   rm   s     r   rL   rL   d  sS    k<00 
/[ / /+/ / /
 
 	

 
r   )NNF)Fr=   )rA   rd   collectionsr   typingr   r   r   r   r   r	   __all__dictr   rE   r&   rC   r   rF   r   r'   r
   rB   r   r   r   r   rK   rR   rL   r!   r   r   <module>ru      s    
     # # # # # # < < < < < < < < < < < < < < < <   /:k$.?.?dDcN*+ ? ? ?(3D(9(9	4d38n$% 9 9 9GBo&&&<# <# < < < <":
c49$
%:
4d3i : : : :D' D' D' D' D' D' D' D'T )-"%. %.%.%. 3-%. cC%&	%.
 %. %. %. %.R =B" ""!"59"" " " "J04 0DI 0 0 0 0	T 	d3i 	 	 	 	 IM 9  9
 9! 9*/T#Y0D*E 9  9  9  9F3 C C    *
T 
 
 
 
 
 
 
r   