MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _track_trackable

Method _track_trackable

tensorflow/python/training/tracking/base.py:774–824  ·  view source on GitHub ↗

Declare a dependency on another `Trackable` object. Indicates that checkpoints for this object should include variables from `trackable`. Variables in a checkpoint are mapped to `Trackable`s based on the names provided when the checkpoint was written. To avoid breaking existing

(self, trackable, name, overwrite=False)

Source from the content-addressed store, hash-verified

772 checkpoint_position=checkpoint_position, shape=shape)
773
774 def _track_trackable(self, trackable, name, overwrite=False):
775 """Declare a dependency on another `Trackable` object.
776
777 Indicates that checkpoints for this object should include variables from
778 `trackable`.
779
780 Variables in a checkpoint are mapped to `Trackable`s based on the names
781 provided when the checkpoint was written. To avoid breaking existing
782 checkpoints when modifying a class, neither variable names nor dependency
783 names (the names passed to `_track_trackable`) may change.
784
785 Args:
786 trackable: A `Trackable` which this object depends on.
787 name: A local name for `trackable`, used for loading checkpoints into the
788 correct objects.
789 overwrite: Boolean, whether silently replacing dependencies is OK. Used
790 for __setattr__, where throwing an error on attribute reassignment would
791 be inappropriate.
792
793 Returns:
794 `trackable`, for convenience when declaring a dependency and
795 assigning to a member variable in one statement.
796
797 Raises:
798 TypeError: If `trackable` does not inherit from `Trackable`.
799 ValueError: If another object is already tracked by this name.
800 """
801 self._maybe_initialize_trackable()
802 if not isinstance(trackable, Trackable):
803 raise TypeError(("Trackable._track_trackable() passed type %s, not a "
804 "Trackable.") % (type(trackable),))
805 new_reference = TrackableReference(name=name, ref=trackable)
806 current_object = self._lookup_dependency(name)
807 if (current_object is not None and current_object is not trackable):
808 if not overwrite:
809 raise ValueError(
810 ("Called Trackable._track_trackable() with name='%s', "
811 "but a Trackable with this name is already declared as a "
812 "dependency. Names must be unique (or overwrite=True).") % (name,))
813 # This is a weird thing to do, but we're not going to stop people from
814 # using __setattr__.
815 for index, (old_name, _) in enumerate(
816 self._self_unconditional_checkpoint_dependencies):
817 if name == old_name:
818 self._self_unconditional_checkpoint_dependencies[
819 index] = new_reference
820 elif current_object is None:
821 self._self_unconditional_checkpoint_dependencies.append(new_reference)
822 self._handle_deferred_dependencies(name=name, trackable=trackable)
823 self._self_unconditional_dependency_names[name] = trackable
824 return trackable
825
826 def _handle_deferred_dependencies(self, name, trackable):
827 """Pop and load any deferred checkpoint restores into `trackable`.

Callers 15

testOverwriteMethod · 0.95
__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45
__init__Method · 0.45

Calls 5

_lookup_dependencyMethod · 0.95
typeFunction · 0.85
appendMethod · 0.45

Tested by 3

testOverwriteMethod · 0.76