o
    `۷i                     @   s   d dl Z d dlmZ d dlmZ d dl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)DictOptional)Booster)RayReportCallback)
Checkpoint)
Deprecated	PublicAPIbeta)	stabilityc                   @   sJ   e Zd Z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dS )TuneReportCheckpointCallbacka  Creates a callback that reports metrics and checkpoints model.

    Args:
        metrics: Metrics to report. If this is a list,
            each item should be a metric key reported by LightGBM,
            and it will be reported to Ray Train/Tune under the same name.
            This can also be a dict of {<key-to-report>: <lightgbm-metric-key>},
            which can be used to rename LightGBM default metrics.
        filename: Customize the saved checkpoint file type by passing
            a filename. Defaults to "model.txt".
        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.

    Examples
    --------

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

    .. testcode::
        :skipif: True

        import lightgbm

        from ray.tune.integration.lightgbm import TuneReportCheckpointCallback

        config = {
            # ...
            "metric": ["binary_logloss", "binary_error"],
        }

        # Report only log loss to Tune after each validation epoch.
        bst = lightgbm.train(
            ...,
            callbacks=[
                TuneReportCheckpointCallback(
                    metrics={"loss": "eval-binary_logloss"}, frequency=1
                )
            ],
        )

    modelreturnc                 c   sR    t  }|t|| j  t|V  W d    d S 1 s"w   Y  d S N)tempfileTemporaryDirectory
save_modelr   	_filenameas_posixr   from_directory)selfr   temp_checkpoint_dir r   S/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/tune/integration/lightgbm.py_get_checkpoint?   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_checkpointE   s   "z8TuneReportCheckpointCallback._save_and_report_checkpointc                 C   s   t j| d S r   )r   r   r    )r   r   r   r   r   _report_metricsI   s   z,TuneReportCheckpointCallback._report_metricsN)__name__
__module____qualname____doc__r   r   r   r   r   r   r!   r"   r   r   r   r   r      s    /r   c                   @   s   e Zd ZdefddZdS )TuneReportCallbackclsc                 O   s   t d)Nzm`TuneReportCallback` is deprecated. Use `ray.tune.integration.lightgbm.TuneReportCheckpointCallback` instead.)DeprecationWarning)r(   argskwargsr   r   r   __new__O   s   zTuneReportCallback.__new__N)r#   r$   r%   typer,   r   r   r   r   r'   M   s    r'   )r   
contextlibr   pathlibr   typingr   r   lightgbmr   ray.tuner   "ray.train.lightgbm._lightgbm_utilsr   r   ray.util.annotationsr	   r
   r   r'   r   r   r   r   <module>   s    >