• 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 identity."""
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.ops import array_ops
26
27
28@register_make_test_function()
29def make_identity_tests(options):
30  """Make a set of tests to do identity."""
31
32  # Chose a set of parameters
33  test_parameters = [{
34      "input_shape": [[], [1], [3, 3]],
35      "op_to_use": [
36          "identity", "identity_n", "snapshot", "identity_n_with_2_inputs"
37      ],
38  }]
39
40  def build_graph(parameters):
41    """Make a set of tests to do identity."""
42
43    input_tensors = []
44    input_count = (2 if parameters["op_to_use"] == "identity_n_with_2_inputs"
45                   else 1)
46    input_tensors = [
47        tf.compat.v1.placeholder(
48            dtype=tf.float32, name="input", shape=parameters["input_shape"])
49        for _ in range(input_count)
50    ]
51
52    # We add the Multiply before Identity just as a walk-around to make the test
53    # pass when input_shape is scalar.
54    # During graph transformation, TOCO will replace the Identity op with
55    # Reshape when input has shape. However, currently TOCO can't distinguish
56    # between missing shape and scalar shape. As a result, when input has scalar
57    # shape, this conversion still fails.
58    # TODO(b/129197312), remove the walk-around code once the bug is fixed.
59    inputs_doubled = [input_tensor * 2.0 for input_tensor in input_tensors]
60    if parameters["op_to_use"] == "identity":
61      identity_outputs = [tf.identity(inputs_doubled[0])]
62    elif parameters["op_to_use"] == "snapshot":
63      identity_outputs = [array_ops.snapshot(inputs_doubled[0])]
64    elif parameters["op_to_use"] in ("identity_n", "identity_n_with_2_inputs"):
65      identity_outputs = tf.identity_n(inputs_doubled)
66    return input_tensors, identity_outputs
67
68  def build_inputs(parameters, sess, inputs, outputs):
69    input_values = [
70        create_tensor_data(
71            np.float32, parameters["input_shape"], min_value=-4, max_value=10)
72        for _ in range(len(inputs))
73    ]
74
75    return input_values, sess.run(
76        outputs, feed_dict=dict(zip(inputs, input_values)))
77
78  make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
79