diff --git a/lite/example/cpp_example/CMakeLists.txt b/lite/example/cpp_example/CMakeLists.txt index 9012fded9a2d8bef2b43b0feb07c8d5203628708..f7227b62f1b491255b5dbd679a52c4ab1c42df15 100644 --- a/lite/example/cpp_example/CMakeLists.txt +++ b/lite/example/cpp_example/CMakeLists.txt @@ -1,5 +1,6 @@ file (GLOB_RECURSE SOURCES ./*.cpp) add_executable(lite_examples ${SOURCES}) +target_include_directories(lite_examples PUBLIC ./) if(LITE_BUILD_WITH_RKNPU) #rknn sdk1.0.0 depend on libc++_shared, use gold to remove NEEDED so symbol check @@ -33,6 +34,7 @@ if(LITE_BUILD_WITH_RKNPU) endif() target_link_libraries(lite_examples_depends_shared lite_shared) +target_include_directories(lite_examples_depends_shared PUBLIC ./) if(UNIX) if(APPLE OR ANDROID) diff --git a/lite/example/cpp_example/example.h b/lite/example/cpp_example/example.h index 0e5da8f5b386396b5f3c9d2174c3a79d94f3df63..8e07dcb105d89b73e2017513b1d4a41f4546cb02 100644 --- a/lite/example/cpp_example/example.h +++ b/lite/example/cpp_example/example.h @@ -49,57 +49,20 @@ ExampleFuncMap* get_example_function_map(); bool register_example(std::string example_name, const ExampleFunc& fuction); -template -struct Register; - -#if LITE_BUILD_WITH_MGE - -bool basic_load_from_path(const Args& args); -bool basic_load_from_path_with_loader(const Args& args); -bool basic_load_from_memory(const Args& args); -bool cpu_affinity(const Args& args); -bool network_share_same_weights(const Args& args); -bool reset_input(const Args& args); -bool reset_input_output(const Args& args); -bool config_user_allocator(const Args& args); -bool register_cryption_method(const Args& args); -bool update_cryption_key(const Args& args); -bool async_forward(const Args& args); -bool set_input_callback(const Args& arg); -bool set_output_callback(const Args& arg); - -bool picture_classification(const Args& arg); -bool detect_yolox(const Args& arg); - -#if LITE_WITH_CUDA -bool load_from_path_run_cuda(const Args& args); -bool device_input(const Args& args); -bool device_input_output(const Args& args); -bool pinned_host_input(const Args& args); -#endif -#endif - } // namespace example } // namespace lite -#if LITE_BUILD_WITH_MGE -bool basic_c_interface(const lite::example::Args& args); -bool device_io_c_interface(const lite::example::Args& args); -bool async_c_interface(const lite::example::Args& args); -#endif - #define CONCAT_IMPL(a, b) a##b #define MACRO_CONCAT(a, b) CONCAT_IMPL(a, b) #define REGIST_EXAMPLE(name_, func_) REGIST_EXAMPLE_WITH_NUM(__COUNTER__, name_, func_) -#define REGIST_EXAMPLE_WITH_NUM(number_, name_, func_) \ - template <> \ - struct Register { \ - Register() { register_example(name_, func_); } \ - }; \ - namespace { \ - Register MACRO_CONCAT(example_function_, number_); \ +#define REGIST_EXAMPLE_WITH_NUM(number_, name_, func_) \ + struct Register_##func_ { \ + Register_##func_() { lite::example::register_example(name_, func_); } \ + }; \ + namespace { \ + Register_##func_ MACRO_CONCAT(func_, number_); \ } // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/main.cpp b/lite/example/cpp_example/main.cpp index b479b63d2b6cf3ea6038524090bcdbb96c683343..ff566ccb8d5e239e49caa448cccb94c16f37bbf6 100644 --- a/lite/example/cpp_example/main.cpp +++ b/lite/example/cpp_example/main.cpp @@ -60,7 +60,8 @@ bool lite::example::register_example( std::string example_name, const ExampleFunc& fuction) { auto map = get_example_function_map(); if (map->find(example_name) != map->end()) { - printf("Error!!! This example is registed yet\n"); + printf("example_name: %s Error!!! This example is registed yet\n", + example_name.c_str()); return false; } (*map)[example_name] = fuction; @@ -142,41 +143,5 @@ int main(int argc, char** argv) { return -1; } } -namespace lite { -namespace example { - -#if LITE_BUILD_WITH_MGE -#if LITE_WITH_CUDA -REGIST_EXAMPLE("load_from_path_run_cuda", load_from_path_run_cuda); -#endif -REGIST_EXAMPLE("basic_load_from_path", basic_load_from_path); -REGIST_EXAMPLE("basic_load_from_path_with_loader", basic_load_from_path_with_loader); -REGIST_EXAMPLE("basic_load_from_memory", basic_load_from_memory); -REGIST_EXAMPLE("cpu_affinity", cpu_affinity); -REGIST_EXAMPLE("register_cryption_method", register_cryption_method); -REGIST_EXAMPLE("update_cryption_key", update_cryption_key); -REGIST_EXAMPLE("network_share_same_weights", network_share_same_weights); -REGIST_EXAMPLE("reset_input", reset_input); -REGIST_EXAMPLE("reset_input_output", reset_input_output); -REGIST_EXAMPLE("config_user_allocator", config_user_allocator); -REGIST_EXAMPLE("async_forward", async_forward); -REGIST_EXAMPLE("set_input_callback", set_input_callback); -REGIST_EXAMPLE("set_output_callback", set_output_callback); - -REGIST_EXAMPLE("basic_c_interface", basic_c_interface); -REGIST_EXAMPLE("device_io_c_interface", device_io_c_interface); -REGIST_EXAMPLE("async_c_interface", async_c_interface); - -REGIST_EXAMPLE("picture_classification", picture_classification); -REGIST_EXAMPLE("detect_yolox", detect_yolox); - -#if LITE_WITH_CUDA -REGIST_EXAMPLE("device_input", device_input); -REGIST_EXAMPLE("device_input_output", device_input_output); -REGIST_EXAMPLE("pinned_host_input", pinned_host_input); -#endif -#endif -} // namespace example -} // namespace lite // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/basic.cpp b/lite/example/cpp_example/mge/basic.cpp index 55e20270f9c0cf03b6519d71693025cf5b1c2686..069ddb93e6b9425e3bb8d6231c2416a1b39a1158 100644 --- a/lite/example/cpp_example/mge/basic.cpp +++ b/lite/example/cpp_example/mge/basic.cpp @@ -10,7 +10,7 @@ */ #include -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE #include @@ -77,61 +77,8 @@ void output_data_info(std::shared_ptr network, size_t output_size) { } } // namespace -#if LITE_WITH_CUDA -bool lite::example::load_from_path_run_cuda(const Args& args) { - std::string network_path = args.model_path; - std::string input_path = args.input_path; - set_log_level(LiteLogLevel::DEBUG); - //! config the network running in CUDA device - lite::Config config{false, -1, LiteDeviceType::LITE_CUDA}; - //! set NetworkIO - NetworkIO network_io; - std::string input_name = "img0_comp_fullface"; - bool is_host = false; - IO device_input{input_name, is_host}; - network_io.inputs.push_back(device_input); - //! create and load the network - std::shared_ptr network = std::make_shared(config, network_io); - network->load_model(network_path); - - std::shared_ptr input_tensor = network->get_input_tensor(0); - Layout input_layout = input_tensor->get_layout(); - - //! read data from numpy data file - auto src_tensor = parse_npy(input_path); - - //! malloc the device memory - auto tensor_device = Tensor(LiteDeviceType::LITE_CUDA, input_layout); - - //! copy to the device memory - tensor_device.copy_from(*src_tensor); - - //! Now the device memory if filled with user input data, set it to the - //! input tensor - input_tensor->reset(tensor_device.get_memory_ptr(), input_layout); - - //! forward - { - lite::Timer ltimer("warmup"); - network->forward(); - network->wait(); - ltimer.print_used_time(0); - } - lite::Timer ltimer("forward_iter"); - for (int i = 0; i < 10; i++) { - ltimer.reset_start(); - network->forward(); - network->wait(); - ltimer.print_used_time(i); - } - //! get the output data or read tensor set in network_in - size_t output_size = network->get_all_output_name().size(); - output_info(network, output_size); - output_data_info(network, output_size); - return true; -} -#endif -bool lite::example::basic_load_from_path(const Args& args) { +namespace { +bool basic_load_from_path(const Args& args) { set_log_level(LiteLogLevel::DEBUG); std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -193,7 +140,7 @@ bool lite::example::basic_load_from_path(const Args& args) { return true; } -bool lite::example::basic_load_from_path_with_loader(const Args& args) { +bool basic_load_from_path_with_loader(const Args& args) { set_log_level(LiteLogLevel::DEBUG); lite::set_loader_lib_path(args.loader_path); std::string network_path = args.model_path; @@ -251,7 +198,7 @@ bool lite::example::basic_load_from_path_with_loader(const Args& args) { return true; } -bool lite::example::basic_load_from_memory(const Args& args) { +bool basic_load_from_memory(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -307,7 +254,7 @@ bool lite::example::basic_load_from_memory(const Args& args) { return true; } -bool lite::example::async_forward(const Args& args) { +bool async_forward(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; Config config; @@ -366,7 +313,7 @@ bool lite::example::async_forward(const Args& args) { return true; } -bool lite::example::set_input_callback(const Args& args) { +bool set_input_callback(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; Config config; @@ -433,7 +380,7 @@ bool lite::example::set_input_callback(const Args& args) { return true; } -bool lite::example::set_output_callback(const Args& args) { +bool set_output_callback(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; Config config; @@ -500,7 +447,73 @@ bool lite::example::set_output_callback(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("basic_load_from_path", basic_load_from_path); +REGIST_EXAMPLE("basic_load_from_path_with_loader", basic_load_from_path_with_loader); +REGIST_EXAMPLE("basic_load_from_memory", basic_load_from_memory); +REGIST_EXAMPLE("async_forward", async_forward); +REGIST_EXAMPLE("set_input_callback", set_input_callback); +REGIST_EXAMPLE("set_output_callback", set_output_callback); + +#if LITE_WITH_CUDA +namespace { +bool load_from_path_run_cuda(const Args& args) { + std::string network_path = args.model_path; + std::string input_path = args.input_path; + set_log_level(LiteLogLevel::DEBUG); + //! config the network running in CUDA device + lite::Config config{false, -1, LiteDeviceType::LITE_CUDA}; + //! set NetworkIO + NetworkIO network_io; + std::string input_name = "img0_comp_fullface"; + bool is_host = false; + IO device_input{input_name, is_host}; + network_io.inputs.push_back(device_input); + //! create and load the network + std::shared_ptr network = std::make_shared(config, network_io); + network->load_model(network_path); + + std::shared_ptr input_tensor = network->get_input_tensor(0); + Layout input_layout = input_tensor->get_layout(); + + //! read data from numpy data file + auto src_tensor = parse_npy(input_path); + + //! malloc the device memory + auto tensor_device = Tensor(LiteDeviceType::LITE_CUDA, input_layout); + + //! copy to the device memory + tensor_device.copy_from(*src_tensor); + + //! Now the device memory if filled with user input data, set it to the + //! input tensor + input_tensor->reset(tensor_device.get_memory_ptr(), input_layout); + + //! forward + { + lite::Timer ltimer("warmup"); + network->forward(); + network->wait(); + ltimer.print_used_time(0); + } + lite::Timer ltimer("forward_iter"); + for (int i = 0; i < 10; i++) { + ltimer.reset_start(); + network->forward(); + network->wait(); + ltimer.print_used_time(i); + } + //! get the output data or read tensor set in network_in + size_t output_size = network->get_all_output_name().size(); + output_info(network, output_size); + output_data_info(network, output_size); + return true; +} +} // namespace +REGIST_EXAMPLE("load_from_path_run_cuda", load_from_path_run_cuda); +#endif #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/cpu_affinity.cpp b/lite/example/cpp_example/mge/cpu_affinity.cpp index ea936bf7a371a4ccb83b054b9a67e9c0502c228f..6d47c94929c879b3022f9727da2be070cb7ad86d 100644 --- a/lite/example/cpp_example/mge/cpu_affinity.cpp +++ b/lite/example/cpp_example/mge/cpu_affinity.cpp @@ -9,13 +9,14 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE using namespace lite; using namespace example; -bool lite::example::cpu_affinity(const Args& args) { +namespace { +bool cpu_affinity(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -65,6 +66,9 @@ bool lite::example::cpu_affinity(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("cpu_affinity", cpu_affinity); #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/cv/detect_yolox.cpp b/lite/example/cpp_example/mge/cv/detect_yolox.cpp index b42485c08e81a49708fd802a3bb18df36a96b46d..e5f7755331b36174d422df58f39da7b1f8b9598b 100644 --- a/lite/example/cpp_example/mge/cv/detect_yolox.cpp +++ b/lite/example/cpp_example/mge/cv/detect_yolox.cpp @@ -10,7 +10,7 @@ */ #include -#include "../../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE #include @@ -289,6 +289,10 @@ void decode_outputs( void draw_objects( uint8_t* image, int width, int height, int channel, const std::vector& objects) { + (void)image; + (void)width; + (void)height; + (void)channel; for (size_t i = 0; i < objects.size(); i++) { const Object& obj = objects[i]; @@ -297,9 +301,7 @@ void draw_objects( } } -} // namespace - -bool lite::example::detect_yolox(const Args& args) { +bool detect_yolox(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -332,6 +334,9 @@ bool lite::example::detect_yolox(const Args& args) { stbi_image_free(image); return 0; } +} // namespace + +REGIST_EXAMPLE("detect_yolox", detect_yolox); #endif diff --git a/lite/example/cpp_example/mge/cv/picture_classification.cpp b/lite/example/cpp_example/mge/cv/picture_classification.cpp index 84387e037dd350bb8348129173269aa5255b696b..970dc080f98edcf44e8af63da41a11db9c1cd401 100644 --- a/lite/example/cpp_example/mge/cv/picture_classification.cpp +++ b/lite/example/cpp_example/mge/cv/picture_classification.cpp @@ -10,7 +10,7 @@ */ #include -#include "../../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE #include @@ -80,9 +80,8 @@ void classfication_process( } printf("output tensor sum is %f\n", sum); } -} // namespace -bool lite::example::picture_classification(const Args& args) { +bool picture_classification(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -109,6 +108,9 @@ bool lite::example::picture_classification(const Args& args) { class_id, score); return 0; } +} // namespace + +REGIST_EXAMPLE("picture_classification", picture_classification); #endif diff --git a/lite/example/cpp_example/mge/device_io.cpp b/lite/example/cpp_example/mge/device_io.cpp index e7689ef5a194d08d991792e8cc642f0258cf8546..fd5626fb0a650a5f67f5d2a38e4888a4d13a3388 100644 --- a/lite/example/cpp_example/mge/device_io.cpp +++ b/lite/example/cpp_example/mge/device_io.cpp @@ -10,15 +10,17 @@ */ #include -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE +#include "misc.h" using namespace lite; using namespace example; #if LITE_WITH_CUDA -bool lite::example::device_input(const Args& args) { +namespace { +bool device_input(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -73,7 +75,7 @@ bool lite::example::device_input(const Args& args) { return true; } -bool lite::example::device_input_output(const Args& args) { +bool device_input_output(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -136,7 +138,7 @@ bool lite::example::device_input_output(const Args& args) { return true; } -bool lite::example::pinned_host_input(const Args& args) { +bool pinned_host_input(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -181,6 +183,11 @@ bool lite::example::pinned_host_input(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("device_input", device_input); +REGIST_EXAMPLE("device_input_output", device_input_output); +REGIST_EXAMPLE("pinned_host_input", pinned_host_input); #endif #endif diff --git a/lite/example/cpp_example/mge/lite_c_interface.cpp b/lite/example/cpp_example/mge/lite_c_interface.cpp index 0424631ce0f8cd127d9bba7ea6ee16d02378ba88..e27e8a59f14d3f09b16dc4568a5f21575eca69be 100644 --- a/lite/example/cpp_example/mge/lite_c_interface.cpp +++ b/lite/example/cpp_example/mge/lite_c_interface.cpp @@ -9,7 +9,7 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #include "misc.h" #if LITE_BUILD_WITH_MGE #include "lite-c/global_c.h" @@ -218,5 +218,10 @@ bool async_c_interface(const lite::example::Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } + +REGIST_EXAMPLE("basic_c_interface", basic_c_interface); +REGIST_EXAMPLE("device_io_c_interface", device_io_c_interface); +REGIST_EXAMPLE("async_c_interface", async_c_interface); + #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/network_share_weights.cpp b/lite/example/cpp_example/mge/network_share_weights.cpp index 0a117d583f0fa0bc5e1303d5753f83067ca0646b..e206d394c4a5a68bc79a7dc1962251952698814b 100644 --- a/lite/example/cpp_example/mge/network_share_weights.cpp +++ b/lite/example/cpp_example/mge/network_share_weights.cpp @@ -9,13 +9,15 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE using namespace lite; using namespace example; -bool lite::example::network_share_same_weights(const Args& args) { +namespace { + +bool network_share_same_weights(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -75,5 +77,9 @@ bool lite::example::network_share_same_weights(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("network_share_same_weights", network_share_same_weights); + #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/reset_io.cpp b/lite/example/cpp_example/mge/reset_io.cpp index cda27726a76d8b1ea52adbaab41e124525f96ee9..b6848195de0e01a7559d7368bc3526a924072312 100644 --- a/lite/example/cpp_example/mge/reset_io.cpp +++ b/lite/example/cpp_example/mge/reset_io.cpp @@ -9,13 +9,15 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE using namespace lite; using namespace example; -bool lite::example::reset_input(const Args& args) { +namespace { + +bool reset_input(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; lite::Config config; @@ -53,7 +55,7 @@ bool lite::example::reset_input(const Args& args) { return true; } -bool lite::example::reset_input_output(const Args& args) { +bool reset_input_output(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; lite::Config config; @@ -92,5 +94,10 @@ bool lite::example::reset_input_output(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("reset_input", reset_input); +REGIST_EXAMPLE("reset_input_output", reset_input_output); + #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/user_allocator.cpp b/lite/example/cpp_example/mge/user_allocator.cpp index 73b9b41df1756bf640d9cd1b94925009fa162aec..7f54743583fe492bbc6a29b171d6a4ce7c736caa 100644 --- a/lite/example/cpp_example/mge/user_allocator.cpp +++ b/lite/example/cpp_example/mge/user_allocator.cpp @@ -9,7 +9,7 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE using namespace lite; using namespace example; @@ -42,9 +42,8 @@ public: #endif }; }; -} // namespace -bool lite::example::config_user_allocator(const Args& args) { +bool config_user_allocator(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -87,5 +86,9 @@ bool lite::example::config_user_allocator(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("config_user_allocator", config_user_allocator); + #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/example/cpp_example/mge/user_cryption.cpp b/lite/example/cpp_example/mge/user_cryption.cpp index 4a8579375f3000e1a2b7ec9488502352f3819bc9..02c8a7aceed226d547e7bc186bfcce0005bd6774 100644 --- a/lite/example/cpp_example/mge/user_cryption.cpp +++ b/lite/example/cpp_example/mge/user_cryption.cpp @@ -9,7 +9,7 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include "../example.h" +#include "example.h" #if LITE_BUILD_WITH_MGE using namespace lite; @@ -31,9 +31,8 @@ std::vector decrypt_model( return {}; } } -} // namespace -bool lite::example::register_cryption_method(const Args& args) { +bool register_cryption_method(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -75,7 +74,7 @@ bool lite::example::register_cryption_method(const Args& args) { return true; } -bool lite::example::update_cryption_key(const Args& args) { +bool update_cryption_key(const Args& args) { std::string network_path = args.model_path; std::string input_path = args.input_path; @@ -120,5 +119,9 @@ bool lite::example::update_cryption_key(const Args& args) { printf("max=%e, sum=%e\n", max, sum); return true; } +} // namespace + +REGIST_EXAMPLE("register_cryption_method", register_cryption_method); +REGIST_EXAMPLE("update_cryption_key", update_cryption_key); #endif // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}