Skip to content

Commit 2610874

Browse files
CarlosLeeGitpymumu
authored andcommitted
add dlengine inference
1 parent 4384504 commit 2610874

21 files changed

+1486
-0
lines changed

CMake/FindDLENGINE.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
set(DLENGINE_PATH $ENV{DLENGINE_PATH})
2+
3+
if(NOT DLENGINE_PATH)
4+
find_package(PythonInterp QUIET)
5+
execute_process(
6+
COMMAND ${PYTHON_EXECUTABLE} "-c" "import re, dlengine; print(re.compile('/__init__.py.*').sub('', dlengine.__file__))"
7+
RESULT_VARIABLE DLENGINE_STATUS
8+
OUTPUT_VARIABLE DLENGINE_PATH
9+
ERROR_QUIET
10+
OUTPUT_STRIP_TRAILING_WHITESPACE
11+
)
12+
endif()
13+
14+
find_path(DLENGINE_INCLUDE
15+
NAMES dlengine.h
16+
HINTS ${CMAKE_INSTALL_FULL_INCLUDEDIR} ${DLENGINE_PATH}/include
17+
)
18+
mark_as_advanced(DLENGINE_INCLUDE)
19+
20+
find_library(DLENGINE_LIBRARY
21+
NAMES dlengine
22+
HINTS ${DLENGINE_PATH}
23+
)
24+
mark_as_advanced(DLENGINE_LIBRARY)
25+
26+
include(FindPackageHandleStandardArgs)
27+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(DLENGINE
28+
REQUIRED_VARS DLENGINE_PATH DLENGINE_LIBRARY DLENGINE_INCLUDE
29+
VERSION_VAR DLENGINE_VERSION_STRING)
30+
31+
if(DLENGINE_FOUND)
32+
set(DLENGINE_LIBRARIES ${DLENGINE_LIBRARY})
33+
set(DLENGINE_INCLUDE_DIR ${DLENGINE_INCLUDE})
34+
set(DLENGINE_BACKEND_ZOO_DIR ${DLENGINE_PATH}/backend_zoo)
35+
endif()

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ find_package(FUSE)
8888
find_package(JNI)
8989
find_package(Java)
9090
find_package(ROCKCHIP)
91+
find_package(DLENGINE)
9192

9293
add_subdirectory(thirdparty)
9394
add_subdirectory(src)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#
2+
# Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
cmake_minimum_required(VERSION 3.10)
18+
19+
set(UNIT_DEVICE "cpu")
20+
set(UNIT_NAME "dlengine_inference")
21+
22+
project(modelbox-flowunit-${UNIT_DEVICE}-${UNIT_NAME})
23+
24+
if (NOT DLENGINE_FOUND)
25+
message(STATUS "Not found dlengine, disable ${UNIT_NAME} flowunit")
26+
return()
27+
endif()
28+
29+
30+
file(GLOB_RECURSE UNIT_SOURCE *.cpp *.cc *.c)
31+
32+
group_source_test_files(MODELBOX_UNIT_SOURCE MODELBOX_UNIT_TEST_SOURCE "_test.c*" ${UNIT_SOURCE})
33+
34+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test_toml/modelbox.test.dlengine.cpu.inference.onnx.in ${TEST_WORKING_DATA_DIR}/dlengine_cpu/modelbox.test.dlengine.cpu.inference.onnx.toml @ONLY)
35+
36+
include_directories(${CMAKE_CURRENT_LIST_DIR})
37+
include_directories(${LIBMODELBOX_INCLUDE})
38+
include_directories(${LIBMODELBOX_BASE_INCLUDE})
39+
include_directories(${LIBMODELBOX_DEVICE_CPU_INCLUDE})
40+
include_directories(${LIBMODELBOX_VIRTUALDRIVER_INFERENCE_INCLUDE})
41+
include_directories(${MODELBOX_COMMON_INFERENCE_INCLUDE})
42+
include_directories(${DLENGINE_INCLUDE_DIR})
43+
include_directories(${LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_INCLUDE})
44+
45+
set(MODELBOX_UNIT_SHARED libmodelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}-shared)
46+
set(MODELBOX_UNIT_SOURCE_INCLUDE ${CMAKE_CURRENT_LIST_DIR})
47+
48+
add_library(${MODELBOX_UNIT_SHARED} SHARED ${MODELBOX_UNIT_SOURCE})
49+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_CPU_SHARED ${MODELBOX_UNIT_SHARED})
50+
51+
set_target_properties(${MODELBOX_UNIT_SHARED} PROPERTIES
52+
SOVERSION ${MODELBOX_VERSION_MAJOR}
53+
VERSION ${MODELBOX_VERSION_MAJOR}.${MODELBOX_VERSION_MINOR}.${MODELBOX_VERSION_PATCH}
54+
)
55+
56+
target_link_libraries(${MODELBOX_UNIT_SHARED} pthread)
57+
target_link_libraries(${MODELBOX_UNIT_SHARED} rt)
58+
target_link_libraries(${MODELBOX_UNIT_SHARED} dl)
59+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${DLENGINE_LIBRARIES})
60+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_DEVICE_CPU_SHARED})
61+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_SHARED})
62+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_VIRTUALDRIVER_INFERENCE_SHARED})
63+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${MODELBOX_COMMON_INFERENCE_LIBRARY})
64+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_SHARED})
65+
66+
set_target_properties(${MODELBOX_UNIT_SHARED} PROPERTIES OUTPUT_NAME "modelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}")
67+
68+
install(TARGETS ${MODELBOX_UNIT_SHARED}
69+
COMPONENT cpu-device-flowunit
70+
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
71+
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
72+
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
73+
OPTIONAL)
74+
75+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_CPU_SHARED ${MODELBOX_UNIT_SHARED} CACHE INTERNAL "")
76+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_CPU_INCLUDE ${MODELBOX_UNIT_SOURCE_INCLUDE} CACHE INTERNAL "")
77+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_CPU_SOURCES ${MODELBOX_UNIT_SOURCE} CACHE INTERNAL "")
78+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_DLENGINE_CPU_SO_PATH ${CMAKE_CURRENT_BINARY_DIR}/libmodelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}.so CACHE INTERNAL "")
79+
80+
list(APPEND DRIVER_UNIT_TEST_SOURCE ${MODELBOX_UNIT_TEST_SOURCE})
81+
list(APPEND DRIVER_UNIT_TEST_TARGET ${MODELBOX_UNIT_SHARED})
82+
set(DRIVER_UNIT_TEST_SOURCE ${DRIVER_UNIT_TEST_SOURCE} CACHE INTERNAL "")
83+
set(DRIVER_UNIT_TEST_TARGET ${DRIVER_UNIT_TEST_TARGET} CACHE INTERNAL "")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "dlengine_cpu_inference_flowunit.h"
18+
19+
static constexpr std::string BACKEND_TYPE = "JwdLCz9nKiM=";
20+
21+
DLEngineCPUInferenceFlowUnit::DLEngineCPUInferenceFlowUnit()
22+
: inference_(std::make_shared<DLEngineInference>()) {}
23+
24+
DLEngineCPUInferenceFlowUnit::~DLEngineCPUInferenceFlowUnit() = default;
25+
26+
modelbox::Status DLEngineCPUInferenceFlowUnit::Open(
27+
const std::shared_ptr<modelbox::Configuration> &config) {
28+
if (!config->Contain("config.model_type")) {
29+
config->SetProperty("config.model_type", "onnx");
30+
}
31+
32+
// fix backend on cpu
33+
config->SetProperty("config.backend_type", BACKEND_TYPE);
34+
35+
return inference_->Init(config, GetFlowUnitDesc(), GetBindDevice()->GetType(),
36+
dev_id_);
37+
}
38+
39+
modelbox::Status DLEngineCPUInferenceFlowUnit::Close() {
40+
return modelbox::STATUS_OK;
41+
}
42+
43+
modelbox::Status DLEngineCPUInferenceFlowUnit::Process(
44+
std::shared_ptr<modelbox::DataContext> data_ctx) {
45+
return inference_->Infer(data_ctx);
46+
}
47+
48+
std::shared_ptr<modelbox::FlowUnit>
49+
DLEngineCPUInferenceFlowUnitFactory::VirtualCreateFlowUnit(
50+
const std::string &unit_name, const std::string &unit_type,
51+
const std::string &virtual_type) {
52+
return std::make_shared<DLEngineCPUInferenceFlowUnit>();
53+
}
54+
55+
std::string DLEngineCPUInferenceFlowUnitFactory::GetFlowUnitFactoryType() {
56+
return FLOWUNIT_TYPE;
57+
}
58+
59+
std::string DLEngineCPUInferenceFlowUnitFactory::GetVirtualType() {
60+
return INFERENCE_TYPE;
61+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MODELBOX_FLOWUNIT_DLENGINE_CPU_INFERENCE_H_
18+
#define MODELBOX_FLOWUNIT_DLENGINE_CPU_INFERENCE_H_
19+
20+
#include "dlengine_inference_flowunit.h"
21+
#include "modelbox/flowunit.h"
22+
23+
constexpr const char *FLOWUNIT_TYPE = "cpu";
24+
25+
class DLEngineCPUInferenceFlowUnit : public modelbox::FlowUnit {
26+
public:
27+
DLEngineCPUInferenceFlowUnit();
28+
29+
~DLEngineCPUInferenceFlowUnit() override;
30+
31+
modelbox::Status Open(
32+
const std::shared_ptr<modelbox::Configuration> &config) override;
33+
34+
modelbox::Status Close() override;
35+
36+
modelbox::Status Process(
37+
std::shared_ptr<modelbox::DataContext> data_ctx) override;
38+
39+
private:
40+
std::shared_ptr<DLEngineInference> inference_;
41+
};
42+
43+
class DLEngineCPUInferenceFlowUnitFactory : public modelbox::FlowUnitFactory {
44+
public:
45+
std::shared_ptr<modelbox::FlowUnit> VirtualCreateFlowUnit(
46+
const std::string &unit_name, const std::string &unit_type,
47+
const std::string &virtual_type) override;
48+
49+
std::string GetFlowUnitFactoryType() override;
50+
51+
std::string GetVirtualType() override;
52+
};
53+
54+
#endif // MODELBOX_FLOWUNIT_DLENGINE_CPU_INFERENCE_H_
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "dlengine_inference_flowunit_test.h"
18+
#include "gmock/gmock.h"
19+
#include "gtest/gtest.h"
20+
#include "test/mock/minimodelbox/mockflow.h"
21+
22+
class DLEngineCPUInferenceFlowUnitTest : public testing::Test {
23+
protected:
24+
void SetUp() override {
25+
test_impl_ = std::make_shared<DLEngineInferenceFlowUnitTest>("cpu");
26+
}
27+
28+
void TearDown() override { test_impl_ = nullptr; }
29+
30+
std::shared_ptr<DLEngineInferenceFlowUnitTest> test_impl_;
31+
};
32+
33+
TEST_F(DLEngineCPUInferenceFlowUnitTest, OnnxRunUnit) {
34+
auto ret = test_impl_->SetUp("dlengine_inference_onnx");
35+
ASSERT_EQ(ret, modelbox::STATUS_OK);
36+
test_impl_->Run("dlengine_cpu_onnx_RunUnit");
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "dlengine_cpu_inference_flowunit.h"
18+
#include "modelbox/base/driver_api_helper.h"
19+
#include "modelbox/device/cpu/device_cpu.h"
20+
21+
constexpr const char *FLOWUNIT_DESC = "A dlengine cpu inference flowunit";
22+
23+
std::shared_ptr<modelbox::DriverFactory> CreateDriverFactory() {
24+
return std::make_shared<DLEngineCPUInferenceFlowUnitFactory>();
25+
}
26+
27+
void DriverDescription(modelbox::DriverDesc *desc) {
28+
desc->SetName(FLOWUNIT_NAME);
29+
desc->SetClass(modelbox::DRIVER_CLASS_INFERENCE);
30+
desc->SetType(modelbox::DEVICE_TYPE);
31+
desc->SetDescription(FLOWUNIT_DESC);
32+
}
33+
34+
modelbox::Status DriverInit() { return modelbox::STATUS_OK; }
35+
36+
void DriverFini() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[base]
2+
name = "dlengine_inference_onnx"
3+
device = "cpu"
4+
version = "1.0.0"
5+
description = "an dlengine cpu inference flowunit"
6+
entry = "@CMAKE_SOURCE_DIR@/test/assets/test_model/test_dynamic.onnx"
7+
type = "inference"
8+
virtual_type = "dlengine"
9+
10+
[config]
11+
model_type = "onnx"
12+
precision = "FP16" # FP32/FP16
13+
14+
[input]
15+
[input.input1]
16+
name = "in1"
17+
min_shape = "1x3x16x16"
18+
opt_shape = "4x3x16x16"
19+
max_shape = "8x3x16x16"
20+
21+
[input.input2]
22+
name = "in2"
23+
min_shape = "1x3x16x16"
24+
opt_shape = "4x3x16x16"
25+
max_shape = "8x3x16x16"
26+
27+
[output]
28+
[output.output1]
29+
name = "out"

0 commit comments

Comments
 (0)