o
    ni)                      @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlZd dlZd dlmZ d dlmZ eeZeeeef  Ze
dedZe
dZG dd	 d	eeef ZG d
d deZG dd dZG dd dZdS )    N)AnyDictOptionalProtocolSequenceTypeVar)	telemetry)TimerK)boundVc                   @   s@   e Zd ZdedefddZ	d	dedee dee fddZdS )
Responsekeyreturnc                 C      d S N )selfr   r   r   f/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/integration_utils/auto_logging.py__getitem__   s    zResponse.__getitem__Ndefaultc                 C   r   r   r   )r   r   r   r   r   r   get   s   zResponse.getr   )__name__
__module____qualname__r
   r   r   r   r   r   r   r   r   r      s    r   c                   @   sF   e Zd Zdee deeef dededede	eeef  fddZ
d	S )
ArgumentResponseResolverargskwargsresponse
start_timetime_elapsedr   c                 C   r   r   r   )r   r   r   r   r   r    r   r   r   __call__   s   z!ArgumentResponseResolver.__call__N)r   r   r   r   r   r   strr   floatr   r!   r   r   r   r   r      s    
r   c                   @   sP   e Zd Zdedee deddfddZedefdd	Z	dddZ
dddZdS )PatchAPInamesymbolsresolverr   Nc                 C   s"   || _ d| _i | _|| _|| _dS )z.Patches the API to log wandb Media or metrics.N)r%   _apioriginal_methodsr&   r'   )r   r%   r&   r'   r   r   r   __init__*   s
   
zPatchAPI.__init__c              	   C   sD   | j  }| jdu rtjj|d| j  d| d| ddd| _| jS )zReturns the API module.NzTo use the W&B z  Autolog, you need to have the `z@` python package installed. Please install it with `pip install z`.F)r%   requiredlazy)r%   lowerr(   wandbutil
get_module)r   lib_namer   r   r   set_api<   s   


zPatchAPI.set_apirunwandb.sdk.wandb_run.Runc                    s   j D ]D}|d}tt|j}dtf fdd}|j|< t|dkr3t	j|d || qt	tt|dd j|d || qdS )	z/Patches the API to log media or metrics to W&B..original_methodc                    sF    fdd} fdd}t  rt |S t |S )Nc                     sl   t   fdd}t  i }t || W d    n1 s,w   Y  I d H S )Nc              
      sv   z!| I d H }  |jj}|d ur| | W d S  ty: } zt| W Y d }~d S d }~ww r   )r'   r   elapsedlog
set_result	Exceptionloggerwarning)cororesultloggable_dicte)r   futurer   r3   r   timerr   r   callbackV   s   

zNPatchAPI.patch.<locals>.method_factory.<locals>.async_method.<locals>.callback)asyncioFuturer	   ensure_future)r   r   rC   r=   r6   r3   r   )r   rA   r   rB   r   async_methodS   s   
z<PatchAPI.patch.<locals>.method_factory.<locals>.async_methodc                     s   t  ?} | i |}z| |||j|j}|d ur | W n ty8 } zt| W Y d }~nd }~ww |W  d    S 1 sEw   Y  d S r   )r	   r'   r   r7   r8   r:   r;   r<   )r   r   rB   r>   r?   r@   rG   r   r   sync_methodh   s   
$z;PatchAPI.patch.<locals>.method_factory.<locals>.sync_method)inspectiscoroutinefunction	functoolswraps)r6   rH   rI   r3   r   )r6   r   method_factoryR   s
   
z&PatchAPI.patch.<locals>.method_factory   r   N)
r&   splitrL   reducegetattrr2   r   r)   lensetattr)r   r3   symbolsymbol_partsoriginalrO   r   rN   r   patchJ   s   


)zPatchAPI.patchc                 C   sf   | j  D ]+\}}|d}t|dkrt| j|d | qttt|dd | j|d | qdS )zUnpatches the API.r5   rP   r   NrQ   )	r)   itemsrR   rU   rV   r2   rL   rS   rT   )r   rW   rY   rX   r   r   r   unpatch   s   
zPatchAPI.unpatch)r3   r4   r   Nr   N)r   r   r   r"   r   r   r*   propertyr   r2   rZ   r\   r   r   r   r   r$   )   s    

<r$   c                   @   s   e Zd Z	ddedee dedee ddf
ddZede	fd	d
Z
ddeddfddZddeddfddZddeddfddZdddZdS )
AutologAPINr%   r&   r'   telemetry_featurer   c                 C   s0   || _ t|||d| _| jj| _d| _d| _dS )zAutolog API calls to W&B.)r%   r&   r'   NF)_telemetry_featurer$   
_patch_apir%   _name_run#_AutologAPI__run_created_by_autolog)r   r%   r&   r'   r`   r   r   r   r*      s   

zAutologAPI.__init__c                 C   s
   | j duS )z'Returns whether autologging is enabled.N)rd   r   r   r   r   _is_enabled   s   
zAutologAPI._is_enabledinitc                 C   s   | j |d dS )zEnable autologging.rh   N)enable)r   rh   r   r   r   r!      s   zAutologAPI.__call__c                 C   s^   |rt j}t jdi || _|| jkrd| _dS dS t jdu r)t  | _d| _dS t j| _dS )z Handle wandb run initialization.TNr   )r.   r3   rh   rd   re   )r   rh   
_wandb_runr   r   r   	_run_init   s   




zAutologAPI._run_initc                 C   s   | j rt| j d |   td| j d | j|d | j| j | j	rKt
| j}t|j| j	d W d   dS 1 sDw   Y  dS dS )zxEnable autologging.

        Args:
            init: Optional dictionary of arguments to pass to wandb.init().

        z; autologging is already enabled, disabling and re-enabling.z	Enabling  autologging.ri   TN)rg   r;   inforc   disablerl   rb   rZ   rd   ra   wb_telemetrycontextrV   feature)r   rh   telr   r   r   rj      s   
"zAutologAPI.enablec                 C   sL   | j du rdS td| j d | jr| j   d| _d| _ | j  dS )zDisable autologging.Nz
Disabling rm   F)rd   r;   rn   rc   re   finishrb   r\   rf   r   r   r   ro      s   

zAutologAPI.disabler   r]   )r   r   r   r"   r   r   r   r*   r^   boolrg   AutologInitArgsr!   rl   rj   ro   r   r   r   r   r_      s&    
r_   )rD   rL   rJ   loggingtypingr   r   r   r   r   r   	wandb.sdkr.   
wandb.utilwandb.sdk.libr   rp   wandb.sdk.lib.timerr	   	getLoggerr   r;   r"   rv   r
   r   r   r   r$   r_   r   r   r   r   <module>   s"     
m