o
    `۷iS1                     @   s   d dl Z d dl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 er2d dlmZ d dlmZ e eZG dd	 d	ZdS )
    N)deepcopy)TYPE_CHECKINGDictOptional)version)flatten_dict)RunMlflowClientc                   @   sF  e Zd ZdZdd Zd0ddZ							d1dee d	ee d
ee dee dee dee defddZ	de
de
fddZ			d2dee dee
 deddfddZ	d3dee dee
 ddfddZdedefdd Zd4d"d#Zd0d$e
dee fd%d&Zd0d'e
dee fd(d)Zd0d*edee fd+d,Zd3d-ee fd.d/ZdS )5_MLflowLoggerUtilzUtil class for setting up and logging to MLflow.

    Use this util for any library that needs MLflow logging/tracking logic
    such as Ray Tune or Ray Train.
    c                 C   s   dd l }|| _d | _d S )Nr   )mlflow_mlflowexperiment_id)selfr    r   N/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/air/_internal/mlflow.py__init__   s   
z_MLflowLoggerUtil.__init__Nc                 C   s@   | j }| jd t| j|}t }|j| || _ ||_ |S )Nr   )r   __dict__popr   r   update)r   memor   	dict_copycopied_objectr   r   r   __deepcopy__   s   z_MLflowLoggerUtil.__deepcopy__Ttracking_uriregistry_urir   experiment_nametracking_tokenartifact_locationcreate_experiment_if_not_existsc           	      C   s>  |r|t jd< | j| | j| |dur|nt jd}|durIddlm} z| jj|d t	
d| d || _W dS  |yH   Y nw |durO|nt jd	}|durs| jj|d
rst	
d| d | j|j| _dS |r|rt	
d|  | jj||d| _dS |rtd| dtd| d| d)a  
        Sets up MLflow.

        Sets the Mlflow tracking uri & token, and registry URI. Also sets
        the MLflow experiment that the logger should use, and possibly
        creates new experiment if it does not exist.

        Args:
            tracking_uri: The tracking URI for the MLflow tracking
                server.
            registry_uri: The registry URI for the MLflow model registry.
            experiment_id: The id of an already existing MLflow
                experiment to use for logging. If None is passed in
                here and the MFLOW_EXPERIMENT_ID is not set, or the
                experiment with this id does not exist,
                ``experiment_name`` will be used instead. This argument takes
                precedence over ``experiment_name`` if both are passed in.
            experiment_name: The experiment name to use for logging.
                If None is passed in here, the MLFLOW_EXPERIMENT_NAME environment
                variable is used to determine the experiment name.
                If the experiment with the name already exists with MLflow,
                it will be reused. If not, a new experiment will be created
                with the provided name if
                ``create_experiment_if_not_exists`` is set to True.
            artifact_location: The location to store run artifacts.
                If not provided, MLFlow picks an appropriate default.
                Ignored if experiment already exists.
            tracking_token: Tracking token used to authenticate with MLflow.
            create_experiment_if_not_exists: Whether to create an
                experiment with the provided name if it does not already
                exist. Defaults to True.

        Returns:
            Whether setup is successful.
        MLFLOW_TRACKING_TOKENNMLFLOW_EXPERIMENT_IDr   MlflowException)r   zExperiment with provided id z( exists. Setting that as the experiment.MLFLOW_EXPERIMENT_NAME)namezExperiment with provided name zBExisting experiment not found. Creating new experiment with name: )r%   r   z,Experiment with the provided experiment_id: zZ does not exist and no experiment_name provided. At least one of these has to be provided.z or experiment_name: zV does not exist. Please create an MLflow experiment and provide either its id or name.)osenvironr   set_tracking_uriset_registry_urigetmlflow.exceptionsr#   get_experimentloggerdebugr   get_experiment_by_namecreate_experiment
ValueError)	r   r   r   r   r   r   r   r   r#   r   r   r   setup_mlflow)   sv   -




z_MLflowLoggerUtil.setup_mlflowdict_to_logreturnc              
   C   sV   i }|  D ]"\}}z
t|}|||< W q ttfy(   td|| Y qw |S )a  Parses provided dict to convert all values to float.

        MLflow can only log metrics that are floats. This does not apply to
        logging parameters or artifacts.

        Args:
            dict_to_log: The dictionary containing the metrics to log.

        Returns:
            A dictionary containing the metrics to log with all values being
                converted to floats, or skipped if not able to be converted.
        zMCannot log key {} with value {} since the value cannot be converted to float.)itemsfloatr1   	TypeErrorr-   r.   format)r   r3   new_dictkeyvaluer   r   r   _parse_dict   s   z_MLflowLoggerUtil._parse_dictFrun_nametags
set_activer   c                 C   s   ddl }ddlm} |du ri }|r| j||dS |  }t|jtdkr3|j|| j	|d}|S |||< |j| j	|d}|S )aM  Starts a new run and possibly sets it as the active run.

        Args:
            tags: Tags to set for the new run.
            set_active: Whether to set the new run as the active run.
                If an active run already exists, then that run is returned.

        Returns:
            The newly created MLflow run.
        r   N)MLFLOW_RUN_NAME)r=   r>   z1.30.0r=   r   r>   )r   r>   )
r   mlflow.utils.mlflow_tagsr@   _start_active_run_get_clientr   parse__version__
create_runr   )r   r=   r>   r?   r   r@   clientrunr   r   r   	start_run   s   z_MLflowLoggerUtil.start_runc                 C   s&   | j  }|r	|S | j j|| j|dS )zStarts a run and sets it as the active run if one does not exist.

        If an active run already exists, then returns it.
        rA   )r   
active_runrJ   r   )r   r=   r>   rK   r   r   r   rC      s   
z#_MLflowLoggerUtil._start_active_runrun_idc                 C   s6   ddl m} z
| jj|d W dS  |y   Y dS w )z)Check if run with the provided id exists.r   r"   )rL   TF)r+   r#   r   get_run)r   rL   r#   r   r   r   _run_exists   s   z_MLflowLoggerUtil._run_existsr
   c                 C   s,   | j  }| j  }ddlm} |||dS )z@Returns an ml.tracking.MlflowClient instance to use for logging.r   r	   )r   r   )r   get_tracking_uriget_registry_urimlflow.trackingr
   )r   r   r   r
   r   r   r   rD      s   

z_MLflowLoggerUtil._get_clientparams_to_logc                 C   sj   t |}|r"| |r"|  }| D ]\}}|j|||d qdS | D ]\}}| jj||d q&dS )a  Logs the provided parameters to the run specified by run_id.

        If no ``run_id`` is passed in, then logs to the current active run.
        If there is not active run, then creates a new run and sets it as
        the active run.

        Args:
            params_to_log: Dictionary of parameters to log.
            run_id (Optional[str]): The ID of the run to log to.
        )rL   r:   r;   )r:   r;   N)r   rN   rD   r5   	log_paramr   )r   rR   rL   rH   r:   r;   r   r   r   
log_params  s   z_MLflowLoggerUtil.log_paramsmetrics_to_logc                 C   sx   t |}| |}|r(| |r(|  }| D ]\}}|j||||d qdS | D ]\}}| jj|||d q,dS )a|  Logs the provided metrics to the run specified by run_id.


        If no ``run_id`` is passed in, then logs to the current active run.
        If there is not active run, then creates a new run and sets it as
        the active run.

        Args:
            metrics_to_log: Dictionary of metrics to log.
            run_id (Optional[str]): The ID of the run to log to.
        )rL   r:   r;   step)r:   r;   rV   N)r   r<   rN   rD   r5   
log_metricr   )r   rV   rU   rL   rH   r:   r;   r   r   r   log_metrics  s   
z_MLflowLoggerUtil.log_metricsdirc                 C   s:   |r|  |r|  }|j||d dS | jj|d dS )a  Saves directory as artifact to the run specified by run_id.

        If no ``run_id`` is passed in, then saves to the current active run.
        If there is not active run, then creates a new run and sets it as
        the active run.

        Args:
            dir: Path to directory containing the files to save.
            run_id (Optional[str]): The ID of the run to log to.
        )rL   	local_dir)rZ   N)rN   rD   log_artifactsr   )r   rY   rL   rH   r   r   r   save_artifacts/  s   z _MLflowLoggerUtil.save_artifactsstatusc                 C   sV   |r"|  |r"| j r| j jj|ks"|  }|j||d dS | jj|d dS )a2  Terminates the run specified by run_id.

        If no ``run_id`` is passed in, then terminates the
        active run if one exists.

        Args:
            status (Optional[str]): The status to set when terminating the run.
            run_id (Optional[str]): The ID of the run to terminate.

        )rL   r]   )r]   N)rN   r   rK   inforL   rD   set_terminatedend_run)r   r]   rL   rH   r   r   r   r`   @  s   z_MLflowLoggerUtil.end_run)N)NNNNNNT)NNF)NN)r4   r
   )__name__
__module____qualname____doc__r   r   r   strboolr2   r   r<   rJ   rC   rN   rD   rT   rX   r\   r`   r   r   r   r   r      sj    

u
'


	r   )loggingr&   copyr   typingr   r   r   	packagingr   ray._private.dictr   mlflow.entitiesr   rQ   r
   	getLoggerra   r-   r   r   r   r   r   <module>   s    
