From be60cfe80faedb2286e90c2cea23ca5cd7f3805a Mon Sep 17 00:00:00 2001 From: Scott Zhu Date: Fri, 11 Sep 2020 14:57:55 -0700 Subject: [PATCH] Copy the generate_combinations_with_testcase_name to keras testing_utils PiperOrigin-RevId: 331229096 Change-Id: Ie4a0bc8eacea7e7d04dee669de18b17ab1b0ecc1 --- .../layers/convolutional_recurrent_test.py | 3 +- .../keras/layers/cudnn_recurrent_test.py | 8 ++-- tensorflow/python/keras/layers/merge_test.py | 5 +-- .../python/keras/layers/recurrent_test.py | 7 ++- .../python/keras/layers/wrappers_test.py | 3 +- tensorflow/python/keras/testing_utils.py | 43 +++++++++++++++++++ .../utils/composite_tensor_support_test.py | 6 +-- 7 files changed, 57 insertions(+), 18 deletions(-) diff --git a/tensorflow/python/keras/layers/convolutional_recurrent_test.py b/tensorflow/python/keras/layers/convolutional_recurrent_test.py index a9187009be2..cb6be1e6f4e 100644 --- a/tensorflow/python/keras/layers/convolutional_recurrent_test.py +++ b/tensorflow/python/keras/layers/convolutional_recurrent_test.py @@ -22,7 +22,6 @@ from absl.testing import parameterized import numpy as np from tensorflow.python import keras -from tensorflow.python.framework import test_util from tensorflow.python.keras import keras_parameterized from tensorflow.python.keras import testing_utils from tensorflow.python.platform import test @@ -32,7 +31,7 @@ from tensorflow.python.platform import test class ConvLSTMTest(keras_parameterized.TestCase): @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( data_format=['channels_first', 'channels_last'], return_sequences=[True, False])) def test_conv_lstm(self, data_format, return_sequences): diff --git a/tensorflow/python/keras/layers/cudnn_recurrent_test.py b/tensorflow/python/keras/layers/cudnn_recurrent_test.py index d25851f6569..3bb392c85ad 100644 --- a/tensorflow/python/keras/layers/cudnn_recurrent_test.py +++ b/tensorflow/python/keras/layers/cudnn_recurrent_test.py @@ -40,7 +40,7 @@ from tensorflow.python.training import gradient_descent class CuDNNTest(keras_parameterized.TestCase): @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer_class=[keras.layers.CuDNNGRU, keras.layers.CuDNNLSTM], return_sequences=[True, False])) @test_util.run_gpu_only @@ -56,7 +56,7 @@ class CuDNNTest(keras_parameterized.TestCase): input_shape=(num_samples, timesteps, input_size)) @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer_class=[keras.layers.CuDNNGRU, keras.layers.CuDNNLSTM], go_backwards=[True, False])) @test_util.run_gpu_only @@ -269,7 +269,7 @@ class CuDNNV1OnlyTest(keras_parameterized.TestCase): # TODO(b/156439419): Reenable after the bug is fixed. @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( rnn_type=['LSTM', 'GRU'], to_cudnn=[True, False], bidirectional=[True, False], implementation=[1, 2], model_nest_level=[1, 2], model_type=['seq', 'func'])) @@ -352,7 +352,7 @@ class CuDNNV1OnlyTest(keras_parameterized.TestCase): os.remove(fname) @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( rnn_type=['LSTM', 'GRU'], to_cudnn=[True, False])) @test_util.run_v1_only('b/120911602') @test_util.run_gpu_only diff --git a/tensorflow/python/keras/layers/merge_test.py b/tensorflow/python/keras/layers/merge_test.py index 16d3701b7e3..028d2eb3925 100644 --- a/tensorflow/python/keras/layers/merge_test.py +++ b/tensorflow/python/keras/layers/merge_test.py @@ -22,7 +22,6 @@ from absl.testing import parameterized import numpy as np from tensorflow.python import keras -from tensorflow.python.framework import test_util as tf_test_util from tensorflow.python.keras import backend as K from tensorflow.python.keras import combinations from tensorflow.python.keras import keras_parameterized @@ -225,7 +224,7 @@ class MergeLayersTest(keras_parameterized.TestCase): self.assertEqual(layer.compute_output_shape([(4, 5), (4, 5)]), (4, 1)) @parameterized.named_parameters( - *tf_test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer=[keras.layers.Add, keras.layers.Subtract, keras.layers.Multiply, keras.layers.Minimum, keras.layers.Maximum, keras.layers.Average, @@ -251,7 +250,7 @@ class MergeLayersTest(keras_parameterized.TestCase): self.assertAllEqual(out_dense, out_ragged) @parameterized.named_parameters( - *tf_test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer=[keras.layers.Add, keras.layers.Subtract, keras.layers.Multiply, keras.layers.Minimum, keras.layers.Maximum, keras.layers.Average])) diff --git a/tensorflow/python/keras/layers/recurrent_test.py b/tensorflow/python/keras/layers/recurrent_test.py index db8cda90553..f9daa659c8a 100644 --- a/tensorflow/python/keras/layers/recurrent_test.py +++ b/tensorflow/python/keras/layers/recurrent_test.py @@ -33,7 +33,6 @@ from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.framework import random_seed from tensorflow.python.framework import tensor_shape -from tensorflow.python.framework import test_util from tensorflow.python.keras import keras_parameterized from tensorflow.python.keras import testing_utils from tensorflow.python.keras.engine import base_layer_utils @@ -722,7 +721,7 @@ class RNNTest(keras_parameterized.TestCase): self.assertAllClose(y_np, y_np_2, atol=1e-4) @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer=[rnn_v1.SimpleRNN, rnn_v1.GRU, rnn_v1.LSTM, rnn_v2.GRU, rnn_v2.LSTM], unroll=[True, False])) @@ -743,7 +742,7 @@ class RNNTest(keras_parameterized.TestCase): model.train_on_batch(x_np, y_np) @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( cell=[keras.layers.SimpleRNNCell, keras.layers.GRUCell, keras.layers.LSTMCell], unroll=[True, False])) @@ -1551,7 +1550,7 @@ class RNNTest(keras_parameterized.TestCase): model.predict(np.ones((batch, timesteps, input_dim))) @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name(layer=[ + *testing_utils.generate_combinations_with_testcase_name(layer=[ rnn_v1.SimpleRNN, rnn_v1.GRU, rnn_v1.LSTM, rnn_v2.GRU, rnn_v2.LSTM ])) def test_rnn_with_ragged_input(self, layer): diff --git a/tensorflow/python/keras/layers/wrappers_test.py b/tensorflow/python/keras/layers/wrappers_test.py index 9f7154f648a..f1412975cc3 100644 --- a/tensorflow/python/keras/layers/wrappers_test.py +++ b/tensorflow/python/keras/layers/wrappers_test.py @@ -28,7 +28,6 @@ from tensorflow.python.eager import context from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import tensor_shape -from tensorflow.python.framework import test_util as tf_test_util from tensorflow.python.keras import combinations from tensorflow.python.keras import keras_parameterized from tensorflow.python.keras import testing_utils @@ -405,7 +404,7 @@ class TimeDistributedTest(keras_parameterized.TestCase): @keras_parameterized.run_all_keras_modes @parameterized.named_parameters( - *tf_test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( layer=[keras.layers.LSTM, keras.layers.Dense])) def test_TimeDistributed_with_ragged_input(self, layer): diff --git a/tensorflow/python/keras/testing_utils.py b/tensorflow/python/keras/testing_utils.py index 9e7adb9d402..952492fca0a 100644 --- a/tensorflow/python/keras/testing_utils.py +++ b/tensorflow/python/keras/testing_utils.py @@ -18,8 +18,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +import collections import contextlib import functools +import itertools import threading import numpy as np @@ -1040,3 +1042,44 @@ def run_v2_only(func=None): return decorator(func) return decorator + + +def generate_combinations_with_testcase_name(**kwargs): + """Generate combinations based on its keyword arguments using combine(). + + This function calls combine() and appends a testcase name to the list of + dictionaries returned. The 'testcase_name' key is a required for named + parameterized tests. + + Args: + **kwargs: keyword arguments of form `option=[possibilities, ...]` or + `option=the_only_possibility`. + + Returns: + a list of dictionaries for each combination. Keys in the dictionaries are + the keyword argument names. Each key has one value - one of the + corresponding keyword argument values. + """ + sort_by_key = lambda k: k[0] + combinations = [] + for key, values in sorted(kwargs.items(), key=sort_by_key): + if not isinstance(values, list): + values = [values] + combinations.append([(key, value) for value in values]) + + combinations = [collections.OrderedDict(result) + for result in itertools.product(*combinations)] + named_combinations = [] + for combination in combinations: + assert isinstance(combination, collections.OrderedDict) + name = ''.join([ + '_{}_{}'.format(''.join(filter(str.isalnum, key)), + ''.join(filter(str.isalnum, str(value)))) + for key, value in combination.items() + ]) + named_combinations.append( + collections.OrderedDict( + list(combination.items()) + + [('testcase_name', '_test{}'.format(name))])) + + return named_combinations diff --git a/tensorflow/python/keras/utils/composite_tensor_support_test.py b/tensorflow/python/keras/utils/composite_tensor_support_test.py index daba188414a..a48408c6788 100644 --- a/tensorflow/python/keras/utils/composite_tensor_support_test.py +++ b/tensorflow/python/keras/utils/composite_tensor_support_test.py @@ -325,7 +325,7 @@ def prepare_inputs(data, use_dict, use_dataset, action, input_name): @keras_parameterized.run_with_all_model_types @keras_parameterized.run_all_keras_modes @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( use_dict=[True, False], use_dataset=[True, False], action=["predict", "evaluate", "fit"])) @@ -490,7 +490,7 @@ class ScipySparseTensorInputTest(keras_parameterized.TestCase, @keras_parameterized.run_with_all_model_types @keras_parameterized.run_all_keras_modes @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( use_dict=[True, False], use_dataset=[True, False], action=["predict", "evaluate", "fit"])) @@ -537,7 +537,7 @@ class RaggedTensorInputTest(keras_parameterized.TestCase, @keras_parameterized.run_with_all_model_types @keras_parameterized.run_all_keras_modes @parameterized.named_parameters( - *test_util.generate_combinations_with_testcase_name( + *testing_utils.generate_combinations_with_testcase_name( use_dict=[True, False], use_dataset=[True, False])) class RaggedTensorInputValidationTest(keras_parameterized.TestCase, test_util.TensorFlowTestCase):