|
| 1 | +""" |
| 2 | +WARNING: This must be run manually, with the custom TensorFlow fork installed. |
| 3 | +Not used in CI/CD. May be useful for DLC testing. |
| 4 | +
|
| 5 | +Be sure to run with Python2 (/usr/bin/python) and Python3. |
| 6 | +Run with and without the flag --zcc. |
| 7 | +
|
| 8 | +Test with DNNClassifier and raw Estimator. |
| 9 | +Test with Session. |
| 10 | +Test with Keras. |
| 11 | +
|
| 12 | +Test with AdamOptimizer and SGD. |
| 13 | +
|
| 14 | +We check that certain tensors are saved. |
| 15 | +""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import numpy as np |
| 19 | +import random |
| 20 | +import tensorflow as tf |
| 21 | +import tornasole.tensorflow as ts |
| 22 | +from tornasole.core.utils import SagemakerSimulator |
| 23 | +from tf_utils import ( |
| 24 | + get_estimator, |
| 25 | + get_input_fns, |
| 26 | + get_train_op_and_placeholders, |
| 27 | + get_data, |
| 28 | + get_keras_data, |
| 29 | + get_keras_model_v1, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def test_estimator(script_mode: bool): |
| 34 | + """ Throws errors about tensors not saving to collection. Investigate after merging PR #304. |
| 35 | + """ |
| 36 | + with SagemakerSimulator() as sim: |
| 37 | + # Setup |
| 38 | + mnist_classifier = get_estimator() |
| 39 | + train_input_fn, eval_input_fn = get_input_fns() |
| 40 | + |
| 41 | + # Train and evaluate |
| 42 | + if script_mode: |
| 43 | + hook = ts.TornasoleEstimatorHook(out_dir=sim.out_dir) |
| 44 | + mnist_classifier.train(input_fn=train_input_fn, steps=50) |
| 45 | + mnist_classifier.evaluate(input_fn=eval_input_fn, steps=10) |
| 46 | + else: |
| 47 | + mnist_classifier.train(input_fn=train_input_fn, steps=50) |
| 48 | + mnist_classifier.evaluate(input_fn=eval_input_fn, steps=10) |
| 49 | + |
| 50 | + # Check that hook created and tensors saved |
| 51 | + trial = ts.create_trial(path=sim.out_dir) |
| 52 | + assert ts.get_hook() is not None, "Hook was not created." |
| 53 | + assert len(trial.available_steps()) > 0, "Nothing saved at any step." |
| 54 | + assert len(trial.tensors()) > 0, "Tensors were not saved." |
| 55 | + |
| 56 | + |
| 57 | +def test_linear_classifier(script_mode: bool): |
| 58 | + """ Throws errors about tensors not saving to collection. Investigate after merging PR #304. |
| 59 | + """ |
| 60 | + with SagemakerSimulator() as sim: |
| 61 | + # Setup |
| 62 | + train_input_fn, eval_input_fn = get_input_fns() |
| 63 | + x_feature = tf.feature_column.numeric_column("x", shape=(28, 28)) |
| 64 | + estimator = tf.compat.v1.estimator.LinearClassifier( |
| 65 | + feature_columns=[x_feature], model_dir="/tmp/mnist_linear_classifier", n_classes=10 |
| 66 | + ) |
| 67 | + |
| 68 | + # Train |
| 69 | + if script_mode: |
| 70 | + hook = ts.TornasoleEstimatorHook(out_dir=sim.out_dir) |
| 71 | + estimator.train(input_fn=train_input_fn, steps=100, hooks=[hook]) |
| 72 | + else: |
| 73 | + estimator.train(input_fn=train_input_fn, steps=100, hooks=[hook]) |
| 74 | + |
| 75 | + # Check that hook created and tensors saved |
| 76 | + trial = ts.create_trial(path=sim.out_dir) |
| 77 | + assert ts.get_hook() is not None, "Hook was not created." |
| 78 | + assert len(trial.available_steps()) > 0, "Nothing saved at any step." |
| 79 | + assert len(trial.tensors()) > 0, "Tensors were not saved." |
| 80 | + |
| 81 | + |
| 82 | +def test_monitored_session(script_mode: bool): |
| 83 | + """ Works as intended. """ |
| 84 | + with SagemakerSimulator() as sim: |
| 85 | + train_op, X, Y = get_train_op_and_placeholders() |
| 86 | + init = tf.compat.v1.global_variables_initializer() |
| 87 | + mnist = get_data() |
| 88 | + |
| 89 | + if script_mode: |
| 90 | + hook = ts.TornasoleKerasHook(out_dir=sim.out_dir) |
| 91 | + sess = tf.train.MonitoredSession(hooks=[hook]) |
| 92 | + else: |
| 93 | + sess = tf.train.MonitoredSession() |
| 94 | + |
| 95 | + with sess: |
| 96 | + sess.run(init) |
| 97 | + for step in range(1, 101): |
| 98 | + batch_x, batch_y = mnist.train.next_batch(32) |
| 99 | + sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) |
| 100 | + |
| 101 | + # Check that hook created and tensors saved |
| 102 | + trial = ts.create_trial(path=sim.out_dir) |
| 103 | + assert ts.get_hook() is not None, "Hook was not created." |
| 104 | + assert len(trial.available_steps()) > 0, "Nothing saved at any step." |
| 105 | + assert len(trial.tensors()) > 0, "Tensors were not saved." |
| 106 | + |
| 107 | + |
| 108 | +def test_keras_v1(script_mode: bool): |
| 109 | + """ Failing because we need TornasoleKerasHook from PR #304. |
| 110 | +
|
| 111 | + Taken from https://www.tensorflow.org/guide/keras/functional. |
| 112 | + """ |
| 113 | + with SagemakerSimulator() as sim: |
| 114 | + import tensorflow.compat.v1.keras as keras |
| 115 | + |
| 116 | + model = get_keras_model_v1() |
| 117 | + (x_train, y_train), (x_test, y_test) = get_keras_data() |
| 118 | + |
| 119 | + model.compile( |
| 120 | + loss="sparse_categorical_crossentropy", |
| 121 | + optimizer=keras.optimizers.RMSprop(), |
| 122 | + metrics=["accuracy"], |
| 123 | + ) |
| 124 | + if script_mode: |
| 125 | + hook = ts.TornasoleKerasHook(out_dir=sim.out_dir) |
| 126 | + history = model.fit( |
| 127 | + x_train, y_train, batch_size=64, epochs=5, validation_split=0.2, callbacks=[hook] |
| 128 | + ) |
| 129 | + test_scores = model.evaluate(x_test, y_test, verbose=2, callbacks=[hook]) |
| 130 | + else: |
| 131 | + history = model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.2) |
| 132 | + test_scores = model.evaluate(x_test, y_test, verbose=2) |
| 133 | + |
| 134 | + # Check that hook created and tensors saved |
| 135 | + trial = ts.create_trial(path=sim.out_dir) |
| 136 | + assert ts.get_hook() is not None, "Hook was not created." |
| 137 | + assert len(trial.available_steps()) > 0, "Nothing saved at any step." |
| 138 | + assert len(trial.tensors()) > 0, "Tensors were not saved." |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + parser = argparse.ArgumentParser() |
| 143 | + parser.add_argument( |
| 144 | + "--script-mode", help="Manually create hooks instead of relying on ZCC", action="store_true" |
| 145 | + ) |
| 146 | + args = parser.parse_args() |
| 147 | + script_mode = args.script_mode |
| 148 | + |
| 149 | + test_estimator(script_mode=script_mode) |
| 150 | + test_monitored_session(script_mode=script_mode) |
| 151 | + test_linear_classifier(script_mode=script_mode) |
| 152 | + test_keras_v1(script_mode=script_mode) |
0 commit comments