
    BVh             
          d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z" ddlm#Z# ddl$m%Z% ddl$m&Z& ddl$m'Z' ddl$m(Z( ddl)m*Z* ddl+m,Z, dZ-dZ.dZ/ ej`                         re/ne.Z1i a2e"jf                  ji                  d      Z5dZ6ejn                  Z8ejr                  Z:ejv                  Z<ejz                  Z>dZ?dZ@dZA ej                  dd      ZCe#j`                  ZD ej                  d       d!k(  aF ej                  d"      d!k(  aG ej                  d#      d$k7  aHd% ZId& ZJd' ZKd( ZLd) ZMd* ZNd+ ZOd, ZPd- ZQd. ZRej                  d/        ZTej                  d0        ZU  e,d1g 2      eD        G d3 d4eV      ZW G d5 d6      ZXi ZY G d7 d8ej                        Z[ ej                  d9g d:      Z] G d; d<ej                        Z^ e,d=       G d> d? ej                  d?d@dAg                   Z_ e,dBdC       G dD dE ej                  dEg dF                   Z` e,dG       G dH dI ej                  dId@dAg                   Za G dJ dKeV      Zb eb       Zc G dL dMeV      Zd G dN dO      Ze G dP dQeV      Zfdag ej                         ZidR ZjdS ZkdT ZldU ZmdV ZndW ZodX ZpdY ZqdZ Zrd[ Zsd\ Ztd] Zu e,d^g 2      d_        Zv e,d^g2      d`        Zwda Zxdb Zydc Zz e,ddg 2      de        Z{df Z|dg Z! e,dhg 2      di        Z} e,djg 2      dk        Z~ e,dlg 2      dm        Z e,dng 2      do        Z e,dp      dq        Z e,dr      ds        Ze(j                  dt        Zdu Ze(j                  dv        Ze(j                  dw        Z e,dx      e(j                  dy               Zdz Zd{ Zd| Zd} Zd~ Zd Zd Zej                  dd       Zd Zd Zd Zd Zd Z e,d      e(j                  d               Zd Z e,d      d        Zd Zd Zd Zd Zd Zd Zd Zee'_        y)z%State management for eager execution.    N)logging)coordination_config_pb2)function_pb2)graph_debug_info_pb2)
config_pb2)rewriter_config_pb2)
pywrap_tfe)tf2)pywrap_tf_session)cancellation)execute)executor)
monitoring)
c_api_util)device)
tfrt_utils)compat)function_utils)is_in_graph_mode)tf_contextlib)
deprecated)	tf_exportT    iiX  z3/tensorflow/api/python/eager_context_create_counterz7Counter for number of eager contexts created in Python.TF_JIT_COMPILE_REWRITE1&TF_XLA_SHARDING_FOR_RESOURCE_VARIABLESTF_OPTIONALS_IN_GRADIENTS0c                       yNT r"       O/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/eager/context.py run_eager_op_as_function_enabledr%   c   s    	r#   c                  <    da t               dt               _        yy)zRun jit_compile functions through rewrite pass.

  This runs jit_compile functions through all of the multidevice function
  rewrite passes.
  TN_JIT_COMPILE_REWRITE_ENABLEDcontext_safejit_compile_rewriter"   r#   r$   enable_jit_compile_rewriter+   h   s!     "&^)-LN&  r#   c                  <    da t               dt               _        y y NFr'   r"   r#   r$   disable_jit_compile_rewriter.   u   s    !&^).LN&  r#   c                  J    t               t               j                  S t        S N)r)   r*   r(   r"   r#   r$   jit_compile_rewrite_enabledr1   |   s    ^>---	%%r#   c                  <    da t               dt               _        yy)zEnables support for annotating TF2 ResourceVariables with XLA sharding.

  This allows placing XLA sharding annotations on the TF2 ResourceVariable
  python object and inserts an XlaShardingOp with the annotation whenever a
  ReadVariableOp is created.
  TN$_XLA_SHARDING_FOR_RESOURCE_VARIABLESr)   #xla_sharding_for_resource_variablesr"   r#   r$   *enable_xla_sharding_for_resource_variablesr6      s!     *.&^9=LN6  r#   c                  <    da t               dt               _        y y r-   r3   r"   r#   r$   +disable_xla_sharding_for_resource_variablesr8      s    ).&^9>LN6  r#   c                  J    t               t               j                  S t        S r0   )r)   r5   r4   r"   r#   r$   +xla_sharding_for_resource_variables_enabledr:      s    ^>===	--r#   c                  <    da t               dt               _        y y r!   _OPTIONALS_IN_GRADIENTSr)   optionals_in_gradientsr"   r#   r$   enable_optionals_in_gradientsr?      s     ^,0LN)  r#   c                  <    da t               dt               _        y y r-   r<   r"   r#   r$   disable_optionals_in_gradientsrA      s    !^,1LN)  r#   c                  J    t               t               j                  S t        S r0   )r)   r>   r=   r"   r#   r$   optionals_in_gradients_enabledrC      s    ^>000	  r#   c               #   |   K   t               } 	 t                d | rt                yy# | rt                w w xY ww)zTemporarily disables XLA sharding for resource variables.

  Should be a no-op if it is already disabled.

  Yields:
    None.
  N)r:   r8   r6   previously_enableds    r$   7temporarily_disable_xla_sharding_for_resource_variablesrG      s=      CD3/1	02 02    <* <9<c               #   |   K   t               } 	 t                d | rt                yy# | rt                w w xY ww)zTemporarily disables generation of optionals in gradients.

  Should be a no-op if it is already disabled.

  Yields:
    None.
  N)rC   rA   r?   rE   s    r$   *temporarily_disable_optionals_in_gradientsrJ      s<      67&"$	#% #% rH   z__internal__.is_tfrt_enabled)v1c                   2    e Zd ZdZg dZddZd Zd Zd Zy)	_EagerTensorCachezASimple cache which evicts items based on length in a FIFO manner.)_data
_max_items_max_tensor_sizec                 R    t        j                         | _        || _        || _        y r0   )collectionsOrderedDictrN   rO   rP   )self	max_itemsmax_tensor_sizes      r$   __init__z_EagerTensorCache.__init__   s!    ((*DJDO+Dr#   c                     |j                         | j                  kD  ry || j                  |<   t        | j                        | j                  kD  r| j                  j                  d       y y )NF)last)_num_elementsrP   rN   lenrO   popitemrT   keyvalues      r$   putz_EagerTensorCache.put   sW    t444DJJsO
4::(
jje$ )r#   c                 :    | j                   j                  |d       S r0   )rN   getrT   r^   s     r$   rb   z_EagerTensorCache.get   s    ::>>#t$$r#   c                 8    | j                   j                          y r0   )rN   clearrT   s    r$   flushz_EagerTensorCache.flush   s    JJr#   N)   i'  )	__name__
__module____qualname____doc__	__slots__rW   r`   rb   rg   r"   r#   r$   rM   rM      s    I9),
%%r#   rM   c                       e Zd ZdZddgZddZed        Zej                  d        Zed        Z	e	j                  d	        Z	d
 Z
y)FunctionCallOptionsz|Options applied at call sites of eager functions.

  Eager functions are functions decorated with tf.contrib.eager.defun.
  _config_proto_serialized_executor_typeNc                      || _         || _        y)aR  Constructor.

    Args:
      executor_type: (optional) name of the executor to be used to execute the
        eager function. If None or an empty string, the default Tensorflow
        executor will be used.
      config_proto: (optional) a `config_pb2.ConfigProto` proto or a serialized
        string of that proto. The config used by Grappler when optimizing the
        function graph. Each concrete function is optimized the first time is
        called. Changing config_proto after the first call has no effect. If
        config_proto is None, an empty RewriterConfig will be used.
    N)config_proto_serializedexecutor_type)rT   rt   config_protos      r$   rW   zFunctionCallOptions.__init__   s     $0D &Dr#   c                     | j                   S r0   rq   rf   s    r$   rt   z!FunctionCallOptions.executor_type  s    r#   c                     || _         y r0   rw   )rT   rt   s     r$   rt   z!FunctionCallOptions.executor_type  s
    'Dr#   c                     | j                   S r0   )rp   rf   s    r$   rs   z+FunctionCallOptions.config_proto_serialized  s    (((r#   c                 0   t        |t        j                        r|j                  d      | _        y t        |t
              r|| _        y |(t        j                         j                         | _        y t        dj                  t        |                  )NT)deterministiczrthe rewriter config must be either a config_pb2.ConfigProto, or a serialized string of that proto or None. got: {})	
isinstancer   ConfigProtoSerializeToStringrp   str
ValueErrorformattyperT   configs     r$   rs   z+FunctionCallOptions.config_proto_serialized  s    &*001&,&>&> '? 'd# 
FC	 &,d#	

 
 
"
4
4
6 # ##)6$v,#7 r#   c                     | j                   t        j                         }n| j                   }| j                  xs d}||dS )Nr   )rt   ru   )rs   r   get_disabled_rewriter_configrt   )rT   r   rt   s      r$   as_attrszFunctionCallOptions.as_attrs+  sB    ##+::<f++f&&,"M*FCCr#   NN)ri   rj   rk   rl   rm   rW   propertyrt   setterrs   r   r"   r#   r$   ro   ro      s{    
 *+;<)'    ( ( ) ) !! "$Dr#   ro   c                   J     e Zd ZdZddgZ fdZed        Zed        Z xZ	S )_TensorCacheszThread local tensor caches._ones_rank_cache_zeros_cachec                 >    t         |           d | _        d | _        y r0   )superrW   r   r   )rT   	__class__s    r$   rW   z_TensorCaches.__init__@  s    	G DDr#   c                 P    | j                   st               | _         | j                   S r0   )r   rM   rf   s    r$   ones_rank_cachez_TensorCaches.ones_rank_cacheE  s"      /1d   r#   c                 P    | j                   st               | _         | j                   S r0   )r   rM   rf   s    r$   zeros_cachez_TensorCaches.zeros_cacheK  s"    +-dr#   )
ri   rj   rk   rl   rm   rW   r   r   r   __classcell__r   s   @r$   r   r   ;  s=    #!>2)
 ! !
  r#   r   ContextSwitchis_building_functionenter_context_fndevice_stackc                   .     e Zd ZdZ fdZd Zd Z xZS )_ContextSwitchStackz)A thread-local stack of context switches.c                 f    t         |           g | _        |r| j                  dt        d        y y )NFr   )r   rW   stackpush
eager_mode)rT   eagerr   s     r$   rW   z_ContextSwitchStack.__init__]  s9    	GDJ ii$%   r#   c                 P    | j                   j                  t        |||             y)a  Push metadata about a context switch onto the stack.

    A context switch can take any one of the two forms: installing a graph as
    the default graph, or entering the eager context. For each context switch,
    we record whether or not the entered context is building a function.

    Args:
      is_building_function: (bool.) Whether the context is building a function.
      enter_context_fn: (function.) A callable that executes the context switch.
        For example, `graph.as_default` or `eager_mode`.
      device_stack: If applicable, the device function stack for this graph.
        When breaking out of graphs in init_scope, the innermost nonempty device
        stack is used. Eager contexts put `None` here and the value is never
        used.
    N)r   appendr   )rT   r   r   r   s       r$   r   z_ContextSwitchStack.pushl  s$    " 	JJ*,<lKr#   c                 8    | j                   j                          y)zPop the stack.N)r   poprf   s    r$   r   z_ContextSwitchStack.pop  s     	JJNNr#   )ri   rj   rk   rl   rW   r   r   r   r   s   @r$   r   r   Z  s    1*r#   r   zconfig.LogicalDevicec                       e Zd ZdZy)LogicalDevicea  Abstraction for a logical device initialized by the runtime.

  A `tf.config.LogicalDevice` corresponds to an initialized logical device on a
  `tf.config.PhysicalDevice` or a remote device visible to the cluster. Tensors
  and operations can be placed on a specific logical device by calling
  `tf.device` with a specified `tf.config.LogicalDevice`.

  Fields:
    name: The fully qualified name of the device. Can be used for Op or function
      placement.
    device_type: String declaring the type of device such as "CPU" or "GPU".
  Nri   rj   rk   rl   r"   r#   r$   r   r     s    r#   r   namedevice_typez!config.LogicalDeviceConfigurationz.config.experimental.VirtualDeviceConfigurationc                   *     e Zd ZdZ	 	 	 d fd	Z xZS )LogicalDeviceConfigurationaV  Configuration class for a logical devices.

  The class specifies the parameters to configure a `tf.config.PhysicalDevice`
  as it is initialized to a `tf.config.LogicalDevice` during runtime
  initialization. Not all fields are valid for all device types.

  See `tf.config.get_logical_device_configuration` and
  `tf.config.set_logical_device_configuration` for usage examples.

  Fields:
    memory_limit: (optional) Maximum memory (in MB) to allocate on the virtual
      device. Currently only supported for GPUs.
    experimental_priority: (optional) Priority to assign to a virtual device.
      Lower values have higher priorities and 0 is the default.
      Within a physical GPU, the GPU scheduler will prioritize ops on virtual
      devices with higher priority. Currently only supported for Nvidia GPUs.
    experimental_device_ordinal: (optional) Ordinal number to order the virtual
    device.
      LogicalDevice with lower ordinal number will receive a lower device id.
      Physical device id and location in the list is used to break ties.
      Currently only supported for Nvidia GPUs.
  c                 (    t         |   | |||      S r0   )r   __new__)clsmemory_limitexperimental_priorityexperimental_device_ordinalr   s       r$   r   z"LogicalDeviceConfiguration.__new__  s!     7?\02M r#   )NNN)ri   rj   rk   rl   r   r   r   s   @r$   r   r     s    2  "&	 r#   r   )r   r   r   zconfig.PhysicalDevicec                       e Zd ZdZy)PhysicalDevicear  Abstraction for a locally visible physical device.

  TensorFlow can utilize various devices such as the CPU or multiple GPUs
  for computation. Before initializing a local device for use, the user can
  customize certain properties of the device such as it's visibility or memory
  configuration.

  Once a visible `tf.config.PhysicalDevice` is initialized one or more
  `tf.config.LogicalDevice` objects are created. Use
  `tf.config.set_visible_devices` to configure the visibility of a physical
  device and `tf.config.set_logical_device_configuration` to configure multiple
  `tf.config.LogicalDevice` objects for a `tf.config.PhysicalDevice`. This is
  useful when separation between models is needed or to simulate a multi-device
  environment.

  Fields:
    name: Unique identifier for device.
    device_type: String declaring the type of device such as "CPU" or "GPU".
  Nr   r"   r#   r$   r   r     s    ( r#   r   c                   $    e Zd ZdZddgZd Zd Zy)_AtomicCounterzA simple atomic counter._value_lockc                 D    d| _         t        j                         | _        y Nr   )r   	threadingLockr   rf   s    r$   rW   z_AtomicCounter.__init__  s    DK!DJr#   c                     | j                   5  | xj                  dz  c_        | j                  cd d d        S # 1 sw Y   y xY w)Nr   )r   r   rf   s    r$   increment_and_getz _AtomicCounter.increment_and_get  s3    	 
kkQk[[  s	   !8AN)ri   rj   rk   rl   rm   rW   r   r"   r#   r$   r   r     s     !)"r#   r   c                   "    e Zd ZdZdgZd Zd Zy)_TensorCacheDeleterz*Deletes tensor caches for a given context._context_idc                     || _         y r0   )r   )rT   
context_ids     r$   rW   z_TensorCacheDeleter.__init__  s
    !Dr#   c                 Z    t         y | j                  t         v rt         | j                  = y y r0   )_tensor_caches_mapr   rf   s    r$   __del__z_TensorCacheDeleter.__del__  s-    !--
T--
. .r#   N)ri   rj   rk   rl   rm   rW   r   r"   r#   r$   r   r     s    2o)"/r#   r   c                      e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd ZefdZefdZd Zd Zd Z	 	 	 	 	 	 	 ddZed        Zd ZddZd Zd Zd Zd Zd Zd Z	 	 	 	 ddZd Zd Z ed        Z!ed         Z"d! Z#e$jJ                  d"        Z&d# Z'd$ Z(d% Z)ed&        Z*e*jV                  d'        Z*ed(        Z,ed)        Z-d* Z.d+ Z/d, Z0d- Z1ed.        Z2e2jV                  d/        Z2d0 Z3ed1        Z4e4jV                  d2        Z4ed3        Z5dd4Z6d5 Z7d6 Z8ed7        Z9e9jV                  d8        Z9d9 Z:d: Z;d; Z<d< Z=d= Z>d> Z?d? Z@d@ ZAdA ZBdB ZCdC ZDdD ZEedE        ZFdF ZGdG ZHdH ZIedI        ZJedJ        ZKeKjV                  dK        ZKddLZLdM ZMddNZNdO ZOdP ZPddQZQddRZRddSZSdT ZTdU ZUdV ZVdW ZWdX ZXdY ZYddZZZ	 dd[Z[ e\dd\d]      d^        Z]ed_        Z^ed`        Z_e^jV                  da        Z^e_jV                  db        Z_edc        Z`e`jV                  dd        Z`de Zadf Zbedg        ZcecjV                  dh        Zcedi        ZdedjV                  dj        Zdedk        ZeeejV                  dl        Zeedm        ZfefjV                  dn        Zfedo        ZgegjV                  dp        Zgedq        ZhehjV                  dr        Zheds        ZieijV                  dt        Ziedu        ZjejjV                  dv        Zjedw        ZkekjV                  dx        Zkedy        ZleljV                  dz        Zld{ Zmd| Znd} Zod~ Zpd Zqd Zred        Zsy)Contextz.Environment in which eager operations execute.Nc                 \   t         j                         | _        t        | j                        | _        t               t        | j                  <   || _        t        j                  | d t              | _        t        | j                               | _        d| _        d| _        d| _        t%        j&                         | _        d| _        |t,        }|| _        d| _        |dt2        t4        fvrt7        d|z        |t2        }|t4        k(  | _        t;               | _        t?               | _         tC               | _"        tG               | _$        || _%        d| _&        d| _'        d| _(        d| _)        d| _*        d| _+        t%        j&                         | _,        d| _-        d| _.        d| _/        g | _0        d| _1        i | _2        d| _3        d| _4        d| _5        d| _6        d| _7        d| _8        d| _9        i | _:        tv        jy                         j{                  d       d| _>        d| _?        y)aH  Creates a new Context.

    Args:
      config: (Optional.) A `ConfigProto` protocol buffer with configuration
        options for the Context. Note that a lot of these options may be
        currently unimplemented or irrelevant when eager execution is enabled.
      device_policy: (Optional.) What policy to use when trying to run an
        operation on a device with inputs which are not on that device. When set
        to None, an appropriate value will be picked automatically. The value
        picked may change between TensorFlow releases.  Defaults to
        DEVICE_PLACEMENT_SILENT. Valid values: DEVICE_PLACEMENT_EXPLICIT -
        raises an error if the placement is not correct. DEVICE_PLACEMENT_WARN -
        copies the tensors which are not on the right device but raises a
        warning. DEVICE_PLACEMENT_SILENT - silently copies the tensors. This
        might hide performance problems. DEVICE_PLACEMENT_SILENT_FOR_INT32 -
        silently copies int32 tensors, raising errors on the other ones.
      execution_mode: (Optional.) Policy controlling how operations dispatched
        are actually executed. When set to None, an appropriate value will be
        picked automatically. The value picked may change between TensorFlow
        releases. Valid values: - SYNC: executes each operation synchronously.
        ASYNC - executes each operation asynchronously. These operations may
        return "non-ready" handles.
      server_def: (Optional.) A tensorflow::ServerDef proto. Enables execution
        on remote devices. GrpcServers need to be started by creating an
        identical server_def to this, and setting the appropriate task_indexes,
        so that the servers can communicate. It will then be possible to execute
        operations on remote devices.

    Raises:
     ValueError: If execution_mode is not valid.
    c                      t         t        k(  S r0   )default_execution_mode
EAGER_MODEr"   r#   r$   <lambda>z"Context.__init__.<locals>.<lambda>;  s    /:= r#   )is_eagerdevice_specNFz0execution_mode should be None/SYNC/ASYNC. Got %sr   r   )@_context_id_counterr   _idr   _tensor_cache_deleterr   r   _configr	   EagerContextThreadLocalData_starting_device_spec_thread_local_datar   executing_eagerly_context_switches_context_handle_context_devices_seedr   r   _initialize_lock_initializedDEVICE_PLACEMENT_SILENT_device_policy_mirroring_policySYNCASYNCr   _default_is_asyncis_tfrt_enabled	_use_tfrtr1   _jit_compile_rewriter:   $_xla_sharding_for_resource_variablesrC   _optionals_in_gradients_server_def_collective_ops_server_def_collective_leader(_collective_scoped_allocator_enabled_ops"_collective_use_nccl_communication_collective_device_filters_coordination_service_config_device_lock_physical_devices_physical_device_to_index_pluggable_devices_visible_device_list_memory_growth_map_virtual_device_map_optimizer_jit_intra_op_parallelism_threads_inter_op_parallelism_threads_soft_device_placement_log_device_placement_operation_timeout_in_ms_enable_mlir_graph_optimization_optimizer_experimental_options$_python_eager_context_create_counterget_cellincrease_by_is_global_context_set_server_def_retries)rT   r   device_policyexecution_mode
server_defs        r$   rW   zContext.__init__  s   P #446DH!4TXX!>D#0?txx DL(DD=)D
 11G1G1IJDD DDJ%NN,DD-m'D!DdD%00
<~
M  n+u4D$&DN ; =D35 	- $B#CD !D&*D#"D48D1.2D+&*D#(,D%!(D!D%)D""D "D"D!D D)-D&)-D&"&D!%D$(D!+/D(+-D((113??B#D
 $%D r#   c                 D   || _         	 t        |       t        j                  |      | _        | j                   t        j                  | j                         yy# t
        $ r; t        t        j                  |            }t        j                  |      | _        Y pw xY w)z,Set a global eager mode seed for random ops.N)r   hashrandomRandom_rng	TypeErrorintnparrayr   r	   TFE_ContextClearCaches)rT   seeds     r$   _set_global_seedzContext._set_global_seedz  s    DJ&
4j--%di
 '''(<(<= (	  &$ d--%di&s   %A ABBc                 B    | j                   j                  dt              S )a2  Returns a fake operation seed.

      In eager mode, user shouldn't set or depend on operation seed.
      Here, we generate a random seed based on global seed to make
      operation's randomness different and depend on the global seed.

    Returns:
      A fake operation seed based on global seed.
    r   )r  randint	_MAXINT32rf   s    r$   _internal_operation_seedz Context._internal_operation_seed  s     99Q	**r#   c                    g }g }t        j                  | j                        }	 d| _        d\  }}| j                  xs | j
                  }||j                  |j                  }}t        t        j                  |            D ]  }t        j                  ||      }|j                  t        j                  |             t        j                  j                  |      }	|	j                   dk(  r|	j#                  ddd      }	|j                  t%        |	j'                         |	j(                               t        j*                  ||      }
|
dk(  s|	j                   |k(  s|	j,                  |k(  s| xj                  dz  c_         	 || _        || _        t        j2                  |       y# || _        || _        t        j2                  |       w xY w)	zHelper to initialize devices.r   r   N	localhost)jobreplicataskr   r   GPUr   )r	   TFE_ContextListDevicesr   	_num_gpusr   r   job_name
task_indexrangeTF_DeviceListCountTF_DeviceListNamer   pydevcanonical_name
DeviceSpecfrom_stringr  replacer   	to_stringr   TF_DeviceListTyper  _logical_devicesr   TF_DeleteDeviceList)rT   logical_devicescontext_devicesdevice_listcurrent_jobcurrent_taskr  idev_namespecdev_types              r$   _initialize_logical_devicesz#Context._initialize_logical_devices  s    OO33D4H4HIK2dn",k<##Ft'F'Fj		$.$7$79N9N\Z22;?@ !//Q?u33H=>++H5 88{"$4@$t~~/T=M=MN	
 //Q?K'		\)
..A
.#( .d-d$$[1 .d-d$$[1s   D-F0 F0 #F0 3F0 0%Gc                 h   | j                   ry| j                  5  | j                   r
	 ddd       y| j                  J t        j                         }	 | j
                  j                         }t        j                  ||       | j                   t        j                  || j                         | j                   t        j                  || j                         | j                  t        k(  rt        j                  |d       | j                   t        j                   || j                         t        j"                  |d       t        j$                  || j&                         t        j(                  |      }t        j*                  |       | j,                  r| j.                  rJ d       | j,                  D| j,                  j                         }d}t        j0                  |t2        ||| j4                         n<| j.                  0| j.                  j                         }t        j6                  ||       || _        | j;                          d| _         | j<                  rt        j>                  | j8                         ddd       y# t        j*                  |       w xY w# 1 sw Y   yxY w)z5Initialize handle and devices if not already done so.NTzzCannot enable remote execution as well as collective ops at the moment. If this is important to you, please file an issue.r   ) r   r   r   r	   TFE_NewContextOptionsr   r~   TFE_ContextOptionsSetConfigr   *TFE_ContextOptionsSetDevicePlacementPolicyr   $TFE_ContextOptionsSetMirroringPolicyr   r   TFE_ContextOptionsSetAsyncr   TFE_ContextOptionsSetTfrt)TFE_ContextOptionsSetRunEagerOpAsFunction&TFE_ContextOptionsSetJitCompileRewriter   TFE_NewContextTFE_DeleteContextOptionsr   r   ,TFE_ContextSetServerDefWithTimeoutAndRetries_KEEP_ALIVE_SECSr  TFE_EnableCollectiveOpsr   r9  r  TFE_Py_SetCEagerContext)rT   opts
config_strcontext_handleserver_def_strtimeouts         r$   ensure_initializedzContext.ensure_initialized  sV   			 2A			2A 2A ""***--/d2[[224
..tZ@*

?
?D'' !!-

9
9D** !!U*

/
/d
;>>%

.
.tT^^
D<<T4H99$++	
 $2248++D1""t'F'F GG 
			%));;=??((	
 **688JJL**>>J+d
&&(d		 	 **4+?+?@e2A 2A4 	++D152A 2As*   J("J(D(J<D	J(J%%J((J1c                     | j                   5  | j                  s
	 ddd       yd| _        d| _        d| _        d| _        | j
                  rt        j                  d       d| _        ddd       y# 1 sw Y   yxY w)z7Uninitialize handle and devices if not already done so.NF)	r   r   r   r.  r   r  r	   rH  r   rf   s    r$   ensure_uninitializedzContext.ensure_uninitialized  sw    			 "" " #d"ddd		 	 **40!d" " "s   A1AA11A:c                 h    | j                   rt        j                  | j                         d| _        y r!   )r   r	   rH  r   r  rf   s    r$   mark_as_global_contextzContext.mark_as_global_context  s)     (()=)=>"Dr#   c                     | j                         j                          | j                         j                          t        j                          y r0   )r   rg   r   r	   TFE_ClearScalarCacherf   s    r$   _clear_cacheszContext._clear_caches  s8      "##%r#   c                     | j                   S r0   )r   rf   s    r$   get_server_defzContext.get_server_def  s    r#   c                    |st        d      || _        | j                  rA|j                         }t	        j
                  | j                  ||       | j                          | j                          t        j                          y)a  Allow setting a server_def on the context.

    When a server def is replaced, it effectively clears a bunch of caches
    within the context. If you attempt to use a tensor object that was pointing
    to a tensor on the remote device, it will raise an error.

    Args:
      server_def: A tensorflow::ServerDef proto. Enables execution on remote
        devices.
      keep_alive_secs: Num. seconds after which the remote end will hang up. As
        long as the client is still alive, the server state for the context will
        be kept alive. If the client is killed (or there is some failure), the
        server will clean up its context keep_alive_secs after the final RPC it
        receives.

    Raises:
      ValueError: if server_def is None.
    server_def is None.N)
r   r   r   r~   r	   TFE_ContextSetServerDefr9  rU  _device_parsing_cachere   rT   r  keep_alive_secsrL  s       r$   set_server_defzContext.set_server_def  sw    & ,--!D!335n((


 &&( 	 !r#   c                     |st        d      || _        | j                  rA|j                         }t	        j
                  | j                  ||       | j                          | j                          y)a  Update a server_def on the context.

    Args:
      server_def: A tensorflow::ServerDef proto. Enables execution on remote
        devices.
      keep_alive_secs: Num. seconds after which the remote end will hang up. As
        long as the client is still alive, the server state for the context will
        be kept alive. If the client is killed (or there is some failure), the
        server will clean up its context keep_alive_secs after the final RPC it
        receives.

    Raises:
      ValueError: if server_def is None.
    rY  N)r   r   r   r~   r	   TFE_ContextUpdateServerDefr9  rU  r\  s       r$   update_server_defzContext.update_server_def6  sg     ,--!D!335n++


 &&(r#   c                 p    | j                   r t        j                  | j                   |      S t        d      )a\  Checks whether a remote worker is alive or not.

    Args:
      worker_name: a string representing the remote worker. It must be a fully
        specified name like "/job:worker/replica:0/task:0".

    Returns:
      a boolean indicating whether the remote worker is alive or not.

    Raises:
      ValueError: if context is not initialized.
    Context is not initialized.)r   r	   TFE_ContextCheckAliver   )rT   worker_names     r$   check_alivezContext.check_aliveS  s2     --d.B.BKPP455r#   c                 p    | j                   r t        j                  | j                          yt        d      )a  Sync both local executors and the ones on remote workers.

    In async execution mode, local function calls can return before the
    corresponding remote op/function execution requests are completed. Calling
    this method creates a synchronization barrier for remote executors. It only
    returns when all remote pending nodes are finished, potentially with errors
    if any remote executors are in error state.

    Raises:
      ValueError: if context is not initialized.
    rc  N)r   r	   TFE_ContextSyncExecutorsr   rf   s    r$   sync_executorszContext.sync_executorsf  s-     ))$*>*>?455r#   c                 p    | j                   r t        j                  | j                          yt        d      )a  Clear errors in both local executors and remote workers.

    After receiving errors from remote workers, additional requests on the fly
    could further taint the status on the remote workers due to the async nature
    of remote execution. Calling this method block on waiting for all pending
    nodes in remote executors to finish and clear their error statuses.

    Raises:
      ValueError: if context is not initialized.
    rc  N)r   r	   TFE_ContextClearExecutorsr   rf   s    r$   clear_executor_errorszContext.clear_executor_errorsw  s-     **4+?+?@455r#   Tc	                    | j                   rt        j                  d       t        j                         }	||	_        |rt        j                  |      |	_        ||	_	        ||	_
        ||	_        ||	_        ||	_        |At        |t              r#|	j                   j#                  |       |	| _        yt%        d|      |	| _        y)z?Enable distributed coordination service with specified configs.zfConfiguring coordination service type may not be effective because the context is already initialized.NzB`coordinated_jobs` must be list[CoordinatedJob] or None, but got: )r   r   warningr   CoordinationServiceConfigservice_typer'  r(  service_leaderenable_health_checkcluster_register_timeout_in_msheartbeat_timeout_in_msshutdown_barrier_timeout_in_ms"allow_new_incarnation_to_reconnectr|   listcoordinated_job_listextendr   r   )
rT   rp  rq  rr  rs  rt  ru  coordinated_jobsrv  r   s
             r$   configure_coordination_servicez&Context.configure_coordination_service  s     ooB %>>@F&F#22>Bf!4F,JF)%<F",JF)* - #	$d	+##**+;< )/D%	 #36
 	
 )/D%r#   c                     | j                   S r0   )r   rf   s    r$   coordination_servicezContext.coordination_service  s    ,,,r#   c                 Z    t                t        j                  | j                  ||       y r0   )rN  r	   TFE_InsertConfigKeyValuer   r]   s      r$   set_config_key_valuezContext.set_config_key_value  s     ''(<(<c5Ir#   c                     t                t        j                         5 }t        j                  | j
                  |||       t        j                  |      j                  d      }d d d        |S # 1 sw Y   S xY wNutf-8)	rN  r   	tf_bufferr	   TFE_GetConfigKeyValuer   r   TF_GetBufferdecode)rT   r^   timeout_in_msbuffer_r_   s        r$   get_config_key_valuezContext.get_config_key_value  sq    				 F7&&


]G  ,,W5<<WEe	F
 LF
 Ls   AA00A:c                 X    t                t        j                  | j                  |       y r0   )rN  r	   TFE_DeleteConfigKeyValuer   rc   s     r$   delete_config_key_valuezContext.delete_config_key_value  s    ''(<(<cBr#   c                 t    | j                   r"t        j                  | j                   ||       yt        d      )zReport error to other members in a multi-client cluster.

    Args:
      error_code: a `tf.errors` error code.
      error_message: a string. The error message.
    rc  N)r   r	   TFE_ReportErrorToClusterr   )rT   
error_codeerror_messages      r$   report_error_to_clusterzContext.report_error_to_cluster  s6     ))



M 455r#   c                     | j                   r,t        | \  }}t        j                  | j                   ||      S t	        d      )zGet task states from the Coordination Service.

    Args:
      job_configs: A list of tuples of job name and task number.

    Returns:
      A list of TF_Status.
    rc  )r   zipr	   TFE_GetTaskStatesr   )rT   job_configs	job_names	task_numss       r$   get_task_stateszContext.get_task_states  sJ      +.i))


	9  455r#   c                 Z    t                t        j                  | j                  ||       y)a  Blocks until all coordinated tasks are at the barrier.

    The barrier may fail if it times out or if one of the tasks is unhealthy.

    Args:
      barrier_id: Unique string identifying the barrier.
      timeout_in_ms: Duration before the barrier times out and fails.
    N)rN  r	   TFE_WaitAtBarrierr   )rT   
barrier_idr  s      r$   wait_at_barrierzContext.wait_at_barrier  s%       j-r#   c                 \    | j                    t        j                  | j                          yy)z2Clear kernel cache and reset all stateful kernels.N)r   r	   r  rf   s    r$   clear_kernel_cachezContext.clear_kernel_cache  s'    '''(<(<= (r#   c                    |st        d      || _        | j                  t        j                  d       | j
                  5  | j                  sJ | j                  j                         }t        j                  | j                  |       | j                          | j                          ddd       yy# 1 sw Y   yxY w)a*  Enable distributed collective ops with an appropriate server_def.

    Args:
      server_def: A tensorflow::ServerDef proto. Enables execution on remote
        devices.

    Raises:
      ValueError: if server_def is None.
      RuntimeError: if this method is not called at program startup.
    rY  NzhEnabling collective ops after program startup may cause error when accessing previously created tensors.)r   r   r   r   rn  r   r   r~   r	   rG  r9  rU  )rT   r  rL  s      r$   enable_collective_opszContext.enable_collective_ops  s     ,--&0D# 'oo=        88JJL**4+?+?P((*  (
 s   A)B55B>c                    | j                   H| j                   |k7  s-| j                  |k7  s| j                  |k7  s| j                  |k7  rt	        d      y| j
                  t        d      || _         || _        || _        || _        y)a  Configure collective ops.

      Collective group leader is necessary for collective ops to run, other
      configurations are mainly for the purpose of performance.

    Args:
      collective_leader: a device string for collective leader, e.g.
        "/job:worker/replica:0/task:0"; empty string means local execution of
        collective ops.
      scoped_allocator_enabled_ops: a tuple or a list of op names for scoped
        allocator to run with.
      use_nccl_communication: whether to use nccl communication for collective
        ops.
      device_filters: a tuple or a list of device strings. If set, corresponding
        task can only see the devices filtered by these device filters.

    Raises:
      RuntimeError: if this method is not called at program startup.
    Nz&Collective ops are already configured.z4Collective ops must be configured at program startup)r   r   r   r   r   r   RuntimeError)rT   collective_leaderscoped_allocator_enabled_opsuse_nccl_communicationdevice_filterss        r$   configure_collective_opsz Context.configure_collective_ops  s    4 *

!
!%6
6::)*448NN,,>ABB'OPP/D4PD1.DD+&4D#r#   c                 f    | j                          t        j                  | j                  ||       y)a  Abort the collective ops.

    This is intended to be used when a peer failure is detected, which allows
    the user to handle the case instead of hanging. This aborts all on-going
    collectives. After all subsequent collectives error immediately, and you
    need to reset_context() to use collectives again.

    Args:
      code: a `tf.errors` error code.
      message: a string. The error message.
    N)rN  r	   TFE_AbortCollectiveOps_handle)rT   codemessages      r$   abort_collective_opszContext.abort_collective_opsB  s&     	%%dllD'Br#   c                 f    | j                          t        j                  | j                  ||       y)a  Check collective peer health.

    This probes each task to see if they're still alive. Note that restarted
    tasks are considered a different one, and they're considered not healthy.

    This should only be used in multi client multi worker training.

    Args:
      task: a task string, must be in the format of /job:xxx/replica:0/task:N.
      timeout_in_ms: an integer, the timeout. If zero, there's no timeout.

    Raises:
      tf.errors.UnavailableError: when a peer is down.
      tf.errors.FailedPreconditionError: when a peer is a different one from the
        one this task has talked to, e.g. the peer has restarted.
      tf.errors.InvalidArgumentError: when the task string is invalid.
    N)rN  r	    TFE_CollectiveOpsCheckPeerHealthr  )rT   r  r  s      r$    check_collective_ops_peer_healthz(Context.check_collective_ops_peer_healthQ  s)    $ 	//dMr#   c                 H    | j                   t        d      | j                   S Nz"Context must be initialized first.)r   AssertionErrorrf   s    r$   r  zContext._handleh  s&    #?@@r#   c                 H    | j                   t        d      | j                   S r  )r   r  rf   s    r$   _deviceszContext._deviceso  s&    $?@@   r#   c                     | j                   y| j                  }dt        |      z  g}t        |      D ]  \  }}|j	                  d||fz          dj                  |      S )Nz:Eager TensorFlow Context. Devices currently uninitialized.z(Eager TensorFlow Context with %d devicesz   Device %d: %s
)r   r  r[   	enumerater   join)rT   deviceslinesr5  ds        r$   __str__zContext.__str__v  sh    #Ig9S\JKeG$ 2$!Q'1a&012YYur#   c              #   l  K   | j                   }|j                  }|t        k(  |_        |t        k(  r!| j                  j	                  dt
        d       	 d ||_        |t        k(  r| j                  j                          yy# ||_        |t        k(  r| j                  j                          w w xY ww)z;A context manager to allow setting the mode to EAGER/GRAPH.FN)r   r   r   context_switchesr   r   r   )rT   modectxold_is_eagers       r$   _modezContext._mode  s      
!
!C<<L:%CLz   
D9$!cl		!!# 
 "cl		!!# 
s   AB4B ,B4-B11B4c                 .    | j                   j                  S )z;Returns True if current thread has eager executing enabled.)r   r   rf   s    r$   r   zContext.executing_eagerly  s    ""+++r#   c                 <    t         | j                     j                  S zPer-device cache for scalars.)r   r   r   rf   s    r$   r   zContext.ones_rank_cache  s    dhh'777r#   c                 <    t         | j                     j                  S r  )r   r   r   rf   s    r$   r   zContext.zeros_cache  s    dhh'333r#   c                 .    | j                   j                  S )z*Returns scope name for the current thread.r   
scope_namerf   s    r$   r  zContext.scope_name  s     ""---r#   c                 &    || j                   _        y)z'Sets scope name for the current thread.Nr  )rT   ss     r$   r  zContext.scope_name  s     *+D&r#   c                 .    | j                   j                  S )z/Returns the device name for the current thread.)r   device_namerf   s    r$   r  zContext.device_name       ""...r#   c                 .    | j                   j                  S )z/Returns the device spec for the current thread.)r   r   rf   s    r$   r   zContext.device_spec  r  r#   c                 H    || j                   _        || j                   _        y r0   )r   r  r   )rT   r  r   s      r$   _set_devicezContext._set_device  s    *5D'*5D'r#   c                     t        |t              r|j                  }n%t        j                  |      r|j                         }t        | |      S )ao  Context-manager to force placement of operations and Tensors on a device.

    Args:
      name: Name of the device or None to get default placement.

    Returns:
      Context manager that forces device placement.

    Raises:
      ValueError: If name is not a string or is an invalid device name.
      RuntimeError: If device scopes are not properly nested.
    )r|   r   r   r'  is_device_specr,  _EagerDeviceContextrT   r   s     r$   r   zContext.device  s>     $&YYd			d	#^^dtT**r#   c                     | j                   S )z=List of the names of devices available to execute operations.)r  rf   s    r$   r  zContext.devices  s    ==r#   c                    | j                          t        j                         5 }t        j                  | j
                  |       t        j                  |      j                  d      }d d d        |S # 1 sw Y   S xY wr  )	rN  r   r  r	   TFE_HostAddressSpacer   r   r  r  )rT   r  address_spaces      r$   host_address_spacezContext.host_address_space  sp    				 N7%%d&:&:GD'44W=DDWMmN N s   AA44A>c                 :    | j                         rt        S t        S )z'Gets execution mode for current thread.)is_asyncr   r   rf   s    r$   r  zContext.execution_mode  s     MMO5--r#   c                    |dt         t        fvrt        d|z        |t         }|t        k(  }| j                         |k7  r| j                  o| j
                  j                          t        j                  |      }|| j                  _        t        j                  | j                  |j                                y|| _        yy)z'Sets execution mode for current thread.Nz0Execution mode should be None/SYNC/ASYNC. Got %s)r   r   r   r  r   r   waitnew_executorr   r	   TFE_ContextSetExecutorForThreadhandler   )rT   r  enable_asyncexecutor_news       r$   r  zContext.execution_mode  s     D$&&
<t
C  |d5=L}},&				),,\:+7(22  ,"5"5"7	
 ". 'r#   c                 f    | j                   | j                  j                         S | j                  S r0   )r   r   r  r   rf   s    r$   r  zContext.is_async  s-    ']]##%%###r#   c                     | j                          t        j                  t        j                  | j
                              S r0   )rN  r   Executorr	   TFE_ContextGetExecutorForThreadr   rf   s    r$   r   zContext.executor  s5    2243G3GH r#   c                     | j                          t        j                  | j                  |j	                                y r0   )rN  r	   r  r   r  )rT   es     r$   r   zContext.executor  s*    ..t/C/CQXXZPr#   c                 
     j                          t        j                          j                  j	                   j                          j
                  Y j
                  rt        j                  j                  nt        j                  j                  j                  j                  _         j                   j                  _         j                   j                  _         j                    j                   _        n j%                         _         j&                   j&                  _         j*                   j*                  _        t/        j0                         }|j2                  _        |t        j                  j6                  j8                  k(  rdj2                  _         j<                   j<                  j2                  _         fd} fd} |d        |d        |d        |d        |d	        |d
        |d        |d        |d        |d        |d        |d        |d        |d        |d        |d        |d        |d        j@                  jC                  dd      }||j                  jD                  _#        djH                  d<   djH                  d<    jJ                  D ]z  }| jL                  vr jN                  jC                  |      }|"jH                  |jP                  xx   dz  cc<   QjH                  |jP                  xx   tS        |      z  cc<   |  jU                         }jV                  jY                  |        j[                         }j\                  jY                  |        j^                  r j^                  j2                  _0         jb                  rj                  jD                  }	td        jf                  jh                  |	_5        |	jl                  jn                  dd=  jb                  D ]'  }
|	jl                  jn                  jq                  |
       )  jr                  rdj2                  _:         jv                  r;jx                  dd=  jv                  D ]  }jx                  jq                  |         jz                  r/j2                  j|                  j	                   jz                         S )z7Return the ConfigProto with all runtime deltas applied.NTc                     j                   j                  | d       }|y t        j                  j                  | |rt
        j                  j                         y t
        j                  j                         y r0   )	r   rb   setattrgraph_optionsrewrite_optionsr   RewriterConfigONOFFoptiontoggler   rT   s     r$   rewriter_togglez'Context.config.<locals>.rewriter_toggle1  si    3377Ef	



.
.
  "0033	 '5599r#   c                     j                   j                  | d       }|y t        j                  j                  | |       y r0   )r   rb   r  r  r  r  s     r$   rewriter_boolz%Context.config.<locals>.rewriter_bool@  s;    3377Ef	f""22FFCr#   layout_optimizerconstant_foldingshape_optimization	remappingarithmetic_optimizationdependency_optimizationloop_optimizationfunction_optimizationdebug_stripperdisable_model_pruningscoped_allocator_optimizationpin_to_host_optimizationimplementation_selectorauto_mixed_precisionuse_plugin_optimizersdisable_meta_optimizer$auto_mixed_precision_onednn_bfloat16auto_mixed_precision_mklmin_graph_nodesr   CPUr  r   )?_initialize_physical_devicesr   r}   r   CopyFromr   OptimizerOptionsON_1r  r  optimizer_optionsglobal_jit_levelr   intra_op_parallelism_threadsr   inter_op_parallelism_threadsr   allow_soft_placementr   r   log_device_placementr   operation_timeout_in_msr	   TF_IsMlirBridgeEnabledexperimentalmlir_bridge_rolloutExperimentalMLIR_BRIDGE_ROLLOUT_ENABLEDenable_mlir_bridger   enable_mlir_graph_optimizationr   rb   r  r  device_countr   r   r   r   r[   _compute_gpu_optionsgpu_options	MergeFrom!_compute_pluggable_device_optionspluggable_device_optionsr   collective_group_leaderr   r   r  r  r  scoped_allocator_opts	enable_opr   r   collective_ncclr   r  r   coordination_config)rT   is_mlir_bridge_enabledr  r  nodesdevvirtual_devicesr"  r%  r  opfr   s   `           @r$   r   zContext.config  st    	%%'##%F||oodll#&    
%
%
*
***.. ,,=
 ))5,0,N,Nf)))5,0,N,Nf)"".$($?$?f!$($:$:$<f!!!-$($>$>f!$$0'+'D'Df$'>>@.DF+!!..JJ	K 04f,++7

.
. 8D &'&'()K -.-.'(+,$%)*34./-.*++,*+:;./00445FME=Bf**: "#F!"F%% E	D--	-0044S9o		 COO,1,COO,O0DD,E ++-K
  -  $EEG
##--.FG 484K4Kf144,,<<o

,
,
/
/ 3 
/
/
9
9!
<== C"--77>>rBC..,0f)&&



".. (!$$Q'( ((--66

+
+ Mr#   c                 B   |dvrt        d      g }g }t        j                  t              }t	               }|dk(  r| j                  d      n| j                  }|dk(  }|D ]  }||j                     }	||j                  xx   dz  cc<   || j                  vr9| j                  |   }
|j                  |
       |dk(  r+|j                  |j                  dz   t        |	      z          n|j                  t        |	             |s| j                  s| j                  j                  |g       }g }g }g }|D ]l  }|j                  |j                  |j                         |j                  |j                          |j"                  R|j                  |j"                         n |r"t%        |      t%        |      k7  rt        d      |r"t%        |      t%        |      k7  rt        d	      |j                  t&        j(                  j*                  j-                  |||
              |s,|r*t%        |      dkD  rt        d      |j/                         }nd}t'        j(                  |dj1                  |      t&        j(                  j+                  |            S )z6Build the GPUOptions proto for GPU or PluggableDevice.)r  PluggableDevicezBdevice types other than GPU and PluggableDevice are not supported.r  r   r2  :Nz2priority must be specified for all virtual devicesz9device_ordinals must be specified for all virtual devices)memory_limit_mbprioritydevice_ordinalz/Memory growth cannot differ between GPU devices,)r.  )allow_growthvisible_device_listr  )r   rR   defaultdictr  setlist_physical_devicesr   r   r   r   addr   r   r   rb   r   r   r   r[   r   
GPUOptionsr  VirtualDevicesr   r  )rT   r   r9  r.  device_to_indicesmemory_growthscompatible_devicessupport_virtual_devicesr-  device_indexgrowthvdevsdevice_ordinalsdevice_limitsr5  virt_devr8  s                    r$   _compute_device_optionszContext._compute_device_options  s   44
N  O#//4UN % 	""5)$$ 
 *U2! *
&s7l(A-(	D--	-&&s+f 	)	)""3??S#83|;L#LM""3|#45	 T%=%=((,,S"5 	<H11=""8#G#GH


x44
5++7OOH::;	< M*c(m;OP
P s=1S5III  	!!..== -!. > 	
I*
\ ~	^	q	 JKK#'')ll  !HH%89**77+ 8 
 r#   c                 &    | j                  d      S )z#Build the GPUOptions proto for GPU.r  r   rJ  rf   s    r$   r!  zContext._compute_gpu_options  s    ''E'::r#   c                 &    | j                  d      S )z/Build the GPUOptions proto for PluggableDevice.r2  rL  rM  rf   s    r$   r$  z)Context._compute_pluggable_device_options  s    ''4E'FFr#   c                     | j                   j                  :| j                  }| j                  d|_        t        |      | j                   _        | j                   j                  S )zReturns function call options for current thread.

    Note that the returned object is still referenced by the eager context.

    Returns: the FunctionCallOptions for current thread.
    T)ru   )r   function_call_optionsr   r   r  ro   r   s     r$   rP  zContext.function_call_options  s_     44<{{f 
	$	$	,&*#6I7d3 ""888r#   c                 &    || j                   _        y)z1Returns function call options for current thread.N)r   rP  rT   optionss     r$   rP  zContext.function_call_options  s     5<D1r#   c                 :    | j                          | j                  S )z3The number of GPUs available to execute operations.)rN  r!  rf   s    r$   num_gpuszContext.num_gpus  s    >>r#   c                 d    | j                          t        j                  | j                  |       y)zAdd a C API TF_Function to the context.

    Once added, the function (identified by its name) can be executed like any
    other operation.

    Args:
      c_func: A wrapped TF_Function (returned from TF_GraphToFunction_wrapper).
    N)rN  r	   TFE_ContextAddFunctionr  )rT   c_funcs     r$   add_c_functionzContext.add_c_function   s$     	%%dllF;r#   c                     | j                          t        j                  t        j                  | j
                  |      |      S )zGet a C API TF_Function from the context.

    Args:
      name: Name of the function to get.

    Returns:
      A ScopedTFFunction wrapping the C API TF_Function.
    )rN  r   ScopedTFFunctionr	   TFE_ContextGetFunctionr  r  s     r$   get_c_functionzContext.get_c_function  s9     	&&))$,,=t r#   c                     | j                          t        r;|j                         }t        j                  | j
                  |t        |             yt        j                  | j
                  |       y)zAdd a function definition to the context.

    Once added, the function (identified by its name) can be executed like any
    other operation.

    Args:
      fdef: A FunctionDef protocol buffer message.
    N)rN  is_ossr~   r	   TFE_ContextAddFunctionDefr  r[   (TFE_ContextAddFunctionDefNoSerialization)rT   fdeffdef_strings      r$   add_function_defzContext.add_function_def  sU     	**,k**
,,S%5 99$,,Mr#   c                 ^   t         rzt        j                         5 }t        j                  | j
                  ||       t        j                  |      }ddd       t        j                         }|j                         |S t        j                  | j
                  |      }|S # 1 sw Y   RxY w)zGet a function definition from the context.

    Args:
      name: function signature name.

    Returns:
      The requested FunctionDef.

    Raises:
      tf.errors.NotFoundError: if name is not the name of a registered function.
    N)r_  r   r  r	   TFE_ContextGetFunctionDefr  r   r  r   FunctionDefParseFromString(TFE_ContextGetFunctionDefNoSerialization)rT   r   r  
proto_datafunction_defs        r$   get_function_defzContext.get_function_def,  s     ! =W,,T\\4I&33G<
= "--/l"":.
   HH
,,l = =s   7B##B,c                    t        j                         5 }t        j                  | j                  ||       t        j                  |      }ddd       t        j                         }|j                         |S # 1 sw Y   0xY w)a  Get GraphDebugInfo associated with a function from the context.

    Args:
      name: function signature name.

    Returns:
      The requested GraphDebugInfo.

    Raises:
      tf.errors.NotFoundError: if name is not the name of a registered function.
    N)
r   r  r	   TFE_ContextGetGraphDebugInfor  r   r  r   GraphDebugInforh  )rT   r   r  rj  graph_debug_infos        r$   get_graph_debug_infozContext.get_graph_debug_infoD  sr     
			 ;7--dllD'J$11':j; ,::<$$Z0; ;s   7A;;Bc                 b    | j                          t        j                  | j                  |      S )z6Calls TFE_IsCustomDevice. See the non-member function.)rN  r	   TFE_Py_IsCustomDevicer  )rT   r  s     r$   is_custom_devicezContext.is_custom_deviceX  s%    ++DLL+FFr#   c                 h    | j                          t        j                  | j                  |||       y)z<Calls TFE_RegisterCustomDevice. See the non-member function.N)rN  r	   TFE_Py_RegisterCustomDevicer  )rT   device_capsuler  device_info_capsules       r$   register_custom_devicezContext.register_custom_device]  s,     	**nk3Fr#   c                 b    | j                          t        j                  | j                  |      S )zPack multiple `EagerTensor`s of the same dtype and shape.

    Args:
      tensors: a list of EagerTensors to pack.

    Returns:
      A packed EagerTensor.
    )rN  r	   TFE_Py_PackEagerTensorsr  )rT   tensorss     r$   pack_eager_tensorszContext.pack_eager_tensorsf  s'     	--dllGDDr#   c                 r    | j                          t        t        j                  | j                              S )zGet a list of names of registered functions.

    Returns:
      A set of names of all registered functions for the context.
    )rN  r;  r	   TFE_ContextListFunctionNamesr  rf   s    r$   list_function_nameszContext.list_function_namesr  s*     	z66t||DEEr#   c                 d    | j                          t        j                  | j                  |       y)zRemove a function from the context.

    Once removed, the function cannot be executed anymore.

    Args:
      name: function signature name.
    N)rN  r	   TFE_ContextRemoveFunctionr  r  s     r$   remove_functionzContext.remove_function{  s$     	((t<r#   c                 t    | j                          t        t        j                  | j                  |            S )z)Check if a function `name` is registered.)rN  boolr	   TFE_ContextHasFunctionr  r  s     r$   has_functionzContext.has_function  s*    
11$,,EFFr#   c                 ,    t        | j                        S )z=Returns an id that is unique to each scope holding functions.)idr   rf   s    r$   function_scope_idzContext.function_scope_id  s     d""##r#   c                 h   t        t        j                  | j                  j	                         j                                }t        j                         }|*t        j                  |j                  d      ||||       }n*t        j                  |j                  d      |||| |      }|xs d}|S )z2Calls the function associated with the given name.Nr  )num_outputsinputsattrsr  )r  r  r  r  cancellation_manager)tuple	itertoolschainrP  r   itemsr   contextr   r  execute_with_cancellation)rT   r   tensor_inputsr  r  cancellation_contextoutputss          r$   call_functionzContext.call_function  s    33<<>DDFGE (//1#
++g
!g 11
++g
!3g oGNr#   c                     || j                   j                  vr&| j                   j                  j                  |       yy)al  Add a post-op callback to the context.

    A post-op callback is invoked immediately after an eager operation or
    function has finished execution or after a op has been added to a graph,
    providing access to the op's type, name input and output tensors. Multiple
    op callbacks can be added, in which case the callbacks will be invoked in
    the order in which they are added.

    Args:
      callback: a callable of the signature `f(op_type, inputs, attrs, outputs,
        op_name=None, graph=None)`. See doc strings in `op_callbacks.py` for
        details on the function signature and its semantics.
    N)r   op_callbacksr   rT   callbacks     r$   add_op_callbackzContext.add_op_callback  s7     t..;;;
**11(; <r#   c                     || j                   j                  vrt        d      | j                   j                  | j                   j                  j                  |      = y)zRemove an already-registered op callback.

    Args:
      callback: The op callback to be removed.

    Raises:
      KeyError: If `callback` is not already registered.
    zOThe specified op callback has not been registered, and hence cannot be removed.N)r   r  KeyErrorindexr  s     r$   remove_op_callbackzContext.remove_op_callback  sZ     t..;;;)  	,,,,228<	r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   r  zContext.op_callbacks  s    ""///r#   c                 .    | j                   j                  S r0   r   invoking_op_callbacksrf   s    r$   r  zContext.invoking_op_callbacks  s    ""888r#   c                 &    || j                   _        y r0   r  )rT   r_   s     r$   r  zContext.invoking_op_callbacks  s    49D1r#   c           
      R   | j                   5  |s| j                  
	 ddd       yt        j                         }|D cg c]=  }t	        |j                         |j                         j                  d      d         ? c}| _        t        | j                        D ci c]  \  }}||
 c}}| _        t        j                         }|D cg c]=  }t	        |j                         |j                         j                  d      d         ? c}| _
        t        | j                        | _        | j                  D ci c]"  }|j                  dk(  s|| j                  v r|d$ c}| _        ddd       | j                          yc c}w c c}}w c c}w c c}w # 1 sw Y   /xY w)zGets local devices visible to the system.

    Args:
      reinitialize: If True, reinitializes self._physical_devices  so that
        dynamic registered devices will also be visible to the python front-end.
    Nr3  r   r  r  )r   r   r	   TF_ListPhysicalDevicesr   r  splitr  r   TF_ListPluggablePhysicalDevicesr   rw  r   r   r   _import_config)rT   reinitializedevsr  r5  ppluggable_devss          r$   r  z$Context._initialize_physical_devices  s    
		 d44@  ..0d   ahhjahhj6F6Fs6KA6N
O d
 %T%;%;<(1a!Q$(d$
 "AACn "! ahhjahhj6F6Fs6KA6N
O!d
 #'t'='=">d ))!]]e#qD,C,C'C T'!d+8 	/ (!!+ sG   FFAF  F F-!FAF0F 'F'FFF&c                 &    | j                  d       y)z)Gets local devices visible to the system.TN)r  rf   s    r$   reinitialize_physical_devicesz%Context.reinitialize_physical_devices  s     	%%d+r#   c                     | j                          |t        | j                        S | j                  D cg c]  }|j                  |k(  s| c}S c c}w )ae  List local devices visible to the system.

    This API allows a client to query the devices before they have been
    initialized by the eager runtime. Additionally a user can filter by device
    type, to get only CPUs or GPUs.

    Args:
      device_type: Optional device type to limit results to

    Returns:
      List of PhysicalDevice objects.
    )r  rw  r   r   rT   r   r  s      r$   r<  zContext.list_physical_devices	  sL     	%%'$(())--N!+1MANNN   AAc                    t        |t              st        d|      | j                  || j                  vrt        d|      | j                  |   }t	        j
                  |      }d|v r3	 |d   j                  d      \  }}t        |      t        |      f|d<   |S |S # t        $ r}t        d|d   z        |d}~ww xY w)zReturns details about a physical devices.

    Args:
      device: A `tf.config.PhysicalDevice` returned by
        `tf.config.list_physical_devices` or `tf.config.get_visible_devices`.

    Returns:
      A dict with string keys.
    z4device must be a tf.config.PhysicalDevice, but got: NzaThe PhysicalDevice must be one obtained from calling `tf.config.list_physical_devices`, but got: compute_capability.z;Device returned compute capability an in invalid format: %s)	r|   r   r   r   r	   TF_GetDeviceDetailsr  r  r  )rT   r   r  detailsmajorminorexcs          r$   get_device_detailszContext.get_device_details  s     fn-FL
N  	&&.777  
 **62E,,U3G w&34::3?u),USZ(@$% N7N  I*+,
 	s   00B$ $	C-B??Cc                 Z   | j                   y| j                   j                  j                  dd      }|dk7  rz| j                  D cg c]  }|j                  dk(  s| }}|dk(  r| j                  g d       n9|dkD  r4| j                  |d   t        |      D cg c]  }t                c}       | j                  D cg c]  }|j                  dk(  s| }}|sy| j                   j                  j                  dd      }g }| j                   j                  j                  }|rY|j                  d      D ]D  }	t        |	      t        |      k\  rt        d|	z        |j                  |t        |	                F n|}||d| }| j                  |d       yc c}w c c}w c c}w )a%  Import config if passed in during construction.

    If Context was created with a ConfigProto such as when calling
    tf.compat.v1.enable_eager_execution(), then we need to pull out the
    various pieces we might be replacing and import then into our internal
    class representation.
    Nr  r   r   r  r7  z Invalid visible device index: %s)r   r   rb   r   r   set_visible_devices set_logical_device_configurationr$  r   r"  r9  r  r  r[   r   r   )
rT   num_cpusr  cpus_gpus	gpu_countvisible_gpusvisible_indicesr  s
             r$   r  zContext._import_configC  s    ||||((,,UA6H1}//JA1==E3IaJdJ	Q  U+a<--GE(OLq02L	

 --H!%1GAHDH ))--eT:ILll..BBO"((- .%u:T"=EF
FDU,-.
 l!*9-l\51A K
 M Is   FFF#
F(F(c                     | j                          |t        | j                        S | j                  D cg c]  }|j                  |k(  s| c}S c c}w )zReturn logical devices.)rN  rw  r.  r   r  s      r$   list_logical_deviceszContext.list_logical_devicesr  sJ    $''((,,M!0LAMMMr  c                     | j                          |t        | j                        S | j                  D cg c]  }|j                  |k(  s| c}S c c}w )z Get the list of visible devices.)r  rw  r   r   r  s      r$   get_visible_deviceszContext.get_visible_devicesz  sU    %%'$++,, ,,0L  r  c                    | j                          t        |t              s|g}|D ]P  }|| j                  vrt	        dt        |      z        |+|j                  |k7  s;t	        dt        |      z         g }|)| j                  D cg c]  }|j                  |k7  s| }}||z  }| j                  |k(  ry| j                  t        d      || _        yc c}w )z Set the list of visible devices.Unrecognized device: %sNz:Visible devices cannot be modified after being initialized)
r  r|   rw  r   r   reprr   r   r   r  )rT   r  r   r  r9  s        r$   r  zContext.set_visible_devices  s    %%'gt$	g >	
$((	(2T!W<==		 Q]]k%A2T!W<==	> ..!--;2N!  7"  $77'
F  !4Ds   C Cc                     | j                          | j                          t        j                  | j                  |      S )z-Returns a dict of memory info for the device.)r  rN  r	   TFE_GetMemoryInfor   rT   r-  s     r$   get_memory_infozContext.get_memory_info  s3    %%'''(<(<cBBr#   c                     | j                          | j                          t        j                  | j                  |       y)z/Resets the tracked memory stats for the device.N)r  rN  r	   TFE_ResetMemoryStatsr   r  s     r$   reset_memory_statszContext.reset_memory_stats  s0    %%'##D$8$8#>r#   c                     | j                          || j                  vrt        dt        |      z        | j                  |   S )z5Get if memory growth is enabled for a PhysicalDevice.r  )r  r   r   r  r   r  s     r$   get_memory_growthzContext.get_memory_growth  sB    %%'
$(((049<==""3''r#   c                 z   | j                          || j                  vrt        dt        |      z        || j                  v rt        d      |j
                  dk7  r|| j                  vrt        d      | j                  j                  |      |k(  ry| j                  t        d      || j                  |<   y)z<Set if memory growth should be enabled for a PhysicalDevice.r  zBCannot set memory growth on device when virtual devices configuredr  z=Cannot set memory growth on non-GPU and non-Pluggable devicesNz;Physical devices cannot be modified after being initialized)r  r   r   r  r   r   r   r   rb   r   r  )rT   r-  enables      r$   set_memory_growthzContext.set_memory_growth  s    %%'
$(((049<==
d&&&
N  %Ct/F/F$F
I  ""3'61'
G  $*DC r#   c                     | j                          || j                  vrt        dt        |      z        | j                  j                  |      S )z:Get the virtual device configuration for a PhysicalDevice.r  )r  r   r   r  r   rb   r  s     r$    get_logical_device_configurationz(Context.get_logical_device_configuration  sH    %%'
$(((049<==##'',,r#   c                 <   | j                          || j                  vrt        dt        |      z        |j                  dk(  rM|D ]G  }|j
                  t        d      |j                  t        d      |j                  >t        d       nF|j                  dk(  r|D ]  }|j
                  t        d       nt        d	|j                  z        | j                  j                  |      |k(  ry| j                  t        d
      || j                  |<   y)z:Set the virtual device configuration for a PhysicalDevice.r  r  NzFSetting memory limit on CPU virtual devices is currently not supportedzPSetting experimental_priority on CPU virtual  devices is currently not supportedzVSetting experimental_device_ordinal on CPU virtual  devices is currently not supportedr  z8Setting memory limit is required for GPU virtual devicesz(Virtual devices are not supported for %sz:Virtual devices cannot be modified after being initialized)r  r   r   r  r   r   r   r   r   rb   r   r  )rT   r-  r.  vdevs       r$   r  z(Context.set_logical_device_configuration  sJ   %%'
$(((049<==
%! $((  %%14  ++74   
E	!! $$H  
4s
F  ##C(O;'
F  %4DS!r#   c                    | j                   xs | j                  }dg}|*|j                  d|j                  |j                  fz         | j                  d      D cg c](  }|j                  j                  t        |            r|* }}| j                          t        |      dkD  rt        d      t        j                  | j                  ||       | j                          yc c}w )a  Set virtual CPU devices in context.

    If virtual CPU devices are already configured at context initialization
    by tf.config.set_logical_device_configuration(), this method should not be
    called.

    Args:
      num_cpus: Number of virtual CPUs.
      prefix: Device name prefix.

    Raises:
     RuntimeError: If virtual CPUs are already configured at context
     initialization.
    z/deviceNz/job:%s/replica:0/task:%dr  r   z.Virtual CPUs already set, cannot modify again.)r   r   r   r"  r#  r  r   
startswithr  rN  r[   r  r	   TFE_SetLogicalCpuDevicesr   r9  )rT   r  prefixr  local_prefixr  logical_local_devicess          r$   set_logical_cpu_deviceszContext.set_logical_cpu_devices
  s     !!DT%D%DJ;L
%  *"7"789 **5166U<01 	
 
 	
 !A%IJJ''(<(<hO$$&s   -C'c           	      L    t        j                  | j                  ||||||      S )a  Get the compiler IR bytes.

    Args:
      device_name: The name of the device with the form as
        "/job:localhost/replica:0/task:0/device:CPU:0", "/device:TPU:0" etc.
        When this is used, actual device is needed for getting the compiler IR.
      platform_name: The name of the platform, e.g. "TPU". When this is used,
        first we find a device whose name contains the platform, if it is found
        we get the compiler IR by device. Otherwise the compiler IR is obtained
        as if using that device. The former logic of falling back to device is
        necessary, as there are cases of TF variables that need to access
        devices, but the upper layer may generally choose platform for getting
        compiler IR in a device-agnostic way.
      function_name: The name of the function to get the compiler IR.
      flat_args: The flat argument inputs.
      captured_inputs: The inputs that are captured.
      stage: The exported stage for the given function.

    Returns:
      The compiler IR bytes.
    )r	   TF_GetCompilerIrr   )rT   r  platform_namefunction_name	flat_argscaptured_inputsstages          r$   get_compiler_irzContext.get_compiler_ir-  s2    < && r#   z*XLA:CPU and XLA:GPU devices are deprecated)	warn_oncec                 ,    t        j                          y)z1Enables XLA:CPU and XLA:GPU devices registration.N)r	   TF_EnableXlaDevicesrf   s    r$   enable_xla_deviceszContext.enable_xla_devicesU  s    
 ""$r#   c                 *    t        j                         S r0   )r	   r  rf   s    r$   r  zContext.enable_mlir_bridge\  s    ,,..r#   c                     | j                   S r0   )r   rf   s    r$   r  z&Context.enable_mlir_graph_optimization`  s    ///r#   c                 P    t        j                  |       d | j                  _        y r0   )r	   TF_EnableMlirBridger   rP  rT   enableds     r$   r  zContext.enable_mlir_bridged  s    ""7+48D1r#   c                 4    || _         d | j                  _        y r0   )r   r   rP  r  s     r$   r  z&Context.enable_mlir_graph_optimizationi  s    +2D(48D1r#   c                     | j                   j                  j                  j                  }|t        j
                  j                  k(  xs |t        j
                  j                  k(  S r0   )r   r  r  r  r   r  r  ON_2)rT   levels     r$   optimizer_jitzContext.optimizer_jitn  sO    KK%%77HHE,,111 	5J//444r#   c                 4    || _         d | j                  _        y r0   )r   r   rP  r  s     r$   r  zContext.optimizer_jitv  s    !D48D1r#   c                    | j                   j                  j                  i fd}fd} |d        |d        |d        |d        |d        |d        |d	        |d
        |d        |d        |d        |d        |d        |d        |d        |d        |d        |d       j                  dk7  rj                  d<   S )zhGet experimental options for the optimizer.

    Returns:
      Dictionary of current option values
    c                 j    t        |       }|dk7  r!|t        j                  j                  k(  | <   y y r   )getattrr   r  r  )r  attrrS  r  s     r$   r  zCContext.get_optimizer_experimental_options.<locals>.rewriter_toggle  s6    _f-d	"5"D"D"G"GG 
r#   c                 $    t        |       | <   y r0   )r  )r  rS  r  s    r$   r  zAContext.get_optimizer_experimental_options.<locals>.rewriter_bool  s    8gfor#   r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r	  r
  r  r   r  )r   r  r  r  )rT   r  r  rS  r  s      @@r$   "get_optimizer_experimental_optionsz*Context.get_optimizer_experimental_options|  s     kk//??OGH
9 &'&'()K -.-.'(+,$%)*34./-.*++,*+:;./&&!+#2#B#Bg Nr#   c                 \    | j                   j                  |       d| j                  _        y)zjSet experimental options for the optimizer.

    Args:
      options: Dictionary of options to modify
    N)r   updater   rP  rR  s     r$   "set_optimizer_experimental_optionsz*Context.set_optimizer_experimental_options  s&     	((//848D1r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   r  z$Context.intra_op_parallelism_threads      ;;333r#   c                 `    | j                   |k(  ry | j                  t        d      || _         y )Nz=Intra op parallelism cannot be modified after initialization.)r   r   r  rT   num_threadss     r$   r  z$Context.intra_op_parallelism_threads  9    ))[8'
I  *5D&r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   r  z$Context.inter_op_parallelism_threads  r	  r#   c                 `    | j                   |k(  ry | j                  t        d      || _         y )Nz=Inter op parallelism cannot be modified after initialization.)r   r   r  r  s     r$   r  z$Context.inter_op_parallelism_threads  r  r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   soft_device_placementzContext.soft_device_placement      ;;+++r#   c                     | j                    t        j                  | j                  |       || _        d | j
                  _        y r0   )r   r	   !TFE_ContextSetSoftDevicePlacementr  r   r   rP  rT   r  s     r$   r  zContext.soft_device_placement  s8    '224<<H"(D48D1r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   r  zContext.log_device_placement  r  r#   c                     | j                    t        j                  | j                  |       || _        d | j
                  _        y r0   )r   r	    TFE_ContextSetLogDevicePlacementr  r   r   rP  r  s     r$   r  zContext.log_device_placement  s8    '11$,,G!'D48D1r#   c                     | j                   S r0   )r   rf   s    r$   r*   zContext.jit_compile_rewrite  s    $$$r#   c                 j    | j                    t        j                  | j                  |       || _        y r0   )r   r	   TFE_ContextSetJitCompileRewriter  r   r  s     r$   r*   zContext.jit_compile_rewrite  s*    '00vF &Dr#   c                     | j                   S r0   r   rf   s    r$   r5   z+Context.xla_sharding_for_resource_variables  s    444r#   c                     || _         y r0   r  r  s     r$   r5   z+Context.xla_sharding_for_resource_variables  s
    06D-r#   c                     | j                   S r0   r   rf   s    r$   r>   zContext.optionals_in_gradients  s    '''r#   c                     || _         y r0   r   r  s     r$   r>   zContext.optionals_in_gradients  s
    #)D r#   c                 p    | j                   t        j                  | j                        S | j                  S r0   )r   r	   #TFE_ContextGetDevicePlacementPolicyr  r   rf   s    r$   r  zContext.device_policy	  s1     ';;DLLIIr#   c                     |t         }| j                  |k7  r?|| _        | j                  +t        j                  | j
                  | j                         y y y r0   )r   r   r   r	   .TFE_ContextSetThreadLocalDevicePlacementPolicyr  )rT   policys     r$   r  zContext.device_policy		  sW    ~&ff$"d 
			)AALL$--	
 
*	 %r#   c                     | j                   S r0   )r   rf   s    r$   use_tfrtzContext.use_tfrt	  s    >>r#   c                     t        |t              st        dt        |      z        | j                  |k7  r| j
                  rt        d      || _        yy)zSets whether to use TFRT.zExpecting a boolean but got %sz0use_tfrt should be set before being initialized.N)r|   r  r   r   r   r   )rT   tfrts     r$   r(  zContext.use_tfrt	  sP     dD!7$t*DEE~~			KLLdn r#   c                 .    | j                   j                  S r0   )r   r  rf   s    r$   r  zContext.operation_timeout_in_ms&	  s    ;;...r#   c                 `    | j                   |k(  ry | j                  t        d      || _         y )Nz:Operation timeout cannot be modified after initialization.)r   r   r  )rT   r  s     r$   r  zContext.operation_timeout_in_ms*	  s9    $$5'
F  %2D!r#   c                 b    | j                          t        j                  | j                         y)zEnables tracing of op execution via RunMetadata.

    To retrieve the accumulated metadata call context.export_run_metadata()
    and to stop tracing call context.disable_run_metadata().
    N)rN  r	   TFE_ContextEnableRunMetadatar  rf   s    r$   enable_run_metadatazContext.enable_run_metadata6	  s"     	++DLL9r#   c                 \    | j                   syt        j                  | j                          yz1Disables tracing of op execution via RunMetadata.N)r   r	   TFE_ContextDisableRunMetadatarf   s    r$   disable_run_metadatazContext.disable_run_metadata?	  s"    ,,T-A-ABr#   c                 b    | j                          t        j                  | j                         y)zEnables graph collection of executed functions.

    To retrieve the accumulated graphs call context.export_run_metadata()
    and to stop collecting graphs call context.disable_graph_collection().
    N)rN  r	    TFE_ContextEnableGraphCollectionr  rf   s    r$   enable_graph_collectionzContext.enable_graph_collectionE	  s"     	//=r#   c                 \    | j                   syt        j                  | j                          yz0Disables graph collection of executed functions.N)r   r	   !TFE_ContextDisableGraphCollectionrf   s    r$   disable_graph_collectionz Context.disable_graph_collectionN	  s"    001E1EFr#   c                 L   | j                   syt        j                         5 }t        j                  | j                   |       t        j                  |      }ddd       t        j                         }|j                  t        j                               |S # 1 sw Y   CxY w)a  Returns a RunMetadata proto with accumulated information.

    The returned protocol buffer contains information since the most recent call
    to either enable_run_metadata or export_run_metadata.

    Returns:
      A RunMetadata protocol buffer. Or None if not enabled.
    N)r   r   r  r	   TFE_ContextExportRunMetadatar   r  r   RunMetadatarh  r   as_bytes)rT   r  rj  run_metadatas       r$   export_run_metadatazContext.export_run_metadataT	  s     				 ;7--d.B.BGL$11':j; ))+L  !<=; ;s   6BB#c                     || _         y)a  Set the number of retries to use when calling SetServerDef.

    In cases where many servers run in high-preemption environments, jobs could
    be preempted during startup and initial connection via SetServerDef. Retries
    allow for more robust connection in these environments.

    Args:
      retries: int specifying the number of connection retries before failing.
        Retries follow an exponential backoff waiting period with min value 1ms,
        max value 10s, and exponent 1.3.
    N)r  )rT   retriess     r$   set_server_def_retrieszContext.set_server_def_retriesf	  s     $+D r#   c                     | j                   S )z$Returns a stack of context switches.)r   rf   s    r$   r  zContext.context_switchest	  s     !!!r#   )NNNN)r   Tr   r   r   NF)r   )r   )CollectiveReduceFN)r  )Fr0   )r   )hlo)tri   rj   rk   rl   rW   r  r  r9  rN  rP  rR  rU  rW  rF  r^  ra  rf  ri  rl  r{  r   r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   contextmanagerr  r   r   r   r  r   r  r   r  r   r  r  r  r  r   r   rJ  r!  r$  rP  rU  rY  r]  rd  rl  rq  rt  ry  r}  r  r  r  r  r  r  r  r  r  r  r  r<  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r*   r5   r>   r  r(  r  r/  r3  r6  r:  r@  rC  r  r"   r#   r$   r   r     s   6 j%\> 
+"2H6Ap"#&
 8H $"L ;K :6&6"6& %&%&).$/L - -JC66">
@ #8",5\C.     ! ! $  $",84 . . + + / / / /6+& . . . ..$   ??Q Q E ENL\;G 9 9& <  <

<N$0(G

EF	=G
 $ $:<"$ 0 0 9 9 :  :%N,O($L-2^N	4>C?(*6-+4Z!'T &P 
8D%% / / 0 0 9 9 "((9 )9   9 9
'R9 4 4  &&	5 '	5 4 4  &&	5 '	5 , , 9  9 , , 9 9 % % ' '
 5 5 '--7 .7 ( (   * !*   
 
   ??  / / !!	2 "	2:C>G$+ " "r#   r   c                   *    e Zd ZdZg dZd Zd Zd Zy)r  zAContext-manager forcing placement of ops and Tensors on a device._device_name_ctx_stackc                 .    || _         || _        g | _        y r0   rI  )rT   r  r  s      r$   rW   z_EagerDeviceContext.__init__	  s    #DDIDKr#   c                 >   | j                   }|j                  }|j                  }| j                  }||f}	 t        |   \  }}|j'                  ||       | j(                  j+                  |||f       y # t
        $ r!}t        dt        |      d|d      |d }~wt        $ r}|t        |t              st        dt        |      d|d      |t        j                  j                  |      }|rt        j                  |      }n<|j                          t        j                  j                  |j                   d         }|j#                  |      }nt        j                  j                  d      }|j%                         }||ft        |<   Y d }~Ud }~ww xY w)Nz$Expecting a string device name. Got ()r   r   )rK  r  r   rJ  r[  r  r   r   r  r|   r   r'  r)  r*  copyrN  r   make_merged_specr,  r  rL  r   )	rT   r  old_device_nameold_device_specnew_device_name	cache_keynew_device_specr  r   s	            r$   	__enter__z_EagerDeviceContext.__enter__	  s   
))CooOooO''O /2IL)>y)I&o: OOO_5KK/JK;  /"O5   L		$/3/o&9  &&22?C IIo6/

 
 
"!,,88""1%/ *::;G**66r:'113o*9?)KI&)Ls$   A3 3	F<BF$C-FFc                     | j                   }| j                  d   \  }}}|j                  |urt        d      | j                  d= |j	                  ||       y )Nz1Exiting device scope without proper scope nesting)rK  rL  r   r  r  )rT   ex_infor  rS  rT  rW  s         r$   __exit__z_EagerDeviceContext.__exit__	  sP    
))C8<B5O_o
o-LMMBOOO_5r#   N)ri   rj   rk   rl   rm   rW   rX  r\  r"   r#   r$   r  r  z	  s    I0)%LN6r#   r  c                 R    t        j                  |        | j                          | ay r0   )r	   TFE_Py_SetEagerContextrR  _contextr  s    r$   _set_context_lockedra  	  s!    ##C((r#   c                 P    t         5  t        |        d d d        y # 1 sw Y   y xY wr0   )_context_lockra  r`  s    r$   _set_contextrd  	  s"       s   %c                  p    t         5  t        t               } t        |        d d d        y # 1 sw Y   y xY wr0   )rc  r_  r   ra  r`  s    r$   _create_contextrf  	  s/     Ic#  s   ,5c                      t        j                          t        j                          t        5  t
        t
        j                          daddd       t                i ay# 1 sw Y   xY w)zWClears and re-initializes the singleton context.

  Should only be used for testing.
  N)	gccollectr	   rT  rc  r_  rU  rf  r[  r"   r#   r$   _reset_contextrj  	  sV     **,!!# h  s   A!!A*c                  ,    t        j                          y)z[Clears and re-initializes the TF JIT compiler flags.

  Should only be used for testing.
  N)r	   TF_ResetJitCompilerFlagsr"   r#   r$   _reset_jit_compiler_flagsrm  	  s    
 %%'r#   c                  .    t         
t                t         S )z#Returns a singleton context object.)r_  rf  r"   r#   r$   r  r  	  s    	/r#   c                      t         S )zAReturns current context (or None if one hasn't been initialized).)r_  r"   r#   r$   r)   r)   	  s    	/r#   c                  4    t               j                          y)zInitialize the context.N)r  rN  r"   r#   r$   rN  rN  	  s    	) r#   c                  4    t               j                          y)zInitialize the virtual devices.N)r  r9  r"   r#   r$   initialize_logical_devicesrr  	  s    	)'')r#   c                 6    t               j                  |        y)zSets the eager mode seed.N)r  r  )r  s    r$   set_global_seedrt   
  s    	)T"r#   c                  *    t               j                  S )zReturns the eager mode seed.)r  r   r"   r#   r$   global_seedrv  
  s    	r#   c                  2    t               j                         S )z:Returns the operation seed generated based on global seed.)r  r  r"   r#   r$   internal_operation_seedrx  

  s    		+	+	--r#   r   c                  T    t               } | t        t        k(  S | j                         S )a\  Checks whether the current thread has eager execution enabled.

  Eager execution is enabled by default and this API returns `True`
  in most of cases. However, this API might return `False` in the following use
  cases.

  *  Executing inside `tf.function`, unless under `tf.init_scope` or
     `tf.config.run_functions_eagerly(True)` is previously called.
  *  Executing inside a transformation function for `tf.dataset`.
  *  `tf.compat.v1.disable_eager_execution()` is called.

  General case:

  >>> print(tf.executing_eagerly())
  True

  Inside `tf.function`:

  >>> @tf.function
  ... def fn():
  ...   with tf.init_scope():
  ...     print(tf.executing_eagerly())
  ...   print(tf.executing_eagerly())
  >>> fn()
  True
  False

  Inside `tf.function` after `tf.config.run_functions_eagerly(True)` is called:

  >>> tf.config.run_functions_eagerly(True)
  >>> @tf.function
  ... def fn():
  ...   with tf.init_scope():
  ...     print(tf.executing_eagerly())
  ...   print(tf.executing_eagerly())
  >>> fn()
  True
  True
  >>> tf.config.run_functions_eagerly(False)

  Inside a transformation function for `tf.dataset`:

  >>> def data_fn(x):
  ...   print(tf.executing_eagerly())
  ...   return x
  >>> dataset = tf.data.Dataset.range(100)
  >>> dataset = dataset.map(data_fn)
  False

  Returns:
    `True` if the current thread has eager execution enabled.
  )r)   r   r   r   r`  s    r$   r   r   
  s+    l 	#[!Z//				  r#   c                      t               S )a*  Checks whether the current thread has eager execution enabled.

  Eager execution is typically enabled via
  `tf.compat.v1.enable_eager_execution`, but may also be enabled within the
  context of a Python function via tf.contrib.eager.py_func.

  When eager execution is enabled, returns `True` in most cases. However,
  this API might return `False` in the following use cases.

  *  Executing inside `tf.function`, unless under `tf.init_scope` or
     `tf.config.run_functions_eagerly(True)` is previously called.
  *  Executing inside a transformation function for `tf.dataset`.
  *  `tf.compat.v1.disable_eager_execution()` is called.

  >>> tf.compat.v1.enable_eager_execution()

  General case:

  >>> print(tf.executing_eagerly())
  True

  Inside `tf.function`:

  >>> @tf.function
  ... def fn():
  ...   with tf.init_scope():
  ...     print(tf.executing_eagerly())
  ...   print(tf.executing_eagerly())
  >>> fn()
  True
  False

  Inside `tf.function`
  after  `tf.config.run_functions_eagerly(True)` is called:

  >>> tf.config.run_functions_eagerly(True)
  >>> @tf.function
  ... def fn():
  ...   with tf.init_scope():
  ...     print(tf.executing_eagerly())
  ...   print(tf.executing_eagerly())
  >>> fn()
  True
  True
  >>> tf.config.run_functions_eagerly(False)

  Inside a transformation function for `tf.dataset`:

  >>> def data_fn(x):
  ...   print(tf.executing_eagerly())
  ...   return x
  >>> dataset = tf.data.Dataset.range(100)
  >>> dataset = dataset.map(data_fn)
  False

  Returns:
    `True` if the current thread has eager execution enabled.
  r   r"   r#   r$   executing_eagerly_v1r|  L
  s    x 
	r#   c                      t               S )z?Use executing_eagerly() instead. This function will be removed.r{  r"   r#   r$   in_eager_moder~  
  s    		r#   c                       y)a  Returns the anonymous shared name.

  In eager mode we create anonymous resources to avoid spurious sharing issues.
  The runtime generates a unique name on our behalf when the reserved
  anonymous shared name is used as a shared name.

  Returns:
    The anonymous shared name.
  z$cd2c89b7-88b7-44c8-ad83-06c2a9158347r"   r"   r#   r$   anonymous_namer  
  s     
0r#   c                  <    t               j                  t              S )zBContext-manager to disable eager execution for the current thread.)r  r  
GRAPH_MODEr"   r#   r$   
graph_moder  
  s    		$$r#   z%__internal__.eager_context.eager_modec                  <    t               j                  t              S )zAContext-manager to enable eager execution for the current thread.)r  r  r   r"   r#   r$   r   r   
  s     
	$$r#   c                  *    t               j                  S )zName of the current scope.)r  r  r"   r#   r$   r  r  
  s    			r#   c                 H    t                t               j                  |       S )a  Context-manager to force placement of operations and Tensors on a device.

  Example:
  ```python
  with tf.device('gpu:0'):
    with tf.device('cpu:0'):
      shape = tf.constant([], dtype=tf.int32)
    x = tf.random.truncated_normal(shape, tf.float32)
  ```
  will ensure that the `shape` Tensor is on CPU but the `truncated_normal`
  operation runs on GPU 0.

  Args:
    name: Name of the device (see context().devices()), or None to perform
      automatic placement.

  Returns:
    Context manager for setting the device.
  )rN  r  r   r   s    r$   r   r   
  s    ( 			$	r#   z%__internal__.eager_context.get_configc                  *    t               j                  S )zNGet the ConfigProto of Context.

  Returns:
    The ConfigProto of Context.
  )r  r   r"   r#   r$   
get_configr  
  s     
		r#   z*__internal__.eager_context.get_device_namec                  *    t               j                  S )zfGet the device name for the current thread.

  Returns:
    The device name for the current thread.
  )r  r  r"   r#   r$   get_device_namer  
  s     
		r#   z4__internal__.eager_context.set_soft_device_placementc                 "    | t               _        y)zrSet if soft device placements should be allowed.

  Args:
    enabled: Whether to enable soft device placement.
  N)r  r  r  s    r$   set_soft_device_placementr  
  s     %,')!r#   z'__internal__.eager_context.get_executorc                  *    t               j                  S )z^Get the Executor of the current thread.

  Returns:
    The Executor of the current thread.
  r  r   r"   r#   r$   get_executorr  
  s     
		r#   z"debugging.get_log_device_placementc                  *    t               j                  S )zXGet if device placements are logged.

  Returns:
    If device placements are logged.
  r  r  r"   r#   r$   get_log_device_placementr  
  s     
	'	''r#   z"debugging.set_log_device_placementc                 "    | t               _        y)a  Turns logging for device placement decisions on or off.

  Operations execute on a particular device, producing and consuming tensors on
  that device. This may change the performance of the operation or require
  TensorFlow to copy data to or from an accelerator, so knowing where operations
  execute is useful for debugging performance issues.

  For more advanced profiling, use the [TensorFlow
  profiler](https://www.tensorflow.org/guide/profiler).

  Device placement for operations is typically controlled by a `tf.device`
  scope, but there are exceptions, for example operations on a `tf.Variable`
  which follow the initial placement of the variable. Turning off soft device
  placement (with `tf.config.set_soft_device_placement`) provides more explicit
  control.

  >>> tf.debugging.set_log_device_placement(True)
  >>> tf.ones([])
  >>> # [...] op Fill in device /job:localhost/replica:0/task:0/device:GPU:0
  >>> with tf.device("CPU"):
  ...  tf.ones([])
  >>> # [...] op Fill in device /job:localhost/replica:0/task:0/device:CPU:0
  >>> tf.debugging.set_log_device_placement(False)

  Turning on `tf.debugging.set_log_device_placement` also logs the placement of
  ops inside `tf.function` when the function is called.

  Args:
    enabled: Whether to enabled device placement logging.
  Nr  r  s    r$   set_log_device_placementr  
  s    @ $+') r#   c              #   v   K   t               }|j                  }	 | |_        d ||_        y# ||_        w xY ww)zGContext manager for setting device placement policy for current thread.N)r  r  )r&  r  
old_policys      r$   r  r    s:      		#  *#C	"C
C   9- 9	69c                 "    | t               _        y)z+Sets execution mode for the current thread.N)r  r  )r  s    r$   set_execution_moder  +  s    !')r#   c              #     K   | d yt               }t        j                  | t        k(        }|j                  }	 |j	                          ||_        d ||_        |j	                          y# ||_        |j	                          w xY ww)z>Context manager for setting execution mode for current thread.N)r  r   r  r   r  )r  r  r  executor_olds       r$   r  r  1  sx      
\	
)C((7L<<L!cl!cl "cls   :BA0 B0B		Bc              #   v   K   t               }|j                  }	 | |_        d ||_        y# ||_        w xY ww)a!  Context manager for changing executor for current thread.

  Args:
    e: A Executor to execute eager ops under this scope. Setting it to None will
      switch back to use the default executor for the context.

  Yields:
    Context manager for setting the executor for current thread.
  Nr  )r  r  r  s      r$   executor_scoper  C  s5      		#, CL	CL<CLr  z#experimental.function_executor_typec              #      K   t               j                  }t        j                  |      }	 | |_        d |t               _        y# |t               _        w xY ww)at  Context manager for setting the executor of eager defined functions.

  Eager defined functions are functions decorated by tf.contrib.eager.defun.

  Args:
    executor_type: a string for the name of the executor to be used to execute
      functions defined by tf.contrib.eager.defun.

  Yields:
    Context manager for setting the executor of eager defined functions.
  N)r  rP  rQ  rt   )rt   current_optionsold_optionss      r$   function_executor_typer  W  sG      I33/		/*+2$1O!	&1GI#kGI#s   *AA AAAc                  2    t               j                         S )z0Returns true if current thread is in async mode.)r  r  r"   r#   r$   r  r  n  s    				r#   c                  2    t               j                         S )z`Get the number of available GPU devices.

  Returns:
    The number of available GPU devices.
  )r  rU  r"   r#   r$   rU  rU  s  s     
			r#   c                  4    t               j                          y)zEnables tracing of op execution via RunMetadata.

  To retrieve the accumulated metadata call context.export_run_metadata()
  and to stop tracing call context.disable_run_metadata().
  N)r  r/  r"   r#   r$   r/  r/  |  s     
)!r#   c                  4    t               j                          yr1  )r  r3  r"   r#   r$   r3  r3    s    	)  "r#   c                  4    t               j                          y)zEnables graph collection of executed functions.

  To retrieve the accumulated graphs call context.export_run_metadata()
  and to stop collecting graphs call context.disable_graph_collection().
  N)r  r6  r"   r#   r$   r6  r6    s     
)##%r#   c                  4    t               j                          yr8  )r  r:  r"   r#   r$   r:  r:    s    	)$$&r#   c                  2    t               j                         S )zReturns a RunMetadata proto with accumulated information.

  The returned protocol buffer contains information since the most recent call
  to either enable_run_metadata or export_run_metadata.

  Returns:
    A RunMetadata protocol buffer.
  )r  r@  r"   r#   r$   r@  r@    s     
	&	&	((r#   c              #   L  K   t               }|j                          	 g }| |j                         }|j                          |j                  D ];  }| r|j                  |j                         !|j                  |j                         = y# |j                          w xY ww)aL  Collects a flat list of pre- or post-optimization graphs.

  The collected graphs include device placements, which can be useful for
  testing.

  Usage:

  ```
  @def_function.function
  def f(x):
    return x + constant_op.constant(1.)

  with context.collect_graphs() as graphs:
    with ops.device("CPU:0"):
      f(constant_op.constant(1.))

  graph, = graphs  # `graph` contains a single GraphDef for inspection
  ```

  Args:
    optimized: whether to collect optimized graphs or non-optimized graphs

  Yields:
    A list of GraphDefs, populated when the context manager exits.
  N)r  r6  r@  r:  function_graphsr   post_optimization_graphpre_optimization_graph)	optimizedr  graphsmetadatagraphs        r$   collect_graphsr    s     6 		##F
L&&(H  "'' 2emmE112mmE001	2   "s   B$B AB$B!!B$c                  2    t               j                         S r0   )r  rW  r"   r#   r$   rW  rW    s    		!	!	##r#   c                 6    t               j                  |        y r0   )r  r^  r  s    r$   r^  r^    s    	):&r#   c                 6    t               j                  |        y)a  Set the number of retries to use when calling SetServerDef.

  In cases where many servers run in high-preemption environments, jobs could
  be preempted during startup and initial connection via SetServerDef. Retries
  allow for more robust connection in these environments.


  Args:
    retries: int specifying the number of connection retries before failing.
      Retries follow an exponential backoff waiting period with min value 1ms,
      max value 10s, and exponent 1.3.
  N)r  rC  )rB  s    r$   rC  rC    s     
)""7+r#   c                 6    t               j                  |        y r0   )r  ra  r  s    r$   ra  ra    s    	)j)r#   c                 4    t               j                  |       S r0   )r  rf  )re  s    r$   rf  rf    s    			{	++r#   zexperimental.async_scopec               #   f  K   d} t         j                  j                  |       }	 t        d      t         j                  | <   d t	               j                          |t         j                  | = y|t         j                  | <   y# |t         j                  | = w |t         j                  | <   w xY ww)a  Context manager for grouping async operations.

  Ops/function calls inside the scope can return before finishing the actual
  execution. When exiting the async scope, a synchronization barrier will be
  automatically added to ensure the completion of all async op and function
  execution, potentially raising exceptions if async execution results in
  an error state.

  Users may write the following code to asynchronously invoke `train_step_fn`
  and log the `loss` metric for every `num_steps` steps in a training loop.
  `train_step_fn` internally consumes data using `iterator.get_next()`, and may
  throw OutOfRangeError when running out of data. In the case:

  ```
  try:
    with tf.experimental.async_scope():
      for _ in range(num_steps):
        # Step function updates the metric `loss` internally
        train_step_fn()
  except tf.errors.OutOfRangeError:
    tf.experimental.async_clear_error()
  logging.info('loss = %s', loss.numpy())
  ```

  Yields:
    Context manager for grouping async operations.
  (TF_ENABLE_EAGER_CLIENT_STREAMING_ENQUEUETN)osenvironrb   r   r  ri  )remote_async_env_varr  s     r$   async_scoper    s     @ Dzz~~23*4'*4yBJJ#$	 I
**)
*)3bjj%& 
**)
*)3bjj%&s   "B18B (B1)B..B1c                      d} t         j                  j                  |       t        d      k(  ryt	               j
                  t	               j                          yy)a  Sync all async operations and raise any errors during execution.

  In async execution mode, an op/function call can return before finishing the
  actual execution. Calling this method creates a synchronization barrier for
  all async op and function execution. It only returns when all pending nodes
  are finished, potentially raising exceptions if async execution results in
  an error state. It is a no-op if the context is not initialized.
  %TF_PS_DISABLE_ASYNC_EXECUTOR_GLOBALLYTN)r  r  rb   r   r  r   ri  )disable_async_executor_env_vars    r$   
async_waitr    sI     $K ZZ^^23s4y@
Y*I +r#   zexperimental.async_clear_errorc                  4    t               j                          y)a:  Clear pending operations and error statuses in async execution.

  In async execution mode, an error in op/function execution can lead to errors
  in subsequent ops/functions that are scheduled but not yet executed. Calling
  this method clears all pending operations and reset the async execution state.

  Example:

  ```
  while True:
    try:
      # Step function updates the metric `loss` internally
      train_step_fn()
    except tf.errors.OutOfRangeError:
      tf.experimental.async_clear_error()
      break
  logging.info('loss = %s', loss.numpy())
  ```
  N)r  rl  r"   r#   r$   async_clear_errorr  .  s    * 
)!!#r#   c                 6    t               j                  |        y)z'Add a C API TF_Function to the context.N)r  rY  )rX  s    r$   rY  rY  F  s    	)6"r#   c                 4    t               j                  |       S )z)Get a C API TF_Function from the context.)r  r]  r  s    r$   r]  r]  K  s    		!	!$	''r#   c                 6    t               j                  |        y)z#Remove a function from the context.N)r  r  r  s    r$   r  r  P  s    	)D!r#   c                 4    t               j                  |       S r0   )r  rl  r  s    r$   rl  rl  U  s    		#	#D	))r#   c                 4    t               j                  |       S )aJ  Calls TFE_IsCustomDevice.

  Enables using C extensions specifying a custom device from Python. See the
  experimental eager C API in tensorflow/c/eager/c_api_experimental.h for
  details.

  Args:
    device_name: A string indicating the name to check whether it is a
      registered custom device.

  Returns:
    A boolean.
  )r  rt  )r  s    r$   rt  rt  Y  s     
	#	#K	00r#   c                 :    t               j                  | ||       y)a!  Calls TFE_RegisterCustomDevice to register a custom device with Python.

  Enables using C extensions specifying a custom device from Python. See the
  experimental eager C API in tensorflow/c/eager/c_api_experimental.h for
  details.

  Note that custom devices are not currently supported inside `tf.function`s.

  Args:
    device_capsule: A PyCapsule with the name set to 'TFE_CustomDevice'
      containing a pointer to a TFE_CustomDevice struct. The capsule retains
      ownership of the memory.
    device_name: A string indicating the name to register the custom device
      under, e.g. '/job:localhost/replica:0/task:0/device:CUSTOM:0'. It may
      subsequently be passed to `with tf.device(...):`.
    device_info_capsule: A PyCapsule with the name set to
      'TFE_CustomDevice_DeviceInfo' containing a pointer to a device-specific
      struct with the initial state of the custom device (the void* device_info
      argument to TFE_RegisterCustomDevice). This method takes ownership of the
      memory and clears the capsule destructor.
  N)r  ry  )rw  r  rx  s      r$   ry  ry  j  s    , 
)""k#6r#   c                  .    t               yt                S r!   )r)   r   r"   r#   r$   _tmp_in_graph_moder    s    ^  	  r#   )T)rl   rR   
contextlibrQ  rh  r  r  r  r   abslr   numpyr  $tensorflow.compiler.xla.tsl.protobufr   tensorflow.core.frameworkr   r   tensorflow.core.protobufr   r   tensorflow.pythonr	   r
   tensorflow.python.clientr   tensorflow.python.eagerr   r   r   r   tensorflow.python.frameworkr   r   r'  r   tensorflow.python.utilr   r   r   r   "tensorflow.python.util.deprecationr    tensorflow.python.util.tf_exportr   r_  r  r   r  r   r[  r)  r*  r   r  TFE_DEVICE_PLACEMENT_EXPLICITDEVICE_PLACEMENT_EXPLICITTFE_DEVICE_PLACEMENT_WARNDEVICE_PLACEMENT_WARNTFE_DEVICE_PLACEMENT_SILENTr   %TFE_DEVICE_PLACEMENT_SILENT_FOR_INT32!DEVICE_PLACEMENT_SILENT_FOR_INT32r   r   rF  Counterr  r   getenvr(   r4   r=   r%   r+   r.   r1   r6   r8   r:   r?   rA   rC   rG  rG   rJ   objectrM   ro   r   localr   
namedtupler   r   r   r   r   r   r   r   r   r  r_  r   rc  ra  rd  rf  rj  rm  r  r)   rN  rr  rt  rv  rx  r   r|  r~  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  rU  r/  r3  r6  r:  r@  r  rW  r^  rC  ra  rf  r  r  r  rY  r]  r  rl  rt  ry  r  IS_IN_GRAPH_MODEr"   r#   r$   <module>r     s   ,    	  	     H 2 : / 8 ( ! 6 0 + , . 2 7 2 ) 1 3 0 9 6 


'2s{{}*   ((44R8 	&DD "<< $@@ 44 " 		 '9z'9'99=( $ $$  )ryy)ABcI  BII673> % $"))$?@CG 
	./&
>?.12! 3 3$ & && 1	
(R 0 A 4>D >DH  IOO . '&&@*)// *Z !"K?V],CD #" '4)K$	
)	)X "#K+fm-DE $4V  %& /& /"p" p"f;96& 96z 	 
((
!
*
#

.
 2&9! '9!x "#$; %;|
0 % 2r:% ;%

 2 2r: ; 7B? @ AbI, J, 4< = /0( 1( /0+ 1+D # #"  "    & 012  22*
"#
&'
	) &2 &2R$', *, %&+4  '+4\  +,$ -$.#
(
"
*1">! %7  !r#   