o
    xi'                      @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dl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 )    )annotationsN)Sequence)AnyOptionalProtocolTypeVar)	telemetry)TimerK)boundVc                   @  s"   e Zd ZdddZddd
dZdS )Responsekeyr
   returnr   c                 C     d S N )selfr   r   r   \/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/integration_utils/auto_logging.py__getitem__       zResponse.__getitem__NdefaultV | Nonec                 C  r   r   r   )r   r   r   r   r   r   get   r   zResponse.get)r   r
   r   r   r   )r   r
   r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r      s    
r   c                   @  s   e Zd ZdddZdS )ArgumentResponseResolverargsSequence[Any]kwargsdict[str, Any]responser   
start_timefloattime_elapsedr   dict[str, Any] | Nonec                 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   r#   r$   r%   r$   r   r&   )r   r   r   r'   r   r   r   r   r      s    r   c                   @  s8   e Zd Zdd	d
ZedddZdddZdddZdS )PatchAPInamestrsymbolsSequence[str]resolverr   r   Nonec                 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__r   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.Runc                   s   j D ]B}|d}tt|j}d
 fdd}|j|< t|dkr1t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_methodr   c                   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    r:   r   timerr   r   callbackW   s   

zNPatchAPI.patch.<locals>.method_factory.<locals>.async_method.<locals>.callback)asyncioFuturer	   ensure_future)r   r    rJ   rD   r=   r:   r   )r   rH   r    rI   r   async_methodT   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#   r>   r?   rA   rB   rC   )r   r    rI   rE   rF   rG   rN   r   r   sync_methodi   s   
$z;PatchAPI.patch.<locals>.method_factory.<locals>.sync_method)inspectiscoroutinefunction	functoolswraps)r=   rO   rP   r:   r   )r=   r   method_factoryS   s
   
z&PatchAPI.patch.<locals>.method_factory   r   N)r=   r   )	r+   splitrS   reducegetattrr9   r0   lensetattr)r   r:   symbolsymbol_partsoriginalrV   r   rU   r   patchK   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.r<   rW   r   NrX   )	r0   itemsrY   r\   r]   r9   rS   rZ   r[   )r   r^   r`   r_   r   r   r   unpatch   s   
zPatchAPI.unpatchN)r)   r*   r+   r,   r-   r   r   r.   )r   r   )r:   r;   r   r.   r   r.   )r   r   r   r1   propertyr9   ra   rc   r   r   r   r   r(   *   s    

<r(   c                   @  sV   e Zd Z	ddddZedddZddddZddddZddddZdddZ	dS ) 
AutologAPINr)   r*   r+   r,   r-   r   telemetry_feature
str | Noner   r.   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-   rg   r   r   r   r1      s   

zAutologAPI.__init__boolc                 C  s
   | j duS )z'Returns whether autologging is enabled.N)rl   r   r   r   r   _is_enabled   s   
zAutologAPI._is_enabledinitAutologInitArgsc                 C  s   | j |d dS )zEnable autologging.rq   N)enable)r   rq   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   )r5   r:   rq   rl   rm   )r   rq   
_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.rs   TN)rp   rB   infork   disablerv   rj   ra   rl   ri   wb_telemetrycontextr]   feature)r   rq   telr   r   r   rt      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 rw   F)rl   rB   rx   rk   rm   finishrj   rc   ro   r   r   r   ry      s   

zAutologAPI.disabler   )
r)   r*   r+   r,   r-   r   rg   rh   r   r.   )r   rn   )rq   rr   r   r.   rd   )
r   r   r   r1   re   rp   r'   rv   rt   ry   r   r   r   r   rf      s    rf   )!
__future__r   rK   rS   rQ   loggingcollections.abcr   typingr   r   r   r   	wandb.sdkr5   
wandb.utilwandb.sdk.libr   rz   wandb.sdk.lib.timerr	   	getLoggerr   rB   dictr*   rr   r
   r   r   r   r(   rf   r   r   r   r   <module>   s&    
m