a
    d                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlZddlmZ ddlmZ ddlmZ dd	lmZ G d
d deeZG dd deZdejfee ee e	ee gef e
dddZdS )z.Abstract base class used to build new loggers.    N)ABC)defaultdict)AnyCallableDictMappingOptionalSequence)Logger)_DummyExperiment)rank_zero_experiment)ModelCheckpointc                   @   s6   e Zd ZdZeddddZeee dddZ	dS )	r
   z"Base class for experiment loggers.N)checkpoint_callbackreturnc                 C   s   dS )zCalled after model checkpoint callback saves a new checkpoint.

        Args:
            checkpoint_callback: the model checkpoint callback instance
        N )selfr   r   r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pytorch_lightning/loggers/logger.pyafter_save_checkpoint"   s    zLogger.after_save_checkpointr   c                 C   s   dS )zvReturn the root directory where experiment logs get saved, or `None` if the logger does not save data
        locally.Nr   r   r   r   r   save_dir*   s    zLogger.save_dir)
__name__
__module____qualname____doc__r   r   propertyr   strr   r   r   r   r   r
      s   r
   c                       s   e Zd ZdZdd fddZeedddZeeddd	d
Z	eeddddZ
eedddZeedddZed dddZeedddZ  ZS )DummyLoggerzDummy logger for internal use.

    It is useful if we want to disable user's logger for a feature, but still ensure that user code can run
    Nr   c                    s   t    t | _d S N)super__init__DummyExperiment_experimentr   	__class__r   r   r    7   s    
zDummyLogger.__init__c                 C   s   | j S )z9Return the experiment object associated with this logger.)r"   r   r   r   r   
experiment;   s    zDummyLogger.experimentargskwargsr   c                 O   s   d S r   r   r   r'   r(   r   r   r   log_metrics@   s    zDummyLogger.log_metricsc                 O   s   d S r   r   r)   r   r   r   log_hyperparamsC   s    zDummyLogger.log_hyperparamsc                 C   s   dS )zReturn the experiment name. r   r   r   r   r   nameF   s    zDummyLogger.namec                 C   s   dS )zReturn the experiment version.r,   r   r   r   r   r   versionK   s    zDummyLogger.version)idxr   c                 C   s   | S r   r   )r   r/   r   r   r   __getitem__P   s    zDummyLogger.__getitem__)r-   r   c                 C   s   t t dddd}|S )zUAllows the DummyLogger to be called with arbitrary methods, to avoid AttributeErrors.Nr&   c                  _   s   d S r   r   )r'   r(   r   r   r   methodW   s    z'DummyLogger.__getattr__.<locals>.method)r   )r   r-   r1   r   r   r   __getattr__T   s    zDummyLogger.__getattr__)r   r   r   r   r    r   r!   r%   r   r*   r+   r   r-   r.   intr0   r   r2   __classcell__r   r   r#   r   r   1   s   r   )dictsagg_key_funcsdefault_funcr   c                    s   |pi }t ttjdd | D }tt}|D ]Z | }dd  fdd| D D }t|d trzt	|||| < q0|p||| < q0t|S )a  Merge a sequence with dictionaries into one dictionary by aggregating the same keys with some given
    function.

    Args:
        dicts:
            Sequence of dictionaries to be merged.
        agg_key_funcs:
            Mapping from key name to function. This function will aggregate a
            list of values, obtained from the same key of all dictionaries.
            If some key has no specified aggregation function, the default one
            will be used. Default is: ``None`` (all keys will be aggregated by the
            default function).
        default_func:
            Default function to aggregate keys, which are not presented in the
            `agg_key_funcs` map.

    Returns:
        Dictionary with merged values.

    Examples:
        >>> import pprint
        >>> d1 = {'a': 1.7, 'b': 2.0, 'c': 1, 'd': {'d1': 1, 'd3': 3}}
        >>> d2 = {'a': 1.1, 'b': 2.2, 'v': 1, 'd': {'d1': 2, 'd2': 3}}
        >>> d3 = {'a': 1.1, 'v': 2.3, 'd': {'d3': 3, 'd4': {'d5': 1}}}
        >>> dflt_func = min
        >>> agg_funcs = {'a': np.mean, 'v': max, 'd': {'d1': sum}}
        >>> pprint.pprint(merge_dicts([d1, d2, d3], agg_funcs, dflt_func))
        {'a': 1.3,
         'b': 2.0,
         'c': 1,
         'd': {'d1': 3, 'd2': 3, 'd3': 3, 'd4': {'d5': 1}},
         'v': 2.3}
    c                 S   s   g | ]}t | qS r   )setkeys).0dr   r   r   
<listcomp>       zmerge_dicts.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS r   r   )r:   vr   r   r   r<      r=   c                    s   g | ]}|  qS r   )get)r:   Zd_inkr   r   r<      r=   r   )
list	functoolsreduceoperatoror_r   dictr?   
isinstancemerge_dicts)r5   r6   r7   r9   Zd_outfnZvalues_to_aggr   r@   r   rI   ^   s    &
rI   )r   rC   rE   abcr   collectionsr   typingr   r   r   r   r   r	   numpynpZlightning_fabric.loggersr
   ZFabricLoggerZlightning_fabric.loggers.loggerr   r!   r   Z,pytorch_lightning.callbacks.model_checkpointr   r   meanfloatrI   r   r   r   r   <module>   s(    /