o
    -wi(                     @   sV  d Z ddlZ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mZmZ ddlmZ ddlmZ ddlmZ erbddlmZ ddlmZ dd	lmZmZmZ dd
lmZ G dd deZe	g df Ze	g df Z e	dge!f Z"e#e$Z%G dd deZ&G dd deZ'G dd deZ(G dd deZ)ee&e'e(e)f Z*G dd dZ+dS )z*Batching file prepare requests to our API.    N)TYPE_CHECKINGCallableMutableMappingMutableSequence
MutableSet
NamedTupleOptionalUnion)	termerror)
upload_job)LogicalPath)	TypedDict)stats)file_streaminternal_apiprogress)SettingsStaticc                   @   s>   e Zd ZU eed< eed< eed< ed ed< ed ed< dS )	ArtifactStatusfinalizepending_countcommit_requestedPreCommitFnpre_commit_callbacksconcurrent.futures.Future[None]result_futuresN)__name__
__module____qualname__bool__annotations__intr    r!   r!   W/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/filesync/step_upload.pyr      s   
 r   zprogress.ProgressFnc                   @   sV   e Zd ZU eed< eed< ee ed< ee ed< eed< ee ed< ee ed< dS )	RequestUploadpath	save_nameartifact_idmd5copiedsave_fndigestN)	r   r   r   strr   r   r   r   SaveFnr!   r!   r!   r"   r#   -   s   
 r#   c                   @   s.   e Zd ZU eed< eed< eed< ded< dS )RequestCommitArtifactr&   r   before_commitr   result_futureN)r   r   r   r+   r   r   r   r!   r!   r!   r"   r-   7   s
   
 r-   c                   @   s   e Zd ZU ee ed< dS )RequestFinishcallbackN)r   r   r   r   OnRequestFinishFnr   r!   r!   r!   r"   r0   >   s   
 r0   c                   @   s"   e Zd ZU eed< ee ed< dS )EventJobDonejobexcN)r   r   r   r#   r   r   BaseExceptionr!   r!   r!   r"   r3   B   s   
 r3   c                   @   s   e Zd Z	d)ddddddded	d
ded ddfddZd*ddZdeddfddZde	ddfddZ
de	ddfddZde	ddfddZdeddfddZdeddfddZded eddfd!d"Zdeddfd#d$Zd*d%d&Zdefd'd(ZdS )+
StepUploadNapizinternal_api.Apir   zstats.Statsevent_queuezqueue.Queue[Event]max_threadsr   zfile_stream.FileStreamApisettingsr   returnc                 C   sr   || _ || _|| _|| _tj| jd| _d| j_t	j
jd|d| _i | _g | _i | _|r4t|j| _d S d| _d S )N)targetTzwandb-upload)thread_name_prefixmax_workersF)_api_stats_event_queue_file_stream	threadingThread_thread_body_threaddaemon
concurrentfuturesThreadPoolExecutor_pool_running_jobs_pending_jobs
_artifactsr   silent)selfr8   r   r9   r:   r   r;   r!   r!   r"   __init__K   s   	zStepUpload.__init__c                 C   s   d }	 | j  }t|tr|j}n| | q	 z	| j dd}W n tjy-   d }Y nw |r6| | n| jsG| j	j
dd |rE|  d S q)NTg?F)wait)rB   get
isinstancer0   r1   _handle_eventqueueEmptyrM   rL   shutdown)rQ   finish_callbackeventr!   r!   r"   rF   i   s,   


zStepUpload._thread_bodyr[   c                 C   s  t |trZ|j}|jd urtjd|j|jd |jrA|jd u r2| j|j d  d8  < | 	|j n| j
s9td | |j|j | j|j | jrX| jd}| | d S d S t |tr|j| jvrk| |j d| j|j d< |j| j|j d	< | j|j d
 |j | j|j d |j | 	|j d S t |tr|jd ur|j| jvr| |j | j|j d  d7  < | | d S td|)NzFailed to upload file: %s)exc_infor      z<Uploading artifact file failed. Artifact won't be committed.r   Tr   r   r   r   zEvent has unexpected type: )rU   r3   r4   r5   logger	exceptionr$   r&   rO   _maybe_commit_artifactrP   r
   _fail_artifact_futuresrM   popr%   rN   _start_upload_jobr-   _init_artifactr   addr.   r/   r#   	TypeErrorrQ   r[   r4   r!   r!   r"   rV      sN   






zStepUpload._handle_eventc                 C   s*   |j | jv r| j| d S | | d S N)r%   rM   rN   append_spawn_upload)rQ   r[   r!   r!   r"   rc      s   zStepUpload._start_upload_jobc                    s,    j  j< d fdd}j| dS )a}  Spawn an upload job, and handles the bookkeeping of `self._running_jobs`.

        Context: it's important that, whenever we add an entry to `self._running_jobs`,
        we ensure that a corresponding `EventJobDone` message will eventually get handled;
        otherwise, the `_running_jobs` entry will never get removed, and the StepUpload
        will never shut down.

        The sole purpose of this function is to make sure that the code that adds an entry
        to `self._running_jobs` is textually right next to the code that eventually enqueues
        the `EventJobDone` message. This should help keep them in sync.
        r<   Nc                      sL   z   W jt t d d d S jt t d d w )Nr]   )r5   )
_do_uploadrB   putr3   sysr\   r!   r[   rQ   r!   r"   run_and_notify   s   >z0StepUpload._spawn_upload.<locals>.run_and_notifyr<   N)rM   r%   rL   submit)rQ   r[   ro   r!   rn   r"   rj      s   zStepUpload._spawn_uploadc                 C   s@   t | j| j| j| j|j|j|j|j	|j
|j|j}|  d S rh   )r   	UploadJobrA   r@   rC   rP   r%   r$   r&   r'   r(   r)   r*   runrg   r!   r!   r"   rk      s   zStepUpload._do_uploadr&   c                 C   s   dddt  t  d| j|< d S )NFr   )r   r   r   r   r   )setrO   )rQ   r&   r!   r!   r"   rd      s   zStepUpload._init_artifactc              
   C   s   | j | }|d dkrT|d rVz|d D ]}|  q|d r$| j| W n' tyL } ztd| d tt| | || W Y d }~d S d }~ww | | d S d S d S )Nr   r   r   r   r   z%Committing artifact failed. Artifact z won't be finalized.)rO   r@   commit_artifact	Exceptionr
   r+   ra   _resolve_artifact_futures)rQ   r&   artifact_statuspre_callbackr5   r!   r!   r"   r`      s(   

z!StepUpload._maybe_commit_artifactr5   c                 C   s.   | j | d }|D ]}|| q	|  d S Nr   )rO   set_exceptionclear)rQ   r&   r5   rJ   r/   r!   r!   r"   ra        z!StepUpload._fail_artifact_futuresc                 C   s.   | j | d }|D ]}|d  q	|  d S rz   )rO   
set_resultr|   )rQ   r&   rJ   r/   r!   r!   r"   rw     r}   z$StepUpload._resolve_artifact_futuresc                 C   s   | j   d S rh   )rG   startrQ   r!   r!   r"   r     s   zStepUpload.startc                 C   s
   | j  S rh   )rG   is_aliver   r!   r!   r"   r     s   
zStepUpload.is_aliverh   rp   )r   r   r   r    r   rR   rF   EventrV   r#   rc   rj   rk   r+   rd   r`   r6   ra   rw   r   r   r   r!   r!   r!   r"   r7   J   s8    

-
#	
r7   ),__doc__concurrent.futuresrI   loggingrW   rm   rD   typingr   r   r   r   r   r   r   r	   wandb.errors.termr
   wandb.filesyncr   wandb.sdk.lib.pathsr   r   r   wandb.sdk.internalr   r   r   "wandb.sdk.internal.settings_staticr   r   r   r2   r   r,   	getLoggerr   r^   r#   r-   r0   r3   r   r7   r!   r!   r!   r"   <module>   s4    (

