
    &`iD                     ^   U d dl Z d dlZd dlZd dlZd dlmZ d dl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 d dlZd dlmZ d d	lmZmZ  ej        e          Ze G d
 de                      Z G d de          Z G d de          Zi aeed<    ej                    Z d Z!defdZ"dS )    Ncontextmanager)	dataclass)Optional)Version)RabitTracker)CommunicatorContext)BaseWorkerGroup)BackendBackendConfigc                   N    e Zd ZU dZdZeed<   ed             Zed             Z	dS )XGBoostConfiga  Configuration for xgboost collective communication setup.

    Ray Train will set up the necessary coordinator processes and environment
    variables for your workers to communicate with each other.
    Additional configuration options can be passed into the
    `xgboost.collective.CommunicatorContext` that wraps your own `xgboost.train` code.

    See the `xgboost.collective` module for more information:
    https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/collective.py

    Args:
        xgboost_communicator: The backend to use for collective communication for
            distributed xgboost training. For now, only "rabit" is supported.
    rabitxgboost_communicatorc                 &    t           d             }|S )Nc               3   t   K   t          di t                      5  d V  d d d            d S # 1 swxY w Y   d S )N )r	   _get_xgboost_argsr       l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/xgboost/config.py collective_communication_contextzJXGBoostConfig.train_func_context.<locals>.collective_communication_context*   s      $;;'8':':;;                   s   -11r   )selfr   s     r   train_func_contextz XGBoostConfig.train_func_context(   s#    		 	 
	 0/r   c                     | j         dk    r8t          t          j                  t          d          k    rt          nt
          S t          d| j                    )Nr   z2.1.0zUnsupported backend: )r   r   xgboost__version___XGBoostRabitBackend_XGBoostRabitBackend_pre_xgb210NotImplementedErrorr   s    r   backend_clszXGBoostConfig.backend_cls1   s\    $// 7.//773C3CCC %$4 ""U$:S"U"UVVVr   N)
__name__
__module____qualname____doc__r   str__annotations__propertyr   r!   r   r   r   r   r      sl           !(#'''0 0 X0 W W XW W Wr   r   c                   @    e Zd Zd ZdefdZdedefdZdedefdZdS )r   c                 "    d | _         d | _        d S N)_tracker_wait_threadr    s    r   __init__z_XGBoostRabitBackend.__init__>   s    048<r   worker_groupc                 4   t          |          }d|i}t          j                                        }t	          ||d          | _        | j                                         t          j        | j        j	        d          | _
        | j
                                         |                    | j                                                   dt          j        |d           }t                              |           d	 }|                    ||           d S )
N	n_workerstaskr1   host_ipsortbyT)targetdaemon2RabitTracker coordinator started with parameters:
   indentc                     dd l }d|j                                                                        dd |j                                                     | d<   t          |            d S )Nr   [xgboost.ray-rank=08]:dmlc_task_id)	ray.traintrainget_contextget_world_rankget_runtime_contextget_actor_id_set_xgboost_args)argsrays     r   set_xgboost_communicator_argsz^_XGBoostRabitBackend._setup_xgboost_distributed_backend.<locals>.set_xgboost_communicator_args^   s    >SY%:%:%<%<%K%K%M%MT > >*3*,,99;;> >  
 d#####r   )lenrI   utilget_node_ip_addressr   r,   start	threadingThreadwait_forr-   updateworker_argsjsondumpsloggerdebugexecute)r   r/   num_workers
rabit_argstrain_driver_ip	start_logrJ   s          r   "_setup_xgboost_distributed_backendz7_XGBoostRabitBackend._setup_xgboost_distributed_backendB   s    ,''!;/
(6688 %!?6
 
 
 	 &,DM4JSWXXX!!!$-33556662z*Q///2 2 	 	Y	$ 	$ 	$ 	:JGGGGGr   backend_configc                 J    |j         dk    sJ |                     |           d S Nr   r   r]   r   r/   r^   s      r   on_training_startz&_XGBoostRabitBackend.on_training_startj   2     2g====//=====r   c                     d}| j         T| j                             |           | j                                         r"t                              d| d           d S d S d S N   )timeoutz?During shutdown, the RabitTracker thread failed to join within zL seconds. The process will still be terminated as part of Ray actor cleanup.)r-   joinis_aliverV   warningr   r/   r^   rh   s       r   on_shutdownz _XGBoostRabitBackend.on_shutdownp   s    (""7"333 ))++ Y%Y Y Y    	 )( r   N	r"   r#   r$   r.   r
   r]   r   rc   rm   r   r   r   r   r   =   s        = = =&H &H &H &H &HP>+>=J> > > >       r   r   c                   @    e Zd Zd ZdefdZdedefdZdedefdZdS )r   c                     d | _         d S r+   )r,   r    s    r   r.   z(_XGBoostRabitBackend_pre_xgb210.__init__   s    04r   r/   c                    t          |          }d|it          j                                        }t	          ||d          | _        | j                            |           | j                                        }                    |           dt          j
        d           }t                              |           fd}|                    |           d S )	NDMLC_NUM_WORKERr2   r3   )r1   r8   r9   r:   c                  8   dd l }                                 D ]!\  }}t          |          t          j        |<   "d| j                                                                        dd | j                    	                                 t          j        d<   d S )Nr   r=   r>   r?   DMLC_TASK_ID)
rA   itemsr&   osenvironrB   rC   rD   rE   rF   )rI   kvrZ   s      r   set_xgboost_env_varsz`_XGBoostRabitBackend_pre_xgb210._setup_xgboost_distributed_backend.<locals>.set_xgboost_env_vars   s    "((** ' '1 #A
1
>SY%:%:%<%<%K%K%M%MT > >*3*,,99;;> > J~&&&r   )rK   rI   rL   rM   r   r,   rN   worker_envsrR   rT   rU   rV   rW   rX   )r   r/   rY   r[   rS   r\   rz   rZ   s          @r   r]   zB_XGBoostRabitBackend_pre_xgb210._setup_xgboost_distributed_backend   s    ,'''5
(6688 %!?6
 
 
 	k222m//11+&&&2z*Q///2 2 	 	Y	 	 	 	 	 	122222r   r^   c                 J    |j         dk    sJ |                     |           d S r`   ra   rb   s      r   rc   z1_XGBoostRabitBackend_pre_xgb210.on_training_start   rd   r   c                     | j         sd S d}| j         j                            |           | j         j                                        r t                              d| d           d S d S rf   )r,   threadri   rj   rV   rk   rl   s       r   rm   z+_XGBoostRabitBackend_pre_xgb210.on_shutdown   s    } 	F!!'!222=((** 	NNU!U U U    	 	r   Nrn   r   r   r   r   r   ~   s        5 5 5%3 %3 %3 %3 %3N>+>=J> > > >       r   r   _xgboost_argsc                 J    t           5  | ad d d            d S # 1 swxY w Y   d S r+   _xgboost_args_lockr   )rH   s    r   rG   rG      sq    	                   s   returnc                  R    t           5  t          cd d d            S # 1 swxY w Y   d S r+   r   r   r   r   r   r      so    	                   s     )#rT   loggingrv   rO   
contextlibr   dataclassesr   typingr   r   packaging.versionr   r   xgboost.collectiver	   rI   %ray.train._internal.base_worker_groupr
   ray.train.backendr   r   	getLoggerr"   rV   r   r   r   r   dictr'   Lockr   rG   r   r   r   r   <module>r      s     				     % % % % % % ! ! ! ! ! !        % % % % % %             2 2 2 2 2 2 



 A A A A A A 4 4 4 4 4 4 4 4		8	$	$ $W $W $W $W $WM $W $W $WN> > > > >7 > > >B= = = = =g = = =@ t   #Y^%%   4      r   