• 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 transpose."""
16import numpy as np
17import tensorflow.compat.v1 as tf
18from tensorflow.lite.testing.zip_test_utils import create_tensor_data
19from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
20from tensorflow.lite.testing.zip_test_utils import register_make_test_function
21
22
23@register_make_test_function()
24def make_transpose_tests(options):
25  """Make a set of tests to do transpose."""
26
27  # TODO(nupurgarg): Add test for uint8.
28  test_parameters = [{
29      "dtype": [tf.int32, tf.int64, tf.float32],
30      "input_shape": [[2, 2, 3]],
31      "perm": [[0, 1, 2], [0, 2, 1]],
32      "constant_perm": [True, False],
33      "fully_quantize": [False],
34  }, {
35      "dtype": [tf.float32],
36      "input_shape": [[1, 2, 3, 4]],
37      "perm": [[0, 1, 2, 3], [3, 0, 1, 2]],
38      "constant_perm": [True, False],
39      "fully_quantize": [False],
40  }, {
41      "dtype": [tf.float32],
42      "input_shape": [[1, 2, 3, 4, 5]],
43      "perm": [[4, 3, 2, 1, 0]],
44      "constant_perm": [True, False],
45      "fully_quantize": [False],
46  }, {
47      "dtype": [tf.float32],
48      "input_shape": [[2, 2, 3]],
49      "perm": [[0, 1, 2], [0, 2, 1]],
50      "constant_perm": [True],
51      "fully_quantize": [True],
52  }, {
53      "dtype": [tf.float32],
54      "input_shape": [[1, 2, 3, 4]],
55      "perm": [[0, 1, 2, 3], [3, 0, 1, 2]],
56      "constant_perm": [True],
57      "fully_quantize": [True],
58  }, {
59      "dtype": [tf.float32],
60      "input_shape": [[1, 2, 3, 4, 5]],
61      "perm": [[0, 1, 2, 3, 4], [3, 4, 0, 1, 2]],
62      "constant_perm": [True],
63      "fully_quantize": [True, False],
64  }]
65
66  def build_graph(parameters):
67    """Build a transpose graph given `parameters`."""
68    input_tensor = tf.compat.v1.placeholder(
69        dtype=parameters["dtype"],
70        name="input",
71        shape=parameters["input_shape"])
72
73    if parameters["constant_perm"]:
74      perm = parameters["perm"]
75      input_tensors = [input_tensor]
76    else:
77      shape = [len(parameters["perm"]), 2]
78      perm = tf.compat.v1.placeholder(dtype=tf.int32, name="perm", shape=shape)
79      input_tensors = [input_tensor, perm]
80
81    out = tf.transpose(input_tensor, perm=perm)
82    return input_tensors, [out]
83
84  def build_inputs(parameters, sess, inputs, outputs):
85    values = [
86        create_tensor_data(parameters["dtype"], parameters["input_shape"],
87                           min_value=-1, max_value=1)
88    ]
89    if not parameters["constant_perm"]:
90      values.append(np.array(parameters["perm"]))
91    return values, sess.run(outputs, feed_dict=dict(zip(inputs, values)))
92
93  make_zip_of_tests(
94      options,
95      test_parameters,
96      build_graph,
97      build_inputs,
98      expected_tf_failures=9)
99