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# 16import collections 17 18TestCase = collections.namedtuple("TestCase", [ 19 "inp", "inp_data", "begin", "begin_data", "size", "size_data", "output", 20 "output_data" 21]) 22 23test_cases = [ 24 TestCase( 25 inp=Input("input", "TENSOR_FLOAT32", "{4}"), 26 inp_data=[1, 2, 3, 4], 27 begin=Input("begin", "TENSOR_INT32", "{1}"), 28 begin_data=[1], 29 size=Input("size", "TENSOR_INT32", "{1}"), 30 size_data=[2], 31 output=Output("output", "TENSOR_FLOAT32", "{2}"), 32 output_data=[2, 3]), 33 TestCase( 34 inp=Input("input", "TENSOR_FLOAT32", "{2,3}"), 35 inp_data=[1, 2, 3, 4, 5, 6], 36 begin=Input("begin", "TENSOR_INT32", "{2}"), 37 begin_data=[1, 0], 38 size=Input("size", "TENSOR_INT32", "{2}"), 39 size_data=[1, 2], 40 output=Output("output", "TENSOR_FLOAT32", "{1, 2}"), 41 output_data=[4, 5]), 42 TestCase( 43 inp=Input("input", "TENSOR_FLOAT32", "{2,3,2}"), 44 inp_data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 45 begin=Input("begin", "TENSOR_INT32", "{3}"), 46 begin_data=[0, 0, 0], 47 size=Input("size", "TENSOR_INT32", "{3}"), 48 size_data=[2, 3, 2], 49 output=Output("output", "TENSOR_FLOAT32", "{2, 3, 2}"), 50 output_data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), 51 TestCase( 52 inp=Input("input", "TENSOR_FLOAT32", "{4, 1, 1, 1}"), 53 inp_data=[1, 2, 3, 4], 54 begin=Input("begin", "TENSOR_INT32", "{4}"), 55 begin_data=[1, 0, 0, 0], 56 size=Input("size", "TENSOR_INT32", "{4}"), 57 size_data=[3, 1, 1, 1], 58 output=Output("output", "TENSOR_FLOAT32", "{3, 1, 1, 1}"), 59 output_data=[2, 3, 4]), 60 TestCase( 61 inp=Input("input", "TENSOR_INT32", "{3, 2, 3, 1}"), 62 inp_data=[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6], 63 begin=Input("begin", "TENSOR_INT32", "{4}"), 64 begin_data=[1, 0, 0, 0], 65 size=Input("size", "TENSOR_INT32", "{4}"), 66 size_data=[1, 1, 3, 1], 67 output=Output("output", "TENSOR_INT32", "{1, 1, 3, 1}"), 68 output_data=[3, 3, 3]), 69 TestCase( 70 inp=Input("input", "TENSOR_INT32", "{3, 2, 3, 1}"), 71 inp_data=[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6], 72 begin=Input("begin", "TENSOR_INT32", "{4}"), 73 begin_data=[1, 0, 0, 0], 74 size=Input("size", "TENSOR_INT32", "{4}"), 75 size_data=[2, 1, 3, 1], 76 output=Output("output", "TENSOR_INT32", "{2, 1, 3, 1}"), 77 output_data=[3, 3, 3, 5, 5, 5]), 78 TestCase( 79 inp=Input("input", "TENSOR_QUANT8_ASYMM", "{3, 2, 3, 1}, 2.0, 128"), 80 inp_data=[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6], 81 begin=Input("begin", "TENSOR_INT32", "{4}"), 82 begin_data=[1, 0, 0, 0], 83 size=Input("size", "TENSOR_INT32", "{4}"), 84 size_data=[2, 1, 3, 1], 85 output=Output("output", "TENSOR_QUANT8_ASYMM", "{2, 1, 3, 1}, 2.0, 128"), 86 output_data=[3, 3, 3, 5, 5, 5]), 87 TestCase( 88 inp=Input("input", "TENSOR_INT32", "{3, 2, 3, 1}"), 89 inp_data=[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6], 90 begin=Input("begin", "TENSOR_INT32", "{4}"), 91 begin_data=[1, 0, 0, 0], 92 size=Input("size", "TENSOR_INT32", "{4}"), 93 size_data=[2, 1, -1, 1], 94 output=Output("output", "TENSOR_INT32", "{2, 1, 3, 1}"), 95 output_data=[3, 3, 3, 5, 5, 5]), 96] 97 98for test_case in test_cases: 99 model = Model().Operation("SLICE", test_case.inp, test_case.begin, 100 test_case.size).To(test_case.output) 101 Example({ 102 test_case.inp: test_case.inp_data, 103 test_case.begin: test_case.begin_data, 104 test_case.size: test_case.size_data, 105 test_case.output: test_case.output_data, 106 }, 107 model=model).AddVariations("relaxed", "float16") 108 109 110# zero-sized input 111 112# Use BOX_WITH_NMS_LIMIT op to generate a zero-sized internal tensor for box cooridnates. 113p1 = Parameter("scores", "TENSOR_FLOAT32", "{1, 2}", [0.90, 0.10]) # scores 114p2 = Parameter("roi", "TENSOR_FLOAT32", "{1, 8}", [1, 1, 10, 10, 0, 0, 10, 10]) # roi 115o1 = Output("scoresOut", "TENSOR_FLOAT32", "{0}") # scores out 116o2 = Output("classesOut", "TENSOR_INT32", "{0}") # classes out 117tmp1 = Internal("roiOut", "TENSOR_FLOAT32", "{0, 4}") # roi out 118tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}") # batch split out 119model = Model("zero_sized").Operation("BOX_WITH_NMS_LIMIT", p1, p2, [0], 0.3, -1, 0, 0.4, 1.0, 0.3).To(o1, tmp1, o2, tmp2) 120 121# Use ROI_ALIGN op to convert into zero-sized feature map. 122layout = BoolScalar("layout", False) # NHWC 123i1 = Input("in", "TENSOR_FLOAT32", "{1, 1, 1, 1}") 124zero_sized = Internal("featureMap", "TENSOR_FLOAT32", "{0, 2, 2, 1}") 125model = model.Operation("ROI_ALIGN", i1, tmp1, tmp2, 2, 2, 2.0, 2.0, 4, 4, layout).To(zero_sized) 126 127# SLICE op with numBatches = 0. 128o3 = Output("out", "TENSOR_FLOAT32", "{0, 1, 1, 1}") # out 129model = model.Operation("SLICE", zero_sized, [0, 1, 1, 0], [-1, 1, -1, 1]).To(o3) 130 131quant8 = DataTypeConverter().Identify({ 132 p1: ("TENSOR_QUANT8_ASYMM", 0.1, 128), 133 p2: ("TENSOR_QUANT16_ASYMM", 0.125, 0), 134 o1: ("TENSOR_QUANT8_ASYMM", 0.1, 128), 135 tmp1: ("TENSOR_QUANT16_ASYMM", 0.125, 0), 136 i1: ("TENSOR_QUANT8_ASYMM", 0.1, 128), 137 zero_sized: ("TENSOR_QUANT8_ASYMM", 0.1, 128), 138 o3: ("TENSOR_QUANT8_ASYMM", 0.1, 128) 139}) 140 141Example({ 142 i1: [1], 143 o1: [], 144 o2: [], 145 o3: [], 146}).AddVariations("relaxed", quant8, "float16") 147