
    BVh(                     l    d Z ddlZddlmZ ddlmZ ddlmZ d Z	d Z
 ed       G d	 d
e             Zy)zCDefines the `Topology` class, that describes a TPU fabric topology.    N)topology_pb2)numpy_compat)	tf_exportc                 $    | d||fz  S d| ||fz  S )z@Returns the device name for the TPU `device` on `task` of `job`.z/task:%d/device:TPU:%dz/job:%s/task:%d/device:TPU:%d )jobtaskdevices      N/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/topology.py_tpu_device_namer      s(    [#tVn44*c4-@@@    c                     | d|z  S d| |fz  S )z>Returns the device name for the CPU device on `task` of `job`.z/task:%d/device:CPU:0z/job:%s/task:%d/device:CPU:0r   )r   r	   s     r   _tpu_host_device_namer       s!    ["T)))S$K77r   ztpu.experimental.Topologyc                       e Zd ZdZddZd Zd Zed        Zed        Z	ed        Z
ed	        Zd
 Zd ZddZddZed        Zed        Zd Zy)TopologyzDescribes a set of TPU devices.

  Represents both the shape of the physical mesh, and the mapping between
  TensorFlow TPU devices to physical mesh coordinates.
  Nc                    || _         |r| j                  |       nqt        j                  |t        j
                        | _        t        j                  |t        j
                        | _        t        | j                        dk7  st        | j                  dk        rt        d| j                   d      t        | j                  j                        dk7  s/| j                  j                  d   t        | j                        k7  rt        dj                  | j                  j                  t        | j                  j                        | j                  j                  d   | j                  t        | j                                    | j                         \  | _        | _        t	        j                   | j                  d	k        | _        y
)a(  Builds a Topology object.

    If `serialized` is not `None`, the topology is parsed from `serialized` and
    the other arguments are ignored. Otherwise, the topology is computed from
    `mesh_shape` and `device_coordinates`.

    Args:
      serialized: A serialized `TopologyProto`, or `None`. If not `None`, the
        serialized proto is parsed to discover the topology.
      mesh_shape: A sequence of 4 positive integers, or `None`. If not `None`,
        the shape of the TPU topology, in number of cores. Ignored if
        `serialized` is not `None`.
      device_coordinates: A rank 3 numpy array that describes the mapping from
        TensorFlow TPU devices to TPU fabric coordinates, or `None`. If
        specified, array is a rank 3 int32 array with shape
        `[tasks, devices, axis]`.  `tasks` is the number of tasks in the TPU
        cluster, `devices` is the number of TPU devices per task, and `axis` is
        the number of axes in the TPU cluster topology. Each entry gives the
        `axis`-th coordinate in the topology of a task/device pair. TPU
        topologies are 4-dimensional, with dimensions `(x, y, z, core number)`.
        This arg is ignored if `serialized is not `None`.

    Raises:
      ValueError: If `serialized` does not describe a well-formed topology.
      ValueError: If `serialized` is `None` and `mesh_shape` is not a sequence
        of 4 positive integers.
      ValueError: If `serialized` is `None` and `device_coordinates` is not a
        rank 3 numpy int32 array that describes a valid coordinate mapping.
    dtype      zG`mesh_shape` must be a sequence of 4 positive entries; got `mesh_shape=`      z`device_coordinates` must be a rank 3 int32 array with minor dimension equal to the `mesh_shape` rankgot device_coordinates.shape={} len(device_coordinates.shape)={} device_coordinates.shape[2]={} mesh_shape={}, len(mesh_shape)={}r   N)_serialized_parse_topologyr   
np_asarraynpint32_mesh_shape_device_coordinateslenany
ValueErrorshapeformat_invert_topology_topology_tasks_topology_devicesargwhere_missing_devices)self
serialized
mesh_shapedevice_coordinatess       r   __init__zTopology.__init__0   s   > "D
:&%00288Ld!-!8!89K?Axx"Id	T	!	#s4+;+;a+?'@ 5595E5E4FaI J 	J d&&,,
-
2

"
"
(
(
+s43C3C/D
DP VD,,2200667,,2215t7G7G(()+	, 	, 483H3H3J0D$0 KK(<(<q(@ADr   c           
         t        j                         }|j                  |       t        j                  |j
                  t        j                        | _        t        | j                        dk7  st        | j                  dk        r$t        dj                  | j                              |j                  dk  r$t        dj                  |j                              |j                  dk  r$t        dj                  |j                              |j                  |j                  z  t        |j
                        z  }t        |j                        |k7  rNt        dj                  |j                  |j                  |j
                  t        |j                                    t        j                  |j                  t        j                        }t        |dk        rt        d	j                  |            |j                  |j                  |j                  t        |j
                        f      }|| _        y
)z0Parses a serialized `TopologyProto` into `self`.r   r   r   zE`mesh_shape` must be a vector of size 4 with positive entries; got {}r   z `num_tasks` must be >= 0; got {}z/`num_tpu_devices_per_task` must be >= 0; got {}zx`device_coordinates` must have shape num_tasks ({}) * num_tpu_devices_per_task ({}) * len(mesh_shape) ({}); got shape {}z7All values in `device_coordinates` must be >= 0, got {}N)r   TopologyProtoParseFromStringr   arrayr-   r   r   r!   r"   r#   r%   	num_tasksnum_tpu_devices_per_taskr.   reshaper    )r+   r,   protoexpected_coordinates_sizecoordss        r   r   zTopology._parse_topologyk   s   &&(E	*%xx 0 0AD
4!S)9)9A)=%> ))/0@0@)AC C 9@@
//  %%)HOO

(
(* + + 	%8883< 	  5##$(AA &&,fU__-2-K-K-2-=-=-01I1I-J'LM M XXe..bhh?F
6A:
C6&>  ^^U__e.L.L !1!124 5F%Dr   c                    t        j                  t        | j                        dt         j                        }t        j                  t        | j                        dt         j                        }t        | j                  j                  d         D ]T  }t        | j                  j                  d         D ]-  }| j                  ||ddf   \  }}}}||||||f<   ||||||f<   / V ||fS )zEInverts a [task,device,axis] topology to [x,y,z] -> task/device maps.r   r   r   N)r   fulllistr-   r   ranger.   r$   )	r+   tasksdevicesr	   r
   xyzcores	            r   r&   zTopology._invert_topology   s    GGD)2RXX>Eggd4??+Rrxx@Gd--33A67 ($1177:; (&//fa@1a#aAtm!'1a((
 '>r   c                     | j                   S )z>A rank 1 int32 array describing the shape of the TPU topology.)r   r+   s    r   r-   zTopology.mesh_shape   s     r   c                 ,    t        | j                        S )z-Returns the number of dimensions in the mesh.)r!   r   rF   s    r   	mesh_rankzTopology.mesh_rank   s     t  r   c                     | j                   S )a  Describes the mapping from TPU devices to topology coordinates.

    Returns:
      A rank 3 int32 array with shape `[tasks, devices, axis]`.
      `tasks` is the number of tasks in the TPU cluster, `devices` is the number
      of TPU devices per task, and `axis` is the number of axes in the TPU
      cluster topology. Each entry gives the `axis`-th coordinate in the
      topology of a task/device pair. TPU topologies are 4-dimensional, with
      dimensions `(x, y, z, core number)`.
    )r    rF   s    r   r.   zTopology.device_coordinates   s     ###r   c                     | j                   S )z$Array of indices of missing devices.)r*   rF   s    r   missing_deviceszTopology.missing_devices   s        r   c                 2    | j                   t        |         S )aC  Returns the TensorFlow task number attached to `device_coordinates`.

    Args:
      device_coordinates: An integer sequence describing a device's physical
        coordinates in the TPU fabric.

    Returns:
      Returns the TensorFlow task number that contains the TPU device with those
      physical coordinates.
    )r'   tupler+   r.   s     r   task_ordinal_at_coordinatesz$Topology.task_ordinal_at_coordinates   s     &8 9::r   c                 2    | j                   t        |         S )aY  Returns the TensorFlow device number at `device_coordinates`.

    Args:
      device_coordinates: An integer sequence describing a device's physical
        coordinates in the TPU fabric.

    Returns:
      Returns the TensorFlow device number within the task corresponding to
      attached to the device with those physical coordinates.
    )r(   rM   rN   s     r   !tpu_device_ordinal_at_coordinatesz*Topology.tpu_device_ordinal_at_coordinates   s     !!%(:";<<r   c                 F    t        || j                  t        |               S )z2Returns the CPU device attached to a logical core.)r   r'   rM   r+   r.   r   s      r   cpu_device_name_at_coordinatesz'Topology.cpu_device_name_at_coordinates   s%     T!!%(:";<> >r   c                 t    t        || j                  t        |         | j                  t        |               S )z>Returns the name of the TPU device assigned to a logical core.)r   r'   rM   r(   rS   s      r   tpu_device_name_at_coordinatesz'Topology.tpu_device_name_at_coordinates   s;    C 007I1JK 2259K3LMO Or   c                 4    | j                   j                  d   S )z8Returns the number of TensorFlow tasks in the TPU slice.r   r    r$   rF   s    r   r4   zTopology.num_tasks        ##))!,,r   c                 4    | j                   j                  d   S )z<Returns the number of TPU devices per task in the TPU slice.r   rX   rF   s    r   num_tpus_per_taskzTopology.num_tpus_per_task   rY   r   c                    | j                   t        j                         }t        | j                        |j
                  dd | j                  j                  d   |_        | j                  j                  d   |_	        |j                  j                  t        | j                  j                                      |j                         | _         | j                   S )z,Returns the serialized form of the topology.Nr   r   )r   r   r1   r=   r   r-   r    r$   r4   r5   r.   extendflattenSerializeToString)r+   r7   s     r   r,   zTopology.serialized   s    ((*e !1!12eq0066q9eo'+'?'?'E'Ea'He$%%d4+C+C+K+K+M&NO002dr   )NNN)N)__name__
__module____qualname____doc__r/   r   r&   propertyr-   rH   r.   rK   rO   rQ   rT   rV   r4   r[   r,   r   r   r   r   r   (   s    9Bv#&J	   ! ! $ $ ! !;=>
O - - - -
r   r   )rc   numpyr   tensorflow.core.protobuf.tpur   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   r   r   objectr   r   r   r   <module>rj      sF    J  5 / 6A8 &'Hv H (Hr   