From d823be2a619f57f68da8c6202800c0ce15c07a9d Mon Sep 17 00:00:00 2001 From: freddiewanah Date: Sun, 23 Jul 2023 20:55:22 +1000 Subject: [PATCH 1/2] Refactor test cases to improve unit test quality --- .../benchmarks/eager_microbenchmarks_test.py | 3 -- keras/tests/add_loss_correctness_test.py | 32 +++++++++---------- keras/tests/model_subclassing_test.py | 15 +++++---- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/keras/benchmarks/eager_microbenchmarks_test.py b/keras/benchmarks/eager_microbenchmarks_test.py index aad975f1f96..19b42f750dc 100644 --- a/keras/benchmarks/eager_microbenchmarks_test.py +++ b/keras/benchmarks/eager_microbenchmarks_test.py @@ -135,7 +135,6 @@ def fn(): self._run(fn, 20) def benchmark_layers_embeddings_embedding_overhead(self): - layer = tf.keras.layers.Embedding(1, 1) x = tf.zeros((1, 1), dtype="int32") @@ -148,7 +147,6 @@ def fn(): class KerasLayerCallOverheadBenchmarks( MicroBenchmarksBase, metaclass=tf.__internal__.test.ParameterizedBenchmark ): - # The set of layers for benchmarking. To add benchmarks for new layers, # please add the parameter configs to "_benchmark_paramters". @@ -225,7 +223,6 @@ class KerasLayerCallOverheadBenchmarks( ] def benchmark_layer(self, layer, input_shape, kwargs=None): - x = tf.ones(input_shape) def fn(): diff --git a/keras/tests/add_loss_correctness_test.py b/keras/tests/add_loss_correctness_test.py index acf9ee16864..5bf87c9ce67 100644 --- a/keras/tests/add_loss_correctness_test.py +++ b/keras/tests/add_loss_correctness_test.py @@ -261,7 +261,7 @@ def call(self, inputs): layer = MyLayer() outputs = layer(inputs) model = Model(inputs, outputs) - self.assertEqual(len(model.losses), 1) + self.assertLen(model.losses, 1) model.compile("sgd", "mse", run_eagerly=test_utils.should_run_eagerly()) loss = model.train_on_batch(np.ones((2, 3)), np.ones((2, 3))) self.assertEqual(loss, 2 * 3) @@ -373,7 +373,7 @@ def call(self, inputs): m = Sequential([shared_layer]) m2 = Sequential([shared_layer, m]) m2(tf.constant([1, 2, 3])) - self.assertEqual(len(m2.losses), 2) + self.assertLen(m2.losses, 2) self.assertAllClose(m2.losses, [6, 12]) @test_combinations.run_all_keras_modes @@ -394,23 +394,23 @@ def call(self, x): x1 = tf.ones((1, 1)) _ = l(x1) if not tf.executing_eagerly(): - self.assertEqual(len(l.get_losses_for(x1)), 2) - self.assertEqual(len(l.get_losses_for(None)), 1) + self.assertLen(l.get_losses_for(x1), 2) + self.assertLen(l.get_losses_for(None), 1) x2 = tf.ones((1, 1)) _ = l(x2) if not tf.executing_eagerly(): - self.assertEqual(len(l.get_losses_for(x1)), 2) - self.assertEqual(len(l.get_losses_for(x2)), 2) - self.assertEqual(len(l.get_losses_for(None)), 1) + self.assertLen(l.get_losses_for(x1), 2) + self.assertLen(l.get_losses_for(x2), 2) + self.assertLen(l.get_losses_for(None), 1) outputs = l(inputs) model = Model(inputs, outputs) if not tf.executing_eagerly(): - self.assertEqual(len(model.losses), 7) - self.assertEqual(len(l.get_losses_for(x1)), 2) - self.assertEqual(len(l.get_losses_for(x2)), 2) - self.assertEqual(len(l.get_losses_for(None)), 1) + self.assertLen(model.losses, 7) + self.assertLen(l.get_losses_for(x1), 2) + self.assertLen(l.get_losses_for(x2), 2) + self.assertLen(l.get_losses_for(None), 1) x3 = tf.ones((1, 1)) model(x3) @@ -418,12 +418,12 @@ def call(self, x): model(x4) if tf.executing_eagerly(): # Eager losses are cleared every `__call__`. - self.assertEqual(len(model.losses), 3) + self.assertLen(model.losses, 3) else: - self.assertEqual(len(model.losses), 11) - self.assertEqual(len(model.get_losses_for(x3)), 2) - self.assertEqual(len(model.get_losses_for(x4)), 2) - self.assertEqual(len(model.get_losses_for(None)), 1) + self.assertLen(model.losses, 11) + self.assertLen(l.get_losses_for(x3), 2) + self.assertLen(l.get_losses_for(x4), 2) + self.assertLen(l.get_losses_for(None), 1) @test_combinations.run_all_keras_modes(always_skip_v1=True) def test_invalid_constant_input(self): diff --git a/keras/tests/model_subclassing_test.py b/keras/tests/model_subclassing_test.py index 60136baab5a..372f8863d0b 100644 --- a/keras/tests/model_subclassing_test.py +++ b/keras/tests/model_subclassing_test.py @@ -121,10 +121,12 @@ def test_invalid_input_shape_build(self): model.weights, "Model should have no weights since it has not been built.", ) - with self.assertRaisesRegex( - ValueError, "input shape is not one of the valid types" - ): - model.build(input_shape=tf.compat.v1.Dimension(input_dim)) + self.assertRaises( + ValueError, + model.build, + input_shape=tf.compat.v1.Dimension(input_dim), + msg="input shape is not one of the valid types", + ) def test_embed_dtype_with_subclass_build(self): class Embedding(keras.layers.Layer): @@ -512,9 +514,9 @@ def call(self, inputs): model(x) if tf.executing_eagerly(): - self.assertEqual(0, len(model.updates)) + self.assertLen(model.updates, 0) else: - self.assertEqual(2, len(model.updates)) + self.assertLen(model.updates, 2) class GraphSpecificModelSubclassingTests(tf.test.TestCase): @@ -557,7 +559,6 @@ def test_multi_io_workflow_with_tensors(self): _ = model.evaluate(steps=10, verbose=0) def test_updates_and_losses_for_nested_models_in_subclassed_model(self): - # Case 1: deferred-build sequential nested in subclass. class TestModel1(keras.Model): def __init__(self): From ddeff03e7322fc79ead78ded0156fb1b352233b6 Mon Sep 17 00:00:00 2001 From: freddiewanah Date: Sun, 23 Jul 2023 21:08:33 +1000 Subject: [PATCH 2/2] revert assertRaisesRegex --- keras/tests/model_subclassing_test.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/keras/tests/model_subclassing_test.py b/keras/tests/model_subclassing_test.py index 372f8863d0b..dc56912e187 100644 --- a/keras/tests/model_subclassing_test.py +++ b/keras/tests/model_subclassing_test.py @@ -121,12 +121,10 @@ def test_invalid_input_shape_build(self): model.weights, "Model should have no weights since it has not been built.", ) - self.assertRaises( - ValueError, - model.build, - input_shape=tf.compat.v1.Dimension(input_dim), - msg="input shape is not one of the valid types", - ) + with self.assertRaisesRegex( + ValueError, "input shape is not one of the valid types" + ): + model.build(input_shape=tf.compat.v1.Dimension(input_dim)) def test_embed_dtype_with_subclass_build(self): class Embedding(keras.layers.Layer):