• 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 fully_connected."""
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
25
26
27@register_make_test_function()
28def make_fully_connected_tests(options):
29  """Make a set of tests to do fully_connected."""
30
31  test_parameters = [{
32      "shape1": [[3, 3]],
33      "shape2": [[3, 3]],
34      "transpose_a": [True, False],
35      "transpose_b": [True, False],
36      "constant_filter": [True, False],
37      "fully_quantize": [False],
38      "quant_16x8": [False]
39  }, {
40      "shape1": [[4, 4], [1, 4], [4]],
41      "shape2": [[4, 4], [4, 1], [4]],
42      "transpose_a": [False],
43      "transpose_b": [False],
44      "constant_filter": [True, False],
45      "fully_quantize": [False],
46      "quant_16x8": [False]
47  }, {
48      "shape1": [[40, 37]],
49      "shape2": [[37, 40]],
50      "transpose_a": [False],
51      "transpose_b": [False],
52      "constant_filter": [True, False],
53      "fully_quantize": [False],
54      "quant_16x8": [False]
55  }, {
56      "shape1": [[40, 37]],
57      "shape2": [[40, 37]],
58      "transpose_a": [False],
59      "transpose_b": [True],
60      "constant_filter": [True, False],
61      "fully_quantize": [False],
62      "quant_16x8": [False]
63  }, {
64      "shape1": [[5, 3]],
65      "shape2": [[5, 3]],
66      "transpose_a": [True],
67      "transpose_b": [False],
68      "constant_filter": [True, False],
69      "fully_quantize": [False],
70      "quant_16x8": [False]
71  }, {
72      "shape1": [[1, 3]],
73      "shape2": [[3, 3]],
74      "transpose_a": [False],
75      "transpose_b": [False],
76      "constant_filter": [True],
77      "fully_quantize": [True],
78      "quant_16x8": [False]
79  }, {
80      "shape1": [[1, 4], [4]],
81      "shape2": [[4, 4], [4, 1], [4]],
82      "transpose_a": [False],
83      "transpose_b": [False],
84      "constant_filter": [True],
85      "fully_quantize": [True],
86      "quant_16x8": [False]
87  }, {
88      "shape1": [[1, 37], [2, 37]],
89      "shape2": [[37, 40]],
90      "transpose_a": [False],
91      "transpose_b": [False],
92      "constant_filter": [True],
93      "fully_quantize": [True],
94      "quant_16x8": [False]
95  }, {
96      "shape1": [[1, 3], [2, 3]],
97      "shape2": [[3, 5], [3, 1]],
98      "transpose_a": [False],
99      "transpose_b": [False],
100      "constant_filter": [True],
101      "fully_quantize": [True],
102      "quant_16x8": [False]
103  }, {
104      "shape1": [[2, 3]],
105      "shape2": [[3, 5]],
106      "transpose_a": [False],
107      "transpose_b": [False],
108      "constant_filter": [True],
109      "fully_quantize": [True],
110      "quant_16x8": [True]
111  }]
112
113  if options.use_experimental_converter:
114    test_parameters = test_parameters + [
115        # Zero in input shape.
116        {
117            "shape1": [[0, 3]],
118            "shape2": [[3, 3]],
119            "transpose_a": [False],
120            "transpose_b": [False],
121            "constant_filter": [True, False],
122            "fully_quantize": [False],
123            "quant_16x8": [False]
124        }
125    ]
126
127  def build_graph(parameters):
128    """Build a matmul graph given `parameters`."""
129    input_tensor1 = tf.compat.v1.placeholder(
130        dtype=tf.float32, name="input1", shape=parameters["shape1"])
131
132    # Get input_tensor2 either as a placeholder or constants. Also get a list of
133    # the input tensors that are represented as placeholders.
134    if parameters["constant_filter"]:
135      input_tensor2 = create_tensor_data(
136          np.float32, parameters["shape2"], min_value=-1, max_value=1)
137      input_tensors = [input_tensor1]
138    else:
139      input_tensor2 = tf.compat.v1.placeholder(
140          dtype=tf.float32, name="input2", shape=parameters["shape2"])
141      input_tensors = [input_tensor1, input_tensor2]
142
143    out = tf.matmul(
144        input_tensor1,
145        input_tensor2,
146        transpose_a=parameters["transpose_a"],
147        transpose_b=parameters["transpose_b"])
148    return input_tensors, [out]
149
150  def build_inputs(parameters, sess, inputs, outputs):
151    # pylint: disable=g-doc-return-or-yield, g-doc-args
152    """Build list of input values.
153
154    It either contains 1 tensor (input_values1) or
155    2 tensors (input_values1, input_values2) based on whether the second input
156    is a constant or variable input.
157    """
158
159    values = [
160        create_tensor_data(
161            np.float32, shape=parameters["shape1"], min_value=-1, max_value=1)
162    ]
163    if not parameters["constant_filter"]:
164      values.append(
165          create_tensor_data(
166              np.float32, parameters["shape2"], min_value=-1, max_value=1))
167    return values, sess.run(outputs, feed_dict=dict(zip(inputs, values)))
168
169  make_zip_of_tests(
170      options,
171      test_parameters,
172      build_graph,
173      build_inputs,
174      expected_tf_failures=14)
175