├── .clang-format ├── .github └── workflows │ ├── cmake-windows.yml │ └── cmake.yml ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE.md ├── README.md ├── bench.txt ├── bench ├── CMakeLists.txt ├── bench_conv.cpp ├── bench_layer.cpp ├── bench_main.cpp ├── bench_mobilenet.cpp ├── bench_reshape.cpp ├── bench_resnet.cpp ├── bench_rmsnorm.cpp ├── bench_simd.cpp ├── bench_unet.cpp └── bench_yolo.cpp ├── demos ├── CMakeLists.txt ├── image_util.cpp ├── image_util.hpp ├── mobilenet │ ├── CMakeLists.txt │ └── mobile.cpp ├── resnet │ ├── CMakeLists.txt │ └── resnet_test.cpp ├── unet │ ├── CMakeLists.txt │ └── unet_test.cpp └── yolo │ ├── CMakeLists.txt │ └── yolo_test.cpp ├── dockerfile ├── imgs ├── 31.jpg ├── KuiperInfer.png ├── a.gif ├── bus.jpg ├── car.jpg ├── demo_car.jpg ├── demo_car_.jpg ├── do.gif ├── logo.jpg ├── me.jpg ├── me.png ├── qa.jpg └── zidane.jpg ├── include ├── data │ ├── load_data.hpp │ ├── tensor.hpp │ └── tensor_util.hpp ├── layer │ └── abstract │ │ ├── layer.hpp │ │ ├── layer_factory.hpp │ │ ├── non_param_layer.hpp │ │ └── param_layer.hpp ├── parser │ └── parse_expression.hpp ├── runtime │ ├── pnnx │ │ ├── ir.h │ │ └── store_zip.hpp │ ├── runtime_attr.hpp │ ├── runtime_datatype.hpp │ ├── runtime_ir.hpp │ ├── runtime_op.hpp │ ├── runtime_operand.hpp │ └── runtime_parameter.hpp ├── status_code.hpp ├── tick.hpp └── utils │ ├── math │ └── fmath.hpp │ └── time │ └── time_logging.hpp ├── slides └── readme.md ├── source ├── data │ ├── load_data.cpp │ ├── tensor.cpp │ └── tensor_utils.cpp ├── layer │ ├── abstract │ │ ├── layer.cpp │ │ ├── layer_factory.cpp │ │ └── param_layer.cpp │ └── details │ │ ├── activation.cpp │ │ ├── activation.hpp │ │ ├── adaptive_avgpooling.cpp │ │ ├── adaptive_avgpooling.hpp │ │ ├── backup │ │ ├── winograd.cpp │ │ └── winograd.hpp │ │ ├── base_convolution.cpp │ │ ├── base_convolution.hpp │ │ ├── batchnorm2d.cpp │ │ ├── batchnorm2d.hpp │ │ ├── cat.cpp │ │ ├── cat.hpp │ │ ├── convolution.cpp │ │ ├── convolution.hpp │ │ ├── deconvolution.cpp │ │ ├── deconvolution.hpp │ │ ├── expression.cpp │ │ ├── expression.hpp │ │ ├── flatten.cpp │ │ ├── flatten.hpp │ │ ├── hardsigmoid.cpp │ │ ├── hardsigmoid.hpp │ │ ├── hardswish.cpp │ │ ├── hardswish.hpp │ │ ├── linear.cpp │ │ ├── linear.hpp │ │ ├── matmul.cpp │ │ ├── matmul.hpp │ │ ├── maxpooling.cpp │ │ ├── maxpooling.hpp │ │ ├── relu.cpp │ │ ├── relu.hpp │ │ ├── relu6.cpp │ │ ├── relu6.hpp │ │ ├── rms_norm.cpp │ │ ├── rms_norm.hpp │ │ ├── sigmoid.cpp │ │ ├── sigmoid.hpp │ │ ├── silu.cpp │ │ ├── silu.hpp │ │ ├── simd.cpp │ │ ├── simd.hpp │ │ ├── softmax.cpp │ │ ├── softmax.hpp │ │ ├── upsample.cpp │ │ ├── upsample.hpp │ │ ├── view.cpp │ │ ├── view.hpp │ │ ├── yolo_detect.cpp │ │ └── yolo_detect.hpp ├── parser │ └── parse_expression.cpp ├── readme.txt ├── runtime │ ├── pnnx │ │ ├── ir.cpp │ │ └── store_zip.cpp │ ├── runtime_ir.cpp │ └── runtime_op.cpp └── utils │ └── time │ └── time_logging.cpp ├── test ├── CMakeLists.txt ├── test_data │ ├── test_load_data.cpp │ ├── test_tensor.cpp │ └── test_utensor.cpp ├── test_layer │ ├── test_average_pooling.cpp │ ├── test_batchnorm.cpp │ ├── test_cat.cpp │ ├── test_conv.cpp │ ├── test_deconv.cpp │ ├── test_expression.cpp │ ├── test_flatten.cpp │ ├── test_hardsigmoid.cpp │ ├── test_hardswish.cpp │ ├── test_layer_factory.cpp │ ├── test_linear.cpp │ ├── test_matmul.cpp │ ├── test_maxpooling.cpp │ ├── test_param_layer.cpp │ ├── test_relu.cpp │ ├── test_reshape.cpp │ ├── test_sigmoid.cpp │ ├── test_silu.cpp │ ├── test_softmax.cpp │ ├── test_upsample.cpp │ └── test_view.cpp ├── test_main.cpp ├── test_net │ ├── test_classify_net.cpp │ └── test_yolo.cpp └── test_runtime │ ├── test_attr.cpp │ ├── test_param.cpp │ └── test_runtime_ir.cpp └── vcpkg.json /.clang-format: -------------------------------------------------------------------------------- 1 | # Use the Google style in this project. 2 | BasedOnStyle: Google 3 | 4 | # Some folks prefer to write "int& foo" while others prefer "int &foo". The 5 | # Google Style Guide only asks for consistency within a project, we chose 6 | # "int& foo" for this project: 7 | DerivePointerAlignment: false 8 | PointerAlignment: Left 9 | 10 | # The Google Style Guide only asks for consistency w.r.t. "east const" vs. 11 | # "const west" alignment of cv-qualifiers. In this project we use "east const". 12 | QualifierAlignment: Left 13 | ColumnLimit: 100 14 | IncludeBlocks: Merge 15 | # Format raw string literals with a `pb` or `proto` tag as proto. 16 | RawStringFormats: 17 | - Language: TextProto 18 | Delimiters: 19 | - 'pb' 20 | - 'proto' 21 | BasedOnStyle: Google 22 | 23 | CommentPragmas: '(@copydoc|@copybrief|@see)' -------------------------------------------------------------------------------- /.github/workflows/cmake-windows.yml: -------------------------------------------------------------------------------- 1 | name: CMake-Windows 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | concurrency: 10 | group: windows-${{ github.ref }} 11 | cancel-in-progress: true 12 | 13 | env: 14 | BUILD_TYPE: Release 15 | 16 | 17 | jobs: 18 | build: 19 | runs-on: windows-latest 20 | 21 | steps: 22 | - run: echo "The job was automatically triggered by a ${{ github.event_name }} event." 23 | - run: echo "This job is now running on a ${{ runner.os }} server hosted by GitHub!" 24 | - run: echo "The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." 25 | - run: echo "The ${{ github.repository }} repository has been cloned to the runner." 26 | 27 | - uses: actions/checkout@v3 28 | with: 29 | submodules: 'true' 30 | 31 | - name: Setup devcmd 32 | uses: ilammy/msvc-dev-cmd@v1 33 | 34 | - name: Install build tools 35 | run: | 36 | choco install ninja 37 | 38 | - uses: lukka/get-cmake@latest 39 | 40 | - name: Make vcpkg .git directory writable 41 | run: | 42 | # vcpkg will fail to update if the .git directory is not writable 43 | attrib -r "c:\vcpkg\.git\*.*" /s 44 | 45 | - name: Restore artifacts, or setup vcpkg for building artifacts 46 | uses: lukka/run-vcpkg@v10 47 | id: runvcpkg 48 | with: 49 | vcpkgDirectory: C:/vcpkg 50 | doNotUpdateVcpkg: true 51 | runVcpkgInstall: true 52 | 53 | - name: Configure CMake 54 | run: echo ${{github.workspace}} ` 55 | && cmake -B ${{github.workspace}}/build ` 56 | -Thost=x64 ` 57 | -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ` 58 | -DCMAKE_TOOLCHAIN_FILE="${{ env.RUNVCPKG_VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake" ` 59 | -DDEVELOPMENT=ON 60 | 61 | - name: Build 62 | run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -- 63 | 64 | 65 | - name: Test 66 | working-directory: ${{github.workspace}} 67 | run: ${{github.workspace}}/build/test/${{ env.BUILD_TYPE }}/test_kuiper.exe 68 | 69 | -------------------------------------------------------------------------------- /.github/workflows/cmake.yml: -------------------------------------------------------------------------------- 1 | name: CMake 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | concurrency: 10 | group: linux-${{ github.ref }} 11 | cancel-in-progress: true 12 | 13 | env: 14 | BUILD_TYPE: Release 15 | 16 | 17 | jobs: 18 | build: 19 | runs-on: ubuntu-latest 20 | container: 21 | image: hellofss/kuiperinfer:latest 22 | 23 | steps: 24 | - run: echo "The job was automatically triggered by a ${{ github.event_name }} event." 25 | - run: echo "This job is now running on a ${{ runner.os }} server hosted by GitHub!" 26 | - run: echo "The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." 27 | - run: echo "The ${{ github.repository }} repository has been cloned to the runner." 28 | 29 | - uses: actions/checkout@v3 30 | with: 31 | submodules: 'true' 32 | 33 | - name: Configure CMake 34 | run: echo ${{github.workspace}} && cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DDEVELOPMENT=ON 35 | 36 | - name: Build 37 | run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -- -j 3 38 | 39 | 40 | - name: Test 41 | working-directory: ${{github.workspace}} 42 | run: ${{github.workspace}}/build/test/test_kuiper 43 | 44 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tmp"] 2 | path = tmp 3 | url = https://github.com/zjhellofss/model_zoo.git 4 | 5 | [submodule "demos/kuiper_llama"] 6 | path = demos/kuiper_llama 7 | url = https://github.com/zjhellofss/KuiperLLama.git 8 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(kuiper_infer) 3 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 4 | set(CMAKE_CXX_STANDARD 17) 5 | include_directories(./include) 6 | set(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) 7 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 8 | if (MSVC) 9 | set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) 10 | # 假如编译x64,那么SSE2永远可用 11 | # 假如编译x86,那么SSE2可用的话,就开启SSE2 12 | if (CMAKE_SIZEOF_VOID_P EQUAL 4) 13 | # check if SSE2 is available 14 | include(CheckCXXCompilerFlag) 15 | check_cxx_compiler_flag("/arch:SSE2" HAS_SSE2) 16 | else () 17 | set(HAS_SSE2 ON) 18 | endif () 19 | if (HAS_SSE2) 20 | # set __SSE2__ and __XOP__ macros 21 | add_definitions(-D__SSE2__ -D__XOP__) 22 | endif () 23 | # Force LLVM OpenMP on MSVC 24 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /openmp:llvm /openmp:experimental /arch:AVX2") 25 | endif () 26 | option(BUILD_DEMO "BUILD THE DEMO PROJECT") 27 | 28 | if (BUILD_DEMO) 29 | MESSAGE(STATUS "BUILD DEMO PROJECT") 30 | add_subdirectory(demos) 31 | endif () 32 | 33 | find_package(benchmark REQUIRED) 34 | find_package(OpenMP REQUIRED) 35 | find_package(Armadillo REQUIRED) 36 | find_package(glog REQUIRED) 37 | find_package(BLAS REQUIRED) 38 | find_package(LAPACK REQUIRED) 39 | 40 | aux_source_directory(./source/data DIR_DATA) 41 | aux_source_directory(./source/runtime DIR_RUNTIME) 42 | aux_source_directory(./source/runtime/pnnx DIR_RUNTIME) 43 | 44 | aux_source_directory(./source/layer/abstract DIR_ABSTRACT_LAYER) 45 | aux_source_directory(./source/layer/details DIR_BINOCULAR_LAYER) 46 | aux_source_directory(./source/parser DIR_PARSER) 47 | aux_source_directory(./source/utils/time DIR_UTILS) 48 | aux_source_directory(./source/utils/math DIR_MATH) 49 | 50 | 51 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") 52 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) 53 | set(link_lib glog::glog) 54 | IF (!WIN32) 55 | set(link_lib ${link_lib} pthread) 56 | ENDIF () 57 | 58 | set(link_math_lib ${ARMADILLO_LIBRARIES} ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES}) 59 | 60 | add_library(kuiper SHARED ${DIR_DATA} ${DIR_PARSER} ${DIR_MATH} ${DIR_UTILS} ${DIR_RUNTIME} ${DIR_ABSTRACT_LAYER} ${DIR_BINOCULAR_LAYER} ${DIR_PARSER} ) 61 | target_link_libraries(kuiper ${link_lib} ${link_math_lib} OpenMP::OpenMP_CXX) 62 | 63 | target_include_directories(kuiper PUBLIC ${benchmark_INCLUDE_DIRS}) 64 | target_include_directories(kuiper PUBLIC ${glog_INCLUDE_DIR}) 65 | target_include_directories(kuiper PUBLIC ${GTest_INCLUDE_DIR}) 66 | target_include_directories(kuiper PUBLIC ${Armadillo_INCLUDE_DIR}) 67 | 68 | # mathfun library defines 69 | add_compile_definitions(SSE_MATHFUN_WITH_CODE USE_SSE_AUTO) 70 | # 本项目的开发者请使用set(DEVELOPMENT ON)或者在cmake中添加-DDEVELOPMENT=ON将选项打开 71 | option(DEVELOPMENT ON) 72 | if (${DEVELOPMENT}) 73 | message(STATUS "DEVELOPMENT MODE ON") 74 | enable_testing() 75 | add_subdirectory(bench) 76 | add_subdirectory(test) 77 | 78 | endif () 79 | 80 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 - 傅莘莘 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bench/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package(benchmark REQUIRED) 2 | aux_source_directory(../bench DIR_BENCH) 3 | 4 | set(link_lib benchmark::benchmark benchmark::benchmark_main) 5 | 6 | add_executable(bench_kuiper ${DIR_BENCH} ) 7 | target_link_directories(bench_kuiper PUBLIC ${PROJECT_SOURCE_DIR}/lib) 8 | if(MSVC) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") 10 | else() 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fopenmp -march=native") 12 | endif() 13 | 14 | target_link_directories(bench_kuiper PUBLIC ${PROJECT_SOURCE_DIR}/lib) 15 | target_link_libraries(bench_kuiper ${link_lib} OpenMP::OpenMP_CXX) 16 | target_link_libraries(bench_kuiper kuiper) 17 | 18 | if (MSVC) 19 | # find kuiper dll 20 | add_custom_command(TARGET bench_kuiper POST_BUILD 21 | COMMAND ${CMAKE_COMMAND} -E copy_if_different 22 | "$/kuiper.dll" 23 | $) 24 | endif() 25 | 26 | target_include_directories(bench_kuiper PUBLIC ${benchmark_INCLUDE_DIRS}) 27 | target_include_directories(bench_kuiper PUBLIC ${glog_INCLUDE_DIR}) 28 | target_include_directories(bench_kuiper PUBLIC ${GTest_INCLUDE_DIR}) 29 | target_include_directories(bench_kuiper PUBLIC ${Armadillo_INCLUDE_DIR}) 30 | -------------------------------------------------------------------------------- /bench/bench_main.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-2-2. 23 | 24 | #include 25 | BENCHMARK_MAIN(); -------------------------------------------------------------------------------- /bench/bench_mobilenet.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-22. 23 | #include 24 | #include "runtime/runtime_ir.hpp" 25 | 26 | static void BM_MobilenetV3_Batch8_224x224(benchmark::State& state) { 27 | using namespace kuiper_infer; 28 | RuntimeGraph graph("tmp/mobilenet/mobile_batch8.pnnx.param", "tmp/mobilenet/mobile_batch8.bin"); 29 | 30 | graph.Build(); 31 | std::vector>> inputs; 32 | 33 | const uint32_t batch_size = 8; 34 | for (int i = 0; i < batch_size; ++i) { 35 | std::shared_ptr> input1 = std::make_shared>(3, 224, 224); 36 | input1->Fill(1.); 37 | inputs.push_back(input1); 38 | } 39 | graph.set_inputs("pnnx_input_0", inputs); 40 | for (auto _ : state) { 41 | graph.Forward(false); 42 | } 43 | } 44 | 45 | BENCHMARK(BM_MobilenetV3_Batch8_224x224)->Unit(benchmark::kMillisecond); 46 | -------------------------------------------------------------------------------- /bench/bench_reshape.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 2023/3/20. 23 | #include 24 | #include "data/tensor.hpp" 25 | #include "data/tensor_util.hpp" 26 | 27 | static void BM_ReshapeRowMajor(benchmark::State& state) { 28 | using namespace kuiper_infer; 29 | std::vector tensors; 30 | const uint32_t batch_size = 8; 31 | for (uint32_t i = 0; i < batch_size; ++i) { 32 | tensors.push_back(TensorCreate({32, 320, 320})); 33 | } 34 | for (auto _ : state) { 35 | for (uint32_t i = 0; i < batch_size; ++i) { 36 | auto tensor = tensors.at(i); 37 | tensor->Reshape({320, 320, 32}, true); 38 | } 39 | } 40 | } 41 | 42 | static void BM_ReshapeColMajor(benchmark::State& state) { 43 | using namespace kuiper_infer; 44 | std::vector tensors; 45 | const uint32_t batch_size = 8; 46 | for (uint32_t i = 0; i < batch_size; ++i) { 47 | tensors.push_back(TensorCreate({32, 320, 320})); 48 | } 49 | for (auto _ : state) { 50 | for (uint32_t i = 0; i < batch_size; ++i) { 51 | auto tensor = tensors.at(i); 52 | tensor->Reshape({320, 320, 32}, false); 53 | } 54 | } 55 | } 56 | 57 | static void BM_FillRowMajor(benchmark::State& state) { 58 | using namespace kuiper_infer; 59 | sftensor tensor = TensorCreate({32, 320, 320}); 60 | std::vector values(32 * 320 * 320, 1.f); 61 | for (auto _ : state) { 62 | tensor->Fill(values, true); 63 | } 64 | } 65 | 66 | BENCHMARK(BM_FillRowMajor)->Unit(benchmark::kMillisecond); 67 | BENCHMARK(BM_ReshapeRowMajor)->Unit(benchmark::kMillisecond); 68 | BENCHMARK(BM_ReshapeColMajor)->Unit(benchmark::kMillisecond); 69 | -------------------------------------------------------------------------------- /bench/bench_resnet.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-2-2. 23 | #include 24 | #include "runtime/runtime_ir.hpp" 25 | 26 | const static int kIterationNum = 5; 27 | 28 | static void BM_Resnet18_Batch8_224x224(benchmark::State& state) { 29 | using namespace kuiper_infer; 30 | RuntimeGraph graph("tmp/resnet/resnet18_batch8.pnnx.param", 31 | "tmp/resnet/resnet18_batch8.pnnx.bin"); 32 | 33 | graph.Build(); 34 | std::vector>> inputs; 35 | 36 | const uint32_t batch_size = 8; 37 | for (int i = 0; i < batch_size; ++i) { 38 | std::shared_ptr> input1 = std::make_shared>(3, 224, 224); 39 | input1->Fill(1.); 40 | inputs.push_back(input1); 41 | } 42 | graph.set_inputs("pnnx_input_0", inputs); 43 | for (auto _ : state) { 44 | graph.Forward(false); 45 | } 46 | } 47 | 48 | static void BM_Resnet18_Batch16_224x224(benchmark::State& state) { 49 | using namespace kuiper_infer; 50 | RuntimeGraph graph("tmp/resnet/resnet18_batch16.pnnx.param", 51 | "tmp/resnet/resnet18_batch16.pnnx.bin"); 52 | 53 | graph.Build(); 54 | std::vector>> inputs; 55 | const uint32_t batch_size = 16; 56 | for (int i = 0; i < batch_size; ++i) { 57 | std::shared_ptr> input1 = std::make_shared>(3, 224, 224); 58 | input1->Fill(1.); 59 | inputs.push_back(input1); 60 | } 61 | graph.set_inputs("pnnx_input_0", inputs); 62 | for (auto _ : state) { 63 | graph.Forward(false); 64 | } 65 | } 66 | 67 | BENCHMARK(BM_Resnet18_Batch8_224x224)->Unit(benchmark::kMillisecond); 68 | BENCHMARK(BM_Resnet18_Batch16_224x224)->Unit(benchmark::kMillisecond); -------------------------------------------------------------------------------- /bench/bench_rmsnorm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // 6 | // Created by fss on 24-2-15. 7 | // 8 | #include "../source/layer/details/rms_norm.hpp" 9 | #include "data/tensor.hpp" 10 | 11 | static void BM_RMSNorm(benchmark::State& state) { 12 | using namespace kuiper_infer; 13 | int input_size = state.range(0); 14 | 15 | std::vector>> input_tensors; 16 | std::vector>> output_tensors; 17 | 18 | std::shared_ptr> input_tensor = std::make_shared>(input_size); 19 | input_tensor->RandN(); 20 | input_tensors.push_back(input_tensor); 21 | 22 | std::vector weight_tensors; 23 | weight_tensors.push_back(input_tensor); 24 | 25 | std::vector>> outputs(input_size); 26 | 27 | std::shared_ptr> output_tensor = std::make_shared>(input_size); 28 | output_tensors.push_back(output_tensor); 29 | 30 | RMSNormLayer rms; 31 | rms.set_weights(weight_tensors); 32 | for (auto _ : state) { 33 | rms.Forward(input_tensors, output_tensors); 34 | } 35 | } 36 | 37 | BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(128)->Iterations(3); 38 | BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(512)->Iterations(3); 39 | BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(1024)->Iterations(3); 40 | BENCHMARK(BM_RMSNorm)->Unit(benchmark::kMillisecond)->Arg(4096)->Iterations(3); 41 | -------------------------------------------------------------------------------- /bench/bench_simd.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by fss on 23-6-14. 3 | // 4 | 5 | #include 6 | #include 7 | #include "../source/layer/details/simd.hpp" 8 | static void BM_SigmoidSimd(benchmark::State& state) { 9 | using namespace kuiper_infer; 10 | uint32_t input_c = state.range(0); 11 | uint32_t input_h = state.range(1); 12 | uint32_t input_w = state.range(2); 13 | sftensor input = std::make_shared(input_c, input_h, input_w); 14 | 15 | using namespace kuiper_infer::activation; 16 | for (auto _ : state) { 17 | ApplySSEActivation(ActivationType::kActivationSigmoid)(input, input); 18 | } 19 | } 20 | 21 | BENCHMARK(BM_SigmoidSimd)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 22 | BENCHMARK(BM_SigmoidSimd)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 23 | BENCHMARK(BM_SigmoidSimd)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); 24 | 25 | static void BM_SigmoidArma(benchmark::State& state) { 26 | using namespace kuiper_infer; 27 | uint32_t input_c = state.range(0); 28 | uint32_t input_h = state.range(1); 29 | uint32_t input_w = state.range(2); 30 | sftensor input = std::make_shared(input_c, input_h, input_w); 31 | input->RandN(); 32 | for (auto _ : state) { 33 | arma::fcube input_data = input->data(); 34 | input_data = 1.f / (1.f + arma::exp(-input_data)); 35 | } 36 | } 37 | 38 | BENCHMARK(BM_SigmoidArma)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 39 | BENCHMARK(BM_SigmoidArma)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 40 | BENCHMARK(BM_SigmoidArma)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); 41 | 42 | static void BM_Relu(benchmark::State& state) { 43 | using namespace kuiper_infer; 44 | uint32_t input_c = state.range(0); 45 | uint32_t input_h = state.range(1); 46 | uint32_t input_w = state.range(2); 47 | sftensor input = std::make_shared(input_c, input_h, input_w); 48 | input->RandN(); 49 | using namespace kuiper_infer::activation; 50 | for (auto _ : state) { 51 | for (uint32_t j = 0; j < input->size(); ++j) { 52 | float value = input->index(j); 53 | input->index(j) = value > 0.f ? value : 0.f; 54 | } 55 | } 56 | } 57 | 58 | BENCHMARK(BM_Relu)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 59 | BENCHMARK(BM_Relu)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 60 | BENCHMARK(BM_Relu)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); 61 | 62 | static void BM_ReluSimd(benchmark::State& state) { 63 | using namespace kuiper_infer; 64 | uint32_t input_c = state.range(0); 65 | uint32_t input_h = state.range(1); 66 | uint32_t input_w = state.range(2); 67 | sftensor input = std::make_shared(input_c, input_h, input_w); 68 | input->RandN(); 69 | using namespace kuiper_infer::activation; 70 | for (auto _ : state) { 71 | ApplySSEActivation(ActivationType::kActivationRelu)(input, input); 72 | } 73 | } 74 | 75 | BENCHMARK(BM_ReluSimd)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 76 | BENCHMARK(BM_ReluSimd)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 77 | BENCHMARK(BM_ReluSimd)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); 78 | 79 | static void BM_SiluArma(benchmark::State& state) { 80 | using namespace kuiper_infer; 81 | uint32_t input_c = state.range(0); 82 | uint32_t input_h = state.range(1); 83 | uint32_t input_w = state.range(2); 84 | sftensor input = std::make_shared(input_c, input_h, input_w); 85 | input->RandN(); 86 | using namespace kuiper_infer::activation; 87 | for (auto _ : state) { 88 | arma::fcube input_data = input->data(); 89 | input_data = input_data / (1.f + arma::exp(-input_data)); 90 | } 91 | } 92 | 93 | BENCHMARK(BM_SiluArma)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 94 | BENCHMARK(BM_SiluArma)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 95 | BENCHMARK(BM_SiluArma)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); 96 | 97 | static void BM_SiluSimd(benchmark::State& state) { 98 | using namespace kuiper_infer; 99 | uint32_t input_c = state.range(0); 100 | uint32_t input_h = state.range(1); 101 | uint32_t input_w = state.range(2); 102 | sftensor input = std::make_shared(input_c, input_h, input_w); 103 | input->RandN(); 104 | using namespace kuiper_infer::activation; 105 | for (auto _ : state) { 106 | ApplySSEActivation(ActivationType::kActivationSilu)(input, input); 107 | } 108 | } 109 | 110 | BENCHMARK(BM_SiluSimd)->Args({255, 80, 80})->Unit(benchmark::kMillisecond); 111 | BENCHMARK(BM_SiluSimd)->Args({255, 40, 40})->Unit(benchmark::kMillisecond); 112 | BENCHMARK(BM_SiluSimd)->Args({255, 20, 20})->Unit(benchmark::kMillisecond); -------------------------------------------------------------------------------- /bench/bench_unet.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-2-2. 23 | #include 24 | #include "runtime/runtime_ir.hpp" 25 | 26 | const static int kIterationNum = 4; 27 | 28 | static void BM_Unet_Batch1_512x512(benchmark::State& state) { 29 | using namespace kuiper_infer; 30 | RuntimeGraph graph("tmp/unet/unet_demo.pnnx.param", "tmp/unet/unet_demo.pnnx.bin"); 31 | graph.Build(); 32 | const uint32_t batch_size = 1; 33 | std::vector>> inputs; 34 | 35 | for (int i = 0; i < batch_size; ++i) { 36 | std::shared_ptr> input = std::make_shared>(3, 512, 512); 37 | input->RandN(); 38 | inputs.push_back(input); 39 | } 40 | 41 | graph.set_inputs("pnnx_input_0", inputs); 42 | for (auto _ : state) { 43 | graph.Forward(false); 44 | } 45 | } 46 | 47 | //BENCHMARK(BM_Unet_Batch1_512x512)->Unit(benchmark::kMillisecond)->Iterations(kIterationNum); -------------------------------------------------------------------------------- /bench/bench_yolo.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-2-2. 23 | #include 24 | #include "runtime/runtime_ir.hpp" 25 | const static int kIterationNum = 5; 26 | 27 | static void BM_Yolov5nano_Batch4_320x320(benchmark::State& state) { 28 | using namespace kuiper_infer; 29 | RuntimeGraph graph("tmp/yolo/demo/yolov5n_small.pnnx.param", 30 | "tmp/yolo/demo/yolov5n_small.pnnx.bin"); 31 | 32 | graph.Build(); 33 | const uint32_t batch_size = 4; 34 | std::vector>> inputs; 35 | 36 | for (int i = 0; i < batch_size; ++i) { 37 | std::shared_ptr> input = std::make_shared>(3, 320, 320); 38 | input->Ones(); 39 | inputs.push_back(input); 40 | } 41 | 42 | graph.set_inputs("pnnx_input_0", inputs); 43 | for (auto _ : state) { 44 | graph.Forward(false); 45 | } 46 | } 47 | 48 | static void BM_Yolov5s_Batch4_640x640(benchmark::State& state) { 49 | using namespace kuiper_infer; 50 | RuntimeGraph graph("tmp/yolo/demo/yolov5s_batch4.pnnx.param", 51 | "tmp/yolo/demo/yolov5s_batch4.pnnx.bin"); 52 | 53 | graph.Build(); 54 | const uint32_t batch_size = 4; 55 | std::vector>> inputs; 56 | 57 | for (int i = 0; i < batch_size; ++i) { 58 | std::shared_ptr> input = std::make_shared>(3, 640, 640); 59 | input->Ones(); 60 | inputs.push_back(input); 61 | } 62 | graph.set_inputs("pnnx_input_0", inputs); 63 | for (auto _ : state) { 64 | graph.Forward(false); 65 | } 66 | } 67 | 68 | static void BM_Yolov5s_Batch8_640x640(benchmark::State& state) { 69 | using namespace kuiper_infer; 70 | RuntimeGraph graph("tmp/yolo/demo/yolov5s_batch8.pnnx.param", 71 | "tmp/yolo/demo/yolov5s_batch8.pnnx.bin"); 72 | 73 | graph.Build(); 74 | const uint32_t batch_size = 8; 75 | std::vector>> inputs; 76 | 77 | for (int i = 0; i < batch_size; ++i) { 78 | std::shared_ptr> input = std::make_shared>(3, 640, 640); 79 | input->Ones(); 80 | inputs.push_back(input); 81 | } 82 | graph.set_inputs("pnnx_input_0", inputs); 83 | for (auto _ : state) { 84 | graph.Forward(false); 85 | } 86 | } 87 | 88 | BENCHMARK(BM_Yolov5nano_Batch4_320x320)->Unit(benchmark::kMillisecond)->Iterations(5); 89 | BENCHMARK(BM_Yolov5s_Batch4_640x640)->Unit(benchmark::kMillisecond)->Iterations(5); 90 | BENCHMARK(BM_Yolov5s_Batch8_640x640)->Unit(benchmark::kMillisecond)->Iterations(5); -------------------------------------------------------------------------------- /demos/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #add_subdirectory(resnet) 2 | #add_subdirectory(unet) 3 | add_subdirectory(yolo) 4 | #add_subdirectory(mobilenet) 5 | #add_subdirectory(llama2) 6 | -------------------------------------------------------------------------------- /demos/image_util.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-5. 23 | 24 | #include "image_util.hpp" 25 | 26 | float Letterbox(const cv::Mat& image, cv::Mat& out_image, const cv::Size& new_shape, int stride, 27 | const cv::Scalar& color, bool fixed_shape, bool scale_up) { 28 | cv::Size shape = image.size(); 29 | float r = std::min((float)new_shape.height / (float)shape.height, 30 | (float)new_shape.width / (float)shape.width); 31 | if (!scale_up) { 32 | r = std::min(r, 1.0f); 33 | } 34 | 35 | int new_unpad[2]{(int)std::round((float)shape.width * r), 36 | (int)std::round((float)shape.height * r)}; 37 | 38 | cv::Mat tmp; 39 | if (shape.width != new_unpad[0] || shape.height != new_unpad[1]) { 40 | cv::resize(image, tmp, cv::Size(new_unpad[0], new_unpad[1])); 41 | } else { 42 | tmp = image.clone(); 43 | } 44 | 45 | float dw = new_shape.width - new_unpad[0]; 46 | float dh = new_shape.height - new_unpad[1]; 47 | 48 | if (!fixed_shape) { 49 | dw = (float)((int)dw % stride); 50 | dh = (float)((int)dh % stride); 51 | } 52 | 53 | dw /= 2.0f; 54 | dh /= 2.0f; 55 | 56 | int top = int(std::round(dh - 0.1f)); 57 | int bottom = int(std::round(dh + 0.1f)); 58 | int left = int(std::round(dw - 0.1f)); 59 | int right = int(std::round(dw + 0.1f)); 60 | cv::copyMakeBorder(tmp, out_image, top, bottom, left, right, cv::BORDER_CONSTANT, color); 61 | return 1.0f / r; 62 | } 63 | 64 | template 65 | T clip(const T& n, const T& lower, const T& upper) { 66 | return std::max(lower, std::min(n, upper)); 67 | } 68 | 69 | void ScaleCoords(const cv::Size& img_shape, cv::Rect& coords, const cv::Size& img_origin_shape) { 70 | float gain = std::min((float)img_shape.height / (float)img_origin_shape.height, 71 | (float)img_shape.width / (float)img_origin_shape.width); 72 | 73 | int pad[2] = {(int)(((float)img_shape.width - (float)img_origin_shape.width * gain) / 2.0f), 74 | (int)(((float)img_shape.height - (float)img_origin_shape.height * gain) / 2.0f)}; 75 | 76 | coords.x = (int)std::round(((float)(coords.x - pad[0]) / gain)); 77 | coords.y = (int)std::round(((float)(coords.y - pad[1]) / gain)); 78 | 79 | coords.width = (int)std::round(((float)coords.width / gain)); 80 | coords.height = (int)std::round(((float)coords.height / gain)); 81 | 82 | coords.x = clip(coords.x, 0, img_origin_shape.width); 83 | coords.y = clip(coords.y, 0, img_origin_shape.height); 84 | coords.width = clip(coords.width, 0, img_origin_shape.width); 85 | coords.height = clip(coords.height, 0, img_origin_shape.height); 86 | } 87 | -------------------------------------------------------------------------------- /demos/image_util.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-5. 23 | 24 | #ifndef KUIPER_INFER_DEMOS_IMAGE_UTIL_HPP_ 25 | #define KUIPER_INFER_DEMOS_IMAGE_UTIL_HPP_ 26 | #include 27 | 28 | struct Detection { 29 | cv::Rect box; 30 | float conf = 0.f; 31 | int class_id = -1; 32 | }; 33 | 34 | float Letterbox(const cv::Mat& image, cv::Mat& out_image, 35 | const cv::Size& new_shape = cv::Size(640, 640), int stride = 32, 36 | const cv::Scalar& color = cv::Scalar(114, 114, 114), bool fixed_shape = false, 37 | bool scale_up = false); 38 | 39 | void ScaleCoords(const cv::Size& img_shape, cv::Rect& coords, const cv::Size& img_origin_shape); 40 | 41 | #endif // KUIPER_INFER_DEMOS_IMAGE_UTIL_HPP_ 42 | -------------------------------------------------------------------------------- /demos/mobilenet/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | set(CMAKE_CXX_STANDARD 17) 3 | 4 | find_package(OpenCV REQUIRED) 5 | include_directories(${OpenCV_INCLUDE_DIRS}) 6 | link_directories(${OpenCV_LIBRARY_DIRS}) 7 | 8 | if (MSVC) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") 10 | else () 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fopenmp -march=native") 12 | endif () 13 | 14 | add_executable(mobile_test mobile.cpp ../image_util.hpp ../image_util.cpp) 15 | target_link_directories(mobile_test PUBLIC ${PROJECT_SOURCE_DIR}/lib) 16 | target_link_libraries(mobile_test ${OpenCV_LIBS} kuiper) 17 | 18 | -------------------------------------------------------------------------------- /demos/resnet/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | set(CMAKE_CXX_STANDARD 17) 3 | 4 | find_package(OpenCV REQUIRED) 5 | include_directories(${OpenCV_INCLUDE_DIRS}) 6 | link_directories(${OpenCV_LIBRARY_DIRS}) 7 | 8 | if (MSVC) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") 10 | else () 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fopenmp -march=native") 12 | endif () 13 | 14 | add_executable(resnet_test resnet_test.cpp ../image_util.hpp ../image_util.cpp) 15 | target_link_directories(resnet_test PUBLIC ${PROJECT_SOURCE_DIR}/lib) 16 | target_link_libraries(resnet_test ${OpenCV_LIBS} kuiper) 17 | 18 | -------------------------------------------------------------------------------- /demos/resnet/resnet_test.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-2-2. 23 | #include 24 | #include 25 | #include 26 | #include "../source/layer/details/softmax.hpp" 27 | #include "data/tensor.hpp" 28 | #include "runtime/runtime_ir.hpp" 29 | #include "tick.hpp" 30 | 31 | // python ref https://pytorch.org/hub/pytorch_vision_resnet/ 32 | kuiper_infer::sftensor PreProcessImage(const cv::Mat& image) { 33 | using namespace kuiper_infer; 34 | assert(!image.empty()); 35 | // 调整输入大小 36 | cv::Mat resize_image; 37 | cv::resize(image, resize_image, cv::Size(224, 224)); 38 | 39 | cv::Mat rgb_image; 40 | cv::cvtColor(resize_image, rgb_image, cv::COLOR_BGR2RGB); 41 | 42 | rgb_image.convertTo(rgb_image, CV_32FC3); 43 | std::vector split_images; 44 | cv::split(rgb_image, split_images); 45 | uint32_t input_w = 224; 46 | uint32_t input_h = 224; 47 | uint32_t input_c = 3; 48 | sftensor input = std::make_shared(input_c, input_h, input_w); 49 | 50 | uint32_t index = 0; 51 | for (const auto& split_image : split_images) { 52 | assert(split_image.total() == input_w * input_h); 53 | const cv::Mat& split_image_t = split_image.t(); 54 | memcpy(input->slice(index).memptr(), split_image_t.data, sizeof(float) * split_image.total()); 55 | index += 1; 56 | } 57 | 58 | float mean_r = 0.485f; 59 | float mean_g = 0.456f; 60 | float mean_b = 0.406f; 61 | 62 | float var_r = 0.229f; 63 | float var_g = 0.224f; 64 | float var_b = 0.225f; 65 | assert(input->channels() == 3); 66 | input->data() = input->data() / 255.f; 67 | input->slice(0) = (input->slice(0) - mean_r) / var_r; 68 | input->slice(1) = (input->slice(1) - mean_g) / var_g; 69 | input->slice(2) = (input->slice(2) - mean_b) / var_b; 70 | return input; 71 | } 72 | 73 | int main(int argc, char* argv[]) { 74 | if (argc != 2) { 75 | printf("usage: ./resnet_test [image path]\n"); 76 | exit(-1); 77 | } 78 | using namespace kuiper_infer; 79 | 80 | const std::string& path = argv[1]; 81 | 82 | const uint32_t batch_size = 1; 83 | std::vector inputs; 84 | for (uint32_t i = 0; i < batch_size; ++i) { 85 | cv::Mat image = cv::imread(path); 86 | // 图像预处理 87 | sftensor input = PreProcessImage(image); 88 | inputs.push_back(input); 89 | } 90 | 91 | const std::string& param_path = "tmp/resnet/demo/resnet18_batch1.pnnx.param"; 92 | const std::string& weight_path = "tmp/resnet/demo/resnet18_batch1.pnnx.bin"; 93 | RuntimeGraph graph(param_path, weight_path); 94 | graph.Build(); 95 | graph.set_inputs("pnnx_input_0", inputs); 96 | 97 | // 推理 98 | TICK(forward) 99 | graph.Forward(false); 100 | std::vector>> outputs = graph.get_outputs("pnnx_output_0"); 101 | TOCK(forward) 102 | 103 | assert(outputs.size() == batch_size); 104 | // softmax 105 | std::vector outputs_softmax(batch_size); 106 | SoftmaxLayer softmax_layer(0); 107 | softmax_layer.Forward(outputs, outputs_softmax); 108 | assert(outputs_softmax.size() == batch_size); 109 | 110 | for (int i = 0; i < outputs_softmax.size(); ++i) { 111 | const sftensor& output_tensor = outputs_softmax.at(i); 112 | assert(output_tensor->size() == 1 * 1000); 113 | // 找到类别概率最大的种类 114 | float max_prob = -1; 115 | int max_index = -1; 116 | for (int j = 0; j < output_tensor->size(); ++j) { 117 | float prob = output_tensor->index(j); 118 | if (max_prob <= prob) { 119 | max_prob = prob; 120 | max_index = j; 121 | } 122 | } 123 | printf("class with max prob is %f index %d\n", max_prob, max_index); 124 | } 125 | return 0; 126 | } -------------------------------------------------------------------------------- /demos/unet/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | set(CMAKE_CXX_STANDARD 17) 3 | 4 | find_package(OpenCV REQUIRED) 5 | include_directories(${OpenCV_INCLUDE_DIRS}) 6 | link_directories(${OpenCV_LIBRARY_DIRS}) 7 | 8 | if(MSVC) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") 10 | else() 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fopenmp -march=native") 12 | endif() 13 | 14 | add_executable(unet_test unet_test.cpp ../image_util.hpp ../image_util.cpp) 15 | target_link_directories(unet_test PUBLIC ${PROJECT_SOURCE_DIR}/lib) 16 | target_link_libraries(unet_test ${OpenCV_LIBS} kuiper) 17 | 18 | -------------------------------------------------------------------------------- /demos/unet/unet_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "../source/layer/details/softmax.hpp" 6 | #include "data/tensor.hpp" 7 | #include "runtime/runtime_ir.hpp" 8 | #include "tick.hpp" 9 | 10 | kuiper_infer::sftensor PreProcessImage(const cv::Mat& image) { 11 | using namespace kuiper_infer; 12 | assert(!image.empty()); 13 | cv::Mat resize_image; 14 | cv::resize(image, resize_image, cv::Size(512, 512)); 15 | 16 | cv::Mat rgb_image; 17 | cv::cvtColor(resize_image, rgb_image, cv::COLOR_BGR2RGB); 18 | 19 | rgb_image.convertTo(rgb_image, CV_32FC3); 20 | std::vector split_images; 21 | cv::split(rgb_image, split_images); 22 | uint32_t input_w = 512; 23 | uint32_t input_h = 512; 24 | uint32_t input_c = 3; 25 | sftensor input = std::make_shared(input_c, input_h, input_w); 26 | 27 | uint32_t index = 0; 28 | for (const auto& split_image : split_images) { 29 | assert(split_image.total() == input_w * input_h); 30 | const cv::Mat& split_image_t = split_image.t(); 31 | memcpy(input->slice(index).memptr(), split_image_t.data, sizeof(float) * split_image.total()); 32 | index += 1; 33 | } 34 | 35 | assert(input->channels() == 3); 36 | input->data() = input->data() / 255.f; 37 | return input; 38 | } 39 | 40 | int main(int argc, char* argv[]) { 41 | if (argc != 4) { 42 | printf("usage: ./unet_test [image path] [pnnx_param path] [pnnx_bin path]\n"); 43 | exit(-1); 44 | } 45 | using namespace kuiper_infer; 46 | 47 | const std::string& path = argv[1]; 48 | const uint32_t batch_size = 1; 49 | std::vector inputs; 50 | for (uint32_t i = 0; i < batch_size; ++i) { 51 | cv::Mat image = cv::imread(path); 52 | // 图像预处理 53 | sftensor input = PreProcessImage(image); 54 | inputs.push_back(input); 55 | } 56 | 57 | const std::string& param_path = argv[2]; 58 | const std::string& weight_path = argv[3]; 59 | RuntimeGraph graph(param_path, weight_path); 60 | graph.Build(); 61 | graph.set_inputs("pnnx_input_0", inputs); 62 | std::cout << "start inference!" << std::endl; 63 | TICK(forward) 64 | graph.Forward(true); 65 | std::vector>> outputs = graph.get_outputs("pnnx_output_0"); 66 | TOCK(forward) 67 | assert(outputs.size() == batch_size); 68 | 69 | uint32_t input_h = 512; 70 | uint32_t input_w = 512; 71 | for (int i = 0; i < outputs.size(); ++i) { 72 | const sftensor& output_tensor = outputs.at(i); 73 | arma::fmat& out_channel_0 = output_tensor->slice(0); 74 | arma::fmat& out_channel_1 = output_tensor->slice(1); 75 | arma::fmat out_channel(input_h, input_w); 76 | assert(out_channel_0.size() == out_channel_1.size()); 77 | assert(out_channel_0.size() == out_channel.size()); 78 | 79 | for (int j = 0; j < out_channel_0.size(); j++) { 80 | if (out_channel_0.at(j) < out_channel_1.at(j)) { 81 | out_channel.at(j) = 255; 82 | } else { 83 | out_channel.at(j) = 0; 84 | } 85 | } 86 | 87 | arma::fmat out_channel_t = out_channel.t(); 88 | auto output_array_ptr = out_channel_t.memptr(); 89 | assert(output_array_ptr != nullptr); 90 | 91 | int data_type = CV_32F; 92 | cv::Mat output(input_h, input_w, data_type, output_array_ptr); 93 | cv::imwrite(cv::String("unet_output.jpg"), output); 94 | } 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /demos/yolo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | set(CMAKE_CXX_STANDARD 17) 3 | 4 | find_package(OpenCV REQUIRED) 5 | include_directories(${OpenCV_INCLUDE_DIRS}) 6 | link_directories(${OpenCV_LIBRARY_DIRS}) 7 | 8 | if (MSVC) 9 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") 10 | else () 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fopenmp -march=native") 12 | endif () 13 | 14 | add_executable(yolo_test yolo_test.cpp ../image_util.hpp ../image_util.cpp) 15 | target_link_directories(yolo_test PUBLIC ${PROJECT_SOURCE_DIR}/lib) 16 | target_link_libraries(yolo_test ${OpenCV_LIBS} kuiper) 17 | 18 | -------------------------------------------------------------------------------- /dockerfile: -------------------------------------------------------------------------------- 1 | FROM --platform=linux/amd64 ubuntu:22.04 2 | 3 | RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 4 | 5 | # 系统更新以及安装编译器等工具 6 | RUN apt -y update 7 | RUN apt -y upgrade 8 | RUN apt install -y build-essential cmake git gfortran wget 9 | 10 | # 编译安装OpenBLAS 11 | RUN git clone https://github.com/xianyi/OpenBLAS.git 12 | RUN mkdir OpenBLAS/build 13 | RUN cmake OpenBLAS/ -B OpenBLAS/build/ -DUSE_OPENMP=ON 14 | RUN make -C OpenBLAS/build/ -j$(nproc) 15 | RUN make install -C OpenBLAS/build/ 16 | 17 | # 下载armadillo的其他依赖 18 | RUN apt install -y liblapack-dev libarpack2-dev libsuperlu-dev 19 | 20 | # 编译安装armadillo 21 | RUN wget https://sourceforge.net/projects/arma/files/armadillo-12.6.3.tar.xz 22 | RUN tar -vxf armadillo-12.6.3.tar.xz 23 | RUN mkdir armadillo-12.6.3/build 24 | RUN cmake armadillo-12.6.3/ -B armadillo-12.6.3/build/ 25 | RUN cmake armadillo-12.6.3 -B armadillo-12.6.3/build/ 26 | RUN make -C armadillo-12.6.3/build -j$(nproc) 27 | RUN make install -C armadillo-12.6.3/build 28 | 29 | # 编译安装googletest 30 | RUN git clone https://github.com/google/googletest.git 31 | RUN mkdir googletest/build 32 | RUN cmake googletest/ -B googletest/build/ 33 | RUN make -C googletest/build/ -j$(nproc) 34 | RUN make install -C googletest/build/ 35 | 36 | # 编译安装googlebenchmark 37 | RUN git clone https://github.com/google/benchmark.git 38 | RUN mkdir benchmark/build 39 | RUN cmake benchmark/ -B benchmark/build/ -DBENCHMARK_ENABLE_TESTING=OFF 40 | RUN make -C benchmark/build/ -j$(nproc) 41 | RUN make install -C benchmark/build/ 42 | 43 | # 编译安装googlelog 44 | RUN git clone https://github.com/google/glog.git 45 | RUN mkdir glog/build 46 | RUN cmake glog/ -B glog/build/ 47 | RUN make -C glog/build/ -j$(nproc) 48 | RUN make install -C glog/build/ 49 | 50 | RUN rm -rf OpenBLAS/ armadillo-12.6.3.tar.xz armadillo-12.6.3 googletest/ benchmark/ glog/ 51 | 52 | WORKDIR /app 53 | -------------------------------------------------------------------------------- /imgs/31.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/31.jpg -------------------------------------------------------------------------------- /imgs/KuiperInfer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/KuiperInfer.png -------------------------------------------------------------------------------- /imgs/a.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/a.gif -------------------------------------------------------------------------------- /imgs/bus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/bus.jpg -------------------------------------------------------------------------------- /imgs/car.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/car.jpg -------------------------------------------------------------------------------- /imgs/demo_car.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/demo_car.jpg -------------------------------------------------------------------------------- /imgs/demo_car_.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/demo_car_.jpg -------------------------------------------------------------------------------- /imgs/do.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/do.gif -------------------------------------------------------------------------------- /imgs/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/logo.jpg -------------------------------------------------------------------------------- /imgs/me.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/me.jpg -------------------------------------------------------------------------------- /imgs/me.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/me.png -------------------------------------------------------------------------------- /imgs/qa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/qa.jpg -------------------------------------------------------------------------------- /imgs/zidane.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjhellofss/KuiperInfer/f9a194e7f7c1ec3b1dc04f77bea5ed44c6f32ec5/imgs/zidane.jpg -------------------------------------------------------------------------------- /include/data/load_data.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-21. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_DATA_LOAD_DATA_HPP_ 25 | #define KUIPER_INFER_INCLUDE_DATA_LOAD_DATA_HPP_ 26 | #include 27 | #include 28 | #include 29 | namespace kuiper_infer { 30 | 31 | /** 32 | * @brief CSV data loader 33 | * 34 | * Provides utility to load CSV data into Armadillo matrices. 35 | */ 36 | class CSVDataLoader { 37 | public: 38 | /** 39 | * @brief Loads CSV file into matrix 40 | * 41 | * Loads data from a CSV file into an Armadillo matrix. 42 | * 43 | * @param file_path Path to CSV file 44 | * @param split_char Delimiter character 45 | * @return Matrix containing loaded data 46 | */ 47 | template 48 | static arma::Mat LoadData(const std::string& file_path, char split_char = ','); 49 | 50 | private: 51 | /** 52 | * @brief Gets matrix dimensions from CSV 53 | * 54 | * Gets number of rows and cols for matrix based on CSV file. 55 | 56 | * @param file CSV file stream 57 | * @param split_char Delimiter character 58 | * @return Pair of rows and cols 59 | */ 60 | static std::pair GetMatrixSize(std::ifstream& file, char split_char); 61 | }; 62 | 63 | template 64 | arma::Mat CSVDataLoader::LoadData(const std::string& file_path, const char split_char) { 65 | arma::Mat data; 66 | if (file_path.empty()) { 67 | LOG(ERROR) << "CSV file path is empty: " << file_path; 68 | return data; 69 | } 70 | 71 | std::ifstream in(file_path); 72 | if (!in.is_open() || !in.good()) { 73 | LOG(ERROR) << "File open failed: " << file_path; 74 | return data; 75 | } 76 | 77 | std::string line_str; 78 | std::stringstream line_stream; 79 | 80 | const auto& [rows, cols] = CSVDataLoader::GetMatrixSize(in, split_char); 81 | data.zeros(rows, cols); 82 | 83 | size_t row = 0; 84 | while (in.good()) { 85 | std::getline(in, line_str); 86 | if (line_str.empty()) { 87 | break; 88 | } 89 | 90 | std::string token; 91 | line_stream.clear(); 92 | line_stream.str(line_str); 93 | 94 | size_t col = 0; 95 | while (line_stream.good()) { 96 | std::getline(line_stream, token, split_char); 97 | try { 98 | if (std::is_same_v) { 99 | data.at(row, col) = std::stof(token); 100 | } else if (std::is_same_v || std::is_same_v) { 101 | data.at(row, col) = std::stoi(token); 102 | } else { 103 | LOG(FATAL) << "Unsupported data type \n"; 104 | } 105 | } catch (std::exception& e) { 106 | DLOG(ERROR) << "Parse CSV File meet error: " << e.what() << " row:" << row 107 | << " col:" << col; 108 | } 109 | col += 1; 110 | CHECK(col <= cols) << "There are excessive elements on the column"; 111 | } 112 | 113 | row += 1; 114 | CHECK(row <= rows) << "There are excessive elements on the row"; 115 | } 116 | return data; 117 | } 118 | 119 | } // namespace kuiper_infer 120 | 121 | #endif // KUIPER_INFER_INCLUDE_DATA_LOAD_DATA_HPP_ 122 | -------------------------------------------------------------------------------- /include/layer/abstract/layer_factory.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-17. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_LAYER_FACTORY_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_LAYER_FACTORY_HPP_ 26 | #include 27 | #include 28 | #include 29 | #include "layer.hpp" 30 | #include "runtime/runtime_op.hpp" 31 | 32 | namespace kuiper_infer { 33 | class LayerRegisterer { 34 | private: 35 | typedef StatusCode (*Creator)(const std::shared_ptr& op, 36 | std::shared_ptr>& layer); 37 | 38 | typedef std::map CreateRegistry; 39 | 40 | public: 41 | friend class LayerRegistererWrapper; 42 | friend class RegistryGarbageCollector; 43 | 44 | /** 45 | * @brief Registers a layer creator function 46 | * 47 | * Registers a layer creation function for the given layer type. 48 | * The creation function generates a layer instance from runtime operator. 49 | * 50 | * @param layer_type The name of the layer type 51 | * @param creator Function to create the layer 52 | */ 53 | static void RegisterCreator(const std::string& layer_type, const Creator& creator); 54 | 55 | /** 56 | * @brief Creates a layer object 57 | * 58 | * Calls the registered creator function for the given runtime operator 59 | * to create a layer instance. 60 | * 61 | * @param op The runtime operator 62 | * @return A shared pointer to the created layer object 63 | */ 64 | static std::shared_ptr> CreateLayer(const std::shared_ptr& op); 65 | 66 | /** 67 | * @brief Gets the layer registry 68 | * 69 | * @return Pointer to the layer creator registry 70 | */ 71 | static CreateRegistry* Registry(); 72 | 73 | /** 74 | * @brief Gets registered layer types 75 | * 76 | * @return A vector of registered layer type names 77 | */ 78 | static std::vector layer_types(); 79 | 80 | private: 81 | static CreateRegistry* registry_; 82 | }; 83 | 84 | /** 85 | * @brief Layer registry wrapper 86 | * 87 | * Helper class to register a layer creator function. 88 | * Automatically calls LayerRegisterer::RegisterCreator. 89 | */ 90 | class LayerRegistererWrapper { 91 | public: 92 | explicit LayerRegistererWrapper(const LayerRegisterer::Creator& creator, 93 | const std::string& layer_type) { 94 | LayerRegisterer::RegisterCreator(layer_type, creator); 95 | } 96 | 97 | template 98 | explicit LayerRegistererWrapper(const LayerRegisterer::Creator& creator, 99 | const std::string& layer_type, const Ts&... other_layer_types) 100 | : LayerRegistererWrapper(creator, other_layer_types...) { 101 | LayerRegisterer::RegisterCreator(layer_type, creator); 102 | } 103 | 104 | explicit LayerRegistererWrapper(const LayerRegisterer::Creator& creator) {} 105 | }; 106 | 107 | /** 108 | * @brief Garbage collector for layer registry 109 | * 110 | * Destructor that cleans up the LayerRegisterer registry 111 | * when program exits. Deletes registry pointer if allocated. 112 | */ 113 | class RegistryGarbageCollector { 114 | public: 115 | ~RegistryGarbageCollector() { 116 | if (LayerRegisterer::registry_ != nullptr) { 117 | delete LayerRegisterer::registry_; 118 | LayerRegisterer::registry_ = nullptr; 119 | } 120 | } 121 | friend class LayerRegisterer; 122 | 123 | private: 124 | RegistryGarbageCollector() = default; 125 | RegistryGarbageCollector(const RegistryGarbageCollector&) = default; 126 | }; 127 | 128 | } // namespace kuiper_infer 129 | 130 | #endif // KUIPER_INFER_SOURCE_LAYER_LAYER_FACTORY_HPP_ 131 | -------------------------------------------------------------------------------- /include/layer/abstract/non_param_layer.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_NONPRARM_LAYER_LAYER_HPP_ 25 | #define KUIPER_INFER_SOURCE_NONPRARM_LAYER_LAYER_HPP_ 26 | #include "layer.hpp" 27 | namespace kuiper_infer { 28 | using NonParamLayer = Layer; 29 | } // namespace kuiper_infer 30 | #endif // KUIPER_INFER_SOURCE_NONPRARM_LAYER_LAYER_HPP_ 31 | -------------------------------------------------------------------------------- /include/parser/parse_expression.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-1. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_PARSER_PARSE_EXPRESSION_HPP_ 25 | #define KUIPER_INFER_INCLUDE_PARSER_PARSE_EXPRESSION_HPP_ 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | namespace kuiper_infer { 32 | 33 | enum class TokenType { 34 | TokenUnknown = -9, 35 | TokenInputNumber = -8, 36 | TokenComma = -7, 37 | TokenAdd = -6, 38 | TokenMul = -5, 39 | TokenLeftBracket = -4, 40 | TokenRightBracket = -3, 41 | }; 42 | 43 | /** 44 | * @brief Token parsed from expression 45 | * 46 | * Represents a token extracted from the input expression string. 47 | */ 48 | struct Token { 49 | /// Type of this token 50 | TokenType token_type = TokenType::TokenUnknown; 51 | 52 | /// Start position in the input expression 53 | int32_t start_pos = 0; 54 | 55 | /// End position in the input expression 56 | int32_t end_pos = 0; 57 | 58 | /** 59 | * @brief Construct a new Token 60 | * 61 | * @param token_type Token type 62 | * @param start_pos Start position 63 | * @param end_pos End position 64 | */ 65 | Token(TokenType token_type, int32_t start_pos, int32_t end_pos) 66 | : token_type(token_type), start_pos(start_pos), end_pos(end_pos) {} 67 | }; 68 | 69 | /** 70 | * @brief Node in expression syntax tree 71 | */ 72 | struct TokenNode { 73 | /// Index of input variable 74 | int32_t num_index = -1; 75 | 76 | /// Left child node 77 | std::shared_ptr left = nullptr; 78 | 79 | /// Right child node 80 | std::shared_ptr right = nullptr; 81 | 82 | /** 83 | * @brief Construct a new Token Node 84 | * 85 | * @param num_index Index of input variable 86 | * @param left Left child node 87 | * @param right Right child node 88 | */ 89 | TokenNode(int32_t num_index, std::shared_ptr left, std::shared_ptr right); 90 | 91 | TokenNode() = default; 92 | }; 93 | 94 | /** 95 | * @brief Parser for math expressions 96 | * 97 | * Parses a math expression string into a syntax tree. 98 | * Performs lexical and syntax analysis. 99 | */ 100 | class ExpressionParser { 101 | public: 102 | /** 103 | * @brief Construct a new Expression Parser 104 | * 105 | * @param statement The expression string 106 | */ 107 | explicit ExpressionParser(std::string statement) : statement_(std::move(statement)) {} 108 | 109 | /** 110 | * @brief Performs lexical analysis 111 | * 112 | * Breaks the expression into tokens. 113 | * 114 | * @param retokenize Whether to re-tokenize 115 | */ 116 | void Tokenizer(bool retokenize = false); 117 | 118 | /** 119 | * @brief Performs syntax analysis 120 | * 121 | * Generates a syntax tree from the tokens. 122 | * 123 | * @return Vector of root nodes 124 | */ 125 | std::vector> Generate(); 126 | 127 | /** 128 | * @brief Gets the tokens 129 | * 130 | * @return The tokens 131 | */ 132 | const std::vector& tokens() const; 133 | 134 | /** 135 | * @brief Gets the token strings 136 | * 137 | * @return The token strings 138 | */ 139 | const std::vector& token_str_array() const; 140 | 141 | private: 142 | std::shared_ptr Generate_(int32_t& index); 143 | 144 | private: 145 | std::vector tokens_; 146 | std::vector token_strs_; 147 | std::string statement_; 148 | }; 149 | } // namespace kuiper_infer 150 | 151 | #endif // KUIPER_INFER_INCLUDE_PARSER_PARSE_EXPRESSION_HPP_ 152 | -------------------------------------------------------------------------------- /include/runtime/pnnx/store_zip.hpp: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. 4 | // 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // https://opensource.org/licenses/BSD-3-Clause 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef PNNX_STOREZIP_H 16 | #define PNNX_STOREZIP_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace pnnx { 24 | 25 | class StoreZipReader 26 | { 27 | public: 28 | StoreZipReader(); 29 | ~StoreZipReader(); 30 | 31 | int open(const std::string& path); 32 | 33 | std::vector get_names() const; 34 | 35 | uint64_t get_file_size(const std::string& name) const; 36 | 37 | int read_file(const std::string& name, char* data); 38 | 39 | int close(); 40 | 41 | private: 42 | FILE* fp; 43 | 44 | struct StoreZipMeta 45 | { 46 | uint64_t offset; 47 | uint64_t size; 48 | }; 49 | 50 | std::map filemetas; 51 | }; 52 | 53 | class StoreZipWriter 54 | { 55 | public: 56 | StoreZipWriter(); 57 | ~StoreZipWriter(); 58 | 59 | int open(const std::string& path); 60 | 61 | int write_file(const std::string& name, const char* data, uint64_t size); 62 | 63 | int close(); 64 | 65 | private: 66 | FILE* fp; 67 | 68 | struct StoreZipMeta 69 | { 70 | std::string name; 71 | uint64_t lfh_offset; 72 | uint32_t crc32; 73 | uint64_t size; 74 | }; 75 | 76 | std::vector filemetas; 77 | }; 78 | 79 | } // namespace pnnx 80 | 81 | #endif // PNNX_STOREZIP_H 82 | -------------------------------------------------------------------------------- /include/runtime/runtime_attr.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-28. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_PARSER_RUNTIME_ATTR_HPP_ 25 | #define KUIPER_INFER_INCLUDE_PARSER_RUNTIME_ATTR_HPP_ 26 | #include 27 | #include 28 | #include "runtime_datatype.hpp" 29 | #include "status_code.hpp" 30 | 31 | namespace kuiper_infer { 32 | 33 | /** 34 | * @brief Runtime operator attribute 35 | * 36 | * Represents an attribute like weights or biases for a graph operator. 37 | * Contains the attribute data, shape, and data type. 38 | */ 39 | struct RuntimeAttribute { 40 | RuntimeAttribute() = default; 41 | 42 | explicit RuntimeAttribute(std::vector shape, RuntimeDataType type, 43 | std::vector weight_data) 44 | : shape(std::move(shape)), type(type), weight_data(std::move(weight_data)) {} 45 | 46 | /** 47 | * @brief Attribute data 48 | * 49 | * Typically contains the binary weight values. 50 | */ 51 | std::vector weight_data; 52 | 53 | /** 54 | * @brief Shape of the attribute 55 | * 56 | * Describes the dimensions of the attribute data. 57 | */ 58 | std::vector shape; 59 | 60 | /** 61 | * @brief Data type of the attribute 62 | * 63 | * Such as float32, int8, etc. 64 | */ 65 | RuntimeDataType type = RuntimeDataType::kTypeUnknown; 66 | 67 | /** 68 | * @brief Gets the attribute data as a typed array 69 | * 70 | * Returns the weight data as a vector of the template type T. 71 | * The attribute data is cleared after get by default. 72 | * 73 | * @tparam T Data type to return (float, int, etc) 74 | * @param need_clear_weight Whether to clear data after get 75 | * @return Vector containing the attribute data 76 | */ 77 | template 78 | std::vector get(bool need_clear_weight = true); 79 | }; 80 | 81 | template 82 | std::vector RuntimeAttribute::get(bool need_clear_weight) { 83 | CHECK(!weight_data.empty()); 84 | CHECK(type != RuntimeDataType::kTypeUnknown); 85 | const uint32_t elem_size = sizeof(T); 86 | CHECK_EQ(weight_data.size() % elem_size, 0); 87 | const uint32_t weight_data_size = weight_data.size() / elem_size; 88 | 89 | std::vector weights; 90 | weights.reserve(weight_data_size); 91 | switch (type) { 92 | case RuntimeDataType::kTypeFloat32: { 93 | static_assert(std::is_same::value == true); 94 | float* weight_data_ptr = reinterpret_cast(weight_data.data()); 95 | for (uint32_t i = 0; i < weight_data_size; ++i) { 96 | float weight = *(weight_data_ptr + i); 97 | weights.push_back(weight); 98 | } 99 | break; 100 | } 101 | default: { 102 | LOG(FATAL) << "Unknown weight data type: " << int32_t(type); 103 | } 104 | } 105 | if (need_clear_weight) { 106 | std::vector empty_vec = std::vector(); 107 | this->weight_data.swap(empty_vec); 108 | } 109 | return weights; 110 | } 111 | 112 | } // namespace kuiper_infer 113 | #endif // KUIPER_INFER_INCLUDE_PARSER_RUNTIME_ATTR_HPP_ 114 | -------------------------------------------------------------------------------- /include/runtime/runtime_datatype.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-23. 23 | #ifndef KUIPER_INFER_INCLUDE_RUNTIME_RUNTIME_DATATYPE_HPP_ 24 | #define KUIPER_INFER_INCLUDE_RUNTIME_RUNTIME_DATATYPE_HPP_ 25 | /** 26 | * @brief Runtime data types for operator attributes 27 | * 28 | * Enumerates the data types supported for operator attributes like 29 | * weights and biases. 30 | */ 31 | enum class RuntimeDataType { 32 | kTypeUnknown = 0, 33 | kTypeFloat32 = 1, 34 | kTypeFloat64 = 2, 35 | kTypeFloat16 = 3, 36 | kTypeInt32 = 4, 37 | kTypeInt64 = 5, 38 | kTypeInt16 = 6, 39 | kTypeInt8 = 7, 40 | kTypeUInt8 = 8, 41 | }; 42 | #endif // KUIPER_INFER_INCLUDE_RUNTIME_RUNTIME_DATATYPE_HPP_ 43 | -------------------------------------------------------------------------------- /include/runtime/runtime_operand.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-28. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_PARSER_RUNTIME_OPERAND_HPP_ 25 | #define KUIPER_INFER_INCLUDE_PARSER_RUNTIME_OPERAND_HPP_ 26 | #include 27 | #include 28 | #include 29 | #include "data/tensor.hpp" 30 | #include "runtime_datatype.hpp" 31 | #include "status_code.hpp" 32 | 33 | namespace kuiper_infer { 34 | /** 35 | * @brief Base for runtime graph operand 36 | * 37 | * Template base class representing an operand (input/output) in a 38 | * graph. Contains operand name, shape, data vector, and data type. 39 | * 40 | * @tparam T Operand data type (float, int, etc.) 41 | */ 42 | template 43 | struct RuntimeOperandBase { 44 | explicit RuntimeOperandBase() = default; 45 | 46 | explicit RuntimeOperandBase(std::string name, std::vector shapes, 47 | std::vector>> datas, RuntimeDataType type) 48 | : name(std::move(name)), shapes(std::move(shapes)), datas(std::move(datas)), type(type) {} 49 | 50 | explicit RuntimeOperandBase(std::string name, std::vector shapes, uint32_t data_size, 51 | RuntimeDataType type) 52 | : name(std::move(name)), shapes(std::move(shapes)), type(type) { 53 | datas.resize(data_size); 54 | } 55 | 56 | size_t size() const; 57 | 58 | /// Name of the operand 59 | std::string name; 60 | 61 | /// Shape of the operand 62 | std::vector shapes; 63 | 64 | /// Vector containing operand data 65 | std::vector>> datas; 66 | 67 | /// Data type of the operand 68 | RuntimeDataType type = RuntimeDataType::kTypeUnknown; 69 | }; 70 | 71 | template 72 | size_t RuntimeOperandBase::size() const { 73 | if (shapes.empty()) { 74 | return 0; 75 | } 76 | size_t size = std::accumulate(shapes.begin(), shapes.end(), 1, std::multiplies()); 77 | return size; 78 | } 79 | 80 | using RuntimeOperand = RuntimeOperandBase; 81 | 82 | using RuntimeOperandQuantized = RuntimeOperandBase; 83 | 84 | } // namespace kuiper_infer 85 | #endif // KUIPER_INFER_INCLUDE_PARSER_RUNTIME_OPERAND_HPP_ 86 | -------------------------------------------------------------------------------- /include/runtime/runtime_parameter.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-28. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_PARSER_RUNTIME_PARAMETER_HPP_ 25 | #define KUIPER_INFER_INCLUDE_PARSER_RUNTIME_PARAMETER_HPP_ 26 | #include 27 | #include 28 | #include "status_code.hpp" 29 | 30 | namespace kuiper_infer { 31 | /** 32 | * 计算节点中的参数信息,参数一共可以分为如下的几类 33 | * 1.int 34 | * 2.float 35 | * 3.string 36 | * 4.bool 37 | * 5.int array 38 | * 6.string array 39 | * 7.float array 40 | */ 41 | struct RuntimeParameter { /// 计算节点中的参数信息 42 | virtual ~RuntimeParameter() = default; 43 | 44 | explicit RuntimeParameter(RuntimeParameterType type = RuntimeParameterType::kParameterUnknown) 45 | : type(type) {} 46 | RuntimeParameterType type = RuntimeParameterType::kParameterUnknown; 47 | }; 48 | 49 | struct RuntimeParameterInt : public RuntimeParameter { 50 | explicit RuntimeParameterInt() : RuntimeParameter(RuntimeParameterType::kParameterInt) {} 51 | 52 | explicit RuntimeParameterInt(int32_t param_value) 53 | : RuntimeParameter(RuntimeParameterType::kParameterInt), value(param_value) {} 54 | 55 | int32_t value = 0; 56 | }; 57 | 58 | struct RuntimeParameterFloat : public RuntimeParameter { 59 | explicit RuntimeParameterFloat() : RuntimeParameter(RuntimeParameterType::kParameterFloat) {} 60 | 61 | explicit RuntimeParameterFloat(float param_value) 62 | : RuntimeParameter(RuntimeParameterType::kParameterFloat), value(param_value) {} 63 | 64 | float value = 0.f; 65 | }; 66 | 67 | struct RuntimeParameterString : public RuntimeParameter { 68 | explicit RuntimeParameterString() : RuntimeParameter(RuntimeParameterType::kParameterString) {} 69 | 70 | explicit RuntimeParameterString(std::string param_value) 71 | : RuntimeParameter(RuntimeParameterType::kParameterString), value(std::move(param_value)) {} 72 | 73 | std::string value; 74 | }; 75 | 76 | struct RuntimeParameterIntArray : public RuntimeParameter { 77 | explicit RuntimeParameterIntArray() 78 | : RuntimeParameter(RuntimeParameterType::kParameterIntArray) {} 79 | 80 | explicit RuntimeParameterIntArray(std::vector param_value) 81 | : RuntimeParameter(RuntimeParameterType::kParameterIntArray), value(std::move(param_value)) {} 82 | 83 | std::vector value; 84 | }; 85 | 86 | struct RuntimeParameterFloatArray : public RuntimeParameter { 87 | explicit RuntimeParameterFloatArray() 88 | : RuntimeParameter(RuntimeParameterType::kParameterFloatArray) {} 89 | 90 | explicit RuntimeParameterFloatArray(std::vector param_value) 91 | : RuntimeParameter(RuntimeParameterType::kParameterFloatArray), 92 | value(std::move(param_value)) {} 93 | 94 | std::vector value; 95 | }; 96 | 97 | struct RuntimeParameterStringArray : public RuntimeParameter { 98 | explicit RuntimeParameterStringArray() 99 | : RuntimeParameter(RuntimeParameterType::kParameterStringArray) {} 100 | 101 | explicit RuntimeParameterStringArray(std::vector param_value) 102 | : RuntimeParameter(RuntimeParameterType::kParameterStringArray), 103 | value(std::move(param_value)) {} 104 | 105 | std::vector value; 106 | }; 107 | 108 | struct RuntimeParameterBool : public RuntimeParameter { 109 | explicit RuntimeParameterBool() : RuntimeParameter(RuntimeParameterType::kParameterBool) {} 110 | 111 | explicit RuntimeParameterBool(bool param_value) 112 | : RuntimeParameter(RuntimeParameterType::kParameterBool), value(param_value) {} 113 | 114 | bool value = false; 115 | }; 116 | } // namespace kuiper_infer 117 | #endif // KUIPER_INFER_INCLUDE_PARSER_RUNTIME_PARAMETER_HPP_ 118 | -------------------------------------------------------------------------------- /include/status_code.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-12. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_COMMON_HPP_ 25 | #define KUIPER_INFER_INCLUDE_COMMON_HPP_ 26 | namespace kuiper_infer { 27 | 28 | enum class RuntimeParameterType { 29 | kParameterUnknown = 0, 30 | kParameterBool = 1, 31 | kParameterInt = 2, 32 | 33 | kParameterFloat = 3, 34 | kParameterString = 4, 35 | kParameterIntArray = 5, 36 | kParameterFloatArray = 6, 37 | kParameterStringArray = 7, 38 | }; 39 | 40 | enum class StatusCode { 41 | kUnknownCode = -1, 42 | kSuccess = 0, 43 | 44 | kInferInputsEmpty = 1, 45 | kInferOutputsEmpty = 2, 46 | kInferParamError = 3, 47 | kInferDimMismatch = 4, 48 | 49 | kFunctionNotImplement = 5, 50 | kParseWeightError = 6, 51 | kParseParamError = 7, 52 | kParseNullOperator = 8, 53 | }; 54 | 55 | } // namespace kuiper_infer 56 | #endif // KUIPER_INFER_INCLUDE_COMMON_HPP_ 57 | -------------------------------------------------------------------------------- /include/tick.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-15. 23 | 24 | #ifndef KUIPER_INFER_INCLUDE_TICK_HPP_ 25 | #define KUIPER_INFER_INCLUDE_TICK_HPP_ 26 | #include 27 | #include 28 | 29 | #ifndef __ycm__ 30 | #define TICK(x) auto bench_##x = std::chrono::steady_clock::now(); 31 | #define TOCK(x) \ 32 | printf("%s: %lfs\n", #x, \ 33 | std::chrono::duration_cast>( \ 34 | std::chrono::steady_clock::now() - bench_##x) \ 35 | .count()); 36 | #else 37 | #define TICK(x) 38 | #define TOCK(x) 39 | #endif 40 | #endif // KUIPER_INFER_INCLUDE_TICK_HPP_ 41 | -------------------------------------------------------------------------------- /slides/readme.md: -------------------------------------------------------------------------------- 1 | 第一次开课的视频链接: 2 | https://space.bilibili.com/1822828582 3 | 4 | 第一次开课的课件链接: 5 | 各个视频的下方简介有链接 6 | 7 | 8 | 第二次开课的视频链接: 9 | https://space.bilibili.com/1822828582 10 | 11 | 第二次开课的课件链接: 12 | https://github.com/zjhellofss/kuiperdatawhale/tree/main/slides -------------------------------------------------------------------------------- /source/data/load_data.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-21. 23 | #include "data/load_data.hpp" 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace kuiper_infer { 31 | 32 | std::pair CSVDataLoader::GetMatrixSize(std::ifstream& file, char split_char) { 33 | bool load_ok = file.good(); 34 | file.clear(); 35 | size_t fn_rows = 0; 36 | size_t fn_cols = 0; 37 | const std::ifstream::pos_type start_pos = file.tellg(); 38 | 39 | std::string token; 40 | std::string line_str; 41 | std::stringstream line_stream; 42 | 43 | while (file.good() && load_ok) { 44 | std::getline(file, line_str); 45 | if (line_str.empty()) { 46 | break; 47 | } 48 | 49 | line_stream.clear(); 50 | line_stream.str(line_str); 51 | size_t line_cols = 0; 52 | 53 | std::string row_token; 54 | while (line_stream.good()) { 55 | std::getline(line_stream, row_token, split_char); 56 | ++line_cols; 57 | } 58 | if (line_cols > fn_cols) { 59 | fn_cols = line_cols; 60 | } 61 | 62 | ++fn_rows; 63 | } 64 | file.clear(); 65 | file.seekg(start_pos); 66 | return {fn_rows, fn_cols}; 67 | } 68 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/data/tensor_utils.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 2023/3/20. 23 | #include 24 | #include "data/tensor.hpp" 25 | #include "data/tensor_util.hpp" 26 | 27 | namespace kuiper_infer {} // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/abstract/layer.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-15. 23 | #include "layer/abstract/layer.hpp" 24 | namespace kuiper_infer { 25 | 26 | const std::vector>>& Layer::weights() const { 27 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 28 | } 29 | 30 | const std::vector>>& Layer::bias() const { 31 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 32 | } 33 | 34 | void Layer::set_bias(const std::vector& bias) { 35 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 36 | } 37 | 38 | void Layer::set_bias(const std::vector>>& bias) { 39 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 40 | } 41 | 42 | void Layer::set_weights(const std::vector& weights) { 43 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 44 | } 45 | 46 | void Layer::set_weights(const std::vector>>& weights) { 47 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 48 | } 49 | 50 | StatusCode Layer::Forward(const std::vector>>& inputs, 51 | std::vector>>& outputs) { 52 | LOG(FATAL) << this->layer_name_ << " layer not implement yet!"; 53 | return StatusCode::kFunctionNotImplement; 54 | } 55 | 56 | StatusCode Layer::Forward() { 57 | LOG_IF(FATAL, this->runtime_operator_.expired()) << "Runtime operator is expired or nullptr"; 58 | const auto& runtime_operator = this->runtime_operator_.lock(); 59 | std::vector>> layer_input_datas; 60 | for (const auto& input_operand_data : runtime_operator->input_operands_seq) { 61 | if (input_operand_data == nullptr) { 62 | return StatusCode::kInferInputsEmpty; 63 | } 64 | std::copy(input_operand_data->datas.begin(), input_operand_data->datas.end(), 65 | std::back_inserter(layer_input_datas)); 66 | } 67 | 68 | if (layer_input_datas.empty()) { 69 | LOG(ERROR) << runtime_operator->name << " Layer input data is empty"; 70 | return StatusCode::kInferInputsEmpty; 71 | } 72 | 73 | for (sftensor layer_input_data : layer_input_datas) { 74 | if (layer_input_data == nullptr || layer_input_data->empty()) { 75 | LOG(ERROR) << "Layer input data is empty"; 76 | return StatusCode::kInferInputsEmpty; 77 | } 78 | } 79 | 80 | const std::shared_ptr& output_operand_datas = runtime_operator->output_operands; 81 | if (output_operand_datas == nullptr || output_operand_datas->datas.empty()) { 82 | LOG(ERROR) << "Layer output data is empty"; 83 | return StatusCode::kInferOutputsEmpty; 84 | } 85 | 86 | StatusCode status = 87 | runtime_operator->layer->Forward(layer_input_datas, output_operand_datas->datas); 88 | if (status != StatusCode::kSuccess) { 89 | LOG(ERROR) << "Forward the layer " << runtime_operator->name << " get a error status"; 90 | } 91 | return status; 92 | } 93 | 94 | StatusCode Layer::Check(const std::vector& inputs, 95 | const std::vector& outputs) { 96 | return StatusCode::kFunctionNotImplement; 97 | } 98 | 99 | void Layer::set_runtime_operator(const std::shared_ptr& runtime_operator) { 100 | CHECK(runtime_operator != nullptr); 101 | this->runtime_operator_ = runtime_operator; 102 | } 103 | 104 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/abstract/layer_factory.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-17. 23 | 24 | #include "layer/abstract/layer_factory.hpp" 25 | #include "runtime/runtime_ir.hpp" 26 | 27 | namespace kuiper_infer { 28 | LayerRegisterer::CreateRegistry* LayerRegisterer::registry_ = nullptr; 29 | 30 | void LayerRegisterer::RegisterCreator(const std::string& layer_type, const Creator& creator) { 31 | CHECK(!layer_type.empty()); 32 | CHECK(creator != nullptr); 33 | CreateRegistry* registry = Registry(); 34 | CHECK_EQ(registry->count(layer_type), 0) 35 | << "Layer type: " << layer_type << " has already registered!"; 36 | registry->insert({layer_type, creator}); 37 | } 38 | 39 | LayerRegisterer::CreateRegistry* LayerRegisterer::Registry() { 40 | if (registry_ == nullptr) { 41 | registry_ = new CreateRegistry(); 42 | static RegistryGarbageCollector c; 43 | } 44 | 45 | CHECK(registry_ != nullptr) << "Global layer register init failed!"; 46 | return registry_; 47 | } 48 | 49 | std::shared_ptr> LayerRegisterer::CreateLayer( 50 | const std::shared_ptr& op) { 51 | CreateRegistry* registry = Registry(); 52 | const std::string& layer_type = op->type; 53 | LOG_IF(FATAL, registry->count(layer_type) <= 0) << "Can not find the layer type: " << layer_type; 54 | const auto& creator = registry->find(layer_type)->second; 55 | 56 | LOG_IF(FATAL, !creator) << "Layer creator is empty!"; 57 | std::shared_ptr> layer; 58 | const auto& status = creator(op, layer); 59 | LOG_IF(FATAL, status != StatusCode::kSuccess) 60 | << "Create the layer: " << layer_type << " failed, error code: " << int32_t(status); 61 | return layer; 62 | } 63 | 64 | std::vector LayerRegisterer::layer_types() { 65 | std::set layer_types_unique; 66 | CreateRegistry* registry = Registry(); 67 | for (const auto& [layer_type, _] : *registry) { 68 | layer_types_unique.insert(layer_type); 69 | } 70 | std::vector layer_types(layer_types_unique.begin(), layer_types_unique.end()); 71 | return layer_types; 72 | } 73 | } // namespace kuiper_infer 74 | -------------------------------------------------------------------------------- /source/layer/details/activation.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by fss on 23-12-14. 3 | // 4 | #include "activation.hpp" 5 | #include "simd.hpp" 6 | namespace kuiper_infer { 7 | namespace activation { 8 | std::string ActivationTypeToString(ActivationType type) { 9 | std::string activate_type; 10 | switch (type) { 11 | case ActivationType::kActivationRelu: { 12 | activate_type = "Relu"; 13 | break; 14 | } 15 | case ActivationType::kActivationSilu: { 16 | activate_type = "Silu"; 17 | break; 18 | } 19 | case ActivationType::kActivationRelu6: { 20 | activate_type = "Relu6"; 21 | break; 22 | } 23 | case ActivationType::kActivationSigmoid: { 24 | activate_type = "Sigmoid"; 25 | break; 26 | } 27 | case ActivationType::kActivationHardSigmoid: { 28 | activate_type = "HardSigmoid"; 29 | break; 30 | } 31 | case ActivationType::kActivationHardSwish: { 32 | activate_type = "HardSwish"; 33 | break; 34 | } 35 | default: { 36 | activate_type = "Unknown"; 37 | break; 38 | } 39 | } 40 | return activate_type; 41 | } 42 | 43 | StatusCode ActivationLayer::Check(const std::vector& inputs, 44 | const std::vector& outputs) { 45 | const std::string& activation_type = ActivationTypeToString(act_type_); 46 | if (inputs.empty()) { 47 | LOG(ERROR) << "The input tensor array in the " + activation_type + " layer is empty"; 48 | return StatusCode::kInferInputsEmpty; 49 | } 50 | 51 | if (outputs.empty()) { 52 | LOG(ERROR) << "The output tensor array in the " + activation_type + " layer is empty"; 53 | return StatusCode::kInferOutputsEmpty; 54 | } 55 | 56 | if (inputs.size() != outputs.size()) { 57 | LOG(ERROR) << "The input and output tensor array size of the " + activation_type + 58 | " layer do not match"; 59 | return StatusCode::kInferDimMismatch; 60 | } 61 | return StatusCode::kSuccess; 62 | } 63 | 64 | StatusCode ActivationLayer::Forward(const std::vector>>& inputs, 65 | std::vector>>& outputs) { 66 | StatusCode check_status = Check(inputs, outputs); 67 | if (check_status != StatusCode::kSuccess) { 68 | return check_status; 69 | } 70 | 71 | const uint32_t batch_size = inputs.size(); 72 | const std::string& act_type_str = ActivationTypeToString(act_type_); 73 | ActivationFunc activation_function = ApplySSEActivation(act_type_); 74 | #pragma omp parallel for num_threads(batch_size) 75 | for (uint32_t i = 0; i < batch_size; ++i) { 76 | const std::shared_ptr>& input = inputs.at(i); 77 | CHECK(input != nullptr && !input->empty()) 78 | << "The input tensor array in the " + act_type_str + " layer has an empty tensor " << i 79 | << " th"; 80 | 81 | std::shared_ptr> output = outputs.at(i); 82 | if (output == nullptr || output->empty()) { 83 | output = std::make_shared>(input->shapes()); 84 | outputs.at(i) = output; 85 | } 86 | CHECK(output != nullptr && output->shapes() == input->shapes()) 87 | << "The input and output tensor shapes of the " + act_type_str + " layer do not match " << i 88 | << " th"; 89 | activation_function(input, output); 90 | } 91 | return StatusCode::kSuccess; 92 | } 93 | 94 | ActivationLayer::ActivationLayer(activation::ActivationType type, std::string layer_name) 95 | : NonParamLayer(std::move(layer_name)), act_type_(type) {} 96 | } // namespace activation 97 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/details/activation.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by fss on 23-12-14. 3 | // 4 | 5 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP 6 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP 7 | #include "data/tensor.hpp" 8 | #include "layer/abstract/non_param_layer.hpp" 9 | #include "status_code.hpp" 10 | namespace kuiper_infer { 11 | namespace activation { 12 | using ActivationFunc = std::function; 13 | 14 | enum class ActivationType { 15 | kActivatetionUnknown = -1, 16 | kActivationRelu = 0, 17 | kActivationSilu = 1, 18 | kActivationSigmoid = 2, 19 | kActivationHardSwish = 3, 20 | kActivationHardSigmoid = 4, 21 | kActivationRelu6 = 5, 22 | }; 23 | 24 | std::string ActivationTypeToString(ActivationType type); 25 | 26 | class ActivationLayer : public NonParamLayer { 27 | public: 28 | explicit ActivationLayer(activation::ActivationType type, std::string layer_name); 29 | 30 | StatusCode Check(const std::vector& inputs, 31 | const std::vector& outputs) override; 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | private: 37 | ActivationType act_type_ = ActivationType::kActivatetionUnknown; 38 | }; 39 | } // namespace activation 40 | } // namespace kuiper_infer 41 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP 42 | -------------------------------------------------------------------------------- /source/layer/details/adaptive_avgpooling.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_AVGPOOLING_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_AVGPOOLING_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | namespace kuiper_infer { 28 | class AdaptiveAveragePoolingLayer : public NonParamLayer { 29 | public: 30 | explicit AdaptiveAveragePoolingLayer(uint32_t output_h, uint32_t output_w); 31 | 32 | StatusCode Check(const std::vector& inputs, 33 | const std::vector& outputs) override; 34 | 35 | StatusCode Forward(const std::vector>>& inputs, 36 | std::vector>>& outputs) override; 37 | 38 | static StatusCode CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& avg_layer); 40 | 41 | private: 42 | uint32_t output_h_ = 0; 43 | uint32_t output_w_ = 0; 44 | }; 45 | } // namespace kuiper_infer 46 | #endif // KUIPER_INFER_SOURCE_LAYER_AVGPOOLING_HPP_ 47 | -------------------------------------------------------------------------------- /source/layer/details/backup/winograd.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fushenshen on 2023/3/15. 23 | 24 | #ifndef KUIPER_INFER_WINOGRAD_HPP 25 | #define KUIPER_INFER_WINOGRAD_HPP 26 | #include "data/tensor.hpp" 27 | namespace kuiper_infer { 28 | void Convolution3x3s1(const std::shared_ptr>& input, 29 | std::shared_ptr>& output, const std::vector& weights); 30 | } // namespace kuiper_infer 31 | #endif // KUIPER_INFER_WINOGRAD_HPP 32 | -------------------------------------------------------------------------------- /source/layer/details/base_convolution.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // 23 | // Created by fss on 23-10-11. 24 | // 25 | 26 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_BASE_CONVOLUTION_H 27 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_BASE_CONVOLUTION_H 28 | #include "layer/abstract/param_layer.hpp" 29 | namespace kuiper_infer { 30 | enum class ConvType { 31 | kOpConvUnknown = -1, 32 | kOpConv = 0, // 普通卷积 33 | kOpDeconv = 1, // 转置卷积 34 | }; 35 | 36 | class BaseConvolutionLayer : public ParamLayer { 37 | public: 38 | explicit BaseConvolutionLayer(ConvType conv_type, uint32_t output_channel, uint32_t in_channel, 39 | uint32_t kernel_h, uint32_t kernel_w, uint32_t padding_h, 40 | uint32_t padding_w, uint32_t stride_h, uint32_t stride_w, 41 | uint32_t groups, bool use_bias = true, 42 | uint32_t output_padding_h = 0, uint32_t output_padding_w = 0, 43 | uint32_t dilation_h = 1, uint32_t dilation_w = 1); 44 | 45 | static StatusCode CreateInstance(const std::shared_ptr& op, 46 | std::shared_ptr>& conv_layer); 47 | 48 | StatusCode Forward(const std::vector>>& inputs, 49 | std::vector>>& outputs) override; 50 | 51 | private: 52 | virtual void ComputeOutput(sftensor input, sftensor output_tensor, uint32_t kernel_h, 53 | uint32_t kernel_w, uint32_t kernel_count_group, uint32_t input_h, 54 | uint32_t input_w, uint32_t input_c_group, uint32_t output_h, 55 | uint32_t output_w, uint32_t group) const = 0; 56 | 57 | virtual std::pair ComputeOutputSize(uint32_t input_h, uint32_t input_w, 58 | uint32_t kernel_h, 59 | uint32_t kernel_w) const = 0; 60 | 61 | public: 62 | StatusCode Check(const std::vector& inputs, const std::vector& outputs); 63 | 64 | private: 65 | virtual void InitIm2ColWeight(); 66 | 67 | protected: 68 | void AddBias(arma::fmat& output, uint32_t bias_index) const; 69 | 70 | protected: 71 | uint32_t groups_ = 1; 72 | bool use_bias_ = false; 73 | 74 | uint32_t padding_h_ = 0; 75 | uint32_t padding_w_ = 0; 76 | uint32_t stride_h_ = 1; 77 | uint32_t stride_w_ = 1; 78 | 79 | uint32_t output_padding_h_ = 0; 80 | uint32_t output_padding_w_ = 0; 81 | 82 | uint32_t dilation_h_ = 1; 83 | uint32_t dilation_w_ = 1; 84 | 85 | ConvType conv_type_ = ConvType::kOpConvUnknown; 86 | std::vector kernel_matrix_arr_; 87 | }; 88 | } // namespace kuiper_infer 89 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_BASE_CONVOLUTION_H 90 | -------------------------------------------------------------------------------- /source/layer/details/batchnorm2d.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-17. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_BATCHNORM2D_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_BATCHNORM2D_HPP_ 26 | 27 | #include "layer/abstract/param_layer.hpp" 28 | #include "runtime/runtime_op.hpp" 29 | 30 | namespace kuiper_infer { 31 | 32 | class BatchNorm2dLayer : public ParamLayer { 33 | public: 34 | explicit BatchNorm2dLayer(uint32_t num_features, float eps, std::vector affine_weight, 35 | std::vector affine_bias); 36 | 37 | StatusCode Forward(const std::vector>>& inputs, 38 | std::vector>>& outputs) override; 39 | 40 | static StatusCode CreateInstance(const std::shared_ptr& op, 41 | std::shared_ptr>& batch_layer); 42 | 43 | private: 44 | float eps_ = 1e-5f; 45 | std::vector affine_weight_; 46 | std::vector affine_bias_; 47 | }; 48 | } // namespace kuiper_infer 49 | 50 | #endif // KUIPER_INFER_SOURCE_LAYER_BATCHNORM2D_HPP_ 51 | -------------------------------------------------------------------------------- /source/layer/details/cat.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-25. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_CAT_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_CAT_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | namespace kuiper_infer { 28 | class CatLayer : public NonParamLayer { 29 | public: 30 | explicit CatLayer(int32_t dim); 31 | 32 | StatusCode Forward(const std::vector>>& inputs, 33 | std::vector>>& outputs) override; 34 | 35 | StatusCode Check(const std::vector& inputs, 36 | const std::vector& outputs) override; 37 | 38 | static StatusCode CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& cat_layer); 40 | 41 | private: 42 | int32_t dim_ = 0; 43 | }; 44 | } // namespace kuiper_infer 45 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_CAT_HPP_ 46 | -------------------------------------------------------------------------------- /source/layer/details/convolution.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-13. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_CONVOLUTION_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_CONVOLUTION_HPP_ 26 | #include "base_convolution.hpp" 27 | #include "layer/abstract/param_layer.hpp" 28 | 29 | namespace kuiper_infer { 30 | 31 | class ConvolutionLayer : public BaseConvolutionLayer { 32 | public: 33 | explicit ConvolutionLayer(uint32_t output_channel, uint32_t in_channel, uint32_t kernel_h, 34 | uint32_t kernel_w, uint32_t padding_h, uint32_t padding_w, 35 | uint32_t stride_h, uint32_t stride_w, uint32_t groups, 36 | bool use_bias = true, uint32_t output_padding_h = 0, 37 | uint32_t output_padding_w = 0, uint32_t dilation_h = 1, 38 | uint32_t dilation_w = 1) 39 | : BaseConvolutionLayer(ConvType::kOpConv, output_channel, in_channel, kernel_h, kernel_w, 40 | padding_h, padding_w, stride_h, stride_w, groups, use_bias, 41 | output_padding_h, output_padding_w, dilation_h, dilation_w) {} 42 | 43 | private: 44 | bool Is1x1KernelNoPadding(uint32_t kernel_h, uint32_t kernel_w) const; 45 | 46 | void InitIm2ColWeight() override; 47 | 48 | void ComputeOutput(sftensor input, sftensor output_tensor, uint32_t kernel_h, uint32_t kernel_w, 49 | uint32_t kernel_count_group, uint32_t input_h, uint32_t input_w, 50 | uint32_t channels_per_group, uint32_t output_h, uint32_t output_w, 51 | uint32_t group) const override; 52 | 53 | std::pair ComputeOutputSize(uint32_t input_h, uint32_t input_w, 54 | uint32_t kernel_h, 55 | uint32_t kernel_w) const override; 56 | 57 | void ConvGEMMBias(const arma::fmat& input_matrix, sftensor output_tensor, uint32_t group, 58 | uint32_t kernel_index, uint32_t kernel_count_group, uint32_t output_h, 59 | uint32_t output_w, bool is_1x1conv_nopadding) const; 60 | 61 | [[nodiscard]] arma::fmat ConvIm2Col(sftensor input, uint32_t kernel_h, uint32_t kernel_w, 62 | uint32_t input_h, uint32_t input_w, 63 | uint32_t channels_per_group, uint32_t output_h, 64 | uint32_t output_w, uint32_t group, uint32_t row_len, 65 | uint32_t col_len) const; 66 | }; 67 | 68 | } // namespace kuiper_infer 69 | 70 | #endif // KUIPER_INFER_SOURCE_LAYER_CONVOLUTION_HPP_ 71 | -------------------------------------------------------------------------------- /source/layer/details/deconvolution.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // 23 | // Created by fss on 23-10-11. 24 | // 25 | 26 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_DECONVOLUTION_H 27 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_DECONVOLUTION_H 28 | #include "base_convolution.hpp" 29 | #include "data/tensor.hpp" 30 | namespace kuiper_infer { 31 | class DeconvolutionLayer : public BaseConvolutionLayer { 32 | public: 33 | explicit DeconvolutionLayer(uint32_t output_channel, uint32_t in_channel, uint32_t kernel_h, 34 | uint32_t kernel_w, uint32_t padding_h, uint32_t padding_w, 35 | uint32_t stride_h, uint32_t stride_w, uint32_t groups, 36 | bool use_bias = true, uint32_t output_padding_h = 0, 37 | uint32_t output_padding_w = 0, uint32_t dilation_h = 1, 38 | uint32_t dilation_w = 1) 39 | : BaseConvolutionLayer(ConvType::kOpDeconv, output_channel, in_channel, kernel_h, kernel_w, 40 | padding_h, padding_w, stride_h, stride_w, groups, use_bias, 41 | output_padding_h, output_padding_w, dilation_h, dilation_w) {} 42 | 43 | void set_weights(const std::vector& weights) override; 44 | 45 | void set_weights(const std::vector>>& weights) override; 46 | 47 | private: 48 | void ComputeOutput(sftensor input, sftensor output_tensor, uint32_t kernel_h, uint32_t kernel_w, 49 | uint32_t kernel_count_group, uint32_t input_h, uint32_t input_w, 50 | uint32_t channels_per_group, uint32_t output_h, uint32_t output_w, 51 | uint32_t group) const override; 52 | 53 | std::pair ComputeOutputSize(uint32_t input_h, uint32_t input_w, 54 | uint32_t kernel_h, 55 | uint32_t kernel_w) const override; 56 | 57 | void DeconvCol2ImBias(const arma::fmat& gemm_result, sftensor output_tensor, uint32_t input_h, 58 | uint32_t input_w, uint32_t group, uint32_t kernel_index, 59 | uint32_t kernel_count_group, uint32_t kernel_h, uint32_t kernel_w, 60 | uint32_t output_h, uint32_t output_w) const; 61 | 62 | [[nodiscard]] arma::fmat DeconvGEMM(const sftensor& input, uint32_t input_h, uint32_t input_w, 63 | uint32_t channels_per_group, uint32_t group, 64 | uint32_t kernel_index, uint32_t kernel_count_group) const; 65 | }; 66 | } // namespace kuiper_infer 67 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_DECONVOLUTION_H 68 | -------------------------------------------------------------------------------- /source/layer/details/expression.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-18. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_MONOCULAR_EXPRESSION_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_MONOCULAR_EXPRESSION_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | #include "parser/parse_expression.hpp" 28 | 29 | namespace kuiper_infer { 30 | class ExpressionLayer : public NonParamLayer { 31 | public: 32 | explicit ExpressionLayer(std::string statement); 33 | 34 | StatusCode Forward(const std::vector>>& inputs, 35 | std::vector>>& outputs) override; 36 | 37 | bool TokenIsOperator(Token token) const; 38 | 39 | static StatusCode CreateInstance(const std::shared_ptr& op, 40 | std::shared_ptr>& expression_layer); 41 | 42 | private: 43 | std::string statement_; 44 | std::unique_ptr parser_; 45 | }; 46 | } // namespace kuiper_infer 47 | #endif // KUIPER_INFER_SOURCE_LAYER_MONOCULAR_EXPRESSION_HPP_ 48 | -------------------------------------------------------------------------------- /source/layer/details/flatten.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-9. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_FLATTEN_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_FLATTEN_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | namespace kuiper_infer { 28 | class FlattenLayer : public NonParamLayer { 29 | public: 30 | explicit FlattenLayer(int32_t start_dim, int32_t end_dim); 31 | 32 | StatusCode Forward(const std::vector>>& inputs, 33 | std::vector>>& outputs) override; 34 | 35 | static StatusCode CreateInstance(const std::shared_ptr& op, 36 | std::shared_ptr>& flatten_layer); 37 | 38 | private: 39 | int32_t start_dim_ = 0; 40 | int32_t end_dim_ = 0; 41 | }; 42 | } // namespace kuiper_infer 43 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_FLATTEN_HPP_ 44 | -------------------------------------------------------------------------------- /source/layer/details/hardsigmoid.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-12. 23 | #include "hardsigmoid.hpp" 24 | #include "layer/abstract/layer_factory.hpp" 25 | #include "simd.hpp" 26 | 27 | namespace kuiper_infer { 28 | using namespace activation; 29 | HardSigmoid::HardSigmoid() 30 | : ActivationLayer(ActivationType::kActivationHardSigmoid, "HardSigmoid") {} 31 | 32 | StatusCode HardSigmoid::Forward(const std::vector>>& inputs, 33 | std::vector>>& outputs) { 34 | using namespace activation; 35 | return ActivationLayer::Forward(inputs, outputs); 36 | } 37 | 38 | StatusCode HardSigmoid::CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& hardsigmoid_layer) { 40 | if (!op) { 41 | LOG(ERROR) << "The hardsigmoid operator parameter in the layer is null pointer."; 42 | return StatusCode::kParseNullOperator; 43 | } 44 | hardsigmoid_layer = std::make_shared(); 45 | return StatusCode::kSuccess; 46 | } 47 | 48 | LayerRegistererWrapper kHardSigmoidCreateInstance(HardSigmoid::CreateInstance, "nn.Hardsigmoid"); 49 | 50 | } // namespace kuiper_infer 51 | -------------------------------------------------------------------------------- /source/layer/details/hardsigmoid.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSIGMOID_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSIGMOID_HPP_ 26 | #include "activation.hpp" 27 | #include "layer/abstract/non_param_layer.hpp" 28 | namespace kuiper_infer { 29 | class HardSigmoid : public activation::ActivationLayer { 30 | public: 31 | explicit HardSigmoid(); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& hardsigmoid_layer); 38 | }; 39 | } // namespace kuiper_infer 40 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSIGMOID_HPP_ 41 | -------------------------------------------------------------------------------- /source/layer/details/hardswish.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-12. 23 | #include "hardswish.hpp" 24 | #include "layer/abstract/layer_factory.hpp" 25 | #include "simd.hpp" 26 | 27 | namespace kuiper_infer { 28 | using namespace activation; 29 | HardSwishLayer::HardSwishLayer() 30 | : ActivationLayer(ActivationType::kActivationHardSwish, "HardSwish") {} 31 | 32 | StatusCode HardSwishLayer::Forward(const std::vector>>& inputs, 33 | std::vector>>& outputs) { 34 | using namespace activation; 35 | return ActivationLayer::Forward(inputs, outputs); 36 | } 37 | 38 | StatusCode HardSwishLayer::CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& hardswish_layer) { 40 | if (!op) { 41 | LOG(ERROR) << "The hardswish operator parameter in the layer is null pointer."; 42 | return StatusCode::kParseNullOperator; 43 | } 44 | hardswish_layer = std::make_shared(); 45 | return StatusCode::kSuccess; 46 | } 47 | 48 | LayerRegistererWrapper kHardSwishCreateInstance(HardSwishLayer::CreateInstance, "nn.Hardswish"); 49 | 50 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/details/hardswish.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSWISH_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSWISH_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | #include "activation.hpp" 28 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_HARDSWISH_HPP_ 29 | namespace kuiper_infer { 30 | class HardSwishLayer : public activation::ActivationLayer { 31 | public: 32 | explicit HardSwishLayer(); 33 | 34 | StatusCode Forward(const std::vector>>& inputs, 35 | std::vector>>& outputs) override; 36 | 37 | static StatusCode CreateInstance(const std::shared_ptr& op, 38 | std::shared_ptr>& hardswish_layer); 39 | }; 40 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/details/linear.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-13. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_LINEAR_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_LINEAR_HPP_ 26 | #include "layer/abstract/layer.hpp" 27 | #include "layer/abstract/param_layer.hpp" 28 | 29 | namespace kuiper_infer { 30 | class LinearLayer : public ParamLayer { 31 | public: 32 | // explicit LinearLayer(uint32_t batch, uint32_t in_channel, uint32_t in_dim, uint32_t out_dim, 33 | // bool use_bias = true); 34 | explicit LinearLayer(int32_t in_features, int32_t out_features, bool use_bias); 35 | 36 | StatusCode Check(const std::vector& inputs, 37 | const std::vector& outputs) override; 38 | 39 | StatusCode Forward(const std::vector>>& inputs, 40 | std::vector>>& outputs) override; 41 | 42 | static StatusCode CreateInstance(const std::shared_ptr& op, 43 | std::shared_ptr>& linear_layer); 44 | 45 | void set_weights(const std::vector& weights) override; 46 | 47 | void set_weights(const std::vector>>& weights) override; 48 | 49 | private: 50 | int32_t in_features_ = 0; 51 | int32_t out_features_ = 0; 52 | bool use_bias_ = false; 53 | }; 54 | } // namespace kuiper_infer 55 | 56 | #endif // KUIPER_INFER_SOURCE_LAYER_LINEAR_HPP_ 57 | -------------------------------------------------------------------------------- /source/layer/details/matmul.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // 23 | // Created by fss on 24-2-9. 24 | // 25 | 26 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_MATMUL_HPP 27 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_MATMUL_HPP 28 | #include "layer/abstract/param_layer.hpp" 29 | namespace kuiper_infer { 30 | class LLamaMatmulLayer : public ParamLayer { 31 | public: 32 | explicit LLamaMatmulLayer(int32_t weight_dim0, int32_t weight_dim1); 33 | 34 | StatusCode Forward(const std::vector>>& inputs, 35 | std::vector>>& outputs) override; 36 | 37 | void set_weights(const std::vector>>& weights) override; 38 | 39 | void set_weights(const std::vector& weights) override; 40 | 41 | private: 42 | int32_t weight_dim0_ = 0; 43 | int32_t weight_dim1_ = 0; 44 | }; 45 | } // namespace kuiper_infer 46 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_MATMUL_HPP 47 | -------------------------------------------------------------------------------- /source/layer/details/maxpooling.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_MAX_POOLING_ 25 | #define KUIPER_INFER_SOURCE_LAYER_MAX_POOLING_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | namespace kuiper_infer { 28 | class MaxPoolingLayer : public NonParamLayer { 29 | public: 30 | explicit MaxPoolingLayer(uint32_t padding_h, uint32_t padding_w, uint32_t pooling_size_h, 31 | uint32_t pooling_size_w, uint32_t stride_h, uint32_t stride_w); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | StatusCode Check(const std::vector& inputs, 37 | const std::vector& outputs) override; 38 | 39 | static StatusCode CreateInstance(const std::shared_ptr& op, 40 | std::shared_ptr>& max_layer); 41 | 42 | private: 43 | uint32_t padding_h_ = 0; 44 | uint32_t padding_w_ = 0; 45 | uint32_t pooling_size_h_ = 0; 46 | uint32_t pooling_size_w_ = 0; 47 | uint32_t stride_h_ = 1; 48 | uint32_t stride_w_ = 1; 49 | }; 50 | } // namespace kuiper_infer 51 | #endif // KUIPER_INFER_SOURCE_LAYER_MAX_POOLING_ 52 | -------------------------------------------------------------------------------- /source/layer/details/relu.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-18. 23 | #include "relu.hpp" 24 | #include "layer/abstract/layer_factory.hpp" 25 | 26 | namespace kuiper_infer { 27 | using namespace activation; 28 | ReluLayer::ReluLayer() : ActivationLayer(ActivationType::kActivationRelu, "nn.ReLU") {} 29 | 30 | StatusCode ReluLayer::Forward(const std::vector>>& inputs, 31 | std::vector>>& outputs) { 32 | return ActivationLayer::Forward(inputs, outputs); 33 | } 34 | 35 | StatusCode ReluLayer::CreateInstance(const std::shared_ptr& op, 36 | std::shared_ptr>& relu_layer) { 37 | if (!op) { 38 | LOG(ERROR) << "The relu operator parameter in the layer is null pointer."; 39 | return StatusCode::kParseNullOperator; 40 | } 41 | 42 | relu_layer = std::make_shared(); 43 | return StatusCode::kSuccess; 44 | } 45 | 46 | LayerRegistererWrapper kReluCreateInstance(ReluLayer::CreateInstance, "nn.ReLU"); 47 | } // namespace kuiper_infer 48 | -------------------------------------------------------------------------------- /source/layer/details/relu.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-18. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU_HPP_ 26 | #include "activation.hpp" 27 | #include "layer/abstract/non_param_layer.hpp" 28 | namespace kuiper_infer { 29 | class ReluLayer : public activation::ActivationLayer { 30 | public: 31 | explicit ReluLayer(); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& relu_layer); 38 | }; 39 | } // namespace kuiper_infer 40 | #endif // KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU_HPP_ 41 | -------------------------------------------------------------------------------- /source/layer/details/relu6.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-18. 23 | #include "relu6.hpp" 24 | #include "layer/abstract/layer_factory.hpp" 25 | #include "simd.hpp" 26 | 27 | namespace kuiper_infer { 28 | using namespace activation; 29 | StatusCode Relu6Layer::Forward(const std::vector>>& inputs, 30 | std::vector>>& outputs) { 31 | return ActivationLayer::Forward(inputs, outputs); 32 | } 33 | StatusCode Relu6Layer::CreateInstance(const std::shared_ptr& op, 34 | std::shared_ptr>& relu_layer) { 35 | if (!op) { 36 | LOG(ERROR) << "The relu6 operator parameter in the layer is null pointer."; 37 | return StatusCode::kParseNullOperator; 38 | } 39 | 40 | relu_layer = std::make_shared(); 41 | return StatusCode::kSuccess; 42 | } 43 | 44 | Relu6Layer::Relu6Layer() : ActivationLayer(ActivationType::kActivationRelu6, "nn.ReLU6") {} 45 | 46 | LayerRegistererWrapper kRelu6CreateInstance(Relu6Layer::CreateInstance, "nn.ReLU6"); 47 | } // namespace kuiper_infer 48 | -------------------------------------------------------------------------------- /source/layer/details/relu6.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-18. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU6_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU6_HPP_ 26 | #include "activation.hpp" 27 | #include "layer/abstract/non_param_layer.hpp" 28 | namespace kuiper_infer { 29 | class Relu6Layer : public activation::ActivationLayer { 30 | public: 31 | explicit Relu6Layer(); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& relu_layer); 38 | }; 39 | } // namespace kuiper_infer 40 | #endif // KUIPER_INFER_SOURCE_LAYER_BINOCULAR_RELU_HPP_ 41 | -------------------------------------------------------------------------------- /source/layer/details/rms_norm.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // 23 | // Created by fss on 24-2-9. 24 | // 25 | 26 | #include "rms_norm.hpp" 27 | namespace kuiper_infer { 28 | RMSNormLayer::RMSNormLayer() : ParamLayer("rms_norm") { this->weights_.resize(1); } 29 | 30 | void RMSNormLayer::set_weights(const std::vector& weights) { 31 | LOG(FATAL) << "This function is not implement in the RMSNorm layer!"; 32 | } 33 | 34 | void RMSNormLayer::set_weights(const std::vector>>& weights) { 35 | CHECK(weights.size() == weights_.size()); 36 | this->weights_ = weights; 37 | } 38 | 39 | StatusCode RMSNormLayer::Forward(const std::vector>>& inputs, 40 | std::vector>>& outputs) { 41 | if (inputs.empty()) { 42 | LOG(ERROR) << "The input tensor array in the rmsnorm layer is empty"; 43 | return StatusCode::kInferInputsEmpty; 44 | } 45 | 46 | if (outputs.empty()) { 47 | LOG(ERROR) << "The output tensor array in the rmsnorm layer is empty"; 48 | return StatusCode::kInferOutputsEmpty; 49 | } 50 | 51 | if (inputs.size() != outputs.size()) { 52 | LOG(ERROR) << "The input and output tensor array size of the rmsnorm " 53 | "layer do not match"; 54 | return StatusCode::kInferDimMismatch; 55 | } 56 | 57 | if (weights_.empty() || weights_.front()->empty()) { 58 | LOG(ERROR) << "The weight for the rmsnorm layer is missing."; 59 | return StatusCode::kInferParamError; 60 | } 61 | 62 | std::shared_ptr> weight = this->weight(0); 63 | arma::fvec weight_vec(weight->raw_ptr(), weight->size(), false, true); 64 | const uint32_t batch_size = inputs.size(); 65 | #pragma omp parallel for if (batch_size > 1) num_threads(batch_size) 66 | for (uint32_t i = 0; i < batch_size; ++i) { 67 | const auto& input = inputs.at(i); 68 | CHECK(input != nullptr && !input->empty()) 69 | << "The input tensor array in the rmsnorm layer has an " 70 | "empty tensor " 71 | << i << " th"; 72 | 73 | std::shared_ptr> output = outputs.at(i); 74 | if (output == nullptr || output->empty()) { 75 | output = std::make_shared>(input->shapes()); 76 | outputs.at(i) = output; 77 | } 78 | CHECK(output->shapes() == input->shapes()) 79 | << "The input and output tensor shapes of the rmsnorm " 80 | "layer do not match " 81 | << i << " th"; 82 | 83 | const size_t size = input->size(); 84 | arma::fvec input_vec(input->raw_ptr(), size, false, true); 85 | 86 | const arma::fvec& input_pow_vec = arma::pow(input_vec, 2.f); 87 | const float mean_value = arma::mean(input_pow_vec); 88 | const float norm_value = 1.f / std::sqrt(mean_value + eps_); 89 | arma::fvec output_vec(output->raw_ptr(), size, false, true); 90 | output_vec = weight_vec % (norm_value * input_vec); 91 | } 92 | return StatusCode::kSuccess; 93 | } 94 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/details/rms_norm.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // 23 | // Created by fss on 24-2-9. 24 | // 25 | 26 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_RMS_NORM_HPP 27 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_RMS_NORM_HPP 28 | #include "layer/abstract/param_layer.hpp" 29 | #include "runtime/runtime_op.hpp" 30 | namespace kuiper_infer { 31 | class RMSNormLayer : public ParamLayer { 32 | public: 33 | explicit RMSNormLayer(); 34 | 35 | StatusCode Forward(const std::vector>>& inputs, 36 | std::vector>>& outputs) override; 37 | 38 | void set_weights(const std::vector>>& weights) override; 39 | 40 | void set_weights(const std::vector& weights) override; 41 | private: 42 | float eps_ = 1e-5f; 43 | }; 44 | } // namespace kuiper_infer 45 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_RMS_NORM_HPP 46 | -------------------------------------------------------------------------------- /source/layer/details/sigmoid.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-13. 23 | 24 | #include "sigmoid.hpp" 25 | #include 26 | #include "layer/abstract/layer_factory.hpp" 27 | #include "simd.hpp" 28 | 29 | namespace kuiper_infer { 30 | using namespace activation; 31 | SigmoidLayer::SigmoidLayer() : ActivationLayer(ActivationType::kActivationSigmoid, "nn.Sigmoid") {} 32 | 33 | StatusCode SigmoidLayer::Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) { 35 | using namespace activation; 36 | return ActivationLayer::Forward(inputs, outputs); 37 | } 38 | 39 | StatusCode SigmoidLayer::CreateInstance(const std::shared_ptr& op, 40 | std::shared_ptr>& sigmoid_layer) { 41 | if (!op) { 42 | LOG(ERROR) << "The sigmoid operator parameter in the layer is null pointer."; 43 | return StatusCode::kParseNullOperator; 44 | } 45 | sigmoid_layer = std::make_shared(); 46 | return StatusCode::kSuccess; 47 | } 48 | 49 | LayerRegistererWrapper kSigmoidCreateInstance(SigmoidLayer::CreateInstance, "nn.Sigmoid"); 50 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /source/layer/details/sigmoid.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-13. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_SIGMOID_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_SIGMOID_HPP_ 26 | #include "activation.hpp" 27 | #include "layer/abstract/non_param_layer.hpp" 28 | namespace kuiper_infer { 29 | class SigmoidLayer : public activation::ActivationLayer { 30 | public: 31 | explicit SigmoidLayer(); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& sigmoid_layer); 38 | }; 39 | } // namespace kuiper_infer 40 | 41 | #endif // KUIPER_INFER_SOURCE_LAYER_SIGMOID_HPP_ 42 | -------------------------------------------------------------------------------- /source/layer/details/silu.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-25. 23 | 24 | #include "silu.hpp" 25 | #include "activation.hpp" 26 | #include "layer/abstract/layer_factory.hpp" 27 | #include "simd.hpp" 28 | #include "tick.hpp" 29 | 30 | namespace kuiper_infer { 31 | using namespace activation; 32 | StatusCode SiLULayer::Forward(const std::vector>>& inputs, 33 | std::vector>>& outputs) { 34 | using namespace activation; 35 | return ActivationLayer::Forward(inputs, outputs); 36 | } 37 | 38 | StatusCode SiLULayer::CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& silu_layer) { 40 | if (!op) { 41 | LOG(ERROR) << "The SiLU operator parameter in the layer is null pointer."; 42 | return StatusCode::kParseNullOperator; 43 | } 44 | silu_layer = std::make_shared(); 45 | return StatusCode::kSuccess; 46 | } 47 | 48 | SiLULayer::SiLULayer() : ActivationLayer(ActivationType::kActivationSilu, "nn.SiLU") {} 49 | 50 | LayerRegistererWrapper kSiluCreateInstance(SiLULayer::CreateInstance, "nn.SiLU"); 51 | 52 | } // namespace kuiper_infer 53 | -------------------------------------------------------------------------------- /source/layer/details/silu.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-25. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_SILU_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_SILU_HPP_ 26 | #include "activation.hpp" 27 | #include "layer/abstract/non_param_layer.hpp" 28 | namespace kuiper_infer { 29 | class SiLULayer : public activation::ActivationLayer { 30 | public: 31 | explicit SiLULayer(); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& silu_layer); 38 | }; 39 | } // namespace kuiper_infer 40 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_SILU_HPP_ 41 | -------------------------------------------------------------------------------- /source/layer/details/simd.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | // 22 | // Created by fss on 23-7-26. 23 | // 24 | 25 | #ifndef KUIPER_INFER_INCLUDE_MATH_ARMA_SSE 26 | #define KUIPER_INFER_INCLUDE_MATH_ARMA_SSE 27 | #include "activation.hpp" 28 | namespace kuiper_infer { 29 | namespace activation { 30 | ActivationFunc ApplySSEActivation(ActivationType act_type); 31 | 32 | } // namespace activation 33 | } // namespace kuiper_infer 34 | #endif // KUIPER_INFER_INCLUDE_MATH_ARMA_SSE 35 | -------------------------------------------------------------------------------- /source/layer/details/softmax.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-13. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_SOFTMAX_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_SOFTMAX_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | 28 | namespace kuiper_infer { 29 | class SoftmaxLayer : public NonParamLayer { 30 | public: 31 | explicit SoftmaxLayer(int32_t dim = -1); 32 | 33 | StatusCode Forward(const std::vector>>& inputs, 34 | std::vector>>& outputs) override; 35 | 36 | static StatusCode CreateInstance(const std::shared_ptr& op, 37 | std::shared_ptr>& softmax_layer); 38 | 39 | private: 40 | int32_t softmax_dim_ = -1; 41 | }; 42 | } // namespace kuiper_infer 43 | 44 | #endif // KUIPER_INFER_SOURCE_LAYER_SOFTMAX_HPP_ 45 | -------------------------------------------------------------------------------- /source/layer/details/upsample.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-25. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_UPSAMPLE_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_UPSAMPLE_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | 28 | namespace kuiper_infer { 29 | enum class UpSampleMode { 30 | kModeNearest = 0, 31 | kModeBilinear = 1, // 目前上采样支持这两种 32 | }; 33 | 34 | class UpSampleLayer : public NonParamLayer { 35 | public: 36 | explicit UpSampleLayer(float scale_h, float scale_w, 37 | UpSampleMode mode = UpSampleMode::kModeNearest, 38 | bool is_align_scale = false); 39 | 40 | StatusCode Forward(const std::vector>>& inputs, 41 | std::vector>>& outputs) override; 42 | 43 | static StatusCode CreateInstance(const std::shared_ptr& op, 44 | std::shared_ptr>& upsample_layer); 45 | 46 | private: 47 | float scale_h_ = 1.f; 48 | float scale_w_ = 1.f; 49 | bool is_align_corner_ = false; 50 | UpSampleMode mode_ = UpSampleMode::kModeNearest; 51 | }; 52 | } // namespace kuiper_infer 53 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_UPSAMPLE_HPP_ 54 | -------------------------------------------------------------------------------- /source/layer/details/view.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-12. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_FLATTEN_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_FLATTEN_HPP_ 26 | #include "layer/abstract/non_param_layer.hpp" 27 | namespace kuiper_infer { 28 | class ViewLayer : public NonParamLayer { 29 | public: 30 | explicit ViewLayer(std::vector shapes); 31 | 32 | StatusCode Check(const std::vector& inputs, 33 | const std::vector& outputs) override; 34 | 35 | StatusCode Forward(const std::vector>>& inputs, 36 | std::vector>>& outputs) override; 37 | 38 | static StatusCode CreateInstance(const std::shared_ptr& op, 39 | std::shared_ptr>& view_layer); 40 | 41 | private: 42 | std::vector shapes_; 43 | }; 44 | } // namespace kuiper_infer 45 | #endif // KUIPER_INFER_SOURCE_LAYER_FLATTEN_HPP_ 46 | -------------------------------------------------------------------------------- /source/layer/details/yolo_detect.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-26. 23 | 24 | #ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_YOLO_DETECT_HPP_ 25 | #define KUIPER_INFER_SOURCE_LAYER_DETAILS_YOLO_DETECT_HPP_ 26 | #include "convolution.hpp" 27 | #include "layer/abstract/layer.hpp" 28 | 29 | namespace kuiper_infer { 30 | class YoloDetectLayer : public Layer { 31 | public: 32 | explicit YoloDetectLayer(int32_t stages, int32_t num_classes, int32_t num_anchors, 33 | std::vector strides, std::vector anchor_grids, 34 | std::vector grids, 35 | std::vector> conv_layers); 36 | 37 | StatusCode Forward(const std::vector>>& inputs, 38 | std::vector>>& outputs) override; 39 | 40 | static StatusCode CreateInstance(const std::shared_ptr& op, 41 | std::shared_ptr>& yolo_detect_layer); 42 | 43 | private: 44 | int32_t stages_ = 0; 45 | int32_t num_classes_ = 0; 46 | int32_t num_anchors_ = 0; 47 | std::vector strides_; 48 | std::vector anchor_grids_; 49 | std::vector grids_; 50 | std::vector> conv_layers_; 51 | }; 52 | } // namespace kuiper_infer 53 | #endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_YOLO_DETECT_HPP_ 54 | -------------------------------------------------------------------------------- /source/readme.txt: -------------------------------------------------------------------------------- 1 | ├── data 张量结构、数据加载 2 | │   ├── load_data.cpp 3 | │   ├── tensor.cpp 4 | │   └── tensor_utils.cpp 5 | ├── layer 6 | │   ├── abstract 抽象算子结构(各算子的父类)、算子注册工厂 7 | │   │   ├── layer.cpp 8 | │   │   ├── layer_factory.cpp 9 | │   │   └── param_layer.cpp 10 | │   └── details 各个算子的实现 11 | │   ├── adaptive_avgpooling.cpp 12 | │   ├── adaptive_avgpooling.hpp 13 | │   ├── batchnorm2d.cpp 14 | │   ├── batchnorm2d.hpp 15 | │   ├── cat.cpp 16 | │   ├── cat.hpp 17 | │   ├── convolution.cpp 18 | │   ├── convolution.hpp 19 | │   ├── expression.cpp 20 | │   ├── expression.hpp 21 | │   ├── flatten.cpp 22 | │   ├── flatten.hpp 23 | │   ├── hardsigmoid.cpp 24 | │   ├── hardsigmoid.hpp 25 | │   ├── hardswish.cpp 26 | │   ├── hardswish.hpp 27 | │   ├── linear.cpp 28 | │   ├── linear.hpp 29 | │   ├── maxpooling.cpp 30 | │   ├── maxpooling.hpp 31 | │   ├── platform.hpp 32 | │   ├── relu.cpp 33 | │   ├── relu.hpp 34 | │   ├── sigmoid.cpp 35 | │   ├── sigmoid.hpp 36 | │   ├── silu.cpp 37 | │   ├── silu.hpp 38 | │   ├── softmax.cpp 39 | │   ├── softmax.hpp 40 | │   ├── sse_mathfun.hpp 41 | │   ├── upsample.cpp 42 | │   ├── upsample.hpp 43 | │   ├── view.cpp 44 | │   ├── view.hpp 45 | │   ├── winograd.cpp 46 | │   ├── winograd.hpp 47 | │   ├── x86_usability.hpp 48 | │   ├── yolo_detect.cpp 49 | │   └── yolo_detect.hpp 50 | ├── parser 表达式层的解析 51 | │   └── parse_expression.cpp 52 | ├── runtime 计算图的定义、运行时相关的流程 53 | │   ├── ir.cpp 54 | │   ├── runtime_ir.cpp 55 | │   ├── runtime_op.cpp 56 | │   └── store_zip.cpp 57 | └── utils 算子的运行时间统计 58 | └── time_logging.cpp 59 | -------------------------------------------------------------------------------- /source/utils/time/time_logging.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-4-27. 23 | 24 | #include "utils/time/time_logging.hpp" 25 | #include 26 | #include "layer/abstract/layer_factory.hpp" 27 | 28 | namespace kuiper_infer { 29 | namespace utils { 30 | PtrLayerTimeStatesCollector LayerTimeStatesSingleton::SingletonInstance() { 31 | std::lock_guard lock_(mutex_); 32 | if (time_states_collector_ == nullptr) { 33 | time_states_collector_ = std::make_shared(); 34 | } 35 | return time_states_collector_; 36 | } 37 | 38 | void LayerTimeStatesSingleton::LayerTimeStatesCollectorInit() { 39 | if (time_states_collector_ != nullptr) { 40 | std::lock_guard lock_(mutex_); 41 | time_states_collector_.reset(); 42 | time_states_collector_ = nullptr; 43 | } 44 | time_states_collector_ = LayerTimeStatesSingleton::SingletonInstance(); 45 | } 46 | 47 | std::mutex LayerTimeStatesSingleton::mutex_; 48 | 49 | PtrLayerTimeStatesCollector LayerTimeStatesSingleton::time_states_collector_; 50 | 51 | LayerTimeLogging::LayerTimeLogging(std::string layer_name, std::string layer_type) 52 | : layer_name_(std::move(layer_name)), 53 | layer_type_(std::move(layer_type)), 54 | start_time_(Time::now()) { 55 | auto layer_time_states = LayerTimeStatesSingleton::SingletonInstance(); 56 | layer_time_states->insert( 57 | {layer_name_, std::make_shared(0l, layer_name_, layer_type_)}); 58 | } 59 | 60 | LayerTimeLogging::~LayerTimeLogging() { 61 | auto layer_time_states = LayerTimeStatesSingleton::SingletonInstance(); 62 | const auto layer_state_iter = layer_time_states->find(layer_name_); 63 | if (layer_state_iter != layer_time_states->end()) { 64 | auto& layer_state = layer_state_iter->second; 65 | CHECK(layer_state != nullptr); 66 | 67 | std::lock_guard lock_guard(layer_state->time_mutex_); 68 | const auto end_time = Time::now(); 69 | const auto duration = 70 | std::chrono::duration_cast(end_time - start_time_).count(); 71 | layer_state->duration_time_ += duration; 72 | } else { 73 | LOG(ERROR) << "Can not find the layer: " << layer_name_ << " in the time logging."; 74 | } 75 | } 76 | 77 | void LayerTimeLogging::SummaryLogging() { 78 | auto layer_time_states = LayerTimeStatesSingleton::SingletonInstance(); 79 | CHECK(layer_time_states != nullptr); 80 | LayerTimeStatesCollector layer_time_states_collector = *layer_time_states.get(); 81 | 82 | long total_time_costs = 0; 83 | for (const auto& [layer_name, layer_time_state] : layer_time_states_collector) { 84 | CHECK(layer_time_state != nullptr); 85 | 86 | std::lock_guard lock(layer_time_state->time_mutex_); 87 | const auto time_cost = layer_time_state->duration_time_; 88 | total_time_costs += time_cost; 89 | if (layer_time_state->duration_time_ != 0) { 90 | LOG(INFO) << "Layer name: " << layer_name << "\t" 91 | << "layer type: " << layer_time_state->layer_type_ << "\t" 92 | << "time cost: " << time_cost << "ms"; 93 | } 94 | } 95 | LOG(INFO) << "Total time: " << total_time_costs << "ms"; 96 | } 97 | 98 | } // namespace utils 99 | } // namespace kuiper_infer -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package(GTest REQUIRED) 2 | #find_package(OpenMP REQUIRED) 3 | find_package(glog REQUIRED) 4 | find_package(Armadillo REQUIRED) 5 | find_package(BLAS REQUIRED) 6 | find_package(LAPACK REQUIRED) 7 | 8 | aux_source_directory(../test/test_data DIR_TEST_DATA) 9 | aux_source_directory(../test/test_layer DIR_TEST_LAYER) 10 | aux_source_directory(../test/test_net DIR_TEST_NET) 11 | aux_source_directory(../test/test_runtime DIR_TEST_RUNTIME) 12 | 13 | set(link_lib glog::glog GTest::gtest) 14 | if (!WIN32) 15 | set(link_lib "${link_lib} pthread") 16 | endif () 17 | set(link_math_lib ${ARMADILLO_LIBRARIES} ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES}) 18 | 19 | add_executable(test_kuiper test_main.cpp ${DIR_TEST_DATA} ${DIR_TEST_LAYER} ${DIR_TEST_NET} ${DIR_TEST_RUNTIME}) 20 | 21 | target_link_libraries(test_kuiper ${link_lib} ${link_math_lib}) 22 | target_link_directories(test_kuiper PUBLIC ${PROJECT_SOURCE_DIR}/lib) 23 | target_link_libraries(test_kuiper kuiper) 24 | 25 | if (MSVC) 26 | add_custom_command(TARGET test_kuiper POST_BUILD 27 | COMMAND ${CMAKE_COMMAND} -E copy_if_different 28 | "$/kuiper.dll" 29 | $) 30 | endif () 31 | 32 | target_include_directories(test_kuiper PUBLIC ${glog_INCLUDE_DIR}) 33 | target_include_directories(test_kuiper PUBLIC ${GTest_INCLUDE_DIR}) 34 | target_include_directories(test_kuiper PUBLIC ${Armadillo_INCLUDE_DIR}) 35 | 36 | set_target_properties(test_kuiper PROPERTIES 37 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 38 | ) 39 | 40 | -------------------------------------------------------------------------------- /test/test_layer/test_hardsigmoid.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-2. 23 | #include 24 | #include 25 | #include "../../source/layer/details/hardsigmoid.hpp" 26 | #include "data/tensor.hpp" 27 | 28 | TEST(test_layer, forward_hardsigmoid1) { 29 | using namespace kuiper_infer; 30 | std::shared_ptr> input = std::make_shared>(32, 224, 512); 31 | input->RandN(); 32 | std::vector>> inputs; 33 | inputs.push_back(input); 34 | std::vector>> outputs(1); 35 | 36 | HardSigmoid hardsigmoid_layer; 37 | const auto status = hardsigmoid_layer.Forward(inputs, outputs); 38 | ASSERT_EQ(status, StatusCode::kSuccess); 39 | for (int i = 0; i < inputs.size(); ++i) { 40 | std::shared_ptr> input_ = inputs.at(i); 41 | std::shared_ptr> output_ = outputs.at(i); 42 | CHECK(input_->size() == output_->size()); 43 | input_->Transform([](float val) { 44 | if (val <= -3.f) { 45 | return 0.f; 46 | } else if (val >= 3.f) { 47 | return 1.f; 48 | } else { 49 | return val / 6.f + 0.5f; 50 | } 51 | }); 52 | uint32_t size = input_->size(); 53 | for (uint32_t j = 0; j < size; ++j) { 54 | ASSERT_EQ(output_->index(j), input_->index(j)); 55 | } 56 | } 57 | } 58 | 59 | TEST(test_layer, forward_hardsigmoid2) { 60 | using namespace kuiper_infer; 61 | std::shared_ptr> input = std::make_shared>(1, 32, 128); 62 | input->RandN(); 63 | std::vector>> inputs; 64 | inputs.push_back(input); 65 | std::vector>> outputs(1); 66 | 67 | HardSigmoid hardsigmoid_layer; 68 | const auto status = hardsigmoid_layer.Forward(inputs, outputs); 69 | ASSERT_EQ(status, StatusCode::kSuccess); 70 | for (int i = 0; i < inputs.size(); ++i) { 71 | std::shared_ptr> input_ = inputs.at(i); 72 | std::shared_ptr> output_ = outputs.at(i); 73 | CHECK(input_->size() == output_->size()); 74 | input_->Transform([](float val) { 75 | if (val <= -3.f) { 76 | return 0.f; 77 | } else if (val >= 3.f) { 78 | return 1.f; 79 | } else { 80 | return val / 6.f + 0.5f; 81 | } 82 | }); 83 | uint32_t size = input_->size(); 84 | for (uint32_t j = 0; j < size; ++j) { 85 | ASSERT_EQ(output_->index(j), input_->index(j)); 86 | } 87 | } 88 | } 89 | 90 | TEST(test_layer, forward_hardsigmoid3) { 91 | using namespace kuiper_infer; 92 | std::shared_ptr> input = std::make_shared>(1, 1, 16); 93 | input->RandN(); 94 | std::vector>> inputs; 95 | inputs.push_back(input); 96 | std::vector>> outputs(1); 97 | 98 | HardSigmoid hardsigmoid_layer; 99 | const auto status = hardsigmoid_layer.Forward(inputs, outputs); 100 | ASSERT_EQ(status, StatusCode::kSuccess); 101 | for (int i = 0; i < inputs.size(); ++i) { 102 | std::shared_ptr> input_ = inputs.at(i); 103 | std::shared_ptr> output_ = outputs.at(i); 104 | CHECK(input_->size() == output_->size()); 105 | input_->Transform([](float val) { 106 | if (val <= -3.f) { 107 | return 0.f; 108 | } else if (val >= 3.f) { 109 | return 1.f; 110 | } else { 111 | return val / 6.f + 0.5f; 112 | } 113 | }); 114 | uint32_t size = input_->size(); 115 | for (uint32_t j = 0; j < size; ++j) { 116 | ASSERT_EQ(output_->index(j), input_->index(j)); 117 | } 118 | } 119 | } -------------------------------------------------------------------------------- /test/test_layer/test_hardswish.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-2. 23 | #include 24 | #include 25 | #include "../../source/layer/details/hardswish.hpp" 26 | #include "data/tensor.hpp" 27 | 28 | TEST(test_layer, forward_hardswish1) { 29 | using namespace kuiper_infer; 30 | std::shared_ptr> input = std::make_shared>(32, 221, 513); 31 | input->RandN(); 32 | std::vector>> inputs; 33 | inputs.push_back(input); 34 | std::vector>> outputs(1); 35 | 36 | HardSwishLayer hard_swish_layer; 37 | const auto status = hard_swish_layer.Forward(inputs, outputs); 38 | ASSERT_EQ(status, StatusCode::kSuccess); 39 | for (int i = 0; i < inputs.size(); ++i) { 40 | std::shared_ptr> input_ = inputs.at(i); 41 | std::shared_ptr> output_ = outputs.at(i); 42 | CHECK(input_->size() == output_->size()); 43 | input_->Transform([](float val) { 44 | if (val <= -3.f) { 45 | return 0.f; 46 | } else if (val >= 3.f) { 47 | return val; 48 | } else { 49 | return val * (val + 3) / 6; 50 | } 51 | }); 52 | uint32_t size = input_->size(); 53 | for (uint32_t j = 0; j < size; ++j) { 54 | ASSERT_EQ(output_->index(j), input_->index(j)); 55 | } 56 | } 57 | } 58 | 59 | TEST(test_layer, forward_hardswish2) { 60 | using namespace kuiper_infer; 61 | std::shared_ptr> input = std::make_shared>(1, 32, 128); 62 | input->RandN(); 63 | std::vector>> inputs; 64 | inputs.push_back(input); 65 | std::vector>> outputs(1); 66 | 67 | HardSwishLayer hard_swish_layer; 68 | const auto status = hard_swish_layer.Forward(inputs, outputs); 69 | ASSERT_EQ(status, StatusCode::kSuccess); 70 | for (int i = 0; i < inputs.size(); ++i) { 71 | std::shared_ptr> input_ = inputs.at(i); 72 | std::shared_ptr> output_ = outputs.at(i); 73 | CHECK(input_->size() == output_->size()); 74 | input_->Transform([](float val) { 75 | if (val <= -3.f) { 76 | return 0.f; 77 | } else if (val >= 3.f) { 78 | return val; 79 | } else { 80 | return val * (val + 3) / 6; 81 | } 82 | }); 83 | uint32_t size = input_->size(); 84 | for (uint32_t j = 0; j < size; ++j) { 85 | ASSERT_EQ(output_->index(j), input_->index(j)); 86 | } 87 | } 88 | } 89 | 90 | TEST(test_layer, forward_hardswish3) { 91 | using namespace kuiper_infer; 92 | std::shared_ptr> input = std::make_shared>(1, 1, 16); 93 | input->RandN(); 94 | std::vector>> inputs; 95 | inputs.push_back(input); 96 | std::vector>> outputs(1); 97 | 98 | HardSwishLayer hard_swish_layer; 99 | const auto status = hard_swish_layer.Forward(inputs, outputs); 100 | ASSERT_EQ(status, StatusCode::kSuccess); 101 | for (int i = 0; i < inputs.size(); ++i) { 102 | std::shared_ptr> input_ = inputs.at(i); 103 | std::shared_ptr> output_ = outputs.at(i); 104 | CHECK(input_->size() == output_->size()); 105 | input_->Transform([](float val) { 106 | if (val <= -3.f) { 107 | return 0.f; 108 | } else if (val >= 3.f) { 109 | return val; 110 | } else { 111 | return val * (val + 3) / 6; 112 | } 113 | }); 114 | uint32_t size = input_->size(); 115 | for (uint32_t j = 0; j < size; ++j) { 116 | ASSERT_EQ(output_->index(j), input_->index(j)); 117 | } 118 | } 119 | } -------------------------------------------------------------------------------- /test/test_layer/test_layer_factory.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-22. 23 | 24 | #include 25 | #include "layer/abstract/layer_factory.hpp" 26 | 27 | using namespace kuiper_infer; 28 | StatusCode TestCreateLayer(const std::shared_ptr& op, 29 | std::shared_ptr>& layer) { 30 | layer = std::make_shared>("test3"); 31 | return StatusCode::kSuccess; 32 | } 33 | 34 | TEST(test_layer_factory, init) { 35 | using namespace kuiper_infer; 36 | const auto& r1 = LayerRegisterer::Registry(); 37 | const auto& r2 = LayerRegisterer::Registry(); 38 | ASSERT_EQ(r1, r2); 39 | } 40 | 41 | TEST(test_layer_factory, registerer) { 42 | using namespace kuiper_infer; 43 | const uint32_t size1 = LayerRegisterer::Registry()->size(); 44 | LayerRegisterer::RegisterCreator("test1", TestCreateLayer); 45 | const uint32_t size2 = LayerRegisterer::Registry()->size(); 46 | LayerRegisterer::RegisterCreator("test2", TestCreateLayer); 47 | const uint32_t size3 = LayerRegisterer::Registry()->size(); 48 | 49 | ASSERT_EQ(size2 - size1, 1); 50 | ASSERT_EQ(size3 - size1, 2); 51 | } 52 | 53 | TEST(test_layer_factory, create) { 54 | using namespace kuiper_infer; 55 | LayerRegisterer::RegisterCreator("test3", TestCreateLayer); 56 | std::shared_ptr op = std::make_shared(); 57 | op->type = "test3"; 58 | std::shared_ptr> layer = LayerRegisterer::CreateLayer(op); 59 | ASSERT_EQ(layer->layer_name(), "test3"); 60 | } -------------------------------------------------------------------------------- /test/test_layer/test_matmul.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by fss on 24-2-10. 3 | // 4 | #include 5 | #include 6 | #include 7 | #include "../../source/layer/details/matmul.hpp" 8 | 9 | TEST(test_layer, forward_matmul) { 10 | using namespace kuiper_infer; 11 | std::vector weights; 12 | for (int i = 0; i < 3 * 4; ++i) { 13 | weights.push_back(float(i)); 14 | } 15 | LLamaMatmulLayer llama_matmul(3, 4); 16 | std::shared_ptr> weight = std::make_shared>(weights.data(), 3, 4); 17 | llama_matmul.set_weights({weight}); 18 | 19 | std::vector inputs; 20 | for (int i = 0; i < 4 * 5; ++i) { 21 | inputs.push_back(float(i)); 22 | } 23 | 24 | std::shared_ptr> input = std::make_shared>(inputs.data(), 4, 5); 25 | std::shared_ptr> output = std::make_shared>(3, 5); 26 | std::vector outputs; 27 | outputs.push_back(output); 28 | 29 | llama_matmul.Forward({input}, outputs); 30 | ASSERT_EQ(output->at(0, 0, 0), 70); 31 | ASSERT_EQ(output->at(0, 0, 1), 76); 32 | ASSERT_EQ(output->at(0, 0, 2), 82); 33 | ASSERT_EQ(output->at(0, 0, 3), 88); 34 | ASSERT_EQ(output->at(0, 0, 4), 94); 35 | 36 | ASSERT_EQ(output->at(0, 2, 0), 310); 37 | ASSERT_EQ(output->at(0, 2, 1), 348); 38 | ASSERT_EQ(output->at(0, 2, 2), 386); 39 | ASSERT_EQ(output->at(0, 2, 3), 424); 40 | ASSERT_EQ(output->at(0, 2, 4), 462); 41 | } 42 | 43 | TEST(test_layer, forward_matmul2) { 44 | using namespace kuiper_infer; 45 | std::vector weights; 46 | for (int i = 0; i < 3 * 4; ++i) { 47 | weights.push_back(float(i)); 48 | } 49 | LLamaMatmulLayer llama_matmul(3, 4); 50 | std::shared_ptr> weight = std::make_shared>(weights.data(), 3, 4); 51 | llama_matmul.set_weights({weight}); 52 | 53 | std::vector inputs; 54 | for (int i = 0; i < 4; ++i) { 55 | inputs.push_back(float(i)); 56 | } 57 | 58 | std::shared_ptr> input = std::make_shared>(inputs.data(), 4, 1); 59 | std::shared_ptr> output = std::make_shared>(3, 1); 60 | std::vector outputs; 61 | outputs.push_back(output); 62 | 63 | llama_matmul.Forward({input}, outputs); 64 | ASSERT_EQ(output->at(0, 0, 0), 14); 65 | ASSERT_EQ(output->at(0, 1, 0), 38); 66 | ASSERT_EQ(output->at(0, 2, 0), 62); 67 | } 68 | 69 | TEST(test_layer, forward_matmul3) { 70 | using namespace kuiper_infer; 71 | std::vector weights; 72 | for (int i = 0; i < 3 * 4; ++i) { 73 | weights.push_back(float(i)); 74 | } 75 | LLamaMatmulLayer llama_matmul(1, 4); 76 | std::shared_ptr> weight = std::make_shared>(weights.data(), 1, 4); 77 | llama_matmul.set_weights({weight}); 78 | 79 | std::vector inputs; 80 | for (int i = 0; i < 8; ++i) { 81 | inputs.push_back(float(i)); 82 | } 83 | 84 | std::shared_ptr> input = std::make_shared>(inputs.data(), 4, 2); 85 | std::shared_ptr> output = std::make_shared>(1, 2); 86 | std::vector outputs; 87 | outputs.push_back(output); 88 | 89 | llama_matmul.Forward({input}, outputs); 90 | ASSERT_EQ(output->at(0, 0, 0), 28); 91 | } 92 | -------------------------------------------------------------------------------- /test/test_layer/test_reshape.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-21. 23 | #include 24 | #include "data/tensor.hpp" 25 | 26 | TEST(test_reshape, reshape1) { 27 | using namespace kuiper_infer; 28 | Tensor tensor1(3, 224, 224); 29 | tensor1.RandN(); 30 | Tensor tensor2 = tensor1; 31 | tensor1.Reshape({224, 224, 3}); 32 | 33 | const auto& raw_shapes = tensor1.raw_shapes(); 34 | ASSERT_EQ(raw_shapes.size(), 3); 35 | ASSERT_EQ(raw_shapes.at(0), 224); 36 | ASSERT_EQ(raw_shapes.at(1), 224); 37 | ASSERT_EQ(raw_shapes.at(2), 3); 38 | 39 | ASSERT_EQ(tensor1.size(), tensor2.size()); 40 | uint32_t size = tensor1.size(); 41 | for (uint32_t i = 0; i < size; ++i) { 42 | ASSERT_EQ(tensor1.index(i), tensor2.index(i)); 43 | } 44 | } 45 | 46 | TEST(test_reshape, reshape2) { 47 | using namespace kuiper_infer; 48 | Tensor tensor1(3, 224, 224); 49 | tensor1.RandN(); 50 | Tensor tensor2 = tensor1; 51 | tensor1.Reshape({672, 224}); 52 | 53 | const auto& raw_shapes = tensor1.raw_shapes(); 54 | ASSERT_EQ(raw_shapes.size(), 2); 55 | ASSERT_EQ(raw_shapes.at(0), 672); 56 | ASSERT_EQ(raw_shapes.at(1), 224); 57 | 58 | ASSERT_EQ(tensor1.size(), tensor2.size()); 59 | uint32_t size = tensor1.size(); 60 | for (uint32_t i = 0; i < size; ++i) { 61 | ASSERT_EQ(tensor1.index(i), tensor2.index(i)); 62 | } 63 | } 64 | 65 | TEST(test_reshape, reshape3) { 66 | using namespace kuiper_infer; 67 | Tensor tensor1(3, 224, 224); 68 | tensor1.RandN(); 69 | Tensor tensor2 = tensor1; 70 | tensor1.Reshape({150528}); 71 | 72 | const auto& raw_shapes = tensor1.raw_shapes(); 73 | ASSERT_EQ(raw_shapes.size(), 1); 74 | ASSERT_EQ(raw_shapes.at(0), 150528); 75 | 76 | ASSERT_EQ(tensor1.size(), tensor2.size()); 77 | uint32_t size = tensor1.size(); 78 | for (uint32_t i = 0; i < size; ++i) { 79 | ASSERT_EQ(tensor1.index(i), tensor2.index(i)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /test/test_main.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-11-17. 23 | #include 24 | #include 25 | 26 | int main(int argc, char* argv[]) { 27 | testing::InitGoogleTest(&argc, argv); 28 | google::InitGoogleLogging("Kuiper"); 29 | FLAGS_log_dir = "./log/"; 30 | FLAGS_alsologtostderr = true; 31 | 32 | LOG(INFO) << "Start test...\n"; 33 | return RUN_ALL_TESTS(); 34 | } -------------------------------------------------------------------------------- /test/test_net/test_yolo.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 22-12-25. 23 | #include 24 | #include 25 | #include "data/load_data.hpp" 26 | #include "runtime/runtime_ir.hpp" 27 | 28 | TEST(test_net, forward_yolo1) { 29 | using namespace kuiper_infer; 30 | RuntimeGraph graph("tmp/yolo/demo/yolov5n_small.pnnx.param", 31 | "tmp/yolo/demo/yolov5n_small.pnnx.bin"); 32 | 33 | graph.Build(); 34 | const uint32_t batch_size = 4; 35 | std::vector>> inputs; 36 | 37 | for (int i = 0; i < batch_size; ++i) { 38 | std::shared_ptr> input = std::make_shared>(3, 320, 320); 39 | input->Fill(127.f); 40 | inputs.push_back(input); 41 | } 42 | 43 | graph.set_inputs("pnnx_input_0", inputs); 44 | graph.Forward(false); 45 | std::vector>> outputs = graph.get_outputs("pnnx_output_0"); 46 | for (int i = 0; i < batch_size; ++i) { 47 | std::string file_path = "tmp/yolo/" + std::to_string(i + 1) + ".csv"; 48 | const auto& output1 = CSVDataLoader::LoadData(file_path); 49 | const auto& output2 = outputs.at(i); 50 | 51 | ASSERT_EQ(output1.size(), output2->size()); 52 | for (int r = 0; r < output1.n_rows; ++r) { 53 | for (int c = 0; c < output1.n_cols; ++c) { 54 | ASSERT_LE(std::abs(output1.at(r, c) - output2->at(0, r, c)), 0.05) 55 | << " row: " << r << " col: " << c; 56 | } 57 | } 58 | } 59 | } 60 | 61 | TEST(test_net, forward_yolo2) { 62 | using namespace kuiper_infer; 63 | RuntimeGraph graph("tmp/yolo/demo/yolov5n_small.pnnx.param", 64 | "tmp/yolo/demo/yolov5n_small.pnnx.bin"); 65 | 66 | graph.Build(); 67 | const uint32_t batch_size = 4; 68 | std::vector>> inputs; 69 | 70 | for (int i = 0; i < batch_size; ++i) { 71 | std::shared_ptr> input = std::make_shared>(3, 320, 320); 72 | input->Fill(42.f); 73 | inputs.push_back(input); 74 | } 75 | graph.set_inputs("pnnx_input_0", inputs); 76 | graph.Forward(false); 77 | std::vector>> outputs = graph.get_outputs("pnnx_output_0"); 78 | for (int i = 0; i < batch_size; ++i) { 79 | std::string file_path = "tmp/yolo/" + std::to_string((i + 1) * 10 + 1) + ".csv"; 80 | const auto& output1 = CSVDataLoader::LoadData(file_path); 81 | const auto& output2 = outputs.at(i); 82 | 83 | ASSERT_EQ(output1.size(), output2->size()); 84 | for (int r = 0; r < output1.n_rows; ++r) { 85 | for (int c = 0; c < output1.n_cols; ++c) { 86 | ASSERT_LE(std::abs(output1.at(r, c) - output2->at(0, r, c)), 0.05) 87 | << " row: " << r << " col: " << c; 88 | } 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /test/test_runtime/test_attr.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by fss on 23-1-29. 23 | #include 24 | #include "runtime/runtime_attr.hpp" 25 | 26 | TEST(test_runtime, attr_weight_data1) { 27 | using namespace kuiper_infer; 28 | RuntimeAttribute runtime_attr; 29 | std::vector weight_data; 30 | for (int i = 0; i < 32; ++i) { 31 | weight_data.push_back((char)i); 32 | } 33 | runtime_attr.weight_data = weight_data; 34 | const auto& result_weight_data = runtime_attr.weight_data; 35 | ASSERT_EQ(result_weight_data.size(), 32); 36 | 37 | for (int i = 0; i < 32; ++i) { 38 | ASSERT_EQ(weight_data.at(i), (char)i); 39 | } 40 | } 41 | 42 | TEST(test_runtime, attr_weight_data2) { 43 | using namespace kuiper_infer; 44 | RuntimeAttribute runtime_attr; 45 | runtime_attr.type = RuntimeDataType::kTypeFloat32; 46 | std::vector weight_data; 47 | for (int i = 0; i < 32; ++i) { 48 | weight_data.push_back(0); 49 | } 50 | runtime_attr.weight_data = weight_data; 51 | 52 | const auto& result_weight_data = runtime_attr.get(true); 53 | ASSERT_EQ(result_weight_data.size(), 8); 54 | ASSERT_EQ(runtime_attr.weight_data.size(), 0); 55 | 56 | for (int i = 0; i < 32; ++i) { 57 | ASSERT_EQ(weight_data.at(i), 0.f); 58 | } 59 | } 60 | 61 | TEST(test_runtime, attr_weight_data3) { 62 | using namespace kuiper_infer; 63 | RuntimeAttribute runtime_attr; 64 | runtime_attr.type = RuntimeDataType::kTypeFloat32; 65 | std::vector weight_data; 66 | for (int i = 0; i < 32; ++i) { 67 | weight_data.push_back(0); 68 | } 69 | runtime_attr.weight_data = weight_data; 70 | 71 | const auto& result_weight_data = runtime_attr.get(false); 72 | ASSERT_EQ(result_weight_data.size(), 8); 73 | ASSERT_EQ(runtime_attr.weight_data.size(), 32); 74 | 75 | for (int i = 0; i < 32; ++i) { 76 | ASSERT_EQ(weight_data.at(i), 0.f); 77 | } 78 | } 79 | 80 | TEST(test_runtime, attr_shape) { 81 | using namespace kuiper_infer; 82 | RuntimeAttribute runtime_attr; 83 | runtime_attr.type = RuntimeDataType::kTypeFloat32; 84 | runtime_attr.shape = std::vector{3, 32, 32}; 85 | ASSERT_EQ(runtime_attr.shape.at(0), 3); 86 | ASSERT_EQ(runtime_attr.shape.at(1), 32); 87 | ASSERT_EQ(runtime_attr.shape.at(2), 32); 88 | } 89 | -------------------------------------------------------------------------------- /test/test_runtime/test_param.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2022 - 傅莘莘 3 | // Source URL: https://github.com/zjhellofss/KuiperInfer 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, 8 | // and/or sell copies of the Software, and to permit persons to whom the 9 | // Software is furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | // SOFTWARE. 21 | 22 | // Created by yizhu on 2023/2/5. 23 | 24 | #include 25 | #include "runtime/runtime_parameter.hpp" 26 | 27 | TEST(test_runtime, runtime_param1) { 28 | using namespace kuiper_infer; 29 | RuntimeParameter* param = new RuntimeParameterInt; 30 | ASSERT_EQ(param->type, RuntimeParameterType::kParameterInt); 31 | } 32 | 33 | TEST(test_runtime, runtime_param2) { 34 | using namespace kuiper_infer; 35 | RuntimeParameter* param = new RuntimeParameterInt; 36 | ASSERT_EQ(param->type, RuntimeParameterType::kParameterInt); 37 | ASSERT_EQ(dynamic_cast(param), nullptr); 38 | ASSERT_NE(dynamic_cast(param), nullptr); 39 | } -------------------------------------------------------------------------------- /vcpkg.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", 3 | "dependencies": [ 4 | "openblas", 5 | "opencv", 6 | "lapack", 7 | "superlu", 8 | "glog", 9 | "gtest", 10 | "benchmark", 11 | "armadillo" 12 | ] 13 | } --------------------------------------------------------------------------------