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 def build_graph(parameters): 114 """Build a matmul graph given `parameters`.""" 115 input_tensor1 = tf.compat.v1.placeholder( 116 dtype=tf.float32, name="input1", shape=parameters["shape1"]) 117 118 # Get input_tensor2 either as a placeholder or constants. Also get a list of 119 # the input tensors that are represented as placeholders. 120 if parameters["constant_filter"]: 121 input_tensor2 = create_tensor_data( 122 np.float32, parameters["shape2"], min_value=-1, max_value=1) 123 input_tensors = [input_tensor1] 124 else: 125 input_tensor2 = tf.compat.v1.placeholder( 126 dtype=tf.float32, name="input2", shape=parameters["shape2"]) 127 input_tensors = [input_tensor1, input_tensor2] 128 129 out = tf.matmul( 130 input_tensor1, 131 input_tensor2, 132 transpose_a=parameters["transpose_a"], 133 transpose_b=parameters["transpose_b"]) 134 return input_tensors, [out] 135 136 def build_inputs(parameters, sess, inputs, outputs): 137 # pylint: disable=g-doc-return-or-yield, g-doc-args 138 """Build list of input values. 139 140 It either contains 1 tensor (input_values1) or 141 2 tensors (input_values1, input_values2) based on whether the second input 142 is a constant or variable input. 143 """ 144 145 values = [ 146 create_tensor_data( 147 np.float32, shape=parameters["shape1"], min_value=-1, max_value=1) 148 ] 149 if not parameters["constant_filter"]: 150 values.append( 151 create_tensor_data( 152 np.float32, parameters["shape2"], min_value=-1, max_value=1)) 153 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 154 155 make_zip_of_tests( 156 options, 157 test_parameters, 158 build_graph, 159 build_inputs, 160 expected_tf_failures=14) 161