-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathcapi_validation.cpp
331 lines (304 loc) · 16.8 KB
/
capi_validation.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//*****************************************************************************
// Copyright 2024 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 <limits>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include "buffer.hpp"
#include "../modelversion.hpp"
#include "../shape.hpp"
#include "../tensorinfo.hpp"
#include "capi_utils.hpp"
#include "inferencerequest.hpp"
#include "inferencetensor.hpp"
#include "capi_request_utils.hpp"
#include "../logging.hpp"
#include "../precision.hpp"
#include "../profiler.hpp"
#include "../status.hpp"
#include "../predict_request_validation_utils.hpp"
namespace ovms {
namespace request_validation_utils {
Status validateCapiTensorPrecision(const ovms::TensorInfo& info, const InferenceTensor& tensor, const std::string& tensorName, const std::string& servableName, const model_version_t servableVersion, ValidationChoice choice) {
if (tensor.getDataType() != getPrecisionAsOVMSDataType(info.getPrecision())) {
std::stringstream ss;
ss << "Expected: " << info.getPrecisionAsString()
<< "; Actual: " << toString(getOVMSDataTypeAsPrecision(tensor.getDataType())) << ";";
if (choice == ValidationChoice::INPUT) {
ss << " input name: ";
}
if (choice == ValidationChoice::OUTPUT) {
ss << " output name: ";
}
ss << tensorName;
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid precision - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_PRECISION, details);
}
return StatusCode::OK;
}
Status validateCapiTensorContent(const InferenceTensor& tensor, ovms::Precision expectedPrecision, size_t bufferId, const std::string& tensorName, const std::string& servableName, const model_version_t servableVersion, ValidationChoice choice) {
const Buffer* buffer = tensor.getBuffer();
if (nullptr == buffer) {
std::stringstream ss;
ss << "Servable: " << servableName
<< "; version: " << servableVersion
<< "; is missing buffer for tensor: " << tensorName;
const std::string details = ss.str();
SPDLOG_DEBUG(details);
return Status(StatusCode::NONEXISTENT_BUFFER, details);
}
size_t elementSize = (expectedPrecision == Precision::STRING) ? sizeof(std::string) : ov::element::Type(ovmsPrecisionToIE2Precision(expectedPrecision)).size();
size_t expectedContentSize;
if (computeExpectedBufferSizeReturnFalseIfOverflow<ovms::dimension_value_t>(tensor.getShape(), elementSize, expectedContentSize) == false) {
SPDLOG_DEBUG("[servable name: {} version: {}] Expected content size overflow for tensor - {}", servableName, servableVersion, tensorName);
return StatusCode::INVALID_SHAPE;
}
if (expectedContentSize != buffer->getByteSize()) {
std::stringstream ss;
ss << "Expected: " << expectedContentSize << " bytes; Actual: " << buffer->getByteSize() << " bytes;";
if (choice == ValidationChoice::INPUT) {
ss << " input name: ";
}
if (choice == ValidationChoice::OUTPUT) {
ss << " output name: ";
}
ss << tensorName;
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid content size of tensor - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_CONTENT_SIZE, details);
}
return StatusCode::OK;
}
Status validateCapiNumberOfShapeDimensions(const InferenceTensor& tensor, const ovms::TensorInfo& tensorInfo, const std::string& tensorName, const std::string& servableName, const model_version_t servableVersion, ValidationChoice choice) {
// Network and request must have the same number of shape dimensions
const auto& shape = tensorInfo.getShape();
if (shape.size() != static_cast<size_t>(tensor.getShape().size())) {
std::stringstream ss;
ss << "Expected: " << shape.toString()
<< "; Actual: " << tensorShapeToString(tensor.getShape()) << ";";
if (choice == ValidationChoice::INPUT) {
ss << " input name: ";
}
if (choice == ValidationChoice::OUTPUT) {
ss << " output name: ";
}
ss << tensorName;
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid number of shape dimensions - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_NO_OF_SHAPE_DIMENSIONS, details);
}
return StatusCode::OK;
}
template <>
dimension_value_t RequestShapeInfo<InferenceTensor, signed_shape_t>::getDim(size_t i) {
return tensor.getShape()[i];
}
template <>
size_t RequestShapeInfo<InferenceTensor, signed_shape_t>::getShapeSize() {
return tensor.getShape().size();
}
template <>
const signed_shape_t& RequestShapeInfo<InferenceTensor, signed_shape_t>::getShape() {
return tensor.getShape();
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validateRequestCoherency() const {
return StatusCode::OK;
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::validateRequestCoherency() const {
return StatusCode::OK;
}
template <>
const std::string RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::getCurrentlyValidatedTensorName() const {
return *currentlyValidatedName;
}
template <>
const std::string RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::getCurrentlyValidatedTensorName() const {
return *currentlyValidatedName;
}
template <>
const InferenceTensor& RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::getTensorFromIt(const InferenceTensor* const& it) const {
return *it;
}
template <>
const InferenceTensor& RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::getTensorFromIt(const InferenceTensor* const& it) const {
return *it;
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validateNumberOfTensors() const {
size_t expectedNumberOfInputs = inputsInfo.size();
if (request.getInputsSize() > 0 && expectedNumberOfInputs == static_cast<size_t>(request.getInputsSize())) {
return StatusCode::OK;
}
std::stringstream ss;
ss << "Expected: " << expectedNumberOfInputs << "; Actual: " << request.getInputsSize();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid number of inputs - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_NO_OF_INPUTS, details);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::validateNumberOfTensors() const {
return Status(StatusCode::OK);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validateNumberOfBinaryInputShapeDimensions(const InferenceTensor& tensor) const {
RequestShapeInfo<InferenceTensor, signed_shape_t> rsi(tensor);
if (rsi.getShapeSize() != 1) {
std::stringstream ss;
ss << "Expected number of input shape dimensions: 1; Actual: " << rsi.getShapeSize() << "; input name: " << getCurrentlyValidatedTensorName();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid number of shape dimensions - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_NO_OF_SHAPE_DIMENSIONS, details);
}
return StatusCode::OK;
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::checkBinaryBatchSizeMismatch(const InferenceTensor& tensor, const std::optional<Dimension>& servableBatchSize, Status& finalStatus, Mode batchingMode, Mode shapeMode, int32_t inputBatchSize) const {
if (!servableBatchSize.has_value()) {
std::stringstream ss;
ss << "Batch not present in input name: " << getCurrentlyValidatedTensorName();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid batch size - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_BATCH_SIZE, details);
}
RequestShapeInfo<InferenceTensor, signed_shape_t> rsi(tensor);
if (rsi.getDim(0) < 0) {
std::stringstream ss;
ss << "Batch size must be positive; input name: " << getCurrentlyValidatedTensorName();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid batch size - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_BATCH_SIZE, details);
}
if (servableBatchSize.value().match(rsi.getDim(0))) {
return StatusCode::OK;
}
if (batchingMode == AUTO) {
finalStatus = StatusCode::BATCHSIZE_CHANGE_REQUIRED;
return StatusCode::OK;
} else if (shapeMode != AUTO) {
std::stringstream ss;
ss << "Expected: " << servableBatchSize.value().toString() << "; Actual: " << tensor.getBuffer()->getByteSize() << "; input name: " << getCurrentlyValidatedTensorName();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Invalid batch size - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_BATCH_SIZE, details);
}
return StatusCode::OK;
}
template <>
size_t getStringInputWidth(const InferenceTensor& src) {
return 0;
}
template <>
int64_t getStringBatchSize(const InferenceTensor& src) {
return 0;
}
template <typename RequestType, typename InputTensorType, ValidationChoice choice, typename IteratorType, typename ShapeType>
Status RequestValidator<RequestType, InputTensorType, choice, IteratorType, ShapeType>::validateInferenceTensorBufferType(const InputTensorType& it) const {
const Buffer* buffer = it.getBuffer();
const OVMS_BufferType bufType = buffer->getBufferType();
if (bufType < OVMS_BUFFERTYPE_CPU || bufType > OVMS_BUFFERTYPE_HDDL) {
std::stringstream ss;
if (choice == ValidationChoice::INPUT) {
ss << "Required input ";
}
if (choice == ValidationChoice::OUTPUT) {
ss << "Required output ";
}
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Has invalid buffer type for tensor with specific name - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_BUFFER_TYPE, details);
} else {
// Remove this when other buffer types are supported
if (bufType != OVMS_BUFFERTYPE_CPU &&
bufType != OVMS_BUFFERTYPE_OPENCL &&
bufType != OVMS_BUFFERTYPE_VASURFACE_Y &&
bufType != OVMS_BUFFERTYPE_VASURFACE_UV) {
std::stringstream ss;
ss << "Required input ";
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Has invalid buffer type for input with specific name - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_BUFFER_TYPE, details);
}
}
if (buffer->getBufferType() == OVMS_BUFFERTYPE_CPU && buffer->getDeviceId() != std::nullopt && buffer->getDeviceId() != 0) {
std::stringstream ss;
ss << "Required input " << getCurrentlyValidatedTensorName();
const std::string details = ss.str();
SPDLOG_DEBUG("[servable name: {} version: {}] Has invalid device id for buffer, input with specific name - {}", servableName, servableVersion, details);
return Status(StatusCode::INVALID_DEVICE_ID, details);
}
return StatusCode::OK;
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validateTensorContent(const InferenceTensor& tensor, ovms::Precision expectedPrecision, size_t bufferId) const {
auto status = validateCapiTensorContent(tensor, expectedPrecision, bufferId, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::INPUT);
if (!status.ok())
return status;
return validateInferenceTensorBufferType(tensor);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::validateTensorContent(const InferenceTensor& tensor, ovms::Precision expectedPrecision, size_t bufferId) const {
auto status = validateCapiTensorContent(tensor, expectedPrecision, bufferId, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::OUTPUT);
if (!status.ok())
return status;
return validateInferenceTensorBufferType(tensor);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validateNumberOfShapeDimensions(const ovms::TensorInfo& tensorInfo, const InferenceTensor& tensor) const {
return validateCapiNumberOfShapeDimensions(tensor, tensorInfo, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::INPUT);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::validateNumberOfShapeDimensions(const ovms::TensorInfo& tensorInfo, const InferenceTensor& tensor) const {
return validateCapiNumberOfShapeDimensions(tensor, tensorInfo, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::OUTPUT);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>::validatePrecision(const ovms::TensorInfo& tensorInfo, const InferenceTensor& tensor) const {
return validateCapiTensorPrecision(tensorInfo, tensor, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::INPUT);
}
template <>
Status RequestValidator<ovms::InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>::validatePrecision(const ovms::TensorInfo& tensorInfo, const InferenceTensor& tensor) const {
return validateCapiTensorPrecision(tensorInfo, tensor, getCurrentlyValidatedTensorName(), servableName, servableVersion, ValidationChoice::OUTPUT);
}
template <>
bool dataInRawInputContents(const ovms::InferenceRequest& request) {
return false;
}
template <>
const std::string* getRawInputContents(const ovms::InferenceRequest& request, size_t bufferId) {
SPDLOG_DEBUG("Raw input contents is not supported for C-API");
throw std::runtime_error("Raw input contents used in C-API flow.");
return nullptr;
}
#define RETURN_IF_ERR(X) \
{ \
auto status = (X); \
if (!status.ok()) \
return status; \
}
template <>
Status validate(const InferenceRequest& request, const tensor_map_t& inputsInfo, const tensor_map_t& outputsInfo, const std::string& servableName, const model_version_t servableVersion, const std::set<std::string>& optionalAllowedInputNames, const Mode batchingMode, const shapes_info_map_t& shapeInfo) {
OVMS_PROFILE_FUNCTION();
auto inputValidationStatus = RequestValidator<InferenceRequest, InferenceTensor, ValidationChoice::INPUT, const InferenceTensor*, signed_shape_t>(request, inputsInfo, outputsInfo, servableName, servableVersion, optionalAllowedInputNames, batchingMode, shapeInfo).validate();
if (!inputValidationStatus.ok())
return inputValidationStatus;
return RequestValidator<InferenceRequest, InferenceTensor, ValidationChoice::OUTPUT, const InferenceTensor*, signed_shape_t>(request, inputsInfo, outputsInfo, servableName, servableVersion, optionalAllowedInputNames, batchingMode, shapeInfo).validate();
}
} // namespace request_validation_utils
} // namespace ovms