o
    ?e(                     @   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   g/home/www/facesmatcher.com/pyenv/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)Zskip_on_eagerz%s_%dc                 S   r   r   )initializerr   r   r   r   r   H   r   	trainableshapedtyper   synchronization
constraintaggregationZdistribute_strategyname	unique_idhandle_nameZgraph_elementinitial_valueZinitializer_opZis_initialized_opZcached_valueZcaching_deviceZis_variablesTr   )!r   Z#executing_eagerly_outside_functions
ValueError
_variablesZpack_eager_tensors_packed_handleweakrefrefr   Z_distributed_container_device_to_handle_primary_handleZ
init_scopeZ
name_scopeZname_from_scope_nameuid
_unique_idr   executing_eagerlyr$   r   groupsuper__init__r   r   r   r   r   r    Z_distribute_strategyr!   _graph_element)selfr
   r   r   r!   r#   r$   r   	__class__r   r   r1   /   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_distributedr+   r   enclosing_tpu_contextr   r.   r&   r   variables_libZVariableSynchronizationZON_READr'   Z_handle_namesplitZget_replicated_var_handler-   r   canonicalizecurrentr*   get)r3   Ztpu_contextZis_mirroredZhandlesZ	is_packedZcommon_namer   r   r   r   r   ]   s0   


zDistributedVariable.handlec                       t  r
| jd jS t jS Nr   )r   r7   r&   r!   r0   r3   r4   r   r   r!   y      zDistributedVariable.namec                    r>   r?   )r   r7   r&   r   r0   r@   r4   r   r   r      rA   zDistributedVariable.initializerc                 C   s8   t |g |  W  d    S 1 sw   Y  d S N)r   Zcontrol_dependencies
read_value)r3   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 rB   )r'   r   r7   r   r8   r   ZNullContextmanagerr   r;   r<   r*   r   r+   )r3   r   r   r   r   _device_scope   s   


z!DistributedVariable._device_scopec                 C   s   |   S rB   )rC   r@   r   r   r   value   s   zDistributedVariable.valuec                    s6   |    t  W  d    S 1 sw   Y  d S rB   )rF   r0   rC   r@   r4   r   r   rC      s   
$zDistributedVariable.read_valueNTc                    >   |    t ||||W  d    S 1 sw   Y  d S rB   )rF   r0   
assign_subr3   deltause_lockingr!   rC   r4   r   r   rI         
$zDistributedVariable.assign_subc                    rH   rB   )rF   r0   
assign_addrJ   r4   r   r   rN      rM   zDistributedVariable.assign_addc                    rH   rB   )rF   r0   assign)r3   rG   rL   r!   rC   r4   r   r   rO      rM   zDistributedVariable.assignc                    <   |    t |||W  d    S 1 sw   Y  d S rB   )rF   r0   scatter_subr3   Zsparse_deltarL   r!   r4   r   r   rQ         
$zDistributedVariable.scatter_subc                    rP   rB   )rF   r0   scatter_addrR   r4   r   r   rT      rS   zDistributedVariable.scatter_addc                    rP   rB   )rF   r0   scatter_mulrR   r4   r   r   rU      rS   zDistributedVariable.scatter_mulc                    rP   rB   )rF   r0   scatter_divrR   r4   r   r   rV      rS   zDistributedVariable.scatter_divc                    rP   rB   )rF   r0   scatter_minrR   r4   r   r   rW      rS   zDistributedVariable.scatter_minc                    rP   rB   )rF   r0   scatter_maxrR   r4   r   r   rX      rS   zDistributedVariable.scatter_maxc                    rP   rB   )rF   r0   scatter_updaterR   r4   r   r   rY      rS   z"DistributedVariable.scatter_updatec                    rP   rB   )rF   r0   batch_scatter_updaterR   r4   r   r   rZ      rS   z(DistributedVariable.batch_scatter_updatec                    rP   rB   )rF   r0   scatter_nd_subr3   indicesZupdatesr!   r4   r   r   r[      rS   z"DistributedVariable.scatter_nd_subc                    rP   rB   )rF   r0   scatter_nd_addr\   r4   r   r   r^      rS   z"DistributedVariable.scatter_nd_addc                    rP   rB   )rF   r0   scatter_nd_updater\   r4   r   r   r_      rS   z%DistributedVariable.scatter_nd_updatec                    :   |    t ||W  d    S 1 sw   Y  d S rB   )rF   r0   sparse_readr3   r]   r!   r4   r   r   ra         
$zDistributedVariable.sparse_readc                    r`   rB   )rF   r0   	gather_ndrb   r4   r   r   rd      rc   zDistributedVariable.gather_ndc                 C   s
   ~ t d)Nz,DistributedVariable doesn't support to_proto	TypeError)r3   Zexport_scoper   r   r   to_proto   s   zDistributedVariable.to_protoc                 C   s   t d)Nz.DistributedVariable doesn't support from_protore   )Zvariable_defZimport_scoper   r   r   
from_proto   s   zDistributedVariable.from_protoc                 C   s   t  jr| jd jS |  S r?   )r   Zget_default_graphZ	finalizedr&   r2   rC   r@   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 rB   )rF   r0   _strided_slice_assign)r3   argskwargsr4   r   r   rj      rM   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&   r5   __name__)r3   Z	debug_strr   r   r   __str__      
zDistributedVariable.__str__c                 C   rm   )Nrn   c                 s   ro   )z  %d: %rNr   rp   r   r   r   rr      rs   z/DistributedVariable.__repr__.<locals>.<genexpr>rt   ru   )r3   Z
debug_reprr   r   r   __repr__   rz   zDistributedVariable.__repr__c                 C   s    t | j|}t|| jd udS )Nr   )copydeepcopyr&   r   r'   )r3   memoZcopied_variablesr   r   r   __deepcopy__   s   
z DistributedVariable.__deepcopy__)NNT)FNrB   )&rx   
__module____qualname____doc__r1   propertyr   r!   r   rE   rF   rG   rC   rI   rN   rO   rQ   rT   rU   rV   rW   rX   rY   rZ   r[   r^   r_   ra   rd   rg   staticmethodrh   ri   rj   ry   r{   r   __classcell__r   r   r4   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   Zconvert_to_tensorrC   )varr   r!   r   r   r   r   _tensor_conversion  s   r   )NNF)r   r|   r(   Ztensorflow.python.distributer   r   r   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.opsr   r	   r
   r9   ZBaseResourceVariabler   r   Z#register_tensor_conversion_functionr   r   r   r   <module>   s$    
`
