1# Copyright 2019 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"""Test configs for where.""" 16import tensorflow.compat.v1 as tf 17from tensorflow.lite.testing.zip_test_utils import create_tensor_data 18from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 19from tensorflow.lite.testing.zip_test_utils import register_make_test_function 20 21 22@register_make_test_function() 23def make_where_tests(options): 24 """Make a set of tests to do where.""" 25 26 test_parameters = [ 27 { 28 "input_dtype": [tf.float32, tf.int32], 29 "input_shape_set": [([1, 2, 3, 4], [1, 2, 3, 4]),], 30 "use_where_v2": [False, True], 31 "fully_quantize": [False], 32 }, 33 { 34 "input_dtype": [tf.float32, tf.int32], 35 "input_shape_set": [([], []),], 36 "use_where_v2": [], 37 "fully_quantize": [False], 38 }, 39 { 40 "input_dtype": [tf.float32], 41 "input_shape_set": [ 42 ([1, 2, 3, 4], [1, 2, 3, 4]), 43 ([], []), 44 ], 45 "use_where_v2": [False, True], 46 "fully_quantize": [True], 47 }, 48 # High dimension broadcasting support in MLIR converter. 49 { 50 "input_dtype": [tf.float32, tf.int32], 51 "input_shape_set": [([8, 7, 6, 5, 4, 3, 2, 1], [4, 3, 2, 1]), 52 ([8, 7, 6, 5, 4, 3, 2, 1], [None, 3, 2, 1]), 53 ([8, 7, 6, 5, None, 3, 2, 1], [None, 3, 2, 1])], 54 "use_where_v2": [True], 55 "fully_quantize": [False], 56 "dynamic_size_value": [4, 1], 57 }, 58 { 59 "input_dtype": [tf.float32], 60 "input_shape_set": [([8, 7, 6, 5, 4, 3, 2, 1], [4, 3, 2, 1])], 61 "use_where_v2": [True], 62 "fully_quantize": [True], 63 "dynamic_size_value": [4], 64 }, 65 { 66 "input_dtype": [tf.float32, tf.int32], 67 "input_shape_set": [([], []), ([1], []), ([], [1])], 68 "use_where_v2": [False, True], 69 "fully_quantize": [False], 70 }, 71 ] 72 73 def populate_dynamic_shape(parameters, input_shape): 74 return [ 75 parameters["dynamic_size_value"] if x is None else x 76 for x in input_shape 77 ] 78 79 def build_graph(parameters): 80 """Build the where op testing graph.""" 81 input_value1 = tf.compat.v1.placeholder( 82 dtype=parameters["input_dtype"], 83 name="input2", 84 shape=parameters["input_shape_set"][0]) 85 input_value2 = tf.compat.v1.placeholder( 86 dtype=parameters["input_dtype"], 87 name="input3", 88 shape=parameters["input_shape_set"][1]) 89 less = tf.less(input_value1, input_value2) 90 where = tf.where_v2 if parameters["use_where_v2"] else tf.where 91 out = where(less, input_value1, input_value2) 92 return [input_value1, input_value2], [out] 93 94 def build_inputs(parameters, sess, inputs, outputs): 95 input_shape_1 = populate_dynamic_shape(parameters, 96 parameters["input_shape_set"][0]) 97 input_shape_2 = populate_dynamic_shape(parameters, 98 parameters["input_shape_set"][1]) 99 100 input_value1 = create_tensor_data( 101 parameters["input_dtype"], input_shape_1, min_value=-1, max_value=1) 102 input_value2 = create_tensor_data( 103 parameters["input_dtype"], input_shape_2, min_value=-1, max_value=1) 104 return [input_value1, input_value2], sess.run( 105 outputs, feed_dict=dict(zip(inputs, [input_value1, input_value2]))) 106 107 make_zip_of_tests( 108 options, 109 test_parameters, 110 build_graph, 111 build_inputs, 112 expected_tf_failures=4) 113