
    AVhP                        d Z ddlmZmZm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 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      Z+ G d dejX                        Z- G d d ej\                        Z/ e'j`                  dd!"       e*d#g $       G d% d&ejb                                      Z2y)'zDTensor Checkpoint.

Note that this module contains deprecated functionality, and the DTensor related
checkpoint has been integrated with tf.train.Checkpoint. It can be used out of
the box to save and restore dtensors.
    )DictListOptionalN)trackable_object_graph_pb2)api)
d_variable)gen_dtensor_ops)layout)save_restore)
checkpoint)checkpoint_options
graph_view)restore)context)constant_op)errors_impl)ops)	array_ops)base)data_structures)py_checkpoint_reader)saveable_object)saveable_object_util)deprecation)nest)	tf_exportc            	           e Zd ZdZdej
                  deej                     fdZ		 dde
deej                     deej                     fd	Z	 dde
deej                     dee
ej                  f   fd
Zy)_DSaverz<A single device saver that places tensors on DTensor Device.meshsaveable_objectsc                      || _         || _        y N)_saveable_objects_mesh)selfr    r!   s      V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/dtensor/python/d_checkpoint.py__init__z_DSaver.__init__6   s    -DDJ    Nfile_prefixoptionsreturnc           	         ||j                   t        d      ~g }g }g }| j                  D ]  }|j                  D ]  }|j                  }|t        j                         |j                  k7  rt        j                  |g| j                  j                         j                         z  t        j                  j                  | j                  j                         |j                  j                               }|j#                  |j$                         |j#                  |       |j#                  |j&                           t)        j*                  | j                  ||||      S )a  Saves the saveable objects to a checkpoint with `file_prefix`.

    Also query the generated shards from the distributed DTensor SaveV2 ops and
    do a MergeV2 on those. Each op here is backed by a global_barrier to avoid
    racing from multiple clients.

    Args:
      file_prefix: A string or scalar string Tensor containing the prefix to
        save under.
      options: Optional `CheckpointOptions` object. This is unused in DTensor.

    Returns:
      An `Operation`, or None when executing eagerly.
    HSpecified experimental_io_device in DTensor checkpoint is not supported.rank)experimental_io_device
ValueErrorr$   specstensorr   device_namedevicepackr%   	host_meshnum_local_devicesr
   Layout
replicatedshaper0   appendname
slice_specr   sharded_save)	r&   r*   r+   tensor_namestensorstensor_slicessaveablespecr4   s	            r'   savez_DSaver.save;   sB   & w==I
T  	LGM** 0.. 0$ __$++- XXvh"jj224FFHI$mm66"&**"6"6"8'-||'8'8 7 :;F
 

dii
(
..
 


t
/!00$ $$TZZl%2G= =r)   c                    ||j                   t        d      ~g }g }| j                  D ]z  }g }|j                  |       |j                  D ]T  }|j                  |j
                         t        |t        j                        rI|j                  |j
                  |j                  |j                  |j                  |j                  f       t        |t        j                        s|j                  |j
                  |j                  |j                  t        j                  j!                  | j"                  j%                         |j&                  j(                  j*                        j-                         |j&                  j(                  j/                         f       W } t1        | \  }}	}
}}t3        j4                  t7        j8                               5  t;        j<                  |||	|||
      }ddd       t?        j@                  |      }i }t1        | j                  |      D ]%  \  }}|jC                  |d      ||j
                  <   ' |S # 1 sw Y   axY w)ae  Restore the saveable objects from a checkpoint with `file_prefix`.

    Args:
      file_prefix: A string or scalar string Tensor containing the prefix for
        files to read from.
      options: Optional `CheckpointOptions` object. This is unused in DTensor.

    Returns:
      A dictionary mapping from SaveableObject names to restore operations.
    Nr.   )prefixrA   shape_and_slicesinput_shapesinput_layoutsdtypes)restored_shapes)"r1   r2   r$   r=   r3   r>   
isinstancer   	DSaveSpecr?   dtyper
   global_shaper   SaveSpecr:   r;   r%   r8   r4   r<   r0   	to_stringas_listzipr   r6   r   r5   r	   d_tensor_restore_v2r   pack_sequence_asr   )r&   r*   r+   restore_specstensor_structurerD   saveable_tensor_structurerE   rA   rC   tensor_dtypeslayoutsglobal_shapesrestored_tensorsstructured_restored_tensorsrestore_opss                   r'   r   z_DSaver.restorek   s2    w==I  	M** -"$78 .. -$!((3dJ001


		4??DJJ $T->-> @ A o667


yy$//4::}}''

(<(<(>(,(9(9(>(>@@I	{{  ((*,---* JM	JFL-	COO%	& 	  )<<#($ 	  #'"7"78H8H#JK&)$*@*@*E'G 2""#+#3#3
D $4 $2k(-- 2 #	  	 s   II#r#   )__name__
__module____qualname____doc__r
   Meshr   r   SaveableObjectr(   strr   r   CheckpointOptionsr   	OperationrF   r   r    r)   r'   r   r   3   s    D6;; !%o&D&D!E AE.=.= *<<=.= 	.=f AE>> *<<=> C	>r)   r   c                        e Zd ZdZdej
                  f fdZ	 	 d
deee	j                  f   deej                     deeeeeej                   f   f      dej$                  deeej(                        f
d	Z xZS )_DCheckpointRestoreCoordinatorz4Holds the status of an object-based checkpoint load.r    c                 2    t        |   di | || _        y )Nrj   )superr(   r%   )r&   r    kwargs	__class__s      r'   r(   z'_DCheckpointRestoreCoordinator.__init__   s    	GvDJr)   tensor_saveablespython_positionsregistered_saversreaderr,   c                    ~g }|ru|t        j                  | j                        }|D ]O  }|j                  j                  d   j
                  }|j                  j                  |j                  |             Q |rt        j                  |      }t        d |D              }	t        |j                               |	k7  rt        d|j                         d|	      t        | j                  |      j!                  | j"                  | j$                        }
t'        j(                         sQt+        |
j-                               D ]5  \  }}|j/                  |       || j0                  vsJ || j0                  |<   7 |S )aa  Run or build restore operations for SaveableObjects.

    Args:
      tensor_saveables: `SaveableObject`s which correspond to Tensors.
      python_positions: `CheckpointPosition`s which correspond to `PythonState`
        Trackables bound to the checkpoint.
      registered_savers: a dict mapping saver names-> object name -> Trackable.
        This argument is not implemented for DTensorCheckpoint.
      reader: A CheckpointReader. Creates one lazily if None.

    Returns:
      When graph building, a list of restore operations, either cached or newly
      created, to restore `tensor_saveables`.
    r   c              3   4   K   | ]  }|j                     y wr#   r>   ).0rD   s     r'   	<genexpr>zC_DCheckpointRestoreCoordinator.restore_saveables.<locals>.<genexpr>   s     NhHMMNs   z0Saveable keys changed when validating. Got back z, was expecting )r   NewCheckpointReadersave_path_stringobject_proto
attributescheckpoint_key	trackabledeserialize
get_tensorr   validate_and_slice_inputssetkeysAssertionErrorr   r%   r   save_path_tensorr+   r   executing_eagerlysorteditemsr=   restore_ops_by_name)r&   rq   rr   rs   rt   r`   positionkeyvalidated_saveablesvalidated_namesnew_restore_opsr>   
restore_ops                r'   restore_saveablesz0_DCheckpointRestoreCoordinator.restore_saveables   so   , 	K 
%99$:O:OP& ?(##..q1@@&&v'8'8'=>?
 0JJ
N:MNNo	""$	%	8 0 5 5 7JK 	K
  

,?@HH


/o&&( &'<'<'> ? 	6D*


Z
(T555
55+5$
"
"4
(	6 r)   )NN)ra   rb   rc   rd   r
   re   r(   r   rg   r   rf   r   restore_libCheckpointPositionr   r   	Trackabler   rz   r   ri   r   __classcell__rp   s   @r'   rl   rl      s    <6;;  6:KO5*.s/>/M/M0N +O5 +/{/M/M*N5 ,4Dd/?1 :1 52 ,3	5 !5 H H5 &d3==&9:5r)   rl   c                   V     e Zd ZdZdej
                  f fdZddZ	 ddZddZ	 xZ
S )	DTrackableSaverz7A DTensor trackable saver that uses _SingleDeviceSaver.r    c                 :    t         t        |   |       || _        y r#   )rn   r   r(   r%   )r&   r    r   rp   s      r'   r(   zDTrackableSaver.__init__   s    	/4)*5DJr)   c                    | j                  |      \  }}}}| j                  }|7|j                  d       t        j                     }t        j                  |      }g }|j                         D ](  }|j                         D ]  }	|j                  |	        * |j                  t        j                  |t        j                               ||||fS )N)r4   r>   )_gather_serialized_tensors_saveables_cachepopr   OBJECT_GRAPH_PROTO_KEYr   $serialized_tensors_to_saveable_cachevaluesextendr=   NoRestoreSaveable)
r&   object_graph_tensorserialized_tensorsfeed_additionsrs   graph_protosaveables_dictnamed_saveable_objectssaveable_by_name	saveabless
             r'   _gather_saveablesz!DTrackableSaver._gather_saveables   s     334GH):**N /22
++- 
C
C "   *113 1'..0 1)%%i011 !!&,,	./ #K r)   c                    | j                  |      \  }}}}| j                  |k7  s(t        j                         st	        j
                         rt        | j                  |      }	|	j                  ||      }
t	        j                  d      5  t	        j                  |
g      5  t        j                  |      | _        ddd       ddd       || _        | j                  |fS # 1 sw Y   &xY w# 1 sw Y   *xY w)aj  Create or retrieve save ops, overrides parents's private method.

    Args:
      file_prefix: The prefix for saved checkpoint files.
      object_graph_tensor: A `Tensor` to which the current object graph will be
        fed.
      options: `CheckpointOptions` object.
      update_ckpt_state: Optional bool flag. Indiciate whether the internal
        checkpoint state needs to be updated. This is used for async checkpoint,
        which DTrackableSaver currently does not support.
    TODO(chienchunh): Implement async checkpoint for DTrackableSaver.

    Returns:
      A two-element tuple with a filename tensor and a feed_dict of tensors to
      feed when running it (if graph building). The feed dict contains the
      current object graph and any Python state to be saved in the
      checkpoint. When executing eagerly only the first argument is meaningful.
    )r   )r+   z/cpu:0N)r   _last_save_object_graphr   r   r   inside_functionr   r%   rF   r6   control_dependenciesr   identity_cached_save_operation)r&   r*   r   r+   update_ckpt_stater   r   r   unused_registered_saverssaversave_ops              r'    _save_cached_when_graph_buildingz0DTrackableSaver._save_cached_when_graph_building  s    0 "&!7!70 "8 "2[.$$3
 $$&#*=*=*? djj"89e

;
8g::h H%%wi0 	H(1(:(:;(G$
%	HH &1d"&&66	H 	HH Hs$   C0$C$?C0$C-	)C00C9c                    |xs t        j                         }|2t        j                  | j                  t        j                               S t        j                  |      }t        j                          }|rd}n|j                         }	 |j                  t        j                        }|r| j0                  t3        j4                  t7        j8                  d      g| j:                  j=                         z  t>        j@                  jC                  | j:                  jE                         d            | _        | j0                  }	| j0                  |i}
n~t3        j4                  t7        j8                  |      g| j:                  j=                         z  t>        j@                  jC                  | j:                  jE                         d            }	d}
tG        jH                         }|jK                  |       tM        | j:                  |||	|| jN                  | j                  || jP                  	      }tS        jT                  |d      jW                  | j                  jX                         | j                  jZ                  r| j                  jZ                  D ]  }|j\                  d	k(  rd}|j^                  d   j`                  D ])  }|jb                  |j\                  k(  s|jd                  } n ||jf                  v ritS        jT                  ||      jW                  |jh                          t        jj                  || j                  |
|
      }|S # t        j                   $ r t        j"                  ||      }|s[| j                  j%                         D ]>  }|j'                          |j(                  j+                  |       |j-                  |       @ t        j.                  || j                        cY S w xY w)z7Restore a training checkpoint with host mesh placement.N)	save_path	dtype_mapr   modelr   r/   )	r    object_graph_protor   r   rt   restore_op_cacher   r+   saveables_cache)r   proto_idroot)r   	feed_dictr+   )6r   rh   utilInitializationOnlyStatus_graph_viewr   uidr   rz   r   r   get_variable_to_dtype_mapr   r   r   r   NotFoundError_NameBasedRestoreCoordinatorlist_objects_maybe_initialize_trackable_name_based_restoresadd_name_based_attribute_restoreNameBasedSaverStatus_file_prefix_placeholderr   r7   r   constantr%   r9   r
   r:   r;   r8   r   TrackableObjectGraphParseFromStringrl   _restore_op_cacher   r   r   r   r   attached_dependenciesr>   nodeschildren
local_namenode_idobject_by_proto_idrefCheckpointLoadStatus)r&   r   r+   rt   graph_buildingr   object_graph_stringrestore_coordinatorexisting_trackablefile_prefix_tensorfile_prefix_feed_dictr   r   r   r   	proto_refload_statuss                    r'   r   zDTrackableSaver.restore;  s   ?+==?G**4+;+;SWWYGG!55i@F 2244Ni224i<"--d.I.IJ" 		&	&	. ),!!'*+djj.J.J.LLMM$$TZZ%9%9%;!$D)F%  88#<<iH 88	*
+djj.J.J.L
L
--
"
"4::#7#7#9
"
BD #4IIK&&':;/ZZ-+//##--	/J ""++2743C3C3H3H+I --!!77 G#88v +11!4== 	I!!SXX- ((H	
 z444 &&!H	66=gcgg6F%G( ++##'	K
 Y $$ < !== "&"2"2"?"?"A 	P

8
8
:

1
1
5
56I
J

:
:;N
O		P &&
$*:*:< <<s   M B(PPr#   )F)ra   rb   rc   rd   r
   re   r(   r   r   r   r   r   s   @r'   r   r      s,    ?6;; > :?	+7\Yr)   r   zPlease use tf.train.Checkpoint instead of DTensorCheckpoint. DTensor is integrated with tf.train.Checkpoint and it can be used out of the box to save and restore dtensors.)dateinstructionsz&experimental.dtensor.DTensorCheckpoint)v1c                   >     e Zd ZdZddej
                  f fdZ xZS )DTensorCheckpointz?Manages saving/restoring trackable values to disk, for DTensor.r    c                 &   t        t        | 
  dd|i| || _        | }d }d | _        d | _        |rrt        j                  |d       |}g }||d<   |j                          t        j                  |j                  d            | _        t        j                  |      | _        t        |j                         d       D ]  \  }}t        | ||       t!        | |      }t        j                  ||       |s8|xs g }|j                  |      }	|	&|j#                  t%        j&                  ||             w|	|k7  s}t)        dj+                  |             t-        |t/        j0                  t3        j4                  |      |            | _        y )	Nr   save_counterc                     | d   S )Nr   rj   )items    r'   <lambda>z,DTensorCheckpoint.__init__.<locals>.<lambda>  s
    Q r)   )r   zVCannot create a Checkpoint with keyword argument {name} if root.{name} already exists.rw   )r   rj   )rn   r   r(   r%   _save_counter_save_assign_opr   _assert_trackabler   r   NoDependency_lookup_dependency_rootr   r   setattrgetattrr=   r   TrackableReferencer2   formatr   graph_view_libObjectGraphViewweakrefr   _saver)r&   r    r   ro   
saver_rootr   kvconverted_vchildrp   s             r'   r(   zDTensorCheckpoint.__init__  s   	
T+@@@DJJ DD
T6*j  fVn
&&(*77

!
!.
13d"//5djv||~+?@ <1dAq D!$k
[!,	 5 ;''*=

&
&t'>'>q+'N
Ok!,,2FFN< <<( "&&KK
#"7	9:DKr)   r#   )ra   rb   rc   rd   r
   re   r(   r   r   s   @r'   r   r     s     H/:6;; /: /:r)   r   )3rd   typingr   r   r   r   tensorflow.core.protobufr   tensorflow.dtensor.pythonr   r   r	   r
   r   tensorflow.python.checkpointr   r   r   r   r   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   tensorflow.python.trackabler   r   tensorflow.python.trainingr   !tensorflow.python.training.savingr   r   tensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   r   _CheckpointRestoreCoordinatorrl   TrackableSaverr   
deprecated
Checkpointr   rj   r)   r'   <module>r     s    ( '  ? ) 0 5 , 2 ; ; E ? + 3 3 + + , 7 ; = B . ' 6v vr<T%G%G <~id)) iX 	89
 3;2: 2: <92:r)   