
    BVh=                         d Z ddlmZ ddlmZ  eh d      Zi Zi Zd Z	d Z
d Z edg 	       G d
 de             Z edg	       G d de             Zy)zClass to represent a device.    )	tf_export)
pywrap_tfe>   CPUEPUGPUTPUCUSTOMc                      | d S t        |       S N)strinps    W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/device_spec.py_as_str_or_noner          *#c(*    c                      | d S t        |       S r   )intr   s    r   _as_int_or_noner   "   r   r   c                 @    | dv r| j                         S t        |       S )N)cpugpu)upperr   )device_types    r   _as_device_str_or_noner   &   s&     N"		%%r   
DeviceSpec)v1c                       e Zd ZdZdZ	 	 	 	 	 ddZd Zed        Zd Z	d Z
d	 Zed
        Zed        Zed        Zed        Zed        Zd Zed        Zedd       Zed        Zd Zd Zd Zy)DeviceSpecV2a  Represents a (possibly partial) specification for a TensorFlow device.

  `DeviceSpec`s are used throughout TensorFlow to describe where state is stored
  and computations occur. Using `DeviceSpec` allows you to parse device spec
  strings to verify their validity, merge them or compose them programmatically.

  Example:

  ```python
  # Place the operations on device "GPU:0" in the "ps" job.
  device_spec = DeviceSpec(job="ps", device_type="GPU", device_index=0)
  with tf.device(device_spec.to_string()):
    # Both my_var and squared_var will be placed on /job:ps/device:GPU:0.
    my_var = tf.Variable(..., name="my_variable")
    squared_var = tf.square(my_var)
  ```

  With eager execution disabled (by default in TensorFlow 1.x and by calling
  disable_eager_execution() in TensorFlow 2.x), the following syntax
  can be used:

  ```python
  tf.compat.v1.disable_eager_execution()

  # Same as previous
  device_spec = DeviceSpec(job="ps", device_type="GPU", device_index=0)
  # No need of .to_string() method.
  with tf.device(device_spec):
    my_var = tf.Variable(..., name="my_variable")
    squared_var = tf.square(my_var)
  ```

  If a `DeviceSpec` is partially specified, it will be merged with other
  `DeviceSpec`s according to the scope in which it is defined. `DeviceSpec`
  components defined in inner scopes take precedence over those defined in
  outer scopes.

  ```python
  gpu0_spec = DeviceSpec(job="ps", device_type="GPU", device_index=0)
  with tf.device(DeviceSpec(job="train").to_string()):
    with tf.device(gpu0_spec.to_string()):
      # Nodes created here will be assigned to /job:ps/device:GPU:0.
    with tf.device(DeviceSpec(device_type="GPU", device_index=1).to_string()):
      # Nodes created here will be assigned to /job:train/device:GPU:1.
  ```

  A `DeviceSpec` consists of 5 components -- each of
  which is optionally specified:

  * Job: The job name.
  * Replica: The replica index.
  * Task: The task index.
  * Device type: The device type string (e.g. "CPU" or "GPU").
  * Device index: The device index.
  )_job_replica_task_device_type_device_index
_as_string_hashNc                 z   t        |      | _        t        |      | _        t        |      | _        t        |      | _        t        |      | _        | j                  | j                  | j                  | j                  | j                  | j                        | _	        t        | j                               | _        y)af  Create a new `DeviceSpec` object.

    Args:
      job: string.  Optional job name.
      replica: int.  Optional replica index.
      task: int.  Optional task index.
      device_type: Optional device type string (e.g. "CPU" or "GPU")
      device_index: int.  Optional device index.  If left unspecified, device
        represents 'any' device_index.
    jobreplicataskr   device_indexN)r   r    r   r!   r"   r   r#   r$   _components_to_stringr%   hash	to_stringr&   )selfr)   r*   r+   r   r,   s         r   __init__zDeviceSpecV2.__init__k   s       $DI#G,DM &DJ.{;D(6D00IIZZ%%'' 1 )DO dnn&'DJr   c                     | j                   S )zReturn a string representation of this `DeviceSpec`.

    Returns:
      a string of the form
      /job:<name>/replica:<id>/task:<id>/device:<device_type>:<id>.
    )r%   r0   s    r   r/   zDeviceSpecV2.to_string   s     ??r   c                 *     | | j                  |       S )a;  Construct a `DeviceSpec` from a string.

    Args:
      spec: a string of the form
       /job:<name>/replica:<id>/task:<id>/device:CPU:<id> or
       /job:<name>/replica:<id>/task:<id>/device:GPU:<id> as cpu and gpu are
         mutually exclusive. All entries are optional.

    Returns:
      A DeviceSpec.
    )_string_to_components)clsspecs     r   from_stringzDeviceSpecV2.from_string   s     ))$/00r   c                 $    | j                  |      S )a  Parse a `DeviceSpec` name into its components.

    **2.x behavior change**:

    In TensorFlow 1.x, this function mutates its own state and returns itself.
    In 2.x, DeviceSpecs are immutable, and this function will return a
      DeviceSpec which contains the spec.

    * Recommended:

      ```
      # my_spec and my_updated_spec are unrelated.
      my_spec = tf.DeviceSpec.from_string("/CPU:0")
      my_updated_spec = tf.DeviceSpec.from_string("/GPU:0")
      with tf.device(my_updated_spec):
        ...
      ```

    * Will work in 1.x and 2.x (though deprecated in 2.x):

      ```
      my_spec = tf.DeviceSpec.from_string("/CPU:0")
      my_updated_spec = my_spec.parse_from_string("/GPU:0")
      with tf.device(my_updated_spec):
        ...
      ```

    * Will NOT work in 2.x:

      ```
      my_spec = tf.DeviceSpec.from_string("/CPU:0")
      my_spec.parse_from_string("/GPU:0")  # <== Will not update my_spec
      with tf.device(my_spec):
        ...
      ```

    In general, `DeviceSpec.from_string` should completely replace
    `DeviceSpec.parse_from_string`, and `DeviceSpec.replace` should
    completely replace setting attributes directly.

    Args:
      spec: an optional string of the form
       /job:<name>/replica:<id>/task:<id>/device:CPU:<id> or
       /job:<name>/replica:<id>/task:<id>/device:GPU:<id> as cpu and gpu are
         mutually exclusive. All entries are optional.

    Returns:
      The `DeviceSpec`.

    Raises:
      ValueError: if the spec was not valid.
    )r8   r0   r7   s     r   parse_from_stringzDeviceSpecV2.parse_from_string   s    j D!!r   c                 >     | j                   | j                  |       S )a!  Returns a new DeviceSpec which incorporates `dev`.

    When combining specs, `dev` will take precedence over the current spec.
    So for instance:
    ```
    first_spec = tf.DeviceSpec(job=0, device_type="CPU")
    second_spec = tf.DeviceSpec(device_type="GPU")
    combined_spec = first_spec.make_merged_spec(second_spec)
    ```

    is equivalent to:
    ```
    combined_spec = tf.DeviceSpec(job=0, device_type="GPU")
    ```

    Args:
      dev: a `DeviceSpec`

    Returns:
      A new `DeviceSpec` which combines `self` and `dev`
    )	__class___get_combined_propertiesr0   devs     r   make_merged_speczDeviceSpecV2.make_merged_spec   s     , 4>>488=>>r   c                     t        | j                  | j                  | j                  | j                  | j
                        }|j                  |        | j                  di |S )a  Convenience method for making a new DeviceSpec by overriding fields.

    For instance:
    ```
    my_spec = DeviceSpec=(job="my_job", device="CPU")
    my_updated_spec = my_spec.replace(device="GPU")
    my_other_spec = my_spec.replace(device=None)
    ```

    Args:
      **kwargs: This method takes the same args as the DeviceSpec constructor

    Returns:
      A DeviceSpec with the fields specified in kwargs overridden.
    r(    )dictr)   r*   r+   r   r,   updater=   )r0   kwargsinit_kwargss      r   replacezDeviceSpecV2.replace   sY      HHYY$$&&(K v4>>(K((r   c                     | j                   S r   )r    r3   s    r   r)   zDeviceSpecV2.job
  s    99r   c                     | j                   S r   )r!   r3   s    r   r*   zDeviceSpecV2.replica  s    ==r   c                     | j                   S r   )r"   r3   s    r   r+   zDeviceSpecV2.task  s    ::r   c                     | j                   S r   )r#   r3   s    r   r   zDeviceSpecV2.device_type  s    r   c                     | j                   S r   )r$   r3   s    r   r,   zDeviceSpecV2.device_index  s    r   c                 f   |j                   |j                   n| j                   |j                  |j                  n| j                  |j                  |j                  n| j                  |j                  |j                  n| j                  |j                  |j                  fS | j                  fS )a*  Combine the current DeviceSpec with another DeviceSpec.

    The combination of DeviceSpecs is will give priority to dev.

    Args:
      dev: a `DeviceSpec`

    Returns:
      A tuple of (job, replica, task, device_type, device_index) which
      represents the combination of self and dev.
    r(   r?   s     r   r>   z%DeviceSpecV2._get_combined_properties  s     77&DHH{{.DLLHH(dii??6D<L<L,,8 
 ?C>O>O r   c                      t        i       } t        j                         }|D ]3  }| j                  |j	                         j                  d      d          5 | t        z  } | S )N:   )setr   TF_ListPluggablePhysicalDevicesadddecodesplit_VALID_DEVICE_TYPES)valid_device_typesphysical_devicesdevices      r   _get_valid_device_typesz$DeviceSpecV2._get_valid_device_types2  s`    R!AAC" <V]]_2237:;<+.AAr   c                    t         j                  |       }||S | }d\  }}}}}| xs d} | j                  d      D cg c]  }|j                  d       }	}t        j	                         }
|	D ]  }t        |      }|s|dk(  r|d   dk(  r|d   }%|dk(  r|d   d	k(  r|d   }8|dk(  r|d   d
k(  r|d   }K|dk(  s|dk(  rW|d   j                         |
v rB|t        d|  d      |d   j                         }|dk(  s|d   dk7  st        |d         }|dk(  r6|d   dk(  r.|t        d|  d      |d   }|d   dk7  st        |d         }|s|d   dk7  st        d|d    d|  d       |||||f}|t         |<   |S c c}w )aE  Stateless portion of device spec string parsing.

    Args:
      spec: An optional string specifying a device specification.

    Returns:
      The parsed components of `spec`. Note that the result of this function
      must go through attribute setters of DeviceSpec, and should therefore NOT
      be used directly.
    NNNNN /rP      r   r)   rQ   r*   r+   zEMultiple device types are not allowed while parsing the device spec: .*   rZ   zUnknown attribute 'z1' is encountered while parsing the device spec: 'z'.)	_STRING_TO_COMPONENTS_CACHEgetrV   r   r[   lenr   
ValueErrorr   )r7   cached_resultraw_specr)   r*   r+   r   r,   xsplitsrX   ylyoutputs                 r   r5   z"DeviceSpecV2._string_to_components;  s
    033D9M H4P1C$\:2D$(JJsO4qaggcl4F4%==? Hq6b	
7qtu}!#1W1*aD'1W11$Qw"'!

8J(J$  ??CfAG H H!

+1W1qt9L1W1)$  ??CfAG H H!+qTS[qt9LAaDBJ01 7>>BV2G H H3H8 7D+|<F,2)MA 5s   Fc                 v   | ||||f}t         j                  |      }||S g }| |j                  d| z          ||j                  dt        |      z          ||j                  dt        |      z          |&d}|t        |      }|j                  d|d|       dj	                  |      }|t         |<   |S )z>Stateless portion of `to_string` (separated to allow caching).z/job:z	/replica:z/task:rb   z/device:rP   r^   )_COMPONENTS_TO_STRING_CACHEre   appendr   join)	r)   r*   r+   r   r,   keyrh   rn   device_index_strings	            r   r-   z"DeviceSpecV2._components_to_stringq  s     {L
9C/33C8M F
mmGcM"mmK#g,./mmHs4y()		!!,/mm{4GHIWWV_F'-$Mr   c                 t    t        || j                        xr! | j                         |j                         k(  S )aF  Checks if the `other` DeviceSpec is same as the current instance, eg have

       same value for all the internal fields.

    Args:
      other: Another DeviceSpec

    Returns:
      Return `True` if `other` is also a DeviceSpec instance and has same value
      as the current instance.
      Return `False` otherwise.
    )
isinstancer=   r/   )r0   others     r   __eq__zDeviceSpecV2.__eq__  s2     udnn- 2NN 113r   c                     | j                   S r   )r&   r3   s    r   __hash__zDeviceSpecV2.__hash__  s    ::r   c                     d| j                    d| j                   d| j                   d| j                   d| j                   dS )Nz<DeviceSpec(job=z
, replica=z, task=z, device_type=z, device_index=z)>r(   r3   s    r   __repr__zDeviceSpecV2.__repr__  sN    
488*Jt||nGDII; O''(8I8I7J"	NOr   r]   r   )__name__
__module____qualname____doc__	__slots__r1   r/   classmethodr8   r;   rA   rH   propertyr)   r*   r+   r   r,   r>   staticmethodr[   r5   r-   rx   rz   r|   rC   r   r   r   r   .   s   6p&)  (: 1 15"n?0)6          (   3 3j  23 Or   r   c                      e Zd Zej                  Zej
                  Zej                  j                  d        Zej                  j                  d        Zej                  j                  d        Z	ej                  j                  d        Z
ej                  j                  d        Zd Zd Zd Zd	 Zej                  j                  e_        ej                  j                  e_        y
)DeviceSpecV1c                 B    t        |      | _        d\  | _        | _        y N)NN)r   r    r%   r&   )r0   r)   s     r   r)   zDeviceSpecV1.job  s    $DI",DOTZr   c                 B    t        |      | _        d\  | _        | _        y r   )r   r!   r%   r&   )r0   r*   s     r   r*   zDeviceSpecV1.replica  s    #G,DM",DOTZr   c                 B    t        |      | _        d\  | _        | _        y r   )r   r"   r%   r&   )r0   r+   s     r   r+   zDeviceSpecV1.task  s     &DJ",DOTZr   c                 B    t        |      | _        d\  | _        | _        y r   )r   r#   r%   r&   )r0   r   s     r   r   zDeviceSpecV1.device_type  s    .{;D",DOTZr   c                 B    t        |      | _        d\  | _        | _        y r   )r   r$   r%   r&   )r0   r,   s     r   r,   zDeviceSpecV1.device_index  s    (6D",DOTZr   c                 n    | j                   t        | j                               | _         | j                   S r   )r&   r.   r/   r3   s    r   rz   zDeviceSpecV1.__hash__  s)    zz()dj::r   c                     | j                   M| j                  | j                  | j                  | j                  | j
                  | j                        | _         | j                   S )Nr(   )r%   r-   r)   r*   r+   r   r,   r3   s    r   r/   zDeviceSpecV1.to_string  sV    22hh,,yy&&(( 3 *do ??r   c                 f    | j                  |      \  | _        | _        | _        | _        | _        | S r   )r5   r)   r*   r+   r   r,   r:   s     r   r;   zDeviceSpecV1.parse_from_string  s1    44T:TXt|TY(8	Kr   c                 d    | j                  |      \  | _        | _        | _        | _        | _        y)zMerge the properties of "dev" into this `DeviceSpec`.

    Note: Will be removed in TensorFlow 2.x since DeviceSpecs will become
          immutable.

    Args:
      dev: a `DeviceSpec`.
    N)r>   r)   r*   r+   r   r,   r?   s     r   
merge_fromzDeviceSpecV1.merge_from  s.     77<TXt|TY(8	r   N)r}   r~   r   r   r   r   r)   setterr*   r+   r   r,   rz   r/   r;   r   rC   r   r   r   r     s      '$$)- - - - - - ""- #- ##- $-

= #,,44)*<<DDr   r   N)r    tensorflow.python.util.tf_exportr   tensorflow.pythonr   	frozensetrW   rd   rp   r   r   r   objectr   r   rC   r   r   <module>r      s    # 6 (   FG 
 !   ++& <BrO6 rO  rOj |n@E< @E @Er   