Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions smdebug/core/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ def _increment_step(self):
# Called in the internal AWS codebase to determine
# if a particular tensor value should be saved
def should_save_tensor_or_collection(self, tensor_name: str, collection_name: str) -> bool:
if self.prepared_collections is False:
# always return false if an attempt to save a
# tensor is made before the collections are prepared.
# this can happen if the fn is called before callbacks are init.
self.logger.warning(
"Tensors cannot be saved with smdebug before callbacks are initialized."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

)
return False
if self._is_collection_being_saved_for_step(collection_name):
return True
return self.is_tensor_saved_for_step(tensor_name)
Expand Down
8 changes: 8 additions & 0 deletions tests/tensorflow2/test_should_save_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ def test_should_save_tensor_with_custom_collection(out_dir):
else:
assert not hook.should_save_tensor_or_collection(layer_name, CollectionKeys.GRADIENTS)
assert not hook.should_save_tensor_or_collection(layer_name, CollectionKeys.LAYERS)


def test_should_save_tensor_behavior_without_prepare_collections(out_dir):
"""Always return false if an attempt to save a tensor is made before the collections are prepared.
This can happen if the fn is called before callbacks are init."""
hook = smd.KerasHook(out_dir, save_config=SaveConfig(save_interval=3), save_all=True)
assert not hook.should_save_tensor_or_collection("dummy", CollectionKeys.GRADIENTS)
assert not hook.should_save_tensor_or_collection("dummy", CollectionKeys.LAYERS)