o
    ni[W                     @   s*  d 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 ddl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 dd	lmZ dd
lmZmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z# errddl$m%Z%m&Z& G dd dZ'G dd dZ(e
d Z)e
d Z*e
d Z+G dd dZ,dS )zPublic API: jobs.    N)TYPE_CHECKINGAnyDictListLiteralMappingOptional)gql)util)public)normalize_exceptions)	CommError)ArtifactState)InvalidTypeTypeTypeRegistry)LaunchError)LAUNCH_DEFAULT_PROJECT_fetch_git_repoapply_patch"convert_jupyter_notebook_to_script)ApiRetryingClientc                   @   s   e Zd ZU eed< eed< eed< eed< eed< ee ed< eed< eed< d$d
ddee dd	fddZ	e
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdee fdd Z							!								d%d"d#Zd	S )&Job_name_input_types_output_types_entity_project_entrypoint_notebook_job_partialNapir   pathreturnc                 C   s  z
|j |dd| _W n ty   td| dw |r'|| _| jj|d n| j | _|| _|| _|j| _t	t
j| jd}t|| _W d    n1 sRw   Y  | jdi }|dd	| _|d
| _|d| _|d| _|d| _|d| _| jdd	| _t
j| jd| _t| jd| _t| jd| _| jddkr| | j | jddkr| | j | jddkr| | j  d S d S )Njob)typezJob artifact z
 not found)rootzwandb-job.jsonsourcenotebookF
entrypoint
dockerfilebuild_context
base_imageargsr!   zrequirements.frozen.txtinput_typesoutput_typessource_typeartifactrepoimage)!	_artifact_job_artifactr   _fpathdownloadr   _apidefault_entityr   openosr#   joinjsonload	_job_infogetr    r   _dockerfile_build_context_base_image_argsr!   _requirements_filer   type_from_dictr   r   _set_configure_launch_project"_configure_launch_project_artifact_configure_launch_project_repo#_configure_launch_project_container)selfr"   namer#   fsource_info rP   T/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/apis/public/jobs.py__init__(   sJ   

zJob.__init__c                 C      | j S Nr   rL   rP   rP   rQ   rM   O      zJob.namec                 C   
   || _ d S rT   )configure_launch_project)rL   funcrP   rP   rQ   rH   S      
z!Job._set_configure_launch_projectc                 C   sp   t |\}}}|rtj|| jj}n| jj|dd}|d u r$td|j	t
jkr6td| j d|j |S )Ncode)rM   r&   zNo code artifact foundJob z" references deleted code artifact )r
   parse_artifact_stringwandbArtifact_from_idr9   _clientr5   r   stater   DELETEDrM   )rL   artifact_stringbase_urlis_idcode_artifactrP   rP   rQ   _get_code_artifactV   s   zJob._get_code_artifactc                 C   s.   t | jd |j}| j}||d< || d S )N)r   r   project_dirset_job_entry_point)rL   launch_project	new_fnamenew_entrypointrP   rP   rQ   "_configure_launch_project_notebookd   s   z&Job._configure_launch_project_notebookc                 C   s  | j di di }t|j|d |d  tjtj| jdrDt	tj| jd}t
| |j W d    n1 s?w   Y  t| j|j | j d|_| jr\| | n|| j | jrk|| j | jrt|| j | jr|| j d S d S )Nr(   gitremotecommitz
diff.patchruntime)r@   rA   r   rk   r<   r#   existsr=   r7   r;   r   readshutilcopyrF   python_versionr    rp   rl   r   rB   set_job_dockerfilerC   set_job_build_contextrD   set_job_base_image)rL   rm   git_inforN   rP   rP   rQ   rJ   l   s,   z"Job._configure_launch_project_repoc                 C   s   | j di d}|d u rtd| j d| |}| j d|_t| j|j	 |
|j	 | jr:| | n|| j | jrI|| j | jrR|| j | jr]|| j d S d S )Nr(   r2   r]   z had no source artifactrt   )r@   rA   r   rM   ri   ry   rw   rx   rF   rk   r8   r    rp   rl   r   rB   rz   rC   r{   rD   r|   )rL   rm   re   rh   rP   rP   rQ   rI      s"   
z&Job._configure_launch_project_artifactc                 C   sB   | j di d|_|jd u rtd| jr|| j d S d S )Nr(   r4   z8Job had malformed source dictionary without an image key)r@   rA   docker_imager   r   rl   )rL   rm   rP   rP   rQ   rK      s   
z'Job._configure_launch_project_containerr*   c                 C   rX   rT   )r   )rL   r*   rP   rP   rQ   set_entrypoint   r[   zJob.set_entrypointlocal-containerc
                 C   s   ddl m}
 i }| D ]\}}t|r*t|tjr#| r#t	dt
|||< q|| | j|}| jr?td nt|trLt| j||
j| jdd|ii||pZ| j|p^| j|||||	d
}|S )Nr   )_launch_addz)Cannot queue jobs with unlogged artifactszELaunching manually created job for the first time, can't verify types	overrides
run_config)
r%   configtemplate_variablesprojectentity
queue_nameresourceproject_queueresource_argspriority)wandb.sdk.launchr   itemsr
   _is_artifact_object
isinstancer_   r`   is_draft
ValueErrorartifact_to_jsonupdater   assignr!   termwarnr   	TypeErrorexplain
launch_addr   r   r   )rL   r   r   r   queuer   r   r   r   r   r   r   keyitemassigned_config_type
queued_runrP   rP   rQ   call   s:   



zJob.callrT   )NNNr   NNNN)__name__
__module____qualname__str__annotations__r   r   boolr   rR   propertyrM   rH   ri   rp   rJ   rI   rK   r   r   rP   rP   rP   rQ   r      s8   
 '
	r   c                   @   s   e Zd ZdZedfddZedd Zedd Zed	d
 Z	edd Z
edd ZedefddZedd Zedd ZedddZedd Zdd ZdS )	QueuedRunzA single queued run associated with an entity and project. Call `run = queued_run.wait_until_running()` or `run = queued_run.wait_until_finished()` to access the run.Nc                 C   s:   || _ || _|| _|| _|| _d | _d | _|| _|| _d S rT   )	clientr   r   _queue_name_run_queue_item_idsweep_runr   r   )rL   r   r   r   r   run_queue_item_idr   r   rP   rP   rQ   rR      s   

zQueuedRun.__init__c                 C   rS   rT   )r   rV   rP   rP   rQ   r      rW   zQueuedRun.queue_namec                 C   rS   rT   )r   rV   rP   rP   rQ   id   rW   zQueuedRun.idc                 C   rS   rT   )r   rV   rP   rP   rQ   r      rW   zQueuedRun.projectc                 C   rS   rT   r   rV   rP   rP   rQ   r      rW   zQueuedRun.entityc                 C   s8   |   }|r|d  S td| j d| j d| j )Nrc   z1Could not find QueuedRunItem associated with id: z
 on queue z at itemId: )	_get_itemlowerr   r   r   )rL   r   rP   rP   rQ   rc      s   zQueuedRun.stater$   c                 C   sl   t d}| j| j| jd}| j||}|d d d d D ]}t|d d t| jkr3|d   S qd S )	Nap  
            query GetRunQueueItem($projectName: String!, $entityName: String!, $runQueue: String!) {
                project(name: $projectName, entityName: $entityName) {
                    runQueue(name:$runQueue) {
                        runQueueItems {
                            edges {
                                node {
                                    id
                                    state
                                    associatedRunId
                                }
                            }
                        }
                    }
                }
            }
            projectName
entityNamerunQueuer   r   runQueueItemsedgesnoder   )r	   r   r   r   r   executer   r   rL   queryvariable_valuesresr   rP   rP   rQ   _get_run_queue_item_legacy  s   z$QueuedRun._get_run_queue_item_legacyc              
   C   s   t d}| j| j| j| jd}z | j||}|d d dd ur+|d d d W S W |  S  tyQ } zdt	|vrDt
d| W Y d }~|  S d }~ww )Na  
            query GetRunQueueItem($projectName: String!, $entityName: String!, $runQueue: String!, $itemId: ID!) {
                project(name: $projectName, entityName: $entityName) {
                    runQueue(name: $runQueue) {
                        runQueueItem(id: $itemId) {
                            id
                            state
                            associatedRunId
                        }
                    }
                }
            }
        )r   r   r   itemIdr   r   runQueueItemzCannot query fieldzUnknown exception: )r	   r   r   r   r   r   r   rA   	Exceptionr   r   r   )rL   r   r   r   erP   rP   rQ   r   '  s*   
zQueuedRun._get_itemc                 C   s,   | j s|   | j   | j jdd | j S )NT)force)r   wait_until_runningwait_until_finishedr?   rV   rP   rP   rQ   r   H  s
   
zQueuedRun.wait_until_finishedFc                 C   sl   t d}| jj|| j| j| jdd}|d ddur$|d d d }t d}| jj||| jd	d dS )
z3Delete the given queued run from the wandb backend.aL  
            query fetchRunQueuesFromProject($entityName: String!, $projectName: String!, $runQueueName: String!) {
                project(name: $projectName, entityName: $entityName) {
                    runQueue(name: $runQueueName) {
                        id
                    }
                }
            }
            )r   r   runQueueName)r   r   r   Nr   a  
            mutation DeleteFromRunQueue(
                $queueID: ID!,
                $runQueueItemId: ID!
            ) {
                deleteFromRunQueue(input: {
                    queueID: $queueID
                    runQueueItemId: $runQueueItemId
                }) {
                    success
                    clientMutationId
                }
            }
            )queueIDrunQueueItemId)r	   r   r   r   r   r   rA   r   )rL   delete_artifactsr   r   queue_idmutationrP   rP   rQ   deleteR  s*   	
zQueuedRun.deletec              
   C   s   | j d ur| j S 	 td |  }|rI|d d urIzt| j| j| j|d d | _ |d | _	| j W S  t
yH } z
t| W Y d }~nd }~ww |rPtd td q	)NT   associatedRunIdzWaiting for run to start   )r   timesleepr   r   Runr   r   r   _run_idr   printr_   termlog)rL   r   r   rP   rP   rQ   r     s0   




zQueuedRun.wait_until_runningc                 C      d| j  d| j dS )Nz<QueuedRun z ())r   r   rV   rP   rP   rQ   __repr__     zQueuedRun.__repr__)F)r   r   r   __doc__r   rR   r   r   r   r   r   rc   r   r   r   r   r   r   r   r   rP   rP   rP   rQ   r      s4    	





	
 
	2
r   )r   zlocal-process
kubernetes	sagemakerz
gcp-vertex)r   user)DISABLEDV0c                   @   st  e Zd Z				d2dddededee dee dee d	ee d
dfddZ	e
dd Ze
dd Ze
d
efddZe
d
efddZe
d
eeef fddZe
d
efddZe
dd Ze
dd Ze
d
efddZe
d
ee fdd Zed!d" Zd#d$ Zed%d& Zed'd( Zed)d* Ze				d2ded+d,dee ded- d.ee d/ee d
d fd0d1Z dS )3RunQueueNr   r   rM   r   prioritization_mode_access_default_resource_config_id_default_resource_configr$   c                 C   sF   || _ || _|| _|| _|| _|| _|| _d | _d | _d | _	d | _
d S rT   )r   rb   r   _prioritization_moder   r   r   _template_variables_type_items_id)rL   r   rM   r   r   r   r   r   rP   rP   rQ   rR     s   

zRunQueue.__init__c                 C   rS   rT   rU   rV   rP   rP   rQ   rM     rW   zRunQueue.namec                 C   rS   rT   r   rV   rP   rP   rQ   r     rW   zRunQueue.entityc                 C      | j d u r	|   | j S rT   )r   _get_metadatarV   rP   rP   rQ   r        
zRunQueue.prioritization_modec                 C   r   rT   )r   r   rV   rP   rP   rQ   access  r   zRunQueue.accessc                 C   r   rT   )_external_linksr   rV   rP   rP   rQ   external_links  r   zRunQueue.external_linksc                 C   *   | j d u r| jd u r|   |   | j S rT   )r   r   r   _get_default_resource_configrV   rP   rP   rQ   r&     
   

zRunQueue.typec                 C   r   rT   )r   r   r   r   rV   rP   rP   rQ   default_resource_config  r   z RunQueue.default_resource_configc                 C   r   rT   )r   r   r   r   rV   rP   rP   rQ   r     r   zRunQueue.template_variablesc                 C   r   rT   )r   r   rV   rP   rP   rQ   r     r   zRunQueue.idc                 C   s   | j du r	|   | j S )zeUp to the first 100 queued runs. Modifying this list will not modify the queue or any enqueued items!N)r   
_get_itemsrV   rP   rP   rQ   r     s   
zRunQueue.itemsc                 C   s^   t d}d| ji}| j||}|d d r'd| _d| _d| _d| _d| _dS t	d| j
 )z,Delete the run queue from the wandb backend.z
            mutation DeleteRunQueue($id: ID!) {
                deleteRunQueues(input: {queueIDs: [$id]}) {
                    success
                    clientMutationId
                }
            }
            r   deleteRunQueuessuccessNzFailed to delete run queue )r	   r   rb   r   r   r   r   r   r   r   rM   rL   r   r   r   rP   rP   rQ   r     s   


zRunQueue.deletec                 C   r   )Nz
<RunQueue />)r   r   rV   rP   rP   rQ   r     r   zRunQueue.__repr__c                 C   s   t d}t| j| jd}| j||}|d d d | _|d d d | _|d d d | _|d d d | _	| jd u r?i | _
|d d d	 | _d S )
Na  
            query GetRunQueueMetadata($projectName: String!, $entityName: String!, $runQueue: String!) {
                project(name: $projectName, entityName: $entityName) {
                    runQueue(name: $runQueue) {
                        id
                        access
                        defaultResourceConfigID
                        prioritizationMode
                        externalLinks
                    }
                }
            }
        r   r   r   r   r   defaultResourceConfigIDexternalLinksprioritizationMode)r	   r   r   r   rb   r   r   r   r   r   r   r   r   rP   rP   rQ   r     s"   

zRunQueue._get_metadatac                 C   s^   t d}| j| jd}| j||}|d d d | _|d d d | _|d d d | _d S )Na  
            query GetDefaultResourceConfig($entityName: String!, $id: ID!) {
                entity(name: $entityName) {
                    defaultResourceConfig(id: $id) {
                        config
                        resource
                        templateVariables {
                            name
                            schema
                        }
                    }
                }
            }
        )r   r   r   defaultResourceConfigr   r   templateVariables)r	   r   r   rb   r   r   r   r   r   rP   rP   rQ   r   >  s   

z%RunQueue._get_default_resource_configc              
   C   sp   t d}t| j| jd}| j||}g | _|d d d d D ]}| jt| j| jt| j|d d  q d S )	Na  
            query GetRunQueueItems($projectName: String!, $entityName: String!, $runQueue: String!) {
                project(name: $projectName, entityName: $entityName) {
                    runQueue(name: $runQueue) {
                        runQueueItems(first: 100) {
                            edges {
                                node {
                                    id
                                }
                            }
                        }
                    }
                }
            }
        r   r   r   r   r   r   r   )	r	   r   r   r   rb   r   r   appendr   r   rP   rP   rQ   r   [  s(   
zRunQueue._get_itemsr   RunQueueResourceTypeRunQueuePrioritizationModer   r   c                 C   s   t  }|||||||S rT   )r   create_run_queue)clsrM   r   r   r   r   r   
public_apirP   rP   rQ   create  s   
zRunQueue.create)NNNN)!r   r   r   r   r   r  RunQueueAccessTypeintdictrR   r   rM   r   r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr  rP   rP   rP   rQ   r     s    	






 

$r   )-r   r>   r<   rw   r   typingr   r   r   r   r   r   r   	wandb_gqlr	   r_   r
   
wandb.apisr   wandb.apis.normalizer   wandb.errorsr   "wandb.sdk.artifacts.artifact_stater   wandb.sdk.data_types._dtypesr   r   r   wandb.sdk.launch.errorsr   wandb.sdk.launch.utilsr   r   r   r   wandb.apis.publicr   r   r   r   r   r  r  r   rP   rP   rP   rQ   <module>   s8    $ 9 O