• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 #ifndef MODEL_H
16 #define MODEL_H
17 
18 #include "const.h"
19 #include "nnrt_utils.h"
20 
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Test {
24 
25 struct AddModel {
26     // ADD MODEL
27     float inputValue0[4] = {0, 1, 2, 3};
28     float inputValue1[4] = {0, 1, 2, 3};
29     int8_t activationValue = OH_NN_FUSED_NONE;
30     float outputValue[4] = {0};
31     float expectValue[4] = {0, 2, 4, 6};
32 
33     OHNNOperandTest input0 = {OH_NN_FLOAT32, OH_NN_TENSOR, TENSOR_SHAPE, inputValue0, ADD_DATA_LENGTH};
34     OHNNOperandTest input1 = {OH_NN_FLOAT32, OH_NN_TENSOR, TENSOR_SHAPE, inputValue1, ADD_DATA_LENGTH};
35     OHNNOperandTest activation = {OH_NN_INT8, OH_NN_ADD_ACTIVATIONTYPE, {}, &activationValue, sizeof(int8_t)};
36     OHNNOperandTest output = {OH_NN_FLOAT32, OH_NN_TENSOR, TENSOR_SHAPE, outputValue, ADD_DATA_LENGTH};
37     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_ADD,
38                                .operands = {input0, input1, activation, output},
39                                .paramIndices = {2},
40                                .inputIndices = {0, 1},
41                                .outputIndices = {3}};
42 };
43 
44 struct AvgPoolDynamicModel {
45     // AVG POOL MODEL
46     float inputValue[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
47     uint64_t kernelValue[2] = {2, 2};
48     uint64_t strideValue[2] = {1, 1};
49     int8_t padValue = 1;
50     int8_t activationValue = OH_NN_FUSED_NONE;
51     float outputValue[4] = {0};
52     float expectValue[4] = {2, 3, 5, 6};
53 
54     OHNNOperandTest dynamicInput = {OH_NN_FLOAT32, OH_NN_TENSOR, {-1, -1, -1, -1}, inputValue, AVG_INPUT_LENGTH};
55     OHNNOperandTest kernel = {OH_NN_INT64, OH_NN_AVG_POOL_KERNEL_SIZE, {2}, kernelValue, sizeof(kernelValue)};
56     OHNNOperandTest strides = {OH_NN_INT64, OH_NN_AVG_POOL_STRIDE, {2}, strideValue, sizeof(strideValue)};
57     OHNNOperandTest padMode = {OH_NN_INT8, OH_NN_AVG_POOL_PAD_MODE, {}, &padValue, sizeof(padValue)};
58     OHNNOperandTest activation = {OH_NN_INT8, OH_NN_AVG_POOL_ACTIVATION_TYPE, {}, &activationValue, sizeof(int8_t)};
59     OHNNOperandTest output = {OH_NN_FLOAT32, OH_NN_TENSOR, {-1, -1, -1, -1}, outputValue, sizeof(outputValue)};
60 
61     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_AVG_POOL,
62                                .operands = {dynamicInput, kernel, strides, padMode, activation, output},
63                                .paramIndices = {1, 2, 3, 4},
64                                .inputIndices = {0},
65                                .outputIndices = {5}};
66 };
67 
68 struct TopKModel {
69     // TopK Model
70     float valueX[6] = {0, 1, 2, 3, 4, 5};
71     int8_t valueK = 2;
72     bool valueSorted = true;
73     float valueOutput1[2];
74     int32_t valueOutput2[2];
75 
76     OHNNOperandTest x = {OH_NN_FLOAT32, OH_NN_TENSOR, {1, 6}, valueX, 6 * sizeof(float)};
77     OHNNOperandTest k = {OH_NN_INT8, OH_NN_TENSOR, {}, &valueK, sizeof(int8_t)};
78     OHNNOperandTest sorted = {OH_NN_BOOL, OH_NN_TOP_K_SORTED, {}, &valueSorted, sizeof(bool)};
79     OHNNOperandTest output1 = {OH_NN_FLOAT32, OH_NN_TENSOR, {1, 2}, valueOutput1, 2 * sizeof(float)};
80     OHNNOperandTest output2 = {OH_NN_INT32, OH_NN_TENSOR, {1, 2}, valueOutput2, 2 * sizeof(int32_t)};
81 
82     OHNNGraphArgs graphArgs = {.operationType = OH_NN_OPS_TOP_K,
83                                .operands = {x, k, sorted, output1, output2},
84                                .paramIndices = {2},
85                                .inputIndices = {0, 1},
86                                .outputIndices = {3, 4}};
87 };
88 
89 class AddTopKModel {
90     // Build two ops Model
91 private:
92     AddModel addModel;
93     TopKModel topKModel;
94 
95 public:
96     OHNNGraphArgsMulti graphArgs = {
97         .operationTypes = {OH_NN_OPS_ADD, OH_NN_OPS_TOP_K},
98         .operands = {{addModel.input0, addModel.input1, addModel.activation, addModel.output},
99             {topKModel.k, topKModel.sorted, topKModel.output1, topKModel.output2}},
100         .paramIndices = {{2}, {5}},
101         .inputIndices = {{0, 1}, {3, 4}},
102         .outputIndices = {{3}, {6, 7}},
103         .graphInput = {0, 1, 4},
104         .graphOutput = {6, 7}};
105 };
106 
107 } // namespace Test
108 } // namespace NeuralNetworkRuntime
109 } // namespace OHOS
110 
111 #endif // MODEL_H