a
    Sic&                     @   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b}t |}d|t  f }t j}tjtjjt	d| gdd}t 
 d|i t d	 |d
n t| st| tjrtdt j| d|d} t| rJ | j|sJ |p| jj}|p| j}W d
   n1 s0    Y  W d
   n1 s0    Y  |s&J |s0J tj||d
|dd
d}W d
   n1 s\0    Y  W d
   n1 s|0    Y  | |||||fS )zFInfer shape and dtype from initial_value and create a variable handle.VariableFskip_on_eagerz%s_%dzloc:@%s)s)list_classInitializerNzSCheckpointInitialValue is not supported to be the initial value of a lazy variable.initial_value)namedtype)shaper   shared_namer   
graph_moder   )r   
name_scopename_from_scope_nameuidNullContextmanagerr   	AttrValue	ListValuer	   as_bytesget_default_graph_attr_scopecallable
isinstance	trackableCheckpointInitialValueNotImplementedErrorconvert_to_tensorr   is_compatible_withr   
base_dtyper   %_variable_handle_from_shape_and_dtype)	r   r   r   r   handle_name	unique_iddevice_context_managerattrhandle r/   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/dtensor/lazy_variable.py$_infer_shape_dtype_and_create_handle!   sJ    
H

Br1   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J |d u sJ |d u s$J |d u r4tdt|tjrht|drh|jjrhtd| d| d|
d u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aggregationin_graph_mode)r   executing_eagerly
ValueErrorr"   r   Tensorhasattrr3   building_functionr!   type_namer1   super__init__)selfr   r7   collectionsvalidate_shapecaching_devicer   r   variable_defimport_scoper5   r4   r8   r9   r   kwargsr.   r*   r+   	__class__r/   r0   rC   ^   sj    
zLazyInitVariable.__init__c              
   C   s   t j| jddd t | j t dd t| jrB|  }n| j}|j| j	spt
d|j d| j	 d| j|jju sJ W d    n1 s0    Y  W d    n1 s0    Y  t| j| W d    n1 s0    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   rA   colocate_with_handler!   _initial_valuer   r'   _shaper<   _dtyper   r(   r   assign_variable_op)rD   r   r/   r/   r0   
initialize   s"    

NzLazyInitVariable.initializec                    s   t | jr|  }t|j8 t|| j| j| j\}}}}}}|   W d    n1 s\0    Y  t	 j
| j|||| j| j| j| j| j||d |d d d d d d S )N)r7   r   r   r.   r8   r5   r9   r4   r   r+   r*   r6   r   initializer_opis_initialized_opcached_valuerG   )r!   rO   r   devicer1   rP   rQ   rA   rS   rB   rC   
_trainable_synchronization_constraint_aggregation_distribute_strategy)rD   r   r   r   r.   r*   r+   rK   r/   r0   create_and_initialize   sB    
&z&LazyInitVariable.create_and_initialize)NNNTNNNNNNNNNN)__name__
__module____qualname____doc__rC   rS   r]   __classcell__r/   r/   rK   r0   r2   T   s$                 Ur2   c                 K   s,   t tddr| f i |S tf i |S d S )NdisabledF)getattr_DISABLE_LAZY_VARIABLE_INITr2   )next_creatorrJ   r/   r/   r0   _lazy_init_variable_creator   s    rg   c                   c   s4   t t d V  W d    n1 s&0    Y  d S )N)r   variable_creator_scoperg   r/   r/   r/   r0   lazy_init_scope   s    ri   c                  c   s0   z"t tdd} dt_d V  W | t_n| t_0 d S )Nrc   FT)rd   re   rc   )existing_valuer/   r/   r0   disable_init_variable_creator   s
    rk   )ra   	threadingtensorflow.core.frameworkr   tensorflow.python.eagerr   tensorflow.python.frameworkr   tensorflow.python.opsr   r   r   tensorflow.python.trackabler   r#   tensorflow.python.utilr	   r
   localre   r1   BaseResourceVariabler2   rg   contextmanagerri   rk   r/   r/   r/   r0   <module>   s&   3 
