o
    ci                     @   s   d dl Z d dlm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 zd d
lmZ W n eyX   G dd dZY nw G dd deZeddG dd deZdS )    N)OrderedDict)contextmanager)Path)CallableDictListOptionalUnion)Booster)
Checkpoint)flatten_dict)	PublicAPI)TrainingCallbackc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   T/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/xgboost/_xgboost_utils.pyr      s    r   c                   @   s.   e Zd ZdZdd ZdededefddZd	S )
TuneCallbackz(Base class for Tune's XGBoost callbacks.c                 C   s   |  |j|j|jS )zCompatibility with xgboost<1.3)after_iterationmodel	iterationevaluation_result_list)selfenvr   r   r   __call__   s   zTuneCallback.__call__r   epoch	evals_logc                 C   s   t N)NotImplementedError)r   r   r   r   r   r   r   r       s   zTuneCallback.after_iterationN)	r   r   r   __doc__r   r
   intr   r   r   r   r   r   r      s    r   beta)	stabilityc                   @   s   e Zd ZdZdZde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
ddZeefdededefddZdd Zededee fddZdede
de	fddZdedefddZdS )RayTrainReportCallbackaa  XGBoost callback to save checkpoints and report metrics.

    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.train.xgboost import RayTrainReportCallback

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

        tuner = Tuner(train_fn)
        results = tuner.fit()

    Loading a model from a checkpoint reported by this callback.

    .. testcode::
        :skipif: True

        from ray.train.xgboost import RayTrainReportCallback

        # Get a `Checkpoint` object that is saved by the callback during training.
        result = trainer.fit()
        booster = RayTrainReportCallback.get_model(result.checkpoint)

    z	model.ubjNr   Tmetricsfilename	frequencycheckpoint_at_endresults_postprocessing_fnc                 C   s>   t |tr|g}|| _|| _|| _|| _|| _d | _d | _d S r   )	
isinstancestr_metrics	_filename
_frequency_checkpoint_at_end_results_postprocessing_fn
_evals_log_last_checkpoint_iteration)r   r%   r&   r'   r(   r)   r   r   r   __init__c   s   


zRayTrainReportCallback.__init__
checkpointreturnc                 C   sJ   |  }t }|t||  |W  d   S 1 sw   Y  dS )a  Retrieve the model stored in a checkpoint reported by this callback.

        Args:
            checkpoint: The checkpoint object returned by a training run.
                The checkpoint should be saved by an instance of this callback.
            filename: The filename to load the model from, which should match
                the filename used when creating the callback.
        N)as_directoryr
   
load_modelr   as_posix)clsr4   r&   checkpoint_pathboosterr   r   r   	get_model}   s
   
$z RayTrainReportCallback.get_modelc                 C   s   t |trt|dd}t|D ]
}|| d ||< qnt|}| js%|}ni }| jD ]}t | jtr8| j| }n|}|| ||< q*| jrI| |}|S )N-)	delimiter)r*   r   r   listdictr,   r0   )r   r   result_dictkreport_dictkeymetricr   r   r   _get_report_dict   s"   


z'RayTrainReportCallback._get_report_dictr   c                 c   sl    t j  dv r1t }|t|| j	  t
|V  W d    d S 1 s*w   Y  d S d V  d S )N)r   N)raytrainget_contextget_world_ranktempfileTemporaryDirectory
save_modelr   r-   r8   r   )r   r   temp_checkpoint_dirr   r   r   _get_checkpoint   s   
"
z&RayTrainReportCallback._get_checkpointr   r   c                 C   s   || _ | jdk}| o|d | j dk}| |}|r?|| _| j|d}tjj||d W d    d S 1 s8w   Y  d S tj| d S )Nr      r   r4   )r1   r.   rG   r2   rP   rH   rI   report)r   r   r   r   checkpointing_disabledshould_checkpointrD   r4   r   r   r   r      s   

"z&RayTrainReportCallback.after_iterationc                 C   s   | j s|S | jd ur| d | jkr|S | jr| | jni }| j|d}tjj||d W d    |S 1 s:w   Y  |S )NrQ   rR   rS   )	r/   r2   num_boosted_roundsr1   rG   rP   rH   rI   rT   )r   r   rD   r4   r   r   r   after_training   s   

z%RayTrainReportCallback.after_training)r   r   r   r    CHECKPOINT_NAMEr   r	   r+   r   r   r!   boolr   floatr3   classmethodr   r
   r<   rG   r   rP   r   rX   r   r   r   r   r$   $   sF    ;(
	r$   )rL   collectionsr   
contextlibr   pathlibr   typingr   r   r   r   r	   xgboost.corer
   	ray.trainrH   r   ray.tune.utilsr   ray.util.annotationsr   xgboost.callbackr   ImportErrorr   r$   r   r   r   r   <module>   s$    