• 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 elementwise ops."""
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
26def _make_elementwise_tests(op, allow_fully_quantize=False, min_value=-100,
27                            max_value=100):
28  """Make a set of tests to do element-wise operations."""
29
30  def f(options):
31    """Actual function that generates examples."""
32    test_parameters = [
33        {
34            "input_dtype": [tf.float32],
35            "input_shape": [[], [1], [1, 2], [5, 6, 7, 8], [3, 4, 5, 6]],
36            "fully_quantize": [False],
37            "input_range": [[min_value, max_value]],
38        },
39        {
40            "input_dtype": [tf.float32],
41            "input_shape": [[], [1], [1, 2], [5, 6, 7, 8], [3, 4, 5, 6]],
42            "fully_quantize": [True],
43            "input_range": [[min_value, max_value]],
44        },
45    ]
46
47    if not allow_fully_quantize:
48      test_parameters = [
49          test_parameter for test_parameter in test_parameters
50          if True not in test_parameter["fully_quantize"]
51      ]
52
53    def build_graph(parameters):
54      """Build the unary op testing graph."""
55      input_value = tf.compat.v1.placeholder(
56          dtype=parameters["input_dtype"],
57          name="input1",
58          shape=parameters["input_shape"])
59      out = op(input_value)
60      return [input_value], [out]
61
62    def build_inputs(parameters, sess, inputs, outputs):
63      input_value = create_tensor_data(parameters["input_dtype"],
64                                       parameters["input_shape"],
65                                       min_value=min_value,
66                                       max_value=max_value)
67      return [input_value], sess.run(
68          outputs, feed_dict={inputs[0]: input_value})
69
70    make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
71
72  return f
73
74
75@register_make_test_function()
76def make_sin_tests(options):
77  """Make a set of tests to do sin."""
78  return _make_elementwise_tests(tf.sin)(options)
79
80
81@register_make_test_function()
82def make_log_tests(options):
83  """Make a set of tests to do log."""
84  return _make_elementwise_tests(tf.math.log)(options)
85
86
87@register_make_test_function()
88def make_sqrt_tests(options):
89  """Make a set of tests to do sqrt."""
90  return _make_elementwise_tests(tf.sqrt)(options)
91
92
93@register_make_test_function()
94def make_rsqrt_tests(options):
95  """Make a set of tests to do 1/sqrt."""
96  return _make_elementwise_tests(tf.math.rsqrt, allow_fully_quantize=True,
97                                 min_value=.1, max_value=1)(options)
98
99
100@register_make_test_function()
101def make_square_tests(options):
102  """Make a set of tests to do square."""
103  return _make_elementwise_tests(tf.square)(options)
104