
    &`i3                        d dl Z d dlmZ d dlmZmZ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 d dlmZ 	 d dlZn# e$ r dZY nw xY w e j        e          Z G d d          Z ed          	 	 	 	 	 	 	 	 	 	 	 d!dee         dee         dee         dee         dee         dee         dee         dee         de dee         de deeef         fd            Z! G d d e          Z"dS )"    N)
ModuleType)DictOptionalUnion)usage)_MLflowLoggerUtil)TRAINING_ITERATION)Trial)LoggerCallback)TIMESTEPS_TOTAL)_in_tune_session)	PublicAPIc                       e Zd Zd Zd ZdS )_NoopModulec                     t                      S N)r   )selfitems     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/integrations/mlflow.py__getattr__z_NoopModule.__getattr__   s    }}    c                     d S r    r   argskwargss      r   __call__z_NoopModule.__call__   s    tr   N)__name__
__module____qualname__r   r   r   r   r   r   r      s2              r   r   alpha)	stabilityFTconfigtracking_uriregistry_uriexperiment_idexperiment_nametracking_tokenartifact_locationrun_namecreate_experiment_if_not_existstagsrank_zero_onlyreturnc           	         t           st          d          d}d}	 t                      rGt          j                                        }|                                }|                                }nFt          j                                        }|
r&|	                                dk    rt                      S n# t          $ r d}d}Y nw xY w| r|                                 ni }|p|}|p|}t                      }|                    |||||||           |                    |p||	d           |                    |           t!          j                     |j        S )a  Set up a MLflow session.

    This function can be used to initialize an MLflow session in a
    (distributed) training or tuning run. The session will be created on the trainable.

    By default, the MLflow experiment ID is the Ray trial ID and the
    MLlflow experiment name is the Ray trial name. These settings can be overwritten by
    passing the respective keyword arguments.

    The ``config`` dict is automatically logged as the run parameters (excluding the
    mlflow settings).

    In distributed training with Ray Train, only the zero-rank worker will initialize
    mlflow. All other workers will return a noop client, so that logging is not
    duplicated in a distributed run. This can be disabled by passing
    ``rank_zero_only=False``, which will then initialize mlflow in every training
    worker. Note: for Ray Tune, there's no concept of worker ranks, so the `rank_zero_only` is ignored.

    This function will return the ``mlflow`` module or a noop module for
    non-rank zero workers ``if rank_zero_only=True``. By using
    ``mlflow = setup_mlflow(config)`` you can ensure that only the rank zero worker
    calls the mlflow API.

    Args:
        config: Configuration dict to be logged to mlflow as parameters.
        tracking_uri: The tracking URI for MLflow tracking. If using
            Tune in a multi-node setting, make sure to use a remote server for
            tracking.
        registry_uri: The registry URI for the MLflow model registry.
        experiment_id: The id of an already created MLflow experiment.
            All logs from all trials in ``tune.Tuner()`` will be reported to this
            experiment. If this is not provided or the experiment with this
            id does not exist, you must provide an``experiment_name``. This
            parameter takes precedence over ``experiment_name``.
        experiment_name: The name of an already existing MLflow
            experiment. All logs from all trials in ``tune.Tuner()`` will be
            reported to this experiment. If this is not provided, you must
            provide a valid ``experiment_id``.
        tracking_token: A token to use for HTTP authentication when
            logging to a remote tracking server. This is useful when you
            want to log to a Databricks server, for example. This value will
            be used to set the MLFLOW_TRACKING_TOKEN environment variable on
            all the remote training processes.
        artifact_location: The location to store run artifacts.
            If not provided, MLFlow picks an appropriate default.
            Ignored if experiment already exists.
        run_name: Name of the new MLflow run that will be created.
            If not set, will default to the ``experiment_name``.
        create_experiment_if_not_exists: Whether to create an
            experiment with the provided name if it does not already
            exist. Defaults to False.
        tags: Tags to set for the new run.
        rank_zero_only: If True, will return an initialized session only for the
            rank 0 worker in distributed training. If False, will initialize a
            session for all workers. Defaults to True.

    Example:

        Per default, you can just call ``setup_mlflow`` and continue to use
        MLflow like you would normally do:

        .. code-block:: python

            from ray.air.integrations.mlflow import setup_mlflow

            def training_loop(config):
                mlflow = setup_mlflow(config)
                # ...
                mlflow.log_metric(key="loss", val=0.123, step=0)

        In distributed data parallel training, you can utilize the return value of
        ``setup_mlflow``. This will make sure it is only invoked on the first worker
        in distributed training runs.

        .. code-block:: python

            from ray.air.integrations.mlflow import setup_mlflow

            def training_loop(config):
                mlflow = setup_mlflow(config)
                # ...
                mlflow.log_metric(key="loss", val=0.123, step=0)


        You can also use MlFlow's autologging feature if using a training
        framework like Pytorch Lightning, XGBoost, etc. More information can be
        found here
        (https://mlflow.org/docs/latest/tracking.html#automatic-logging).

        .. code-block:: python

            from ray.air.integrations.mlflow import setup_mlflow

            def train_fn(config):
                mlflow = setup_mlflow(config)
                mlflow.autolog()
                xgboost_results = xgb.train(config, ...)

    z?mlflow was not found - please install with `pip install mlflow`Nr   )r$   r%   r&   r'   r(   r)   r+   T)r*   r,   
set_active)mlflowRuntimeErrorr   raytuneget_contextget_trial_idget_trial_nametrainget_world_rankr   copyr   setup_mlflow	start_run
log_params	air_usagetag_setup_mlflow_mlflow)r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   default_trial_iddefault_trial_namecontext_configmlflow_utils                   r   r;   r;       s   b  
M
 
 	
 " 	%,/H,@,@,B,BG&3355!(!7!7!9!9.1i.C.C.E.EG %'"8"8":":a"?"?"}}$ " " "!"  &-fkkmmm2G!5%5M%;);O $%%K!!#'%+(G     ,_    
 7###    s   BB8 8C	C	c                       e Zd ZdZ	 dddddddddee         dee         dee         dee         d	ee         d
edefdZd Z	ddZ
dedddefdZddddefdZdS )MLflowLoggerCallbacka	  MLflow Logger to automatically log Tune results and config to MLflow.

    MLflow (https://mlflow.org) Tracking is an open source library for
    recording and querying experiments. This Ray Tune ``LoggerCallback``
    sends information (config parameters, training results & metrics,
    and artifacts) to MLflow for automatic experiment tracking.

    Keep in mind that the callback will open an MLflow session on the driver and
    not on the trainable. Therefore, it is not possible to call MLflow functions
    like ``mlflow.log_figure()`` inside the trainable as there is no MLflow session
    on the trainable. For more fine grained control, use
    :func:`ray.air.integrations.mlflow.setup_mlflow`.

    Args:
        tracking_uri: The tracking URI for where to manage experiments
            and runs. This can either be a local file path or a remote server.
            This arg gets passed directly to mlflow
            initialization. When using Tune in a multi-node setting, make sure
            to set this to a remote server and not a local file path.
        registry_uri: The registry URI that gets passed directly to
            mlflow initialization.
        experiment_name: The experiment name to use for this Tune run.
            If the experiment with the name already exists with MLflow,
            it will be reused. If not, a new experiment will be created with
            that name.
        tags: An optional dictionary of string keys and values to set
            as tags on the run
        tracking_token: Tracking token used to authenticate with MLflow.
        save_artifact: If set to True, automatically save the entire
            contents of the Tune local_dir as an artifact to the
            corresponding run in MlFlow.
        log_params_on_trial_end: If set to True, log parameters to MLflow
            at the end of the trial instead of at the beginning

    Example:

    .. code-block:: python

        from ray.air.integrations.mlflow import MLflowLoggerCallback

        tags = { "user_name" : "John",
                 "git_commit_hash" : "abc123"}

        tune.run(
            train_fn,
            config={
                # define search space here
                "parameter_1": tune.choice([1, 2, 3]),
                "parameter_2": tune.choice([4, 5, 6]),
            },
            callbacks=[MLflowLoggerCallback(
                experiment_name="experiment1",
                tags=tags,
                save_artifact=True,
                log_params_on_trial_end=True)])

    NF)r%   r'   r,   r(   save_artifactlog_params_on_trial_endr$   r%   r'   r,   r(   rH   rI   c                   || _         || _        || _        || _        || _        || _        || _        t                      | _        t          j
        j        j	                                        rt                              d           d S d S )NzWhen using MLflowLoggerCallback with Ray Client, it is recommended to use a remote tracking server. If you are using a MLflow tracking server backed by the local filesystem, then it must be setup on the server side and not on the client side.)r$   r%   r'   r,   r(   should_save_artifactrI   r   rE   r3   utilclientis_connectedloggerwarning)r   r$   r%   r'   r,   r(   rH   rI   s           r   __init__zMLflowLoggerCallback.__init__   s     )(.	,$1!'>$,..8?++-- 	NN    	 	r   c                     | j                             | j        | j        | j        | j                   | j        i | _        i | _        d S )N)r$   r%   r'   r(   )rE   r;   r$   r%   r'   r(   r,   _trial_runsr   s      r   setupzMLflowLoggerCallback.setup  sY    %%** 0.	 	& 	
 	
 	
 9DIr   trialr
   c                 Z   || j         vrh| j                                        }t          |          |d<   | j                            |t          |                    }|j        j        | j         |<   | j         |         }|j        }| j	        s| j        
                    ||           d S d S )N
trial_name)r,   r*   run_idparams_to_log)rS   r,   r:   strrE   r<   inforY   r#   rI   r=   )r   rU   r,   runrY   r#   s         r   log_trial_startz$MLflowLoggerCallback.log_trial_start.  s    ((( 9>>##D!$UD",,$U,LLC&)hoDU#!%( + 	M''vV'LLLLL	M 	Mr   	iterationresultc                     |                     t                    p|t                   }| j        |         }| j                            |||           d S )N)rY   metrics_to_logstep)getr   r	   rS   rE   log_metrics)r   r_   rU   r`   rc   rY   s         r   log_trial_resultz%MLflowLoggerCallback.log_trial_result@  sP    zz/**Hf5G.H!%($$F6PT$UUUUUr   failedc                    | j         |         }| j        r!| j                            ||j                   |sdnd}|j        }| j        r| j                            ||           | j                            ||           d S )N)rY   dirFINISHEDFAILEDrX   )rY   status)	rS   rK   rE   save_artifacts
local_pathr#   rI   r=   end_run)r   rU   rg   rY   rl   r#   s         r   log_trial_endz"MLflowLoggerCallback.log_trial_endE  s    !%( $ 	Q++6u?O+PPP $*7x ' 	M''vV'LLL  v >>>>>r   r   )rU   r
   )F)r   r   r    __doc__r   r[   r   boolrQ   rT   r^   intrf   rp   r   r   r   rG   rG      s2       8 8x '+ '+)-#(,#(-  sm sm	
 "# tn !  "&   @  M M M M$V# Vg Vt V V V V
? ?7 ?D ? ? ? ? ? ?r   rG   )NNNNNNNNFNT)#loggingtypesr   typingr   r   r   r3   ray.air._internalr   r>   ray.air._internal.mlflowr   ray.air.constantsr	   ray.tune.experimentr
   ray.tune.loggerr   ray.tune.resultr   %ray.tune.trainable.trainable_fn_utilsr   ray.util.annotationsr   r1   ImportError	getLoggerr   rO   r   r[   rr   r;   rG   r   r   r   <module>r      s          ( ( ( ( ( ( ( ( ( ( 



 0 0 0 0 0 0 6 6 6 6 6 6 0 0 0 0 0 0 % % % % % % * * * * * * + + + + + + B B B B B B * * * * * *MMMM   FFF 
	8	$	$        W!"&"&#'%)$('+",1` `TN`3-` 3-` C=	`
 c]` SM`  }` sm` &*` 4.` ` :{"#` ` ` `FP? P? P? P? P?> P? P? P? P? P?s   
A AA