o
    wii                     @  s6  d Z 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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mZmZ ddlmZ ddlmZm Z m!Z!m"Z" e
rxddl#m$Z$m%Z% ddl&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 )zW&B Public API for management Launch Jobs and Launch Queues.

This module provides classes for managing W&B jobs, queued runs, and run
queues.
    )annotationsN)Mapping)TYPE_CHECKINGAnyCallableLiteral)gql)util)public)normalize_exceptions)	CommError)InvalidTypeTypeTypeRegistry)LaunchError)LAUNCH_DEFAULT_PROJECT_fetch_git_repoapply_patch"convert_jupyter_notebook_to_script)ApiRetryingClient)LaunchProjectc                   @  s   e Zd ZU ded< ded< ded< ded< ded< ded	< d
ed< d
ed< d.d/ddZed0ddZd1ddZdd Zd2d d!Z	d2d"d#Z
d2d$d%Zd2d&d'Zd3d)d*Z				+				d4d,d-ZdS )5Jobstr_namer   _input_types_output_types_entity_project	list[str]_entrypointbool_notebook_job_partialNapir   path
str | NonereturnNonec                 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 rT   J/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/public/jobs.py__init__/   sJ   

zJob.__init__c                 C     | j S )zThe name of the job.r   rP   rT   rT   rU   rQ   V      zJob.namefuncCallable[[LaunchProject], None]c                 C  s
   || _ d S N)configure_launch_project)rP   r[   rT   rT   rU   rL   [   s   
z!Job._set_configure_launch_projectc                 C  s|   ddl m} t|\}}}|rtj|| jj}n| jj	|dd}|d u r*t
d|j|jkr<t
d| j d|j |S )Nr   )ArtifactStatecode)rQ   r*   zNo code artifact foundJob z" references deleted code artifact )"wandb.sdk.artifacts.artifact_stater_   r	   parse_artifact_stringwandbArtifact_from_idr=   _clientr9   r   stateDELETEDrQ   )rP   artifact_stringr_   base_urlis_idcode_artifactrT   rT   rU   _get_code_artifact^   s   zJob._get_code_artifactlaunch_projectr   c                 C  s.   t | jd |j}| j}||d< || d S )N)r   r    project_dirset_job_entry_point)rP   ro   	new_fnamenew_entrypointrT   rT   rU   "_configure_launch_project_notebookn   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)rD   rE   r   rq   r@   r%   existsrA   r;   r?   r   readshutilcopyrJ   python_versionr"   ru   rr   r    rF   set_job_dockerfilerG   set_job_build_contextrH   set_job_base_image)rP   ro   git_inforR   rT   rT   rU   rN   v   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,   r6   ra   z had no source artifactry   )rD   rE   r   rQ   rn   r~   r|   r}   rJ   rq   r<   r"   ru   rr   r    rF   r   rG   r   rH   r   )rP   ro   rj   rm   rT   rT   rU   rM      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,   r8   z8Job had malformed source dictionary without an image key)rD   rE   docker_imager   r    rr   )rP   ro   rT   rT   rU   rO      s   
z'Job._configure_launch_project_containerr.   c                 C  s
   || _ dS )zSet the entrypoint for the job.N)r    )rP   r.   rT   rT   rU   set_entrypoint   s   
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 )a  Call the job with the given configuration.

        Args:
            config (dict): The configuration to pass to the job.
                This should be a dictionary containing key-value pairs that
                match the input types defined in the job.
            project (str, optional): The project to log the run to. Defaults
                to the job's project.
            entity (str, optional): The entity to log the run under. Defaults
                to the job's entity.
            queue (str, optional): The name of the queue to enqueue the job to.
                Defaults to None.
            resource (str, optional): The resource type to use for execution.
                Defaults to "local-container".
            resource_args (dict, optional): Additional arguments for the
                resource type. Defaults to None.
            template_variables (dict, optional): Template variables to use for
                the job. Defaults to None.
            project_queue (str, optional): The project that manages the queue.
                Defaults to None.
            priority (int, optional): The priority of the queued run.
                Defaults to None.
        r   )_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
isinstancerd   re   is_draft
ValueErrorartifact_to_jsonupdater   assignr#   termwarnr   	TypeErrorexplain
launch_addr   r   r   )rP   r   r   r   queuer   r   r   r   r   r   r   keyitemassigned_config_type
queued_runrT   rT   rU   call   s:   #



zJob.callr]   )r$   r   r%   r&   r'   r(   r'   r   )r[   r\   )ro   r   r'   r(   )r.   r   r'   r(   )NNNr   NNNN)__name__
__module____qualname____annotations__rV   propertyrQ   rL   rn   ru   rN   rM   rO   r   r   rT   rT   rT   rU   r   %   s8   
 '





r   c                   @  s   e Zd ZdZedfd,ddZed-ddZed-ddZed-ddZ	ed-ddZ
ed-ddZed.ddZed.ddZed/d d!Zed0d1d&d'Zed/d(d)Zd-d*d+ZdS )2	QueuedRuna6  A single queued run associated with an entity and project.

    Args:
        entity: The entity associated with the queued run.
        project (str): The project where runs executed by the queue are logged to.
        queue_name (str): The name of the queue.
        run_queue_item_id (int): The id of the run queue item.
        project_queue (str): The project that manages the queue.
        priority (str): The priority of the queued run.

    Call `run = queued_run.wait_until_running()` or
    `run = queued_run.wait_until_finished()` to access the run.
    Nclientr   r   r   r   r   run_queue_item_idr   r   
int | Nonec                 C  s:   || _ || _|| _|| _|| _d | _d | _|| _|| _d S r]   )	r   r   r   _queue_name_run_queue_item_idsweep_runr   r   )rP   r   r   r   r   r   r   r   rT   rT   rU   rV   	  s   

zQueuedRun.__init__r'   c                 C  rW   zThe name of the queue.)r   rY   rT   rT   rU   r     rZ   zQueuedRun.queue_namec                 C  rW   )zThe id of the queued run.)r   rY   rT   rT   rU   id"  rZ   zQueuedRun.idc                 C  rW   )z+The project associated with the queued run.)r   rY   rT   rT   rU   r   '  rZ   zQueuedRun.projectc                 C  rW   )z*The entity associated with the queued run.r   rY   rT   rT   rU   r   ,  rZ   zQueuedRun.entityc                 C  s8   |   }|r|d  S td| j d| j d| j )zThe state of the queued run.rh   z1Could not find QueuedRunItem associated with id: z
 on queue z at itemId: )	_get_itemlowerr   r   r   )rP   r   rT   rT   rU   rh   1  s   zQueuedRun.statedict[str, Any]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   rP   queryvariable_valuesresr   rT   rT   rU   _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   rE   	Exceptionr   r   r   )rP   r   r   r   erT   rT   rU   r   \  s*   
zQueuedRun._get_item
public.Runc                 C  s,   | j s|   | j   | j jdd | j S )z@Wait for the queued run to complete and return the finished run.T)force)r   wait_until_runningwait_until_finishedrC   rY   rT   rT   rU   r   }  s
   
zQueuedRun.wait_until_finishedFdelete_artifactsr!   r(   c                 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   rE   r   )rP   r   r   r   queue_idmutationrT   rT   rU   delete  s*   	
zQueuedRun.deletec              
   C  s   | j dur| j S 	 td |  }|rL|d durLzt| j| j| j|d d| _ |d | _	W | j S  t
yK } ztt| W Y d}~nd}~ww |rStd td q	)z8Wait until the queued run is running and return the run.NT   associatedRunIdzWaiting for run to start   )r   timesleepr   r
   Runr   r   r   _run_idr   rd   r   r   termlog)rP   r   r   rT   rT   rU   r     s0   



zQueuedRun.wait_until_runningc                 C     d| j  d| j dS )Nz<QueuedRun z ())r   r   rY   rT   rT   rU   __repr__     zQueuedRun.__repr__)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r   )r'   r   )F)r   r!   r'   r(   )r   r   r   __doc__r   rV   r   r   r   r   r   rh   r   r   r   r   r   r   r   rT   rT   rT   rU   r      s4    
 
2r   )r   zlocal-process
kubernetes	sagemakerz
gcp-vertex)r   user)DISABLEDV0c                   @  s
  e Zd ZdZ				d?d@ddZedAddZedAddZedBddZedCddZ	edDdd Z
edEd"d#ZedFd%d&ZedFd'd(ZedAd)d*ZedGd,d-ZedHd.d/ZdAd0d1ZedHd2d3ZedHd4d5ZedHd6d7Ze				d?dId=d>ZdS )JRunQueuea  Class that represents a run queue in W&B.

    Args:
        client: W&B API client instance.
        name: Name of the run queue
        entity: The entity (user or team) that owns this queue
        prioritization_mode: Queue priority mode
            Can be "DISABLED" or "V0". Defaults to `None`.
        _access: Access level for the queue
            Can be "project" or "user". Defaults to `None`.
        _default_resource_config_id: ID of default resource config
        _default_resource_config: Default resource configuration
    Nr   r   rQ   r   r   prioritization_mode!RunQueuePrioritizationMode | None_accessRunQueueAccessType | None_default_resource_config_idr   _default_resource_configdict[str, Any] | Noner'   r(   c                 C  sF   || _ || _|| _|| _|| _|| _|| _d | _d | _d | _	d | _
d S r]   )r   rg   r   _prioritization_moder   r   r   _template_variables_type_items_id)rP   r   rQ   r   r   r   r   r   rT   rT   rU   rV     s   

zRunQueue.__init__c                 C  rW   r   rX   rY   rT   rT   rU   rQ     rZ   zRunQueue.namec                 C  rW   )zThe entity that owns the queue.r   rY   rT   rT   rU   r     rZ   zRunQueue.entityRunQueuePrioritizationModec                 C     | j du r	|   | j S )zYThe prioritization mode of the queue.

        Can be set to "DISABLED" or "V0".
        N)r   _get_metadatarY   rT   rT   rU   r     s   
zRunQueue.prioritization_modeRunQueueAccessTypec                 C  r   )zThe access level of the queue.N)r   r   rY   rT   rT   rU   access     
zRunQueue.accessdict[str, str]c                 C  r   )z&External resource links for the queue.N)_external_linksr   rY   rT   rT   rU   external_links#  r   zRunQueue.external_linksRunQueueResourceTypec                 C  *   | j du r| jdu r|   |   | j S )z The resource type for execution.N)r   r   r   _get_default_resource_configrY   rT   rT   rU   r*   *  
   

zRunQueue.typer   c                 C  r   )z(The default configuration for resources.N)r   r   r   r   rY   rT   rT   rU   default_resource_config3  r  z RunQueue.default_resource_configc                 C  r   )z!Variables for resource templates.N)r   r   r   r   rY   rT   rT   rU   r   <  r  zRunQueue.template_variablesc                 C  r   )zThe id of the queue.N)r   r   rY   rT   rT   rU   r   E  r   zRunQueue.idlist[QueuedRun]c                 C  r   )zeUp to the first 100 queued runs. Modifying this list will not modify the queue or any enqueued items!N)r   
_get_itemsrY   rT   rT   rU   r   L  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   rg   r   r   r   r   r   r   r   rQ   rP   r   r   r   rT   rT   rU   r   T  s   


zRunQueue.deletec                 C  r   )Nz
<RunQueue />)r   r   rY   rT   rT   rU   r   l  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   rg   r   r   r   r   r   r   r   r  rT   rT   rU   r   o  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   rg   r   r   r   r   r  rT   rT   rU   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   rg   r   r   appendr   r   rT   rT   rU   r    s(   
zRunQueue._get_itemsr   r&   r   dict | Noner   c           	      C  s&   ddl m} | }|||||||S )a  Create a RunQueue.

        Args:
            name: The name of the run queue to create.
            resource: The resource type for execution.
            entity: The entity (user or team) that will own the queue.
                Defaults to the default entity of the API client.
            prioritization_mode: The prioritization mode for the queue.
                Can be "DISABLED" or "V0". Defaults to None.
            config: Optional dictionary for the default resource
                configuration. Defaults to None.
            template_variables: Optional dictionary for template variables
                used in the resource configuration.
        r   )r   )wandb.apis.publicr   create_run_queue)	clsrQ   r   r   r   r   r   r   
public_apirT   rT   rU   create  s
   zRunQueue.create)NNNN)r   r   rQ   r   r   r   r   r   r   r   r   r   r   r   r'   r(   r   )r'   r   )r'   r   )r'   r   )r'   r   r   )r'   r  )r'   r(   )rQ   r   r   r   r   r&   r   r   r   r  r   r  r'   r   )r   r   r   r   rV   r   rQ   r   r   r   r   r*   r  r   r   r   r   r   r   r   r   r  classmethodr  rT   rT   rT   rU   r     sT    	
 $r   ).r   
__future__r   rB   r@   r|   r   collections.abcr   typingr   r   r   r   	wandb_gqlr   rd   r	   
wandb.apisr
   wandb.apis.normalizer   wandb.errorsr   wandb.sdk.data_types._dtypesr   r   r   wandb.sdk.launch.errorsr   wandb.sdk.launch.utilsr   r   r   r   r  r   r   wandb.sdk.launch._project_specr   r   r   r   r   r   r   rT   rT   rT   rU   <module>   s<     V c