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