• 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 cond."""
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
25from tensorflow.python.framework import test_util
26
27
28@register_make_test_function("make_cond_tests")
29@test_util.enable_control_flow_v2
30def make_cond_tests(options):
31  """Make a set of tests to do relu1."""
32
33  # Chose a set of parameters
34  test_parameters = [{
35      # Note: The `tf.string` test case also serves as a regression test to
36      # ensure that branch subgraph with dynamically allocated inputs/outputs
37      # are handled correctly.
38      "dtype": [tf.float32, tf.string],
39      "pred": [False, True],
40  }]
41
42  def build_graph(parameters):
43    """Build the graph for cond tests."""
44    input1 = tf.placeholder(dtype=parameters["dtype"], shape=(1,))
45    input2 = tf.placeholder(dtype=parameters["dtype"], shape=(1,))
46    # MLIR TFLite converter can't handle scalar inputs. This is a workaround
47    # to input (1,) tensors and then reshape to scalar.
48    # TODO(b/129003347): Remove the workaround after scalar inputs are
49    # supported.
50    pred = tf.placeholder(dtype=tf.bool, shape=(1,))
51    pred_scalar = tf.reshape(pred, ())
52
53    out = tf.cond(pred_scalar, lambda: input1, lambda: input2)
54    return [input1, input2, pred], [out]
55
56  def build_inputs(parameters, sess, inputs, outputs):
57    input_values = [
58        create_tensor_data(parameters["dtype"], (1,)),
59        create_tensor_data(parameters["dtype"], (1,)),
60        np.array([parameters["pred"]], dtype=np.bool_),
61    ]
62    return input_values, sess.run(
63        outputs, feed_dict=dict(zip(inputs, input_values)))
64
65  make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
66