
    AVh2              	          d Z ddl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 ddlmZ dZd	Zd
ZdZdZ edg       	 d.dedee   deej,                     fd       Z edg       dedefd       Z edg       dedefd       Z edg       defd       Z edg       defd       Z edg       defd       Z edg       d.dee   defd       Zd edeeef   fd!Z ed"g       dee   fd#       Z ed$g       de fd%       Z!de fd&Z"de fd'Z#de fd(Z$ ed)g       defd*       Z%de fd+Z&de fd,Z'de fd-Z(y)/zDTensor Configuration API.    N)ListOptionalUnion)context)config)device)	tf_exportDTENSOR_CLIENT_IDDTENSOR_NUM_CLIENTSDTENSOR_JOB_NAMEDTENSOR_JOBSDTENSOR_ENABLE_HEARTBEATz"experimental.dtensor.local_devices)v1device_typefor_client_idreturnc           
          | j                         dvrt        d|  d      |
t               }t        t	        |             D cg c]$  }t        j                  t               d|| |      & c}S c c}w )z9Returns a list of device specs configured on this client.)CPUGPUTPUzDevice type z is not CPU, GPU, or TPU.r   )jobreplicataskr   device_index)upper
ValueError	client_idrangenum_local_devices	tf_device
DeviceSpecjob_name)r   r   is      P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/dtensor/python/config.pylocal_devicesr%   .   s    
  55
|K=0IJ
KKKM $)):;)G#H
   j!
  
s   )A0z&experimental.dtensor.num_local_devicesc                     | j                         dv r1t        j                         }|j                  | j                            S t	        t        j                  |             S )zGReturns the number of devices of device_type configured on this client.)r   r   )r   r   
get_configdevice_countlen	tf_configlist_physical_devices)r   context_configs     r$   r   r   D   sS    
 N*'')N&&{'8'8':;;	Y,,[9	::    z'experimental.dtensor.num_global_devicesc                 .    t        |       t               z  S )zEReturns the number of devices of device_type in this DTensor cluster.)r   num_clients)r   s    r$   num_global_devicesr0   P   s     
;	'+-	77r-   zexperimental.dtensor.client_idc                      t        t        j                  j                  t        d            } | dk  rt        dt         d|  d      | t               k\  r t        dt         dt                d|        | S )zReturns this client's ID.0r   zEnvironment variable z must be >= 0, got z. z must be < z, got )intosenvironget_DT_CLIENT_IDr   r/   )client_id_values    r$   r   r   V   s     

}c:;/q
,]O <**9):"> ? ?%
,]O <""--6GI J J	r-   z experimental.dtensor.num_clientsc                  >    t               ryt        t                     S )z6Returns the number of clients in this DTensor cluster.   )is_local_moder)   jobs r-   r$   r/   r/   d   s     _	TVr-   zexperimental.dtensor.job_namec                  p    t         j                  j                  t        t	               dk(  rd      S d      S )zAReturns the job name used by all clients in this DTensor cluster.r:   	localhostworker)r4   r5   r6   _DT_JOB_NAMEr/   r=   r-   r$   r"   r"   l   s8    
 
'2}'9
I I?G
I Ir-   z"experimental.dtensor.full_job_nametask_idc                 z    | 
t               } t               dk(  r| dk7  rt        d|  d      t                d|  S )zAReturns the fully qualified TF job name for this or another task.r:   r   zUnexpected task ID z in local runsz/replica:0/task:)r   r/   r   r"   )rB   s    r$   full_job_namerD   u   sK     _kG ]aGqL
*7)>B
CCJ<'y	11r-   r   c                     | j                  d      d   j                  d      d   }	 t        |      S # t        $ r | cY S w xY w)zTries to extract an integer task ID from a job name.

  For example, for `job` = '/.../tpu_worker/0:port_name', return 0.

  Args:
    job: A job name to extract task ID from.

  Returns:
    The task ID on success, or the original job name on failure.
  /:r   )rsplitr3   r   )r   maybe_task_ids     r$   _bns_task_idrK      sJ     **S/"%,,S1!4-}	 Js   
3 A Azexperimental.dtensor.jobsc                     t         j                  j                  t              } | g S | j	                  d      }t        |D cg c]  }|j                  d       c}      r#|t        |t              k7  rt        d|  d      |S c c}w )zCReturns a list of job names of all clients in this DTensor cluster.,z/bns/)keyz Unexpected DTENSOR_JOBS content zP. Sort entries in DTENSOR_JOBS because cluster construction relies on the order.)
r4   r5   r6   _DT_JOBSsplitany
startswithsortedrK   r   )d_jobsd_jobs_listnames      r$   r<   r<      s     ::>>(#&^IS!+ 	{	;t$//'
"	;<f[l;;,VH 5  
 
 
<s   Bz&experimental.dtensor.heartbeat_enabledc                  j    t         j                  j                  t        d      j	                         dv S )z5Returns true if DTensor heartbeat service is enabled.true)rX   1)r4   r5   r6   _DT_HEARTBEAT_ENABLEDlowerr=   r-   r$   heartbeat_enabledr\      s(     
-v	6	<	<	>-	OOr-   c                      t                S )z0Returns true if DTensor shall run in local mode.)r<   r=   r-   r$   r;   r;      s    Vr-   c                  B    t        j                  d      } t        |       S )(Returns true if TPU devices are present.
TPU_SYSTEM)r*   r+   bool)tpu_system_devicess    r$   is_tpu_presentrc      s!     !66|D	 	!!r-   c                  >    t        t        j                  d            S )r_   r   )ra   r*   r+   r=   r-   r$   is_gpu_presentre      s    	i--e4	55r-   z*experimental.dtensor.preferred_device_typec                  0    t               ryt               ryy)zReturns the preferred device type for the accelerators.

  The returned device type is determined by checking the first present device
  type from all supported device types in the order of 'TPU', 'GPU', 'CPU'.
  r   r   r   )rc   re   r=   r-   r$   preferred_device_typerg      s     	r-   c                  H    t         j                  j                  dd      dk7  S )zBReturn True if environment indicates multi-device mode is enabled.%DTENSOR_ENABLE_MULTI_DEVICE_EXPANSIONr2   r4   r5   r6   r=   r-   r$   use_multi_device_moderk      s    	?	E	LLr-   c                  H    t         j                  j                  dd      dk7  S )z@Return True if environment indicates NCCL shall be used for GPU."DTENSOR_GPU_USE_NCCL_COMMUNICATIONr2   rj   r=   r-   r$   gpu_use_nccl_communicationrn      s    	<c	Bc	IIr-   c                  F    t         j                  j                  d      dk(  S )z=Return True if environment indicates the backend is Pathways.DTENSOR_USE_PARALLEL_EXECUTORpwrj   r=   r-   r$   backend_is_pwrr      s    	7	8D	@@r-   )N))__doc__r4   typingr   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r*   r   r     tensorflow.python.util.tf_exportr	   r7   _DT_NUM_CLIENTSrA   rO   rZ   strr3   r!   r%   r   r0   r   r/   r"   rD   rK   r<   ra   r\   r;   rc   re   rg   rk   rn   rr   r=   r-   r$   <module>rz      sI   ! 	 ( ( + ; ; 6#'!2   /B7 $(C=,01E1E,F 8* 3;;3 ;3 ; <; 4<8C 8C 8 =8
 +3
3 
 4
 -"5S  6 *r2I# I 3I /B7	28C= 	2C 	2 8	2c eCHo $ &2.d3i  /& 3;P4 P <P
t 
" "6 6
 7B?s  @Mt M
JD J
At Ar-   