Skip to content

Commit eccbef2

Browse files
DefTruthjoey12300
andauthored
Add text examples (#18)
* Add text examples (PaddlePaddle#69) * Add text init file * Fix some cmake * Add TextModel, TextResult, PredictionOption * Add text preprocessor, postprocessor, pipeline of text model prediction * remove Text prefix * Add faster_tokenizer deps * Add ernie IE examples * Add eigen computation * eigen.h->compute.h * Add python ernie examples * Fix some comments * Add const Data of FDTensor (PaddlePaddle#72) * Add const Data of FDTensor * use SetExternalData instead of memcpy * Add Softmax * Add Max * Add viterbi decode for ernie tokencls Co-authored-by: Jack Zhou <[email protected]>
1 parent 904f1e1 commit eccbef2

31 files changed

+1831
-13
lines changed

CMakeLists.txt

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ if(NOT MSVC)
3737
endif(NOT MSVC)
3838

3939
#############################CMAKE FOR FASTDEPLOY################################
40-
option(ENABLE_PADDLE_FRONTEND "if to enable PaddlePaddle frontend to support load paddle model in fastdeploy." ON)
41-
option(WITH_GPU "if WITH_GPU=ON, will enable onnxruntime-gpu/paddle-infernce-gpu" OFF)
42-
option(ENABLE_ORT_BACKEND "if to enable onnxruntime backend." OFF)
43-
option(ENABLE_TRT_BACKEND "if to enable tensorrt backend." OFF)
44-
option(ENABLE_PADDLE_BACKEND "if to enable paddle backend." OFF)
45-
option(CUDA_DIRECTORY "if build tensorrt backend, need to define path of cuda library.")
46-
option(TRT_DIRECTORY "if build tensorrt backend, need to define path of tensorrt library.")
47-
option(ENABLE_VISION "if to enable vision models usage." OFF)
48-
option(ENABLE_VISION_VISUALIZE "if to enable visualize vision model result toolbox." ON)
40+
option(ENABLE_PADDLE_FRONTEND "Whether to enable PaddlePaddle frontend to support load paddle model in fastdeploy." ON)
41+
option(WITH_GPU "Whether WITH_GPU=ON, will enable onnxruntime-gpu/paddle-infernce-gpu" OFF)
42+
option(ENABLE_ORT_BACKEND "Whether to enable onnxruntime backend." OFF)
43+
option(ENABLE_TRT_BACKEND "Whether to enable tensorrt backend." OFF)
44+
option(ENABLE_PADDLE_BACKEND "Whether to enable paddle backend." OFF)
45+
option(CUDA_DIRECTORY "If build tensorrt backend, need to define path of cuda library.")
46+
option(TRT_DIRECTORY "If build tensorrt backend, need to define path of tensorrt library.")
47+
option(ENABLE_VISION "Whether to enable vision models usage." OFF)
48+
option(ENABLE_VISION_VISUALIZE "Whether to enable visualize vision model result toolbox." ON)
49+
option(ENABLE_TEXT "Whether to enable text models usage." OFF)
4950

5051
# Please don't open this flag now, some bugs exists.
51-
option(ENABLE_OPENCV_CUDA "if to enable opencv with cuda, this will allow process image with GPU." OFF)
52-
option(ENABLE_DEBUG "if to enable print debug information, this may reduce performance." OFF)
52+
option(ENABLE_OPENCV_CUDA "Whether to enable opencv with cuda, this will allow process image with GPU." OFF)
53+
option(ENABLE_DEBUG "Whether to enable print debug information, this may reduce performance." OFF)
5354

5455
# Whether to build fastdeply with vision/text/... examples, only for testings.
5556
option(WITH_VISION_EXAMPLES "Whether to build fastdeply with vision examples" OFF)
57+
option(WITH_TEXT_EXAMPLES "Whether to build fastdeply with text examples" OFF)
5658

5759
# Check for 32bit system
5860
if(WIN32)
@@ -98,21 +100,29 @@ if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
98100
set(ENABLE_VISION_VISUALIZE ON CACHE BOOL "force to enable visualize vision model result toolbox" FORCE)
99101
endif()
100102

103+
if (WITH_TEXT_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
104+
# ENABLE_TEXT must be ON if enable text examples.
105+
message(STATUS "Found WITH_TEXT_EXAMPLES ON, so, force ENABLE_TEXT ON")
106+
set(ENABLE_TEXT ON CACHE BOOL "force to enable text models usage" FORCE)
107+
endif()
108+
101109
add_definitions(-DFASTDEPLOY_LIB)
102110
file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*.cc)
103111
file(GLOB_RECURSE DEPLOY_ORT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/ort/*.cc)
104112
file(GLOB_RECURSE DEPLOY_PADDLE_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/paddle/*.cc)
105113
file(GLOB_RECURSE DEPLOY_TRT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/tensorrt/*.cc ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/tensorrt/*.cpp)
106114
file(GLOB_RECURSE DEPLOY_VISION_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*.cc)
115+
file(GLOB_RECURSE DEPLOY_TEXT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/text/*.cc)
107116
file(GLOB_RECURSE DEPLOY_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/*.cc ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*_pybind.cc)
108-
list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_ORT_SRCS} ${DEPLOY_PADDLE_SRCS} ${DEPLOY_TRT_SRCS} ${DEPLOY_VISION_SRCS})
117+
list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_ORT_SRCS} ${DEPLOY_PADDLE_SRCS} ${DEPLOY_TRT_SRCS} ${DEPLOY_VISION_SRCS} ${DEPLOY_TEXT_SRCS})
109118

110119
set(DEPEND_LIBS "")
111120

112121
file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" FASTDEPLOY_VERSION)
113122
string(STRIP "${FASTDEPLOY_VERSION}" FASTDEPLOY_VERSION)
114123

115124
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
125+
include(external/eigen.cmake)
116126
if(ENABLE_PADDLE_FRONTEND)
117127
add_definitions(-DENABLE_PADDLE_FRONTEND)
118128
include(${PROJECT_SOURCE_DIR}/external/paddle2onnx.cmake)
@@ -207,6 +217,12 @@ else()
207217
endif()
208218
endif()
209219

220+
if(ENABLE_TEXT)
221+
add_definitions(-DENABLE_TEXT)
222+
list(APPEND ALL_DEPLOY_SRCS ${DEPLOY_TEXT_SRCS})
223+
include(external/faster_tokenizer.cmake)
224+
endif()
225+
210226
configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h)
211227
configure_file(${PROJECT_SOURCE_DIR}/FastDeploy.cmake.in ${PROJECT_SOURCE_DIR}/FastDeploy.cmake @ONLY)
212228

@@ -249,6 +265,15 @@ if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
249265
add_subdirectory(examples)
250266
endif()
251267

268+
if (WITH_TEXT_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
269+
add_definitions(-DWITH_TEXT_EXAMPLES)
270+
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/examples/bin)
271+
# Avoid to add_subdirectory repeatedly
272+
if (NOT WITH_VISION_EXAMPLES)
273+
add_subdirectory(examples)
274+
endif()
275+
endif()
276+
252277
include(external/summary.cmake)
253278
fastdeploy_summary()
254279
if(WIN32)
@@ -307,6 +332,12 @@ if(BUILD_FASTDEPLOY_PYTHON)
307332
file(GLOB_RECURSE VISION_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*_pybind.cc)
308333
list(REMOVE_ITEM DEPLOY_PYBIND_SRCS ${VISION_PYBIND_SRCS})
309334
endif()
335+
336+
if (NOT ENABLE_TEXT)
337+
file(GLOB_RECURSE TEXT_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/text/*_pybind.cc)
338+
list(REMOVE_ITEM DEPLOY_PYBIND_SRCS ${TEXT_PYBIND_SRCS})
339+
endif()
340+
310341
add_library(${PY_LIBRARY_NAME} MODULE ${DEPLOY_PYBIND_SRCS})
311342
redefine_file_macro(${PY_LIBRARY_NAME})
312343
set_target_properties(${PY_LIBRARY_NAME} PROPERTIES PREFIX "")

FastDeploy.cmake.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(PADDLEINFERENCE_VERSION @PADDLEINFERENCE_VERSION@)
77
set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@)
88
set(ENABLE_PADDLE_FRONTEND @ENABLE_PADDLE_FRONTEND@)
99
set(ENABLE_VISION @ENABLE_VISION@)
10+
set(ENABLE_TEXT @ENABLE_TEXT@)
1011
set(ENABLE_OPENCV_CUDA @ENABLE_OPENCV_CUDA@)
1112
set(LIBRARY_NAME @LIBRARY_NAME@)
1213

@@ -87,6 +88,10 @@ if(ENABLE_VISION)
8788
endif()
8889
endif()
8990

91+
if (ENABLE_TEXT)
92+
# Add dependency libs later
93+
endif()
94+
9095
if(ENABLE_PADDLE_FRONTEND)
9196
find_library(PADDLE2ONNX_LIB paddle2onnx ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib)
9297
list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
@@ -109,6 +114,7 @@ if(ENABLE_PADDLE_BACKEND)
109114
endif()
110115
message(STATUS " ENABLE_TRT_BACKEND : ${ENABLE_TRT_BACKEND}")
111116
message(STATUS " ENABLE_VISION : ${ENABLE_VISION}")
117+
message(STATUS " ENABLE_TEXT : ${ENABLE_TEXT}")
112118

113119
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
114120
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")

csrcs/fastdeploy/core/fd_tensor.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ void* FDTensor::Data() {
5050
return data.data();
5151
}
5252

53+
const void* FDTensor::Data() const {
54+
if (external_data_ptr != nullptr) {
55+
return external_data_ptr;
56+
}
57+
return data.data();
58+
}
59+
5360
void FDTensor::SetExternalData(const std::vector<int>& new_shape,
5461
const FDDataType& data_type, void* data_buffer) {
5562
dtype = data_type;

csrcs/fastdeploy/core/fd_tensor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct FASTDEPLOY_DECL FDTensor {
5454
// will copy to cpu store in `temporary_cpu_buffer`
5555
void* Data();
5656

57+
const void* Data() const;
58+
5759
// Set user memory buffer for Tensor, the memory is managed by
5860
// the user it self, but the Tensor will share the memory with user
5961
// So take care with the user buffer
@@ -81,4 +83,4 @@ struct FASTDEPLOY_DECL FDTensor {
8183
explicit FDTensor(const std::string& tensor_name);
8284
};
8385

84-
} // namespace fastdeploy
86+
} // namespace fastdeploy

csrcs/fastdeploy/core/fd_type.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,31 @@ std::string Str(const FDDataType& fdt) {
9393
return out;
9494
}
9595

96+
template <typename PlainType>
97+
const FDDataType TypeToDataType<PlainType>::dtype = UNKNOWN1;
98+
99+
template <>
100+
const FDDataType TypeToDataType<bool>::dtype = BOOL;
101+
102+
template <>
103+
const FDDataType TypeToDataType<int16_t>::dtype = INT16;
104+
105+
template <>
106+
const FDDataType TypeToDataType<int32_t>::dtype = INT32;
107+
108+
template <>
109+
const FDDataType TypeToDataType<int64_t>::dtype = INT64;
110+
111+
template <>
112+
const FDDataType TypeToDataType<float>::dtype = FP32;
113+
114+
template <>
115+
const FDDataType TypeToDataType<double>::dtype = FP64;
116+
117+
template <>
118+
const FDDataType TypeToDataType<uint8_t>::dtype = UINT8;
119+
120+
template <>
121+
const FDDataType TypeToDataType<int8_t>::dtype = INT8;
122+
96123
} // namespace fastdeploy

csrcs/fastdeploy/core/fd_type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ enum FASTDEPLOY_DECL FDDataType {
5454
FASTDEPLOY_DECL std::string Str(const FDDataType& fdt);
5555

5656
FASTDEPLOY_DECL int32_t FDDataTypeSize(const FDDataType& data_dtype);
57+
58+
template <typename PlainType>
59+
struct FASTDEPLOY_DECL TypeToDataType {
60+
static const FDDataType dtype;
61+
};
62+
5763
} // namespace fastdeploy

csrcs/fastdeploy/text.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "fastdeploy/core/config.h"
17+
#ifdef ENABLE_TEXT
18+
#include "fastdeploy/text/text_model.h"
19+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include "fastdeploy/utils/utils.h"
17+
18+
namespace fastdeploy {
19+
namespace text {
20+
21+
struct FASTDEPLOY_DECL TextPreprocessOption {};
22+
struct FASTDEPLOY_DECL TextPostprocessOption {};
23+
struct FASTDEPLOY_DECL PredictionOption {};
24+
25+
} // namespace text
26+
} // namespace fastdeploy
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include "fastdeploy/text/common/result.h"
15+
16+
namespace fastdeploy {
17+
namespace text {} // namespace text
18+
} // namespace fastdeploy
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
#include "fastdeploy/utils/utils.h"
16+
17+
namespace fastdeploy {
18+
namespace text {
19+
20+
struct FASTDEPLOY_DECL Result {};
21+
22+
} // namespace text
23+
} // namespace fastdeploy

0 commit comments

Comments
 (0)