Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mkulakow/mediapipe kfs rest test #2377

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/http_rest_api_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ static bool isInputEmpty(const ::KFSRequest::InferInputTensor& input) {
return input.contents().fp64_contents_size() == 0;
if (input.datatype() == "BYTES")
return input.contents().bytes_contents_size() == 0;
if (input.datatype() == "BOOL")
return input.contents().bool_contents_size() == 0;
return true;
}

Expand Down Expand Up @@ -317,7 +319,6 @@ static Status handleBinaryInputs(::KFSRequest& grpc_request, const std::string&
binary_input_size = calculateBinaryDataSize(*input);
}
}

auto status = handleBinaryInput(binary_input_size, binary_input_offset, binary_buffer_size, binary_inputs_buffer, *input, grpc_request.add_raw_input_contents());
if (!status.ok())
return status;
Expand Down
51 changes: 40 additions & 11 deletions src/mediapipe_internal/mediapipegraphexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
case TFSDataType::DT_FLOAT: {
COPY_INPUT_VALUE_BY_VALUE(float, fp32);
}
case TFSDataType::DT_DOUBLE: {
COPY_INPUT_VALUE_BY_VALUE(double, fp64);
}
case TFSDataType::DT_INT64: {
COPY_INPUT_VALUE_BY_VALUE(int64_t, int64);
}
Expand Down Expand Up @@ -410,7 +413,6 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
case TFSDataType::DT_BOOL: {
COPY_INPUT_VALUE_BY_VALUE(bool, bool);
}
case TFSDataType::DT_DOUBLE:
case TFSDataType::DT_HALF:
default:
return ovms::Status(ovms::StatusCode::NOT_IMPLEMENTED, "There is no support for types different than fp32, int64, int32, uint32, uint64, int8, uint8, bool");
Expand Down Expand Up @@ -455,10 +457,10 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
}
} else {
OVMS_RETURN_ON_FAIL(validateInputContent(*requestInputItr, expectedBytes, requestedName, request));
outTensor = std::make_unique<ov::Tensor>(precision, shape);
if (expectedBytes == 0) {
return StatusCode::OK;
}
outTensor = std::make_unique<ov::Tensor>(precision, shape);
void* data = outTensor->data();
switch (precision) {
case ov::element::Type_t::f32: {
Expand Down Expand Up @@ -491,12 +493,14 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
case ov::element::Type_t::boolean: {
COPY_INPUT_VALUE_BY_VALUE(bool, bool);
}
case ov::element::Type_t::f64: {
COPY_INPUT_VALUE_BY_VALUE(double, fp64);
}
// the rest not supported by KFS
case ov::element::Type_t::u1:
case ov::element::Type_t::u4:
case ov::element::Type_t::i4:
case ov::element::Type_t::f16:
case ov::element::Type_t::f64:
case ov::element::Type_t::bf16:
case ov::element::Type_t::undefined:
case ov::element::Type_t::dynamic:
Expand Down Expand Up @@ -689,20 +693,32 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
SPDLOG_DEBUG("[servable name: {} version: {}] {}", request.model_name(), request.model_version(), details);
return Status(StatusCode::INVALID_PRECISION, details);
}
size_t expectedBytes = 1;
bool expectedBufferSizeValid = computeExpectedBufferSizeReturnFalseIfOverflow<py::ssize_t>(shape, precision.size(), expectedBytes);
if (!expectedBufferSizeValid) {
const std::string details = "Provided shape and datatype declare too large buffer.";
SPDLOG_DEBUG("[servable name: {} version: {}] {}", request.model_name(), request.model_version(), details);
return Status(StatusCode::INVALID_CONTENT_SIZE, details);
size_t expectedBytes;
if (precision == ov::element::Type_t::string) {
expectedBytes = 0;
for (auto contents : request.inputs(inputIndex).contents().bytes_contents()) {
expectedBytes = expectedBytes + contents.size() + sizeof(uint32_t);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't size written in first bytes of the input? If so I suggest keeping the order also here.

Suggested change
expectedBytes = expectedBytes + contents.size() + sizeof(uint32_t);
expectedBytes += sizeof(uint32_t) + contents.size();

}
} else {
expectedBytes = 1;
bool expectedBufferSizeValid = computeExpectedBufferSizeReturnFalseIfOverflow<py::ssize_t>(shape, precision.size(), expectedBytes);
if (!expectedBufferSizeValid) {
const std::string details = "Provided shape and datatype declare too large buffer.";
SPDLOG_DEBUG("[servable name: {} version: {}] {}", request.model_name(), request.model_version(), details);
return Status(StatusCode::INVALID_CONTENT_SIZE, details);
}
OVMS_RETURN_ON_FAIL(validateInputContent(*requestInputItr, expectedBytes, requestedName, request));
}
OVMS_RETURN_ON_FAIL(validateInputContent(*requestInputItr, expectedBytes, requestedName, request));
auto ok = pythonBackend->createEmptyOvmsPyTensor(
requestedName,
shape,
requestInputItr->datatype(),
expectedBytes,
outTensor);
if (!ok) {
SPDLOG_DEBUG("Error creating empty Python tensor");
return StatusCode::UNKNOWN_ERROR;
}
void* data;
if (!pythonBackend->getOvmsPyTensorData(outTensor, &data)) {
return Status(StatusCode::INTERNAL_ERROR);
Expand All @@ -711,6 +727,9 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
case ov::element::Type_t::f32: {
COPY_INPUT_VALUE_BY_VALUE(float, fp32);
}
case ov::element::Type_t::f64: {
COPY_INPUT_VALUE_BY_VALUE(double, fp64);
}
case ov::element::Type_t::i64: {
COPY_INPUT_VALUE_BY_VALUE(int64_t, int64);
}
Expand Down Expand Up @@ -738,13 +757,23 @@ static Status deserializeTensor(const std::string& requestedName, const KFSReque
case ov::element::Type_t::boolean: {
COPY_INPUT_VALUE_BY_VALUE(bool, bool);
}
case ov::element::Type_t::string: {
uint32_t offset = 0;
for (auto contents : request.inputs(inputIndex).contents().bytes_contents()) {
uint32_t size = contents.size();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used only below as a buffer to copy from. Do we need that? If so, can we make it const and replace following contents.size() calls?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be const?

std::memcpy(reinterpret_cast<char*>(data) + offset, &size, sizeof(uint32_t));
offset += sizeof(uint32_t);
std::memcpy(reinterpret_cast<char*>(data) + offset, contents.data(), contents.size());
offset += contents.size();
}
return StatusCode::OK;
}

// the rest not supported by KFS
case ov::element::Type_t::u1:
case ov::element::Type_t::u4:
case ov::element::Type_t::i4:
case ov::element::Type_t::f16:
case ov::element::Type_t::f64:
case ov::element::Type_t::bf16:
case ov::element::Type_t::undefined:
case ov::element::Type_t::dynamic:
Expand Down
3 changes: 3 additions & 0 deletions src/python/ovms_py_tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const std::unordered_map<std::string, std::string> datatypeToBufferFormat{
{"FP16", "e"},
{"FP32", "f"},
{"FP64", "d"},
{"BYTES", "s"},
// {"BF16", X} to be considered, for now it shall be treated as a custom datatype
};

Expand All @@ -64,6 +65,7 @@ const std::unordered_map<std::string, std::string> bufferFormatToDatatype{
{"e", "FP16"},
{"f", "FP32"},
{"d", "FP64"},
{"s", "BYTES"},
// {X, "BF16"} to be considered, for now it shall be treated as a custom datatype
};

Expand All @@ -83,6 +85,7 @@ const std::unordered_map<std::string, py::ssize_t> bufferFormatToItemsize{
{"e", 2},
{"f", 4},
{"d", 8},
{"s", 1},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it 1? BYTES type was considered custom type since in a way it's defined in KServe, it's impossible to determine size of a single element.

// {"BF16", X} to be considered, for now it shall be treated as a custom datatype
};

Expand Down
1 change: 1 addition & 0 deletions src/python/python_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ bool PythonBackend::createEmptyOvmsPyTensor(const std::string& name, const std::
outTensor = std::make_unique<PyObjectWrapper<py::object>>(ovmsPyTensor);
return true;
} catch (const pybind11::error_already_set& e) {
SPDLOG_ERROR("TENSOR {}", size);
SPDLOG_DEBUG("PythonBackend::createEmptyOvmsPyTensor - Py Error: {}", e.what());
return false;
} catch (std::exception& e) {
Expand Down
4 changes: 4 additions & 0 deletions src/rest_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ Status KFSRestParser::parseInput(rapidjson::Value& node, bool onlyOneInput) {
if (!(dataItr->value.IsArray())) {
return StatusCode::REST_COULD_NOT_PARSE_INPUT;
}
if (std::strcmp(datatypeItr->value.GetString(), "FP16") == 0 || std::strcmp(datatypeItr->value.GetString(), "BF16") == 0) {
SPDLOG_DEBUG("{} datatype is supported only when data is located in raw_input_contents", datatypeItr->value.GetString());
return StatusCode::REST_COULD_NOT_PARSE_INPUT;
}
return parseData(dataItr->value, *input);
} else {
auto binary_data_size_parameter = input->parameters().find("binary_data_size");
Expand Down
2 changes: 2 additions & 0 deletions src/rest_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ static Status parseOutputs(const ::KFSResponse& response_proto, rapidjson::Prett
PARSE_OUTPUT_DATA(int64_contents, int64_t, Int64)
} else if (tensor.datatype() == "UINT64") {
PARSE_OUTPUT_DATA(uint64_contents, uint64_t, Uint64)
} else if (tensor.datatype() == "BOOL") {
PARSE_OUTPUT_DATA(bool_contents, bool, Bool)
} else if (tensor.datatype() == "BYTES") {
PARSE_OUTPUT_DATA_STRING(bytes_contents, String)
} else {
Expand Down
Loading