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: GROUPED_CONV2D, pad = 0, stride = 1, numGroups = 2 20i1 = Input("op1", "TENSOR_FLOAT32", "{1, 3, 3, 2}") # input 0 21w1 = Parameter("op2", "TENSOR_FLOAT32", "{2, 2, 2, 1}", [1, 2, 2, 1, 4, 3, 2, 1]) # weight 22b1 = Parameter("op3", "TENSOR_FLOAT32", "{2}", [10, -33.5]) # bias 23act = Int32Scalar("act", 0) # act = none 24o1 = Output("op4", "TENSOR_FLOAT32", "{1, 2, 2, 2}") # output 0 25Model().Operation("GROUPED_CONV_2D", i1, w1, b1, 0, 0, 0, 0, 1, 1, 2, act, layout).To(o1) 26 27# Additional data type 28quant8 = DataTypeConverter().Identify({ 29 i1: ("TENSOR_QUANT8_ASYMM", 0.25, 100), 30 w1: ("TENSOR_QUANT8_ASYMM", 0.25, 128), 31 b1: ("TENSOR_INT32", 0.0625, 0), 32 o1: ("TENSOR_QUANT8_ASYMM", 0.5, 80) 33}) 34 35quant8_mult_gt_1 = DataTypeConverter().Identify({ 36 i1: ("TENSOR_QUANT8_ASYMM", 0.25, 100), 37 w1: ("TENSOR_QUANT8_ASYMM", 0.25, 128), 38 b1: ("TENSOR_INT32", 0.0625, 0), 39 o1: ("TENSOR_QUANT8_ASYMM", 0.05, 80) 40}) 41 42# Per-channel quantization 43channelQuant8 = DataTypeConverter().Identify({ 44 i1: ("TENSOR_QUANT8_ASYMM", 0.25, 100), 45 w1: ("TENSOR_QUANT8_SYMM_PER_CHANNEL", 0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.25, 0.5])), 46 b1: ("TENSOR_INT32", 0.0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.0625, 0.125], hide=True)), 47 o1: ("TENSOR_QUANT8_ASYMM", 0.5, 80) 48}) 49 50channelQuant8_mult_gt_1 = DataTypeConverter().Identify({ 51 i1: ("TENSOR_QUANT8_ASYMM", 0.25, 100), 52 w1: ("TENSOR_QUANT8_SYMM_PER_CHANNEL", 0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.25, 0.5])), 53 b1: ("TENSOR_INT32", 0.0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.0625, 0.125], hide=True)), 54 o1: ("TENSOR_QUANT8_ASYMM", 0.1, 80) 55}) 56 57example = Example({ 58 i1: [1, 2, 3, 4, 5, 6, 59 6, 5, 4, 3, 2, 1, 60 2, 3, 3, 3, 3, 3], 61 o1: [33, -0.5, 62 33, 7.5, 63 31, 4.5, 64 27, -9.5] 65}).AddNchw(i1, o1, layout).AddAllActivations(o1, act).AddVariations("relaxed", quant8, quant8_mult_gt_1, channelQuant8, channelQuant8_mult_gt_1, "float16").AddInput(w1, b1) 66 67 68# TEST 2: GROUPED_CONV2D_LARGE, pad = same, stride = 1, numGroups = 2, act = none 69i2 = Input("op1", "TENSOR_FLOAT32", "{1, 3, 2, 2}") # input 0 70w2 = Parameter("op2", "TENSOR_FLOAT32", "{2, 2, 3, 1}", [100, 20, 1, 200, 10, 2, 200, 30, 1, 100, 20, 3]) # weight 71b2 = Parameter("op3", "TENSOR_FLOAT32", "{2}", [500, -1000]) # bias 72o2 = Output("op4", "TENSOR_FLOAT32", "{1, 3, 2, 2}") # output 0 73Model("large").Operation("GROUPED_CONV_2D", i2, w2, b2, 1, 1, 1, 2, 0, layout).To(o2) 74 75# Additional data type 76quant8 = DataTypeConverter().Identify({ 77 i2: ("TENSOR_QUANT8_ASYMM", 0.25, 128), 78 w2: ("TENSOR_QUANT8_ASYMM", 1.0, 0), 79 b2: ("TENSOR_INT32", 0.25, 0), 80 o2: ("TENSOR_QUANT8_ASYMM", 10.0, 100) 81}) 82 83# Per-channel quantization 84channelQuant8 = DataTypeConverter().Identify({ 85 i2: ("TENSOR_QUANT8_ASYMM", 0.25, 128), 86 w2: ("TENSOR_QUANT8_SYMM_PER_CHANNEL", 0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[2.0, 2.5])), 87 b2: ("TENSOR_INT32", 0.0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.5, 0.625], hide=True)), 88 o2: ("TENSOR_QUANT8_ASYMM", 10.0, 100) 89}) 90 91example = Example({ 92 i2: [1, 2, 3, 4, 93 4, 3, 2, 1, 94 2, 3, 3, 3], 95 o2: [567, -873, 96 1480, -160, 97 608, -840, 98 1370, -10, 99 543, -907, 100 760, -310] 101}).AddNchw(i2, o2, layout).AddVariations("relaxed", quant8, channelQuant8, "float16").AddInput(w2, b2) 102 103 104# TEST 3: GROUPED_CONV2D_CHANNEL, pad = same, stride = 1, numGroups = 3, act = none 105i3 = Input("op1", "TENSOR_FLOAT32", "{1, 2, 2, 9}") # input 0 106w3 = Parameter("op2", "TENSOR_FLOAT32", "{6, 1, 1, 3}", [1, 2, 3, 2, 1, 0, 2, 3, 3, 6, 6, 6, 9, 8, 5, 2, 1, 1]) # weight 107b3 = Parameter("op3", "TENSOR_FLOAT32", "{6}", [10, -20, 30, -40, 50, -60]) # bias 108o3 = Output("op4", "TENSOR_FLOAT32", "{1, 2, 2, 6}") # output 0 109Model("channel").Operation("GROUPED_CONV_2D", i3, w3, b3, 1, 1, 1, 3, 0, layout).To(o3) 110 111# Additional data type 112quant8 = DataTypeConverter().Identify({ 113 i3: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 114 w3: ("TENSOR_QUANT8_ASYMM", 0.25, 0), 115 b3: ("TENSOR_INT32", 0.125, 0), 116 o3: ("TENSOR_QUANT8_ASYMM", 2.0, 60) 117}) 118 119channelQuant8 = DataTypeConverter().Identify({ 120 i3: ("TENSOR_QUANT8_ASYMM", 0.5, 0), 121 w3: ("TENSOR_QUANT8_SYMM_PER_CHANNEL", 0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.25, 0.3] * 3)), 122 b3: ("TENSOR_INT32", 0.0, 0, SymmPerChannelQuantParams(channelDim=0, scales=[0.125, 0.15] * 3, hide=True)), 123 o3: ("TENSOR_QUANT8_ASYMM", 2.0, 60) 124}) 125 126example = Example({ 127 i3: [1, 2, 3, 4, 55, 4, 3, 2, 1, 128 5, 4, 3, 2, 11, 2, 3, 4, 5, 129 2, 3, 2, 3, 22, 3, 2, 3, 2, 130 1, 0, 2, 1, 33, 1, 2, 0, 1], 131 o3: [24, -16, 215, 338, 98, -51, 132 32, -6, 73, 50, 134, -45, 133 24, -13, 111, 128, 102, -51, 134 17, -18, 134, 170, 73, -55] 135}).AddNchw(i3, o3, layout).AddVariations("relaxed", quant8, channelQuant8, "float16").AddInput(w3, b3) 136