
    AVh                     j   d Z ddlZddlZddlmZmZmZ ddlZddl	m
Z
 ddl	mZ ddl	mZ ddl	mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddl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a$da%da&da' G d d      Z(d@dZ)dee(   de*de*dejV                  fdZ,d Z-	 	 dAd Z.dBd!Z/d"ee*   d#ee*   d$ee*   d%ee*   d&ee*   deee*      fd'Z0d"ee*   d#ee*   d(ee1   d)e2d*e*dee(   fd+Z3	 dBdee(   d,e2dee*   fd-Z4dee(   d*e*d.e2dee(   fd/Z5 e!d0g 1      ddddddejl                  fd2ee1   d3ee*   d4e1d5ee*   d6eee1      d#eee*      d)e2d7e2d.e2d8e2dejn                  fd9       Z8	 d@d:ejn                  d;ee*   dee*   fd<Z9	 d@d:ejn                  d;ee*   deee1e*f      fd=Z:dBd>Z;d? Z<y)Cz#TPU-specific utilities for DTensor.    N)ListOptionalDict)config)dtensor_device)gen_dtensor_ops)layout)context)def_function)constant_op)errors)ops)	array_ops)math_ops)
tf_logging)topology)numpy_compat)	tf_exportxTPUc            	       H    e Zd ZdZddedededefdZd Zd Zd	 Zd
 Z	d Z
y)_CoreLocationz-Represents a TPU core's location in the mesh.r   yzcorec                 <    || _         || _        || _        || _        y Nr   r   r   r   )selfr   r   r   r   s        R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/dtensor/python/tpu_util.py__init__z_CoreLocation.__init__;   s    DFDFDFDI    c                     t        |t              sy| j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S )NF)
isinstancer   r   r   r   r   r   others     r    __eq__z_CoreLocation.__eq__A   s[    e]+66UWWd577!2dtvv7HdTYYZ_ZdZdMddr"   c                 0    t        |t              sy| |k(   S )NT)r$   r   r%   s     r    __ne__z_CoreLocation.__ne__F   s    e]+u}r"   c                 p    t        | j                  | j                  | j                  | j                  f      S r   )hashr   r   r   r   r   s    r    __hash__z_CoreLocation.__hash__K   s&    344r"   c           
          t        |       j                   d| j                   d| j                   d| j                   d| j
                   d
S )Nz(x=z, y=z, z=z, core=))type__name__r   r   r   r   r,   s    r    __repr__z_CoreLocation.__repr__N   sC    4j!!"#dffXT$&&dffXWTYYKWXYYr"   c                 ^    | j                   | j                  | j                  | j                  gS r   r   r,   s    r    to_listz_CoreLocation.to_listQ   s!    FFDFFDFFDII..r"   N)r   r   r   r   )r1   
__module____qualname____doc__intr!   r'   r)   r-   r2   r4    r"   r    r   r   8   sA    5 C  s e

5Z/r"   r   c                    t        j                  |      }t        j                  |      j	                  |       }t        j
                  |      }t        |      }|st        |      D cg c]
  }|||z  z    c}n|}|||fS c c}w )z>Returns ID and device lists that can be used to create a mesh.)r   num_global_devicesnparangereshapelocal_deviceslenrange)	shapedevice_typehost_idlocal_device_idsr;   global_device_idslocal_device_listnum_local_devicesr   s	            r    _create_device_arrayrI   U   s    00=ii 23;;EB**;7 +,  055F/G*+a'%
%%!1  
,.?	??	s   )Bcore_locations	num_tasksnum_devices_per_taskreturnc                    t        | D cg c]  }|j                   c}      dk(  sJ t        | D cg c]  }|j                   c}      dk(  sJ t        | D cg c]  }|j                   c}      dk(  sJ t        | D cg c]  }|j                   c}      dk(  sJ t        | D cg c]  }|j                   c}      }t        | D cg c]  }|j                   c}      }t        | D cg c]  }|j                   c}      }t        | D cg c]  }|j                   c}      }|dz   |dz   |dz   |dz   g}| D cg c]0  }|j                  |j                  |j                  |j                  g2 }	}t        j                  |	      j                  ||d      }	t        j                  ||	      S c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )a?  Returns a Topology object build from a _CoreLocation list.

  Args:
    core_locations: A list of _CoreLocation objects sorted first by TF task ID
      and then by per-task device ordinals.
    num_tasks: The number of TF tasks in the cluster.
    num_devices_per_task: The number of TPU devices local to each task.
  r         )
mesh_shapedevice_coordinates)minr   r   r   r   maxr   
np_asarrayr>   r   Topology)
rJ   rK   rL   lx_maxy_maxz_maxcore_maxrQ   rR   s
             r    _create_tpu_topologyr\   d   s    
>*aacc*	+q	00	0	>*aacc*	+q	00	0	>*aacc*	+q	00	0	n-aff-	.!	33	3
N+qqss+
,%
N+qqss+
,%
N+qqss+
,%.1Q!&&12(	519eaiA>*9GHAacc133/HH#../ABJJ%q* 
		0B
D D +**-+++1 Is4   F7F<GG*GG0GG5Gc                      t         j                  d        } t        j                         r |        nd}|rt	        j
                  d       yt	        j                  d       y)zShuts down the TPU system.c                  *    t        j                         S r   )r   shutdown_tpu_systemr9   r"   r    _shutdown_tpu_systemz1shutdown_tpu_system.<locals>._shutdown_tpu_system   s    ..00r"   TzTPU system shut down.zTPU system fails to shut down.N)r   functionr
   is_tfrt_enabledlogginginfowarning)r`   successs     r    r_   r_      sQ     1 1 '.&=&=&? "T'LL()OO45r"   TFc           
      &   t         j                  fd       }t         j                  d        }t        j                  dt	        j
                         z   dz         5   |       }ddd       |rt        j                  d       dz  }t        j                  d       t        ||z        }t        j                  t        ggt        |ft        t	        j                                }	t        j                  t        t        j                   g|	      }
t#        j$                  |	g	      }t        j                  d
|j'                  |             t)        j*                  |gt(        j,                        }t/        t1        |            D ]  }||   || |z  |z   <    t3        j4                  |g      }t7        j8                  |      }|g|g|dz
  z  z   }t        j                  |j:                        5  |j=                  ||
      }t?        j@                  |dg      }|jC                  |      }ddd       tE        d   jG                               }t        j                  d|       |jI                  d|       |a%|j'                  |      }|D cg c]  }tM        |d   |d   |d   |d          }}|a't        j                  d|       tQ        |||      } ||jS                                ||fS # 1 sw Y   xY w# 1 sw Y   xY wc c}w )z8A helper function to initialize multi-client tpu system.c                  0    t        j                         S )Nuse_tfrt_host_runtime)r   #configure_and_initialize_global_tpuri   s   r    _tpu_init_fnz,tpu_system_init_helper.<locals>._tpu_init_fn   s    >>35 5r"   c                 .    t        j                  |        y r   )r   d_tensor_set_global_tpu_array)topology_protos    r    _set_global_tpu_array_fnz8tpu_system_init_helper.<locals>._set_global_tpu_array_fn   s    11.Ar"   z/job:z/device:TPU_SYSTEM:0NzUsing TPU megacore   zTPU core IDs: %s)mesheszTPU core locations: %s)dtyperO   r   )axiszAll TPU core IDs: %s    zAll TPU core locations: %s)*r   ra   r   devicer   full_job_namerc   rd   r8   
layout_libMesh_MESH_DIM_XrI   _TPU_DEVICE_TYPE	client_idLayout	UNSHARDEDr   DTensorDevicetpu_core_ids_to_locationsr<   zerosint32rA   r@   r   constantr   
zeros_likenamepackr   
reduce_sumunpacklistnumpyset_tpu_core_ids_all_core_idsr   _all_core_locationsr\   
serialized)task_idrK   num_devicesrj   use_megacorerl   rp   my_core_idsrL   meshr	   rw   all_core_idsir   unpacked_all_tpu_idsall_core_locationsrW   tpu_topologys      `               r    tpu_system_init_helperr      s    5 5 B B zz'F00225KKL !.K! LL%&/K	,,!;/ [945 
+ 
D.~?O/5/?/?/AC
D$ k:+?+?@$G&''v6&	,,'//<> ;-rxx8,[!" Fa7B1~L//!34F %%|n5,


|
,%5'-AA-E"FF,
 zz&++ 7;;|V4L&&|1#>L!==67
 *1-3356,	,,%|4 	"l+
 -77E5G01mAaD!A$!ad+  +	,,+-?@%&8)&:<, <2245	v	o! !j7 7.s   K54<L!L5K?Lc           	      x   t        j                          t        j                          t        j                          j                          t        j                          j                  }t        j                  d|       	 t        j                         }t        j                         }t        j                  t              }t        |||||       \  }}|at        j                  dd|j                  |j                          |at        j                          t        j                  d       t        j                          j                          y# t$        j&                  $ r)}t%        j(                  dddt+        |      z         |d}~wt$        j,                  $ r}t        j.                  d       |d}~ww xY w)	zInitializes the TPU system.z$Using TFRT host runtime is set to %s)rj   r   rO   zTPU Topology: %s, %sNz,Initialization failed, no valid TPUs found. zHit internal error during TPU system initialization. It is likely hardware failure. 
Please check the error messages above to see whether that's the case. 
If so, consider to restart the job or try another machine.zClearing out eager caches)r
   ensure_initialized
async_wait_clear_cachesuse_tfrtrc   rd   r   r}   num_clientsr;   r|   r   _tpu_topologyvlogrQ   rR   _dtensor_devicer   InvalidArgumentErrorNotFoundErrorstrInternalErrorerror)r   rj   r   rK   r   r   rw   es           r    initialize_tpu_systemr      sm   
 
		//!!#!//+44	,,57LM G""$I++,<=K13!#L& !MLL*L,C,C002 O 
,,*+	//!!# 
	$	$ H


d6Q?AFGH 
		 MM J K Gs%    BE F9#$FF9F44F9boundsring_bounds
ring_sizeshost_bounds
host_sizesc                    | sg gS t        | dd |dd |dd |dd |dd       }g }t        d| d   |d         D ]  }t        dt        |      |d         D ]~  }t        |||d   z   |d         D ]c  }	t        |||d   z   |d         D ]H  }
t        |	|	|d   z         D ]1  }t        |
|
|d   z         D ]  }|j                  ||   |gz           3 J e   |S )a  Enumerates cores within `bounds` from fatest to slowest varying axes.

  Args:
    bounds: Upper bounds of axes, from fastest to slowest varying.
    ring_bounds: Upper bounds of ring size per axis in the same axis order.
    ring_sizes: Number consecutive cores in the ring built so far, cumulatively.
    host_bounds: Number of axis values per host in the same axis order.
    host_sizes: Number consecutive cores on one host, cumulatively.

  Returns:
    Cores represented as a list of 4 integers in the same axis order.
  Nr   )_enumerate_coresrA   r@   append)r   r   r   r   r   partialsresultsring_iring_jhost_ihost_jr   js                r    r   r   *  s3    
4K fSbk;s+;Z_)#2.
3BA( 'a[_5 0f3x=*R.9 0&&;r?":KOL 0&FFZ^$;Z^L 	0F+b/!9: 0a66JrN#:; 0nnXa[A3./00	0000 
.r"   axescan_split_host_across_rings	ring_sizec           
      ~   | d   }|dk7  r|dk7  rt        d| z        |D cg c]  }ddddd|    }}|D cg c]  }| |   	 } }|rg d}nEt        j                  |       dk  r|D cg c]  }ddd|g|    }}n|D cg c]  }ddd|g|    }}dg}	|D ]  }
|	j                  |	d   |
z          |	j	                         }||k  r|rJ t        d	|z        |D cg c]  }||   	 }}||k  rt        d
|d|      dg}|D ]  }|j                  |d   |z          |j	                          t        | ||||	      }g }|D ]U  }t        d      D cg c]  }||j                  |          }}|j                  t        |d   |d   |d   |d                W |S c c}w c c}w c c}w c c}w c c}w c c}w )a  Enumerates all possible core locations under the axis iteration order.

  Args:
    bounds: A list of 4 positive integers, upper bound values for x, y, z, core.
    ring_bounds: A list of 4 positive integers, upper bound values for ring size
      in x, y, z, core axes.
    axes: A permutation of ["x", "y", "z", "core"], the axis iteration order.
    can_split_host_across_rings: If true, devices attached to the same host may
      get assigned to different rings.
    ring_size: Number of devices in a ring, only for argument validation.

  Returns:
    A list of all CoreLocation objects defined in a TPU slice of shape `bounds`,
    sorted by axis iteration order specified by `axes`.

    For example, given bounds=[2, 2, 1, 2] and axes=["core", "z", "y", "x"],
    return 8 core locations expressed in (x, y, z, core) format but iterated in
    core -> z -> y -> x order (fatest to slowest varying):

    [_CoreLocation(0, 0, 0, 0),
     _CoreLocation(0, 0, 0, 1),
     _CoreLocation(0, 1, 0, 0),
     _CoreLocation(0, 1, 0, 1),
     _CoreLocation(1, 0, 0, 0),
     _CoreLocation(1, 0, 0, 1),
     _CoreLocation(1, 1, 0, 0),
     _CoreLocation(1, 1, 0, 1)]

  Raises:
    ValueError: If ring_size cannot be fulfilled without splitting hosts.
  rv   rO   rq   zUnsupported TPU slice size: %sr   r   )rO   rO   rO   rO   r   z;Rings too small for can_split_host_across_rings = False: %dring_bounds z should be >= host_bounds rP   )	
ValueErrorr<   prodr   popr   rA   indexr   )r   r   r   r   r   num_cores_per_chiprt   r   r   r   
host_bound	host_sizer   
ring_boundcoresrJ   r   s                    r    _enumerate_core_locationsr   Q  sD   H ay1!3q!8
5>
?? AE	E1
-d
3	E$	E#$!F1I$&$ !K	wwv!=ABAq!/03BKB >BBAq!/03BKBs* 3jjnz123nn) ***
E	 
 *..AQ.+.;
!;0 1 1s* 3jjnz123.. 6;
K%'% . Md).q2ADA2D2-Qa$q'47KLM 
_ 
F$ C C /" 3s"   F!F&*F+F0F5F:rotatec                 F    t        t        t                           }|s|S t        j                  dd        t         D cg c]  }|j                   c}      t         D cg c]  }|j                   c}      t        t         D cg c]  }|j                   c}            dk(  }t        j                  dd       t        j                  dd       t        j                  dd|       dt        dt        d	t        f fd
dt        dt        d	t        f fd}|r&|j                  t        j                               n%|j                  t        j                  |             t        j                  dd|       |S c c}w c c}w c c}w )a  Reorders a list of TPU cores to optimize for AllReduce performance.

  This is ported from the C++ tensorflow::BuildAllReduceRing function,
  mixed with some logic from TF TPU's device_assignment._ring_3d.

  Args:
    core_locations: A list of core locations expressed as [x, y, z, core].
    rotate: If true, scan the cores in a column-major order. False by default.

  Returns:
    A permutation of the input list such that neighbors in the sequence are
    nearby in the TPU topology.
  rq   zCore locations in: %srO   zfirst_column: %dzfirst_row: %dz
same_z: %siaibrM   c                 j   	s|    }|   }|j                   k(  xr |j                  k7  }|j                   k(  xr |j                  k7  }||k7  r|rdS dS |j                  |j                  k7  r4|r|j                  |j                  z
  S |j                  |j                  z
  S |j                   |j                   k7  rD|j                  dz  dk(  r|j                   |j                   z
  S |j                   |j                   z
  S |j                  |j                  z
  S |    }|   }|j                  k(  xr |j                   k7  }|j                  k(  xr |j                   k7  }||k7  r|rdS dS |j                   |j                   k7  r4|r|j                   |j                   z
  S |j                   |j                   z
  S |j                  |j                  k7  rD|j                   dz  dk(  r|j                  |j                  z
  S |j                  |j                  z
  S |j                  |j                  z
  S Nr   rO   rq   r   )r   r   r   )
r   r   aba_firstb_firstrJ   first_column	first_rowr   s
         r    _cmp_2dz'_build_all_reduce_ring.<locals>._cmp_2d  s   

a

a $9	)9g$9	)9g	G	r#!# 
#qssQSSy2qss2 
CC!GqLqssQSSy7accACCi7 VVaff_

a

a 	!9acc\&9g	!9acc\&9g	G	r#!# 
#qssQSSy2qss2 
CC!GqLqssQSSy7accACCi7 VVaff_r"   c                    |    }|   }|j                   k(  xr |j                  	k(  }|j                   k(  xr |j                  	k(  }|rM|rK|j                  |j                  k7  r|j                  |j                  z
  S |j                  |j                  z
  S ||k7  r|rdS dS |j                  |j                  k(  r%|j                  dz  dk(  r	 | |      S  | |       S |j                  |j                  z
  S r   r   )
r   r   r   r   a_cornerb_cornerr   rJ   r   r   s
         r    _cmp_3dz'_build_all_reduce_ring.<locals>._cmp_3d  s    rArA|#8y(8H|#8y(8H H##*QSS133Y9!&&166/9 8R"" 	ssaccz !a1WR_B72r?2BB 339r"   )keyzPermutation out: %s)r   rA   r@   rc   r   rS   r   r   setr   r8   sort	functools
cmp_to_key)	rJ   r   permutationrW   same_zr   r   r   r   s	   ``    @@@r    _build_all_reduce_ringr     sX     U3~./0+		,,q)>:>2aacc23,/1133/0).1QQSS123q8&	,,q$l3	,,q/9-	,,q,'(# (3 (3 ( (T# 3 3  2 --g67--g67	,,q'5	_ 3/1s   F*FFrotate_ring_across_ringsc                    t        |       }t        | d|       }t        d||      D ]*  }t        |      D cg c]  }| |||   z       c}| |||z    , t        j                  dd|        g }t        |      D ]&  }|t        d||      D cg c]
  }| ||z       c}z  }( t        ||z        }	t        |d|	 |      }t        d||	      D ]*  }t        |	      D cg c]  }||||   z       c}||||	z    , g }
t        |	      D ]&  }|
t        d||	      D cg c]
  }|||z       c}z  }
( t        j                  dd|
       |
S c c}w c c}w c c}w c c}w )a  Build two all-reduce rings orthogonal to each other.

  One ring includes every `ring_size` consecutive core locations. It is usually
  applied to the model-parallel dimension of a mesh to achieve best 1D
  all-reduce performance. The other ring includes core locations separated by
  a stride of `ring_size`. It is usually applied to the data-parallel dimension
  of a mesh to get predictable strided all-reduce performance.

  Args:
    core_locations: A list of core locations expressed as [x, y, z, core].
    ring_size: The number of core locations in the consecutive ring.
    rotate_ring_across_rings: Build column-major secondary rings.

  Returns:
    A permutation of the input list forming the described rings.
  Nr   rO   Permutated core locations: %s)r   z$Stride-permutated core locations: %s)r@   r   rA   rc   r   r8   )rJ   r   r   	num_coresr   rr   
transposedg	num_ringsuntransposeds              r    _build_orthogonal_ringsr     s   * .!)&~jy'AB+Iy) a49)4D'/0q;q>)*'N1Q]# 
,,q1>B
 * a',Q	9'E"#q1u J
 )i'()&)%=?+Iy) a05i0@#+,
1{1~%&#JqY
 , PaaI0NO1ZA&OOLP	,,q8,G	7'# Ps   EEEEz$experimental.dtensor.create_tpu_mesh)v1mesh_dim_namesrQ   	mesh_name	ring_dims	ring_axesbuild_ring_across_ringsuse_xla_spmdc
                    t        j                  d||       t        j                  d|       t        j                  d|       t        j                  d|       t        j                  d|       |sdt        j                         z  }t        j                  d|       |d	t        |      z
  }n+|t        |       k  s|t        |      kD  rt	        d
|z        t        j                  d|       |g d}n:t        |      dk7  rt	        d|z        t        |      g dk7  rt	        d|z        t        j                  d|       t        t	        d      t        t        j                        }
||
}n2t        |      dk7  rt	        d|z        ||
kD  rt	        d|d|
      t        j                  d|       |dkD  rt        j                  |d|       }n |dk  rt        j                  ||d       }nd	}t        j                  d|       t        |
||||      }t        j                  d	d|       t        |      }t        j                  |      }||k7  rt	        d||fz        |rt        |||      }n_t        |d|       }t        d||      D ]*  }t        |      D cg c]  }||||   z       c}||||z    , t        j                  d	d|       |D cg c]  }|j!                          }}t"        t	        d      t"        j%                  |      }t"        j'                  ||       t        j(                  t+        j,                            }|D cg c]  }|j/                  t        |             }}t1        |t2        d|      \  }}}t5        j6                  | |||||	      S c c}w c c}w c c}w )aJ  Returns a distributed TPU mesh optimized for AllReduce ring reductions.

  Only as many as leading axes specified by `ring_axes` as necessary will be
  used to build rings, as long as the subslice formed by these axes have enough
  cores to contain a ring of the required size. The leftover axes in `ring_axes`
  won't affect results.

  This function always uses all TPU devices, and offers more customization than
  `tf.experimental.dtensor.create_distributed_mesh`.

  Args:
    mesh_dim_names: List of mesh dimension names.
    mesh_shape: Shape of the mesh.
    mesh_name: A unique name for the mesh. If empty, internally generate one.
    ring_dims: Optional; The number of leading (ring_dims > 0) or trailing
      (ring_dims < 0) mesh dimensions to build rings for. If unspecified, build
      rings for all but the first dimension.
    ring_axes: Optional; A permutation of ["x", "y", "z", "core"], specifying
      the order of TPU topology axes to build rings in. If unspecified, default
      to ["core", "x", "y", "z"].
    ring_bounds: Optional; The maximum number of devices on each axis, in the x,
      y, z, core order. If unspecified, default to physical topology limits.
    can_split_host_across_rings: Optional; If true, devices attached to the same
      host (i.e., DTensor client) may get assigned to different rings. Setting
      it to false may cause some combinations of arguments to be infeasible; see
      DeviceAssignmentTest.testCreateMesh[No]SplittingHosts* for examples.
    build_ring_across_rings: Optional; If true, also build a data-parallel ring
      across model-parallel rings. This ring could be strided.
    rotate_ring_across_rings: Optional; If true, build the data-parallel ring in
      column-major instead of row-major order.
    use_xla_spmd: Boolean when True, will use XLA SPMD instead of
      DTensor SPMD.
  z"Building a TPU mesh %s of shape %szRequested ring_dims: %szRequested ring_axes: %szRequested ring_bounds: %sz)Requested can_split_host_across_rings: %szmesh_%fzRequested mesh_name: %sNrO   zInvalid ring_dims value: %dzActual ring_dims: %s)r   r   r   r   rP   z(Expected 4 elements in ring_axes, got %szInvalid ring_axes value: %szActual ring_axes: %sz?Invalid TPU topology, run dtensor.initialize_tpu_system() firstz*Expected 4 elements in ring_bounds, got %sr   z should be <= topology sizes zActual ring_bounds: %sr   zActual ring_size: %dzEnumerated core locations: %sz?Invalid mesh size: mesh shape %s cannot 1:1 map to %d TPU coresr   zHInvalid system device, run dtensor.initialize_accelerator_system() first)rE   )r   )rc   rd   timer@   r   sortedr   r   rQ   r<   r   r   r   r   r   rA   r4   r   tpu_core_locations_to_idsr   rR   r   r}   r   rI   r|   ry   rz   )r   rQ   r   r   r   r   r   r   r   r   topology_shaper   global_core_locationsr   	mesh_sizer   r   r   rW   global_core_idslocal_core_locationslocal_core_locationindexesrF   rE   rG   s                             r    create_tpu_meshr   I  s   \ 
,,3Y
K	,,()4	,,()4	,,*K8	,,:*,	DIIK'I	,,()4 C
O#IC
O##y3z?'B
2Y>
??	,,%y1 'I
9~
?)K
LLi33
2Y>
??	,,%y1 
IK K001. K
;1
AKO
PP^#
!>3 4 4	,,'5 ]
:I./I1}
9:./II	,,%y1 4k9.I 
,,q13HI'() ggj!))
I	Y	 ! ! 34I4=4LN ))>z	)JKK1i+ =B9=M089
KN 2
30Aa)m, LL35JK 1FF1199;FF
 I J J#==/ ""9o> '99&:J:J:LM "6
 !!$':";<'  <P"D7<D8%'8	
 30 Gs   )N4!N9!N>r   r}   c                     | j                         t        k7  rt        d      ||t        j                         k(  r| j                         S t        d      )a  Returns the device IDs of all TPU cores local to the given client.

  A device ID is a non-negative integer that uniquely identifies a device in the
  mesh. For example, for a 2x2 mesh ('x', 'y'), this function returns a
  permutation of [0, 1, 2, 3].

  Note that device IDs and device locations are equivalent. The former is a
  linearization of the latter along mesh dimensions.

  Args:
    mesh: A TPU mesh.
    client_id: Optional; A DTensor client ID. If empty, query this client.
  The mesh must be a TPU meshz5Looking up other clients' device IDs is not supported)rC   r|   r   r   r}   rE   NotImplementedErrorr   r}   s     r    get_device_idsr    sY      
++
2
33)v'7'7'99  "" 	=	? ?r"   c                     | j                         t        k7  rt        d      ||t        j                         k(  r| j                         S t        d      )aM  Returns the device locations of all TPU cores local to the given client.

  A device location is a dictionary from dimension names to indices on those
  dimensions. For example, for a 2x2 mesh ('x', 'y'), this function returns a
  permutation of this list:

    [{'x': 0, 'y': 0},
     {'x': 0, 'y': 1},
     {'x': 1, 'y': 0},
     {'x': 1, 'y': 1}].

  Note that device IDs and device locations are equivalent. The former is a
  linearization of the latter along mesh dimensions.

  Args:
    mesh: A TPU mesh.
    client_id: Optional; A DTensor client ID. If empty, query this client.
  r  z;Looking up other clients' device locations is not supported)rC   r|   r   r   r}   local_device_locationsr  r  s     r    get_device_locationsr    s[    , 
++
2
33)v'7'7'99&&(( 	C	E Er"   c                 6    ddl m} |j                  d|        y)z,Deprecated way to initialize the TPU system.rO   accelerator_utilr   )enable_coordination_serviceN)ru   r
  initialize_accelerator_system)r  r
  s     r    dtensor_initialize_tpu_systemr  "  s     00)D 1 Fr"   c                  0    ddl m}  | j                          y)z*Deprecated way to shutodwn the TPU system.rO   r	  N)ru   r
  shutdown_accelerator_systemr	  s    r    dtensor_shutdown_tpu_systemr  )  s     ..0r"   r   )TF)F)=r7   r   r   typingr   r   r   r   r<   tensorflow.dtensor.pythonr   r   r   r	   ry   tensorflow.python.eagerr
   r   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   r   tensorflow.python.platformr   rc   tensorflow.python.tpur   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   r{   r|   r   r   r   r   r   rI   r8   rV   r\   r_   r   r   r   r   boolr   r   r   USE_XLA_SPMDrz   r   r  r  r  r  r9   r"   r    <module>r     s   *   ' '  , 4 5 : + 0 3 . + + * < * / 6      / /:@Dm)< D D/2D7?7H7HD:6" 26(-	gT.$b$T#Y $T#Y $!%c$9=c$!%c$/3DI$NXd3i Xd3i X$(IX;?X *-X 26m1DXx +0d4+> d#'d48IdN3'3473"3'+M':3l 1b9
  $%)'+(,$)%*#00UIUS	U U }	U
 S	"U $s)$U "&U "U #U U 6@__U :Ur /3? ?&sm?7;Cy?<  $E
//E}E(,T#s(^(<EHF1r"   