o
    -wi7                     @   st   d dl mZmZmZmZmZ d dlZd dlm	Z	 d dl
Z
d dlmZ d dlmZ ed Ze  G dd de	jZdS )	    )AnyDictLiteralOptionalUnionN)	callbacks)patch_tf_keras)	telemetry)epochbatchc                       s   e Zd ZdZ		ddeeef dedededd	f
 fd
dZdee	d	f fddZ
ddedeeeef  dd	fddZddedeeeef  dd	fddZ		ddedeeeef  dd	fddZ  ZS )WandbMetricsLoggerat  Logger that sends system metrics to W&B.

    `WandbMetricsLogger` automatically logs the `logs` dictionary that callback methods
    take as argument to wandb.

    This callback automatically logs the following to a W&B run page:
    * system (CPU/GPU/TPU) metrics,
    * train and validation metrics defined in `model.compile`,
    * learning rate (both for a fixed value or a learning rate scheduler)

    Notes:
    If you resume training by passing `initial_epoch` to `model.fit` and you are using a
    learning rate scheduler, make sure to pass `initial_global_step` to
    `WandbMetricsLogger`. The `initial_global_step` is `step_size * initial_step`, where
    `step_size` is number of training steps per epoch. `step_size` can be calculated as
    the product of the cardinality of the training dataset and the batch size.

    Args:
        log_freq: ("epoch", "batch", or int) if "epoch", logs metrics
            at the end of each epoch. If "batch", logs metrics at the end
            of each batch. If an integer, logs metrics at the end of that
            many batches. Defaults to "epoch".
        initial_global_step: (int) Use this argument to correctly log the
            learning rate when you resume training from some `initial_epoch`,
            and a learning rate scheduler is used. This can be computed as
            `step_size * initial_step`. Defaults to 0.
    r
   r   log_freqinitial_global_stepargskwargsreturnNc                    s   t  j|i | tjd u rtdtjtjd}d|j_W d    n1 s)w   Y  |dkr4d}t	|t
| _| jr?|nd | _d| _|| _| jrYtd tjddd	 d S td
 tjdd
d	 d S )Nz8You must call `wandb.init()` before WandbMetricsLogger())runTr      r   batch/batch_stepzbatch/*)step_metricepoch/epochzepoch/*)super__init__wandbr   Errorr	   contextfeaturekeras_metrics_logger
isinstanceintlogging_batch_wiser   global_batchglobal_stepdefine_metric)selfr   r   r   r   tel	__class__ m/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/integration/keras/callbacks/metrics_logger.pyr   -   s&   



zWandbMetricsLogger.__init__c              
   C   s   t | jjjtjtjfst| jjjdr'| jjjjdkr't	| jjj
  S zt	| jjj| jd
  W S  tyT } ztjd| dd W Y d }~d S d }~ww )Nshaper(   )stepzUnable to log learning rate: F)repeat)r   model	optimizerlearning_ratetfVariableTensorhasattrr*   floatnumpyitemr"   	Exceptionr   	termerror)r$   er(   r(   r)   _get_lrQ   s"   
zWandbMetricsLogger._get_lrlogsc                 C   sN   |du rt  ndd | D }||d< |  }|dur ||d< t| dS )zCalled at the end of an epoch.Nc                 S      i | ]
\}}d | |qS )zepoch/r(   .0kvr(   r(   r)   
<dictcomp>d       z3WandbMetricsLogger.on_epoch_end.<locals>.<dictcomp>r   zepoch/learning_rate)dictitemsr:   r   log)r$   r
   r;   lrr(   r(   r)   on_epoch_endb   s    zWandbMetricsLogger.on_epoch_endr   c                 C   s   |  j d7  _ 	 | jr?|| j dkrA|rdd | D ni }| j|d< |  }|d ur0||d< t| |  j| j7  _d S d S d S )Nr   r   c                 S   r<   )zbatch/r(   r=   r(   r(   r)   rA   r   rB   z3WandbMetricsLogger.on_batch_end.<locals>.<dictcomp>r   zbatch/learning_rate)r"   r    r   rD   r!   r:   r   rE   )r$   r   r;   rF   r(   r(   r)   on_batch_endn   s   

zWandbMetricsLogger.on_batch_endc                 C   s   |  ||r
| dS i  dS )z7Called at the end of a training batch in `fit` methods.N)rH   )r$   r   r;   r(   r(   r)   on_train_batch_end}   s   z%WandbMetricsLogger.on_train_batch_end)r
   r   )N)__name__
__module____qualname____doc__r   LogStrategyr   r   r   r4   r:   r   r   strrG   rH   rI   __classcell__r(   r(   r&   r)   r      s4    
$$$r   )typingr   r   r   r   r   
tensorflowr0   tensorflow.kerasr   r   wandb.integration.keras.kerasr   wandb.sdk.libr	   rN   Callbackr   r(   r(   r(   r)   <module>   s    