• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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 batchmatmul."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import tensorflow 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("make_batchmatmul_tests")
27def make_batchmatmul_tests(options):
28  """Make a set of tests to do basic batch matrix multiply."""
29
30  test_parameters = [
31      {
32          "dtype": [tf.float32],
33          "shapes": [((3, 4, 7), (7, 9), (3, 4, 7), (7, 9)),
34                     ((None, 4, 5), (None, 5, 6), (3, 4, 5), (3, 5, 6)),
35                     ((None, 1, 3, 4), (None, 4, 2), (2, 1, 3, 4), (5, 4, 2))],
36          "adjoint_b": [False, True],
37          "adjoint_a": [False, True],
38          "rhs_constant": [False],
39          "fully_quantize": [False, True],
40      },
41  ]
42
43  def swap_last_two_dims(*args):
44    """Return a tuple with the last two dimensions swapped."""
45    return args[:-2] + (args[-1],) + (args[-2],)
46
47  def build_graph(parameters):
48    """Build a simple graph with BatchMatMul."""
49    placeholder0_shape = parameters["shapes"][0]
50    adj_a = parameters["adjoint_a"]
51    adj_b = parameters["adjoint_b"]
52    rhs_constant = parameters["rhs_constant"]
53    if adj_a:
54      placeholder0_shape = swap_last_two_dims(*placeholder0_shape)
55    input0_tensor = tf.compat.v1.placeholder(
56        dtype=parameters["dtype"], shape=placeholder0_shape)
57    if rhs_constant:
58      if adj_b:
59        constant1_shape = swap_last_two_dims(*parameters["shapes"][3])
60      else:
61        constant1_shape = parameters["shapes"][3]
62      data = create_tensor_data(
63          parameters["dtype"], constant1_shape, min_value=-1.0, max_value=1.0)
64      input1_constant = tf.constant(
65          data, shape=constant1_shape, dtype=parameters["dtype"])
66      out = tf.matmul(
67          input0_tensor, input1_constant, adjoint_a=adj_a, adjoint_b=adj_b)
68      return [input0_tensor], [out]
69    else:
70      if adj_b:
71        placeholder1_shape = swap_last_two_dims(*parameters["shapes"][1])
72      else:
73        placeholder1_shape = parameters["shapes"][1]
74      input1_tensor = tf.compat.v1.placeholder(
75          dtype=parameters["dtype"], shape=placeholder1_shape)
76      out = tf.matmul(
77          input0_tensor, input1_tensor, adjoint_a=adj_a, adjoint_b=adj_b)
78      return [input0_tensor, input1_tensor], [out]
79
80  def build_inputs(parameters, sess, inputs, outputs):
81    """Feed inputs, assign variables, and freeze graph."""
82    input0_shape = parameters["shapes"][2]
83    adj_a = parameters["adjoint_a"]
84    adj_b = parameters["adjoint_b"]
85    rhs_constant = parameters["rhs_constant"]
86    if adj_a:
87      input0_shape = swap_last_two_dims(*input0_shape)
88    input0_value = create_tensor_data(
89        parameters["dtype"], input0_shape, min_value=-1.0, max_value=1.0)
90    if rhs_constant:
91      output_values = sess.run(
92          outputs, feed_dict=dict(zip(inputs, [input0_value])))
93      return [input0_value], output_values
94    else:
95      input1_shape = parameters["shapes"][3] if not adj_b else \
96          swap_last_two_dims(*parameters["shapes"][3])
97      input1_value = create_tensor_data(
98          parameters["dtype"], input1_shape, min_value=-1.0, max_value=1.0)
99      output_values = sess.run(
100          outputs, feed_dict=dict(zip(inputs, [input0_value, input1_value])))
101      return [input0_value, input1_value], output_values
102
103  options.use_experimental_converter = True
104  make_zip_of_tests(
105      options,
106      test_parameters,
107      build_graph,
108      build_inputs,
109      expected_tf_failures=0)
110