def test_gradtape_include_collections(out_dir): """ This test ensures that a training script written with GradientTape handles the case where hook config contains all collections mentioned through include collections """ include_collections = [ CollectionKeys.WEIGHTS, CollectionKeys.BIASES, CollectionKeys.GRADIENTS, CollectionKeys.LOSSES, CollectionKeys.OUTPUTS, CollectionKeys.METRICS, CollectionKeys.OPTIMIZER_VARIABLES, ] save_config = SaveConfig(save_interval=3) hook = smd.KerasHook( out_dir, save_config=save_config, include_collections=include_collections, reduction_config=ReductionConfig(norms=ALLOWED_NORMS, reductions=ALLOWED_REDUCTIONS), ) helper_keras_gradtape(out_dir, hook=hook) trial = smd.create_trial(path=out_dir) # can't save gradients in TF 2.x assert len(trial.tensor_names()) == (16 if is_tf_2_2() else 15) assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 assert len( trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 1
def test_subclassed_model(out_dir): # Download and load MNIST dataset. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data("MNIST-data") x_train, x_test = x_train / 255.0, x_test / 255.0 # Add a channels dimension x_train = x_train[..., tf.newaxis] x_test = x_test[..., tf.newaxis] # Create an instance of the model model = MyModel() train_ds = ( tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000, seed=123).batch(2) ) MyModel.hook = smd.KerasHook( out_dir, save_all=True, save_config=smd.SaveConfig(save_steps=[x for x in range(10)], save_interval=1), ) MyModel.hook.register_model(model) model.compile(optimizer="Adam", loss="mse", run_eagerly=True) model.fit(train_ds, epochs=1, steps_per_epoch=10, callbacks=[MyModel.hook]) trial = smd.create_trial(out_dir) assert len(trial.tensor_names(collection=smd.CollectionKeys.LAYERS)) == 8 assert trial.tensor_names(collection=smd.CollectionKeys.INPUTS) == ["model_input"] assert trial.tensor_names(collection=smd.CollectionKeys.OUTPUTS) == ["labels", "predictions"] assert trial.tensor_names(collection=smd.CollectionKeys.LOSSES) == ["loss"] assert len(trial.tensor_names(collection=smd.CollectionKeys.GRADIENTS)) == 6
def test_keras_fit(out_dir, tf_eager_mode, saveall): hook = smd.KerasHook(out_dir=out_dir, save_all=saveall) helper_keras_fit( trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) # can't save gradients in TF 2.x eager mode if saveall: # save losses, metrics, weights, biases if tf_eager_mode: assert len(trial.tensor_names()) == (12 if is_tf_2_2() else 13) else: assert len(trial.tensor_names()) == 21 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len( trial.tensor_names( collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 assert ( len( trial.tensor_names( collection=CollectionKeys.OPTIMIZER_VARIABLES, mode=ModeKeys.EVAL)) == 0, "No Optimizer Variables Should be Saved in EVAL Mode", ) else: # save the default losses and metrics assert len(trial.tensor_names()) == (3 if is_tf_2_2() and tf_eager_mode else 4) assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == ( 2 if is_tf_2_2() and tf_eager_mode else 3)
def main(): parser = argparse.ArgumentParser(description="Train resnet50 cifar10") parser.add_argument("--batch_size", type=int, default=32) parser.add_argument("--epoch", type=int, default=3) parser.add_argument("--model_dir", type=str, default="./model_keras_resnet") parser.add_argument("--out_dir", type=str) parser.add_argument("--save_interval", type=int, default=500) opt = parser.parse_args() model = ResNet50(weights=None, input_shape=(32, 32, 3), classes=10) ##### Enabling SageMaker Debugger ########### # creating hook hook = smd.KerasHook( out_dir=opt.out_dir, include_collections=["weights", "gradients", "losses"], save_config=smd.SaveConfig(save_interval=opt.save_interval), ) optimizer = tf.keras.optimizers.Adam() ##### Enabling SageMaker Debugger ########### # wrap the optimizer so the hook can identify the gradients optimizer = hook.wrap_optimizer(optimizer) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]) # start the training. train(opt.batch_size, opt.epoch, model, hook)
def test_regex_filtering_for_default_collections(out_dir): hook = smd.KerasHook( out_dir, save_config=SaveConfig(save_interval=9), include_collections=[CollectionKeys.LAYERS, CollectionKeys.GRADIENTS], ) hook.get_collection(CollectionKeys.LAYERS).include("^dense") hook.get_collection(CollectionKeys.GRADIENTS).include("gradients/dense") helper_keras_fit( out_dir, hook=hook, save_config=SaveConfig(save_interval=10), steps=["train"], run_eagerly=True, ) tr = create_trial_fast_refresh(out_dir) layer_tnames = tr.tensor_names(collection=CollectionKeys.LAYERS) gradient_tnames = tr.tensor_names(collection=CollectionKeys.GRADIENTS) assert len(layer_tnames) == (4 if is_tf_2_2() else 0) assert len(gradient_tnames) == (4 if is_tf_2_2() else 0) layer_pattern = r"^(dense)(_\d+)?\/(inputs|outputs)" gradient_pattern = r"gradients/dense" for tname in layer_tnames: assert tr.tensor(tname).value(0) is not None assert re.match(pattern=layer_pattern, string=tname) is not None for tname in gradient_tnames: assert tr.tensor(tname).value(0) is not None assert re.match(pattern=gradient_pattern, string=tname) is not None
def test_model_inputs_and_outputs(out_dir, tf_eager_mode): # explicitly save INPUTS and OUTPUTS include_collections = [CollectionKeys.INPUTS, CollectionKeys.OUTPUTS] hook = smd.KerasHook(out_dir=out_dir, include_collections=include_collections) helper_keras_fit( trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) assert len(trial.steps(mode=ModeKeys.TRAIN)) == 3 assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 1 for tname in trial.tensor_names(collection=CollectionKeys.OUTPUTS): output = trial.tensor(tname) assert tname in ["y", "y_pred"] assert output.value(0) is not None # Check the shape of output tensors assert trial.tensor("y").value(0).shape[1] == 1 # label assert trial.tensor("y_pred").value( 0).shape[1] == 10 # Output probability for each class
def test_keras_gradients(script_mode, tf_optimizer): """ Works as intended. """ smd.del_hook() tf.reset_default_graph() tf.keras.backend.clear_session() json_file_contents = """ { "S3OutputPath": "s3://sagemaker-test", "LocalPath": "/opt/ml/output/tensors", "CollectionConfigurations": [ { "CollectionName": "gradients" }, { "CollectionName": "optimizer_variables" }, { "CollectionName": "losses" } ] } """ with SagemakerSimulator(json_file_contents=json_file_contents) as sim: model = get_keras_model_v1() (x_train, y_train), (x_test, y_test) = get_keras_data() if tf_optimizer: opt = tf.train.RMSPropOptimizer(0.1) else: opt = tf.keras.optimizers.RMSprop() if script_mode: hook = smd.KerasHook( out_dir=sim.out_dir, include_collections=["gradients", "optimizer_variables", "losses"], ) opt = hook.wrap_optimizer(opt) model.compile( loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"] ) history = model.fit( x_train, y_train, batch_size=16, epochs=5, validation_split=0.2, callbacks=[hook] ) test_scores = model.evaluate(x_test, y_test, verbose=2, callbacks=[hook]) else: model.compile( loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"] ) history = model.fit(x_train, y_train, batch_size=16, epochs=5, validation_split=0.2) test_scores = model.evaluate(x_test, y_test, verbose=2) # Check that hook created and tensors saved trial = smd.create_trial(path=sim.out_dir) assert smd.get_hook() is not None, "Hook was not created." assert len(trial.steps()) > 0, "Nothing saved at any step." assert len(trial.tensor_names()) > 0, "Tensors were not saved." assert len(trial.tensor_names(collection="gradients")) > 0 if not tf_optimizer: # as this is only supported for keras optimizers currently assert len(trial.tensor_names(collection="optimizer_variables")) > 0
def test_keras_fit(out_dir, tf_eager_mode, saveall): hook = smd.KerasHook(out_dir=out_dir, save_all=saveall) helper_keras_fit( trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) # can't save gradients in TF 2.x eager mode if saveall: # save losses, metrics, weights, biases if tf_eager_mode: assert len(trial.tensor_names()) == 7 if is_tf_2_2() else 8 else: assert len(trial.tensor_names()) == 21 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 else: # save the default losses and metrics assert len(trial.tensor_names()) == 3 if is_tf_2_2() and tf_eager_mode else 4 assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert ( len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 2 if is_tf_2_2() and tf_eager_mode else 3 )
def test_keras_v1(script_mode): """ Works as intended. """ smd.del_hook() tf.reset_default_graph() tf.keras.backend.clear_session() with SagemakerSimulator() as sim: model = get_keras_model_v1() (x_train, y_train), (x_test, y_test) = get_keras_data() model.compile( loss="sparse_categorical_crossentropy", optimizer=tf.keras.optimizers.RMSprop(), metrics=["accuracy"], ) if script_mode: hook = smd.KerasHook(out_dir=sim.out_dir) history = model.fit( x_train, y_train, batch_size=64, epochs=5, validation_split=0.2, callbacks=[hook] ) test_scores = model.evaluate(x_test, y_test, verbose=2, callbacks=[hook]) else: history = model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.2) test_scores = model.evaluate(x_test, y_test, verbose=2) # Check that hook created and tensors saved trial = smd.create_trial(path=sim.out_dir) assert smd.get_hook() is not None, "Hook was not created." assert len(trial.steps()) > 0, "Nothing saved at any step." assert len(trial.tensor_names()) > 0, "Tensors were not saved."
def test_include_collections(out_dir, tf_eager_mode): include_collections = [ CollectionKeys.WEIGHTS, CollectionKeys.BIASES, CollectionKeys.GRADIENTS, CollectionKeys.LOSSES, CollectionKeys.OUTPUTS, CollectionKeys.METRICS, CollectionKeys.OPTIMIZER_VARIABLES, ] save_config = SaveConfig(save_interval=3) hook = smd.KerasHook( out_dir, save_config=save_config, include_collections=include_collections, reduction_config=ReductionConfig(norms=ALLOWED_NORMS, reductions=ALLOWED_REDUCTIONS), ) helper_keras_fit(out_dir, hook=hook, steps=["train", "eval", "predict"], eager=tf_eager_mode) trial = smd.create_trial(path=out_dir) # can't save gradients in TF 2.x if tf_eager_mode: assert len(trial.tensor_names()) == 7 if is_tf_2_2() else 8 else: assert len(trial.tensor_names()) == 18 assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert ( len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 2 if is_tf_2_2() and tf_eager_mode else 3 )
def test_save_layer_inputs_and_outputs(out_dir, tf_eager_mode): # explicitly save INPUTS and OUTPUTS include_collections = [CollectionKeys.INPUTS, CollectionKeys.OUTPUTS] hook = smd.KerasHook(out_dir=out_dir, include_collections=include_collections) helper_keras_fit( trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 4 assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 4 # Check that output of layer is equal to the input of the next boolean_matrix = trial.tensor("flatten/outputs").value(0) == trial.tensor( "dense/inputs").value(0) assert boolean_matrix.all() boolean_matrix = trial.tensor("dense/outputs").value(0) == trial.tensor( "dropout/inputs").value(0) assert boolean_matrix.all() boolean_matrix = trial.tensor("dropout/outputs").value(0) == trial.tensor( "dense_1/inputs").value(0) assert boolean_matrix.all()
def test_keras_fit_pure_eager(out_dir, tf_eager_mode): """ Test save all and save default collection in fit() pure eager mode """ hook = smd.KerasHook(out_dir=out_dir, save_all=True, save_config=SaveConfig(save_interval=3)) helper_keras_fit(trial_dir=out_dir, hook=hook, eager=tf_eager_mode, run_eagerly=True) trial = smd.create_trial(path=out_dir) if is_tf_2_2(): assert len(trial.tensor_names()) == 27 else: assert len(trial.tensor_names()) == (20 if is_tf_2_3() else 21) assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len( trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 assert len(trial.tensor_names( collection=CollectionKeys.INPUTS)) == (1 if is_tf_2_2() else 0) assert len(trial.tensor_names( collection=CollectionKeys.OUTPUTS)) == (2 if is_tf_2_2() else 0)
def helper_test_keras_v2(script_mode: bool = False, eager_mode: bool = True): """ Test the default ZCC behavior of saving losses and metrics in eager and non-eager modes.""" smd.del_hook() tf.keras.backend.clear_session() if not eager_mode: tf.compat.v1.disable_eager_execution() with SagemakerSimulator() as sim: model = get_keras_model_v2() (x_train, y_train), (x_test, y_test) = get_keras_data() x_train, x_test = x_train / 255, x_test / 255 opt = tf.keras.optimizers.RMSprop() if script_mode: hook = smd.KerasHook(out_dir=sim.out_dir, export_tensorboard=True) opt = hook.wrap_optimizer(opt) model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) history = model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2, callbacks=[hook]) test_scores = model.evaluate(x_test, y_test, verbose=2, callbacks=[hook]) else: model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) history = model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2) test_scores = model.evaluate(x_test, y_test, verbose=2) hook = smd.get_hook() assert hook hook.close() # Check that hook created and tensors saved trial = smd.create_trial(path=sim.out_dir) assert len(trial.steps()) > 0, "Nothing saved at any step." assert len(trial.tensor_names()) > 0, "Tensors were not saved." # DEFAULT TENSORS SAVED assert len(trial.tensor_names( collection=CollectionKeys.LOSSES)) > 0, "No Losses Saved" assert len(trial.tensor_names( collection=CollectionKeys.METRICS)) > 0, "No Metrics Saved" assert (len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 0 ), "Weights were not expected to be saved by default" assert (len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 0 ), "Biases were not expected to be saved by default"
def test_keras_fit_shapes(out_dir): hook = smd.KerasHook( out_dir=out_dir, save_all=True, save_config=SaveConfig(save_steps=[0]), reduction_config=ReductionConfig(save_shape=True), ) helper_keras_fit(trial_dir=out_dir, hook=hook) print(create_trial_fast_refresh(out_dir).tensor_names(step=0)) verify_shapes(out_dir, 0)
def test_keras_gradtape_shapes(out_dir): hook = smd.KerasHook( out_dir=out_dir, save_all=True, save_config=SaveConfig(save_steps=[0]), reduction_config=ReductionConfig(save_shape=True), ) helper_keras_gradtape(trial_dir=out_dir, hook=hook) verify_shapes(out_dir, 0) verify_shapes(out_dir, 500)
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)
def helper_create_hook(out_dir, collections, include_regex=None): hook = smd.KerasHook(out_dir, save_config=SaveConfig(save_interval=3), include_collections=collections) if include_regex: for collection in collections: hook.get_collection(collection).include(include_regex) hook.register_model(model) hook.on_train_begin() return hook
def test_layer_names_gradient_tape(out_dir): hook = smd.KerasHook( out_dir, save_config=SaveConfig(save_interval=9), include_collections=[CollectionKeys.LAYERS], ) helper_keras_gradtape(out_dir, hook=hook, save_config=SaveConfig(save_interval=9)) tr = create_trial_fast_refresh(out_dir) tnames = tr.tensor_names(collection=CollectionKeys.LAYERS) pattern = r"^(flatten|dense|dropout)(_\d+)?\/(inputs|outputs)" for tname in tnames: assert re.match(pattern=pattern, string=tname) is not None
def test_mixed_precision_training(out_dir): from tensorflow.keras.mixed_precision import experimental as mixed_precision hook = smd.KerasHook(out_dir=out_dir, save_all=True) policy = mixed_precision.Policy("mixed_float16") mixed_precision.set_policy(policy) inputs = keras.Input(shape=(784,), name="digits") if tf.config.list_physical_devices("GPU"): # The model will run with 4096 units on a GPU num_units = 4096 else: # Use fewer units on CPUs so the model finishes in a reasonable amount of time # The model will run with 64 units on a CPU num_units = 64 dense1 = layers.Dense(num_units, activation="relu", name="dense_1") x = dense1(inputs) dense2 = layers.Dense(num_units, activation="relu", name="dense_2") x = dense2(x) # CORRECT: softmax and model output are float32 x = layers.Dense(10, name="dense_logits")(x) outputs = layers.Activation("softmax", dtype="float32", name="predictions")(x) # The linear activation is an identity function. So this simply casts 'outputs' # to float32. In this particular case, 'outputs' is already float32 so this is a # no-op. outputs = layers.Activation("linear", dtype="float32")(outputs) model = keras.Model(inputs=inputs, outputs=outputs) model.compile( loss="sparse_categorical_crossentropy", optimizer=keras.optimizers.RMSprop(), metrics=["accuracy"], ) (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train = x_train.reshape(60000, 784).astype("float32") / 255 x_test = x_test.reshape(10000, 784).astype("float32") / 255 initial_weights = model.get_weights() hooks = [hook] history = model.fit( x_train, y_train, batch_size=8192, epochs=5, callbacks=hooks, validation_split=0.2 ) test_scores = model.evaluate(x_test, y_test, verbose=2) trial = create_trial(out_dir) assert len(trial.tensor_names()) == 30
def test_keras_fit_false(out_dir, tf_eager_mode=False): test_include_collections = [ CollectionKeys.LOSSES, CollectionKeys.METRICS, CollectionKeys.WEIGHTS, CollectionKeys.BIASES, CollectionKeys.GRADIENTS, CollectionKeys.INPUTS, CollectionKeys.OUTPUTS, CollectionKeys.LAYERS, CollectionKeys.OPTIMIZER_VARIABLES, ] hook = smd.KerasHook(out_dir=out_dir, include_collections=test_include_collections) helper_keras_fit( include_collections=test_include_collections, trial_dir=out_dir, hook=hook, run_eagerly=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) # We first assert that none of the collections we requested for are empty assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 assert len(trial.tensor_names( collection=CollectionKeys.INPUTS)) == 1 # 1 Model Input assert len(trial.tensor_names( collection=CollectionKeys.OUTPUTS)) == 2 # 2 Model outputs assert len( trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 # We assert that all the tensors saved have a valid value for tname in trial.tensor_names(): assert trial.tensor(tname).value(0) is not None # We then analyse Layer Inputs and Layer Outputs # Check that output of layer is equal to the input of the next boolean_matrix = trial.tensor("flatten_1/outputs").value( 0) == trial.tensor("dense_2/inputs").value(0) assert boolean_matrix.all() boolean_matrix = trial.tensor("dense_2/outputs").value(0) == trial.tensor( "dropout_1/inputs").value(0) assert boolean_matrix.all() boolean_matrix = trial.tensor("dropout_1/outputs").value( 0) == trial.tensor("dense_3/inputs").value(0) assert boolean_matrix.all()
def test_keras_fit(out_dir, tf_eager_mode, saveall): hook = smd.KerasHook(out_dir=out_dir, save_all=saveall) ts = time.time() hook.save_scalar("foobar", 1, sm_metric=True, timestamp=ts) scalars_to_be_saved = dict() scalars_to_be_saved["scalar/foobar"] = (ts, 0) helper_keras_fit( trial_dir=out_dir, hook=hook, run_eagerly=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) # can't save gradients in TF 2.x eager mode if saveall: # save losses, metrics, weights, biases, scalar if tf_eager_mode: if is_tf_2_2(): assert len(trial.tensor_names()) == 28 else: assert len(trial.tensor_names()) == (21 if is_tf_2_3() else 14) assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == ( 1 if is_tf_2_2() else 0 ) assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == ( 2 if is_tf_2_2() else 0 ) else: assert len(trial.tensor_names()) == 21 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 assert ( len( trial.tensor_names( collection=CollectionKeys.OPTIMIZER_VARIABLES, mode=ModeKeys.EVAL ) ) == 0, "No Optimizer Variables Should be Saved in EVAL Mode", ) else: # save the default losses and metrics assert len(trial.tensor_names()) == ( 4 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 5 ) assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == ( 2 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 3 ) for tname in trial.tensor_names(): assert trial.tensor(tname).value(0) is not None
def helper_create_hook(out_dir, collections, include_regex=None): hook = smd.KerasHook(out_dir, save_config=SaveConfig(save_interval=3), include_collections=collections) if include_regex: for collection in collections: hook.get_collection(collection).include(include_regex) hook.register_model(model) hook.set_mode(ModeKeys.TRAIN) hook._prepare_collections_for_tf2() hook._increment_step() hook.on_train_begin() return hook
def test_include_only_custom_collection(out_dir, tf_eager_mode): include_collections = ["custom_optimizer_variables"] save_config = SaveConfig(save_interval=3) hook = smd.KerasHook( out_dir, save_config=save_config, include_collections=include_collections, reduction_config=ReductionConfig(norms=ALLOWED_NORMS, reductions=ALLOWED_REDUCTIONS), ) hook.get_collection("custom_optimizer_variables").include("Adam") helper_keras_fit(out_dir, hook=hook, steps=["train", "eval", "predict"], eager=tf_eager_mode) trial = smd.create_trial(path=out_dir) assert len(trial.tensor_names()) == (8 if is_tf_2_2() and tf_eager_mode else 9) assert len(trial.tensor_names(collection="custom_optimizer_variables")) == 5
def test_hook_timeline_file_write( set_up_smprofiler_config_path, set_up_resource_config, out_dir, tf_eager_mode ): hook = smd.KerasHook(out_dir=out_dir, save_all=False) helper_keras_fit(trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval"]) files = [] for path in Path(out_dir + "/" + DEFAULT_PREFIX).rglob("*.json"): files.append(path) assert len(files) == 1 with open(files[0]) as timeline_file: events_dict = json.load(timeline_file) assert events_dict
def test_gradtape_include_regex(out_dir): """ Test custom collection with regex """ hook = smd.KerasHook( out_dir, save_config=SaveConfig(save_interval=9), include_collections=["custom_coll"] ) hook.get_collection("custom_coll").include("dense") helper_keras_gradtape(out_dir, hook=hook, save_config=SaveConfig(save_interval=9)) tr = create_trial_fast_refresh(out_dir) tnames = tr.tensor_names(collection="custom_coll") assert len(tnames) == (12 if is_tf_2_2() else 8) for tname in tnames: assert tr.tensor(tname).value(0) is not None
def test_gradtape_persistent(out_dir, saveall): """ Test save all and save default collection """ hook = smd.KerasHook(out_dir=out_dir, save_all=saveall, save_config=SaveConfig(save_interval=3)) helper_keras_gradtape(trial_dir=out_dir, hook=hook, persistent=True) trial = smd.create_trial(path=out_dir) if saveall: # save losses, metrics, weights, biases assert len(trial.tensor_names()) == 10 assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 else: # save the default losses and metrics assert len(trial.tensor_names()) == 2 assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 1
def test_multiple_inputs(out_dir): my_model = MyModel() hook = smd.KerasHook( out_dir, save_all=True, save_config=smd.SaveConfig(save_steps=[0], save_interval=1) ) hook.register_model(my_model) x_train = np.random.random((1000, 20)) y_train = np.random.random((1000, 1)) my_model.compile(optimizer="Adam", loss="mse", run_eagerly=True) my_model.fit(x_train, y_train, epochs=1, steps_per_epoch=1, callbacks=[hook]) trial = create_trial(path=out_dir) tnames = sorted(trial.tensor_names(collection=smd.CollectionKeys.LAYERS)) assert "concatenate" in tnames[0] assert len(trial.tensor(tnames[0]).value(0)) == 2 assert trial.tensor(tnames[0]).shape(0) == (2, 1000, 20)
def test_save_gradients(out_dir, tf_eager_mode): # explicitly save INPUTS and OUTPUTS include_collections = [CollectionKeys.GRADIENTS] hook = smd.KerasHook(out_dir=out_dir, include_collections=include_collections) helper_keras_fit( trial_dir=out_dir, hook=hook, eager=tf_eager_mode, steps=["train", "eval", "predict", "train"], ) trial = smd.create_trial(path=out_dir) assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 for tname in trial.tensor_names(collection=CollectionKeys.GRADIENTS): output = trial.tensor(tname) assert output.value(0) is not None
def test_keras_gradtape(out_dir, saveall): """ Test save all and save default collection """ hook = smd.KerasHook(out_dir=out_dir, save_all=saveall, save_config=SaveConfig(save_interval=3)) helper_keras_gradtape(trial_dir=out_dir, hook=hook) trial = smd.create_trial(path=out_dir) if saveall: # save losses, metrics, weights, biases assert len(trial.tensor_names()) == (25 if is_tf_2_2() else 15) assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 else: # save the default losses and metrics assert len(trial.tensor_names()) == 2 assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 1
def test_include_regex(out_dir, tf_eager_mode): hook = smd.KerasHook(out_dir, save_config=SaveConfig(save_interval=9), include_collections=["custom_coll"]) hook.get_collection("custom_coll").include("dense") helper_keras_fit( out_dir, hook=hook, save_config=SaveConfig(save_interval=9), steps=["train"], run_eagerly=tf_eager_mode, ) tr = create_trial_fast_refresh(out_dir) tnames = tr.tensor_names(collection="custom_coll") assert len(tnames) == 12 for tname in tnames: assert tr.tensor(tname).value(0) is not None