
    ^is'                     J   U d Z 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m	Z	 ddl
mZmZmZmZm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  ej6                  d	      Zeeeeef   f   Zee d
<   eeeef      Z!ee d<   eee!f   Z"ee d<   e G d d             Z#de!ddfdZ$d&dZ%defdZ&defdZ'de(fdZ)deddfdZ*defdZ+dededefdZ,dejZ                  defdZ.e	 G d de             Z/dej`                  d e/dej`                  fd!Z1d&d"Z2dee   fd#Z3 G d$ d%      Z4y)'z-XGBoost collective communication related API.    N)	dataclass)IntEnumunique)AnyDictOptional	TypeAliasUnion   )_T)_LIB_check_call
build_infoc_strmake_jcargspy_strz[xgboost.collective]_Conf_ArgVals_Argsc                       e Zd ZU dZdZee   ed<   dZee   ed<   dZ	ee
   ed<   dZee   ed<   dZee   ed<   ded	efd
Zy)Configa  User configuration for the communicator context. This is used for easier
    integration with distributed frameworks. Users of the collective module can pass the
    parameters directly into tracker and the communicator.

    .. versionadded:: 3.0

    Attributes
    ----------
    retry : See `dmlc_retry` in :py:meth:`init`.

    timeout :
        See `dmlc_timeout` in :py:meth:`init`. This is only used for communicators, not
        the tracker. They are different parameters since the timeout for tracker limits
        only the time for starting and finalizing the communication group, whereas the
        timeout for communicators limits the time used for collective operations, like
        :py:meth:`allreduce`.

    tracker_host_ip : See :py:class:`~xgboost.tracker.RabitTracker`.

    tracker_port : See :py:class:`~xgboost.tracker.RabitTracker`.

    tracker_timeout : See :py:class:`~xgboost.tracker.RabitTracker`.

    Nretrytimeouttracker_host_iptracker_porttracker_timeoutargsreturnc                 r    | j                   | j                   |d<   | j                  | j                  |d<   |S )z*Update the arguments for the communicator.
dmlc_retrydmlc_timeout)r   r   selfr   s     S/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/xgboost/collective.pyget_comm_configzConfig.get_comm_config:   s7    ::!!%D<<##'<<D     )__name__
__module____qualname____doc__r   r   int__annotations__r   r   strr   r   r   r%    r&   r$   r   r      sb    2  E8C=!GXc]!%)OXc])"&L(3-&%)OXc])E e r&   r   r   r   c            	      R    t        t        j                  t        di |              y)a  Initialize the collective library with arguments.

    Parameters
    ----------
    args :
        Keyword arguments representing the parameters and their values.

        Accepted parameters:
          - dmlc_communicator: The type of the communicator.
            * rabit: Use Rabit. This is the default if the type is unspecified.
            * federated: Use the gRPC interface for Federated Learning.

        Only applicable to the Rabit communicator:
          - dmlc_tracker_uri: Hostname of the tracker.
          - dmlc_tracker_port: Port number of the tracker.
          - dmlc_task_id: ID of the current task, can be used to obtain deterministic
          - dmlc_retry: The number of retry when handling network errors.
          - dmlc_timeout: Timeout in seconds.
          - dmlc_nccl_path: Path to load (dlopen) nccl for GPU-based communication.

        Only applicable to the Federated communicator:
          - federated_server_address: Address of the federated server.
          - federated_world_size: Number of federated workers.
          - federated_rank: Rank of the current worker.
          - federated_server_cert: Server certificate file path. Only needed for the SSL
            mode.
          - federated_client_key: Client key file path. Only needed for the SSL mode.
          - federated_client_cert: Client certificate file path. Only needed for the SSL
            mode.

        Use upper case for environment variables, use lower case for runtime
        configuration.

    Nr.   )r   r   XGCommunicatorInitr   )r   s    r$   initr1   C   s     F ''(;d(;<=r&   c                  >    t        t        j                                y)zFinalize the communicator.N)r   r   XGCommunicatorFinalizer.   r&   r$   finalizer4   i   s    ++-.r&   c                  .    t        j                         } | S )zjGet rank of current process.

    Returns
    -------
    rank : int
        Rank of current process.
    )r   XGCommunicatorGetRankrets    r$   get_rankr9   n   s     
$
$
&CJr&   c                  .    t        j                         } | S )z`Get total number workers.

    Returns
    -------
    n :
        Total number of process.
    )r   XGCommunicatorGetWorldSizer7   s    r$   get_world_sizer<   z   s     
)
)
+CJr&   c                  @    t        j                         } t        |       S )z.If the collective communicator is distributed.)r   XGCommunicatorIsDistributedbool)is_dists    r$   is_distributedrA      s    ..0G=r&   msgc                    t        | t              st        |       } t        j                         }|dk7  r6t	        t        j
                  t        | j                                            yt        | j                         d       y)zPrint message to the communicator.

    This function can be used to communicate the information of
    the progress to the communicator.

    Parameters
    ----------
    msg : str
        The message to be printed to the communicator.
    r   T)flushN)	
isinstancer-   r   r>   r   XGCommunicatorPrintr   stripprint)rB   r@   s     r$   communicator_printrI      sZ     c3#h..0G!|D,,U399;-?@Aciik&r&   c                      t        j                         } t        t        j                  t        j
                  |                    | j                  }t        |      S )zdGet the processor name.

    Returns
    -------
    name :
        The name of processor(host)
    )ctypesc_char_pr   r   XGCommunicatorGetProcessorNamebyrefvaluer   )name_strrO   s     r$   get_processor_namerQ      s?      H33FLL4JKLNNE%=r&   datarootc           	      X   t               }t        j                         }||k(  r>| J d       t        j                  | t        j
                        }t        |      |_        t        t        j                  t        j                  |      t        j                  t        j                        |             ||k7  rt        j                  |j                  z         }t        t        j                  t        j                  |t        j                        |j                  |             t        j                   |j"                        } ~| S t        t        j                  t        j                  t        j$                        t        j                        |j                  |             ~| S )aS  Broadcast object from one node to all other nodes.

    Parameters
    ----------
    data : any type that can be pickled
        Input data, if current rank does not equal root, this can be None
    root : int
        Rank of the node to broadcast data from.

    Returns
    -------
    object : int
        the result of broadcast.
    z&need to pass in data when broadcasting)protocol)r9   rK   c_ulongpickledumpsHIGHEST_PROTOCOLlenrO   r   r   XGCommunicatorBroadcastrN   sizeofc_charcastc_void_ploadsrawrL   )rR   rS   ranklengthsdptrs         r$   	broadcastrf      s6    :D^^Ft|I!IILL(?(?@1v$$LL &--"?	

 t|,/((D&//2FLL$	

 ||DHH% K 	((FOOA.@&,,PT	

 Kr&   dtypec                 p   t        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d	      d
t        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      di}	 |j                  t        j                  d      di       | |vrt        d|  d      ||    S # t        $ r Y #w xY w)Nfloat16r   float32r   float64   int8   int16   int32   int64   uint8   uint16	   uint32
   uint64   float128   z
data type z* is not supported on the current platform.)nprg   update	TypeError)rg   	dtype_maps     r$   
_map_dtyper      s   
Q
Q
Q
!
1
1
1
1
A
B
BI"((:.23 I*UG+UVWWU  s   +&D) )	D54D5c                   (    e Zd ZdZdZdZdZdZdZdZ	y)	Opz#Supported operations for allreduce.r   r   rl   r~   rn   rp   N)
r'   r(   r)   r*   MAXMINSUMBITWISE_AND
BITWISE_ORBITWISE_XORr.   r&   r$   r   r      s#    -
C
C
CKJKr&   r   opc           
      h   t        | t        j                        st        d      | j	                         j                         }t        t        j                  |j                  j                  t        j                        |j                  t        |j                        t        |                   |S )a'  Perform allreduce, return the result.

    Parameters
    ----------
    data :
        Input data.
    op :
        Reduction operator.

    Returns
    -------
    result :
        The result of allreduce, have same shape as data

    Notes
    -----
    This function is not thread-safe.
    z%allreduce only takes in numpy.ndarray)rE   r   ndarrayr   ravelcopyr   r   XGCommunicatorAllreducerK   data_asr_   sizer   rg   r+   )rR   r   bufs      r$   	allreducer     s    & dBJJ'?@@
**,


C$$JJv/HHsyy!G		
 Jr&   c                  >    t        t        j                                y)zKill the process.N)r   r   XGCommunicatorSignalErrorr.   r&   r$   signal_errorr   $  s    ..01r&   c                     ddl m}  | j                  *t        j                  j                  | j                        }n6t        | d      r(t        | j                        dkD  r| j                  d   }nd }|sy t        j                  |      }|sy d }|D ]  }|j                  d      s|} n |"t        j                  j                  ||      }|S y )Nr   )lib__path__z
libnccl.so)nvidia.ncclr   __file__ospathdirnamehasattrrZ   r   listdir
startswithjoin)r   r   fileslibnamenamer   s         r$   
_find_ncclr   )  s     ||!#!>	j	!c#,,&7!&;,,q/ JJwE!G ??<(G
 ww||GW-r&   c                   <    e Zd ZdZdeddfdZdefdZdeddfdZ	y)CommunicatorContextzNA context controlling collective communicator initialization and finalization.r   r   Nc                     || _         d}|j                  |d       y t               }|d   sy 	 t               }|r|| j                   |<   y y # t        $ r Y y w xY w)Ndmlc_nccl_pathUSE_DLOPEN_NCCL)r   getr   r   ImportError)r#   r   keybinfor   s        r$   __init__zCommunicatorContext.__init__N  sh    	88C*&'	<D!%		#  		s   A 	AAc                     t        di | j                   t               sJ t        j	                  d       | j                  S )Nz8-------------- communicator say hello ------------------r.   )r1   r   rA   LOGGERdebug)r#   s    r$   	__enter__zCommunicatorContext.__enter__`  s4    tyyOPyyr&   c                 B    t                t        j                  d       y )Nz7--------------- communicator say bye ------------------)r4   r   r   r"   s     r$   __exit__zCommunicatorContext.__exit__f  s    
NOr&   )
r'   r(   r)   r*   r   r   r   r   r   r   r.   r&   r$   r   r   K  s:    Xx D $5 Pc Pd Pr&   r   )r   N)5r*   rK   loggingr   rW   dataclassesr   enumr   r   typingr   r   r   r	   r
   numpyr   _typingr   corer   r   r   r   r   r   	getLoggerr   r-   r+   r   r,   r   r   r   r1   r4   r9   r<   r?   rA   rI   rQ   rf   rg   r   r   r   r   r   r   r   r.   r&   r$   <module>r      s   3   	  !   8 8   K K			1	2 U38_,-y -uS#X/) /X&y & ' ' 'T#> #>d #>L/
	# 		 	 'C 'D '(C ,B ,c ,b ,`bhh 3 2   BJJ B 2:: B2
HSM DP Pr&   