
    AVh                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddZd Z	d	 Z
 G d
 de      Z G d de      Zd Zd Zd Zy)z!Device-related support functions.    )context)configdevice)opsNc                 2   t        | t        j                        r*t        j                  j                  | j                        } nt        j                  j                  |       } | j                  <| j                  | j                  j                         k(  sJ d| j                  d       t        j                  dddd      }t        j                         rot        j                  j                  t        j                  d      d   j                        }|j                  r|j                  |      }n|j                  d      }|r.|j                  t        j                  j                  |            }|j                  |       }|j!                         S )a  Canonicalize device string.

  If d has missing components, the rest would be deduced from the `default`
  argument or from '/replica:0/task:0/device:CPU:0'. For example:
    If d = '/cpu:0', default='/job:worker/task:1', it returns
      '/job:worker/replica:0/task:1/device:CPU:0'.
    If d = '/cpu:0', default='/job:worker', it returns
      '/job:worker/replica:0/task:0/device:CPU:0'.
    If d = '/gpu:0', default=None, it returns
      '/replica:0/task:0/device:GPU:0'.

  Note: This uses "job:localhost" as the default if executing eagerly.

  Args:
    d: a device string or tf.config.LogicalDevice
    default: a string for default device if d doesn't have all components.

  Returns:
    a canonicalized device string.
  zDevice type 'z' must be all-caps.r   CPU)replicataskdevice_typedevice_index	localhost)job)
isinstancer   LogicalDevice	tf_device
DeviceSpecfrom_stringnamer   upperr   #executing_eagerly_outside_functionsr   list_logical_devicesr   make_merged_specreplace	to_string)ddefaultresulthost_cpus       X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/distribute/device_util.pycanonicalizer!      sF   * 7(()((0A((+A	
	!--1==3F3F3H"H ?./mm=?	H aU<&,,. ##//##E*1-224H||&&x0f ~~+~.f$$((13F ""1%&				    c                     t        |       }t        j                  j                  |      }|j	                  ddd      }|j                         S )a  Partially canonicalize device string.

  This returns device string from `d` without including job and task.
  This is most useful for parameter server strategy where the device strings are
  generated on the chief, but executed on workers.

   For example:
    If d = '/cpu:0', default='/job:worker/task:1', it returns
      '/replica:0/device:CPU:0'.
    If d = '/cpu:0', default='/job:worker', it returns
      '/replica:0/device:CPU:0'.
    If d = '/gpu:0', default=None, it returns
      '/replica:0/device:GPU:0'.

  Note: This uses "job:localhost" as the default if executing eagerly.

  Args:
    d: a device string or tf.config.LogicalDevice

  Returns:
    a partially canonicalized device string.
  Nr   )r   r   r
   )r!   r   r   r   r   r   )r   canonicalized_devicespecs      r    !canonicalize_without_job_and_taskr&   L   sF    . &a				)	)*>	?$	$T1	5$		r"   c                 ,    t        | t                     S )z0Canonicalize `d` with current device as default.)r   )r!   current)r   s    r    resolver)   i   s    	a	++r"   c                       e Zd ZdZddgZd Zy)_FakeNodeDefz"A fake NodeDef for _FakeOperation.opr   c                      d| _         d| _        y N )r,   r   selfs    r    __init__z_FakeNodeDef.__init__s   s    DGDIr"   N)__name__
__module____qualname____doc__	__slots__r2    r"   r    r+   r+   n   s    *Vn)r"   r+   c                   "    e Zd ZdZd Zd Zd Zy)_FakeOperationz4A fake Operation object to pass to device functions.c                 L    d| _         d| _        d| _        t               | _        y r.   )r   typer   r+   node_defr0   s    r    r2   z_FakeOperation.__init__{   s     DKDIDI NDMr"   c                 8    t        j                  |      | _        y N)r   _device_stringr   )r1   r   s     r    _set_devicez_FakeOperation._set_device   s    $$V,DKr"   c                     || _         y r?   r   )r1   
device_strs     r    _set_device_from_stringz&_FakeOperation._set_device_from_string   s	    DKr"   N)r3   r4   r5   r6   r2   rA   rD   r8   r"   r    r:   r:   x   s    <#-r"   r:   c                      t        j                         r t        j                         j                  } | S t	               }t        j
                         j                  |       |j                  } | S )z;Return a string (not canonicalized) for the current device.)r   r   r   device_namer:   get_default_graph_apply_device_functionsr   )r   r,   s     r    r(   r(      sX     	,,.%%A
 
( 
	B33B7
		A	
(r"   c                     t         j                  j                  |       }t        j                  |j                  |j                  |j
                  dd      j                         S )z;Returns the corresponding host device for the given device.r	   r   )r   r
   r   r   r   )r   r   r   r   r
   r   r   )r   r%   s     r    get_host_for_devicerJ      sJ    				)	)&	1$			
((DLLtyya
))25r"   c                 @    t        d t        |       D              xs dS )z-Returns device strings for local GPUs or CPU.c              3   &   K   | ]	  }d |z    yw)z/device:GPU:%dNr8   ).0is     r    	<genexpr>z.local_devices_from_num_gpus.<locals>.<genexpr>   s     > 1$>s   )z/device:CPU:0)tuplerange)num_gpuss    r    local_devices_from_num_gpusrS      s!    
>eHo>
> 
r"   r?   )r6   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   r!   r&   r)   objectr+   r:   r(   rJ   rS   r8   r"   r    <module>rW      sO    ( , . ; +1h:,
6 V  	5r"   