a
    .Sic'                     @   sP   d Z ddlZddlmZ ddlmZ dd Zdd Zed	G d
d de	Z
dS )zCDefines the `Topology` class, that describes a TPU fabric topology.    N)topology_pb2)	tf_exportc                 C   s&   | du rd||f S d| ||f S dS )z@Returns the device name for the TPU `device` on `task` of `job`.Nz/task:%d/device:TPU:%dz/job:%s/task:%d/device:TPU:%d )jobtaskdevicer   r   Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/tpu/topology.py_tpu_device_name   s    r	   c                 C   s    | du rd| S d| |f S dS )z>Returns the device name for the CPU device on `task` of `job`.Nz/task:%d/device:CPU:0z/job:%s/task:%d/device:CPU:0r   )r   r   r   r   r   _tpu_host_device_name   s    r
   ztpu.experimental.Topologyc                   @   s   e Zd ZdZdddZdd Zdd Zed	d
 Zedd Z	edd Z
edd Zdd Zdd Zd ddZd!ddZedd Zedd Zdd ZdS )"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              	   C   s   || _ |r| | ntj|tjd| _t|tj| _t| jdksTt| jdk rft	d| j dt| jj
dks| jj
d t| jkrt	d| jj
t| jj
| jj
d | jt| j|  \| _| _t| jd	k | _d
S )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_topologynpasarrayint32_mesh_shape_device_coordinateslenany
ValueErrorshapeformat_invert_topology_topology_tasks_topology_devicesargwhere_missing_devices)self
serialized
mesh_shapedevice_coordinatesr   r   r   __init__/   s,    

	zTopology.__init__c                 C   s  t  }|| tj|jtjd| _t| jdksBt	| jdk rRt
d| j|jdk rlt
d|j|jdk rt
d|j|j|j t|j }t|j|krt
d|j|j|jt|jtj|jtjd}t	|dk rt
d	|||j|jt|jf}|| _d
S )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%   protoZexpected_coordinates_sizecoordsr   r   r   r   i   sJ    


zTopology._parse_topologyc           	      C   s   t jt| jdt jd}t jt| jdt jd}t| jjd D ]V}t| jjd D ]@}| j||ddf \}}}}||||||f< ||||||f< qTq@||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corer   r   r   r      s    zTopology._invert_topologyc                 C   s   | j S )z>A rank 1 int32 array describing the shape of the TPU topology.)r   r$   r   r   r   r&      s    zTopology.mesh_shapec                 C   s
   t | jS )z-Returns the number of dimensions in the mesh.)r   r   r;   r   r   r   	mesh_rank   s    zTopology.mesh_rankc                 C   s   | 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   r;   r   r   r   r'      s    zTopology.device_coordinatesc                 C   s   | j S )z$Array of indices of missing devices.)r#   r;   r   r   r   missing_devices   s    zTopology.missing_devicesc                 C   s   | 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'   r   r   r   task_ordinal_at_coordinates   s    z$Topology.task_ordinal_at_coordinatesc                 C   s   | 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!   r>   r?   r   r   r   !tpu_device_ordinal_at_coordinates   s    z*Topology.tpu_device_ordinal_at_coordinatesc                 C   s   t || jt| S )z2Returns the CPU device attached to a logical core.)r
   r    r>   r$   r'   r   r   r   r   cpu_device_name_at_coordinates   s    z'Topology.cpu_device_name_at_coordinatesc                 C   s    t || jt| | jt| S )z>Returns the name of the TPU device assigned to a logical core.)r	   r    r>   r!   rB   r   r   r   tpu_device_name_at_coordinates   s    z'Topology.tpu_device_name_at_coordinatesc                 C   s   | j jd S )z8Returns the number of TensorFlow tasks in the TPU slice.r   r   r   r;   r   r   r   r,      s    zTopology.num_tasksc                 C   s   | j jd S )z<Returns the number of TPU devices per task in the TPU slice.r   rE   r;   r   r   r   num_tpus_per_task   s    zTopology.num_tpus_per_taskc                 C   sh   | j du rbt }t| j|jdd< | jjd |_| jjd |_	|j
t| j  | | _ | j S )z,Returns the serialized form of the topology.Nr   r   )r   r   r)   r3   r   r&   r   r   r,   r-   r'   extendflattenSerializeToString)r$   r/   r   r   r   r%      s    

zTopology.serialized)NNN)N)N)__name__
__module____qualname____doc__r(   r   r   propertyr&   r<   r'   r=   r@   rA   rC   rD   r,   rF   r%   r   r   r   r   r   '   s*   
:%







r   )rM   numpyr   tensorflow.core.protobuf.tpur    tensorflow.python.util.tf_exportr   r	   r
   objectr   r   r   r   r   <module>   s   