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