forked from openvinotoolkit/model_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_choose_maximum.cpp
197 lines (188 loc) · 7.45 KB
/
node_choose_maximum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//*****************************************************************************
// Copyright 2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
#include "../../custom_node_interface.h"
extern "C" {
enum Method {
MAXIMUM_MINIMUM,
MAXIMUM_AVERAGE,
MAXIMUM_MAXIMUM,
METHOD_COUNT
};
static const std::string INPUT_TENSOR_NAME = "input_tensors";
int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount) {
std::stringstream ss;
// choose selection criteria
Method selectionMethod = Method::METHOD_COUNT;
if (paramsCount != 1) {
std::cout << "Wrong number of parameters - expected 1" << std::endl;
return 1;
}
if (strcmp(params[0].key, "selection_criteria") == 0) {
if (strcmp(params[0].value, "MAXIMUM_MINIMUM") == 0) {
selectionMethod = Method::MAXIMUM_MINIMUM;
} else if (strcmp(params[0].value, "MAXIMUM_MAXIMUM") == 0) {
selectionMethod = Method::MAXIMUM_MAXIMUM;
} else if (strcmp(params[0].value, "MAXIMUM_AVERAGE") == 0) {
selectionMethod = Method::MAXIMUM_AVERAGE;
} else {
ss << "Not allowed selection criteria chosen:" << params[0].value << std::endl;
std::cout << ss.str() << std::endl;
return 1;
}
} else {
std::cout << "Non recognized param string" << std::endl;
return 1;
}
// get input tensor
if (inputsCount != 1) {
std::cout << "Wrong number of inputs - expected 1" << std::endl;
return 1;
}
float* inputTensor;
size_t valuesPerTensor = 0;
size_t numberOfOps = 0;
if (INPUT_TENSOR_NAME == inputs[0].name) {
if (inputs[0].dimsCount != 3 ||
inputs[0].dims[1] != 1 ||
inputs[0].dims[0] == 0 ||
inputs[0].dims[2] == 0) {
ss << "improper " << INPUT_TENSOR_NAME
<< " dimensions: [" << inputs[0].dims[0]
<< ", " << inputs[0].dims[1]
<< ", " << inputs[0].dims[2] << "]" << std::endl;
std::cout << ss.str() << std::endl;
return 1;
}
ss << "Input valuesPerTensor: " << inputs[0].dims[1] << std::endl;
numberOfOps = inputs[0].dims[0];
valuesPerTensor = inputs[0].dims[2];
inputTensor = reinterpret_cast<float*>(inputs[0].data);
} else {
ss << "Lacking input: " << INPUT_TENSOR_NAME << std::endl;
std::cout << ss.str() << std::endl;
return 1;
}
// prepare output
*outputsCount = 1;
*outputs = (struct CustomNodeTensor*)malloc(*outputsCount * sizeof(CustomNodeTensor));
float* result = (float*)malloc(valuesPerTensor * sizeof(float));
CustomNodeTensor& resultTensor = **outputs;
resultTensor.name = "maximum_tensor";
resultTensor.data = reinterpret_cast<uint8_t*>(result);
resultTensor.dimsCount = 2;
resultTensor.dims = (uint64_t*)malloc(resultTensor.dimsCount * sizeof(uint64_t));
resultTensor.dims[0] = 1;
resultTensor.dims[1] = valuesPerTensor;
resultTensor.dataBytes = resultTensor.dims[0] * resultTensor.dims[1] * sizeof(float);
resultTensor.precision = FP32;
// perform operations
std::vector<float> minimums(numberOfOps, std::numeric_limits<int>::max());
std::vector<float> maximums(numberOfOps, std::numeric_limits<int>::lowest());
std::vector<float> averages(numberOfOps, 0);
for (size_t opId = 0; opId < numberOfOps; ++opId) {
for (size_t dummyPos = 0; dummyPos < valuesPerTensor; ++dummyPos) {
auto index = opId * valuesPerTensor + dummyPos;
switch (selectionMethod) {
case Method::MAXIMUM_MAXIMUM:
maximums[opId] = std::max(maximums[opId], inputTensor[index]);
break;
case Method::MAXIMUM_MINIMUM:
minimums[opId] = std::min(minimums[opId], inputTensor[index]);
break;
case Method::MAXIMUM_AVERAGE:
averages[opId] += inputTensor[index];
break;
default:
return 1;
}
ss << "opId:" << opId
<< " dummyPos:" << dummyPos
<< " input:" << inputTensor[index]
<< " minimums:" << minimums[opId]
<< " averages:" << averages[opId]
<< " maximums:" << maximums[opId]
<< " selected method:" << selectionMethod
<< std::endl;
}
averages[opId] /= valuesPerTensor;
}
// find which tensor to choose
const std::vector<float>* fromWhichContainerToChoose = &maximums;
switch (selectionMethod) {
case Method::MAXIMUM_MAXIMUM:
fromWhichContainerToChoose = &maximums;
break;
case Method::MAXIMUM_MINIMUM:
fromWhichContainerToChoose = &minimums;
break;
case Method::MAXIMUM_AVERAGE:
fromWhichContainerToChoose = &averages;
break;
default:
return 1;
}
size_t whichTensor = std::distance(fromWhichContainerToChoose->begin(),
std::max_element(fromWhichContainerToChoose->begin(),
fromWhichContainerToChoose->end()));
ss << "Selected tensor pos: " << whichTensor << std::endl;
// copy appropiate tensor
for (size_t i = 0; i < valuesPerTensor; ++i) {
size_t index = whichTensor * valuesPerTensor + i;
ss << "Putting tensor:" << whichTensor
<< " index:" << index
<< " with value:" << inputTensor[index] << std::endl;
result[i] = inputTensor[index];
}
std::cout << ss.str() << std::endl;
return 0;
}
int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount) {
*infoCount = 1;
*info = (struct CustomNodeTensorInfo*)malloc(*infoCount * sizeof(struct CustomNodeTensorInfo));
(*info)->name = "input_tensors";
(*info)->dimsCount = 3;
(*info)->dims = (uint64_t*)malloc((*info)->dimsCount * sizeof(uint64_t));
(*info)->dims[0] = 4;
(*info)->dims[1] = 1;
(*info)->dims[2] = 10;
(*info)->precision = FP32;
return 0;
}
int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount) {
*infoCount = 1;
*info = (struct CustomNodeTensorInfo*)malloc(*infoCount * sizeof(struct CustomNodeTensorInfo));
(*info)->name = "maximum_tensor";
(*info)->dimsCount = 2;
(*info)->dims = (uint64_t*)malloc((*info)->dimsCount * sizeof(uint64_t));
(*info)->dims[0] = 1;
(*info)->dims[1] = 10;
(*info)->precision = FP32;
return 0;
}
int release(void* ptr) {
std::cout << "ChooseMaximumCustomLibrary release" << std::endl;
free(ptr);
return 0;
}
}