
    &`i                         d dl Z d dlmZ d dlmZ d dl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mZ  ed	
           G d de                      Ze G d d                      ZdS )    N)contextmanager)Path)CallableDictListOptionalUnion)Booster)RayReportCallback)
Checkpoint)
Deprecated	PublicAPIbeta)	stabilityc                   *    e Zd ZdZdej        dddfdeeee	e         e
eef         f                  dededed	eee
eeee	e         f         f         ge
eef         f                  f
 fd
Zededee         fd            Zde
defdZde
fdZ xZS )TuneReportCheckpointCallbacka"  XGBoost callback to save checkpoints and report metrics for Ray Tune.

    Args:
        metrics: Metrics to report. If this is a list,
            each item describes the metric key reported to XGBoost,
            and it will be reported under the same name.
            This can also be a dict of {<key-to-report>: <xgboost-metric-key>},
            which can be used to rename xgboost default metrics.
        filename: Customize the saved checkpoint file type by passing
            a filename. Defaults to "model.ubj".
        frequency: How often to save checkpoints, in terms of iterations.
            Defaults to 0 (no checkpoints are saved during training).
        checkpoint_at_end: Whether or not to save a checkpoint at the end of training.
        results_postprocessing_fn: An optional Callable that takes in
            the metrics dict that will be reported (after it has been flattened)
            and returns a modified dict. For example, this can be used to
            average results across CV fold when using ``xgboost.cv``.

    Examples
    --------

    Reporting checkpoints and metrics to Ray Tune when running many
    independent xgboost trials (without data parallelism within a trial).

    .. testcode::
        :skipif: True

        import xgboost

        from ray.tune import Tuner
        from ray.tune.integration.xgboost import TuneReportCheckpointCallback

        def train_fn(config):
            # Report log loss to Ray Tune after each validation epoch.
            bst = xgboost.train(
                ...,
                callbacks=[
                    TuneReportCheckpointCallback(
                        metrics={"loss": "eval-logloss"}, frequency=1
                    )
                ],
            )

        tuner = Tuner(train_fn)
        results = tuner.fit()
    Nr   Tmetricsfilename	frequencycheckpoint_at_endresults_postprocessing_fnc                 T    t                                          |||||           d S )N)r   r   r   r   r   )super__init__)selfr   r   r   r   r   	__class__s         p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/integration/xgboost.pyr   z%TuneReportCheckpointCallback.__init__?   s?     	/&? 	 	
 	
 	
 	
 	
    modelreturnc              #      K   t          j                    5 }|                    t          || j                                                             t          |          V  d d d            d S # 1 swxY w Y   d S N)tempfileTemporaryDirectory
save_modelr   	_filenameas_posixr   )r   r   temp_checkpoint_dirs      r   _get_checkpointz,TuneReportCheckpointCallback._get_checkpointQ   s      (** 	2.AT"5t~FFOOQQRRR011111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   AA//A36A3report_dictc                     |                      |          5 }t          j                            ||           d d d            d S # 1 swxY w Y   d S )N)r   )
checkpoint)r)   raytunereport)r   r*   r   r,   s       r   _save_and_report_checkpointz8TuneReportCheckpointCallback._save_and_report_checkpointW   s    !!!.. 	@*HOOKJO???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   "AA
A
c                 D    t           j                            |           d S r"   )r-   r.   r/   )r   r*   s     r   _report_metricsz,TuneReportCheckpointCallback._report_metrics[   s    $$$$$r   )__name__
__module____qualname____doc__r   CHECKPOINT_NAMEr   r	   strr   r   intboolr   floatr   r   r
   r   r)   r0   r2   __classcell__)r   s   @r   r   r      s^       - -b DH)9"& 
 
%T#YS#X >?@
 
 	

  
 $,d3eT%[&8 99:;T#u*=MMN$

 
 
 
 
 
$ 2W 2*1E 2 2 2 ^2
@t @G @ @ @ @%4 % % % % % % % %r   r   c                       e Zd ZdefdZdS )TuneReportCallbackclsc                      t          d          )Nzl`TuneReportCallback` is deprecated. Use `ray.tune.integration.xgboost.TuneReportCheckpointCallback` instead.)DeprecationWarning)r?   argskwargss      r   __new__zTuneReportCallback.__new__a   s     W
 
 	
r   N)r3   r4   r5   typerD    r   r   r>   r>   _   s/        
T 
 
 
 
 
 
r   r>   )r#   
contextlibr   pathlibr   typingr   r   r   r   r	   xgboost.corer
   ray.tuner-    ray.train.xgboost._xgboost_utilsr   r   ray.util.annotationsr   r   r   r>   rF   r   r   <module>rN      s?    % % % % % %       8 8 8 8 8 8 8 8 8 8 8 8 8 8              > > > > > >       6 6 6 6 6 6 6 6 VM% M% M% M% M%#4 M% M% M%` 
 
 
 
 
 
 
 
 
 
r   