o
    xi-                     @  s4  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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 ddlmZmZ ddlmZ ddlmZmZmZmZ ee Z!d9ddZ"d:ddZ#d;d"d#Z$													d<d=d4d5Z%											6		d>d?d7d8Z&dS )@    )annotationsN)Any)Api   )loader)LaunchProject)LaunchAgent)construct_agent_configs)LocalEnvironment)ExecutionErrorLaunchError)AbstractRun)LAUNCH_CONFIG_FILEPROJECT_SYNCHRONOUSconstruct_launch_specvalidate_launch_spec_sourcelogfilestrreturnNonec              
   C  s   t d}| dkrtj}n&zt| d}W n ty2 } ztd|  d|  W Y d}~dS d}~ww td| dkr<dn|  d	 t 	|}t 
d
|_|| |t jd|  dS )z%Set the logfile for the launch agent.zwandb.sdk.launch-wzCould not open zA for writing logs. Please check the path and permissions.
Error: Nz Internal agent logs printing to stdoutz. zn%(asctime)s %(levelname)-7s %(threadName)-10s:%(process)d [%(filename)s:%(funcName)s():%(lineno)s] %(message)sz"Internal agent logs printing to %s)logging	getLoggersysr   open	Exceptionwandb	termerrortermlogStreamHandler	Formatter	formatter
addHandlerlogINFO)r   _launch_loggerlogfile_streamehandler r+   L/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/_launch.pyset_launch_logfile   s.   


r-   entity
str | Nonemax_jobs
int | Nonequeuestuple[str] | Noneconfig	verbositytuple[dict[str, Any], Api]c                   s   ddl }ddg i i dd}| |ptjt}tj|r]i }t|*}	z||	}|du r0i }W n |jyE }
 zt	d|
 d}
~
ww W d   n1 sPw   Y   
|  n|durht	d| tjddur{ 
dtjdi tjd	dur 
d
ttjd	di | dur 
d| i |dur 
d
t|i |r 
dt|i |r 
dt|i  drt dtrֈ d  d  nd}t	|dg} fdd|D }t|d} ddu r 
d|ji  |fS )a  Resolve the agent config.

    Arguments:
        api (Api): The api.
        entity (str): The entity.
        max_jobs (int): The max number of jobs.
        queues (Tuple[str]): The queues.
        config (str): The config.
        verbosity (int): How verbose to print, 0 or None = default, 1 = print status every 20 seconds, 2 = also print debugging information

    Returns:
        Tuple[Dict[str, Any], Api]: The resolved config and api.
    r   Nr   )r0   max_schedulersr2   registrybuilderr5   zInvalid launch agent config: z1Could not find use specified launch config file: WANDB_ENTITYr.   WANDB_LAUNCH_MAX_JOBSr0   r2   r5   queuezInvalid launch agent config for key 'queue' with type: {type(resolved_config.get('queue'))} (expected str). Specify multiple queues with the 'queues' keyc                   s&   i | ]}  |d ur|  |qS )N)get).0kresolved_configr+   r,   
<dictcomp>   s     z(resolve_agent_config.<locals>.<dictcomp>)default_settings)yamlospath
expanduserr   isfiler   	safe_load	YAMLErrorr   updateitemsenvironr=   intlist
isinstancer   appendr   default_entity)r.   r0   r2   r4   r5   rD   defaultsconfig_pathlaunch_configfr)   msgkeyssettingsapir+   r@   r,   resolve_agent_config:   sr   




r[   rZ   r   dict[str, Any]c           
      C  s  zddl m} W n ty   tdw z
|jdi | W nM |jyk } z@| }|D ]5}ddd |dg D }d| }|d	}t	|t
sT|d
| d7 }|d|d  7 }t| q-tdd }~ww t| |}	z
t|	  W d S  tjy   Y d S w )Nr   )r4   zewandb launch-agent requires pydantic to be installed. Please install with `pip install wandb[launch]`.c                 S  s   g | ]}t |qS r+   )r   )r>   xr+   r+   r,   
<listcomp>   s    z(create_and_run_agent.<locals>.<listcomp>loczAgent config error in field inputz	 (value: )z: rW   zInvalid launch agent configr+   )wandb.sdk.launch.agentr4   ModuleNotFoundErrorr   AgentConfigValidationErrorerrorsjoinr=   rP   dictr   r   r   asynciorunloopCancelledError)
rZ   r4   agent_configr)   rg   errorr`   rW   valueagentr+   r+   r,   create_and_run_agent   s8   



rr   jobnameprojectdocker_imageentry_pointlist[str] | Noneversionresourceresource_argsdict[str, Any] | NonerU   synchronousbool | Nonerun_id
repositoryr   c                   sV  |
du ri }
|du rd}t d|| ||||||||	|
||dd}t| t|| }|  | }|jp7|j}i }||t< |
pAi }t	|\}}}t
|}|dur^t|ts^| I dH  t
||}t
|||}|js|js|suJ |||dI dH }t
|| |||}|r|sJ |||I dH }|sJ |S td| ddt
j )z[Helper that delegates to the project-running method corresponding to the passed-in backend.Nzlocal-container)authorzUnavailable backend z, available backends: z, )r   r   r   	from_specfetch_and_validate_projectget_job_entry_pointrv   job_base_imager   r	   r   environment_from_configrP   r
   verifyregistry_from_configbuilder_from_configbuild_imagerunner_from_configrk   r   rh   WANDB_RUNNERS)rZ   rs   rt   ru   r.   rv   rw   ry   rz   r{   rU   r}   r   r   launch_speclaunch_project
entrypoint	image_urirunner_configr4   environment_configbuild_configregistry_configenvironmentr8   r9   backendsubmitted_runr+   r+   r,   _launch   sf   


r   Tc                 C  s.   t t|||||	|||||
|| ||d}|S )a  Launch a W&B launch experiment.

    Arguments:
        job: string reference to a wandb.Job eg: wandb/test/my-job:latest
        api: An instance of a wandb Api from wandb.apis.internal.
        entry_point: Entry point to run within the project. Defaults to using the entry point used
            in the original run for wandb URIs, or main.py for git repository URIs.
        version: For Git-based projects, either a commit hash or a branch name.
        name: Name run under which to launch the run.
        resource: Execution backend for the run.
        resource_args: Resource related arguments for launching runs onto a remote backend.
            Will be stored on the constructed launch config under ``resource_args``.
        project: Target project to send launched run to
        entity: Target entity to send launched run to
        config: A dictionary containing the configuration for the run. May also contain
        resource specific arguments under the key "resource_args".
        synchronous: Whether to block while waiting for a run to complete. Defaults to True.
            Note that if ``synchronous`` is False and ``backend`` is "local-container", this
            method will return, but the current process will block when exiting until
            the local run completes. If the current process is interrupted, any
            asynchronous runs launched via this method will be terminated. If
            ``synchronous`` is True and the run fails, the current process will
            error out as well.
        run_id: ID for the run (To ultimately replace the :name: field)
        repository: string name of repository path for remote registry

    Example:
        ```python
        from wandb.sdk.launch import launch

        job = "wandb/jobs/Hello World:latest"
        params = {"epochs": 5}
        # Run W&B project and create a reproducible docker environment
        # on a local host
        api = wandb.apis.internal.Api()
        launch(api, job, parameters=params)
        ```


    Returns:
        an instance of`wandb.launch.SubmittedRun` exposing information (e.g. run ID)
        about the launched run.

    Raises:
        `wandb.exceptions.ExecutionError` If a run launched in blocking mode
        is unsuccessful.
    )rs   rt   ru   r.   rv   rw   ry   rz   r{   rU   r}   rZ   r   r   )rj   rk   r   )rZ   rs   rw   ry   rt   rz   r{   ru   r.   rv   r4   r}   r   r   submitted_run_objr+   r+   r,   launch   s&   ?r   )r   r   r   r   )r.   r/   r0   r1   r2   r3   r4   r/   r5   r1   r   r6   )rZ   r   r4   r\   r   r   )NNNNNNNNNNNNN)rZ   r   rs   r/   rt   r/   ru   r/   r.   r/   rv   r/   rw   rx   ry   r/   rz   r/   r{   r|   rU   r|   r}   r~   r   r/   r   r/   r   r   )NNNNNNNNNNTNN)rZ   r   rs   r/   rw   rx   ry   r/   rt   r/   rz   r/   r{   r|   ru   r/   r.   r/   rv   r/   r4   r|   r}   r~   r   r/   r   r/   r   r   )'
__future__r   rj   r   rE   r   typingr   r   wandb.apis.internalr    r   _project_specr   rq   r   agent.agentr	   environment.local_environmentr
   rg   r   r   runner.abstractr   utilsr   r   r   r   r   __name___loggerr-   r[   rr   r   r   r+   r+   r+   r,   <module>   s`    



V!N