B
    dz&                 @   s   d 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 e add ZG dd de
jZdd Zejdd Zejdd ZdS )zILazily initialized variables, useful for creating a symbolic Keras model.    N)attr_value_pb2)context)ops)gen_resource_variable_ops)resource_variable_ops)variable_scope)base)compat)tf_contextlibc       	      C   s@  t j|ddd}t |}d|t  f }t j}tjtjjt	d| gdd}t 
 d|i t d	x |d
d t| st| tjrtdt j| d|d} t| rt| j|st|p| jj}|p| j}W d
Q R X W d
Q R X |st|sttj||d
|dd
d}W d
Q R X W d
Q R X | |||||fS )zFInfer shape and dtype from initial_value and create a variable handle.VariableF)skip_on_eagerz%s_%dzloc:@)s)list_classInitializerNzSCheckpointInitialValue is not supported to be the initial value of a lazy variable.initial_value)namedtype)shaper   Zshared_namer   Z
graph_moder   )r   
name_scopeZname_from_scope_nameuidZNullContextmanagerr   Z	AttrValueZ	ListValuer	   as_bytesZget_default_graphZ_attr_scopecallable
isinstance	trackableZCheckpointInitialValueNotImplementedErrorZconvert_to_tensorAssertionErrorr   is_compatible_withr   
base_dtyper   Z%_variable_handle_from_shape_and_dtype)	r   r   r   r   handle_name	unique_idZdevice_context_managerattrhandle r#   M/var/www/html/venv/lib/python3.7/site-packages/keras/dtensor/lazy_variable.py$_infer_shape_dtype_and_create_handle!   s<    


r%   c                   s6   e Zd ZdZd
 fdd	Zdd Z fdd	Z  ZS )LazyInitVariablea  Lazily initialized variables.

    The major use case for this class is to serve as a memory efficient
    alternative for tf.Variable. The resource handle of this class is point to
    nothing, which mean it will raise error when its value is fetched in a eager
    context. Having said that, it will perform like a normal tf.Variable when
    using with graph tensor, like KerasTensor produced from tf.keras.Input.
    NTc                s   t  st|d kst|d ks$t|d kr4tdt|tjrht|drh|jj	rhtd| d| d|
d k	rt
|
stdt|
 d|
 || _t||||\}}}}}}t j||||||||
|d |||dd	 d S )
NzThe `initial_value` arg to `tf.Variable` must be specified except when you are not providing a `variable_def`. You provided neither.graphzArgument `initial_value` (zS) could not be lifted out of a `tf.function`. (Tried to create variable with name='a8  '). To avoid this error, when constructing `tf.Variable`s inside of `tf.function` you can create the `initial_value` tensor in a `tf.init_scope` or pass a callable `initial_value` (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`). Please file a feature request if this restriction inconveniences you.zDArgument `constraint` must be None or a callable. a callable. Got a z:  F)distribute_strategyr   r   r   r   r    r   
constraintr"   graph_element	trainablesynchronizationaggregationZin_graph_mode)r   Zexecuting_eagerlyr   
ValueErrorr   r   ZTensorhasattrr'   Zbuilding_functionr   type_namer%   super__init__)selfr   r+   collectionsZvalidate_shapecaching_devicer   r   Zvariable_defZimport_scoper)   r(   r,   r-   r   kwargsr"   r   r    )	__class__r#   r$   r3   ^   s@    
zLazyInitVariable.__init__c             C   s   t j| jddd t | jp t dZ t| jrB|  }n| j}|j| j	spt
d|j d| j	 d| j|jjkstW d Q R X W d Q R X t| j| W d Q R X d S )Nr   F)r   r   z;In this `tf.Variable` creation, the initial value's shape (zC) is not compatible with the explicitly supplied `shape` argument (z).)r   r   r1   Zcolocate_withZ_handler   _initial_valuer   r   _shaper.   _dtyper   r   r   r   Zassign_variable_op)r4   r   r#   r#   r$   
initialize   s    

&zLazyInitVariable.initializec                s   t | jr|  }t|j. t|| j| j| j\}}}}}}|   W d Q R X t	 j
| j|||| j| j| j| j| j||d |d d d d d d S )N)r+   r   r   r"   r,   r)   r-   r(   r   r    r   r*   r   Zinitializer_opZis_initialized_opZcached_valuer6   )r   r9   r   Zdevicer%   r:   r;   r1   r<   r2   r3   Z
_trainableZ_synchronizationZ_constraintZ_aggregationZ_distribute_strategy)r4   r   r   r   r"   r   r    )r8   r#   r$   create_and_initialize   s0    
z&LazyInitVariable.create_and_initialize)NNNTNNNNNNNNNN)__name__
__module____qualname____doc__r3   r<   r=   __classcell__r#   r#   )r8   r$   r&   T   s"                Fr&   c             K   s$   t tddr| f |S tf |S d S )NdisabledF)getattr_DISABLE_LAZY_VARIABLE_INITr&   )Znext_creatorr7   r#   r#   r$   _lazy_init_variable_creator   s    
rF   c            	   c   s    t t d V  W d Q R X d S )N)r   Zvariable_creator_scoperF   r#   r#   r#   r$   lazy_init_scope   s    rG   c              c   s*   zt tdd} dt_d V  W d | t_X d S )NrC   FT)rD   rE   rC   )Zexisting_valuer#   r#   r$   disable_init_variable_creator   s
    
rH   )rA   	threadingZtensorflow.core.frameworkr   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   Ztensorflow.python.opsr   r   r   Ztensorflow.python.trackabler   r   Ztensorflow.python.utilr	   r
   localrE   r%   ZBaseResourceVariabler&   rF   contextmanagerrG   rH   r#   r#   r#   r$   <module>   s"   3 