o
    ziU                     @   s<   d dl mZmZmZmZmZ d dlmZ G dd deZ	dS )    )AnyCallableDictListOptional)overridec                   @   s   e Zd ZdZ			ddedee de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fddZd	efddZdS )_StrategyRegistrya>  This class is a Registry that stores information about the Training Strategies.

    The Strategies are mapped to strings. These strings are names that identify
    a strategy, e.g., "deepspeed". It also returns Optional description and
    parameters to initialize the Strategy, which were defined durng the
    registration.

    The motivation for having a StrategyRegistry is to make it convenient
    for the Users to try different Strategies by passing just strings
    to the strategy flag to the Trainer.

    Example::

        @StrategyRegistry.register("lightning", description="Super fast", a=1, b=True)
        class LightningStrategy:
            def __init__(self, a, b):
                ...

        or

        StrategyRegistry.register("lightning", LightningStrategy, description="Super fast", a=1, b=True)

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

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

        Nz`name` must be a str, found 'z@' is already present in the registry. HINT: Use `override=True`. r   r   r
   r   c                    s   |  d<  d<  < | S )Nr
   strategy_name )r
   datar	   selfr   X/home/ubuntu/.local/lib/python3.10/site-packages/lightning_fabric/strategies/registry.pydo_registerI   s   z/_StrategyRegistry.register.<locals>.do_register)
isinstancestr	TypeError
ValueErrorr   )r   r	   r
   r   r   r   r   r   r   r   register,   s   z_StrategyRegistry.registerdefaultc                 C   s\   || v r| | }|d di |d S |dur|S d}d t|  p%d}t|||)zCalls the registered strategy with the required parameters and returns the strategy object.

        Args:
            name (str): the name that identifies a strategy, e.g. "deepspeed_stage_3"

        r
   r   Nz/'{}' not found in registry. Available names: {}, noner   )joinsortedkeysKeyErrorformat)r   r	   r   r   err_msgavailable_namesr   r   r   getT   s   z_StrategyRegistry.getc                 C   s   |  | dS )z(Removes the registered strategy by name.N)pop)r   r	   r   r   r   removeg   s   z_StrategyRegistry.removec                 C   s   t |  S )z(Returns a list of registered strategies.)listr!   r   r   r   r   available_strategiesk   s   z&_StrategyRegistry.available_strategiesc                 C   s   d d|  S )NzRegistered Strategies: {}r   )r#   r   r!   r*   r   r   r   __str__o   s   z_StrategyRegistry.__str__)NNF)N)__name__
__module____qualname____doc__r   r   r   boolr   r   r   r&   r(   r   r+   r,   r   r   r   r   r      s.    
(r   N)
typingr   r   r   r   r   typing_extensionsr   dictr   r   r   r   r   <module>   s   