
    BVhI                         d Z ddlmZ ddlmZ  ej
                         rej                  Znej                  Zd Z	d Z
d Zi Zi Zd Z G d d	e      Zy
)zClass to represent a device.    )tf2)device_specc                 .    t         j                  |        y)zvCheck that a device spec is valid.

  Args:
    spec: a string.

  Raises:
    An exception if the spec is invalid.
  N)
DeviceSpecfrom_string)specs    R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/device.pycheck_validr
      s         c                 6    t        | t        j                        S )z;Abstract away the fact that DeviceSpecV2 is the base class.)
isinstancer   DeviceSpecV2)objs    r	   is_device_specr   (   s    	C11	22r   c                     | yt        |       r| j                         S t        j                  |       } | j                         S )zCReturns a canonical name for the given `DeviceSpec` or device name. )r   	to_stringr   r   )devices    r	   canonical_namer   -   s@    ^F##F+Fr   c                     t        | t              r| S t        j                  |       }|r|S t        |       }|t        | <   |S )a  Returns a device function that merges devices specifications.

  This can be used to merge partial specifications of devices. The
  innermost setting for a device field takes precedence. For example:

    with tf.device(merge_device("/device:GPU:0"))
      # Nodes created here have device "/device:GPU:0"
      with tf.device(merge_device("/job:worker")):
        # Nodes created here have device "/job:worker/device:GPU:0"
        with tf.device(merge_device("/device:CPU:0")):
          # Nodes created here have device "/job:worker/device:CPU:0"
          with tf.device(merge_device("/job:ps")):
            # Nodes created here have device "/job:ps/device:CPU:0"

  Args:
    spec: A `DeviceSpec` or a device spec string (partially) describing the
      device that should be used for all nodes created in the scope of
      the returned device function's with block.

  Returns:
    A MergeDevice object with the above-described behavior.

  Raises:
    ValueError: if the spec was not valid.
  )r   MergeDevice_cached_mergersget)r   mergers     r	   merge_devicer   =   sD    6 k"Kt$&Mt& /$	-r   c                   J     e Zd ZdZdgZd Zd Zd Z fdZe	d        Z
 xZS )r   a  Wraps a device specification (DeviceSpec or str) with merge functionality.

  When called, this class will merge a node_def with its own spec. It also
  exposes a `shortcut_string_merge` method which can significantly improve
  performance of device placement.
  _specc                    t        |t        j                        r|| _        y t        |t        j                        r/|j
                  j                  |j                               | _        y t        j                  |      | _        y )N)	r   r   r   r   DeviceSpecV1	__class__r   r   r   )selfr   s     r	   __init__zMergeDevice.__init__n   sX    $001dj	D+22	3>>--dnn.>?dj))$/djr   c                 ~    t         j                  |j                  xs d      }| j                  j	                  |      S )Nr   )r   r   r   r   make_merged_spec)r!   node_defcurrent_devices      r	   __call__zMergeDevice.__call__w   s2      ++HOO,ArBN::&&~66r   c                     |j                   xs d}| j                  |f}t        j                  |      }|(| j	                  |      j                         }|t        |<   |S )aH  Merge a node def without materializing a full DeviceSpec object.

    Often a device merge is invoked in order to generate a string which can be
    passed into the c api. In such a case, we can cache the
      node_def.device  ->  merge_result_string

    map, and in most cases avoid:
      - Materializing a copy of self._spec (In the case of DeviceSpecV1)
      - Materializing a DeviceSpec for node_def.device
      - A DeviceSpec.merge_from invocation

    In practice the cache hit rate for this function is very high, because the
    number of invocations when iterating through the device stack is much
    larger than the number of devices.

    Args:
      node_def: An Operation (or Operation-like) to merge device constraints
        with self._spec

    Returns:
      A string containing the merged device specification.
    r   )r   r   _string_merge_cacher   r'   r   )r!   r%   r   	merge_keyresults        r	   shortcut_string_mergez!MergeDevice.shortcut_string_merge   s^    . __"FV$I $$Y/F~ }}X&002f'-)$Mr   c                 x    dj                  t        t        |          | j                  j                               S )Nz{} (spec: {}))formatsuperr   __repr__r   r   )r!   r    s    r	   r0   zMergeDevice.__repr__   s4    !!k4)+TZZ-A-A-CE Er   c                 J    t        | j                  j                                S )aT  Indicate whether the wrapped spec is empty.

    In the degenerate case where self._spec is an empty specification, a caller
    may wish to skip a merge step entirely. (However this class does not have
    enough information to make that determination.)

    Returns:
      A boolean indicating whether a device merge will be trivial.
    )boolr   r   )r!   s    r	   is_null_mergezMergeDevice.is_null_merge   s     DJJ((*+++r   )__name__
__module____qualname____doc__	__slots__r"   r'   r,   r0   propertyr3   __classcell__)r    s   @r	   r   r   d   s;     i)07!FE 
, 
,r   r   N)r7   tensorflow.pythonr   tensorflow.python.frameworkr   enabledr   r   r   r
   r   r   r   r)   r   objectr    r   r	   <module>r@      sa     # ! 33;;=''*''*
3
  $NN,& N,r   