
    uki                    p    d dl mZ d dlmZ d dlZd dlZd dlmZ  ej                  e	      Z
 G d d      Zy)    )annotations)SequenceN)running_in_cloud_tpu_vmc                       e Zd ZU dZg Zded<   dZded<    fdZe	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	edd	       Z
edd
       Zedd       Zedd       Zedd       Z xZS )
ClusterEnva  Interface for defining a cluster environment.

  To enable auto bootstrapping (aka :func:`jax.distributed.initialize()`),
  cluster environments need to derive from :class:`ClusterEnv` and implement
  :func:`is_env_present`, :func:`get_coordinator_address`,
  :func:`get_process_count`, and :func:`get_process_id`.
  :class:`ClusterEnv` subclasses are automatically detected when imported.
  zlist[type[ClusterEnv]]_cluster_typesFboolopt_in_only_methodc                Z    t        |   di | | j                  j                  |        y )N )super__init_subclass__r   append)clskwargs	__class__s     T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/clusters/cluster.pyr   zClusterEnv.__init_subclass__%   s'    	G''c"    c                   et        fd| j                  D        d       }|t        j                  d d       nG|j	                         s7t        j                  d d       nt        d | j                  D        d       }|rt        j                  d|j                         |2t        j                  j                  d      }|j                  ||      }||j                         }||j                         }|^t        sX|j                         H|j                         g}n6t        j                  d	d
j                  d | j                  D                     ||||fS )Nc              3  B   K   | ]  }|j                   k(  s|  y wN)name).0envcluster_detection_methods     r   	<genexpr>zBClusterEnv.auto_detect_unset_distributed_params.<locals>.<genexpr>8   s     ]3D\8\3]s   z6Automatic Distributed initialization can not proceed: z is not supported.z5 is supported but not functional in this environment.c              3  b   K   | ]'  }|j                   d k(  s|j                         s$| ) yw)FN)r
   is_env_present)r   r   s     r   r   zBClusterEnv.auto_detect_unset_distributed_params.<locals>.<genexpr>@   s*     p#s7M7MQV7V[^[m[m[o#ps   ///z/Initializing distributed JAX environment via %sJAX_COORDINATOR_PORT)timeout_secsoverride_coordinator_portz[Could not find a known environment for initializing distributed JAX. Known environments: %sz, c              3  4   K   | ]  }|j                     y wr   )__name__)r   es     r   r   zBClusterEnv.auto_detect_unset_distributed_params.<locals>.<genexpr>Y   s     +S1AJJ+Ss   )nextr   loggererrorr   debugr#   osenvirongetget_coordinator_addressget_process_countget_process_idr   get_local_process_idjoin)	r   coordinator_addressnum_processes
process_idlocal_device_idsr   initialization_timeoutr   coordinator_ports	        `   r   $auto_detect_unset_distributed_paramsz/ClusterEnv.auto_detect_unset_distributed_params*   sq     +]#"4"4]_cec	 122DF 	G!!# 122gi 	j p!3!3prvwc llDcllS		$::>>*@A!99G]  zJ9  K		--/		'')
 
"+B

"
"
$
04467ll !"&))+S@R@R+S"SU
<LMMr   c                    t        d      )zDReturns True if process is running in this cluster environment.
    z3ClusterEnv subclasses must implement is_env_presentNotImplementedErrorr   s    r   r   zClusterEnv.is_env_present]   s     S
TTr   c                    t        d      )ax  Returns address and port used by JAX to bootstrap.

    Process id 0 will open a tcp socket at "hostname:port" where
    all the processes will connect to initialize the distributed JAX service.
    The selected port needs to be free.
    :func:`get_coordinator_address` needs to return the same hostname and port on all the processes.

    Returns:
      "hostname:port"
    z<ClusterEnv subclasses must implement get_coordinator_addressr9   )r   r    r!   s      r   r,   z"ClusterEnv.get_coordinator_addressc   s     \
]]r   c                    t        d      )Nz6ClusterEnv subclasses must implement get_process_countr9   r;   s    r   r-   zClusterEnv.get_process_countq   s    
V
WWr   c                    t        d      )Nz3ClusterEnv subclasses must implement get_process_idr9   r;   s    r   r.   zClusterEnv.get_process_idu   s    
S
TTr   c                     y)a   Get index of current process inside a host.

    The method is only useful to support single device per process.
    In that case, each process will see a local device whose ID is
    the same as its local process ID.
    If None, JAX will not restrict the visible devices.
    Nr   r;   s    r   r/   zClusterEnv.get_local_process_idy   s     r   )r1   
str | Noner2   
int | Noner3   rA   r4   zSequence[int] | Noner   r@   r5   rA   returnz?tuple[str | None, int | None, int | None, Sequence[int] | None])rB   r	   )r    rA   r!   r@   rB   str)rB   int)rB   rA   )r#   
__module____qualname____doc__r   __annotations__r
   r   classmethodr7   r   r,   r-   r.   r/   __classcell__)r   s   @r   r   r      s     ,..(-"d"#
 .N@J.N:D.N 8B.N >R	.N
 FP.N DN.N0K.N .Nb U U
 ^ ^ X X U U  r   r   )
__future__r   collections.abcr   r)   loggingjax._src.cloud_tpu_initr   	getLoggerr#   r&   r   r   r   r   <module>rP      s4    # $ 	  ;			8	$j jr   