
    &`i                     P   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZmZmZ d dlmZ d dlmZmZ d dlmZmZmZmZ d dlmZmZmZ d d	lmZ d d
l m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z'  e
d          Z( e
d          Z) e j*        e+          Z,e' G d d                      Z-dS )    N)Path)AnyCallableDictListOptionalTypeVarUnion)StartTracebackStartTracebackWithWorkerRankskip_exceptions)Dataset)
Checkpoint
DataConfig)BackendExecutorInactiveWorkerGroupErrorTrainBackendErrorTrainingWorkerError)_TrainingResult_TrainSessionget_session)ActorWrapper)BackendConfig)BaseTrainer
GenDatasetTrainingFailedError)DeveloperAPITSc                   2   e Zd ZdZdeeef         dedeeg e	f         ee
eef         ge	f         f         de
eef         de
eef         dedeee
eeef                  fd	Zd
 Z	 ddee         fdZdefdZd Zdeee
                  fdZd ZdefdZdS )TrainingIteratorzEAn iterator over Train results. Returned by ``trainer.run_iterator``.backend_executorbackend_config
train_funcdatasetsmetadatadata_config
checkpointc                     || _         |                                | _        || _        || _        || _        || _        |                     || j        | j        | j        |           d| _        d S )N)r$   r%   r&   r'   r(   F)	_backend_executorbackend_cls_backend_train_func	_datasets	_metadata_data_config_start_training_finished_training)selfr"   r#   r$   r%   r&   r'   r(   s           e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/trainer.py__init__zTrainingIterator.__init__'   s     "2&2244%!!'!^^)! 	 	
 	
 	
 #(    c                     | S N r3   s    r4   __iter__zTrainingIterator.__iter__B   s    r6   Nc           	           t                      }|s
J d            |j                              fd           d S )Nz8`_start_training` should only be called from within Tunec                  D    j                                        S )N)r$   r%   r&   r'   storager(   )r*   start_training)r(   r'   r%   r&   r3   r>   r$   s   r4   <lambda>z2TrainingIterator._start_training.<locals>.<lambda>R   s1    D*99%!!'% :   r6   )r   r>   _run_with_error_handling)r3   r$   r%   r&   r'   r(   tune_sessionr>   s   `````` @r4   r1   z TrainingIterator._start_trainingE   s     '2mmWWWWW|&%%         		
 		
 		
 		
 		
r6   funcc                 R   	  |            S # t           $ r^ t                              d           |                     | j        | j        | j        | j                   |                     |          cY S t          $ r t          d          d t          $ r t          d          d w xY w)NzSWorkers have been successfully restarted. Resuming training from latest checkpoint.zThis Trainer is not active. It is either shutdown already or never started in the first place. Either create a new Trainer or start this one.zTraining failed. You should not be seeing this error and this is a bug. Please create a new issue at https://github.com/ray-project/ray.)r   loggerinfor1   r-   r.   r/   r0   rA   r   RuntimeErrorr   )r3   rC   s     r4   rA   z)TrainingIterator._run_with_error_handling\   s    	466M" 	7 	7 	7 KK3      !	   0066666' 	 	 	A  	
 ! 	 	 	6 
 	s   	 A%B&33B&c                    |                                  r"| j                            d           t          	 |                     | j                  }|C| j                            d           |                     | j                   d| _        t          |S # t          $ r}t          |t                    r|j        }nd }t          |          }d                    t          j        t!          |          ||j                            }| j                            d||           	 | j                            d           d| _        n# t&          $ r Y nw xY w d }~ww xY w)NF)erroredT )rI   stack_tracefailed_rank)graceful_termination)is_finishedr*   report_final_run_statusStopIterationrA   _fetch_next_result_finish_trainingr2   r   
isinstancer   worker_rankr   join	tracebackformat_exceptiontype__traceback__shutdown	Exception)r3   next_resultserL   rK   s        r4   __next__zTrainingIterator.__next__|   s    	 "::5:III!	889PQQL#&>>u>MMM--d.CDDD*.'#### 	 	 	 !9:: #m"  ""A''*477AqGG K "::+; ;    &//U/KKK*.''   1	s=   A B 
E##BE*"EE
EEEEE#returnc                 v    | j                                         }|dS t          d |D                       sJ |S )a  Fetch next results produced by ``session.report()`` from each worker.

        Assumes ``start_training`` has already been called.

        Returns:
            A list of dictionaries of values passed to ``session.report()`` from
                each worker. Each item corresponds to an intermediate result
                a single worker. If there are no more items to fetch,
                returns None.
        Nc              3   @   K   | ]}t          |t                    V  d S r8   )rS   r   ).0results     r4   	<genexpr>z6TrainingIterator._fetch_next_result.<locals>.<genexpr>   s,      MM6:fo66MMMMMMr6   )r*   get_next_resultsall)r3   resultss     r4   rQ   z#TrainingIterator._fetch_next_result   sJ     (99;;?4MMWMMMMMMMMr6   c                 4    | j                                         S )at  Finish training and return final results. Propagate any exceptions.

        Blocks until training is finished on all workers.

        Assumes `start_training` has already been called.

        Returns:
            A list of return values from calling ``train_func`` on each worker.
                Each item corresponds to the return value from a single worker.
        )r*   finish_trainingr:   s    r4   rR   z!TrainingIterator._finish_training   s     %55777r6   c                     | j         S r8   )r2   r:   s    r4   rN   zTrainingIterator.is_finished   s    &&r6   r8   )__name__
__module____qualname____doc__r
   r   r   r   r   r   r   strr   r   r   r   r   r   r5   r;   r1   rA   r^   r   rQ   rR   boolrN   r9   r6   r4   r!   r!   #   s}       OO( =>( &( (2q5/8T#s(^4Da4G+HHI	(
 sG|$( sCx.(  ( U4dJ#>?@( ( ( (6   ,0
 
 Z(
 
 
 
.X    @% % %NHT$Z$8    "8 8 8'T ' ' ' ' ' 'r6   r!   ).loggingrV   pathlibr   typingr   r   r   r   r   r	   r
   ray.air._internal.utilr   r   r   ray.datar   	ray.trainr   r   $ray.train._internal.backend_executorr   r   r   r   ray.train._internal.sessionr   r   r   ray.train._internal.utilsr   ray.train.backendr   ray.train.base_trainerr   r   r   ray.util.annotationsr   r   r   	getLoggerrk   rE   r!   r9   r6   r4   <module>r~      s              F F F F F F F F F F F F F F F F F F         
       , , , , , , , ,            T S S S S S S S S S 2 2 2 2 2 2 + + + + + +         
 . - - - - -GCLLGCLL		8	$	$ ^' ^' ^' ^' ^' ^' ^' ^' ^' ^'r6   