From 3a7bc37fbd59fcecc25259ba707ac62c03e772c0 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Sat, 18 Sep 2021 12:36:00 +0800 Subject: [PATCH] fix(lite): fix lite type define error GitOrigin-RevId: 10338593644b2c917ff5ca4553f941dfe4fbbc24 --- lite/example/CMakeLists.txt | 49 +--------- lite/example/c_example/CMakeLists.txt | 44 +++++++++ lite/example/c_example/main.c | 89 +++++++++++++++++++ lite/example/cpp_example/CMakeLists.txt | 47 ++++++++++ lite/example/{ => cpp_example}/example.h | 2 +- lite/example/{ => cpp_example}/main.cpp | 8 +- lite/example/{ => cpp_example}/mge/README.md | 0 lite/example/{ => cpp_example}/mge/basic.cpp | 2 +- .../{ => cpp_example}/mge/cpu_affinity.cpp | 2 +- .../{ => cpp_example}/mge/device_io.cpp | 2 +- .../mge/lite_c_interface.cpp | 2 +- .../mge/network_share_weights.cpp | 2 +- .../{ => cpp_example}/mge/reset_io.cpp | 2 +- .../{ => cpp_example}/mge/user_allocator.cpp | 2 +- .../{ => cpp_example}/mge/user_cryption.cpp | 2 +- lite/example/{ => cpp_example}/npy.h | 0 lite/include/lite/common_enum_c.h | 16 ++-- 17 files changed, 205 insertions(+), 66 deletions(-) create mode 100644 lite/example/c_example/CMakeLists.txt create mode 100644 lite/example/c_example/main.c create mode 100644 lite/example/cpp_example/CMakeLists.txt rename lite/example/{ => cpp_example}/example.h (98%) rename lite/example/{ => cpp_example}/main.cpp (97%) rename lite/example/{ => cpp_example}/mge/README.md (100%) rename lite/example/{ => cpp_example}/mge/basic.cpp (99%) rename lite/example/{ => cpp_example}/mge/cpu_affinity.cpp (98%) rename lite/example/{ => cpp_example}/mge/device_io.cpp (99%) rename lite/example/{ => cpp_example}/mge/lite_c_interface.cpp (99%) rename lite/example/{ => cpp_example}/mge/network_share_weights.cpp (98%) rename lite/example/{ => cpp_example}/mge/reset_io.cpp (98%) rename lite/example/{ => cpp_example}/mge/user_allocator.cpp (98%) rename lite/example/{ => cpp_example}/mge/user_cryption.cpp (98%) rename lite/example/{ => cpp_example}/npy.h (100%) diff --git a/lite/example/CMakeLists.txt b/lite/example/CMakeLists.txt index 9012fded9..229d2d797 100644 --- a/lite/example/CMakeLists.txt +++ b/lite/example/CMakeLists.txt @@ -1,47 +1,2 @@ -file (GLOB_RECURSE SOURCES ./*.cpp) -add_executable(lite_examples ${SOURCES}) - -if(LITE_BUILD_WITH_RKNPU) - #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check - target_link_options(lite_examples PRIVATE "-fuse-ld=gold") -endif() - -target_link_libraries(lite_examples lite_static) -if(LITE_BUILD_WITH_MGE AND MGE_WITH_ROCM) - # FIXME: hip obj can not find cpp obj only through lite_static - target_link_libraries(lite_examples megdnn) -endif() - -if(UNIX) - if(APPLE OR ANDROID) - target_link_libraries(lite_examples dl) - else() - target_link_libraries(lite_examples dl rt) - endif() -endif() - -install (TARGETS lite_examples - EXPORT ${LITE_EXPORT_TARGETS} - RUNTIME DESTINATION lite/bin) - -# add lite_examples_depends_shared for CI check symbol export valid -add_executable(lite_examples_depends_shared ${SOURCES}) - -if(LITE_BUILD_WITH_RKNPU) - #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check - target_link_options(lite_examples_depends_shared PRIVATE "-fuse-ld=gold") -endif() - -target_link_libraries(lite_examples_depends_shared lite_shared) - -if(UNIX) - if(APPLE OR ANDROID) - target_link_libraries(lite_examples_depends_shared dl) - else() - target_link_libraries(lite_examples_depends_shared dl rt) - endif() -endif() - -install (TARGETS lite_examples_depends_shared - EXPORT ${LITE_EXPORT_TARGETS} - RUNTIME DESTINATION lite/bin) +add_subdirectory(cpp_example) +add_subdirectory(c_example) diff --git a/lite/example/c_example/CMakeLists.txt b/lite/example/c_example/CMakeLists.txt new file mode 100644 index 000000000..141725e58 --- /dev/null +++ b/lite/example/c_example/CMakeLists.txt @@ -0,0 +1,44 @@ +add_executable(lite_c_examples ./main.c) +if(LITE_BUILD_WITH_RKNPU) + #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check + target_link_options(lite_c_examples PRIVATE "-fuse-ld=gold") +endif() +target_link_libraries(lite_c_examples lite_static) +if(LITE_BUILD_WITH_MGE AND MGE_WITH_ROCM) + # FIXME: hip obj can not find cpp obj only through lite_static + target_link_libraries(lite_c_examples megdnn) +endif() + +if(UNIX) + if(APPLE OR ANDROID) + target_link_libraries(lite_c_examples dl) + else() + target_link_libraries(lite_c_examples dl rt) + endif() +endif() + +install (TARGETS lite_c_examples + EXPORT ${LITE_EXPORT_TARGETS} + RUNTIME DESTINATION lite/bin) + +# add lite_examples_depends_shared for CI check symbol export valid +add_executable(lite_c_examples_depends_shared ./main.c) + +if(LITE_BUILD_WITH_RKNPU) + #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check + target_link_options(lite_c_examples_depends_shared PRIVATE "-fuse-ld=gold") +endif() + +target_link_libraries(lite_c_examples_depends_shared lite_shared) + +if(UNIX) + if(APPLE OR ANDROID) + target_link_libraries(lite_c_examples_depends_shared dl) + else() + target_link_libraries(lite_c_examples_depends_shared dl rt) + endif() +endif() + +install (TARGETS lite_c_examples_depends_shared + EXPORT ${LITE_EXPORT_TARGETS} + RUNTIME DESTINATION lite/bin) diff --git a/lite/example/c_example/main.c b/lite/example/c_example/main.c new file mode 100644 index 000000000..c8a91c7bd --- /dev/null +++ b/lite/example/c_example/main.c @@ -0,0 +1,89 @@ +/** + * \file example/c_example/main.c + * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") + * + * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + */ + +#include "lite-c/global_c.h" +#include "lite-c/network_c.h" +#include "lite-c/tensor_c.h" + +#include +#include + +#define LITE_CAPI_CHECK(_expr) \ + do { \ + int _ret = (_expr); \ + if (_ret) { \ + fprintf(stderr, "error msg: %s", LITE_get_last_error()); \ + return -1; \ + } \ + } while (0) + +int basic_c_interface(const char* mode_path) { + //! create and load the network + LiteNetwork c_network; + LITE_CAPI_CHECK( + LITE_make_network(&c_network, *default_config(), *default_network_io())); + + LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, mode_path)); + + //! set input data to input tensor + LiteTensor c_input_tensor; + LITE_CAPI_CHECK( + LITE_get_io_tensor(c_network, "data", LITE_IO, &c_input_tensor)); + void* dst_ptr; + size_t length_in_byte; + LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte(c_input_tensor, + &length_in_byte)); + LITE_CAPI_CHECK(LITE_get_tensor_memory(c_input_tensor, &dst_ptr)); + //! copy or forward data to network + memset(dst_ptr, 5, length_in_byte); + + //! forward + LITE_CAPI_CHECK(LITE_forward(c_network)); + LITE_CAPI_CHECK(LITE_wait(c_network)); + + //! get the output data or read tensor data + const char* output_name; + LiteTensor c_output_tensor; + //! get the first output tensor name + LITE_CAPI_CHECK(LITE_get_output_name(c_network, 0, &output_name)); + LITE_CAPI_CHECK(LITE_get_io_tensor(c_network, output_name, LITE_IO, + &c_output_tensor)); + void* output_ptr; + size_t length_output_in_byte; + LITE_CAPI_CHECK(LITE_get_tensor_memory(c_output_tensor, &output_ptr)); + LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte(c_output_tensor, + &length_output_in_byte)); + + size_t out_length = length_output_in_byte / sizeof(float); + printf("length=%zu\n", out_length); + + float max = -1.0f; + float sum = 0.0f; + for (size_t i = 0; i < out_length; i++) { + float data = ((float*)(output_ptr))[i]; + sum += data; + if (max < data) + max = data; + } + printf("max=%e, sum=%e\n", max, sum); + return 0; +} + +int main(int argc, char** argv) { + if (argc < 2) { + printf("usage: lite_c_examples , just test C interface " + "build.\n"); + return -1; + } + return basic_c_interface(argv[1]); +} + +// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/CMakeLists.txt b/lite/example/cpp_example/CMakeLists.txt new file mode 100644 index 000000000..9012fded9 --- /dev/null +++ b/lite/example/cpp_example/CMakeLists.txt @@ -0,0 +1,47 @@ +file (GLOB_RECURSE SOURCES ./*.cpp) +add_executable(lite_examples ${SOURCES}) + +if(LITE_BUILD_WITH_RKNPU) + #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check + target_link_options(lite_examples PRIVATE "-fuse-ld=gold") +endif() + +target_link_libraries(lite_examples lite_static) +if(LITE_BUILD_WITH_MGE AND MGE_WITH_ROCM) + # FIXME: hip obj can not find cpp obj only through lite_static + target_link_libraries(lite_examples megdnn) +endif() + +if(UNIX) + if(APPLE OR ANDROID) + target_link_libraries(lite_examples dl) + else() + target_link_libraries(lite_examples dl rt) + endif() +endif() + +install (TARGETS lite_examples + EXPORT ${LITE_EXPORT_TARGETS} + RUNTIME DESTINATION lite/bin) + +# add lite_examples_depends_shared for CI check symbol export valid +add_executable(lite_examples_depends_shared ${SOURCES}) + +if(LITE_BUILD_WITH_RKNPU) + #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check + target_link_options(lite_examples_depends_shared PRIVATE "-fuse-ld=gold") +endif() + +target_link_libraries(lite_examples_depends_shared lite_shared) + +if(UNIX) + if(APPLE OR ANDROID) + target_link_libraries(lite_examples_depends_shared dl) + else() + target_link_libraries(lite_examples_depends_shared dl rt) + endif() +endif() + +install (TARGETS lite_examples_depends_shared + EXPORT ${LITE_EXPORT_TARGETS} + RUNTIME DESTINATION lite/bin) diff --git a/lite/example/example.h b/lite/example/cpp_example/example.h similarity index 98% rename from lite/example/example.h rename to lite/example/cpp_example/example.h index eaec211de..37c12b9e3 100644 --- a/lite/example/example.h +++ b/lite/example/cpp_example/example.h @@ -1,5 +1,5 @@ /** - * \file example/example.cpp + * \file example/cpp_example/example.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/main.cpp b/lite/example/cpp_example/main.cpp similarity index 97% rename from lite/example/main.cpp rename to lite/example/cpp_example/main.cpp index ca03caf41..2f93e8faf 100644 --- a/lite/example/main.cpp +++ b/lite/example/cpp_example/main.cpp @@ -1,5 +1,5 @@ /** - * \file example/example.cpp + * \file example/cpp_example/cpp_example/example.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. @@ -133,7 +133,11 @@ int main(int argc, char** argv) { auto example = (*map)[args.example_name]; if (example) { printf("Begin to run %s example.\n", args.example_name.c_str()); - return example(args); + if (example(args)) { + return 0; + } else { + return -1; + } } else { printf("The example of %s is not registed.", args.example_name.c_str()); return -1; diff --git a/lite/example/mge/README.md b/lite/example/cpp_example/mge/README.md similarity index 100% rename from lite/example/mge/README.md rename to lite/example/cpp_example/mge/README.md diff --git a/lite/example/mge/basic.cpp b/lite/example/cpp_example/mge/basic.cpp similarity index 99% rename from lite/example/mge/basic.cpp rename to lite/example/cpp_example/mge/basic.cpp index 7300e09a8..bcde2f51e 100644 --- a/lite/example/mge/basic.cpp +++ b/lite/example/cpp_example/mge/basic.cpp @@ -1,5 +1,5 @@ /** - * \file example/basic.cpp + * \file example/cpp_example/basic.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/cpu_affinity.cpp b/lite/example/cpp_example/mge/cpu_affinity.cpp similarity index 98% rename from lite/example/mge/cpu_affinity.cpp rename to lite/example/cpp_example/mge/cpu_affinity.cpp index 508910d4e..a11210047 100644 --- a/lite/example/mge/cpu_affinity.cpp +++ b/lite/example/cpp_example/mge/cpu_affinity.cpp @@ -1,5 +1,5 @@ /** - * \file example/cpu_affinity.cpp + * \file example/cpp_example/cpu_affinity.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/device_io.cpp b/lite/example/cpp_example/mge/device_io.cpp similarity index 99% rename from lite/example/mge/device_io.cpp rename to lite/example/cpp_example/mge/device_io.cpp index 3aee29490..fa5c56837 100644 --- a/lite/example/mge/device_io.cpp +++ b/lite/example/cpp_example/mge/device_io.cpp @@ -1,5 +1,5 @@ /** - * \file example/device_io.cpp + * \file example/cpp_example/device_io.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/lite_c_interface.cpp b/lite/example/cpp_example/mge/lite_c_interface.cpp similarity index 99% rename from lite/example/mge/lite_c_interface.cpp rename to lite/example/cpp_example/mge/lite_c_interface.cpp index c96794802..2696a1caa 100644 --- a/lite/example/mge/lite_c_interface.cpp +++ b/lite/example/cpp_example/mge/lite_c_interface.cpp @@ -1,5 +1,5 @@ /** - * \file example/basic_c_interface.cpp + * \file example/cpp_example/basic_c_interface.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/network_share_weights.cpp b/lite/example/cpp_example/mge/network_share_weights.cpp similarity index 98% rename from lite/example/mge/network_share_weights.cpp rename to lite/example/cpp_example/mge/network_share_weights.cpp index 818f09d8a..61b9cb364 100644 --- a/lite/example/mge/network_share_weights.cpp +++ b/lite/example/cpp_example/mge/network_share_weights.cpp @@ -1,5 +1,5 @@ /** - * \file example/network_share_weights.cpp + * \file example/cpp_example/network_share_weights.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/reset_io.cpp b/lite/example/cpp_example/mge/reset_io.cpp similarity index 98% rename from lite/example/mge/reset_io.cpp rename to lite/example/cpp_example/mge/reset_io.cpp index 1a8143b30..5d21526b0 100644 --- a/lite/example/mge/reset_io.cpp +++ b/lite/example/cpp_example/mge/reset_io.cpp @@ -1,5 +1,5 @@ /** - * \file example/reset_io.cpp + * \file example/cpp_example/reset_io.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/user_allocator.cpp b/lite/example/cpp_example/mge/user_allocator.cpp similarity index 98% rename from lite/example/mge/user_allocator.cpp rename to lite/example/cpp_example/mge/user_allocator.cpp index a0f78f259..73b9b41df 100644 --- a/lite/example/mge/user_allocator.cpp +++ b/lite/example/cpp_example/mge/user_allocator.cpp @@ -1,5 +1,5 @@ /** - * \file example/user_allocator.cpp + * \file example/cpp_example/user_allocator.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/mge/user_cryption.cpp b/lite/example/cpp_example/mge/user_cryption.cpp similarity index 98% rename from lite/example/mge/user_cryption.cpp rename to lite/example/cpp_example/mge/user_cryption.cpp index 88496090d..ef7e8c6de 100644 --- a/lite/example/mge/user_cryption.cpp +++ b/lite/example/cpp_example/mge/user_cryption.cpp @@ -1,5 +1,5 @@ /** - * \file example/user_cryption.cpp + * \file example/cpp_example/user_cryption.cpp * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. diff --git a/lite/example/npy.h b/lite/example/cpp_example/npy.h similarity index 100% rename from lite/example/npy.h rename to lite/example/cpp_example/npy.h diff --git a/lite/include/lite/common_enum_c.h b/lite/include/lite/common_enum_c.h index 361b52d2b..f9b7a3a73 100644 --- a/lite/include/lite/common_enum_c.h +++ b/lite/include/lite/common_enum_c.h @@ -15,18 +15,18 @@ /*! * \brief The log level. */ -typedef enum LiteLogLevel { +typedef enum { DEBUG = 0, /*!< The lowest level and most verbose */ INFO = 1, /*!< The lowest level and most verbose */ WARN = 2, /*!< Print only warning and errors */ ERROR = 3, /*!< Print only errors */ } LiteLogLevel; -typedef enum LiteBackend { +typedef enum { LITE_DEFAULT = 0, //! default backend is mge } LiteBackend; -typedef enum LiteDeviceType { +typedef enum { LITE_CPU = 0, LITE_CUDA = 1, LITE_ATLAS = 3, @@ -36,7 +36,7 @@ typedef enum LiteDeviceType { LITE_DEVICE_DEFAULT = 5, } LiteDeviceType; -typedef enum LiteDataType { +typedef enum { LITE_FLOAT = 0, LITE_HALF = 1, LITE_INT = 2, @@ -46,9 +46,9 @@ typedef enum LiteDataType { LITE_UINT = 6, LITE_UINT16 = 7, LITE_INT64 = 8, -} LiteCDataType; +} LiteDataType; -typedef enum LiteTensorPhase { +typedef enum { //! Tensor maybe input or output LITE_IO = 0, //! Tensor is input @@ -61,7 +61,7 @@ typedef enum LiteTensorPhase { * \brief the input and output type, include SHAPE and VALUE * sometimes user only need the shape of the output tensor */ -typedef enum LiteIOType { +typedef enum { LITE_IO_VALUE = 0, LITE_IO_SHAPE = 1, } LiteIOType; @@ -88,7 +88,7 @@ typedef enum LiteIOType { * 5. LITE_ALGO_PROFILE | LITE_ALGO_OPTIMIZED | LITE_ALGO_REPRODUCIBLE means: * profile the best algorithm form the optimzed and reproducible algorithms */ -typedef enum LiteAlgoSelectStrategy { +typedef enum { LITE_ALGO_HEURISTIC = 1 << 0, LITE_ALGO_PROFILE = 1 << 1, LITE_ALGO_REPRODUCIBLE = 1 << 2, -- GitLab