
    AVhP)                     8   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 ddlmZ ddlmZ  G d dej.                        Z G d dej2                        Z edg        G d dej6                               Zy)zDTensor variable and saveable.    N)api)layout)context)def_function)dtypes)errors_impl)ops)math_ops)resource_variable_ops)base)saveable_object)	tf_exportc                   (     e Zd ZdZ	 	 d fd	Z xZS )	DSaveSpeczCDTensor SaveSpec that additionaly captures global_shape and layout.c                 J    t         |   |||||       || _        || _        y )N)tensor
slice_specnamedtypedevice)super__init__global_shaper   )	selfr   r   r   r   r   r   r   	__class__s	           T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/dtensor/python/d_variable.pyr   zDSaveSpec.__init__$   s7     
G   %DDK    )NN)__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r   !   s    K  r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )_DVariableSaveablez8Class for defining how to save/restore DTensor variable.c                 j    t        j                  j                        5  t        j                        d d d         _         _        fd}t        j                  j                  j                  j                                fd}j                  j                         }t        t         ;  d t        | |dg|z  t        j                  j!                  j                  j                         d             ||g|z  t        j                  j!                  j                  j                         d            j"                  j%                          j'                        rt(        j*                  nj,                  j                        g|       y # 1 sw Y   oxY w)Nc                     t        j                  j                        5  t        j                  | |      cd d d        S # 1 sw Y   y xY wN)r	   r   r   pack)tensorsr   	dvariables     r   r)   z)_DVariableSaveable.__init__.<locals>.pack@   s8    ::i&&' )xx() ) )s   AA
c                     j                   j                         j                         dk7  rqt        j                         r8t        j                  t        j                  j                                     } n5t        j                  j                               } nj                         } j                  |       r$t        j                  | t        j                        S | S )NCPU)meshdevice_typeupperr   executing_eagerlyr   r)   unpack
read_valuecopy_to_meshshould_castr
   castr   bfloat16)host_dtensorr+   host_layoutoriginal_layoutr   s    r   get_host_dtensorz5_DVariableSaveable.__init__.<locals>.get_host_dtensorG   s    				)	)	+	1	1	3u	< $$&jj--/0+?, )))*>*>*@+N, ++-!!,/ mmL&//: C5ACr    r   )rank)r   r   r   r   r   r   r   )r	   r   r   fetch_layout_original_layout
_dvariable
layout_libLayoutsharding_specsr.   	host_meshnum_local_devicesr   r%   r   r   
replicatedshape	to_stringr5   r   r7   r   )	r   r+   r   r)   r;   rE   r9   r:   r   s	   ``    @@r   r   z_DVariableSaveable.__init__9   s   	I$$	% 4((3o4 ,DDO) ##O$B$B$3$8$8$B$B$DFKC (,,>>@	
d,''8 8 * 1 1 < <$3$8$8$B$B$D1 != !NO 4&#44$++66-22<<>Q 7 HI '__ #,,.##I. oo4=OO '')	
" 	';4 4s   F((F2c                 l    | j                   j                  xr |j                  t        j                  k(  S )a  Returns True if v has float32 dtype and is intructed to save as bf16.

    Args:
      v : The variable that determines whether to cast.

    Returns:
      True if current savable DVariable is instructed to save as bfloat16 and
        the variable has dtype float32.
    )r@   save_as_bf16r   r   float32)r   vs     r   r5   z_DVariableSaveable.should_castl   s&     ??''EAGGv~~,EEr   c                 v    |\  }t         j                   fd       } j                  j                  j	                         j                         dk7  r ||      } j                  j                   j                  j                  r/t        j                  | j                  j                              S |      S )z+Restores the same value into all variables.c                     t        j                  j                  j                        5  t        j                  | j
                        cd d d        S # 1 sw Y   y xY wr(   )r	   r   r@   r   r4   r?   )tr   s    r   _restorez,_DVariableSaveable.restore.<locals>._restore|   sD    ::doo,,- :4#8#89: : :s    AAr-   r   )r   functionr?   r.   r/   r0   r@   assignrJ   r
   r6   r   )r   restored_tensorsrestored_shapesr   rP   s   `    r   restorez_DVariableSaveable.restorex   s    GF: : !!--/5575@f??!!>Boo	?fDOO$9$9:# #!# #r   )r   r   r    r!   r   r5   rV   r"   r#   s   @r   r%   r%   6   s    @1f
F#r   r%   zexperimental.dtensor.DVariable)v1c                   b     e Zd ZdZdd fd
Zed        Zej                  d        Zd Z xZ	S )	DVariablezA replacement for tf.Variable which follows initial value placement.

    The class also handles restore/save operations in DTensor. Note that,
    DVariable may fall back to normal tf.Variable at this moment if
    `initial_value` is not a DTensor.
  NrQ   c                   |j                  dd      }|j                  dd      }t        |      r|}t        t	        |      t
        j                        r|j                  }t        t	        |      t        j                        rJ|r|st        d      t        j                  ||t        j                  |dgt        |      z              }n |       }t        |t        j                         r|j"                  }t%        j&                  ||      }|j(                  }d	| _        t%        j(                  |      5  t-        j.                         rt        j0                  |      r=t        j2                  |      }	|#||	k7  rt5        j6                  ddd
|	 d| d      |	}n0|t        j8                  ||      }nt5        j6                  ddd      || _        t        j<                  |j>                        5  tA        tB        |   |g|d|i| ddd       n1|t        j8                  ||      }tA        tB        |   |g|d|i| ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z4Overrides tf.Variable to fix VarHandleOp placements.r   NrG   z)Expected shape and layout to be not None.r   )rG   offset)
shard_inforQ   Fz:Conflicting layout are provided for initial value layout (z) and variable (z).z6Neither layout nor DTensor initial value are provided.r   )#popgetcallable
issubclasstype	functoolspartialfunc	trackableCheckpointInitialValueCallable
ValueErrorr   call_with_layout	ShardInfolen
isinstanceCheckpointInitialValuewrapped_valuer	   convert_to_tensorr   _save_as_bf16r   r1   
is_dtensorr>   r   InvalidArgumentErrorrelayoutr   default_meshr.   r   rY   r   )r   initial_valuer   argskwargsr   rG   	unwrappedvariable_devicevalue_layoutr   s             r   r   zDVariable.__init__   se    ZZ$'FJJw%Ei	D'):):	;!&&	 
DOY%M%M	NFFG
G ,, **QC#e*$467 & -!A!AB#11m))-uEM#**OD 
O	$  9 
	"	"	$>>-())-8,Fl$:22!!-.>vhbJ   &,,}f=-00F 
 fkk* 	
	4
)"*/39	 	 ,,}f=-i'	9 	9(-	917	9? 9  9.	 	/ 9  9s%   ;B=I!8I:I!I	I!!I*c                     | j                   S r(   )ro   r   s    r   rJ   zDVariable.save_as_bf16   s    r   c                 P    |xr | j                   t        j                  k(  | _        y)z#Enables saving float32 as bfloat16.N)r   r   rK   ro   )r   rJ   s     r   rJ   zDVariable.save_as_bf16   s     &F$***FDr   c                 V    t         j                  t        j                  t        |       iS r(   )re   VARIABLE_VALUE_KEYrb   rc   r%   r{   s    r    _gather_saveables_for_checkpointz*DVariable._gather_saveables_for_checkpoint   s&    $$0$7 r   )
r   r   r    r!   r   propertyrJ   setterr   r"   r#   s   @r   rY   rY      sI     26 ]9~   G Gr   rY   )r!   rb   tensorflow.dtensor.pythonr   r   rA   tensorflow.python.eagerr   r   tensorflow.python.frameworkr   r   r	   tensorflow.python.opsr
   r   tensorflow.python.trackabler   re   !tensorflow.python.training.savingr    tensorflow.python.util.tf_exportr   SaveSpecr   SaveableObjectr%   ResourceVariablerY    r   r   <module>r      s    %  ) : + 0 . 3 + * 7 9 = 6(( *V#77 V#r +3t%66 t 4tr   