o
    Mez&                     @   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s t d	K |d
7 t| sot| tjrNtdt j| d|d} t| r\J | j|sdJ |pi| jj}|pn| j}W d
   n1 syw   Y  W d
   n1 sw   Y  |sJ |sJ tj||d
|dd
d}W d
   n1 sw   Y  W d
   n1 sw   Y  | |||||fS )zFInfer shape and dtype from initial_value and create a variable handle.VariableFZ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_tensorr   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"   KD:\Projects\ConvertPro\env\Lib\site-packages\keras/dtensor/lazy_variable.py$_infer_shape_dtype_and_create_handle!   sX   

 .r$   c                       sR   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J |d u sJ |d u sJ |d u rtdt|tjr4t|dr4|jjr4td| d| d|
d urHt	|
sHt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_eagerly
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#   r2   ^   sj   

zLazyInitVariable.__init__c              
   C   s   t j| jddde t | jG t d2 t| jr!|  }n| j}|j| j	s8t
d|j d| j	 d| j|jju sAJ W d    n1 sKw   Y  W d    n1 sZw   Y  t| j| W d    d S 1 sqw   Y  d S )Nr   Fr   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   r0   Zcolocate_with_handler   _initial_valuer   r   _shaper-   _dtyper   r   r   Zassign_variable_op)r3   r   r"   r"   r#   
initialize   s*   

 "zLazyInitVariable.initializec                    s   t | jr	|  }t|j t|| j| j| j\}}}}}}|   W d    n1 s.w   Y  t	 j
di d| jd|d|d|d| jd| jd| jd| jd	| jd
|d|dd d|dd dd dd dd  d S )Nr*   r   r   r!   r+   r(   r,   r'   r   r   r   r)   r   Zinitializer_opZis_initialized_opZcached_valuer5   r"   )r   r:   r   Zdevicer$   r;   r<   r0   r=   r1   r2   Z
_trainableZ_synchronizationZ_constraintZ_aggregationZ_distribute_strategy)r3   r   r   r   r!   r   r   r7   r"   r#   create_and_initialize   sd   

	
z&LazyInitVariable.create_and_initialize)NNNTNNNNNNNNNN)__name__
__module____qualname____doc__r2   r=   r>   __classcell__r"   r"   r7   r#   r%   T   s&    Ur%   c                 K   s(   t tddr| di |S tdi |S )NdisabledFr"   )getattr_DISABLE_LAZY_VARIABLE_INITr%   )Znext_creatorr6   r"   r"   r#   _lazy_init_variable_creator   s   rG   c                   c   s8    t t d V  W d    d S 1 sw   Y  d S )N)r   Zvariable_creator_scoperG   r"   r"   r"   r#   lazy_init_scope   s   "rH   c                  c   s0    zt tdd} dt_d V  W | t_d S | t_w )NrD   FT)rE   rF   rD   )Zexisting_valuer"   r"   r#   disable_init_variable_creator   s   rI   )rB   	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
   localrF   r$   ZBaseResourceVariabler%   rG   contextmanagerrH   rI   r"   r"   r"   r#   <module>   s(   3 
