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 slice.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import numpy as np 21import tensorflow.compat.v1 as tf 22from tensorflow.lite.testing.zip_test_utils import create_tensor_data 23from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 24from tensorflow.lite.testing.zip_test_utils import register_make_test_function 25from tensorflow.lite.testing.zip_test_utils import TF_TYPE_INFO 26 27 28@register_make_test_function() 29def make_slice_tests(options): 30 """Make a set of tests to do slice.""" 31 32 # TODO(renjieliu): add test/support for uint8. 33 test_parameters = [ 34 # 4-D 35 { 36 "dtype": [tf.float32, tf.int32, tf.int64, tf.string], 37 "index_type": [tf.int32, tf.int64], 38 "input_shape": [[12, 2, 2, 5]], 39 "begin": [[0, 0, 0, 0], [1, 0, 1, 0]], 40 "size": [[8, 2, 2, 3], [11, 2, 1, 5]], 41 "constant_indices": [False], 42 "fully_quantize": [False], 43 }, 44 # 5-D 45 { 46 "dtype": [tf.float32], 47 "index_type": [tf.int32], 48 "input_shape": [[6, 2, 2, 2, 5]], 49 "begin": [[0, 0, 0, 0, 0], [0, 1, 0, 1, 0]], 50 "size": [[4, 2, 2, 2, 3], [5, 2, 1, 1, 5]], 51 "constant_indices": [False], 52 "fully_quantize": [False], 53 }, 54 # 2-D 55 { 56 "dtype": [tf.float32, tf.int32, tf.int64, tf.string], 57 "index_type": [tf.int32, tf.int64], 58 "input_shape": [[2, 3]], 59 "begin": [[0, 0], [1, 0]], 60 "size": [[2, 3], [2, 2]], 61 "constant_indices": [False], 62 "fully_quantize": [False], 63 }, 64 # 4-D with size -1 65 { 66 "dtype": [tf.float32], 67 "index_type": [tf.int32], 68 "input_shape": [[4, 4, 4, 4]], 69 "begin": [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], 70 [0, 0, 0, 1]], 71 "size": [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1], [1, 1, 1, -1]], 72 "constant_indices": [False, True], 73 "fully_quantize": [False], 74 }, 75 # last dimension out of index 76 { 77 "dtype": [tf.float32], 78 "index_type": [tf.int32], 79 "input_shape": [[4, 4, 4]], 80 "begin": [[3, 3, 4]], 81 "size": [[-1, -1, -1]], 82 "constant_indices": [False, True], 83 "fully_quantize": [False], 84 }, 85 # 4-D 86 { 87 "dtype": [tf.float32], 88 "index_type": [tf.int32], 89 "input_shape": [[12, 2, 2, 5]], 90 "begin": [[0, 0, 0, 0], [1, 0, 1, 0]], 91 "size": [[8, 2, 2, 3], [11, 2, 1, 5]], 92 "constant_indices": [True], 93 "fully_quantize": [True], 94 }, 95 # 2-D 96 { 97 "dtype": [tf.float32], 98 "index_type": [tf.int32], 99 "input_shape": [[2, 3]], 100 "begin": [[0, 0], [1, 0]], 101 "size": [[2, 3], [2, 2]], 102 "constant_indices": [True], 103 "fully_quantize": [True], 104 }, 105 # 4-D with size -1 106 { 107 "dtype": [tf.float32], 108 "index_type": [tf.int32], 109 "input_shape": [[4, 4, 4, 4]], 110 "begin": [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], 111 [0, 0, 0, 1]], 112 "size": [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1], [1, 1, 1, -1]], 113 "constant_indices": [True], 114 "fully_quantize": [True], 115 }, 116 { 117 "dtype": [tf.float32], 118 "index_type": [tf.int32], 119 "input_shape": [[1, 4, 4, 4]], 120 "begin": [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], 121 "size": [[-1, 1, 1, 1], [1, -1, 1, 1], [1, 1, -1, 1], [1, 1, 1, -1]], 122 "constant_indices": [True], 123 "fully_quantize": [True], 124 }, 125 ] 126 127 def build_graph(parameters): 128 """Build graph for slice test.""" 129 input_tensor = tf.compat.v1.placeholder( 130 dtype=parameters["dtype"], 131 name="input", 132 shape=parameters["input_shape"]) 133 if parameters["constant_indices"]: 134 index_type = TF_TYPE_INFO[parameters["index_type"]][0] 135 begin_values = np.array(parameters["begin"]).astype(index_type) 136 size_values = np.array(parameters["size"]).astype(index_type) 137 out = tf.slice(input_tensor, begin_values, size_values) 138 return [input_tensor], [out] 139 else: 140 begin = tf.compat.v1.placeholder( 141 dtype=parameters["index_type"], 142 name="begin", 143 shape=[len(parameters["input_shape"])]) 144 size = tf.compat.v1.placeholder( 145 dtype=parameters["index_type"], 146 name="size", 147 shape=[len(parameters["input_shape"])]) 148 tensors = [input_tensor, begin, size] 149 out = tf.slice(input_tensor, begin, size) 150 return tensors, [out] 151 152 def build_inputs(parameters, sess, inputs, outputs): 153 """Build inputs for slice test.""" 154 input_values = create_tensor_data( 155 parameters["dtype"], 156 parameters["input_shape"], 157 min_value=-1, 158 max_value=1) 159 if parameters["constant_indices"]: 160 return [input_values], sess.run( 161 outputs, feed_dict=dict(zip(inputs, [input_values]))) 162 else: 163 index_type = TF_TYPE_INFO[parameters["index_type"]][0] 164 begin_values = np.array(parameters["begin"]).astype(index_type) 165 size_values = np.array(parameters["size"]).astype(index_type) 166 values = [input_values, begin_values, size_values] 167 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 168 169 # Note: Not all [begin x size] permutations are compatible for each grouping 170 # of test_parameters, but for brevity we ignore the failures rather than 171 # separating out each compatible set into separate test_parameters entries. 172 make_zip_of_tests( 173 options, 174 test_parameters, 175 build_graph, 176 build_inputs, 177 expected_tf_failures=29) 178