1# Copyright 2018 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 Tensorflow. 16 17Additionally it provides `generate()`, `combine()` and `times()` with Tensorflow 18customizations as a default. 19""" 20 21from __future__ import absolute_import 22from __future__ import division 23from __future__ import print_function 24 25import functools 26 27from tensorflow.python import tf2 28from tensorflow.python.eager import context 29from tensorflow.python.framework import ops 30from tensorflow.python.framework import test_combinations 31from tensorflow.python.util.tf_export import tf_export 32 33 34class EagerGraphCombination(test_combinations.TestCombination): 35 """Run the test in Graph or Eager mode. 36 37 The optional `mode` parameter controls the test's execution mode. Its 38 accepted values are "graph" or "eager" literals. 39 """ 40 41 def context_managers(self, kwargs): 42 mode = kwargs.pop("mode", None) 43 if mode is None: 44 return [] 45 elif mode == "eager": 46 return [context.eager_mode()] 47 elif mode == "graph": 48 return [ops.Graph().as_default(), context.graph_mode()] 49 else: 50 raise ValueError( 51 "'mode' has to be either 'eager' or 'graph' and not {}".format(mode)) 52 53 def parameter_modifiers(self): 54 return [test_combinations.OptionalParameter("mode")] 55 56 57class TFVersionCombination(test_combinations.TestCombination): 58 """Control the execution of the test in TF1.x and TF2. 59 60 If TF2 is enabled then a test with TF1 test is going to be skipped and vice 61 versa. 62 63 Test targets continuously run in TF2 thanks to the tensorflow.v2 TAP target. 64 A test can be run in TF2 with bazel by passing --test_env=TF2_BEHAVIOR=1. 65 """ 66 67 def should_execute_combination(self, kwargs): 68 tf_api_version = kwargs.pop("tf_api_version", None) 69 if tf_api_version == 1 and tf2.enabled(): 70 return (False, "Skipping a TF1.x test when TF2 is enabled.") 71 elif tf_api_version == 2 and not tf2.enabled(): 72 return (False, "Skipping a TF2 test when TF2 is not enabled.") 73 return (True, None) 74 75 def parameter_modifiers(self): 76 return [test_combinations.OptionalParameter("tf_api_version")] 77 78 79generate = functools.partial( 80 test_combinations.generate, 81 test_combinations=(EagerGraphCombination(), TFVersionCombination())) 82combine = test_combinations.combine 83times = test_combinations.times 84NamedObject = test_combinations.NamedObject 85 86tf_export("__internal__.test.combinations.generate", v1=[])(generate) 87