o
    i e&                     @   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 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)tensor)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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   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 r0   N/var/www/myenv/lib/python3.10/site-packages/keras/src/dtensor/lazy_variable.py$_infer_shape_dtype_and_create_handle"   sX   

 .r2   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aggregationin_graph_mode)r   executing_eagerly
ValueErrorr#   r   Tensorhasattrr4   building_functionr"   type_namer2   super__init__)selfr   r8   collectionsvalidate_shapecaching_devicer   r   variable_defimport_scoper6   r5   r9   r:   r   kwargsr/   r+   r,   	__class__r0   r1   rD   _   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   rB   colocate_with_handler"   _initial_valuer   r(   _shaper=   _dtyper   r)   r   assign_variable_op)rE   r   r0   r0   r1   
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 )Nr8   r   r   r/   r9   r6   r:   r5   r   r,   r+   r7   r   initializer_opis_initialized_opcached_valuerH   r0   )r"   rP   r   devicer2   rQ   rR   rB   rT   rC   rD   
_trainable_synchronization_constraint_aggregation_distribute_strategy)rE   r   r   r   r/   r+   r,   rL   r0   r1   create_and_initialize   sd   

	
z&LazyInitVariable.create_and_initialize)NNNTNNNNNNNNNN)__name__
__module____qualname____doc__rD   rT   r^   __classcell__r0   r0   rL   r1   r3   U   s&    Ur3   c                 K   s(   t tddr| di |S tdi |S )NdisabledFr0   )getattr_DISABLE_LAZY_VARIABLE_INITr3   )next_creatorrK   r0   r0   r1   _lazy_init_variable_creator   s   rh   c                   c   s8    t t d V  W d    d S 1 sw   Y  d S )N)r   variable_creator_scoperh   r0   r0   r0   r1   lazy_init_scope   s   "rj   c                  c   s0    zt tdd} dt_d V  W | t_d S | t_w )Nrd   FT)re   rf   rd   )existing_valuer0   r0   r1   disable_init_variable_creator   s   rl   )rb   	threadingtensorflow.core.frameworkr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   tensorflow.python.trackabler	   r$   tensorflow.python.utilr
   r   localrf   r2   BaseResourceVariabler3   rh   contextmanagerrj   rl   r0   r0   r0   r1   <module>   s*   3 
