o
    $i                     @   s   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	d
G dd deZeG dd dZdS )    N)contextmanager)Path)CallableDictListOptionalUnion)Booster)RayReportCallback)
Checkpoint)
Deprecated	PublicAPIbeta)	stabilityc                       s   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
dZededee fddZde
defddZde
fddZ  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                    s   t  j|||||d d S )N)r   r   r   r   r   )super__init__)selfr   r   r   r   r   	__class__ Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/integration/xgboost.pyr   ?   s   

z%TuneReportCheckpointCallback.__init__modelreturnc                 c   sP    t  }|t|| j  t|V  W d    d S 1 s!w   Y  d S N)tempfileTemporaryDirectory
save_modelr   	_filenameas_posixr   )r   r   temp_checkpoint_dirr   r   r   _get_checkpointQ   s
   
"z,TuneReportCheckpointCallback._get_checkpointreport_dictc                 C   sB   | j |d}tjj||d W d    d S 1 sw   Y  d S )N)r   )
checkpoint)r&   raytunereport)r   r'   r   r(   r   r   r   _save_and_report_checkpointW   s   "z8TuneReportCheckpointCallback._save_and_report_checkpointc                 C   s   t j| d S r   )r)   r*   r+   )r   r'   r   r   r   _report_metrics[   s   z,TuneReportCheckpointCallback._report_metrics)__name__
__module____qualname____doc__r
   CHECKPOINT_NAMEr   r   strr   r   intboolr   floatr   r   r	   r   r&   r,   r-   __classcell__r   r   r   r   r      s0    1(r   c                   @   s   e Zd ZdefddZdS )TuneReportCallbackclsc                 O   s   t d)Nzl`TuneReportCallback` is deprecated. Use `ray.tune.integration.xgboost.TuneReportCheckpointCallback` instead.)DeprecationWarning)r9   argskwargsr   r   r   __new__a   s   zTuneReportCallback.__new__N)r.   r/   r0   typer=   r   r   r   r   r8   _   s    r8   )r    
contextlibr   pathlibr   typingr   r   r   r   r   xgboost.corer	   ray.tuner)    ray.train.xgboost._xgboost_utilsr
   r   ray.util.annotationsr   r   r   r8   r   r   r   r   <module>   s    P