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 hardswish.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import functools 21 22import numpy as np 23import tensorflow as tf 24from tensorflow.lite.testing.zip_test_utils import create_tensor_data 25from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 26from tensorflow.lite.testing.zip_test_utils import register_make_test_function 27 28 29def _tflite_convert_verify_num_ops(tflite_convert_function, *args, **kwargs): 30 """Verifies that the result of the conversion is a single op.""" 31 num_ops = kwargs.pop("num_ops", 2) 32 result = tflite_convert_function(*args, **kwargs) 33 tflite_model_binary = result[0] 34 if not result[0]: 35 tf.compat.v1.logging.error(result[1]) # stderr from running tflite_convert. 36 raise RuntimeError("Failed to bulid model: \n\n" + result[1]) 37 interpreter = tf.lite.Interpreter(model_content=tflite_model_binary) 38 interpreter.allocate_tensors() 39 if len(interpreter.get_tensor_details()) != num_ops: 40 raise RuntimeError( 41 "Expected to generate two node graph got %s " % 42 "\n".join(str(x) for x in interpreter.get_tensor_details())) 43 return result 44 45 46@register_make_test_function() 47def make_hardswish_tests(options): 48 """Make a set of tests to do hardswish.""" 49 50 # Chose a set of parameters 51 test_parameters = [{ 52 "input_shape": [[], [1], [2, 3], [1, 1, 1, 1], [1, 3, 4, 3], 53 [3, 15, 14, 3], [3, 1, 2, 4, 6], [2, 2, 3, 4, 5, 6]], 54 }] 55 56 def build_graph(parameters): 57 inp = tf.compat.v1.placeholder( 58 dtype=tf.float32, name="input", shape=parameters["input_shape"]) 59 out = inp * tf.nn.relu6(inp + np.float32(3)) * np.float32(1. / 6.) 60 61 return [inp], [out] 62 63 def build_inputs(parameters, sess, inputs, outputs): 64 input_values = create_tensor_data( 65 np.float32, parameters["input_shape"], min_value=-10, max_value=10) 66 return [input_values], sess.run( 67 outputs, feed_dict=dict(zip(inputs, [input_values]))) 68 69 # Add additional validation if we are using toco. 70 # Flex doesn't yet support this. 71 if not options.run_with_flex: 72 options.tflite_convert_function = functools.partial( 73 _tflite_convert_verify_num_ops, 74 options.tflite_convert_function, 75 num_ops=2) 76 make_zip_of_tests(options, test_parameters, build_graph, build_inputs) 77