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 conv_with_shared_weights.""" 16import numpy as np 17import tensorflow.compat.v1 as tf 18from tensorflow.lite.testing.zip_test_utils import create_tensor_data 19from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 20from tensorflow.lite.testing.zip_test_utils import register_make_test_function 21 22 23@register_make_test_function() 24def make_conv_with_shared_weights_tests(options): 25 """Make a test where 2 Conv ops shared the same constant weight tensor.""" 26 27 test_parameters = [{ 28 "input_shape": [[1, 10, 10, 3]], 29 "filter_shape": [[3, 3]], 30 "strides": [[1, 1, 1, 1]], 31 "dilations": [[1, 1, 1, 1]], 32 "padding": ["SAME"], 33 "data_format": ["NHWC"], 34 "channel_multiplier": [1], 35 "dynamic_range_quantize": [False, True], 36 }] 37 38 def get_tensor_shapes(parameters): 39 input_shape = parameters["input_shape"] 40 filter_size = parameters["filter_shape"] 41 filter_shape = filter_size + [ 42 input_shape[3], parameters["channel_multiplier"] 43 ] 44 return [input_shape, filter_shape] 45 46 def build_graph(parameters): 47 """Build a conv graph given `parameters`.""" 48 input_shape, filter_shape = get_tensor_shapes(parameters) 49 input_tensor = tf.compat.v1.placeholder( 50 dtype=tf.float32, name="input", shape=input_shape) 51 input_tensors = [input_tensor] 52 53 # Construct a constant weights tensor which will be used by both Conv2D. 54 filter_tensor = tf.constant( 55 create_tensor_data(np.float32, filter_shape), dtype=tf.float32) 56 57 # Ensure that FuseBinaryIntoFollowingAffine works with an input which 58 # is shared by multiple affine ops. 59 conv_input = input_tensor + 0.1 60 61 # Construct 2 Conv2D operations which use exactly the same input and 62 # weights. 63 result1 = tf.nn.conv2d( 64 conv_input, 65 filter_tensor, 66 strides=parameters["strides"], 67 dilations=parameters["dilations"], 68 padding=parameters["padding"], 69 data_format=parameters["data_format"]) 70 result2 = tf.nn.conv2d( 71 conv_input, 72 filter_tensor, 73 strides=parameters["strides"], 74 dilations=parameters["dilations"], 75 padding=parameters["padding"], 76 data_format=parameters["data_format"]) 77 # Add MUL ops after Conv2D ops. These MUL ops should be fused into the 78 # weights of Conv2D. 79 result1 = result1 * 2 80 result2 = result2 * 3 81 # Add the 2 results up. 82 out = result1 + result2 83 return input_tensors, [out] 84 85 def build_inputs(parameters, sess, inputs, outputs): 86 # Build list of input values either containing 1 tensor (input) or 2 tensors 87 # (input, filter) based on whether filter is constant or variable input. 88 input_shape, unused_filter_shape = get_tensor_shapes(parameters) 89 values = [create_tensor_data(np.float32, input_shape)] 90 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 91 92 make_zip_of_tests(options, test_parameters, build_graph, build_inputs) 93