From 5cfbc30bbfb80a8df3a2df5e197af78a517abb75 Mon Sep 17 00:00:00 2001 From: Qi <1825013335@qq.com> Date: Tue, 10 Sep 2024 16:15:11 +0800 Subject: [PATCH 1/3] cpp test --- tools/pnnx/src/save_ncnn.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/pnnx/src/save_ncnn.cpp b/tools/pnnx/src/save_ncnn.cpp index bbe778ddc42b..c1d55dcf57df 100644 --- a/tools/pnnx/src/save_ncnn.cpp +++ b/tools/pnnx/src/save_ncnn.cpp @@ -463,6 +463,5 @@ int save_ncnn(const Graph& g, const std::string& parampath, const std::string& b fclose(pyfp); return 0; -} } // namespace pnnx From 4b3d8c769061f5e6762e07ab1f7ce7fac208bab3 Mon Sep 17 00:00:00 2001 From: Qi <1825013335@qq.com> Date: Tue, 10 Sep 2024 16:15:51 +0800 Subject: [PATCH 2/3] cpp test and CMakeList change --- tools/pnnx/src/save_ncnn.cpp | 79 ++++++++++++++++++++++++++++ tools/pnnx/tests/ncnn/CMakeLists.txt | 1 + 2 files changed, 80 insertions(+) diff --git a/tools/pnnx/src/save_ncnn.cpp b/tools/pnnx/src/save_ncnn.cpp index c1d55dcf57df..ed18dc08168c 100644 --- a/tools/pnnx/src/save_ncnn.cpp +++ b/tools/pnnx/src/save_ncnn.cpp @@ -462,6 +462,85 @@ int save_ncnn(const Graph& g, const std::string& parampath, const std::string& b fclose(pyfp); + // Generate C++ test + std::string cpppath = pypath.substr(0, pypath.find_last_of('.')) + ".cpp"; + FILE* cppfp = fopen(cpppath.c_str(), "wb"); + if (!cppfp) + { + fprint(stderr, "fopen %s failed\n", cpppath.c_str()); + return -1; + } + + fprintf(cppfp, "#include \n"); + fprintf(cppfp, "#include \n"); + fprintf(cppfp, "#include \n"); + fprintf(cppfp, "#include \"ncnn.hpp\"\n\n"); + + fprintf(cppfp, "void test_inference(const std::string& parampath, const std::string& binpath)\n"); + fprintf(cppfp, "{\n"); + fprintf(cppfp, " ncnn:::Net net;\n"); + fprintf(cppfp, " net.load_param(parampath.c_str());\n"); + fprintf(cppfp, " ner.load_model(binpath.c_str());\n\n"); + + for (int input_index = 0;; input_index++) + { + std::string input_name = std::string("in") + std::to_string(input_index); + const Operand* r = g.get_operand(input_name); + if (!r) + break; + + fprintf(cppfp, " std::vector %s(", input_name.c_str()); + for (size_t i = 0; i < r->shape.size(); i++) + { + fprintf(cppfp, "%d", r->shape[i]); + if (i + 1 != r->shape.size()) + fprintf(cppfp, " * "); + } + fprint(cppfp, ");\n"); + + if (type_is_integer(r->type)) + { + fprintf(cppfp, " std::default_random_engine engine;\n"); + fprintf(cppfp, " std::uniform_int_distribution dist(0, 9);\n"); + fprintf(cppfp, " for (float& v : %s) {{ v = dist(engine); }}\n", input_name.c_str()); + + } + else + { + fprintf(cppfp, " std::default_random_engine engine;\n"); + fprintf(cppfp, " std::uniform_real_distribution dist(0.0, 1.0);\n"); + fprintf(cppfp, " for (float& v : %s) {{ v = dist(engine); }}\n", input_name.c_str()); + } + + fprintf(cppfp, " ncnn::Mat %s_mat = ncnn::Mat::from_pixels(%s.data(), ncnn::Mat::PixelType::PIXEL_GRAY, %d, %d);\n", + input_name.c_str(), input_name.c_str(), r->shape[2], r->shape[1]); + // Assuming shape order: batch, height, width + fprintf(cppfp, " net.input(\"net_input\", %s_mat);\n", input_name.c_str()); + + } + fprintf(cppfp, " std::vector output;\n"); + fprintf(cppfp, " net.extract(\"output\", output);\n\n"); + + fprintf(cppfp, " for (const auto& m : output) {\n"); + fprintf(cppfp, " const float* data = m.channel_data(0);\n"); + fprintf(cppfp, " for (int i = 0; i < m.w; i++) {\n"); + fprintf(cppfp, " std::cout << data[i] << \" \";\n"); + fprintf(cppfp, " }\n"); + fprintf(cppfp, " std::cout << std::endl;\n"); + fprintf(cppfp, " }\n"); + + fprintf(cppfp, "}\n\n"); + + fprintf(cppfp, "int main()\n"); + fprintf(cppfp, "{\n"); + fprintf(cppfp, " std::string parampath = \"%s\";\n", parampath.c_str()); + fprintf(cppfp, " std::string binpath = \"%s\";\n", binpath.c_str()); + fprintf(cppfp, " test_inference(parampath, binpath);\n"); + fprintf(cppfp, " return 0;\n"); + fprintf(cppfp, "}\n"); + + fclose(cppfp); + return 0; } // namespace pnnx diff --git a/tools/pnnx/tests/ncnn/CMakeLists.txt b/tools/pnnx/tests/ncnn/CMakeLists.txt index 49cb063f335e..f871b5e20567 100644 --- a/tools/pnnx/tests/ncnn/CMakeLists.txt +++ b/tools/pnnx/tests/ncnn/CMakeLists.txt @@ -3,6 +3,7 @@ find_package(Python3 REQUIRED COMPONENTS Interpreter) macro(pnnx_ncnn_add_test name) add_test(NAME test_ncnn_${name} COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${Python3_EXECUTABLE} -DPYTHON_SCRIPT=${CMAKE_CURRENT_SOURCE_DIR}/test_${name}.py -P ${CMAKE_CURRENT_SOURCE_DIR}/../run_test.cmake) + add_test(NAME test_cpp_${name} COMMAND ${CMAKE_COMMAND} -DCPP_EXECUTABLE=${Cpp_EXECUTABLE} -DCPP_SCRIPT=${CMAKE_CURRENT_SOURCE_DIR}/test_${name}.cpp -P ${CMAKE_CURRENT_SOURCE_DIR}/../run_test.cmake) endmacro() pnnx_ncnn_add_test(F_adaptive_avg_pool1d) From 8c939dce5a8bd8d3b312024316f7aeb3c01b7802 Mon Sep 17 00:00:00 2001 From: Qi <1825013335@qq.com> Date: Sun, 6 Oct 2024 11:21:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/simpleomp.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/simpleomp.cpp b/src/simpleomp.cpp index 6de069a9b6f6..a52001eba909 100644 --- a/src/simpleomp.cpp +++ b/src/simpleomp.cpp @@ -25,6 +25,13 @@ #include #include +#if defined(_MSC_VER) +void omp_parallel_for(int start, int end, std::function task, int numThreads) +{ + parallelFor(start, end, task, numThreads); +} +#endif + #if __clang__ extern "C" typedef void (*kmpc_micro)(int32_t* gtid, int32_t* tid, ...); extern "C" typedef void (*kmpc_micro_0)(int32_t* gtid, int32_t* tid); @@ -800,4 +807,26 @@ void GOMP_parallel(void (*fn)(void*), void* data, unsigned num_threads, unsigned } // extern "C" #endif -#endif // NCNN_SIMPLEOMP +void parallelFor(int start, int end, std::function task, int numThreads) +{ + int step = (end - start) / numThreads; + std::vector threads; + for (int i = 0; i < numThreads; ++i) + { + int threadStart = start + i * step; + int threadEnd = (i == numThreads - 1) ? end : threadStart + step; + threads.push_back(std::thread([threadStart, threadEnd, &task]() { + for (int j = threadStart; j < threadEnd; ++j) + { + task(j); + } + })) + } + for (auto& th : threads) + { + if (th.joinable()) + { + th.join(); + } + } +}