a
    dY                     @   sr   d dl Z d dlmZmZ d dlmZmZmZmZm	Z	 d dl
mZ d dlmZ G dd deZeeddd	d
ZdS )    N)
getmembersisclass)AnyCallableDictListOptional)Strategy)_is_register_method_overriddenc                   @   sv   e Zd ZdZdeee ee eeedddZ	deee
 e
ddd	Zedd
ddZedddZed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descriptionoverrideinit_paramsreturnc                    s   du s t ts td v r<|s<td di  |durL|nd d< | d< ttd fd	d
}|dur||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   Zstrategy_name )r   datar   selfr   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/strategies/registry.pydo_registerJ   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)r   defaultr   c                 C   s`   || v r&| | }|d f i |d S |dur2|S d}d t|  pJd}t|||dS )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: {}, none)joinsortedkeysKeyErrorformat)r   r   r   r   err_msgZavailable_namesr   r   r   getU   s    z_StrategyRegistry.get)r   r   c                 C   s   |  | dS )z(Removes the registered strategy by name.N)pop)r   r   r   r   r   removef   s    z_StrategyRegistry.remove)r   c                 C   s   t |  S )z(Returns a list of registered strategies.)listr$   r   r   r   r   available_strategiesj   s    z&_StrategyRegistry.available_strategiesc                 C   s   d d|  S )NzRegistered Strategies: {}r    )r&   r"   r$   r,   r   r   r   __str__n   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   )registrybase_moduler   c                 C   sB   t |}t|tD ](\}}t|trt|tdr||  qd S )Nregister_strategies)	importlibimport_moduler   r   
issubclassr	   r
   r6   )r4   r5   module_modr   r   r   _call_register_strategiesr   s    
r=   )r7   inspectr   r   typingr   r   r   r   r   Z$lightning_fabric.strategies.strategyr	   Z#lightning_fabric.utilities.registryr
   dictr   r   r=   r   r   r   r   <module>   s   \