
    6bi&                     (   S r SSKrSSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  SS	K	Jr  SS
KJr  SSKJr  SSKJr  \R&                  " 5       qS r " S S\R,                  5      rS r\R2                  S 5       r\R2                  S 5       rg)zJLazily initialized variables, useful for creating symbolic TF-Keras model.    N)attr_value_pb2)context)ops)tensor)gen_resource_variable_ops)resource_variable_ops)variable_scope)base)compat)tf_contextlibc                 h   [         R                  " USSS9 n[         R                  " U5      nSU[         R                  " 5       4-  n[         R                  n[
        R                  " [
        R                  R                  [        R                  " SU 35      /S9S9n[         R                  " 5       R                  SU05         [         R                  " S	5         U" S
5         [        U 5      (       d  [        U [        R                  5      (       a  [!        S5      e[         R"                  " U SUS9n [        U 5      (       a   eU R$                  R'                  U5      (       d   eU=(       d    U R(                  R*                  nU=(       d    U R$                  nS
S
S
5        S
S
S
5        U(       d   eU(       d   e[,        R.                  " UUS
USS
S9nS
S
S
5        S
S
S
5        XUWWW4$ ! , (       d  f       NX= f! , (       d  f       Na= f! , (       d  f       N?= f! , (       d  f       NH= f)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handles	            \/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/dtensor/lazy_variable.py$_infer_shape_dtype_and_create_handler3   "   s   	j	>$..t4{CGGI66	
 "%!7!7''))33??U;-#89: 4 

 ""$00(D1AB.0Ft0L..!%y'G'G  2@  %($9$9%O5%M  (6666(..AA%HHHH!C]%8%8%C%CE!8]%8%8E! 1M.$ L5L5%KK $$"& + C 
?\ YFFA 1M0L.. CB 
?	>sU   B.H#H	H%B9G0	H&3HH#0
G>:H
HH
H 	H##
H1c                   ^   ^  \ rS rSrSr              SU 4S jjrS rU 4S jrSrU =r	$ )LazyInitVariableU   a  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.
c                   > [         R                  " 5       (       d   eUb   eUb   eUc  [        S5      e[        U[        R
                  5      (       a>  [        US5      (       a-  UR                  R                  (       a  [        SU SU S35      eU
b*  [        U
5      (       d  [        S[        U
5       SU
 35      eX`l        [        XXv5      u  nnnnnn[        TU ]9  UUUUUUUU
US UUUSS	9  g )
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-   
constraintr1   graph_element	trainablesynchronizationaggregationin_graph_mode)r   executing_eagerly
ValueErrorr%   r   Tensorhasattrr8   building_functionr$   type_namer3   super__init__)selfr   r<   collectionsvalidate_shapecaching_devicer   r   variable_defimport_scoper:   r9   r=   r>   r   kwargsr1   r-   r.   	__class__s                      r2   rH   LazyInitVariable.__init___   sG   $ ((****###%%% 8  }fmm44w//##55,]O <88<v >2
2  !(:*>*>%%)*%5$6c*G 
 
 1%
	

 	 3'#!+# 	 	
    c           
         [         R                  " U R                  SSS9   [         R                  " U R                  5         [         R                  " S5         [        U R                  5      (       a  U R                  5       nOU R                  nUR                  R                  U R                  5      (       d&  [        SUR                   SU R                   S35      eU R                  UR                  R                  L d   e S S S 5        S S S 5        [        R                  " U R                  W5        S S S 5        g ! , (       d  f       N@= f! , (       d  f       NI= f! , (       d  f       g = f)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   rF   colocate_with_handler$   _initial_valuer   r*   _shaperA   _dtyper   r+   r   assign_variable_op)rI   r   s     r2   
initializeLazyInitVariable.initialize   s   ^^DJJ
%H""4<<0#..2OD//00$($7$7$9M$($7$7M$**==dkkJJ$""/"5"5!6 7%%)[[M5  {{m&9&9&D&DDDD 3P0 &88m IH2O2O00 IHs<   !E#EB-EE)E#
EE
E 	E##
E1c                   > [        U R                  5      (       a  U R                  5       n[        R                  " WR                  5         [	        XR
                  U R                  U R                  5      u  nnnnnnU R                  5         S S S 5        [        TU ])  U R                  WWWU R                  U R                  U R                  U R                  U R                  WWS US S S S S9  g ! , (       d  f       Nh= f)N)r<   r   r   r1   r=   r:   r>   r9   r   r.   r-   r;   r   initializer_opis_initialized_opcached_valuerL   )r$   rV   r   devicer3   rW   rX   rF   rZ   rG   rH   
_trainable_synchronization_constraint_aggregation_distribute_strategy)rI   r   r   r   r1   r-   r.   rP   s          r2   create_and_initialize&LazyInitVariable.create_and_initialize   s    D''(( //1MZZ,,- 5{{DKK OO . 	oo 11'')) $ 9 9#'"# 	 	
 .-s   AC11
C?)rF   )NNNTNNNNNNNNNN)
__name__
__module____qualname____firstlineno____doc__rH   rZ   rf   __static_attributes____classcell__)rP   s   @r2   r5   r5   U   sJ      Q
j(#
 #
rR   r5   c                 T    [        [        SS5      (       a  U " S0 UD6$ [        S0 UD6$ )NdisabledF )getattr_DISABLE_LAZY_VARIABLE_INITr5   )next_creatorrO   s     r2   _lazy_init_variable_creatorru      s.    *J>>%f%%)&))rR   c               #   |   #    [         R                  " [        5         S v   S S S 5        g ! , (       d  f       g = f7f)N)r	   variable_creator_scoperu   rq   rR   r2   lazy_init_scoperx      s#     		.	./J	K 
L	K	Ks   <+	<
9<c               #      #     [        [        SS5      n S[        l        S v   U [        l        g ! W [        l        f = f7f)Nrp   FT)rr   rs   rp   )existing_values    r2   disable_init_variable_creatorr{      s7     > !<j%P/3#,/=#,~#,s   A  0 A =A )rl   	threadingtensorflow.core.frameworkr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r	   tensorflow.python.trackabler
   r&   tensorflow.python.utilr   r   localrs   r3   BaseResourceVariabler5   ru   contextmanagerrx   r{   rq   rR   r2   <module>r      s    Q  5 + + . ; 7 0 9 ) 0'oo/ 0GfV
,AA V
r*  
 > >rR   