1# Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""This module customizes `test_combinations` for `tf.keras` related tests.""" 16 17import functools 18 19from tensorflow.python import tf2 20from tensorflow.python.framework import combinations 21from tensorflow.python.framework import test_combinations 22from tensorflow.python.keras import testing_utils 23 24KERAS_MODEL_TYPES = ['functional', 'subclass', 'sequential'] 25 26 27def keras_mode_combinations(mode=None, run_eagerly=None): 28 """Returns the default test combinations for tf.keras tests. 29 30 Note that if tf2 is enabled, then v1 session test will be skipped. 31 32 Args: 33 mode: List of modes to run the tests. The valid options are 'graph' and 34 'eager'. Default to ['graph', 'eager'] if not specified. If a empty list 35 is provide, then the test will run under the context based on tf's 36 version, eg graph for v1 and eager for v2. 37 run_eagerly: List of `run_eagerly` value to be run with the tests. 38 Default to [True, False] if not specified. Note that for `graph` mode, 39 run_eagerly value will only be False. 40 41 Returns: 42 A list contains all the combinations to be used to generate test cases. 43 """ 44 if mode is None: 45 mode = ['eager'] if tf2.enabled() else ['graph', 'eager'] 46 if run_eagerly is None: 47 run_eagerly = [True, False] 48 result = [] 49 if 'eager' in mode: 50 result += combinations.combine(mode=['eager'], run_eagerly=run_eagerly) 51 if 'graph' in mode: 52 result += combinations.combine(mode=['graph'], run_eagerly=[False]) 53 return result 54 55 56def keras_model_type_combinations(): 57 return combinations.combine(model_type=KERAS_MODEL_TYPES) 58 59 60class KerasModeCombination(test_combinations.TestCombination): 61 """Combination for Keras test mode. 62 63 It by default includes v1_session, v2_eager and v2_tf_function. 64 """ 65 66 def context_managers(self, kwargs): 67 run_eagerly = kwargs.pop('run_eagerly', None) 68 69 if run_eagerly is not None: 70 return [testing_utils.run_eagerly_scope(run_eagerly)] 71 else: 72 return [] 73 74 def parameter_modifiers(self): 75 return [test_combinations.OptionalParameter('run_eagerly')] 76 77 78class KerasModelTypeCombination(test_combinations.TestCombination): 79 """Combination for Keras model types when doing model test. 80 81 It by default includes 'functional', 'subclass', 'sequential'. 82 83 Various methods in `testing_utils` to get models will auto-generate a model 84 of the currently active Keras model type. This allows unittests to confirm 85 the equivalence between different Keras models. 86 """ 87 88 def context_managers(self, kwargs): 89 model_type = kwargs.pop('model_type', None) 90 if model_type in KERAS_MODEL_TYPES: 91 return [testing_utils.model_type_scope(model_type)] 92 else: 93 return [] 94 95 def parameter_modifiers(self): 96 return [test_combinations.OptionalParameter('model_type')] 97 98 99_defaults = combinations.generate.keywords['test_combinations'] 100generate = functools.partial( 101 combinations.generate, 102 test_combinations=_defaults + 103 (KerasModeCombination(), KerasModelTypeCombination())) 104combine = test_combinations.combine 105times = test_combinations.times 106NamedObject = test_combinations.NamedObject 107