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 unroll_batch_matmul.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import tensorflow.compat.v1 as tf 21from tensorflow.lite.testing.zip_test_utils import create_tensor_data 22from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 23from tensorflow.lite.testing.zip_test_utils import register_make_test_function 24 25 26@register_make_test_function() 27def make_unroll_batch_matmul_tests(options): 28 """Make a set of tests to test unroll_batch_matmul.""" 29 30 # The test cases below requires broadcasting support (BatchMatMulV2 semantic), 31 # whis isn't supported as of this change. 32 broadcast_shape_params = [ 33 # Simple broadcast. 34 [(1, 2, 3), (3, 5), False, False], 35 # Empty batch broadcast. 36 [(2, 5, 3), (3, 7), False, False], 37 # Single batch with non-empty batch broadcast. 38 [(1, 5, 3), (4, 3, 7), False, False], 39 # Broadcast both operands 40 [(3, 1, 5, 3), (1, 4, 3, 7), False, False], 41 ] 42 43 test_parameters = [{ 44 "dtype": [tf.float32], 45 "shape": [[(2, 2, 3), 46 (2, 3, 2), False, False], [(2, 2, 3), (2, 3, 2), True, True], 47 [(2, 2, 3), 48 (2, 2, 3), False, True], [(2, 2, 3), (2, 2, 3), True, False], 49 [(4, 2, 2, 3), (4, 2, 3, 2), False, False], 50 [(4, 2, 2, 3), (4, 2, 3, 2), True, True], 51 [(4, 2, 2, 3), (4, 2, 2, 3), False, True], 52 [(4, 2, 2, 3), 53 (4, 2, 2, 3), True, False]] + broadcast_shape_params, 54 }] 55 56 def build_graph(parameters): 57 """Build the batch_matmul op testing graph.""" 58 59 def _build_graph(): 60 """Build the graph.""" 61 input_tensor1 = tf.compat.v1.placeholder( 62 dtype=parameters["dtype"], shape=parameters["shape"][0]) 63 input_tensor2 = tf.compat.v1.placeholder( 64 dtype=parameters["dtype"], shape=parameters["shape"][1]) 65 # Should be unrolled and replaced with fully_connected ops in the end. 66 out = tf.matmul( 67 input_tensor1, 68 input_tensor2, 69 transpose_a=parameters["shape"][2], 70 transpose_b=parameters["shape"][3]) 71 return [input_tensor1, input_tensor2], [out] 72 73 return _build_graph() 74 75 def build_inputs(parameters, sess, inputs, outputs): 76 input_value1 = create_tensor_data( 77 parameters["dtype"], shape=parameters["shape"][0]) 78 input_value2 = create_tensor_data( 79 parameters["dtype"], shape=parameters["shape"][1]) 80 return [input_value1, input_value2], sess.run( 81 outputs, feed_dict=dict(zip(inputs, [input_value1, input_value2]))) 82 83 make_zip_of_tests(options, test_parameters, build_graph, build_inputs) 84