Skip to content

Commit 91d20ac

Browse files
authored
Update Assert Statements For New TF 2.2.0 DLC (aws#320)
1 parent 65b4a33 commit 91d20ac

File tree

5 files changed

+226
-69
lines changed

5 files changed

+226
-69
lines changed

tests/tensorflow2/test_keras.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import tensorflow.compat.v2 as tf
1515
import tensorflow_datasets as tfds
1616
from tests.constants import TEST_DATASET_S3_PATH
17-
from tests.tensorflow2.utils import is_tf_2_2
17+
from tests.tensorflow2.utils import is_tf_2_2, is_tf_2_3
1818
from tests.tensorflow.utils import create_trial_fast_refresh
1919
from tests.utils import use_s3_datasets
2020

@@ -195,7 +195,7 @@ def test_keras_gradtape(out_dir, saveall):
195195

196196
trial = smd.create_trial(path=out_dir)
197197
if saveall: # save losses, metrics, weights, biases
198-
assert len(trial.tensor_names()) == 15
198+
assert len(trial.tensor_names()) == (25 if is_tf_2_2() else 15)
199199
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2
200200
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
201201
assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5
@@ -275,7 +275,7 @@ def test_gradtape_include_regex(out_dir):
275275
tr = create_trial_fast_refresh(out_dir)
276276
tnames = tr.tensor_names(collection="custom_coll")
277277

278-
assert len(tnames) == 8
278+
assert len(tnames) == (12 if is_tf_2_2() else 8)
279279
for tname in tnames:
280280
assert tr.tensor(tname).value(0) is not None
281281

@@ -343,7 +343,7 @@ def test_gradtape_include_collections(out_dir):
343343

344344
trial = smd.create_trial(path=out_dir)
345345
# can't save gradients in TF 2.x
346-
assert len(trial.tensor_names()) == 15
346+
assert len(trial.tensor_names()) == (16 if is_tf_2_2() else 15)
347347
assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4
348348
assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5
349349
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2
@@ -388,7 +388,7 @@ def test_gradtape_persistent(out_dir, saveall):
388388

389389
trial = smd.create_trial(path=out_dir)
390390
if saveall: # save losses, metrics, weights, biases
391-
assert len(trial.tensor_names()) == 15
391+
assert len(trial.tensor_names()) == (25 if is_tf_2_2() else 15)
392392
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2
393393
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
394394
assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5
@@ -409,17 +409,24 @@ def test_keras_fit(out_dir, tf_eager_mode, saveall):
409409
helper_keras_fit(
410410
trial_dir=out_dir,
411411
hook=hook,
412-
eager=tf_eager_mode,
412+
run_eagerly=tf_eager_mode,
413413
steps=["train", "eval", "predict", "train"],
414414
)
415415

416416
trial = smd.create_trial(path=out_dir)
417417
# can't save gradients in TF 2.x eager mode
418418
if saveall: # save losses, metrics, weights, biases, scalar
419419
if tf_eager_mode:
420-
assert len(trial.tensor_names()) == (13 if is_tf_2_2() else 14)
421-
assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 0
422-
assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 0
420+
if is_tf_2_2():
421+
assert len(trial.tensor_names()) == 28
422+
else:
423+
assert len(trial.tensor_names()) == (21 if is_tf_2_3() else 14)
424+
assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == (
425+
1 if is_tf_2_2() else 0
426+
)
427+
assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == (
428+
2 if is_tf_2_2() else 0
429+
)
423430
else:
424431
assert len(trial.tensor_names()) == 21
425432
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2
@@ -435,10 +442,12 @@ def test_keras_fit(out_dir, tf_eager_mode, saveall):
435442
"No Optimizer Variables Should be Saved in EVAL Mode",
436443
)
437444
else: # save the default losses and metrics
438-
assert len(trial.tensor_names()) == (4 if is_tf_2_2() and tf_eager_mode else 5)
445+
assert len(trial.tensor_names()) == (
446+
4 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 5
447+
)
439448
assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1
440449
assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == (
441-
2 if is_tf_2_2() and tf_eager_mode else 3
450+
2 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 3
442451
)
443452
for tname in trial.tensor_names():
444453
assert trial.tensor(tname).value(0) is not None
@@ -510,7 +519,7 @@ def test_include_regex(out_dir, tf_eager_mode):
510519
tnames = tr.tensor_names(collection="custom_coll")
511520

512521
if tf_eager_mode:
513-
assert len(tnames) == 8
522+
assert len(tnames) == (12 if is_tf_2_2() else 8)
514523
else:
515524
assert len(tnames) == 8
516525
for tname in tnames:
@@ -534,7 +543,7 @@ def test_clash_with_tb_callback(out_dir):
534543
add_callbacks=["tensorboard"],
535544
)
536545
tr = create_trial_fast_refresh(out_dir)
537-
assert len(tr.tensor_names()) == (7 if is_tf_2_2() else 8)
546+
assert len(tr.tensor_names()) == (7 if (is_tf_2_2() or is_tf_2_3()) else 8)
538547

539548

540549
@pytest.mark.slow
@@ -560,12 +569,12 @@ def test_weights_collections(out_dir, tf_eager_mode):
560569

561570
trial = smd.create_trial(path=out_dir)
562571
# can't save gradients in TF 2.x
563-
assert len(trial.tensor_names()) == (5 if is_tf_2_2() and tf_eager_mode else 6)
572+
assert len(trial.tensor_names()) == (5 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 6)
564573
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 0
565574
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
566575
assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1
567576
assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == (
568-
2 if is_tf_2_2() and tf_eager_mode else 3
577+
2 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 3
569578
)
570579

571580

@@ -595,7 +604,10 @@ def test_include_collections(out_dir, tf_eager_mode):
595604
trial = smd.create_trial(path=out_dir)
596605
# can't save gradients in TF 2.x
597606
if tf_eager_mode:
598-
assert len(trial.tensor_names()) == (12 if is_tf_2_2() else 13)
607+
if is_tf_2_2():
608+
assert len(trial.tensor_names()) == 16
609+
else:
610+
assert len(trial.tensor_names()) == (12 if is_tf_2_3() else 13)
599611
else:
600612
assert len(trial.tensor_names()) == 18
601613
assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4
@@ -605,7 +617,7 @@ def test_include_collections(out_dir, tf_eager_mode):
605617
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
606618
assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1
607619
assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == (
608-
2 if is_tf_2_2() and tf_eager_mode else 3
620+
2 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 3
609621
)
610622

611623

@@ -625,7 +637,7 @@ def test_include_only_custom_collection(out_dir, tf_eager_mode):
625637
)
626638

627639
trial = smd.create_trial(path=out_dir)
628-
assert len(trial.tensor_names()) == (8 if is_tf_2_2() and tf_eager_mode else 9)
640+
assert len(trial.tensor_names()) == (8 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 9)
629641
assert len(trial.tensor_names(collection="custom_optimizer_variables")) == 5
630642

631643

@@ -640,12 +652,12 @@ def test_hook_from_json(out_dir, tf_eager_mode, monkeypatch):
640652

641653
trial = smd.create_trial(path=out_dir)
642654
# can't save gradients in TF 2.x
643-
assert len(trial.tensor_names()) == (5 if is_tf_2_2() and tf_eager_mode else 6)
655+
assert len(trial.tensor_names()) == (5 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 6)
644656
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 0
645657
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
646658
assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1
647659
assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == (
648-
2 if is_tf_2_2() and tf_eager_mode else 3
660+
2 if (is_tf_2_2() or is_tf_2_3()) and tf_eager_mode else 3
649661
)
650662

651663

@@ -658,12 +670,15 @@ def test_keras_fit_pure_eager(out_dir, tf_eager_mode):
658670
helper_keras_fit(trial_dir=out_dir, hook=hook, eager=tf_eager_mode, run_eagerly=True)
659671

660672
trial = smd.create_trial(path=out_dir)
661-
assert len(trial.tensor_names()) == (20 if is_tf_2_2() else 21)
673+
if is_tf_2_2():
674+
assert len(trial.tensor_names()) == 27
675+
else:
676+
assert len(trial.tensor_names()) == (20 if is_tf_2_3() else 21)
662677
assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2
663678
assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2
664679
assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5
665-
assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 0
666-
assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 0
680+
assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == (1 if is_tf_2_2() else 0)
681+
assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == (2 if is_tf_2_2() else 0)
667682

668683

669684
@pytest.mark.skip # skip until aws tf update

tests/tensorflow2/test_keras_mirrored.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import tensorflow_datasets as tfds
1212
from tensorflow.python.client import device_lib
1313
from tests.core.utils import verify_files
14-
from tests.tensorflow2.utils import is_tf_2_2
14+
from tests.tensorflow2.utils import is_tf_2_2, is_tf_2_3
1515
from tests.tensorflow.utils import create_trial_fast_refresh
1616

1717
# First Party
@@ -164,11 +164,16 @@ def exhaustive_check(trial_dir, include_workers="one", eager=True):
164164
if include_workers == "all":
165165
assert len(tr.workers()) == strategy.num_replicas_in_sync
166166
if eager:
167-
assert len(tr.tensor_names()) == (
168-
6 + 1 + 2 + 5 + 1 if is_tf_2_2() else 6 + 1 + 3 + 5 + 1
169-
)
170-
# 6 weights, 1 loss, 3 metrics, 5 optimizer variables for Tf 2.1, 1 scalar
171-
# 6 weights, 1 loss, 2 metrics, 5 optimizer variables for Tf 2.2, 1 scalar
167+
if is_tf_2_2():
168+
assert len(tr.tensor_names()) == (6 + 1 + 2 + 5 + 1 + 6 + 2)
169+
# 6 weights, 1 loss, 2 metrics, 5 optimizer variables, 6 gradients, 2 outputs for Tf 2.2, 1 scalar
170+
else:
171+
assert len(tr.tensor_names()) == (
172+
6 + 1 + 2 + 5 + 1 if (is_tf_2_2() or is_tf_2_3()) else 6 + 1 + 3 + 5 + 1
173+
)
174+
# 6 weights, 1 loss, 2 metrics, 5 optimizer variables for Tf 2.3, 1 scalar
175+
# 6 weights, 1 loss, 3 metrics, 5 optimizer variables for Tf 2.1, 1 scalar
176+
172177
else:
173178
assert len(tr.tensor_names()) == (6 + 6 + 1 + 3 + strategy.num_replicas_in_sync * 3 + 5)
174179
else:
@@ -232,7 +237,7 @@ def exhaustive_check(trial_dir, include_workers="one", eager=True):
232237
assert len(tr.tensor(loss_name).steps()) == 12
233238

234239
metricnames = tr.tensor_names(collection=CollectionKeys.METRICS)
235-
assert len(metricnames) == (2 if is_tf_2_2() else 3)
240+
assert len(metricnames) == (2 if (is_tf_2_2() or is_tf_2_3()) else 3)
236241

237242

238243
@pytest.mark.slow
@@ -256,8 +261,15 @@ def test_save_all(out_dir, tf_eager_mode, workers):
256261
tr = create_trial_fast_refresh(out_dir)
257262
print(tr.tensor_names())
258263
if tf_eager_mode:
259-
assert len(tr.tensor_names()) == (6 + 2 + 1 + 5 + 1 if is_tf_2_2() else 6 + 3 + 1 + 5 + 1)
260-
# weights, metrics, losses, optimizer variables, scalar
264+
if is_tf_2_2():
265+
assert len(tr.tensor_names()) == (
266+
6 + 2 + 1 + 5 + 1 + 1 + 2 + 8 + 8 if is_tf_2_2() else 6 + 3 + 1 + 5 + 1
267+
)
268+
# weights, metrics, losses, optimizer variables, scalar, inputs, outputs, gradients, layers
269+
else:
270+
assert len(tr.tensor_names()) == (
271+
6 + 2 + 1 + 5 + 1 if is_tf_2_3() else 6 + 3 + 1 + 5 + 1
272+
)
261273
else:
262274
assert (
263275
len(tr.tensor_names())
@@ -366,7 +378,7 @@ def test_include_regex(out_dir, tf_eager_mode, workers):
366378
tnames = tr.tensor_names(collection="custom_coll")
367379

368380
if tf_eager_mode:
369-
assert len(tnames) == 4
381+
assert len(tnames) == (12 if is_tf_2_2() else 4)
370382
else:
371383
assert len(tnames) == 4 + 3 * strategy.num_replicas_in_sync
372384
for tname in tnames:
@@ -421,7 +433,10 @@ def test_clash_with_tb_callback(out_dir):
421433
add_callbacks=["tensorboard"],
422434
)
423435
tr = create_trial_fast_refresh(out_dir)
424-
assert len(tr.tensor_names()) == (10 if is_tf_2_2() else 11)
436+
if is_tf_2_2():
437+
assert len(tr.tensor_names()) == 16
438+
else:
439+
assert len(tr.tensor_names()) == (10 if is_tf_2_3() else 11)
425440

426441

427442
@pytest.mark.skip

tests/tensorflow2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def is_tf_2_2():
1212
number of tensor_names emitted by 1.
1313
:return: bool
1414
"""
15-
if version.parse(tf.__version__) >= version.parse("2.2.0"):
15+
if version.parse(tf.__version__) == version.parse("2.2.0"):
1616
return True
1717
return False
1818

tests/zero_code_change/test_tensorflow2_gradtape_integration.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Third Party
1313
import pytest
1414
import tensorflow.compat.v2 as tf
15-
from tests.tensorflow2.utils import is_tf_2_2
15+
from tests.tensorflow2.utils import is_tf_2_2, is_tf_2_3
1616

1717
# First Party
1818
import smdebug.tensorflow as smd
@@ -26,7 +26,9 @@ def get_keras_data():
2626
return (x_train, y_train), (x_test, y_test)
2727

2828

29-
def helper_test_keras_v2_gradienttape(script_mode: bool = False, json_file_contents="{}"):
29+
def helper_test_keras_v2_gradienttape(
30+
script_mode: bool = False, json_file_contents="{}", default=False
31+
):
3032
""" Test the default ZCC behavior of saving losses and metrics in eager and non-eager modes."""
3133
smd.del_hook()
3234
tf.keras.backend.clear_session()
@@ -49,7 +51,7 @@ def helper_test_keras_v2_gradienttape(script_mode: bool = False, json_file_conte
4951
opt = tf.keras.optimizers.RMSprop()
5052
cce = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
5153
train_acc_metric = tf.keras.metrics.SparseCategoricalAccuracy()
52-
n_epochs = 2
54+
n_epochs = 1
5355
if script_mode:
5456
if json_file_contents == "{}":
5557
hook = smd.KerasHook(out_dir=sim.out_dir, export_tensorboard=True)
@@ -100,7 +102,7 @@ def helper_test_keras_v2_gradienttape(script_mode: bool = False, json_file_conte
100102
print(log)
101103
train_acc_metric.reset_states()
102104
hook = smd.get_hook()
103-
if not is_tf_2_2():
105+
if not (is_tf_2_2() or is_tf_2_3()):
104106
assert not hook # only supported on TF 2.2 and greater
105107
return
106108
assert hook
@@ -110,12 +112,23 @@ def helper_test_keras_v2_gradienttape(script_mode: bool = False, json_file_conte
110112
assert len(trial.steps()) > 0, "Nothing saved at any step."
111113
assert len(trial.tensor_names()) > 0, "Tensors were not saved."
112114
assert len(trial.tensor_names(collection="losses")) > 0
115+
if is_tf_2_2() and default is False:
116+
# Inputs and Outputs are not saved with the default collection configurations.
117+
assert len(trial.tensor_names(collection="inputs")) > 0
118+
assert len(trial.tensor_names(collection="outputs")) > 0
119+
assert trial.tensor_names(collection="outputs") == ["predictions"]
120+
if "dense_layers" in json_file_contents:
121+
# Only assert for test_keras_v2_multi_collections
122+
# which defines this custom collection
123+
assert len(trial.tensor_names(collection="dense_layers")) > 0
124+
else:
125+
assert len(trial.tensor_names(collection="dense_layers")) == 0
113126

114127

115128
@pytest.mark.parametrize("script_mode", [False])
116129
def test_keras_v2_default(script_mode):
117130
# Test default ZCC behavior
118-
helper_test_keras_v2_gradienttape(script_mode=script_mode)
131+
helper_test_keras_v2_gradienttape(script_mode=script_mode, default=True)
119132

120133

121134
@pytest.mark.parametrize("script_mode", [False])
@@ -144,6 +157,18 @@ def test_keras_v2_multi_collections(script_mode):
144157
},
145158
{
146159
"CollectionName": "optimizer_variables"
160+
},
161+
{
162+
"CollectionName": "outputs"
163+
},
164+
{
165+
"CollectionName": "inputs"
166+
},
167+
{
168+
"CollectionName": "dense_layers",
169+
"CollectionParameters": {
170+
"include_regex": ".*dense.*"
171+
}
147172
}
148173
]
149174
}
@@ -161,7 +186,7 @@ def test_keras_v2_save_all(script_mode):
161186
"S3OutputPath": "s3://sagemaker-test",
162187
"LocalPath": "/opt/ml/output/tensors",
163188
"HookParameters" : {
164-
"save_steps": "0,1,2,3",
189+
"save_steps": "0",
165190
"save_all": true
166191
}
167192
}

0 commit comments

Comments
 (0)