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

Add text examples #18

Merged
merged 2 commits into from
Aug 8, 2022
Merged
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
55 changes: 43 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ if(NOT MSVC)
endif(NOT MSVC)

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

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

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

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

if (WITH_TEXT_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
# ENABLE_TEXT must be ON if enable text examples.
message(STATUS "Found WITH_TEXT_EXAMPLES ON, so, force ENABLE_TEXT ON")
set(ENABLE_TEXT ON CACHE BOOL "force to enable text models usage" FORCE)
endif()

add_definitions(-DFASTDEPLOY_LIB)
file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*.cc)
file(GLOB_RECURSE DEPLOY_ORT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/ort/*.cc)
file(GLOB_RECURSE DEPLOY_PADDLE_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/paddle/*.cc)
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)
file(GLOB_RECURSE DEPLOY_VISION_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*.cc)
file(GLOB_RECURSE DEPLOY_TEXT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/text/*.cc)
file(GLOB_RECURSE DEPLOY_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/*.cc ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*_pybind.cc)
list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_ORT_SRCS} ${DEPLOY_PADDLE_SRCS} ${DEPLOY_TRT_SRCS} ${DEPLOY_VISION_SRCS})
list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_ORT_SRCS} ${DEPLOY_PADDLE_SRCS} ${DEPLOY_TRT_SRCS} ${DEPLOY_VISION_SRCS} ${DEPLOY_TEXT_SRCS})

set(DEPEND_LIBS "")

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

set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
include(external/eigen.cmake)
if(ENABLE_PADDLE_FRONTEND)
add_definitions(-DENABLE_PADDLE_FRONTEND)
include(${PROJECT_SOURCE_DIR}/external/paddle2onnx.cmake)
Expand Down Expand Up @@ -207,6 +217,12 @@ else()
endif()
endif()

if(ENABLE_TEXT)
add_definitions(-DENABLE_TEXT)
list(APPEND ALL_DEPLOY_SRCS ${DEPLOY_TEXT_SRCS})
include(external/faster_tokenizer.cmake)
endif()

configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h)
configure_file(${PROJECT_SOURCE_DIR}/FastDeploy.cmake.in ${PROJECT_SOURCE_DIR}/FastDeploy.cmake @ONLY)

Expand Down Expand Up @@ -249,6 +265,15 @@ if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
add_subdirectory(examples)
endif()

if (WITH_TEXT_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
add_definitions(-DWITH_TEXT_EXAMPLES)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/examples/bin)
# Avoid to add_subdirectory repeatedly
if (NOT WITH_VISION_EXAMPLES)
add_subdirectory(examples)
endif()
endif()

include(external/summary.cmake)
fastdeploy_summary()
if(WIN32)
Expand Down Expand Up @@ -307,6 +332,12 @@ if(BUILD_FASTDEPLOY_PYTHON)
file(GLOB_RECURSE VISION_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*_pybind.cc)
list(REMOVE_ITEM DEPLOY_PYBIND_SRCS ${VISION_PYBIND_SRCS})
endif()

if (NOT ENABLE_TEXT)
file(GLOB_RECURSE TEXT_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/text/*_pybind.cc)
list(REMOVE_ITEM DEPLOY_PYBIND_SRCS ${TEXT_PYBIND_SRCS})
endif()

add_library(${PY_LIBRARY_NAME} MODULE ${DEPLOY_PYBIND_SRCS})
redefine_file_macro(${PY_LIBRARY_NAME})
set_target_properties(${PY_LIBRARY_NAME} PROPERTIES PREFIX "")
Expand Down
6 changes: 6 additions & 0 deletions FastDeploy.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(PADDLEINFERENCE_VERSION @PADDLEINFERENCE_VERSION@)
set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@)
set(ENABLE_PADDLE_FRONTEND @ENABLE_PADDLE_FRONTEND@)
set(ENABLE_VISION @ENABLE_VISION@)
set(ENABLE_TEXT @ENABLE_TEXT@)
set(ENABLE_OPENCV_CUDA @ENABLE_OPENCV_CUDA@)
set(LIBRARY_NAME @LIBRARY_NAME@)

Expand Down Expand Up @@ -87,6 +88,10 @@ if(ENABLE_VISION)
endif()
endif()

if (ENABLE_TEXT)
# Add dependency libs later
endif()

if(ENABLE_PADDLE_FRONTEND)
find_library(PADDLE2ONNX_LIB paddle2onnx ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib)
list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
Expand All @@ -109,6 +114,7 @@ if(ENABLE_PADDLE_BACKEND)
endif()
message(STATUS " ENABLE_TRT_BACKEND : ${ENABLE_TRT_BACKEND}")
message(STATUS " ENABLE_VISION : ${ENABLE_VISION}")
message(STATUS " ENABLE_TEXT : ${ENABLE_TEXT}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")
Expand Down
7 changes: 7 additions & 0 deletions csrcs/fastdeploy/core/fd_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ void* FDTensor::Data() {
return data.data();
}

const void* FDTensor::Data() const {
if (external_data_ptr != nullptr) {
return external_data_ptr;
}
return data.data();
}

void FDTensor::SetExternalData(const std::vector<int>& new_shape,
const FDDataType& data_type, void* data_buffer) {
dtype = data_type;
Expand Down
4 changes: 3 additions & 1 deletion csrcs/fastdeploy/core/fd_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct FASTDEPLOY_DECL FDTensor {
// will copy to cpu store in `temporary_cpu_buffer`
void* Data();

const void* Data() const;

// Set user memory buffer for Tensor, the memory is managed by
// the user it self, but the Tensor will share the memory with user
// So take care with the user buffer
Expand Down Expand Up @@ -81,4 +83,4 @@ struct FASTDEPLOY_DECL FDTensor {
explicit FDTensor(const std::string& tensor_name);
};

} // namespace fastdeploy
} // namespace fastdeploy
27 changes: 27 additions & 0 deletions csrcs/fastdeploy/core/fd_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,31 @@ std::string Str(const FDDataType& fdt) {
return out;
}

template <typename PlainType>
const FDDataType TypeToDataType<PlainType>::dtype = UNKNOWN1;

template <>
const FDDataType TypeToDataType<bool>::dtype = BOOL;

template <>
const FDDataType TypeToDataType<int16_t>::dtype = INT16;

template <>
const FDDataType TypeToDataType<int32_t>::dtype = INT32;

template <>
const FDDataType TypeToDataType<int64_t>::dtype = INT64;

template <>
const FDDataType TypeToDataType<float>::dtype = FP32;

template <>
const FDDataType TypeToDataType<double>::dtype = FP64;

template <>
const FDDataType TypeToDataType<uint8_t>::dtype = UINT8;

template <>
const FDDataType TypeToDataType<int8_t>::dtype = INT8;

} // namespace fastdeploy
6 changes: 6 additions & 0 deletions csrcs/fastdeploy/core/fd_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ enum FASTDEPLOY_DECL FDDataType {
FASTDEPLOY_DECL std::string Str(const FDDataType& fdt);

FASTDEPLOY_DECL int32_t FDDataTypeSize(const FDDataType& data_dtype);

template <typename PlainType>
struct FASTDEPLOY_DECL TypeToDataType {
static const FDDataType dtype;
};

} // namespace fastdeploy
19 changes: 19 additions & 0 deletions csrcs/fastdeploy/text.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#pragma once

#include "fastdeploy/core/config.h"
#ifdef ENABLE_TEXT
#include "fastdeploy/text/text_model.h"
#endif
26 changes: 26 additions & 0 deletions csrcs/fastdeploy/text/common/option.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once
#include "fastdeploy/utils/utils.h"

namespace fastdeploy {
namespace text {

struct FASTDEPLOY_DECL TextPreprocessOption {};
struct FASTDEPLOY_DECL TextPostprocessOption {};
struct FASTDEPLOY_DECL PredictionOption {};

} // namespace text
} // namespace fastdeploy
18 changes: 18 additions & 0 deletions csrcs/fastdeploy/text/common/result.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "fastdeploy/text/common/result.h"

namespace fastdeploy {
namespace text {} // namespace text
} // namespace fastdeploy
23 changes: 23 additions & 0 deletions csrcs/fastdeploy/text/common/result.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#pragma once
#include "fastdeploy/utils/utils.h"

namespace fastdeploy {
namespace text {

struct FASTDEPLOY_DECL Result {};

} // namespace text
} // namespace fastdeploy
31 changes: 31 additions & 0 deletions csrcs/fastdeploy/text/postprocessor/postprocessor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "fastdeploy/text/postprocessor/postprocessor.h"

namespace fastdeploy {
namespace text {

bool Postprocessor::Decode(const std::vector<FDTensor>& model_result,
Result* decoded_result) const {
return true;
}

bool Postprocessor::DecodeBatch(const std::vector<FDTensor>& model_result,
Result* decoded_result) const {
return true;
}

} // namespace text
} // namespace fastdeploy
34 changes: 34 additions & 0 deletions csrcs/fastdeploy/text/postprocessor/postprocessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/text/common/result.h"
#include "fastdeploy/utils/utils.h"

namespace fastdeploy {
namespace text {

class Postprocessor {
public:
virtual bool Decode(const std::vector<FDTensor>& model_result,
Result* decoded_result) const;
virtual bool DecodeBatch(const std::vector<FDTensor>& model_result,
Result* decoded_result) const;
};

} // namespace text
} // namespace fastdeploy
32 changes: 32 additions & 0 deletions csrcs/fastdeploy/text/preprocessor/preprocessor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "fastdeploy/text/preprocessor/preprocessor.h"

namespace fastdeploy {
namespace text {

bool Preprocessor::Encode(const std::string& raw_text,
std::vector<FDTensor>* encoded_tensor) const {
return true;
}

bool Preprocessor::EncodeBatch(const std::vector<std::string>& raw_texts,
std::vector<FDTensor>* encoded_tensor) const {
return true;
}

} // namespace text
} // namespace fastdeploy
Loading