
    &`iI                        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
mZ d dlmZ d dlmZ er*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mZmZmZ d dlmZ e G d de                      Ze G d de                      Z e G d de                      Z!e G d de                      Z"e G d de                      Z#dS )    )contextmanager)TYPE_CHECKINGAnyDictListOptional)_TrainingReport)RayTrainCallback)ScalingConfig)DeveloperAPI)TrainRunContext)TrainControllerState)FailureDecision)ResizeDecision)WorkerWorkerGroupWorkerGroupContextWorkerGroupPollStatus)Resultc                       e Zd Zded         deeee         f         fdZed             Z	ddZ
ddZddZed             ZddZddZ	 	 ddZddZddZdS )WorkerGroupCallbackworkersr   returnc                     i S )aJ  Called before initializing the TrainContext for the worker_group.

        Return:
            A dictionary of additional arguments for TrainContext.
            The key is the argument name and the value is a list of argument values
            to pass to the TrainContext constructor of each worker in the worker group.
         )selfr   s     }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/v2/_internal/execution/callback.pybefore_init_train_contextz-WorkerGroupCallback.before_init_train_context   s	     	    c              #      K   d V  d S Nr   r   s    r   on_worker_group_startz)WorkerGroupCallback.on_worker_group_start'         r   worker_group_contextr   c                     dS )z6Called before the worker group actors are initialized.Nr   r   r%   s     r   before_worker_group_startz-WorkerGroupCallback.before_worker_group_start+       r   worker_groupr   c                     dS )zkCalled after the worker group actors are initialized.
        All workers should be ready to execute tasks.Nr   r   r*   s     r   after_worker_group_startz,WorkerGroupCallback.after_worker_group_start/   	     	r   c                     d S r!   r   r,   s     r   !after_worker_group_training_startz5WorkerGroupCallback.after_worker_group_training_start4       r   c              #      K   d V  d S r!   r   r"   s    r   on_worker_group_shutdownz,WorkerGroupCallback.on_worker_group_shutdown7   r$   r   c                     dS )zCalled before the worker group is shut down.
        Workers may be dead at this point due to actor failures, so this method
        should catch and handle exceptions if attempting to execute tasks.Nr   r,   s     r   before_worker_group_shutdownz0WorkerGroupCallback.before_worker_group_shutdown;   s	     	r   c                     dS )z+Called after the worker group is shut down.Nr   r'   s     r   after_worker_group_shutdownz/WorkerGroupCallback.after_worker_group_shutdownA   r)   r   worker_group_statusr   c                     d S r!   r   )r   r8   s     r   after_worker_group_poll_statusz2WorkerGroupCallback.after_worker_group_poll_statusE   r.   r   c                     dS )z*Called before the worker group is aborted.Nr   r'   s     r   before_worker_group_abortz-WorkerGroupCallback.before_worker_group_abortJ   r)   r   c                     dS )z)Called after the worker group is aborted.Nr   r'   s     r   after_worker_group_abortz,WorkerGroupCallback.after_worker_group_abortN   r)   r   N)r%   r   )r*   r   )r8   r   )__name__
__module____qualname__r   r   strr   r   r   r#   r(   r-   r0   r3   r5   r7   r:   r<   r>   r   r   r   r   r      s'       
H~
	c49n	
 
 
 
   ^      
      ^      #:   
        r   r   c            	       |    e Zd ZddZdededeeeef                  fdZ	d Z
	 	 	 	 ddZ	 	 ddZ	 	 ddZddZdS )ControllerCallbacktrain_run_contextr   c                     dS )zkCalled immediately after `TrainController.run` is called,
        before the control loop starts executing.Nr   )r   rE   s     r   after_controller_startz)ControllerCallback.after_controller_startU   r.   r   scaling_confignum_workersr   c                    dS )a   Called by the TrainController before the worker group is started.

        This hook can be used to perform setup that modifies the worker group's
        placement, such as reserving an accelerator slice.

        Args:
            scaling_config: The scaling configuration for the run.
            num_workers: The number of workers to be started.

        Returns:
            An optional dictionary defining a `label_selector`
            to gang schedule the worker group on the reserved TPU slice.
        Nr   )r   rH   rI   s      r    on_controller_start_worker_groupz3ControllerCallback.on_controller_start_worker_group^   s	      tr   c                     dS )zUCalled before `TrainController.run` exits,
        after the control loop has exited.Nr   r"   s    r   before_controller_shutdownz-ControllerCallback.before_controller_shutdownp   r.   r   previous_stater   current_statec                     dS )z0Called whenever the controller state is updated.Nr   )r   rN   rO   s      r   after_controller_state_updatez0ControllerCallback.after_controller_state_updateu   	     	r   failure_decisionr   c                     dS )z9Called before the controller executes a failure decision.Nr   )r   rS   s     r   *before_controller_execute_failure_decisionz=ControllerCallback.before_controller_execute_failure_decision}   	    
 	r   resize_decisionr   c                     dS )z8Called before the controller executes a resize decision.Nr   )r   rW   s     r   )before_controller_execute_resize_decisionz<ControllerCallback.before_controller_execute_resize_decision   rV   r   resultr   c                     dS )zCalled after the training run completes, providing access to the final result.

        Args:
            result: The final training result containing metrics and checkpoint.
        Nr   )r   rZ   s     r   after_controller_finishz*ControllerCallback.after_controller_finish   rR   r   N)rE   r   )rN   r   rO   r   )rS   r   )rW   r   )rZ   r   )r?   r@   rA   rG   r   intr   r   rB   rK   rM   rQ   rU   rY   r\   r   r   r   rD   rD   S   s           !.=@	$sCx.	!   $  
. .   +   )        r   rD   c                   :    e Zd Zdedeeeef                  fdZdS )ReportCallbacktraining_reportmetricsc                     dS )aQ  Called after all workers have reported a training result.

        Note that this differs from `after_worker_group_poll_status`,
        which may only contain a subset of workers that have reported.
        For example, if only rank 0 is performing checkpointing, then
        rank 0 would report a training result the slowest.
        Nr   )r   r`   ra   s      r   after_reportzReportCallback.after_report   s	     	r   N)	r?   r@   rA   r	   r   r   rB   r   rc   r   r   r   r_   r_      sG        ( d38n%     r   r_   c                       e Zd ZdZd Zd ZdS )WorkerCallbacka  
    Callbacks that are hooked to the worker event.

    These callbacks are created on the train driver process and then
    copied and passed to all the workers.
    The execution of these callbacks happens on each of the workers,
    not on the train driver process.
    c                     d S r!   r   r"   s    r   after_init_train_contextz'WorkerCallback.after_init_train_context   r1   r   c                     d S r!   r   r"   s    r   before_worker_shutdownz%WorkerCallback.before_worker_shutdown   r1   r   N)r?   r@   rA   __doc__rg   ri   r   r   r   re   re      s<               r   re   c                   (    e Zd ZdZed             ZdS )TrainContextCallbacka  
    Callbacks that are hooked to the train context event.

    These callbacks are created on the train driver process and then
    copied and passed to all the workers.
    The execution of these callbacks happens on the train context of the workers.
    c              #      K   d V  d S r!   r   r"   s    r   	on_reportzTrainContextCallback.on_report   r$   r   N)r?   r@   rA   rj   r   rn   r   r   r   rl   rl      s9            ^  r   rl   N)$
contextlibr   typingr   r   r   r   r   0ray.train.v2._internal.execution.training_reportr	   ray.train.v2.api.callbackr
   ray.train.v2.api.configr   ray.util.annotationsr   (ray.train.v2._internal.execution.contextr   +ray.train.v2._internal.execution.controllerr   1ray.train.v2._internal.execution.failure_handlingr   /ray.train.v2._internal.execution.scaling_policyr   -ray.train.v2._internal.execution.worker_groupr   r   r   r   ray.train.v2.api.resultr   r   rD   r_   re   rl   r   r   r   <module>r{      sW   % % % % % % ; ; ; ; ; ; ; ; ; ; ; ; ; ; L L L L L L 6 6 6 6 6 6 1 1 1 1 1 1 - - - - - - /HHHHHH      RQQQQQNNNNNN            /..... 6 6 6 6 6* 6 6 6r = = = = =) = = =B     %         %   "     +     r   