
    AVh                     d    d Z ddlZ ej                  d      Z ej                  d      Zd Zd Zy)zJUtility to re-use variables created on first device on subsequent devices.    Nz_\d/z_\d$c                 d    | yt         j                  d|       } t        j                  d|       } | S )NVariable/ )_VARIABLE_UNIQUIFYING_REGEXsub"_VARIABLE_UNIQUIFYING_REGEX_AT_END)names    d/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/distribute/shared_variable_creator.py_canonicalize_variable_namer      s3    	\	$	(	(d	3$	+	/	/D	9$	+    c                 Z     i t        t              sJ  fd} fd}dk(  r|S |S )a{  Construct the variable creator function for device `device_id`.

  Constructs custom variable creator functions for the given device.
  On first device (device_id == 0), it creates the variable using the
  `next_creator`, and stores it in the provided `shared_variable_store`.
  On all other devices (device_id > 0), it tries to re-use the variable
  already created with the same name. If no such variable exists, it throws an
  error.
  Additionally, we de-uniquify variable names before checking for matches. This
  helps re-use variables which are intended to be the same but have different
  names due to variable uniquification happening upstream. Since this might
  mean we may have multiple variables with the same canonical name, we store
  them in a list per canonical name and return them in the same order as well.

  Args:
    shared_variable_store: A dictionary that we will use to store variables
      created on the first device, and re-used by creators for other devices.
    device_id: Integer index of the device whose creator should be
      constructed.

  Returns:
    An appropriate creator function based on device_id.

  c                     t        |j                  d            } | di |}|vrg |<   |   j                  |       |S )z6Create the variable using `next_creator` and store it.r
    )r   getappend)next_creatorkwargscanonical_namevshared_variable_stores       r   create_new_variablez$make_fn.<locals>.create_new_variable>   sO    0F1CDNvA22.0N+.)003Hr   c                     ~ |j                  d      }t        |      }	 j                  |d      }|   |   }|dz   |<   |S # t        t        f$ r t	        dj                  |            w xY w)z>Re-use existing variable from store with same name (in order).r
   r      z>Tried to create variable {} with mismatching name on device {})r   r   KeyError
IndexErrorRuntimeErrorformat)	r   r   r
   r   variable_indexr   	device_idr   variable_scope_access_indexs	         r   reuse_variablezmake_fn.<locals>.reuse_variableH   s    ::fD06N
#266~qIn

/
?a 5CQ4F!.1hj! #
J
&y
!# ##s   #A +A/r   )
isinstanceint)r   r    r   r"   r!   s   ``  @r   make_fnr%   "   s:    2 !#	Is	##	##$ !^r   )__doc__recompiler   r	   r   r%   r   r   r   <module>r)      s:    Q 	(bjj1 %/RZZ%8 ";r   