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 pack.""" 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 25 26 27@register_make_test_function() 28def make_pack_tests(options): 29 """Make a set of tests to do stack.""" 30 31 test_parameters = [ 32 # Avoid creating all combinations to keep the test size small. 33 { 34 "dtype": [tf.float32], 35 "base_shape": [[3, 4, 3], [3, 4], [5]], 36 "num_tensors": [1, 2, 3, 4, 5, 6], 37 "axis": [0, 1, 2, 3], 38 "additional_shape": [1, 2, 3], 39 "fully_quantize": [False], 40 }, 41 { 42 "dtype": [tf.int32], 43 "base_shape": [[3, 4, 3], [3, 4], [5]], 44 "num_tensors": [6], 45 "axis": [0, 1, 2, 3], 46 "additional_shape": [1, 2, 3], 47 "fully_quantize": [False], 48 }, 49 { 50 "dtype": [tf.int64], 51 "base_shape": [[3, 4, 3], [3, 4], [5]], 52 "num_tensors": [5], 53 "axis": [0, 1, 2, 3], 54 "additional_shape": [1, 2, 3], 55 "fully_quantize": [False], 56 }, 57 { 58 "dtype": [tf.float32], 59 "base_shape": [[1, 4, 3], [3, 4], [5]], 60 "num_tensors": [2, 3, 4, 5, 6], # 1 tensor would go to Reshape. 61 "axis": [0, 1, 2, 3], 62 "additional_shape": [1, 2, 3], 63 "fully_quantize": [True], 64 }, 65 ] 66 67 def get_shape(parameters): 68 """Return a tweaked version of 'base_shape'.""" 69 axis = parameters["axis"] 70 shape = parameters["base_shape"][:] 71 if axis < len(shape): 72 shape[axis] += parameters["additional_shape"] 73 return shape 74 75 def build_graph(parameters): 76 all_tensors = [] 77 for n in range(0, parameters["num_tensors"]): 78 input_tensor = tf.compat.v1.placeholder( 79 dtype=parameters["dtype"], 80 name=("input%d" % n), 81 shape=get_shape(parameters)) 82 all_tensors.append(input_tensor) 83 out = tf.stack(all_tensors, parameters["axis"]) 84 return all_tensors, [out] 85 86 def build_inputs(parameters, sess, inputs, outputs): 87 all_values = [] 88 for _ in range(0, parameters["num_tensors"]): 89 input_values = create_tensor_data( 90 np.float32, get_shape(parameters), min_value=-1, max_value=1) 91 all_values.append(input_values) 92 return all_values, sess.run( 93 outputs, feed_dict=dict(zip(inputs, all_values))) 94 95 make_zip_of_tests( 96 options, 97 test_parameters, 98 build_graph, 99 build_inputs, 100 expected_tf_failures=117) 101