o
    2h(                     @   s   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 dejZdddZeee dS )z0Various classes representing distributed values.    N)device_util)tpu_util)values_util)context)ops)tensor_conversion_registry)control_flow_ops)resource_variable_ops)	variablesc                       s  e Zd ZdZdd fdd
Zedd Ze fdd	Ze fd
dZdd Z	dd Z
dd Z fddZdD fdd	ZdD fdd	ZdD fdd	ZdE fdd	ZdE fdd	ZdE fd d!	ZdE fd"d#	ZdE fd$d%	ZdE fd&d'	ZdE fd(d)	ZdE fd*d+	ZdF fd,d-	ZdF fd.d/	ZdF fd0d1	ZdF fd2d3	ZdF fd4d5	ZdFd6d7ZedFd8d9Zd:d; Z  fd<d=Z!d>d? Z"d@dA Z#dBdC Z$  Z%S )G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             	      s  |r
t  s
td|| _|rt dd |D | _nd | _|D ]	}t| |j_	q dd |D | _
|d j| _t   t jddd	}t |}d
|t  f | _t r\d }d }n|d j}tdd |D }t jdi d|d jd|d jd|d jdd d|d jd|d jd|d jd|d jd|d jd| jd|d|d j d|d|dd dd dd dd W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S ) NzArgument `enable_packed_handle` is true, but packed handle is only supported in eager mode. Please make sure eager execution is enabled.c                 S      g | ]}|j qS  handle.0vr   r   a/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/distribute/values_v2.py
<listcomp>8       z0DistributedVariable.__init__.<locals>.<listcomp>c                 S   s   i | ]}|j |jqS r   )devicer   r   r   r   r   
<dictcomp>=   s    z0DistributedVariable.__init__.<locals>.<dictcomp>r   r   F)skip_on_eagerz%s_%dc                 S   r   r   )initializerr   r   r   r   r   H   r   	trainableshapedtyper   synchronization
constraintaggregationdistribute_strategyname	unique_idhandle_namegraph_elementinitial_valueinitializer_opis_initialized_opcached_valuecaching_deviceis_variablesTr   )!r   #executing_eagerly_outside_functions
ValueError
_variablespack_eager_tensors_packed_handleweakrefrefr   _distributed_container_device_to_handle_primary_handle
init_scope
name_scopename_from_scope_nameuid
_unique_idr   executing_eagerlyr'   r   groupsuper__init__r   r   r   r   r    r!   _distribute_strategyr#   _graph_element)selfr
   r   r   r#   r%   r'   r   	__class__r   r   r?   /   s~   










	

PzDistributedVariable.__init__c                 C   s   t  r| jS t }|rIt sI| jd jt	j
jk}| jd u r+dd | jD }d}n| jg}d}| j}d|v r?|dd }||| j|||S | jd urUt sU| jS tt }| j|| jS )Nr   c                 S   r   r   r   r   r   r   r   r   g   r   z.DistributedVariable.handle.<locals>.<listcomp>FT:)r   is_saving_non_distributedr6   r   enclosing_tpu_contextr   r<   r/   r   variables_libVariableSynchronizationON_READr1   _handle_namesplitget_replicated_var_handler;   r   canonicalizecurrentr5   get)rB   tpu_contextis_mirroredhandles	is_packedcommon_namer   r   r   r   r   ]   s0   


zDistributedVariable.handlec                       t  r
| jd jS t jS Nr   )r   rF   r/   r#   r>   rB   rC   r   r   r#   y      zDistributedVariable.namec                    rV   rW   )r   rF   r/   r   r>   rX   rC   r   r   r      rY   zDistributedVariable.initializerc                 C   s8   t |g |  W  d    S 1 sw   Y  d S N)r   control_dependencies
read_value)rB   opr   r   r   
_lazy_read   s   $zDistributedVariable._lazy_readc                 C   sT   | j d u st st d urt S tt	 }|| j
v r#t S t| jjS rZ   )r1   r   rF   r   rG   r   NullContextmanagerr   rN   rO   r5   r   r6   )rB   r   r   r   r   _device_scope   s   

z!DistributedVariable._device_scopec                 C   s   |   S rZ   )r\   rX   r   r   r   value   s   zDistributedVariable.valuec                    s6   |    t  W  d    S 1 sw   Y  d S rZ   )r`   r>   r\   rX   rC   r   r   r\      s   
$zDistributedVariable.read_valueNTc                    >   |    t ||||W  d    S 1 sw   Y  d S rZ   )r`   r>   
assign_subrB   deltause_lockingr#   r\   rC   r   r   rc         
$zDistributedVariable.assign_subc                    rb   rZ   )r`   r>   
assign_addrd   rC   r   r   rh      rg   zDistributedVariable.assign_addc                    rb   rZ   )r`   r>   assign)rB   ra   rf   r#   r\   rC   r   r   ri      rg   zDistributedVariable.assignc                    <   |    t |||W  d    S 1 sw   Y  d S rZ   )r`   r>   scatter_subrB   sparse_deltarf   r#   rC   r   r   rk         
$zDistributedVariable.scatter_subc                    rj   rZ   )r`   r>   scatter_addrl   rC   r   r   ro      rn   zDistributedVariable.scatter_addc                    rj   rZ   )r`   r>   scatter_mulrl   rC   r   r   rp      rn   zDistributedVariable.scatter_mulc                    rj   rZ   )r`   r>   scatter_divrl   rC   r   r   rq      rn   zDistributedVariable.scatter_divc                    rj   rZ   )r`   r>   scatter_minrl   rC   r   r   rr      rn   zDistributedVariable.scatter_minc                    rj   rZ   )r`   r>   scatter_maxrl   rC   r   r   rs      rn   zDistributedVariable.scatter_maxc                    rj   rZ   )r`   r>   scatter_updaterl   rC   r   r   rt      rn   z"DistributedVariable.scatter_updatec                    rj   rZ   )r`   r>   batch_scatter_updaterl   rC   r   r   ru      rn   z(DistributedVariable.batch_scatter_updatec                    rj   rZ   )r`   r>   scatter_nd_subrB   indicesupdatesr#   rC   r   r   rv      rn   z"DistributedVariable.scatter_nd_subc                    rj   rZ   )r`   r>   scatter_nd_addrw   rC   r   r   rz      rn   z"DistributedVariable.scatter_nd_addc                    rj   rZ   )r`   r>   scatter_nd_updaterw   rC   r   r   r{      rn   z%DistributedVariable.scatter_nd_updatec                    :   |    t ||W  d    S 1 sw   Y  d S rZ   )r`   r>   sparse_readrB   rx   r#   rC   r   r   r}         
$zDistributedVariable.sparse_readc                    r|   rZ   )r`   r>   	gather_ndr~   rC   r   r   r      r   zDistributedVariable.gather_ndc                 C   s
   ~ t d)Nz,DistributedVariable doesn't support to_proto	TypeError)rB   export_scoper   r   r   to_proto   s   zDistributedVariable.to_protoc                 C   s   t d)Nz.DistributedVariable doesn't support from_protor   )variable_defimport_scoper   r   r   
from_proto   s   zDistributedVariable.from_protoc                 C   s   t  jr| jd jS |  S rW   )r   get_default_graph	finalizedr/   rA   r\   rX   r   r   r   _as_graph_element   s   
z%DistributedVariable._as_graph_elementc                    s>   |    t j|i |W  d    S 1 sw   Y  d S rZ   )r`   r>   _strided_slice_assign)rB   argskwargsrC   r   r   r      rg   z)DistributedVariable._strided_slice_assignc                 C   *   d dd t| jD }d| jj|f S )N,
c                 s        | ]\}}d ||f V  qdS )z  %d: %sNr   r   ir   r   r   r   	<genexpr>       
z.DistributedVariable.__str__.<locals>.<genexpr>	%s:{
%s
}join	enumerater/   rD   __name__)rB   	debug_strr   r   r   __str__      
zDistributedVariable.__str__c                 C   r   )Nr   c                 s   r   )z  %d: %rNr   r   r   r   r   r      r   z/DistributedVariable.__repr__.<locals>.<genexpr>r   r   )rB   
debug_reprr   r   r   __repr__   r   zDistributedVariable.__repr__c                 C   s    t | j|}t|| jd udS )Nr   )copydeepcopyr/   r   r1   )rB   memocopied_variablesr   r   r   __deepcopy__   s   
z DistributedVariable.__deepcopy__)NNT)FNrZ   )&r   
__module____qualname____doc__r?   propertyr   r#   r   r^   r`   ra   r\   rc   rh   ri   rk   ro   rp   rq   rr   rs   rt   ru   rv   rz   r{   r}   r   r   staticmethodr   r   r   r   r   r   __classcell__r   r   rC   r   r   "   sJ    .


r   Fc                 C   s"   |rt dtj|  |||d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_tensorr\   )varr   r#   r   r   r   r   _tensor_conversion  s   r   )NNF)r   r   r2   tensorflow.python.distributer   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r	   r
   rH   BaseResourceVariabler   r   #register_tensor_conversion_functionr   r   r   r   <module>   s$    
`
