diff --git a/CMakeLists.txt b/CMakeLists.txt index 8351a520..58ab0049 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,14 +171,9 @@ function(RELATIVE_PROTOBUF_GENERATE_CPP NAME SRCS HDRS ROOT_DIR DEPEND) file(RELATIVE_PATH REL_DIR ${ROOT_DIR} ${FILE_DIR}) set(OUTPUT_PROTO_DIR "${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}") - set(OUTPUT_PB_HEADER "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.h") + set(OUTPUT_PB_HEADER "${PROTOBUF_INCLUDE_DIRS}/${GENERATED_FILE_WE}.pb.h") set(OUTPUT_PB_SRC "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.cc") set(GENERATED_PROTO "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.proto") - if(NOT (ONNX_NAMESPACE STREQUAL "onnx")) - # We need this dummy header generated by gen_proto.py when ONNX_NAMESPACE - # is not onnx - list(APPEND ${HDRS} "${OUTPUT_PROTO_DIR}/${GENERATED_FILE_WE}.pb.h") - endif() list(APPEND ${SRCS} "${OUTPUT_PB_SRC}") list(APPEND ${HDRS} "${OUTPUT_PB_HEADER}") @@ -217,7 +212,7 @@ function(RELATIVE_PROTOBUF_GENERATE_CPP NAME SRCS HDRS ROOT_DIR DEPEND) DEPENDS ${INFILE} COMMENT "Running gen_proto.py on ${INFILE}" VERBATIM) - message("Generated: ${GENERATED_PROTO}") + message("Generated: ${GENERATED_PROTO}\n") set(PROTOC_ARGS ${GENERATED_PROTO} -I @@ -368,6 +363,14 @@ target_include_directories(onnx PUBLIC target_link_libraries(onnx PUBLIC onnx_proto) add_onnx_global_defines(onnx) +add_library(onnx_c "${ONNX_ROOT}/onnx/onnx-c.h" "${ONNX_ROOT}/onnx/onnx-c.cc") +target_include_directories(onnx_c PUBLIC + $ + $ + $) +target_link_libraries(onnx_c PUBLIC onnx onnx_proto) +add_onnx_global_defines(onnx_c) + if(BUILD_ONNX_PYTHON) if("${PY_EXT_SUFFIX}" STREQUAL "") if(MSVC) @@ -434,7 +437,6 @@ if(BUILD_ONNX_PYTHON) target_link_libraries(onnx_cpp2py_export PRIVATE ${PYTHON_LIBRARIES}) target_compile_options(onnx_cpp2py_export PRIVATE /MP - /WX /wd4800 # disable warning type' : forcing # value to bool 'true' or 'false' # (performance warning) @@ -481,7 +483,6 @@ endif() if(MSVC) target_compile_options(onnx_proto PRIVATE /MP - /WX /wd4800 # disable warning type' : forcing value # to bool 'true' or 'false' # (performance warning) @@ -494,7 +495,6 @@ if(MSVC) ${EXTRA_FLAGS}) target_compile_options(onnx PRIVATE /MP - /WX /wd4800 # disable warning type' : forcing value # to bool 'true' or 'false' # (performance warning) @@ -619,6 +619,7 @@ install(EXPORT ONNXTargets DESTINATION share/cmake/ONNX) install(TARGETS onnx onnx_proto onnxifi onnxifi_dummy onnxifi_loader + onnx_c EXPORT ONNXTargets DESTINATION lib) if(NOT ANDROID AND NOT IOS) diff --git a/onnx/onnx-c.cc b/onnx/onnx-c.cc new file mode 100644 index 00000000..941e3311 --- /dev/null +++ b/onnx/onnx-c.cc @@ -0,0 +1,18 @@ +#include "onnx/onnx-c.h" +#include "onnx/proto_utils.h" +#include "onnx/shape_inference/implementation.h" + +extern "C" { + +using namespace onnx; + +size_t onnx_proto_shape_inference(const char* buffer, size_t size, char* output) { + ModelProto proto{}; + bool status = ParseProtoFromBytes(&proto, buffer, size); + shape_inference::InferShapes(proto); + int written = proto.ByteSizeLong(); + proto.SerializeToArray(output, written); + return static_cast(written); +} + +} // extern "C" diff --git a/onnx/onnx-c.h b/onnx/onnx-c.h new file mode 100644 index 00000000..6e42ef55 --- /dev/null +++ b/onnx/onnx-c.h @@ -0,0 +1,26 @@ + +#ifndef THIRD_PARTY_ONNX_C_H_ +#define THIRD_PARTY_ONNX_C_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief Inferes shape from protocol buffers model. + * + * @param buffer: Pointer to protocol buffers message. + * @param size: Size of the protocol buffers message. + * @param output: Pointer to output buffer. + * @retval Bytes written to output buffer. + */ +size_t +onnx_proto_shape_inference(const char* buffer, size_t size, char* output); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* THIRD_PARTY_ONNX_C_H_ */ \ No newline at end of file