
    AVh(                         d Z ddl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  G d dej"                        ZddZ ej(                  ee       y)z0Various classes representing distributed values.    N)device_util)tpu_util)values_util)context)ops)tensor_conversion_registry)control_flow_ops)resource_variable_ops)	variablesc                   x    e Zd ZdZdd fd
Zed        Ze fd       Ze fd       Zd Z	d	 Z
d
 Z fdZd# fd	Zd# fd	Zd# fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd$ fd	Zd% fd	Zd% fd	Zd% fd	Zd% fd	Zd% fd	Zd%dZed%d       Zd Z  fdZ!d  Z"d! Z#d" Z$ xZ%S )&DistributedVariablea  Represents variables that are replicated.

  It behaves exactly as a normal variable, but uses corresponding variable
  handle based on the context.
  - In each replica, it uses the handle from that replica.
  - In tpu.replicate(), it uses the replicated handle.
  - Otherwise, it uses the handle from the primary replica.

  Note that it doesn't synchronize automatically as the old DistributedVariable
  in values.py.
  Fenable_packed_handlec                   |rt        j                         st        d      || _        |r3t        j                  |D cg c]  }|j
                   c}      | _        nd | _        |D ]&  }t        j                  |       |j
                  _	        ( |D ci c]  }|j                  |j
                   c}| _        |d   j
                  | _        t        j                         5  t        j                  dd      5 }t        j                  |      }d|t        j                          fz  | _        t%        j&                         rd }d }n<|d   j(                  }t+        j,                  |D cg c]  }|j.                   c}      }t0        | e  |d   j4                  |d   j6                  |d   j8                  d |d   j:                  |d   j<                  |d   j>                  |d   j@                  |d   jB                  | j"                  ||d   jD                  ||d d d d       d d d        d d d        y c c}w c c}w c c}w # 1 sw Y   !xY w# 1 sw Y   y xY w)	NzArgument `enable_packed_handle` is true, but packed handle is only supported in eager mode. Please make sure eager execution is enabled.r   r   F)skip_on_eagerz%s_%dT)	trainableshapedtypehandlesynchronization
constraintaggregationdistribute_strategyname	unique_idhandle_namegraph_elementinitial_valueinitializer_opis_initialized_opcached_valuecaching_deviceis_variables)#r   #executing_eagerly_outside_functions
ValueError
_variablespack_eager_tensorsr   _packed_handleweakrefref_distributed_containerdevice_device_to_handle_primary_handle
init_scope
name_scopename_from_scope_nameuid
_unique_idr   executing_eagerlyr   r	   groupinitializersuper__init__r   r   r   r   r   r   _distribute_strategyr   _graph_element)	selfr   r   vr   r   r   r6   	__class__s	           V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/distribute/values_v2.pyr8   zDistributedVariable.__init__/   s!   C$K$K$M   DO22&
'188
')d !d :(/D(9ahh%::CDQahh0DD$Q<..D		 	-U	CGK,,T2k;	"::do		"	"	$!!22&,,Y-Oamm-OPgaL**!""!""#A,66q\,,l..'l??|  OO!!!33%$ %     (
 E .P   sC   H5H:#I;A/I*H?=B'I$I?II		IIc                    t        j                         r| j                  S t        j                         }|rt        j                         s| j                  d   j                  t        j                  j                  k7  }| j                  &| j                  D cg c]  }|j                   }}d}n| j                  g}d}| j                  }d|v r|j                  d      d   }|j!                  || j"                  |||      S | j                   t        j                         s| j                  S t%        j&                  t%        j(                               }| j*                  j-                  || j                        S c c}w )Nr   FT:)r   is_saving_non_distributedr.   r   enclosing_tpu_contextr   r4   r&   r   variables_libVariableSynchronizationON_READr(   r   _handle_namesplitget_replicated_var_handler3   r   canonicalizecurrentr-   get)r;   tpu_contextis_mirroredr<   handles	is_packedcommon_namer,   s           r>   r   zDistributedVariable.handle]   sQ   ,,.!!!002K7446
//!

,
,

/
/
7
78  
			$%)__518855	&&'	%%k 
	!'',Q/22;3:K3<> > &w/H/H/J   %%k&9&9&;<F!!%%fd.B.BCC! 6s   E<c                 r    t        j                         r| j                  d   j                  S t        |   S Nr   )r   rA   r&   r   r7   r;   r=   s    r>   r   zDistributedVariable.namey   s.    ,,.__Q$$$7<    c                 r    t        j                         r| j                  d   j                  S t        |   S rR   )r   rA   r&   r6   r7   rS   s    r>   r6   zDistributedVariable.initializer   s/    ,,.__Q+++7rT   c                 z    t        j                  |g      5  | j                         cd d d        S # 1 sw Y   y xY wN)r   control_dependencies
read_value)r;   ops     r>   
_lazy_readzDistributedVariable._lazy_read   s1    		!	!2$	' __  s   1:c                 v   | j                   (t        j                         st        j                         t        j                         S t        j                  t        j                               }|| j                  v rt        j                         S t        j                  | j                  j                        S rW   )r(   r   rA   r   rB   r   NullContextmanagerr   rI   rJ   r-   r,   r.   )r;   r,   s     r>   _device_scopez!DistributedVariable._device_scope   s    #--/&&(4##%%%%k&9&9&;<F'''##%%::d**1122rT   c                 "    | j                         S rW   )rY   r;   s    r>   valuezDistributedVariable.value   s     ??rT   c                 l    | j                         5  t        | 	         cd d d        S # 1 sw Y   y xY wrW   )r^   r7   rY   rS   s    r>   rY   zDistributedVariable.read_value   s/    				 "W!" " "s   *3c                 t    | j                         5  t        | 	  ||||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   
assign_subr;   deltause_lockingr   rY   r=   s        r>   rd   zDistributedVariable.assign_sub   ;    				 FW{D*EF F F   .7c                 t    | j                         5  t        | 	  ||||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   
assign_addre   s        r>   rk   zDistributedVariable.assign_add   rh   ri   c                 t    | j                         5  t        | 	  ||||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   assign)r;   ra   rg   r   rY   r=   s        r>   rm   zDistributedVariable.assign   s:    				 BW^E;jAB B Bri   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_subr;   sparse_deltarg   r   r=   s       r>   ro   zDistributedVariable.scatter_sub   9    				 BW {DAB B B   -6c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_addrp   s       r>   ru   zDistributedVariable.scatter_add   rr   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_mulrp   s       r>   rw   zDistributedVariable.scatter_mul   rr   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_divrp   s       r>   ry   zDistributedVariable.scatter_div   rr   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_minrp   s       r>   r{   zDistributedVariable.scatter_min   rr   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_maxrp   s       r>   r}   zDistributedVariable.scatter_max   rr   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_updaterp   s       r>   r   z"DistributedVariable.scatter_update   s9    				 EW#L+tDE E Ers   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   batch_scatter_updaterp   s       r>   r   z(DistributedVariable.batch_scatter_update   s9    				 KW),TJK K Krs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_nd_subr;   indicesupdatesr   r=   s       r>   r   z"DistributedVariable.scatter_nd_sub   5    				 <W#GWd;< < <rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_nd_addr   s       r>   r   z"DistributedVariable.scatter_nd_add   r   rs   c                 r    | j                         5  t        | 	  |||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   scatter_nd_updater   s       r>   r   z%DistributedVariable.scatter_nd_update   s5    				 ?W&w>? ? ?rs   c                 p    | j                         5  t        | 	  ||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   sparse_readr;   r   r   r=   s      r>   r   zDistributedVariable.sparse_read   s3    				 0W $/0 0 0   ,5c                 p    | j                         5  t        | 	  ||      cd d d        S # 1 sw Y   y xY wrW   )r^   r7   	gather_ndr   s      r>   r   zDistributedVariable.gather_nd   s3    				 .Ww-. . .r   c                     ~ t        d      )Nz,DistributedVariable doesn't support to_proto	TypeError)r;   export_scopes     r>   to_protozDistributedVariable.to_proto   s    
B
CCrT   c                     t        d      )Nz.DistributedVariable doesn't support from_protor   )variable_defimport_scopes     r>   
from_protozDistributedVariable.from_proto   s    
D
EErT   c                     t        j                         j                  r| j                  d   j                  S | j                         S rR   )r   get_default_graph	finalizedr&   r:   rY   r`   s    r>   _as_graph_elementz%DistributedVariable._as_graph_element   s6    
((__Q...??rT   c                 n    | j                         5  t        |   |i |cd d d        S # 1 sw Y   y xY wrW   )r^   r7   _strided_slice_assign)r;   argskwargsr=   s      r>   r   z)DistributedVariable._strided_slice_assign   s6    				 <W*D;F;< < <s   +4c                     dj                  d t        | j                        D              }| j                  j                  d|dS )N,
c              3   0   K   | ]  \  }}d ||fz    yw)z  %d: %sN .0ir<   s      r>   	<genexpr>z.DistributedVariable.__str__.<locals>.<genexpr>   s$      D $1
aVD   :{

}join	enumerater&   r=   __name__)r;   	debug_strs     r>   __str__zDistributedVariable.__str__   s>    

 D(1$//(BD DI NN33Y??rT   c                     dj                  d t        | j                        D              }| j                  j                  d|dS )Nr   c              3   0   K   | ]  \  }}d ||fz    yw)z  %d: %rNr   r   s      r>   r   z/DistributedVariable.__repr__.<locals>.<genexpr>   s$      D $1
aVDr   r   r   r   )r;   
debug_reprs     r>   __repr__zDistributedVariable.__repr__   s>     D(1$//(BD DJ NN33Z@@rT   c                 t    t        j                  | j                  |      }t        || j                  d u      S )Nr   )copydeepcopyr&   r   r(   )r;   memocopied_variabless      r>   __deepcopy__z DistributedVariable.__deepcopy__   s6    }}T__d;t/B/B$/NP PrT   )NNT)FNrW   )&r   
__module____qualname____doc__r8   propertyr   r   r6   r[   r^   ra   rY   rd   rk   rm   ro   ru   rw   ry   r{   r}   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   __classcell__)r=   s   @r>   r   r   "   s    
 9> ,\ D D6  
  
3
"FFBBBBBBBEK<<?0.D F F
<@
A
PrT   r   c                 j    |rt        d      t        j                  | j                         |||      S )NzYou may be using variable created under distribute strategy in TF 1.x control flows. Try explicitly converting the variable to Tensor using variable.read_value(), or switch to TF 2.x.)r   r   as_ref)r%   r   convert_to_tensorrY   )varr   r   r   s       r>   _tensor_conversionr     s<    
	<= = 
			nne$v
? ?rT   )NNF)r   r   r)   tensorflow.python.distributer   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr	   r
   r   rC   BaseResourceVariabler   r   #register_tensor_conversion_functionr   rT   r>   <module>r      sa    7   4 1 4 + + B 2 7 <\P/DD \P~? ?  > >+-rT   