1# 2# Copyright (C) 2018 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17layout = BoolScalar("layout", False) # NHWC 18 19# TEST 1: dilation set to 1 (default) 20i1 = Input("op1", "TENSOR_FLOAT32", "{1, 3, 3, 1}") 21f1 = Parameter("op2", "TENSOR_FLOAT32", "{1, 2, 2, 1}", [.25, .25, .25, .25]) 22b1 = Parameter("op3", "TENSOR_FLOAT32", "{1}", [0]) 23o1 = Output("op4", "TENSOR_FLOAT32", "{1, 2, 2, 1}") 24Model().Operation("CONV_2D", i1, f1, b1, 0, 0, 0, 0, 1, 1, 0, layout, 1, 1).To(o1) 25 26# Additional data type 27quant8 = DataTypeConverter().Identify({ 28 i1: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 29 f1: ("TENSOR_QUANT8_ASYMM", 0.125, 0), 30 b1: ("TENSOR_INT32", 0.0625, 0), 31 o1: ("TENSOR_QUANT8_ASYMM", 0.125, 0) 32}) 33 34# Instantiate an example 35example = Example({ 36 i1: [1.0, 1.0, 1.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0], 37 o1: [.875, .875, .875, .875] 38}).AddNchw(i1, o1, layout).AddVariations("relaxed", quant8, "float16") 39 40 41# TEST 2: dilation set to 3 42i2 = Input("op1", "TENSOR_FLOAT32", "{1, 9, 9, 1}") 43f2 = Parameter("op2", "TENSOR_FLOAT32", "{1, 3, 3, 1}", [1, 2, 3, 4, 5, 6, 7, 8, 9]) 44b2 = Parameter("op3", "TENSOR_FLOAT32", "{1}", [0]) 45o2 = Output("op4", "TENSOR_FLOAT32", "{1, 3, 3, 1}") 46Model().Operation("CONV_2D", i2, f2, b2, 0, 0, 0, 0, 1, 1, 0, layout, 3, 3).To(o2) 47 48# Additional data type 49quant8 = DataTypeConverter().Identify({ 50 i2: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 51 f2: ("TENSOR_QUANT8_ASYMM", 0.125, 0), 52 b2: ("TENSOR_INT32", 0.0625, 0), 53 o2: ("TENSOR_QUANT8_ASYMM", 0.125, 0) 54}) 55 56# Instantiate an example 57example = Example({ 58 i2: [0, 0, 0, 0, 0, 0, 0, 0, 0, 59 0, 0, 0, 0, 0, 0, 0, 0, 0, 60 0, 0, 0, 0, 0, 0, 0, 0, 0, 61 0, 0, 0, 1, 1, 1, 0, 0, 0, 62 0, 0, 0, 1, 1, 1, 0, 0, 0, 63 0, 0, 0, 1, 1, 1, 0, 0, 0, 64 0, 0, 0, 0, 0, 0, 0, 0, 0, 65 0, 0, 0, 0, 0, 0, 0, 0, 0, 66 0, 0, 0, 0, 0, 0, 0, 0, 0], 67 o2: [5, 5, 5, 5, 5, 5, 5, 5, 5] 68}).AddNchw(i2, o2, layout).AddVariations("relaxed", quant8, "float16") 69 70# TEST 3: same as test 1 but with implicit VALID padding 71i1 = Input("op1", "TENSOR_FLOAT32", "{1, 3, 3, 1}") 72f1 = Parameter("op2", "TENSOR_FLOAT32", "{1, 2, 2, 1}", [.25, .25, .25, .25]) 73b1 = Parameter("op3", "TENSOR_FLOAT32", "{1}", [0]) 74o1 = Output("op4", "TENSOR_FLOAT32", "{1, 2, 2, 1}") 75Model().Operation("CONV_2D", i1, f1, b1, 2, 1, 1, 0, layout, 1, 1).To(o1) 76 77# Additional data type 78quant8 = DataTypeConverter().Identify({ 79 i1: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 80 f1: ("TENSOR_QUANT8_ASYMM", 0.125, 0), 81 b1: ("TENSOR_INT32", 0.0625, 0), 82 o1: ("TENSOR_QUANT8_ASYMM", 0.125, 0) 83}) 84 85# Instantiate an example 86example = Example({ 87 i1: [1.0, 1.0, 1.0, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0], 88 o1: [.875, .875, .875, .875] 89}, name="valid_padding").AddNchw(i1, o1, layout).AddVariations("relaxed", quant8, "float16") 90 91 92# TEST 4: same as test 2 but with implicit VALID padding 93i2 = Input("op1", "TENSOR_FLOAT32", "{1, 9, 9, 1}") 94f2 = Parameter("op2", "TENSOR_FLOAT32", "{1, 3, 3, 1}", [1, 2, 3, 4, 5, 6, 7, 8, 9]) 95b2 = Parameter("op3", "TENSOR_FLOAT32", "{1}", [0]) 96o2 = Output("op4", "TENSOR_FLOAT32", "{1, 3, 3, 1}") 97Model().Operation("CONV_2D", i2, f2, b2, 2, 1, 1, 0, layout, 3, 3).To(o2) 98 99# Additional data type 100quant8 = DataTypeConverter().Identify({ 101 i2: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 102 f2: ("TENSOR_QUANT8_ASYMM", 0.125, 0), 103 b2: ("TENSOR_INT32", 0.0625, 0), 104 o2: ("TENSOR_QUANT8_ASYMM", 0.125, 0) 105}) 106 107# Instantiate an example 108example = Example({ 109 i2: [0, 0, 0, 0, 0, 0, 0, 0, 0, 110 0, 0, 0, 0, 0, 0, 0, 0, 0, 111 0, 0, 0, 0, 0, 0, 0, 0, 0, 112 0, 0, 0, 1, 1, 1, 0, 0, 0, 113 0, 0, 0, 1, 1, 1, 0, 0, 0, 114 0, 0, 0, 1, 1, 1, 0, 0, 0, 115 0, 0, 0, 0, 0, 0, 0, 0, 0, 116 0, 0, 0, 0, 0, 0, 0, 0, 0, 117 0, 0, 0, 0, 0, 0, 0, 0, 0], 118 o2: [5, 5, 5, 5, 5, 5, 5, 5, 5] 119}, name="valid_padding").AddNchw(i2, o2, layout).AddVariations("relaxed", quant8, "float16") 120 121 122# TEST 5: dilation set to 3, SAME padding 123i3 = Input("op1", "TENSOR_FLOAT32", "{1, 6, 6, 1}") 124f3 = Parameter("op2", "TENSOR_FLOAT32", "{1, 2, 2, 1}", [1, 2, 3, 4]) 125b3 = Parameter("op3", "TENSOR_FLOAT32", "{1}", [0]) 126o3 = Output("op4", "TENSOR_FLOAT32", "{1, 3, 3, 1}") 127Model().Operation("CONV_2D", i3, f3, b3, 1, 2, 2, 0, layout, 3, 3).To(o3) 128 129# Additional data type 130quant8 = DataTypeConverter().Identify({ 131 i3: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 132 f3: ("TENSOR_QUANT8_ASYMM", 0.125, 0), 133 b3: ("TENSOR_INT32", 0.0625, 0), 134 o3: ("TENSOR_QUANT8_ASYMM", 0.125, 0) 135}) 136 137# Instantiate an example 138example = Example({ 139 i3: [0, 0, 0, 0, 0, 0, 140 0, 0, 0, 0, 0, 0, 141 0, 0, 4, 3, 0, 0, 142 0, 0, 2, 1, 0, 0, 143 0, 0, 0, 0, 0, 0, 144 0, 0, 0, 0, 0, 0], 145 o3: [16, 0, 9, 0, 0, 0, 4, 0, 1] 146}).AddNchw(i3, o3, layout).AddVariations("relaxed", quant8, "float16") 147 148# The tests below can comply with a lower version because the runtime removes 149# optional arguments set to default values. 150Example.SetVersion("V1_0", 151 "conv2d_dilation_nhwc", 152 "conv2d_dilation_nhwc_all_inputs_as_internal", 153 "conv2d_dilation_nhwc_all_tensors_as_inputs", 154 "conv2d_dilation_nhwc_all_tensors_as_inputs_all_inputs_as_internal", 155 "conv2d_dilation_nhwc_quant8", 156 "conv2d_dilation_nhwc_quant8_all_inputs_as_internal", 157 "conv2d_dilation_nhwc_quant8_all_tensors_as_inputs", 158 "conv2d_dilation_nhwc_quant8_all_tensors_as_inputs_all_inputs_as_internal", 159 "conv2d_dilation_valid_padding_nhwc", 160 "conv2d_dilation_valid_padding_nhwc_all_inputs_as_internal", 161 "conv2d_dilation_valid_padding_nhwc_all_tensors_as_inputs", 162 "conv2d_dilation_valid_padding_nhwc_all_tensors_as_inputs_all_inputs_as_internal", 163 "conv2d_dilation_valid_padding_nhwc_quant8", 164 "conv2d_dilation_valid_padding_nhwc_quant8_all_inputs_as_internal", 165 "conv2d_dilation_valid_padding_nhwc_quant8_all_tensors_as_inputs", 166 "conv2d_dilation_valid_padding_nhwc_quant8_all_tensors_as_inputs_all_inputs_as_internal") 167