o
    oi                     @   sj   d dl mZmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ G dd deZdeded	d
fddZd
S )    )AnyCallableDictListOptional)override)MisconfigurationException)_register_classesc                   @   s   e Zd ZdZ			ddedee deded	ed
efddZ	e
ddedee d
efddZded
dfddZd
ee fddZd
efddZdS )_AcceleratorRegistryad  This class is a Registry that stores information about the Accelerators.

    The Accelerators are mapped to strings. These strings are names that identify
    an accelerator, e.g., "gpu". It also returns Optional description and
    parameters to initialize the Accelerator, which were defined during the
    registration.

    The motivation for having a AcceleratorRegistry is to make it convenient
    for the Users to try different accelerators by passing mapped aliases
    to the accelerator flag to the Trainer.

    Example::

        @AcceleratorRegistry.register("sota", description="Custom sota accelerator", a=1, b=True)
        class SOTAAccelerator(Accelerator):
            def __init__(self, a, b):
                ...

        or

        AcceleratorRegistry.register("sota", SOTAAccelerator, description="Custom sota accelerator", a=1, b=True)

    N Fnameacceleratordescriptionr   init_paramsreturnc                    s   |du st |tstd| |v r|std| di  | d< | d< dtdtd	tf fd
d}|dur?|||S |S )a  Registers a accelerator mapped to a name and with required metadata.

        Args:
            name : the name that identifies a accelerator, e.g. "gpu"
            accelerator : accelerator class
            description : accelerator description
            override : overrides the registered accelerator, if True
            init_params: parameters to initialize the accelerator

        Nz`name` must be a str, found 'z@' is already present in the registry. HINT: Use `override=True`.r   r   r   r   r   c                    s   | d< |  d<  | < |S )Nr   accelerator_name )r   r   dataselfr   Z/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/accelerators/registry.pydo_registerL   s   z2_AcceleratorRegistry.register.<locals>.do_register)
isinstancestr	TypeErrorr   r   )r   r   r   r   r   r   r   r   r   r   register/   s   
z_AcceleratorRegistry.registerdefaultc                 C   sN   || v r| | }|d di |d S |dur|S d}|   }t|||)zCalls the registered accelerator with the required parameters and returns the accelerator object.

        Args:
            name (str): the name that identifies a accelerator, e.g. "gpu"

        r   r   Nz/'{}' not found in registry. Available names: {}r   )available_acceleratorsKeyErrorformat)r   r   r   r   err_msgavailable_namesr   r   r   getW   s   z_AcceleratorRegistry.getc                 C   s   |  | dS )z+Removes the registered accelerator by name.N)pop)r   r   r   r   r   removej   s   z_AcceleratorRegistry.removec                 C   s   t |  S )z*Returns a list of registered accelerators.)listkeysr   r   r   r   r   n   s   z+_AcceleratorRegistry.available_acceleratorsc                 C   s   d d|  S )NzRegistered Accelerators: {}z, )r    joinr   r(   r   r   r   __str__r   s   z_AcceleratorRegistry.__str__)Nr   F)N)__name__
__module____qualname____doc__r   r   r   boolr   r   r   r#   r%   r   r   r*   r   r   r   r   r
      s.    
(r
   registrybase_moduler   Nc                 C   s0   ddl }||}ddlm} t| d|| dS )zLegacy.

    Do not use.

    r   N)Acceleratorregister_accelerators)	importlibimport_module)lightning.fabric.accelerators.acceleratorr2   r	   )r0   r1   r4   moduler2   r   r   r   call_register_acceleratorsv   s   
r8   )typingr   r   r   r   r   typing_extensionsr   %lightning.fabric.utilities.exceptionsr   #lightning.fabric.utilities.registryr	   dictr
   r   r8   r   r   r   r   <module>   s   `