├── image ├── 4.png ├── fs.h └── img2c.py ├── ncnn_demo ├── BUILD.gn └── src │ ├── fs.h │ ├── main.cpp │ ├── mnist-1-bin.h │ └── ncnn │ ├── include │ └── ncnn │ │ ├── allocator.h │ │ ├── benchmark.h │ │ ├── blob.h │ │ ├── c_api.h │ │ ├── command.h │ │ ├── cpu.h │ │ ├── datareader.h │ │ ├── gpu.h │ │ ├── layer.h │ │ ├── layer_shader_type.h │ │ ├── layer_shader_type_enum.h │ │ ├── layer_type.h │ │ ├── layer_type_enum.h │ │ ├── mat.h │ │ ├── modelbin.h │ │ ├── ncnn_export.h │ │ ├── net.h │ │ ├── option.h │ │ ├── paramdict.h │ │ ├── pipeline.h │ │ ├── pipelinecache.h │ │ ├── platform.h │ │ ├── simpleocv.h │ │ ├── simpleomp.h │ │ ├── simplestl.h │ │ └── vulkan_header_fix.h │ └── lib │ ├── cmake │ └── ncnn │ │ ├── ncnn-release.cmake │ │ ├── ncnn.cmake │ │ └── ncnnConfig.cmake │ └── libncnn.a └── xr806.toolchain.cmake /image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nihui/ncnn_on_xr806/c4a2dc4979554364316486971b4d274d055a5630/image/4.png -------------------------------------------------------------------------------- /image/fs.h: -------------------------------------------------------------------------------- 1 | const int IMAGE_H = 28; 2 | const int IMAGE_W = 28; 3 | const unsigned char IN_IMG[] __attribute__((aligned(128))) ={ 4 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 5 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 6 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 7 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 8 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 9 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 10 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 11 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 12 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 13 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 14 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 15 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 16 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 17 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 18 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 19 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 20 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 21 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 22 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x18, 0x20, 0x17, 23 | 0xd, 0x8, 0x8, 0x8, 0x8, 0x8, 0xb, 0x10, 24 | 0x12, 0xa, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 25 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 26 | 0x8, 0x3d, 0x55, 0x3a, 0x19, 0x8, 0x8, 0x8, 27 | 0x8, 0x9, 0x13, 0x1f, 0x26, 0xe, 0x8, 0x8, 28 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 29 | 0x8, 0x8, 0x8, 0x9, 0xc, 0x68, 0x8d, 0x50, 30 | 0x20, 0x8, 0x8, 0x8, 0x8, 0x10, 0x48, 0x85, 31 | 0x9e, 0x2c, 0xc, 0x8, 0x8, 0x8, 0x8, 0x8, 32 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0xa, 33 | 0xf, 0x7c, 0xa2, 0x50, 0x1f, 0x8, 0x8, 0x8, 34 | 0x9, 0x19, 0x86, 0xc4, 0xc3, 0x35, 0xd, 0x8, 35 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 36 | 0x8, 0x8, 0x8, 0xa, 0x12, 0x78, 0x97, 0x3b, 37 | 0x18, 0x8, 0x8, 0x8, 0xc, 0x27, 0xca, 0xd6, 38 | 0x91, 0x25, 0x9, 0x8, 0x8, 0x8, 0x8, 0x8, 39 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0xf, 40 | 0x21, 0x9a, 0xb6, 0x37, 0x17, 0x8, 0x8, 0x8, 41 | 0xf, 0x2d, 0xcc, 0xb9, 0x5a, 0x18, 0x8, 0x8, 42 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 43 | 0x8, 0x8, 0x8, 0x12, 0x31, 0xa1, 0xb1, 0x27, 44 | 0x11, 0x8, 0x8, 0x8, 0x18, 0x44, 0xd1, 0xa0, 45 | 0x2d, 0xf, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 46 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x16, 47 | 0x3f, 0x93, 0x93, 0x13, 0xe, 0xe, 0xe, 0xc, 48 | 0x2d, 0x6d, 0xd9, 0x8c, 0xa, 0x8, 0x8, 0x8, 49 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 50 | 0x8, 0x8, 0x8, 0x1d, 0x5f, 0xb1, 0xba, 0x5b, 51 | 0x5b, 0x5a, 0x53, 0x41, 0x71, 0xc5, 0xd9, 0x7b, 52 | 0x9, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 53 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x1f, 54 | 0x66, 0xbe, 0xdf, 0xb0, 0xb1, 0xb0, 0xaa, 0x9b, 55 | 0xbb, 0xf0, 0xd7, 0x73, 0x8, 0x8, 0x8, 0x8, 56 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 57 | 0x8, 0x8, 0x8, 0x1b, 0x56, 0xb4, 0xed, 0xeb, 58 | 0xeb, 0xea, 0xe9, 0xed, 0xf5, 0xf9, 0xcc, 0x69, 59 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 60 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0xe, 61 | 0x1e, 0x4f, 0x65, 0x50, 0x4f, 0x4a, 0x43, 0x62, 62 | 0xa9, 0xf1, 0x89, 0x3a, 0x8, 0x8, 0x8, 0x8, 63 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 64 | 0x8, 0x8, 0x8, 0x8, 0xa, 0x1c, 0x22, 0x12, 65 | 0x11, 0xe, 0xb, 0x53, 0xaa, 0xee, 0x69, 0x25, 66 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 67 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 68 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0xd, 0x7d, 69 | 0xcd, 0xed, 0x5e, 0x1d, 0x8, 0x8, 0x8, 0x8, 70 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 71 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 72 | 0x8, 0x8, 0xe, 0x83, 0xd1, 0xed, 0x5b, 0x1b, 73 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 74 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 75 | 0x8, 0x8, 0x8, 0x8, 0x8, 0xd, 0x1b, 0x9a, 76 | 0xe2, 0xec, 0x59, 0x1a, 0x8, 0x8, 0x8, 0x8, 77 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 78 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 79 | 0x8, 0x10, 0x25, 0xaa, 0xe7, 0xdd, 0x52, 0x17, 80 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 81 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 82 | 0x8, 0x8, 0x8, 0x8, 0x8, 0xa, 0x12, 0x85, 83 | 0xb2, 0xa2, 0x3a, 0x11, 0x8, 0x8, 0x8, 0x8, 84 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 85 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 86 | 0x8, 0x8, 0xb, 0x40, 0x55, 0x4c, 0x1d, 0xc, 87 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 88 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 89 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 90 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 91 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 92 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 93 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 94 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 95 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 96 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 97 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 98 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 99 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 100 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 101 | 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 102 | 103 | }; 104 | -------------------------------------------------------------------------------- /image/img2c.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import sys 4 | import numpy as np 5 | import glob 6 | 7 | shape = (28, 28) 8 | if __name__ == '__main__': 9 | 10 | im = cv2.imread(sys.argv[1], 0) 11 | im = cv2.resize(im, shape) 12 | 13 | img = im.flatten() 14 | 15 | print(img.shape, img.dtype) 16 | 17 | with open(os.getcwd() + '/fs.h', 'w+') as f: 18 | print('const int IMAGE_H = %d;' % shape[0], file=f) 19 | print('const int IMAGE_W = %d;' % shape[1], file=f) 20 | print('const unsigned char IN_IMG[] __attribute__((aligned(128))) ={', file=f) 21 | counter = 0 22 | for pixel in img: 23 | counter += 1 24 | print('0x%x, ' % (255 - pixel), end='', file=f) 25 | if counter % 8 == 0: 26 | print(file=f) 27 | print(file=f) 28 | print('};', file=f) 29 | -------------------------------------------------------------------------------- /ncnn_demo/BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2021 XRADIO TECHNOLOGY CO., LTD. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 1. Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # 2. Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the 11 | # distribution. 12 | # 3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of 13 | # its contributors may be used to endorse or promote products derived 14 | # from this software without specific prior written permission. 15 | # 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | import("//device/xradio/xr806/liteos_m/config.gni") 28 | 29 | static_library("app_ncnn") { 30 | configs = [] 31 | 32 | sources = [ 33 | "src/main.cpp", 34 | ] 35 | 36 | cflags = board_cflags 37 | cflags_cc = board_cflags 38 | 39 | include_dirs = board_include_dirs 40 | include_dirs += [ 41 | "//kernel/liteos_m/kernel/arch/include", 42 | "src/ncnn/include", 43 | ] 44 | } 45 | -------------------------------------------------------------------------------- /ncnn_demo/src/fs.h: -------------------------------------------------------------------------------- 1 | const int IMAGE_H = 28; 2 | const int IMAGE_W = 28; 3 | const unsigned char IN_IMG[] __attribute__((aligned(128))) ={ 4 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 5 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 6 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 7 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 8 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 9 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 10 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 11 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 12 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 13 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 14 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 15 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 16 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 17 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 18 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 19 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 20 | 0x3, 0x4, 0x4, 0x4, 0x4, 0x2, 0x0, 0x0, 21 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 22 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 23 | 0x0, 0x0, 0xa, 0x34, 0x77, 0xa9, 0xb1, 0xb1, 24 | 0xa5, 0x49, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 25 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 26 | 0x7, 0x35, 0x64, 0xb2, 0xbb, 0xbb, 0xbf, 0xd4, 27 | 0xee, 0xfc, 0xff, 0xff, 0xf9, 0xa3, 0x15, 0x1, 28 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 29 | 0x0, 0x0, 0x0, 0x14, 0x7d, 0xce, 0xe8, 0xfa, 30 | 0xfc, 0xfc, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xff, 31 | 0xfe, 0xc5, 0x32, 0x1, 0x0, 0x0, 0x0, 0x0, 32 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x36, 33 | 0xcc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 34 | 0xfa, 0xf3, 0xfc, 0xff, 0xfb, 0xa5, 0xb, 0x0, 35 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 36 | 0x0, 0x0, 0x0, 0x22, 0x98, 0xd4, 0xed, 0xd1, 37 | 0xcd, 0xcd, 0xc7, 0xaa, 0x94, 0x77, 0xdf, 0xff, 38 | 0xf9, 0x9b, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 39 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 40 | 0x22, 0x49, 0x76, 0x42, 0x3b, 0x3b, 0x34, 0x15, 41 | 0x17, 0x4d, 0xdb, 0xff, 0xf4, 0x87, 0x0, 0x0, 42 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 43 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 44 | 0x0, 0x0, 0x0, 0x7, 0x62, 0xd2, 0xf9, 0xf7, 45 | 0xac, 0x37, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 46 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 47 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x51, 48 | 0xc9, 0xfb, 0xff, 0xea, 0x49, 0x5, 0x0, 0x0, 49 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 50 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 51 | 0x0, 0x8, 0x54, 0xcb, 0xfa, 0xff, 0xf5, 0x97, 52 | 0x15, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 53 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 54 | 0x0, 0x0, 0x0, 0x0, 0x3, 0x39, 0xc7, 0xfa, 55 | 0xff, 0xff, 0xc8, 0x2c, 0x2, 0x0, 0x0, 0x0, 56 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 57 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 58 | 0x28, 0x92, 0xf7, 0xff, 0xfe, 0xf4, 0x50, 0x5, 59 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 60 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 61 | 0x0, 0x0, 0x2, 0x30, 0xb3, 0xf7, 0xff, 0xff, 62 | 0xda, 0x88, 0x1b, 0x0, 0x0, 0x0, 0x0, 0x0, 63 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x28, 0x9c, 65 | 0xf6, 0xff, 0xff, 0xe8, 0x72, 0xe, 0x0, 0x0, 66 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 67 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 68 | 0x0, 0x2e, 0xa0, 0xf6, 0xff, 0xff, 0xf1, 0x91, 69 | 0x1e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 70 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 71 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x4f, 0xde, 0xfe, 72 | 0xff, 0xff, 0xe6, 0x63, 0x3, 0x0, 0x0, 0x0, 73 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 74 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 75 | 0x0, 0x7f, 0xef, 0xff, 0xff, 0xfa, 0xae, 0x2c, 76 | 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 77 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 78 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x4b, 0xd5, 0xfd, 79 | 0xff, 0xef, 0x7f, 0xd, 0x0, 0x0, 0x0, 0x0, 80 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 81 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 82 | 0x0, 0xb, 0x7e, 0xe5, 0xf9, 0xbd, 0x2b, 0x0, 83 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 84 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 85 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x14, 0x5d, 86 | 0x8e, 0x41, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 87 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 88 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 89 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 90 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 91 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 92 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 93 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 94 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 95 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 96 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 97 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 98 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 99 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 100 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 101 | 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 102 | 103 | }; 104 | -------------------------------------------------------------------------------- /ncnn_demo/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 XRADIO TECHNOLOGY CO., LTD. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 1. Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the 12 | * distribution. 13 | * 3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of 14 | * its contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include "ohos_init.h" 32 | #include "kernel/os/os.h" 33 | 34 | #include "ncnn/benchmark.h" 35 | #include "ncnn/cpu.h" 36 | #include "ncnn/net.h" 37 | #include "ncnn/mat.h" 38 | #include "ncnn/datareader.h" 39 | 40 | #include "mnist-1-bin.h" 41 | #include "fs.h" 42 | 43 | static OS_Thread_t g_main_thread; 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | void app_main(void) 50 | { 51 | ncnn::Net net; 52 | 53 | printf("Loading ncnn mnist model..."); 54 | net.load_param(mnist_1_opt_param_bin); 55 | net.load_model(mnist_1_opt_bin); 56 | // net.opt.lightmode = true; 57 | printf("Done.\n"); 58 | 59 | // return; 60 | 61 | printf("Preparing input..."); 62 | 63 | ncnn::Mat in = ncnn::Mat::from_pixels(IN_IMG, ncnn::Mat::PIXEL_GRAY, IMAGE_W, IMAGE_H); 64 | ncnn::Mat out; 65 | 66 | printf("[28, 28, 1].\n"); 67 | 68 | printf("Start Mesuring!\n"); 69 | 70 | double total_latency = 0; 71 | float max = -1; 72 | float min = 10000000000000000; 73 | 74 | for(int i=0; i<10; i++){ 75 | // vTaskDelay(20 / portTICK_PERIOD_MS); 76 | 77 | // long start = esp_timer_get_time(); 78 | double start = ncnn::get_current_time(); 79 | 80 | ncnn::Extractor ex = net.create_extractor(); 81 | ex.input(0, in); 82 | ex.extract(17, out); 83 | 84 | // long end = esp_timer_get_time(); 85 | double end = ncnn::get_current_time(); 86 | 87 | float lat = end - start; 88 | total_latency += lat; 89 | if(lat > max){ 90 | max = lat; 91 | } 92 | if(lat < min){ 93 | min = lat; 94 | } 95 | } 96 | 97 | printf("Done!\n"); 98 | 99 | const float* ptr = out.channel(0); 100 | int gussed = -1; 101 | float guss_exp = -10000000; 102 | for(int i=0; i 21 | #endif 22 | 23 | #include "platform.h" 24 | 25 | #include 26 | #include 27 | 28 | #if NCNN_VULKAN 29 | #include 30 | #endif // NCNN_VULKAN 31 | 32 | #if NCNN_PLATFORM_API 33 | #if __ANDROID_API__ >= 26 34 | #include 35 | #endif // __ANDROID_API__ >= 26 36 | #endif // NCNN_PLATFORM_API 37 | 38 | namespace ncnn { 39 | 40 | #if __AVX__ 41 | // the alignment of all the allocated buffers 42 | #define NCNN_MALLOC_ALIGN 32 43 | #else 44 | // the alignment of all the allocated buffers 45 | #define NCNN_MALLOC_ALIGN 16 46 | #endif 47 | 48 | // we have some optimized kernels that may overread buffer a bit in loop 49 | // it is common to interleave next-loop data load with arithmetic instructions 50 | // allocating more bytes keeps us safe from SEGV_ACCERR failure 51 | #define NCNN_MALLOC_OVERREAD 64 52 | 53 | // Aligns a pointer to the specified number of bytes 54 | // ptr Aligned pointer 55 | // n Alignment size that must be a power of two 56 | template 57 | static inline _Tp* alignPtr(_Tp* ptr, int n = (int)sizeof(_Tp)) 58 | { 59 | return (_Tp*)(((size_t)ptr + n - 1) & -n); 60 | } 61 | 62 | // Aligns a buffer size to the specified number of bytes 63 | // The function returns the minimum number that is greater or equal to sz and is divisible by n 64 | // sz Buffer size to align 65 | // n Alignment size that must be a power of two 66 | static inline size_t alignSize(size_t sz, int n) 67 | { 68 | return (sz + n - 1) & -n; 69 | } 70 | 71 | static inline void* fastMalloc(size_t size) 72 | { 73 | #if _MSC_VER 74 | return _aligned_malloc(size, NCNN_MALLOC_ALIGN); 75 | #elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) 76 | void* ptr = 0; 77 | if (posix_memalign(&ptr, NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD)) 78 | ptr = 0; 79 | return ptr; 80 | #elif __ANDROID__ && __ANDROID_API__ < 17 81 | return memalign(NCNN_MALLOC_ALIGN, size + NCNN_MALLOC_OVERREAD); 82 | #else 83 | unsigned char* udata = (unsigned char*)malloc(size + sizeof(void*) + NCNN_MALLOC_ALIGN + NCNN_MALLOC_OVERREAD); 84 | if (!udata) 85 | return 0; 86 | unsigned char** adata = alignPtr((unsigned char**)udata + 1, NCNN_MALLOC_ALIGN); 87 | adata[-1] = udata; 88 | return adata; 89 | #endif 90 | } 91 | 92 | static inline void fastFree(void* ptr) 93 | { 94 | if (ptr) 95 | { 96 | #if _MSC_VER 97 | _aligned_free(ptr); 98 | #elif (defined(__unix__) || defined(__APPLE__)) && _POSIX_C_SOURCE >= 200112L || (__ANDROID__ && __ANDROID_API__ >= 17) 99 | free(ptr); 100 | #elif __ANDROID__ && __ANDROID_API__ < 17 101 | free(ptr); 102 | #else 103 | unsigned char* udata = ((unsigned char**)ptr)[-1]; 104 | free(udata); 105 | #endif 106 | } 107 | } 108 | 109 | #if NCNN_THREADS 110 | // exchange-add operation for atomic operations on reference counters 111 | #if defined __riscv && !defined __riscv_atomic 112 | // riscv target without A extension 113 | static inline int NCNN_XADD(int* addr, int delta) 114 | { 115 | int tmp = *addr; 116 | *addr += delta; 117 | return tmp; 118 | } 119 | #elif defined __INTEL_COMPILER && !(defined WIN32 || defined _WIN32) 120 | // atomic increment on the linux version of the Intel(tm) compiler 121 | #define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd(const_cast(reinterpret_cast(addr)), delta) 122 | #elif defined __GNUC__ 123 | #if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__ && !defined __EMSCRIPTEN__ && !defined(__CUDACC__) 124 | #ifdef __ATOMIC_ACQ_REL 125 | #define NCNN_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), delta, __ATOMIC_ACQ_REL) 126 | #else 127 | #define NCNN_XADD(addr, delta) __atomic_fetch_add((_Atomic(int)*)(addr), delta, 4) 128 | #endif 129 | #else 130 | #if defined __ATOMIC_ACQ_REL && !defined __clang__ 131 | // version for gcc >= 4.7 132 | #define NCNN_XADD(addr, delta) (int)__atomic_fetch_add((unsigned*)(addr), (unsigned)(delta), __ATOMIC_ACQ_REL) 133 | #else 134 | #define NCNN_XADD(addr, delta) (int)__sync_fetch_and_add((unsigned*)(addr), (unsigned)(delta)) 135 | #endif 136 | #endif 137 | #elif defined _MSC_VER && !defined RC_INVOKED 138 | #define NCNN_XADD(addr, delta) (int)_InterlockedExchangeAdd((long volatile*)addr, delta) 139 | #else 140 | // thread-unsafe branch 141 | static inline int NCNN_XADD(int* addr, int delta) 142 | { 143 | int tmp = *addr; 144 | *addr += delta; 145 | return tmp; 146 | } 147 | #endif 148 | #else // NCNN_THREADS 149 | static inline int NCNN_XADD(int* addr, int delta) 150 | { 151 | int tmp = *addr; 152 | *addr += delta; 153 | return tmp; 154 | } 155 | #endif // NCNN_THREADS 156 | 157 | class NCNN_EXPORT Allocator 158 | { 159 | public: 160 | virtual ~Allocator(); 161 | virtual void* fastMalloc(size_t size) = 0; 162 | virtual void fastFree(void* ptr) = 0; 163 | }; 164 | 165 | class PoolAllocatorPrivate; 166 | class NCNN_EXPORT PoolAllocator : public Allocator 167 | { 168 | public: 169 | PoolAllocator(); 170 | ~PoolAllocator(); 171 | 172 | // ratio range 0 ~ 1 173 | // default cr = 0.75 174 | void set_size_compare_ratio(float scr); 175 | 176 | // release all budgets immediately 177 | void clear(); 178 | 179 | virtual void* fastMalloc(size_t size); 180 | virtual void fastFree(void* ptr); 181 | 182 | private: 183 | PoolAllocator(const PoolAllocator&); 184 | PoolAllocator& operator=(const PoolAllocator&); 185 | 186 | private: 187 | PoolAllocatorPrivate* const d; 188 | }; 189 | 190 | class UnlockedPoolAllocatorPrivate; 191 | class NCNN_EXPORT UnlockedPoolAllocator : public Allocator 192 | { 193 | public: 194 | UnlockedPoolAllocator(); 195 | ~UnlockedPoolAllocator(); 196 | 197 | // ratio range 0 ~ 1 198 | // default cr = 0.75 199 | void set_size_compare_ratio(float scr); 200 | 201 | // release all budgets immediately 202 | void clear(); 203 | 204 | virtual void* fastMalloc(size_t size); 205 | virtual void fastFree(void* ptr); 206 | 207 | private: 208 | UnlockedPoolAllocator(const UnlockedPoolAllocator&); 209 | UnlockedPoolAllocator& operator=(const UnlockedPoolAllocator&); 210 | 211 | private: 212 | UnlockedPoolAllocatorPrivate* const d; 213 | }; 214 | 215 | #if NCNN_VULKAN 216 | 217 | class VulkanDevice; 218 | 219 | class NCNN_EXPORT VkBufferMemory 220 | { 221 | public: 222 | VkBuffer buffer; 223 | 224 | // the base offset assigned by allocator 225 | size_t offset; 226 | size_t capacity; 227 | 228 | VkDeviceMemory memory; 229 | void* mapped_ptr; 230 | 231 | // buffer state, modified by command functions internally 232 | mutable VkAccessFlags access_flags; 233 | mutable VkPipelineStageFlags stage_flags; 234 | 235 | // initialize and modified by mat 236 | int refcount; 237 | }; 238 | 239 | class NCNN_EXPORT VkImageMemory 240 | { 241 | public: 242 | VkImage image; 243 | VkImageView imageview; 244 | 245 | // underlying info assigned by allocator 246 | int width; 247 | int height; 248 | int depth; 249 | VkFormat format; 250 | 251 | VkDeviceMemory memory; 252 | void* mapped_ptr; 253 | 254 | // the base offset assigned by allocator 255 | size_t bind_offset; 256 | size_t bind_capacity; 257 | 258 | // image state, modified by command functions internally 259 | mutable VkAccessFlags access_flags; 260 | mutable VkImageLayout image_layout; 261 | mutable VkPipelineStageFlags stage_flags; 262 | 263 | // in-execution state, modified by command functions internally 264 | mutable int command_refcount; 265 | 266 | // initialize and modified by mat 267 | int refcount; 268 | }; 269 | 270 | class NCNN_EXPORT VkAllocator 271 | { 272 | public: 273 | explicit VkAllocator(const VulkanDevice* _vkdev); 274 | virtual ~VkAllocator(); 275 | 276 | virtual void clear(); 277 | 278 | virtual VkBufferMemory* fastMalloc(size_t size) = 0; 279 | virtual void fastFree(VkBufferMemory* ptr) = 0; 280 | virtual int flush(VkBufferMemory* ptr); 281 | virtual int invalidate(VkBufferMemory* ptr); 282 | 283 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack) = 0; 284 | virtual void fastFree(VkImageMemory* ptr) = 0; 285 | 286 | public: 287 | const VulkanDevice* vkdev; 288 | uint32_t buffer_memory_type_index; 289 | uint32_t image_memory_type_index; 290 | uint32_t reserved_type_index; 291 | bool mappable; 292 | bool coherent; 293 | 294 | protected: 295 | VkBuffer create_buffer(size_t size, VkBufferUsageFlags usage); 296 | VkDeviceMemory allocate_memory(size_t size, uint32_t memory_type_index); 297 | VkDeviceMemory allocate_dedicated_memory(size_t size, uint32_t memory_type_index, VkImage image, VkBuffer buffer); 298 | 299 | VkImage create_image(int width, int height, int depth, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage); 300 | VkImageView create_imageview(VkImage image, VkFormat format); 301 | }; 302 | 303 | class VkBlobAllocatorPrivate; 304 | class NCNN_EXPORT VkBlobAllocator : public VkAllocator 305 | { 306 | public: 307 | explicit VkBlobAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 16 * 1024 * 1024); // 16M 308 | virtual ~VkBlobAllocator(); 309 | 310 | public: 311 | // release all budgets immediately 312 | virtual void clear(); 313 | 314 | virtual VkBufferMemory* fastMalloc(size_t size); 315 | virtual void fastFree(VkBufferMemory* ptr); 316 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); 317 | virtual void fastFree(VkImageMemory* ptr); 318 | 319 | private: 320 | VkBlobAllocator(const VkBlobAllocator&); 321 | VkBlobAllocator& operator=(const VkBlobAllocator&); 322 | 323 | private: 324 | VkBlobAllocatorPrivate* const d; 325 | }; 326 | 327 | class VkWeightAllocatorPrivate; 328 | class NCNN_EXPORT VkWeightAllocator : public VkAllocator 329 | { 330 | public: 331 | explicit VkWeightAllocator(const VulkanDevice* vkdev, size_t preferred_block_size = 8 * 1024 * 1024); // 8M 332 | virtual ~VkWeightAllocator(); 333 | 334 | public: 335 | // release all blocks immediately 336 | virtual void clear(); 337 | 338 | public: 339 | virtual VkBufferMemory* fastMalloc(size_t size); 340 | virtual void fastFree(VkBufferMemory* ptr); 341 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); 342 | virtual void fastFree(VkImageMemory* ptr); 343 | 344 | private: 345 | VkWeightAllocator(const VkWeightAllocator&); 346 | VkWeightAllocator& operator=(const VkWeightAllocator&); 347 | 348 | private: 349 | VkWeightAllocatorPrivate* const d; 350 | }; 351 | 352 | class VkStagingAllocatorPrivate; 353 | class NCNN_EXPORT VkStagingAllocator : public VkAllocator 354 | { 355 | public: 356 | explicit VkStagingAllocator(const VulkanDevice* vkdev); 357 | virtual ~VkStagingAllocator(); 358 | 359 | public: 360 | // ratio range 0 ~ 1 361 | // default cr = 0.75 362 | void set_size_compare_ratio(float scr); 363 | 364 | // release all budgets immediately 365 | virtual void clear(); 366 | 367 | virtual VkBufferMemory* fastMalloc(size_t size); 368 | virtual void fastFree(VkBufferMemory* ptr); 369 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); 370 | virtual void fastFree(VkImageMemory* ptr); 371 | 372 | private: 373 | VkStagingAllocator(const VkStagingAllocator&); 374 | VkStagingAllocator& operator=(const VkStagingAllocator&); 375 | 376 | private: 377 | VkStagingAllocatorPrivate* const d; 378 | }; 379 | 380 | class VkWeightStagingAllocatorPrivate; 381 | class NCNN_EXPORT VkWeightStagingAllocator : public VkAllocator 382 | { 383 | public: 384 | explicit VkWeightStagingAllocator(const VulkanDevice* vkdev); 385 | virtual ~VkWeightStagingAllocator(); 386 | 387 | public: 388 | virtual VkBufferMemory* fastMalloc(size_t size); 389 | virtual void fastFree(VkBufferMemory* ptr); 390 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); 391 | virtual void fastFree(VkImageMemory* ptr); 392 | 393 | private: 394 | VkWeightStagingAllocator(const VkWeightStagingAllocator&); 395 | VkWeightStagingAllocator& operator=(const VkWeightStagingAllocator&); 396 | 397 | private: 398 | VkWeightStagingAllocatorPrivate* const d; 399 | }; 400 | 401 | #if NCNN_PLATFORM_API 402 | #if __ANDROID_API__ >= 26 403 | class NCNN_EXPORT VkAndroidHardwareBufferImageAllocator : public VkAllocator 404 | { 405 | public: 406 | VkAndroidHardwareBufferImageAllocator(const VulkanDevice* _vkdev, AHardwareBuffer* _hb); 407 | virtual ~VkAndroidHardwareBufferImageAllocator(); 408 | 409 | public: 410 | virtual VkBufferMemory* fastMalloc(size_t size); 411 | virtual void fastFree(VkBufferMemory* ptr); 412 | virtual VkImageMemory* fastMalloc(int w, int h, int c, size_t elemsize, int elempack); 413 | virtual void fastFree(VkImageMemory* ptr); 414 | 415 | private: 416 | VkAndroidHardwareBufferImageAllocator(const VkAndroidHardwareBufferImageAllocator&); 417 | VkAndroidHardwareBufferImageAllocator& operator=(const VkAndroidHardwareBufferImageAllocator&); 418 | 419 | public: 420 | int init(); 421 | 422 | int width() const; 423 | int height() const; 424 | uint64_t external_format() const; 425 | 426 | public: 427 | AHardwareBuffer* hb; 428 | AHardwareBuffer_Desc bufferDesc; 429 | VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties; 430 | VkAndroidHardwareBufferPropertiesANDROID bufferProperties; 431 | VkSamplerYcbcrConversionKHR samplerYcbcrConversion; 432 | }; 433 | #endif // __ANDROID_API__ >= 26 434 | #endif // NCNN_PLATFORM_API 435 | 436 | #endif // NCNN_VULKAN 437 | 438 | } // namespace ncnn 439 | 440 | #endif // NCNN_ALLOCATOR_H 441 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/benchmark.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_BENCHMARK_H 16 | #define NCNN_BENCHMARK_H 17 | 18 | #include "layer.h" 19 | #include "mat.h" 20 | #include "platform.h" 21 | 22 | namespace ncnn { 23 | 24 | // get now timestamp in ms 25 | NCNN_EXPORT double get_current_time(); 26 | 27 | #if NCNN_BENCHMARK 28 | 29 | NCNN_EXPORT void benchmark(const Layer* layer, double start, double end); 30 | NCNN_EXPORT void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end); 31 | 32 | #endif // NCNN_BENCHMARK 33 | 34 | } // namespace ncnn 35 | 36 | #endif // NCNN_BENCHMARK_H 37 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/blob.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_BLOB_H 16 | #define NCNN_BLOB_H 17 | 18 | #include "mat.h" 19 | #include "platform.h" 20 | 21 | namespace ncnn { 22 | 23 | class NCNN_EXPORT Blob 24 | { 25 | public: 26 | // empty 27 | Blob(); 28 | 29 | public: 30 | #if NCNN_STRING 31 | // blob name 32 | std::string name; 33 | #endif // NCNN_STRING 34 | // layer index which produce this blob as output 35 | int producer; 36 | // layer index which need this blob as input 37 | int consumer; 38 | // shape hint 39 | Mat shape; 40 | }; 41 | 42 | } // namespace ncnn 43 | 44 | #endif // NCNN_BLOB_H 45 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/c_api.h: -------------------------------------------------------------------------------- 1 | /* Tencent is pleased to support the open source community by making ncnn available. 2 | * 3 | * Copyright (C) 2020 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 | 16 | #ifndef NCNN_C_API_H 17 | #define NCNN_C_API_H 18 | 19 | #include 20 | #include "platform.h" 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | NCNN_EXPORT const char* ncnn_version(); 27 | 28 | /* allocator api */ 29 | typedef struct __ncnn_allocator_t* ncnn_allocator_t; 30 | struct NCNN_EXPORT __ncnn_allocator_t 31 | { 32 | void* pthis; 33 | 34 | void* (*fast_malloc)(ncnn_allocator_t allocator, size_t size); 35 | void (*fast_free)(ncnn_allocator_t allocator, void* ptr); 36 | }; 37 | 38 | NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_pool_allocator(); 39 | NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator(); 40 | NCNN_EXPORT void ncnn_allocator_destroy(ncnn_allocator_t allocator); 41 | 42 | /* option api */ 43 | typedef struct __ncnn_option_t* ncnn_option_t; 44 | 45 | NCNN_EXPORT ncnn_option_t ncnn_option_create(); 46 | NCNN_EXPORT void ncnn_option_destroy(ncnn_option_t opt); 47 | 48 | NCNN_EXPORT int ncnn_option_get_num_threads(const ncnn_option_t opt); 49 | NCNN_EXPORT void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads); 50 | 51 | NCNN_EXPORT int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt); 52 | NCNN_EXPORT void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute); 53 | 54 | /* mat api */ 55 | typedef struct __ncnn_mat_t* ncnn_mat_t; 56 | 57 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator); 58 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator); 59 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator); 60 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator); 61 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator); 62 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator); 63 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator); 64 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator); 65 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator); 66 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator); 67 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator); 68 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d_elem(int w, int h, int d, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator); 69 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); 70 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); 71 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); 72 | NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d_elem(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator); 73 | NCNN_EXPORT void ncnn_mat_destroy(ncnn_mat_t mat); 74 | 75 | NCNN_EXPORT void ncnn_mat_fill_float(ncnn_mat_t mat, float v); 76 | 77 | NCNN_EXPORT ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator); 78 | NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator); 79 | NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator); 80 | NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator); 81 | NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator); 82 | 83 | NCNN_EXPORT int ncnn_mat_get_dims(const ncnn_mat_t mat); 84 | NCNN_EXPORT int ncnn_mat_get_w(const ncnn_mat_t mat); 85 | NCNN_EXPORT int ncnn_mat_get_h(const ncnn_mat_t mat); 86 | NCNN_EXPORT int ncnn_mat_get_d(const ncnn_mat_t mat); 87 | NCNN_EXPORT int ncnn_mat_get_c(const ncnn_mat_t mat); 88 | NCNN_EXPORT size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat); 89 | NCNN_EXPORT int ncnn_mat_get_elempack(const ncnn_mat_t mat); 90 | NCNN_EXPORT size_t ncnn_mat_get_cstep(const ncnn_mat_t mat); 91 | NCNN_EXPORT void* ncnn_mat_get_data(const ncnn_mat_t mat); 92 | 93 | NCNN_EXPORT void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c); 94 | 95 | #if NCNN_PIXEL 96 | 97 | /* mat pixel api */ 98 | #define NCNN_MAT_PIXEL_RGB 1 99 | #define NCNN_MAT_PIXEL_BGR 2 100 | #define NCNN_MAT_PIXEL_GRAY 3 101 | #define NCNN_MAT_PIXEL_RGBA 4 102 | #define NCNN_MAT_PIXEL_BGRA 5 103 | #define NCNN_MAT_PIXEL_X2Y(X, Y) (X | (Y << 16)) 104 | NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator); 105 | NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, ncnn_allocator_t allocator); 106 | NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, ncnn_allocator_t allocator); 107 | NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, ncnn_allocator_t allocator); 108 | NCNN_EXPORT void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride); 109 | NCNN_EXPORT void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride); 110 | 111 | #endif /* NCNN_PIXEL */ 112 | 113 | NCNN_EXPORT void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals); 114 | 115 | NCNN_EXPORT void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt); 116 | NCNN_EXPORT void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt); 117 | 118 | /* blob api */ 119 | typedef struct __ncnn_blob_t* ncnn_blob_t; 120 | 121 | #if NCNN_STRING 122 | NCNN_EXPORT const char* ncnn_blob_get_name(const ncnn_blob_t blob); 123 | #endif /* NCNN_STRING */ 124 | 125 | NCNN_EXPORT int ncnn_blob_get_producer(const ncnn_blob_t blob); 126 | NCNN_EXPORT int ncnn_blob_get_consumer(const ncnn_blob_t blob); 127 | 128 | NCNN_EXPORT void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c); 129 | 130 | /* paramdict api */ 131 | typedef struct __ncnn_paramdict_t* ncnn_paramdict_t; 132 | 133 | NCNN_EXPORT ncnn_paramdict_t ncnn_paramdict_create(); 134 | NCNN_EXPORT void ncnn_paramdict_destroy(ncnn_paramdict_t pd); 135 | 136 | NCNN_EXPORT int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id); 137 | 138 | NCNN_EXPORT int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def); 139 | NCNN_EXPORT float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def); 140 | NCNN_EXPORT ncnn_mat_t ncnn_paramdict_get_array(const ncnn_paramdict_t pd, int id, const ncnn_mat_t def); 141 | 142 | NCNN_EXPORT void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i); 143 | NCNN_EXPORT void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f); 144 | NCNN_EXPORT void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t v); 145 | 146 | /* datareader api */ 147 | typedef struct __ncnn_datareader_t* ncnn_datareader_t; 148 | struct NCNN_EXPORT __ncnn_datareader_t 149 | { 150 | void* pthis; 151 | 152 | #if NCNN_STRING 153 | int (*scan)(ncnn_datareader_t dr, const char* format, void* p); 154 | #endif /* NCNN_STRING */ 155 | size_t (*read)(ncnn_datareader_t dr, void* buf, size_t size); 156 | }; 157 | 158 | NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create(); 159 | #if NCNN_STDIO 160 | NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp); 161 | #endif /* NCNN_STDIO */ 162 | NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem); 163 | NCNN_EXPORT void ncnn_datareader_destroy(ncnn_datareader_t dr); 164 | 165 | /* modelbin api */ 166 | typedef struct __ncnn_modelbin_t* ncnn_modelbin_t; 167 | struct NCNN_EXPORT __ncnn_modelbin_t 168 | { 169 | void* pthis; 170 | 171 | ncnn_mat_t (*load_1d)(const ncnn_modelbin_t mb, int w, int type); 172 | ncnn_mat_t (*load_2d)(const ncnn_modelbin_t mb, int w, int h, int type); 173 | ncnn_mat_t (*load_3d)(const ncnn_modelbin_t mb, int w, int h, int c, int type); 174 | }; 175 | 176 | NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr); 177 | NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n); 178 | NCNN_EXPORT void ncnn_modelbin_destroy(ncnn_modelbin_t mb); 179 | 180 | /* layer api */ 181 | typedef struct __ncnn_layer_t* ncnn_layer_t; 182 | struct NCNN_EXPORT __ncnn_layer_t 183 | { 184 | void* pthis; 185 | 186 | int (*load_param)(ncnn_layer_t layer, const ncnn_paramdict_t pd); 187 | int (*load_model)(ncnn_layer_t layer, const ncnn_modelbin_t mb); 188 | 189 | int (*create_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt); 190 | int (*destroy_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt); 191 | 192 | int (*forward_1)(const ncnn_layer_t layer, const ncnn_mat_t bottom_blob, ncnn_mat_t* top_blob, const ncnn_option_t opt); 193 | int (*forward_n)(const ncnn_layer_t layer, const ncnn_mat_t* bottom_blobs, int n, ncnn_mat_t** top_blobs, int n2, const ncnn_option_t opt); 194 | 195 | int (*forward_inplace_1)(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt); 196 | int (*forward_inplace_n)(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt); 197 | }; 198 | 199 | NCNN_EXPORT ncnn_layer_t ncnn_layer_create(); 200 | NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex); 201 | #if NCNN_STRING 202 | NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_type(const char* type); 203 | #endif /* NCNN_STRING */ 204 | NCNN_EXPORT void ncnn_layer_destroy(ncnn_layer_t layer); 205 | 206 | #if NCNN_STRING 207 | NCNN_EXPORT const char* ncnn_layer_get_name(const ncnn_layer_t layer); 208 | #endif /* NCNN_STRING */ 209 | 210 | NCNN_EXPORT int ncnn_layer_get_typeindex(const ncnn_layer_t layer); 211 | #if NCNN_STRING 212 | NCNN_EXPORT const char* ncnn_layer_get_type(const ncnn_layer_t layer); 213 | #endif /* NCNN_STRING */ 214 | 215 | NCNN_EXPORT int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer); 216 | NCNN_EXPORT int ncnn_layer_get_support_inplace(const ncnn_layer_t layer); 217 | NCNN_EXPORT int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer); 218 | NCNN_EXPORT int ncnn_layer_get_support_packing(const ncnn_layer_t layer); 219 | NCNN_EXPORT int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer); 220 | NCNN_EXPORT int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer); 221 | NCNN_EXPORT int ncnn_layer_get_support_image_storage(const ncnn_layer_t layer); 222 | 223 | NCNN_EXPORT void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable); 224 | NCNN_EXPORT void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable); 225 | NCNN_EXPORT void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable); 226 | NCNN_EXPORT void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable); 227 | NCNN_EXPORT void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable); 228 | NCNN_EXPORT void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable); 229 | NCNN_EXPORT void ncnn_layer_set_support_image_storage(ncnn_layer_t layer, int enable); 230 | 231 | NCNN_EXPORT int ncnn_layer_get_bottom_count(const ncnn_layer_t layer); 232 | NCNN_EXPORT int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i); 233 | NCNN_EXPORT int ncnn_layer_get_top_count(const ncnn_layer_t layer); 234 | NCNN_EXPORT int ncnn_layer_get_top(const ncnn_layer_t layer, int i); 235 | 236 | NCNN_EXPORT void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c); 237 | NCNN_EXPORT void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c); 238 | 239 | /* layer factory function */ 240 | typedef ncnn_layer_t (*ncnn_layer_creator_t)(void* userdata); 241 | typedef void (*ncnn_layer_destroyer_t)(ncnn_layer_t layer, void* userdata); 242 | 243 | typedef struct __ncnn_net_custom_layer_factory_t* ncnn_net_custom_layer_factory_t; 244 | struct __ncnn_net_custom_layer_factory_t 245 | { 246 | ncnn_layer_creator_t creator; 247 | ncnn_layer_destroyer_t destroyer; 248 | void* userdata; 249 | ncnn_net_custom_layer_factory_t next; 250 | }; 251 | 252 | /* net api */ 253 | typedef struct __ncnn_net_t* ncnn_net_t; 254 | struct __ncnn_net_t 255 | { 256 | void* pthis; 257 | 258 | ncnn_net_custom_layer_factory_t custom_layer_factory; 259 | }; 260 | 261 | NCNN_EXPORT ncnn_net_t ncnn_net_create(); 262 | NCNN_EXPORT void ncnn_net_destroy(ncnn_net_t net); 263 | 264 | NCNN_EXPORT void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt); 265 | 266 | #if NCNN_STRING 267 | NCNN_EXPORT void ncnn_net_register_custom_layer_by_type(ncnn_net_t net, const char* type, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata); 268 | #endif /* NCNN_STRING */ 269 | NCNN_EXPORT void ncnn_net_register_custom_layer_by_typeindex(ncnn_net_t net, int typeindex, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata); 270 | 271 | #if NCNN_STDIO 272 | #if NCNN_STRING 273 | NCNN_EXPORT int ncnn_net_load_param(ncnn_net_t net, const char* path); 274 | #endif /* NCNN_STRING */ 275 | NCNN_EXPORT int ncnn_net_load_param_bin(ncnn_net_t net, const char* path); 276 | NCNN_EXPORT int ncnn_net_load_model(ncnn_net_t net, const char* path); 277 | #endif /* NCNN_STDIO */ 278 | 279 | #if NCNN_STDIO 280 | #if NCNN_STRING 281 | NCNN_EXPORT int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem); 282 | #endif /* NCNN_STRING */ 283 | #endif /* NCNN_STDIO */ 284 | NCNN_EXPORT int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem); 285 | NCNN_EXPORT int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem); 286 | 287 | #if NCNN_STRING 288 | NCNN_EXPORT int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr); 289 | #endif /* NCNN_STRING */ 290 | NCNN_EXPORT int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr); 291 | NCNN_EXPORT int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr); 292 | 293 | NCNN_EXPORT void ncnn_net_clear(ncnn_net_t net); 294 | 295 | /* extractor api */ 296 | typedef struct __ncnn_extractor_t* ncnn_extractor_t; 297 | 298 | NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net); 299 | NCNN_EXPORT void ncnn_extractor_destroy(ncnn_extractor_t ex); 300 | 301 | NCNN_EXPORT void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt); 302 | 303 | #if NCNN_STRING 304 | NCNN_EXPORT int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat); 305 | NCNN_EXPORT int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat); 306 | #endif /* NCNN_STRING */ 307 | NCNN_EXPORT int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat); 308 | NCNN_EXPORT int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat); 309 | 310 | #ifdef __cplusplus 311 | } /* extern "C" */ 312 | #endif 313 | 314 | #endif /* NCNN_C_API_H */ 315 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/command.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_COMMAND_H 16 | #define NCNN_COMMAND_H 17 | 18 | #include "platform.h" 19 | 20 | #if NCNN_VULKAN 21 | 22 | #include "mat.h" 23 | 24 | #include 25 | 26 | namespace ncnn { 27 | 28 | class Pipeline; 29 | #if NCNN_PLATFORM_API 30 | #if __ANDROID_API__ >= 26 31 | class ImportAndroidHardwareBufferPipeline; 32 | #endif // __ANDROID_API__ >= 26 33 | #endif // NCNN_PLATFORM_API 34 | class VkComputePrivate; 35 | class NCNN_EXPORT VkCompute 36 | { 37 | public: 38 | explicit VkCompute(const VulkanDevice* vkdev); 39 | virtual ~VkCompute(); 40 | 41 | public: 42 | void record_upload(const Mat& src, VkMat& dst, const Option& opt); 43 | 44 | void record_upload(const Mat& src, VkImageMat& dst, const Option& opt); 45 | 46 | void record_download(const VkMat& src, Mat& dst, const Option& opt); 47 | 48 | void record_download(const VkImageMat& src, Mat& dst, const Option& opt); 49 | 50 | void record_buffer_to_image(const VkMat& src, VkImageMat& dst, const Option& opt); 51 | 52 | void record_image_to_buffer(const VkImageMat& src, VkMat& dst, const Option& opt); 53 | 54 | void record_clone(const Mat& src, VkMat& dst, const Option& opt); 55 | 56 | void record_clone(const Mat& src, VkImageMat& dst, const Option& opt); 57 | 58 | void record_clone(const VkMat& src, Mat& dst, const Option& opt); 59 | 60 | void record_clone(const VkImageMat& src, Mat& dst, const Option& opt); 61 | 62 | void record_clone(const VkMat& src, VkMat& dst, const Option& opt); 63 | 64 | void record_clone(const VkImageMat& src, VkImageMat& dst, const Option& opt); 65 | 66 | void record_clone(const VkMat& src, VkImageMat& dst, const Option& opt); 67 | 68 | void record_clone(const VkImageMat& src, VkMat& dst, const Option& opt); 69 | 70 | void record_pipeline(const Pipeline* pipeline, const std::vector& bindings, const std::vector& constants, const VkMat& dispatcher); 71 | 72 | void record_pipeline(const Pipeline* pipeline, const std::vector& bindings, const std::vector& constants, const VkImageMat& dispatcher); 73 | 74 | void record_pipeline(const Pipeline* pipeline, const std::vector& buffer_bindings, const std::vector& image_bindings, const std::vector& constants, const VkMat& dispatcher); 75 | void record_pipeline(const Pipeline* pipeline, const std::vector& buffer_bindings, const std::vector& image_bindings, const std::vector& constants, const VkImageMat& dispatcher); 76 | void record_pipeline(const Pipeline* pipeline, const std::vector& buffer_bindings, const std::vector& image_bindings, const std::vector& constants, const Mat& dispatcher); 77 | 78 | #if NCNN_BENCHMARK 79 | void record_write_timestamp(uint32_t query); 80 | #endif // NCNN_BENCHMARK 81 | 82 | #if NCNN_PLATFORM_API 83 | #if __ANDROID_API__ >= 26 84 | void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkMat& dst); 85 | 86 | void record_import_android_hardware_buffer(const ImportAndroidHardwareBufferPipeline* pipeline, const VkImageMat& src, const VkImageMat& dst); 87 | #endif // __ANDROID_API__ >= 26 88 | #endif // NCNN_PLATFORM_API 89 | 90 | int submit_and_wait(); 91 | 92 | int reset(); 93 | 94 | #if NCNN_BENCHMARK 95 | int create_query_pool(uint32_t query_count); 96 | 97 | int get_query_pool_results(uint32_t first_query, uint32_t query_count, std::vector& results); 98 | #endif // NCNN_BENCHMARK 99 | 100 | protected: 101 | const VulkanDevice* vkdev; 102 | 103 | void barrier_readwrite(const VkMat& binding); 104 | void barrier_readwrite(const VkImageMat& binding); 105 | void barrier_readonly(const VkImageMat& binding); 106 | 107 | private: 108 | VkComputePrivate* const d; 109 | }; 110 | 111 | class VkTransferPrivate; 112 | class NCNN_EXPORT VkTransfer 113 | { 114 | public: 115 | explicit VkTransfer(const VulkanDevice* vkdev); 116 | virtual ~VkTransfer(); 117 | 118 | public: 119 | void record_upload(const Mat& src, VkMat& dst, const Option& opt, bool flatten = true); 120 | 121 | void record_upload(const Mat& src, VkImageMat& dst, const Option& opt); 122 | 123 | int submit_and_wait(); 124 | 125 | protected: 126 | const VulkanDevice* vkdev; 127 | 128 | private: 129 | VkTransferPrivate* const d; 130 | }; 131 | 132 | } // namespace ncnn 133 | 134 | #endif // NCNN_VULKAN 135 | 136 | #endif // NCNN_COMMAND_H 137 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/cpu.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_CPU_H 16 | #define NCNN_CPU_H 17 | 18 | #include 19 | 20 | #if defined __ANDROID__ || defined __linux__ 21 | #include // cpu_set_t 22 | #endif 23 | 24 | #include "platform.h" 25 | 26 | namespace ncnn { 27 | 28 | class NCNN_EXPORT CpuSet 29 | { 30 | public: 31 | CpuSet(); 32 | void enable(int cpu); 33 | void disable(int cpu); 34 | void disable_all(); 35 | bool is_enabled(int cpu) const; 36 | int num_enabled() const; 37 | 38 | public: 39 | #if defined __ANDROID__ || defined __linux__ 40 | cpu_set_t cpu_set; 41 | #endif 42 | #if __APPLE__ 43 | unsigned int policy; 44 | #endif 45 | }; 46 | 47 | // test optional cpu features 48 | // neon = armv7 neon or aarch64 asimd 49 | NCNN_EXPORT int cpu_support_arm_neon(); 50 | // vfpv4 = armv7 fp16 + fma 51 | NCNN_EXPORT int cpu_support_arm_vfpv4(); 52 | // asimdhp = aarch64 asimd half precision 53 | NCNN_EXPORT int cpu_support_arm_asimdhp(); 54 | // asimddp = aarch64 asimd dot product 55 | NCNN_EXPORT int cpu_support_arm_asimddp(); 56 | 57 | // avx2 = x86_64 avx2 + fma + f16c 58 | NCNN_EXPORT int cpu_support_x86_avx2(); 59 | 60 | // avx = x86_64 avx 61 | NCNN_EXPORT int cpu_support_x86_avx(); 62 | 63 | // msa = mips mas 64 | NCNN_EXPORT int cpu_support_mips_msa(); 65 | // mmi = loongson mmi 66 | NCNN_EXPORT int cpu_support_loongson_mmi(); 67 | 68 | // v = riscv vector 69 | NCNN_EXPORT int cpu_support_riscv_v(); 70 | // zfh = riscv half-precision float 71 | NCNN_EXPORT int cpu_support_riscv_zfh(); 72 | // vlenb = riscv vector length in bytes 73 | NCNN_EXPORT int cpu_riscv_vlenb(); 74 | 75 | // cpu info 76 | NCNN_EXPORT int get_cpu_count(); 77 | NCNN_EXPORT int get_little_cpu_count(); 78 | NCNN_EXPORT int get_big_cpu_count(); 79 | 80 | // bind all threads on little clusters if powersave enabled 81 | // affects HMP arch cpu like ARM big.LITTLE 82 | // only implemented on android at the moment 83 | // switching powersave is expensive and not thread-safe 84 | // 0 = all cores enabled(default) 85 | // 1 = only little clusters enabled 86 | // 2 = only big clusters enabled 87 | // return 0 if success for setter function 88 | NCNN_EXPORT int get_cpu_powersave(); 89 | NCNN_EXPORT int set_cpu_powersave(int powersave); 90 | 91 | // convenient wrapper 92 | NCNN_EXPORT const CpuSet& get_cpu_thread_affinity_mask(int powersave); 93 | 94 | // set explicit thread affinity 95 | NCNN_EXPORT int set_cpu_thread_affinity(const CpuSet& thread_affinity_mask); 96 | 97 | // misc function wrapper for openmp routines 98 | NCNN_EXPORT int get_omp_num_threads(); 99 | NCNN_EXPORT void set_omp_num_threads(int num_threads); 100 | 101 | NCNN_EXPORT int get_omp_dynamic(); 102 | NCNN_EXPORT void set_omp_dynamic(int dynamic); 103 | 104 | NCNN_EXPORT int get_omp_thread_num(); 105 | 106 | NCNN_EXPORT int get_kmp_blocktime(); 107 | NCNN_EXPORT void set_kmp_blocktime(int time_ms); 108 | 109 | // need to flush denormals on Intel Chipset. 110 | // Other architectures such as ARM can be added as needed. 111 | // 0 = DAZ OFF, FTZ OFF 112 | // 1 = DAZ ON , FTZ OFF 113 | // 2 = DAZ OFF, FTZ ON 114 | // 3 = DAZ ON, FTZ ON 115 | NCNN_EXPORT int get_flush_denormals(); 116 | NCNN_EXPORT int set_flush_denormals(int flush_denormals); 117 | 118 | } // namespace ncnn 119 | 120 | #endif // NCNN_CPU_H 121 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/datareader.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2019 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 NCNN_DATAREADER_H 16 | #define NCNN_DATAREADER_H 17 | 18 | #include "platform.h" 19 | #if NCNN_STDIO 20 | #include 21 | #endif 22 | 23 | #if NCNN_PLATFORM_API 24 | #if __ANDROID_API__ >= 9 25 | #include 26 | #endif 27 | #endif // NCNN_PLATFORM_API 28 | 29 | namespace ncnn { 30 | 31 | // data read wrapper 32 | class NCNN_EXPORT DataReader 33 | { 34 | public: 35 | DataReader(); 36 | virtual ~DataReader(); 37 | 38 | #if NCNN_STRING 39 | // parse plain param text 40 | // return 1 if scan success 41 | virtual int scan(const char* format, void* p) const; 42 | #endif // NCNN_STRING 43 | 44 | // read binary param and model data 45 | // return bytes read 46 | virtual size_t read(void* buf, size_t size) const; 47 | 48 | // get model data reference 49 | // return bytes referenced 50 | virtual size_t reference(size_t size, const void** buf) const; 51 | }; 52 | 53 | #if NCNN_STDIO 54 | class DataReaderFromStdioPrivate; 55 | class NCNN_EXPORT DataReaderFromStdio : public DataReader 56 | { 57 | public: 58 | explicit DataReaderFromStdio(FILE* fp); 59 | virtual ~DataReaderFromStdio(); 60 | 61 | #if NCNN_STRING 62 | virtual int scan(const char* format, void* p) const; 63 | #endif // NCNN_STRING 64 | virtual size_t read(void* buf, size_t size) const; 65 | 66 | private: 67 | DataReaderFromStdio(const DataReaderFromStdio&); 68 | DataReaderFromStdio& operator=(const DataReaderFromStdio&); 69 | 70 | private: 71 | DataReaderFromStdioPrivate* const d; 72 | }; 73 | #endif // NCNN_STDIO 74 | 75 | class DataReaderFromMemoryPrivate; 76 | class NCNN_EXPORT DataReaderFromMemory : public DataReader 77 | { 78 | public: 79 | explicit DataReaderFromMemory(const unsigned char*& mem); 80 | virtual ~DataReaderFromMemory(); 81 | 82 | #if NCNN_STRING 83 | virtual int scan(const char* format, void* p) const; 84 | #endif // NCNN_STRING 85 | virtual size_t read(void* buf, size_t size) const; 86 | virtual size_t reference(size_t size, const void** buf) const; 87 | 88 | private: 89 | DataReaderFromMemory(const DataReaderFromMemory&); 90 | DataReaderFromMemory& operator=(const DataReaderFromMemory&); 91 | 92 | private: 93 | DataReaderFromMemoryPrivate* const d; 94 | }; 95 | 96 | #if NCNN_PLATFORM_API 97 | #if __ANDROID_API__ >= 9 98 | class DataReaderFromAndroidAssetPrivate; 99 | class NCNN_EXPORT DataReaderFromAndroidAsset : public DataReader 100 | { 101 | public: 102 | explicit DataReaderFromAndroidAsset(AAsset* asset); 103 | virtual ~DataReaderFromAndroidAsset(); 104 | 105 | #if NCNN_STRING 106 | virtual int scan(const char* format, void* p) const; 107 | #endif // NCNN_STRING 108 | virtual size_t read(void* buf, size_t size) const; 109 | 110 | private: 111 | DataReaderFromAndroidAsset(const DataReaderFromAndroidAsset&); 112 | DataReaderFromAndroidAsset& operator=(const DataReaderFromAndroidAsset&); 113 | 114 | private: 115 | DataReaderFromAndroidAssetPrivate* const d; 116 | }; 117 | #endif // __ANDROID_API__ >= 9 118 | #endif // NCNN_PLATFORM_API 119 | 120 | } // namespace ncnn 121 | 122 | #endif // NCNN_DATAREADER_H 123 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/gpu.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2018 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 NCNN_GPU_H 16 | #define NCNN_GPU_H 17 | 18 | #include "platform.h" 19 | 20 | #if NCNN_VULKAN 21 | 22 | #include "mat.h" 23 | 24 | #include 25 | 26 | #include "vulkan_header_fix.h" 27 | 28 | namespace ncnn { 29 | 30 | // instance 31 | NCNN_EXPORT int create_gpu_instance(); 32 | NCNN_EXPORT void destroy_gpu_instance(); 33 | 34 | // instance extension capability 35 | extern int support_VK_KHR_external_memory_capabilities; 36 | extern int support_VK_KHR_get_physical_device_properties2; 37 | extern int support_VK_KHR_get_surface_capabilities2; 38 | extern int support_VK_KHR_surface; 39 | extern int support_VK_EXT_debug_utils; 40 | #if __ANDROID_API__ >= 26 41 | extern int support_VK_KHR_android_surface; 42 | #endif // __ANDROID_API__ >= 26 43 | 44 | // VK_KHR_external_memory_capabilities 45 | extern PFN_vkGetPhysicalDeviceExternalBufferPropertiesKHR vkGetPhysicalDeviceExternalBufferPropertiesKHR; 46 | 47 | // VK_KHR_get_physical_device_properties2 48 | extern PFN_vkGetPhysicalDeviceFeatures2KHR vkGetPhysicalDeviceFeatures2KHR; 49 | extern PFN_vkGetPhysicalDeviceProperties2KHR vkGetPhysicalDeviceProperties2KHR; 50 | extern PFN_vkGetPhysicalDeviceFormatProperties2KHR vkGetPhysicalDeviceFormatProperties2KHR; 51 | extern PFN_vkGetPhysicalDeviceImageFormatProperties2KHR vkGetPhysicalDeviceImageFormatProperties2KHR; 52 | extern PFN_vkGetPhysicalDeviceQueueFamilyProperties2KHR vkGetPhysicalDeviceQueueFamilyProperties2KHR; 53 | extern PFN_vkGetPhysicalDeviceMemoryProperties2KHR vkGetPhysicalDeviceMemoryProperties2KHR; 54 | extern PFN_vkGetPhysicalDeviceSparseImageFormatProperties2KHR vkGetPhysicalDeviceSparseImageFormatProperties2KHR; 55 | 56 | // VK_KHR_get_surface_capabilities2 57 | extern PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR vkGetPhysicalDeviceSurfaceCapabilities2KHR; 58 | extern PFN_vkGetPhysicalDeviceSurfaceFormats2KHR vkGetPhysicalDeviceSurfaceFormats2KHR; 59 | 60 | // VK_KHR_surface 61 | extern PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR; 62 | extern PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR; 63 | extern PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR; 64 | extern PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR; 65 | extern PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR; 66 | 67 | #if __ANDROID_API__ >= 26 68 | // VK_KHR_android_surface 69 | extern PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR; 70 | #endif // __ANDROID_API__ >= 26 71 | 72 | // get info 73 | NCNN_EXPORT int get_gpu_count(); 74 | NCNN_EXPORT int get_default_gpu_index(); 75 | 76 | class GpuInfoPrivate; 77 | class NCNN_EXPORT GpuInfo 78 | { 79 | public: 80 | explicit GpuInfo(); 81 | virtual ~GpuInfo(); 82 | 83 | // vulkan physical device 84 | VkPhysicalDevice physical_device() const; 85 | 86 | // memory properties 87 | const VkPhysicalDeviceMemoryProperties& physical_device_memory_properties() const; 88 | 89 | // info 90 | uint32_t api_version() const; 91 | uint32_t driver_version() const; 92 | uint32_t vendor_id() const; 93 | uint32_t device_id() const; 94 | const char* device_name() const; 95 | uint8_t* pipeline_cache_uuid() const; 96 | 97 | // 0 = discrete gpu 98 | // 1 = integrated gpu 99 | // 2 = virtual gpu 100 | // 3 = cpu 101 | int type() const; 102 | 103 | // hardware limit 104 | uint32_t max_shared_memory_size() const; 105 | uint32_t max_workgroup_count_x() const; 106 | uint32_t max_workgroup_count_y() const; 107 | uint32_t max_workgroup_count_z() const; 108 | uint32_t max_workgroup_invocations() const; 109 | uint32_t max_workgroup_size_x() const; 110 | uint32_t max_workgroup_size_y() const; 111 | uint32_t max_workgroup_size_z() const; 112 | size_t memory_map_alignment() const; 113 | size_t buffer_offset_alignment() const; 114 | size_t non_coherent_atom_size() const; 115 | size_t buffer_image_granularity() const; 116 | uint32_t max_image_dimension_1d() const; 117 | uint32_t max_image_dimension_2d() const; 118 | uint32_t max_image_dimension_3d() const; 119 | float timestamp_period() const; 120 | 121 | // runtime 122 | uint32_t compute_queue_family_index() const; 123 | uint32_t graphics_queue_family_index() const; 124 | uint32_t transfer_queue_family_index() const; 125 | 126 | uint32_t compute_queue_count() const; 127 | uint32_t graphics_queue_count() const; 128 | uint32_t transfer_queue_count() const; 129 | 130 | // property 131 | bool unified_compute_transfer_queue() const; 132 | 133 | // subgroup 134 | uint32_t subgroup_size() const; 135 | bool support_subgroup_basic() const; 136 | bool support_subgroup_vote() const; 137 | bool support_subgroup_ballot() const; 138 | bool support_subgroup_shuffle() const; 139 | 140 | // bug is not feature 141 | bool bug_storage_buffer_no_l1() const; 142 | bool bug_corrupted_online_pipeline_cache() const; 143 | bool bug_buffer_image_load_zero() const; 144 | 145 | // but sometimes bug is a feature 146 | bool bug_implicit_fp16_arithmetic() const; 147 | 148 | // fp16 and int8 feature 149 | bool support_fp16_packed() const; 150 | bool support_fp16_storage() const; 151 | bool support_fp16_arithmetic() const; 152 | bool support_int8_packed() const; 153 | bool support_int8_storage() const; 154 | bool support_int8_arithmetic() const; 155 | 156 | // ycbcr conversion feature 157 | bool support_ycbcr_conversion() const; 158 | 159 | // extension capability 160 | int support_VK_KHR_8bit_storage() const; 161 | int support_VK_KHR_16bit_storage() const; 162 | int support_VK_KHR_bind_memory2() const; 163 | int support_VK_KHR_create_renderpass2() const; 164 | int support_VK_KHR_dedicated_allocation() const; 165 | int support_VK_KHR_descriptor_update_template() const; 166 | int support_VK_KHR_external_memory() const; 167 | int support_VK_KHR_get_memory_requirements2() const; 168 | int support_VK_KHR_maintenance1() const; 169 | int support_VK_KHR_maintenance2() const; 170 | int support_VK_KHR_maintenance3() const; 171 | int support_VK_KHR_multiview() const; 172 | int support_VK_KHR_push_descriptor() const; 173 | int support_VK_KHR_sampler_ycbcr_conversion() const; 174 | int support_VK_KHR_shader_float16_int8() const; 175 | int support_VK_KHR_shader_float_controls() const; 176 | int support_VK_KHR_storage_buffer_storage_class() const; 177 | int support_VK_KHR_swapchain() const; 178 | int support_VK_EXT_descriptor_indexing() const; 179 | int support_VK_EXT_memory_budget() const; 180 | int support_VK_EXT_queue_family_foreign() const; 181 | #if __ANDROID_API__ >= 26 182 | int support_VK_ANDROID_external_memory_android_hardware_buffer() const; 183 | #endif // __ANDROID_API__ >= 26 184 | 185 | private: 186 | GpuInfo(const GpuInfo&); 187 | GpuInfo& operator=(const GpuInfo&); 188 | 189 | private: 190 | friend int create_gpu_instance(); 191 | GpuInfoPrivate* const d; 192 | }; 193 | 194 | NCNN_EXPORT const GpuInfo& get_gpu_info(int device_index = get_default_gpu_index()); 195 | 196 | class VkAllocator; 197 | class VkCompute; 198 | class Option; 199 | class PipelineCache; 200 | class VulkanDevicePrivate; 201 | class NCNN_EXPORT VulkanDevice 202 | { 203 | public: 204 | VulkanDevice(int device_index = get_default_gpu_index()); 205 | ~VulkanDevice(); 206 | 207 | const GpuInfo& info; 208 | 209 | VkDevice vkdevice() const; 210 | 211 | VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const; 212 | 213 | // with fixed workgroup size 214 | VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z) const; 215 | 216 | // helper for creating pipeline 217 | int create_descriptorset_layout(int binding_count, const int* binding_types, VkDescriptorSetLayout* descriptorset_layout) const; 218 | int create_pipeline_layout(int push_constant_count, VkDescriptorSetLayout descriptorset_layout, VkPipelineLayout* pipeline_layout) const; 219 | int create_pipeline(VkShaderModule shader_module, VkPipelineLayout pipeline_layout, const std::vector& specializations, VkPipeline* pipeline) const; 220 | int create_descriptor_update_template(int binding_count, const int* binding_types, VkDescriptorSetLayout descriptorset_layout, VkPipelineLayout pipeline_layout, VkDescriptorUpdateTemplateKHR* descriptor_update_template) const; 221 | 222 | uint32_t find_memory_index(uint32_t memory_type_bits, VkFlags required, VkFlags preferred, VkFlags preferred_not) const; 223 | bool is_mappable(uint32_t memory_type_index) const; 224 | bool is_coherent(uint32_t memory_type_index) const; 225 | 226 | VkQueue acquire_queue(uint32_t queue_family_index) const; 227 | void reclaim_queue(uint32_t queue_family_index, VkQueue queue) const; 228 | 229 | // allocator on this device 230 | VkAllocator* acquire_blob_allocator() const; 231 | void reclaim_blob_allocator(VkAllocator* allocator) const; 232 | 233 | VkAllocator* acquire_staging_allocator() const; 234 | void reclaim_staging_allocator(VkAllocator* allocator) const; 235 | 236 | // immutable sampler for texelfetch 237 | const VkSampler* immutable_texelfetch_sampler() const; 238 | 239 | // dummy buffer image 240 | VkMat get_dummy_buffer() const; 241 | VkImageMat get_dummy_image() const; 242 | VkImageMat get_dummy_image_readonly() const; 243 | 244 | // pipeline cache on this device 245 | const PipelineCache* get_pipeline_cache() const; 246 | 247 | // test image allocation 248 | bool shape_support_image_storage(const Mat& shape) const; 249 | 250 | // current gpu heap memory budget in MB 251 | uint32_t get_heap_budget() const; 252 | 253 | // utility operator 254 | void convert_packing(const VkMat& src, VkMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; 255 | void convert_packing(const VkImageMat& src, VkImageMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; 256 | void convert_packing(const VkMat& src, VkImageMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; 257 | void convert_packing(const VkImageMat& src, VkMat& dst, int dst_elempack, VkCompute& cmd, const Option& opt) const; 258 | 259 | // VK_KHR_bind_memory2 260 | PFN_vkBindBufferMemory2KHR vkBindBufferMemory2KHR; 261 | PFN_vkBindImageMemory2KHR vkBindImageMemory2KHR; 262 | 263 | // VK_KHR_create_renderpass2 264 | PFN_vkCmdBeginRenderPass2KHR vkCmdBeginRenderPass2KHR; 265 | PFN_vkCmdEndRenderPass2KHR vkCmdEndRenderPass2KHR; 266 | PFN_vkCmdNextSubpass2KHR vkCmdNextSubpass2KHR; 267 | PFN_vkCreateRenderPass2KHR vkCreateRenderPass2KHR; 268 | 269 | // VK_KHR_descriptor_update_template 270 | PFN_vkCreateDescriptorUpdateTemplateKHR vkCreateDescriptorUpdateTemplateKHR; 271 | PFN_vkDestroyDescriptorUpdateTemplateKHR vkDestroyDescriptorUpdateTemplateKHR; 272 | PFN_vkUpdateDescriptorSetWithTemplateKHR vkUpdateDescriptorSetWithTemplateKHR; 273 | 274 | // VK_KHR_get_memory_requirements2 275 | PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR; 276 | PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR; 277 | PFN_vkGetImageSparseMemoryRequirements2KHR vkGetImageSparseMemoryRequirements2KHR; 278 | 279 | // VK_KHR_maintenance1 280 | PFN_vkTrimCommandPoolKHR vkTrimCommandPoolKHR; 281 | 282 | // VK_KHR_maintenance3 283 | PFN_vkGetDescriptorSetLayoutSupportKHR vkGetDescriptorSetLayoutSupportKHR; 284 | 285 | // VK_KHR_push_descriptor 286 | PFN_vkCmdPushDescriptorSetWithTemplateKHR vkCmdPushDescriptorSetWithTemplateKHR; 287 | PFN_vkCmdPushDescriptorSetKHR vkCmdPushDescriptorSetKHR; 288 | 289 | // VK_KHR_sampler_ycbcr_conversion 290 | PFN_vkCreateSamplerYcbcrConversionKHR vkCreateSamplerYcbcrConversionKHR; 291 | PFN_vkDestroySamplerYcbcrConversionKHR vkDestroySamplerYcbcrConversionKHR; 292 | 293 | // VK_KHR_swapchain 294 | PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR; 295 | PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR; 296 | PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR; 297 | PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR; 298 | PFN_vkQueuePresentKHR vkQueuePresentKHR; 299 | 300 | #if __ANDROID_API__ >= 26 301 | // VK_ANDROID_external_memory_android_hardware_buffer 302 | PFN_vkGetAndroidHardwareBufferPropertiesANDROID vkGetAndroidHardwareBufferPropertiesANDROID; 303 | PFN_vkGetMemoryAndroidHardwareBufferANDROID vkGetMemoryAndroidHardwareBufferANDROID; 304 | #endif // __ANDROID_API__ >= 26 305 | 306 | protected: 307 | // device extension 308 | int init_device_extension(); 309 | 310 | private: 311 | VulkanDevice(const VulkanDevice&); 312 | VulkanDevice& operator=(const VulkanDevice&); 313 | 314 | private: 315 | VulkanDevicePrivate* const d; 316 | }; 317 | 318 | NCNN_EXPORT VulkanDevice* get_gpu_device(int device_index = get_default_gpu_index()); 319 | 320 | // online spirv compilation 321 | NCNN_EXPORT int compile_spirv_module(const char* comp_string, const Option& opt, std::vector& spirv); 322 | NCNN_EXPORT int compile_spirv_module(const char* comp_data, int comp_data_size, const Option& opt, std::vector& spirv); 323 | NCNN_EXPORT int compile_spirv_module(int shader_type_index, const Option& opt, std::vector& spirv); 324 | 325 | // info from spirv 326 | class NCNN_EXPORT ShaderInfo 327 | { 328 | public: 329 | int specialization_count; 330 | int binding_count; 331 | int push_constant_count; 332 | 333 | // 0 = null 334 | // 1 = storage buffer 335 | // 2 = storage image 336 | // 3 = combined image sampler 337 | int binding_types[16]; // 16 is large enough I think ... 338 | 339 | int reserved_0; 340 | int reserved_1; 341 | int reserved_2; 342 | int reserved_3; 343 | }; 344 | 345 | NCNN_EXPORT int resolve_shader_info(const uint32_t* spv_data, size_t spv_data_size, ShaderInfo& shader_info); 346 | 347 | } // namespace ncnn 348 | 349 | #endif // NCNN_VULKAN 350 | 351 | #endif // NCNN_GPU_H 352 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/layer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_LAYER_H 16 | #define NCNN_LAYER_H 17 | 18 | #include "mat.h" 19 | #include "modelbin.h" 20 | #include "option.h" 21 | #include "paramdict.h" 22 | #include "platform.h" 23 | 24 | #include 25 | 26 | #if NCNN_VULKAN 27 | #include "command.h" 28 | #include "pipeline.h" 29 | 30 | #include 31 | #endif // NCNN_VULKAN 32 | 33 | namespace ncnn { 34 | 35 | class NCNN_EXPORT Layer 36 | { 37 | public: 38 | // empty 39 | Layer(); 40 | // virtual destructor 41 | virtual ~Layer(); 42 | 43 | // load layer specific parameter from parsed dict 44 | // return 0 if success 45 | virtual int load_param(const ParamDict& pd); 46 | 47 | // load layer specific weight data from model binary 48 | // return 0 if success 49 | virtual int load_model(const ModelBin& mb); 50 | 51 | // layer implementation specific setup 52 | // return 0 if success 53 | virtual int create_pipeline(const Option& opt); 54 | 55 | // layer implementation specific clean 56 | // return 0 if success 57 | virtual int destroy_pipeline(const Option& opt); 58 | 59 | public: 60 | // one input and one output blob 61 | bool one_blob_only; 62 | 63 | // support inplace inference 64 | bool support_inplace; 65 | 66 | // support vulkan compute 67 | bool support_vulkan; 68 | 69 | // accept input blob with packed storage 70 | bool support_packing; 71 | 72 | // accept bf16 73 | bool support_bf16_storage; 74 | 75 | // accept fp16 76 | bool support_fp16_storage; 77 | 78 | // accept int8 79 | bool support_int8_storage; 80 | 81 | // shader image storage 82 | bool support_image_storage; 83 | 84 | // shader tensor storage 85 | bool support_tensor_storage; 86 | 87 | // TODO drop these fields 88 | bool support_weight_fp16_storage; 89 | 90 | bool support_reserved_0; 91 | bool support_reserved_1; 92 | bool support_reserved_2; 93 | bool support_reserved_3; 94 | bool support_reserved_4; 95 | bool support_reserved_5; 96 | bool support_reserved_6; 97 | bool support_reserved_7; 98 | bool support_reserved_8; 99 | bool support_reserved_9; 100 | bool support_reserved_10; 101 | bool support_reserved_11; 102 | bool support_reserved_12; 103 | bool support_reserved_13; 104 | 105 | public: 106 | // implement inference 107 | // return 0 if success 108 | virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; 109 | virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; 110 | 111 | // implement inplace inference 112 | // return 0 if success 113 | virtual int forward_inplace(std::vector& bottom_top_blobs, const Option& opt) const; 114 | virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const; 115 | 116 | #if NCNN_VULKAN 117 | public: 118 | // upload weight blob from host to device 119 | virtual int upload_model(VkTransfer& cmd, const Option& opt); 120 | 121 | public: 122 | // implement inference 123 | // return 0 if success 124 | virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; 125 | virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const; 126 | 127 | // implement inference 128 | // return 0 if success 129 | virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; 130 | virtual int forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const; 131 | 132 | // implement inplace inference 133 | // return 0 if success 134 | virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; 135 | virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const; 136 | 137 | // implement inplace inference 138 | // return 0 if success 139 | virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; 140 | virtual int forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const; 141 | 142 | public: 143 | // assigned immediately after creating this layer 144 | const VulkanDevice* vkdev; 145 | #endif // NCNN_VULKAN 146 | 147 | public: 148 | // custom user data 149 | void* userdata; 150 | // layer type index 151 | int typeindex; 152 | #if NCNN_STRING 153 | // layer type name 154 | std::string type; 155 | // layer name 156 | std::string name; 157 | #endif // NCNN_STRING 158 | // blob index which this layer needs as input 159 | std::vector bottoms; 160 | // blob index which this layer produces as output 161 | std::vector tops; 162 | // shape hint 163 | std::vector bottom_shapes; 164 | std::vector top_shapes; 165 | }; 166 | 167 | // layer factory function 168 | typedef Layer* (*layer_creator_func)(void*); 169 | typedef void (*layer_destroyer_func)(Layer*, void*); 170 | 171 | struct layer_registry_entry 172 | { 173 | #if NCNN_STRING 174 | // layer type name 175 | const char* name; 176 | #endif // NCNN_STRING 177 | // layer factory entry 178 | layer_creator_func creator; 179 | }; 180 | 181 | struct custom_layer_registry_entry 182 | { 183 | #if NCNN_STRING 184 | // layer type name 185 | const char* name; 186 | #endif // NCNN_STRING 187 | // layer factory entry 188 | layer_creator_func creator; 189 | layer_destroyer_func destroyer; 190 | void* userdata; 191 | }; 192 | 193 | #if NCNN_STRING 194 | // get layer type from type name 195 | NCNN_EXPORT int layer_to_index(const char* type); 196 | // create layer from type name 197 | NCNN_EXPORT Layer* create_layer(const char* type); 198 | #endif // NCNN_STRING 199 | // create layer from layer type 200 | NCNN_EXPORT Layer* create_layer(int index); 201 | 202 | #define DEFINE_LAYER_CREATOR(name) \ 203 | ::ncnn::Layer* name##_layer_creator(void* /*userdata*/) \ 204 | { \ 205 | return new name; \ 206 | } 207 | 208 | #define DEFINE_LAYER_DESTROYER(name) \ 209 | void name##_layer_destroyer(::ncnn::Layer* layer, void* /*userdata*/) \ 210 | { \ 211 | delete layer; \ 212 | } 213 | 214 | } // namespace ncnn 215 | 216 | #endif // NCNN_LAYER_H 217 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/layer_shader_type.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_LAYER_SHADER_TYPE_H 16 | #define NCNN_LAYER_SHADER_TYPE_H 17 | 18 | namespace ncnn { 19 | 20 | namespace LayerShaderType { 21 | enum LayerShaderType 22 | { 23 | #include "layer_shader_type_enum.h" 24 | }; 25 | } // namespace LayerShaderType 26 | 27 | } // namespace ncnn 28 | 29 | #endif // NCNN_LAYER_SHADER_TYPE_H 30 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/layer_shader_type_enum.h: -------------------------------------------------------------------------------- 1 | // Layer Shader Enum header 2 | // 3 | // This file is auto-generated by cmake, don't edit it. 4 | 5 | 6 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/layer_type.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_LAYER_TYPE_H 16 | #define NCNN_LAYER_TYPE_H 17 | 18 | namespace ncnn { 19 | 20 | namespace LayerType { 21 | enum LayerType 22 | { 23 | #include "layer_type_enum.h" 24 | CustomBit = (1 << 8), 25 | }; 26 | } // namespace LayerType 27 | 28 | } // namespace ncnn 29 | 30 | #endif // NCNN_LAYER_TYPE_H 31 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/layer_type_enum.h: -------------------------------------------------------------------------------- 1 | // Layer Type Enum header 2 | // 3 | // This file is auto-generated by cmake, don't edit it. 4 | 5 | AbsVal = 0, 6 | ArgMax = 1, 7 | BatchNorm = 2, 8 | Bias = 3, 9 | BNLL = 4, 10 | Concat = 5, 11 | Convolution = 6, 12 | Crop = 7, 13 | Deconvolution = 8, 14 | Dropout = 9, 15 | Eltwise = 10, 16 | ELU = 11, 17 | Embed = 12, 18 | Exp = 13, 19 | Flatten = 14, 20 | InnerProduct = 15, 21 | Input = 16, 22 | Log = 17, 23 | LRN = 18, 24 | MemoryData = 19, 25 | MVN = 20, 26 | Pooling = 21, 27 | Power = 22, 28 | PReLU = 23, 29 | Proposal = 24, 30 | Reduction = 25, 31 | ReLU = 26, 32 | Reshape = 27, 33 | ROIPooling = 28, 34 | Scale = 29, 35 | Sigmoid = 30, 36 | Slice = 31, 37 | Softmax = 32, 38 | Split = 33, 39 | SPP = 34, 40 | TanH = 35, 41 | Threshold = 36, 42 | Tile = 37, 43 | RNN = 38, 44 | LSTM = 39, 45 | BinaryOp = 40, 46 | UnaryOp = 41, 47 | ConvolutionDepthWise = 42, 48 | Padding = 43, 49 | Squeeze = 44, 50 | ExpandDims = 45, 51 | Normalize = 46, 52 | Permute = 47, 53 | PriorBox = 48, 54 | DetectionOutput = 49, 55 | Interp = 50, 56 | DeconvolutionDepthWise = 51, 57 | ShuffleChannel = 52, 58 | InstanceNorm = 53, 59 | Clip = 54, 60 | Reorg = 55, 61 | YoloDetectionOutput = 56, 62 | Quantize = 57, 63 | Dequantize = 58, 64 | Yolov3DetectionOutput = 59, 65 | PSROIPooling = 60, 66 | ROIAlign = 61, 67 | Packing = 62, 68 | Requantize = 63, 69 | Cast = 64, 70 | HardSigmoid = 65, 71 | SELU = 66, 72 | HardSwish = 67, 73 | Noop = 68, 74 | PixelShuffle = 69, 75 | DeepCopy = 70, 76 | Mish = 71, 77 | StatisticsPooling = 72, 78 | Swish = 73, 79 | Gemm = 74, 80 | GroupNorm = 75, 81 | LayerNorm = 76, 82 | Softplus = 77, 83 | GRU = 78, 84 | MultiHeadAttention = 79, 85 | GELU = 80, 86 | Convolution1D = 81, 87 | Pooling1D = 82, 88 | ConvolutionDepthWise1D = 83, 89 | Convolution3D = 84, 90 | ConvolutionDepthWise3D = 85, 91 | Pooling3D = 86, 92 | 93 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/modelbin.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_MODELBIN_H 16 | #define NCNN_MODELBIN_H 17 | 18 | #include "mat.h" 19 | 20 | namespace ncnn { 21 | 22 | class DataReader; 23 | class NCNN_EXPORT ModelBin 24 | { 25 | public: 26 | ModelBin(); 27 | virtual ~ModelBin(); 28 | // element type 29 | // 0 = auto 30 | // 1 = float32 31 | // 2 = float16 32 | // 3 = int8 33 | // load vec 34 | virtual Mat load(int w, int type) const = 0; 35 | // load image 36 | virtual Mat load(int w, int h, int type) const; 37 | // load dim 38 | virtual Mat load(int w, int h, int c, int type) const; 39 | }; 40 | 41 | class ModelBinFromDataReaderPrivate; 42 | class NCNN_EXPORT ModelBinFromDataReader : public ModelBin 43 | { 44 | public: 45 | explicit ModelBinFromDataReader(const DataReader& dr); 46 | virtual ~ModelBinFromDataReader(); 47 | 48 | virtual Mat load(int w, int type) const; 49 | 50 | private: 51 | ModelBinFromDataReader(const ModelBinFromDataReader&); 52 | ModelBinFromDataReader& operator=(const ModelBinFromDataReader&); 53 | 54 | private: 55 | ModelBinFromDataReaderPrivate* const d; 56 | }; 57 | 58 | class ModelBinFromMatArrayPrivate; 59 | class NCNN_EXPORT ModelBinFromMatArray : public ModelBin 60 | { 61 | public: 62 | // construct from weight blob array 63 | explicit ModelBinFromMatArray(const Mat* weights); 64 | virtual ~ModelBinFromMatArray(); 65 | 66 | virtual Mat load(int w, int type) const; 67 | 68 | private: 69 | ModelBinFromMatArray(const ModelBinFromMatArray&); 70 | ModelBinFromMatArray& operator=(const ModelBinFromMatArray&); 71 | 72 | private: 73 | ModelBinFromMatArrayPrivate* const d; 74 | }; 75 | 76 | } // namespace ncnn 77 | 78 | #endif // NCNN_MODELBIN_H 79 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/ncnn_export.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef NCNN_EXPORT_H 3 | #define NCNN_EXPORT_H 4 | 5 | #ifdef NCNN_STATIC_DEFINE 6 | # define NCNN_EXPORT 7 | # define NCNN_NO_EXPORT 8 | #else 9 | # ifndef NCNN_EXPORT 10 | # ifdef ncnn_EXPORTS 11 | /* We are building this library */ 12 | # define NCNN_EXPORT 13 | # else 14 | /* We are using this library */ 15 | # define NCNN_EXPORT 16 | # endif 17 | # endif 18 | 19 | # ifndef NCNN_NO_EXPORT 20 | # define NCNN_NO_EXPORT 21 | # endif 22 | #endif 23 | 24 | #ifndef NCNN_DEPRECATED 25 | # define NCNN_DEPRECATED __attribute__ ((__deprecated__)) 26 | #endif 27 | 28 | #ifndef NCNN_DEPRECATED_EXPORT 29 | # define NCNN_DEPRECATED_EXPORT NCNN_EXPORT NCNN_DEPRECATED 30 | #endif 31 | 32 | #ifndef NCNN_DEPRECATED_NO_EXPORT 33 | # define NCNN_DEPRECATED_NO_EXPORT NCNN_NO_EXPORT NCNN_DEPRECATED 34 | #endif 35 | 36 | #if 0 /* DEFINE_NO_DEPRECATED */ 37 | # ifndef NCNN_NO_DEPRECATED 38 | # define NCNN_NO_DEPRECATED 39 | # endif 40 | #endif 41 | 42 | #endif /* NCNN_EXPORT_H */ 43 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/net.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_NET_H 16 | #define NCNN_NET_H 17 | 18 | #include "blob.h" 19 | #include "layer.h" 20 | #include "mat.h" 21 | #include "option.h" 22 | #include "platform.h" 23 | 24 | #if NCNN_PLATFORM_API 25 | #if __ANDROID_API__ >= 9 26 | #include 27 | #endif // __ANDROID_API__ >= 9 28 | #endif // NCNN_PLATFORM_API 29 | 30 | namespace ncnn { 31 | 32 | #if NCNN_VULKAN 33 | class VkCompute; 34 | #endif // NCNN_VULKAN 35 | class DataReader; 36 | class Extractor; 37 | class NetPrivate; 38 | class NCNN_EXPORT Net 39 | { 40 | public: 41 | // empty init 42 | Net(); 43 | // clear and destroy 44 | virtual ~Net(); 45 | 46 | public: 47 | // option can be changed before loading 48 | Option opt; 49 | 50 | #if NCNN_VULKAN 51 | // set gpu device by index 52 | void set_vulkan_device(int device_index); 53 | 54 | // set gpu device by device handle, no owner transfer 55 | void set_vulkan_device(const VulkanDevice* vkdev); 56 | 57 | const VulkanDevice* vulkan_device() const; 58 | #endif // NCNN_VULKAN 59 | 60 | #if NCNN_STRING 61 | // register custom layer by layer type name 62 | // return 0 if success 63 | int register_custom_layer(const char* type, layer_creator_func creator, layer_destroyer_func destroyer = 0, void* userdata = 0); 64 | virtual int custom_layer_to_index(const char* type); 65 | #endif // NCNN_STRING 66 | // register custom layer by layer type 67 | // return 0 if success 68 | int register_custom_layer(int index, layer_creator_func creator, layer_destroyer_func destroyer = 0, void* userdata = 0); 69 | 70 | #if NCNN_STRING 71 | int load_param(const DataReader& dr); 72 | #endif // NCNN_STRING 73 | 74 | int load_param_bin(const DataReader& dr); 75 | 76 | int load_model(const DataReader& dr); 77 | 78 | #if NCNN_STDIO 79 | #if NCNN_STRING 80 | // load network structure from plain param file 81 | // return 0 if success 82 | int load_param(FILE* fp); 83 | int load_param(const char* protopath); 84 | int load_param_mem(const char* mem); 85 | #endif // NCNN_STRING 86 | // load network structure from binary param file 87 | // return 0 if success 88 | int load_param_bin(FILE* fp); 89 | int load_param_bin(const char* protopath); 90 | 91 | // load network weight data from model file 92 | // return 0 if success 93 | int load_model(FILE* fp); 94 | int load_model(const char* modelpath); 95 | #endif // NCNN_STDIO 96 | 97 | // load network structure from external memory 98 | // memory pointer must be 32-bit aligned 99 | // return bytes consumed 100 | int load_param(const unsigned char* mem); 101 | 102 | // reference network weight data from external memory 103 | // weight data is not copied but referenced 104 | // so external memory should be retained when used 105 | // memory pointer must be 32-bit aligned 106 | // return bytes consumed 107 | int load_model(const unsigned char* mem); 108 | 109 | #if NCNN_PLATFORM_API 110 | #if __ANDROID_API__ >= 9 111 | #if NCNN_STRING 112 | // convenient load network structure from android asset plain param file 113 | int load_param(AAsset* asset); 114 | int load_param(AAssetManager* mgr, const char* assetpath); 115 | #endif // NCNN_STRING 116 | // convenient load network structure from android asset binary param file 117 | int load_param_bin(AAsset* asset); 118 | int load_param_bin(AAssetManager* mgr, const char* assetpath); 119 | 120 | // convenient load network weight data from android asset model file 121 | int load_model(AAsset* asset); 122 | int load_model(AAssetManager* mgr, const char* assetpath); 123 | #endif // __ANDROID_API__ >= 9 124 | #endif // NCNN_PLATFORM_API 125 | 126 | // unload network structure and weight data 127 | void clear(); 128 | 129 | // construct an Extractor from network 130 | Extractor create_extractor() const; 131 | 132 | // get input/output indexes/names 133 | const std::vector& input_indexes() const; 134 | const std::vector& output_indexes() const; 135 | #if NCNN_STRING 136 | const std::vector& input_names() const; 137 | const std::vector& output_names() const; 138 | #endif 139 | 140 | const std::vector& blobs() const; 141 | const std::vector& layers() const; 142 | 143 | std::vector& mutable_blobs(); 144 | std::vector& mutable_layers(); 145 | 146 | protected: 147 | friend class Extractor; 148 | #if NCNN_STRING 149 | int find_blob_index_by_name(const char* name) const; 150 | int find_layer_index_by_name(const char* name) const; 151 | virtual Layer* create_custom_layer(const char* type); 152 | #endif // NCNN_STRING 153 | virtual Layer* create_custom_layer(int index); 154 | 155 | private: 156 | Net(const Net&); 157 | Net& operator=(const Net&); 158 | 159 | private: 160 | NetPrivate* const d; 161 | }; 162 | 163 | class ExtractorPrivate; 164 | class NCNN_EXPORT Extractor 165 | { 166 | public: 167 | virtual ~Extractor(); 168 | 169 | // copy 170 | Extractor(const Extractor&); 171 | 172 | // assign 173 | Extractor& operator=(const Extractor&); 174 | 175 | // clear blob mats and alloctors 176 | void clear(); 177 | 178 | // enable light mode 179 | // intermediate blob will be recycled when enabled 180 | // enabled by default 181 | void set_light_mode(bool enable); 182 | 183 | // set thread count for this extractor 184 | // this will overwrite the global setting 185 | // default count is system depended 186 | void set_num_threads(int num_threads); 187 | 188 | // set blob memory allocator 189 | void set_blob_allocator(Allocator* allocator); 190 | 191 | // set workspace memory allocator 192 | void set_workspace_allocator(Allocator* allocator); 193 | 194 | #if NCNN_VULKAN 195 | void set_vulkan_compute(bool enable); 196 | 197 | void set_blob_vkallocator(VkAllocator* allocator); 198 | 199 | void set_workspace_vkallocator(VkAllocator* allocator); 200 | 201 | void set_staging_vkallocator(VkAllocator* allocator); 202 | #endif // NCNN_VULKAN 203 | 204 | #if NCNN_STRING 205 | // set input by blob name 206 | // return 0 if success 207 | int input(const char* blob_name, const Mat& in); 208 | 209 | // get result by blob name 210 | // return 0 if success 211 | // type = 0, default 212 | // type = 1, do not convert fp16/bf16 or / and packing 213 | int extract(const char* blob_name, Mat& feat, int type = 0); 214 | #endif // NCNN_STRING 215 | 216 | // set input by blob index 217 | // return 0 if success 218 | int input(int blob_index, const Mat& in); 219 | 220 | // get result by blob index 221 | // return 0 if success 222 | // type = 0, default 223 | // type = 1, do not convert fp16/bf16 or / and packing 224 | int extract(int blob_index, Mat& feat, int type = 0); 225 | 226 | #if NCNN_VULKAN 227 | #if NCNN_STRING 228 | // set input by blob name 229 | // return 0 if success 230 | int input(const char* blob_name, const VkMat& in); 231 | 232 | // get result by blob name 233 | // return 0 if success 234 | int extract(const char* blob_name, VkMat& feat, VkCompute& cmd); 235 | 236 | // set input by blob name 237 | // return 0 if success 238 | int input(const char* blob_name, const VkImageMat& in); 239 | 240 | // get result by blob name 241 | // return 0 if success 242 | int extract(const char* blob_name, VkImageMat& feat, VkCompute& cmd); 243 | #endif // NCNN_STRING 244 | 245 | // set input by blob index 246 | // return 0 if success 247 | int input(int blob_index, const VkMat& in); 248 | 249 | // get result by blob index 250 | // return 0 if success 251 | int extract(int blob_index, VkMat& feat, VkCompute& cmd); 252 | 253 | // set input by blob index 254 | // return 0 if success 255 | int input(int blob_index, const VkImageMat& in); 256 | 257 | // get result by blob index 258 | // return 0 if success 259 | int extract(int blob_index, VkImageMat& feat, VkCompute& cmd); 260 | #endif // NCNN_VULKAN 261 | 262 | protected: 263 | friend Extractor Net::create_extractor() const; 264 | Extractor(const Net* net, size_t blob_count); 265 | 266 | private: 267 | ExtractorPrivate* const d; 268 | }; 269 | 270 | } // namespace ncnn 271 | 272 | #endif // NCNN_NET_H 273 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/option.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2019 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 NCNN_OPTION_H 16 | #define NCNN_OPTION_H 17 | 18 | #include "platform.h" 19 | 20 | namespace ncnn { 21 | 22 | #if NCNN_VULKAN 23 | class VkAllocator; 24 | class PipelineCache; 25 | #endif // NCNN_VULKAN 26 | 27 | class Allocator; 28 | class NCNN_EXPORT Option 29 | { 30 | public: 31 | // default option 32 | Option(); 33 | 34 | public: 35 | // light mode 36 | // intermediate blob will be recycled when enabled 37 | // enabled by default 38 | bool lightmode; 39 | 40 | // thread count 41 | // default value is the one returned by get_cpu_count() 42 | int num_threads; 43 | 44 | // blob memory allocator 45 | Allocator* blob_allocator; 46 | 47 | // workspace memory allocator 48 | Allocator* workspace_allocator; 49 | 50 | #if NCNN_VULKAN 51 | // blob memory allocator 52 | VkAllocator* blob_vkallocator; 53 | 54 | // workspace memory allocator 55 | VkAllocator* workspace_vkallocator; 56 | 57 | // staging memory allocator 58 | VkAllocator* staging_vkallocator; 59 | 60 | // pipeline cache 61 | PipelineCache* pipeline_cache; 62 | #endif // NCNN_VULKAN 63 | 64 | // the time openmp threads busy-wait for more work before going to sleep 65 | // default value is 20ms to keep the cores enabled 66 | // without too much extra power consumption afterwards 67 | int openmp_blocktime; 68 | 69 | // enable winograd convolution optimization 70 | // improve convolution 3x3 stride1 performance, may consume more memory 71 | // changes should be applied before loading network structure and weight 72 | // enabled by default 73 | bool use_winograd_convolution; 74 | 75 | // enable sgemm convolution optimization 76 | // improve convolution 1x1 stride1 performance, may consume more memory 77 | // changes should be applied before loading network structure and weight 78 | // enabled by default 79 | bool use_sgemm_convolution; 80 | 81 | // enable quantized int8 inference 82 | // use low-precision int8 path for quantized model 83 | // changes should be applied before loading network structure and weight 84 | // enabled by default 85 | bool use_int8_inference; 86 | 87 | // enable vulkan compute 88 | bool use_vulkan_compute; 89 | 90 | // enable bf16 data type for storage 91 | // improve most operator performance on all arm devices, may consume more memory 92 | bool use_bf16_storage; 93 | 94 | // enable options for gpu inference 95 | bool use_fp16_packed; 96 | bool use_fp16_storage; 97 | bool use_fp16_arithmetic; 98 | bool use_int8_packed; 99 | bool use_int8_storage; 100 | bool use_int8_arithmetic; 101 | 102 | // enable simd-friendly packed memory layout 103 | // improve all operator performance on all arm devices, will consume more memory 104 | // changes should be applied before loading network structure and weight 105 | // enabled by default 106 | bool use_packing_layout; 107 | 108 | bool use_shader_pack8; 109 | 110 | // subgroup option 111 | bool use_subgroup_basic; 112 | bool use_subgroup_vote; 113 | bool use_subgroup_ballot; 114 | bool use_subgroup_shuffle; 115 | 116 | // turn on for adreno 117 | bool use_image_storage; 118 | bool use_tensor_storage; 119 | 120 | // used for fp16 weight storage in AVX 121 | // TODO drop this option 122 | bool use_weight_fp16_storage; 123 | 124 | // enable DAZ(Denormals-Are-Zero) and FTZ(Flush-To-Zero) 125 | // default value is 3 126 | // 0 = DAZ OFF, FTZ OFF 127 | // 1 = DAZ ON , FTZ OFF 128 | // 2 = DAZ OFF, FTZ ON 129 | // 3 = DAZ ON, FTZ ON 130 | int flush_denormals; 131 | 132 | bool use_local_pool_allocator; 133 | 134 | bool use_reserved_1; 135 | bool use_reserved_2; 136 | bool use_reserved_3; 137 | bool use_reserved_4; 138 | bool use_reserved_5; 139 | bool use_reserved_6; 140 | bool use_reserved_7; 141 | bool use_reserved_8; 142 | bool use_reserved_9; 143 | bool use_reserved_10; 144 | bool use_reserved_11; 145 | }; 146 | 147 | } // namespace ncnn 148 | 149 | #endif // NCNN_OPTION_H 150 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/paramdict.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_PARAMDICT_H 16 | #define NCNN_PARAMDICT_H 17 | 18 | #include "mat.h" 19 | 20 | // at most 32 parameters 21 | #define NCNN_MAX_PARAM_COUNT 32 22 | 23 | namespace ncnn { 24 | 25 | class DataReader; 26 | class Net; 27 | class ParamDictPrivate; 28 | class NCNN_EXPORT ParamDict 29 | { 30 | public: 31 | // empty 32 | ParamDict(); 33 | 34 | virtual ~ParamDict(); 35 | 36 | // copy 37 | ParamDict(const ParamDict&); 38 | 39 | // assign 40 | ParamDict& operator=(const ParamDict&); 41 | 42 | // get type 43 | int type(int id) const; 44 | 45 | // get int 46 | int get(int id, int def) const; 47 | // get float 48 | float get(int id, float def) const; 49 | // get array 50 | Mat get(int id, const Mat& def) const; 51 | 52 | // set int 53 | void set(int id, int i); 54 | // set float 55 | void set(int id, float f); 56 | // set array 57 | void set(int id, const Mat& v); 58 | 59 | protected: 60 | friend class Net; 61 | 62 | void clear(); 63 | 64 | int load_param(const DataReader& dr); 65 | int load_param_bin(const DataReader& dr); 66 | 67 | private: 68 | ParamDictPrivate* const d; 69 | }; 70 | 71 | } // namespace ncnn 72 | 73 | #endif // NCNN_PARAMDICT_H 74 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/pipeline.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2019 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 NCNN_PIPELINE_H 16 | #define NCNN_PIPELINE_H 17 | 18 | #include "mat.h" 19 | #include "platform.h" 20 | #if NCNN_VULKAN 21 | #include "gpu.h" 22 | 23 | #include 24 | #endif // NCNN_VULKAN 25 | 26 | namespace ncnn { 27 | 28 | #if NCNN_VULKAN 29 | class Option; 30 | class PipelinePrivate; 31 | class NCNN_EXPORT Pipeline 32 | { 33 | public: 34 | explicit Pipeline(const VulkanDevice* vkdev); 35 | virtual ~Pipeline(); 36 | 37 | public: 38 | void set_optimal_local_size_xyz(int w = 4, int h = 4, int c = 4); 39 | void set_optimal_local_size_xyz(const Mat& local_size_xyz); 40 | void set_local_size_xyz(int w, int h, int c); 41 | 42 | int create(const uint32_t* spv_data, size_t spv_data_size, const std::vector& specializations); 43 | 44 | int create(int shader_type_index, const Option& opt, const std::vector& specializations); 45 | 46 | public: 47 | VkShaderModule shader_module() const; 48 | VkDescriptorSetLayout descriptorset_layout() const; 49 | VkPipelineLayout pipeline_layout() const; 50 | VkPipeline pipeline() const; 51 | VkDescriptorUpdateTemplateKHR descriptor_update_template() const; 52 | 53 | const ShaderInfo& shader_info() const; 54 | 55 | uint32_t local_size_x() const; 56 | uint32_t local_size_y() const; 57 | uint32_t local_size_z() const; 58 | 59 | protected: 60 | void set_shader_module(VkShaderModule shader_module); 61 | void set_descriptorset_layout(VkDescriptorSetLayout descriptorset_layout); 62 | void set_pipeline_layout(VkPipelineLayout pipeline_layout); 63 | void set_pipeline(VkPipeline pipeline); 64 | void set_descriptor_update_template(VkDescriptorUpdateTemplateKHR descriptor_update_template); 65 | 66 | void set_shader_info(const ShaderInfo& shader_info); 67 | 68 | public: 69 | const VulkanDevice* vkdev; 70 | 71 | private: 72 | Pipeline(const Pipeline&); 73 | Pipeline& operator=(const Pipeline&); 74 | 75 | private: 76 | PipelinePrivate* const d; 77 | }; 78 | 79 | #if NCNN_PLATFORM_API 80 | #if __ANDROID_API__ >= 26 81 | class VkCompute; 82 | class NCNN_EXPORT ImportAndroidHardwareBufferPipeline : private Pipeline 83 | { 84 | public: 85 | explicit ImportAndroidHardwareBufferPipeline(const VulkanDevice* vkdev); 86 | virtual ~ImportAndroidHardwareBufferPipeline(); 87 | 88 | int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, const Option& opt); 89 | int create(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator, int type_to, int rotate_from, int target_width, int target_height, const Option& opt); 90 | void destroy(); 91 | 92 | friend class VkCompute; 93 | 94 | protected: 95 | int create_shader_module(const Option& opt); 96 | int create_sampler(VkAndroidHardwareBufferImageAllocator* ahb_im_allocator); 97 | int create_descriptorset_layout(); 98 | 99 | public: 100 | int type_to; 101 | int rotate_from; 102 | bool need_resize; 103 | 104 | VkSampler sampler; 105 | }; 106 | #endif // __ANDROID_API__ >= 26 107 | #endif // NCNN_PLATFORM_API 108 | 109 | #endif // NCNN_VULKAN 110 | 111 | } // namespace ncnn 112 | 113 | #endif // NCNN_PIPELINE_H 114 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/pipelinecache.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_PIPELINECACHE_H 16 | #define NCNN_PIPELINECACHE_H 17 | 18 | #include "platform.h" 19 | 20 | #if NCNN_VULKAN 21 | #include 22 | #endif // NCNN_VULKAN 23 | 24 | #include "mat.h" 25 | #include "gpu.h" 26 | 27 | namespace ncnn { 28 | 29 | #if NCNN_VULKAN 30 | 31 | class VulkanDevice; 32 | class PipelineCachePrivate; 33 | class NCNN_EXPORT PipelineCache 34 | { 35 | public: 36 | explicit PipelineCache(const VulkanDevice* _vkdev); 37 | 38 | virtual ~PipelineCache(); 39 | 40 | void clear(); 41 | 42 | int get_pipeline(const uint32_t* spv_data, size_t spv_data_size, const std::vector& specializations, 43 | uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z, 44 | VkShaderModule* shader_module, 45 | VkDescriptorSetLayout* descriptorset_layout, 46 | VkPipelineLayout* pipeline_layout, 47 | VkPipeline* pipeline, 48 | VkDescriptorUpdateTemplateKHR* descriptor_update_template, 49 | ShaderInfo& shader_info) const; 50 | 51 | int get_pipeline(int shader_type_index, const Option& opt, const std::vector& specializations, 52 | uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z, 53 | VkShaderModule* shader_module, 54 | VkDescriptorSetLayout* descriptorset_layout, 55 | VkPipelineLayout* pipeline_layout, 56 | VkPipeline* pipeline, 57 | VkDescriptorUpdateTemplateKHR* descriptor_update_template, 58 | ShaderInfo& shader_info) const; 59 | 60 | protected: 61 | int create_shader_module(int shader_type_index, const Option& opt, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z, 62 | VkShaderModule* _shader_module, ShaderInfo& si) const; 63 | 64 | int new_pipeline(VkShaderModule shader_module, const ShaderInfo& shader_info, const std::vector& specializations, 65 | VkDescriptorSetLayout* descriptorset_layout, 66 | VkPipelineLayout* pipeline_layout, 67 | VkPipeline* pipeline, 68 | VkDescriptorUpdateTemplateKHR* descriptor_update_template) const; 69 | 70 | protected: 71 | const VulkanDevice* vkdev; 72 | 73 | private: 74 | PipelineCache(const PipelineCache&); 75 | PipelineCache& operator=(const PipelineCache&); 76 | 77 | private: 78 | PipelineCachePrivate* const d; 79 | }; 80 | 81 | #endif // NCNN_VULKAN 82 | 83 | } // namespace ncnn 84 | 85 | #endif // NCNN_PIPELINECACHE_H 86 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/platform.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_PLATFORM_H 16 | #define NCNN_PLATFORM_H 17 | 18 | #define NCNN_STDIO 0 19 | #define NCNN_STRING 0 20 | #define NCNN_SIMPLEOCV 0 21 | #define NCNN_SIMPLEOMP 0 22 | #define NCNN_SIMPLESTL 1 23 | #define NCNN_THREADS 0 24 | #define NCNN_BENCHMARK 0 25 | #define NCNN_PLATFORM_API 0 26 | #define NCNN_PIXEL 1 27 | #define NCNN_PIXEL_ROTATE 0 28 | #define NCNN_PIXEL_AFFINE 0 29 | #define NCNN_PIXEL_DRAWING 0 30 | #define NCNN_VULKAN 0 31 | #define NCNN_RUNTIME_CPU 0 32 | #define NCNN_AVX2 0 33 | #define NCNN_AVX 0 34 | #define NCNN_ARM82 0 35 | #define NCNN_ARM82DOT 0 36 | #define NCNN_MSA 0 37 | #define NCNN_MMI 0 38 | #define NCNN_RVV 0 39 | #define NCNN_INT8 0 40 | #define NCNN_BF16 0 41 | #define NCNN_FORCE_INLINE 1 42 | 43 | #define NCNN_VERSION_STRING "1.0.20211215" 44 | 45 | #include "ncnn_export.h" 46 | 47 | #ifdef __cplusplus 48 | 49 | #if NCNN_THREADS 50 | #if (defined _WIN32 && !(defined __MINGW32__)) 51 | #define WIN32_LEAN_AND_MEAN 52 | #include 53 | #include 54 | #else 55 | #include 56 | #endif 57 | #endif // NCNN_THREADS 58 | 59 | #if __ANDROID_API__ >= 26 60 | #define VK_USE_PLATFORM_ANDROID_KHR 61 | #endif // __ANDROID_API__ >= 26 62 | 63 | namespace ncnn { 64 | 65 | #if NCNN_THREADS 66 | #if (defined _WIN32 && !(defined __MINGW32__)) 67 | class NCNN_EXPORT Mutex 68 | { 69 | public: 70 | Mutex() { InitializeSRWLock(&srwlock); } 71 | ~Mutex() {} 72 | void lock() { AcquireSRWLockExclusive(&srwlock); } 73 | void unlock() { ReleaseSRWLockExclusive(&srwlock); } 74 | private: 75 | friend class ConditionVariable; 76 | // NOTE SRWLock is available from windows vista 77 | SRWLOCK srwlock; 78 | }; 79 | 80 | class NCNN_EXPORT ConditionVariable 81 | { 82 | public: 83 | ConditionVariable() { InitializeConditionVariable(&condvar); } 84 | ~ConditionVariable() {} 85 | void wait(Mutex& mutex) { SleepConditionVariableSRW(&condvar, &mutex.srwlock, INFINITE, 0); } 86 | void broadcast() { WakeAllConditionVariable(&condvar); } 87 | void signal() { WakeConditionVariable(&condvar); } 88 | private: 89 | CONDITION_VARIABLE condvar; 90 | }; 91 | 92 | static unsigned __stdcall start_wrapper(void* args); 93 | class NCNN_EXPORT Thread 94 | { 95 | public: 96 | Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); } 97 | ~Thread() {} 98 | void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); } 99 | private: 100 | friend unsigned __stdcall start_wrapper(void* args) 101 | { 102 | Thread* t = (Thread*)args; 103 | t->_start(t->_args); 104 | return 0; 105 | } 106 | HANDLE handle; 107 | void* (*_start)(void*); 108 | void* _args; 109 | }; 110 | 111 | class NCNN_EXPORT ThreadLocalStorage 112 | { 113 | public: 114 | ThreadLocalStorage() { key = TlsAlloc(); } 115 | ~ThreadLocalStorage() { TlsFree(key); } 116 | void set(void* value) { TlsSetValue(key, (LPVOID)value); } 117 | void* get() { return (void*)TlsGetValue(key); } 118 | private: 119 | DWORD key; 120 | }; 121 | #else // (defined _WIN32 && !(defined __MINGW32__)) 122 | class NCNN_EXPORT Mutex 123 | { 124 | public: 125 | Mutex() { pthread_mutex_init(&mutex, 0); } 126 | ~Mutex() { pthread_mutex_destroy(&mutex); } 127 | void lock() { pthread_mutex_lock(&mutex); } 128 | void unlock() { pthread_mutex_unlock(&mutex); } 129 | private: 130 | friend class ConditionVariable; 131 | pthread_mutex_t mutex; 132 | }; 133 | 134 | class NCNN_EXPORT ConditionVariable 135 | { 136 | public: 137 | ConditionVariable() { pthread_cond_init(&cond, 0); } 138 | ~ConditionVariable() { pthread_cond_destroy(&cond); } 139 | void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); } 140 | void broadcast() { pthread_cond_broadcast(&cond); } 141 | void signal() { pthread_cond_signal(&cond); } 142 | private: 143 | pthread_cond_t cond; 144 | }; 145 | 146 | class NCNN_EXPORT Thread 147 | { 148 | public: 149 | Thread(void* (*start)(void*), void* args = 0) { pthread_create(&t, 0, start, args); } 150 | ~Thread() {} 151 | void join() { pthread_join(t, 0); } 152 | private: 153 | pthread_t t; 154 | }; 155 | 156 | class NCNN_EXPORT ThreadLocalStorage 157 | { 158 | public: 159 | ThreadLocalStorage() { pthread_key_create(&key, 0); } 160 | ~ThreadLocalStorage() { pthread_key_delete(key); } 161 | void set(void* value) { pthread_setspecific(key, value); } 162 | void* get() { return pthread_getspecific(key); } 163 | private: 164 | pthread_key_t key; 165 | }; 166 | #endif // (defined _WIN32 && !(defined __MINGW32__)) 167 | #else // NCNN_THREADS 168 | class NCNN_EXPORT Mutex 169 | { 170 | public: 171 | Mutex() {} 172 | ~Mutex() {} 173 | void lock() {} 174 | void unlock() {} 175 | }; 176 | 177 | class NCNN_EXPORT ConditionVariable 178 | { 179 | public: 180 | ConditionVariable() {} 181 | ~ConditionVariable() {} 182 | void wait(Mutex& /*mutex*/) {} 183 | void broadcast() {} 184 | void signal() {} 185 | }; 186 | 187 | class NCNN_EXPORT Thread 188 | { 189 | public: 190 | Thread(void* (*/*start*/)(void*), void* /*args*/ = 0) {} 191 | ~Thread() {} 192 | void join() {} 193 | }; 194 | 195 | class NCNN_EXPORT ThreadLocalStorage 196 | { 197 | public: 198 | ThreadLocalStorage() { data = 0; } 199 | ~ThreadLocalStorage() {} 200 | void set(void* value) { data = value; } 201 | void* get() { return data; } 202 | private: 203 | void* data; 204 | }; 205 | #endif // NCNN_THREADS 206 | 207 | class NCNN_EXPORT MutexLockGuard 208 | { 209 | public: 210 | MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); } 211 | ~MutexLockGuard() { mutex.unlock(); } 212 | private: 213 | Mutex& mutex; 214 | }; 215 | 216 | } // namespace ncnn 217 | 218 | #if NCNN_SIMPLESTL 219 | #include "simplestl.h" 220 | #else 221 | #include 222 | #include 223 | #include 224 | #include 225 | #endif 226 | 227 | #endif // __cplusplus 228 | 229 | #if NCNN_STDIO 230 | #if NCNN_PLATFORM_API && __ANDROID_API__ >= 8 231 | #include 232 | #define NCNN_LOGE(...) do { \ 233 | fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); \ 234 | __android_log_print(ANDROID_LOG_WARN, "ncnn", ##__VA_ARGS__); } while(0) 235 | #else // NCNN_PLATFORM_API && __ANDROID_API__ >= 8 236 | #include 237 | #define NCNN_LOGE(...) do { \ 238 | fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); } while(0) 239 | #endif // NCNN_PLATFORM_API && __ANDROID_API__ >= 8 240 | #else 241 | #define NCNN_LOGE(...) 242 | #endif 243 | 244 | 245 | #if NCNN_FORCE_INLINE 246 | #ifdef _MSC_VER 247 | #define NCNN_FORCEINLINE __forceinline 248 | #elif defined(__GNUC__) 249 | #define NCNN_FORCEINLINE inline __attribute__((__always_inline__)) 250 | #elif defined(__CLANG__) 251 | #if __has_attribute(__always_inline__) 252 | #define NCNN_FORCEINLINE inline __attribute__((__always_inline__)) 253 | #else 254 | #define NCNN_FORCEINLINE inline 255 | #endif 256 | #else 257 | #define NCNN_FORCEINLINE inline 258 | #endif 259 | #else 260 | #define NCNN_FORCEINLINE inline 261 | #endif 262 | 263 | #endif // NCNN_PLATFORM_H 264 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/simpleocv.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2017 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 NCNN_SIMPLEOCV_H 16 | #define NCNN_SIMPLEOCV_H 17 | 18 | #include "platform.h" 19 | 20 | #if NCNN_SIMPLEOCV 21 | 22 | #include 23 | #include 24 | #include "allocator.h" 25 | #include "mat.h" 26 | 27 | #if defined(_MSC_VER) || defined(__GNUC__) 28 | #pragma push_macro("min") 29 | #pragma push_macro("max") 30 | #undef min 31 | #undef max 32 | #endif 33 | 34 | #ifndef NCNN_XADD 35 | using ncnn::NCNN_XADD; 36 | #endif 37 | 38 | typedef unsigned char uchar; 39 | typedef unsigned short ushort; 40 | typedef unsigned int uint; 41 | 42 | enum 43 | { 44 | CV_LOAD_IMAGE_UNCHANGED = -1, 45 | CV_LOAD_IMAGE_GRAYSCALE = 0, 46 | CV_LOAD_IMAGE_COLOR = 1, 47 | }; 48 | 49 | enum 50 | { 51 | CV_IMWRITE_JPEG_QUALITY = 1 52 | }; 53 | 54 | // minimal opencv style data structure implementation 55 | namespace cv { 56 | 57 | template 58 | static inline _Tp saturate_cast(int v) 59 | { 60 | return _Tp(v); 61 | } 62 | template<> 63 | inline uchar saturate_cast(int v) 64 | { 65 | return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); 66 | } 67 | 68 | template 69 | struct Scalar_ 70 | { 71 | Scalar_() 72 | { 73 | v[0] = 0; 74 | v[1] = 0; 75 | v[2] = 0; 76 | v[3] = 0; 77 | } 78 | Scalar_(_Tp _v0) 79 | { 80 | v[0] = _v0; 81 | v[1] = 0; 82 | v[2] = 0; 83 | v[3] = 0; 84 | } 85 | Scalar_(_Tp _v0, _Tp _v1, _Tp _v2) 86 | { 87 | v[0] = _v0; 88 | v[1] = _v1; 89 | v[2] = _v2; 90 | v[3] = 0; 91 | } 92 | Scalar_(_Tp _v0, _Tp _v1, _Tp _v2, _Tp _v3) 93 | { 94 | v[0] = _v0; 95 | v[1] = _v1; 96 | v[2] = _v2; 97 | v[3] = _v3; 98 | } 99 | 100 | const _Tp operator[](const int i) const 101 | { 102 | return v[i]; 103 | } 104 | 105 | _Tp operator[](const int i) 106 | { 107 | return v[i]; 108 | } 109 | 110 | _Tp v[4]; 111 | }; 112 | 113 | typedef Scalar_ Scalar; 114 | 115 | template 116 | struct Point_ 117 | { 118 | Point_() 119 | : x(0), y(0) 120 | { 121 | } 122 | Point_(_Tp _x, _Tp _y) 123 | : x(_x), y(_y) 124 | { 125 | } 126 | 127 | template 128 | operator Point_<_Tp2>() const 129 | { 130 | return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); 131 | } 132 | 133 | _Tp x; 134 | _Tp y; 135 | }; 136 | 137 | typedef Point_ Point; 138 | typedef Point_ Point2f; 139 | 140 | template 141 | struct Size_ 142 | { 143 | Size_() 144 | : width(0), height(0) 145 | { 146 | } 147 | Size_(_Tp _w, _Tp _h) 148 | : width(_w), height(_h) 149 | { 150 | } 151 | 152 | template 153 | operator Size_<_Tp2>() const 154 | { 155 | return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); 156 | } 157 | 158 | _Tp width; 159 | _Tp height; 160 | }; 161 | 162 | typedef Size_ Size; 163 | typedef Size_ Size2f; 164 | 165 | template 166 | struct Rect_ 167 | { 168 | Rect_() 169 | : x(0), y(0), width(0), height(0) 170 | { 171 | } 172 | Rect_(_Tp _x, _Tp _y, _Tp _w, _Tp _h) 173 | : x(_x), y(_y), width(_w), height(_h) 174 | { 175 | } 176 | Rect_(Point_<_Tp> _p, Size_<_Tp> _size) 177 | : x(_p.x), y(_p.y), width(_size.width), height(_size.height) 178 | { 179 | } 180 | 181 | template 182 | operator Rect_<_Tp2>() const 183 | { 184 | return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); 185 | } 186 | 187 | _Tp x; 188 | _Tp y; 189 | _Tp width; 190 | _Tp height; 191 | 192 | // area 193 | _Tp area() const 194 | { 195 | return width * height; 196 | } 197 | }; 198 | 199 | template 200 | static inline Rect_<_Tp>& operator&=(Rect_<_Tp>& a, const Rect_<_Tp>& b) 201 | { 202 | _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y); 203 | a.width = std::min(a.x + a.width, b.x + b.width) - x1; 204 | a.height = std::min(a.y + a.height, b.y + b.height) - y1; 205 | a.x = x1; 206 | a.y = y1; 207 | if (a.width <= 0 || a.height <= 0) 208 | a = Rect_<_Tp>(); 209 | return a; 210 | } 211 | 212 | template 213 | static inline Rect_<_Tp>& operator|=(Rect_<_Tp>& a, const Rect_<_Tp>& b) 214 | { 215 | _Tp x1 = std::min(a.x, b.x), y1 = std::min(a.y, b.y); 216 | a.width = std::max(a.x + a.width, b.x + b.width) - x1; 217 | a.height = std::max(a.y + a.height, b.y + b.height) - y1; 218 | a.x = x1; 219 | a.y = y1; 220 | return a; 221 | } 222 | 223 | template 224 | static inline Rect_<_Tp> operator&(const Rect_<_Tp>& a, const Rect_<_Tp>& b) 225 | { 226 | Rect_<_Tp> c = a; 227 | return c &= b; 228 | } 229 | 230 | template 231 | static inline Rect_<_Tp> operator|(const Rect_<_Tp>& a, const Rect_<_Tp>& b) 232 | { 233 | Rect_<_Tp> c = a; 234 | return c |= b; 235 | } 236 | 237 | typedef Rect_ Rect; 238 | typedef Rect_ Rect2f; 239 | 240 | #define CV_8UC1 1 241 | #define CV_8UC3 3 242 | #define CV_8UC4 4 243 | #define CV_32FC1 4 244 | 245 | struct NCNN_EXPORT Mat 246 | { 247 | Mat() 248 | : data(0), refcount(0), rows(0), cols(0), c(0) 249 | { 250 | } 251 | 252 | Mat(int _rows, int _cols, int flags) 253 | : data(0), refcount(0) 254 | { 255 | create(_rows, _cols, flags); 256 | } 257 | 258 | // copy 259 | Mat(const Mat& m) 260 | : data(m.data), refcount(m.refcount) 261 | { 262 | if (refcount) 263 | NCNN_XADD(refcount, 1); 264 | 265 | rows = m.rows; 266 | cols = m.cols; 267 | c = m.c; 268 | } 269 | 270 | Mat(int _rows, int _cols, int flags, void* _data) 271 | : data((unsigned char*)_data), refcount(0) 272 | { 273 | rows = _rows; 274 | cols = _cols; 275 | c = flags; 276 | } 277 | 278 | ~Mat() 279 | { 280 | release(); 281 | } 282 | 283 | // assign 284 | Mat& operator=(const Mat& m) 285 | { 286 | if (this == &m) 287 | return *this; 288 | 289 | if (m.refcount) 290 | NCNN_XADD(m.refcount, 1); 291 | 292 | release(); 293 | 294 | data = m.data; 295 | refcount = m.refcount; 296 | 297 | rows = m.rows; 298 | cols = m.cols; 299 | c = m.c; 300 | 301 | return *this; 302 | } 303 | 304 | Mat& operator=(const Scalar& s) 305 | { 306 | if (total() > 0) 307 | { 308 | uchar* p = data; 309 | for (int i = 0; i < cols * rows; i++) 310 | { 311 | for (int j = 0; j < c; j++) 312 | { 313 | *p++ = s[j]; 314 | } 315 | } 316 | } 317 | 318 | return *this; 319 | } 320 | 321 | void create(int _rows, int _cols, int flags) 322 | { 323 | release(); 324 | 325 | rows = _rows; 326 | cols = _cols; 327 | c = flags; 328 | 329 | if (total() > 0) 330 | { 331 | // refcount address must be aligned, so we expand totalsize here 332 | size_t totalsize = (total() + 3) >> 2 << 2; 333 | data = (uchar*)ncnn::fastMalloc(totalsize + (int)sizeof(*refcount)); 334 | refcount = (int*)(((uchar*)data) + totalsize); 335 | *refcount = 1; 336 | } 337 | } 338 | 339 | void release() 340 | { 341 | if (refcount && NCNN_XADD(refcount, -1) == 1) 342 | ncnn::fastFree(data); 343 | 344 | data = 0; 345 | 346 | rows = 0; 347 | cols = 0; 348 | c = 0; 349 | 350 | refcount = 0; 351 | } 352 | 353 | Mat clone() const 354 | { 355 | if (empty()) 356 | return Mat(); 357 | 358 | Mat m(rows, cols, c); 359 | 360 | if (total() > 0) 361 | { 362 | memcpy(m.data, data, total()); 363 | } 364 | 365 | return m; 366 | } 367 | 368 | bool empty() const 369 | { 370 | return data == 0 || total() == 0; 371 | } 372 | 373 | int channels() const 374 | { 375 | return c; 376 | } 377 | 378 | int type() const 379 | { 380 | return c; 381 | } 382 | 383 | size_t total() const 384 | { 385 | return cols * rows * c; 386 | } 387 | 388 | const uchar* ptr(int y) const 389 | { 390 | return data + y * cols * c; 391 | } 392 | 393 | uchar* ptr(int y) 394 | { 395 | return data + y * cols * c; 396 | } 397 | 398 | template 399 | const _Tp* ptr(int y) const 400 | { 401 | return (const _Tp*)data + y * cols * c; 402 | } 403 | 404 | template 405 | _Tp* ptr(int y) 406 | { 407 | return (_Tp*)data + y * cols * c; 408 | } 409 | 410 | // roi 411 | Mat operator()(const Rect& roi) const 412 | { 413 | if (empty()) 414 | return Mat(); 415 | 416 | Mat m(roi.height, roi.width, c); 417 | 418 | int sy = roi.y; 419 | for (int y = 0; y < roi.height; y++) 420 | { 421 | const uchar* sptr = ptr(sy) + roi.x * c; 422 | uchar* dptr = m.ptr(y); 423 | memcpy(dptr, sptr, roi.width * c); 424 | sy++; 425 | } 426 | 427 | return m; 428 | } 429 | 430 | uchar* data; 431 | 432 | // pointer to the reference counter; 433 | // when points to user-allocated data, the pointer is NULL 434 | int* refcount; 435 | 436 | int rows; 437 | int cols; 438 | 439 | int c; 440 | }; 441 | 442 | enum ImreadModes 443 | { 444 | IMREAD_UNCHANGED = -1, 445 | IMREAD_GRAYSCALE = 0, 446 | IMREAD_COLOR = 1 447 | }; 448 | 449 | NCNN_EXPORT Mat imread(const std::string& path, int flags = IMREAD_COLOR); 450 | 451 | enum ImwriteFlags 452 | { 453 | IMWRITE_JPEG_QUALITY = 1 454 | }; 455 | 456 | NCNN_EXPORT bool imwrite(const std::string& path, const Mat& m, const std::vector& params = std::vector()); 457 | 458 | NCNN_EXPORT void imshow(const std::string& name, const Mat& m); 459 | 460 | NCNN_EXPORT int waitKey(int delay = 0); 461 | 462 | #if NCNN_PIXEL 463 | NCNN_EXPORT void resize(const Mat& src, Mat& dst, const Size& size, float sw = 0.f, float sh = 0.f, int flags = 0); 464 | #endif // NCNN_PIXEL 465 | 466 | #if NCNN_PIXEL_DRAWING 467 | 468 | enum 469 | { 470 | FILLED = -1 471 | }; 472 | 473 | NCNN_EXPORT void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness = 1); 474 | 475 | NCNN_EXPORT void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness = 1); 476 | 477 | NCNN_EXPORT void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness = 1); 478 | 479 | NCNN_EXPORT void line(Mat& img, Point p0, Point p1, const Scalar& color, int thickness = 1); 480 | 481 | enum 482 | { 483 | FONT_HERSHEY_SIMPLEX = 0 484 | }; 485 | 486 | NCNN_EXPORT void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1); 487 | 488 | NCNN_EXPORT Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine); 489 | 490 | #endif // NCNN_PIXEL_DRAWING 491 | 492 | } // namespace cv 493 | 494 | #if defined(_MSC_VER) || defined(__GNUC__) 495 | #pragma pop_macro("min") 496 | #pragma pop_macro("max") 497 | #endif 498 | 499 | #endif // NCNN_SIMPLEOCV 500 | 501 | #endif // NCNN_SIMPLEOCV_H 502 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/simpleomp.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_SIMPLEOMP_H 16 | #define NCNN_SIMPLEOMP_H 17 | 18 | #include "platform.h" 19 | 20 | #if NCNN_SIMPLEOMP 21 | 22 | #include 23 | 24 | // This minimal openmp runtime implementation only supports the llvm openmp abi 25 | // and only supports #pragma omp parallel for num_threads(X) 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | NCNN_EXPORT int omp_get_max_threads(); 32 | 33 | NCNN_EXPORT void omp_set_num_threads(int num_threads); 34 | 35 | NCNN_EXPORT int omp_get_dynamic(); 36 | 37 | NCNN_EXPORT void omp_set_dynamic(int dynamic); 38 | 39 | NCNN_EXPORT int omp_get_num_threads(); 40 | 41 | NCNN_EXPORT int omp_get_thread_num(); 42 | 43 | NCNN_EXPORT int kmp_get_blocktime(); 44 | 45 | NCNN_EXPORT void kmp_set_blocktime(int blocktime); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif // NCNN_SIMPLEOMP 52 | 53 | #endif // NCNN_SIMPLEOMP_H 54 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/simplestl.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_SIMPLESTL_H 16 | #define NCNN_SIMPLESTL_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #if !NCNN_SIMPLESTL 23 | 24 | #include 25 | 26 | #else 27 | 28 | // allocation functions 29 | NCNN_EXPORT void* operator new(size_t size); 30 | NCNN_EXPORT void* operator new[](size_t size); 31 | // placement allocation functions 32 | NCNN_EXPORT void* operator new(size_t size, void* ptr); 33 | NCNN_EXPORT void* operator new[](size_t size, void* ptr); 34 | // deallocation functions 35 | NCNN_EXPORT void operator delete(void* ptr); 36 | NCNN_EXPORT void operator delete[](void* ptr); 37 | // deallocation functions since c++14 38 | #if __cplusplus >= 201402L 39 | NCNN_EXPORT void operator delete(void* ptr, size_t sz); 40 | NCNN_EXPORT void operator delete[](void* ptr, size_t sz); 41 | #endif 42 | // placement deallocation functions 43 | NCNN_EXPORT void operator delete(void* ptr, void* voidptr2); 44 | NCNN_EXPORT void operator delete[](void* ptr, void* voidptr2); 45 | 46 | #endif 47 | 48 | // minimal stl data structure implementation 49 | namespace std { 50 | 51 | template 52 | const T& max(const T& a, const T& b) 53 | { 54 | return (a < b) ? b : a; 55 | } 56 | 57 | template 58 | const T& min(const T& a, const T& b) 59 | { 60 | return (a > b) ? b : a; 61 | } 62 | 63 | template 64 | void swap(T& a, T& b) 65 | { 66 | T temp(a); 67 | a = b; 68 | b = temp; 69 | } 70 | 71 | template 72 | struct pair 73 | { 74 | pair() 75 | : first(), second() 76 | { 77 | } 78 | pair(const T1& t1, const T2& t2) 79 | : first(t1), second(t2) 80 | { 81 | } 82 | 83 | T1 first; 84 | T2 second; 85 | }; 86 | 87 | template 88 | bool operator==(const pair& x, const pair& y) 89 | { 90 | return (x.first == y.first && x.second == y.second); 91 | } 92 | template 93 | bool operator<(const pair& x, const pair& y) 94 | { 95 | return x.first < y.first || (!(y.first < x.first) && x.second < y.second); 96 | } 97 | template 98 | bool operator!=(const pair& x, const pair& y) 99 | { 100 | return !(x == y); 101 | } 102 | template 103 | bool operator>(const pair& x, const pair& y) 104 | { 105 | return y < x; 106 | } 107 | template 108 | bool operator<=(const pair& x, const pair& y) 109 | { 110 | return !(y < x); 111 | } 112 | template 113 | bool operator>=(const pair& x, const pair& y) 114 | { 115 | return !(x < y); 116 | } 117 | 118 | template 119 | pair make_pair(const T1& t1, const T2& t2) 120 | { 121 | return pair(t1, t2); 122 | } 123 | 124 | template 125 | struct node 126 | { 127 | node* prev_; 128 | node* next_; 129 | T data_; 130 | 131 | node() 132 | : prev_(0), next_(0), data_() 133 | { 134 | } 135 | node(const T& t) 136 | : prev_(0), next_(0), data_(t) 137 | { 138 | } 139 | }; 140 | 141 | template 142 | struct iter_list 143 | { 144 | iter_list() 145 | : curr_(0) 146 | { 147 | } 148 | iter_list(node* n) 149 | : curr_(n) 150 | { 151 | } 152 | iter_list(const iter_list& i) 153 | : curr_(i.curr_) 154 | { 155 | } 156 | ~iter_list() 157 | { 158 | } 159 | 160 | iter_list& operator=(const iter_list& i) 161 | { 162 | curr_ = i.curr_; 163 | return *this; 164 | } 165 | 166 | T& operator*() 167 | { 168 | return curr_->data_; 169 | } 170 | T* operator->() 171 | { 172 | return &(curr_->data_); 173 | } 174 | 175 | bool operator==(const iter_list& i) 176 | { 177 | return curr_ == i.curr_; 178 | } 179 | bool operator!=(const iter_list& i) 180 | { 181 | return curr_ != i.curr_; 182 | } 183 | 184 | iter_list& operator++() 185 | { 186 | curr_ = curr_->next_; 187 | return *this; 188 | } 189 | iter_list& operator--() 190 | { 191 | curr_ = curr_->prev_; 192 | return *this; 193 | } 194 | 195 | node* curr_; 196 | }; 197 | 198 | template 199 | struct list 200 | { 201 | typedef iter_list iterator; 202 | 203 | list() 204 | { 205 | head_ = new node(); 206 | tail_ = head_; 207 | count_ = 0; 208 | } 209 | ~list() 210 | { 211 | clear(); 212 | delete head_; 213 | } 214 | list(const list& l) 215 | { 216 | head_ = new node(); 217 | tail_ = head_; 218 | count_ = 0; 219 | 220 | for (iter_list i = l.begin(); i != l.end(); ++i) 221 | { 222 | push_back(*i); 223 | } 224 | } 225 | 226 | list& operator=(const list& l) 227 | { 228 | if (this == &l) 229 | { 230 | return *this; 231 | } 232 | clear(); 233 | 234 | for (iter_list i = l.begin(); i != l.end(); ++i) 235 | { 236 | push_back(*i); 237 | } 238 | return *this; 239 | } 240 | 241 | void clear() 242 | { 243 | while (count_ > 0) 244 | { 245 | pop_front(); 246 | } 247 | } 248 | 249 | void pop_front() 250 | { 251 | if (count_ > 0) 252 | { 253 | head_ = head_->next_; 254 | delete head_->prev_; 255 | head_->prev_ = 0; 256 | --count_; 257 | } 258 | } 259 | 260 | size_t size() const 261 | { 262 | return count_; 263 | } 264 | iter_list begin() const 265 | { 266 | return iter_list(head_); 267 | } 268 | iter_list end() const 269 | { 270 | return iter_list(tail_); 271 | } 272 | bool empty() const 273 | { 274 | return count_ == 0; 275 | } 276 | 277 | void push_back(const T& t) 278 | { 279 | if (count_ == 0) 280 | { 281 | head_ = new node(t); 282 | head_->prev_ = 0; 283 | head_->next_ = tail_; 284 | tail_->prev_ = head_; 285 | count_ = 1; 286 | } 287 | else 288 | { 289 | node* temp = new node(t); 290 | temp->prev_ = tail_->prev_; 291 | temp->next_ = tail_; 292 | tail_->prev_->next_ = temp; 293 | tail_->prev_ = temp; 294 | ++count_; 295 | } 296 | } 297 | 298 | iter_list erase(iter_list pos) 299 | { 300 | if (pos != end()) 301 | { 302 | node* temp = pos.curr_; 303 | if (temp == head_) 304 | { 305 | ++pos; 306 | temp->next_->prev_ = 0; 307 | head_ = temp->next_; 308 | } 309 | else 310 | { 311 | --pos; 312 | temp->next_->prev_ = temp->prev_; 313 | temp->prev_->next_ = temp->next_; 314 | ++pos; 315 | } 316 | delete temp; 317 | --count_; 318 | } 319 | return pos; 320 | } 321 | 322 | protected: 323 | node* head_; 324 | node* tail_; 325 | size_t count_; 326 | }; 327 | 328 | template 329 | struct greater 330 | { 331 | bool operator()(const T& x, const T& y) const 332 | { 333 | return (x > y); 334 | } 335 | }; 336 | 337 | template 338 | struct less 339 | { 340 | bool operator()(const T& x, const T& y) const 341 | { 342 | return (x < y); 343 | } 344 | }; 345 | 346 | template 347 | void partial_sort(RandomAccessIter first, RandomAccessIter middle, RandomAccessIter last, Compare comp) 348 | { 349 | // [TODO] heap sort should be used here, but we simply use bubble sort now 350 | for (RandomAccessIter i = first; i < middle; ++i) 351 | { 352 | // bubble sort 353 | for (RandomAccessIter j = last - 1; j > first; --j) 354 | { 355 | if (comp(*j, *(j - 1))) 356 | { 357 | swap(*j, *(j - 1)); 358 | } 359 | } 360 | } 361 | } 362 | 363 | template 364 | struct vector 365 | { 366 | vector() 367 | : data_(0), size_(0), capacity_(0) 368 | { 369 | } 370 | vector(const size_t new_size, const T& value = T()) 371 | : data_(0), size_(0), capacity_(0) 372 | { 373 | resize(new_size, value); 374 | } 375 | ~vector() 376 | { 377 | clear(); 378 | } 379 | vector(const vector& v) 380 | : data_(0), size_(0), capacity_(0) 381 | { 382 | resize(v.size()); 383 | for (size_t i = 0; i < size_; i++) 384 | { 385 | data_[i] = v.data_[i]; 386 | } 387 | } 388 | 389 | vector& operator=(const vector& v) 390 | { 391 | if (this == &v) 392 | { 393 | return *this; 394 | } 395 | resize(0); 396 | resize(v.size()); 397 | for (size_t i = 0; i < size_; i++) 398 | { 399 | data_[i] = v.data_[i]; 400 | } 401 | return *this; 402 | } 403 | 404 | void resize(const size_t new_size, const T& value = T()) 405 | { 406 | try_alloc(new_size); 407 | if (new_size > size_) 408 | { 409 | for (size_t i = size_; i < new_size; i++) 410 | { 411 | new (&data_[i]) T(value); 412 | } 413 | } 414 | else if (new_size < size_) 415 | { 416 | for (size_t i = new_size; i < size_; i++) 417 | { 418 | data_[i].~T(); 419 | } 420 | } 421 | size_ = new_size; 422 | } 423 | 424 | void clear() 425 | { 426 | for (size_t i = 0; i < size_; i++) 427 | { 428 | data_[i].~T(); 429 | } 430 | delete[](char*) data_; 431 | data_ = 0; 432 | size_ = 0; 433 | capacity_ = 0; 434 | } 435 | 436 | T* data() const 437 | { 438 | return data_; 439 | } 440 | size_t size() const 441 | { 442 | return size_; 443 | } 444 | T& operator[](size_t i) const 445 | { 446 | return data_[i]; 447 | } 448 | T* begin() const 449 | { 450 | return &data_[0]; 451 | } 452 | T* end() const 453 | { 454 | return &data_[size_]; 455 | } 456 | bool empty() const 457 | { 458 | return size_ == 0; 459 | } 460 | 461 | void push_back(const T& t) 462 | { 463 | try_alloc(size_ + 1); 464 | new (&data_[size_]) T(t); 465 | size_++; 466 | } 467 | 468 | void insert(T* pos, T* b, T* e) 469 | { 470 | vector* v = 0; 471 | if (b >= begin() && b < end()) 472 | { 473 | //the same vector 474 | v = new vector(*this); 475 | b = v->begin() + (b - begin()); 476 | e = v->begin() + (e - begin()); 477 | } 478 | size_t diff = pos - begin(); 479 | try_alloc(size_ + (e - b)); 480 | pos = begin() + diff; 481 | memmove(pos + (e - b), pos, (end() - pos) * sizeof(T)); 482 | size_t len = e - b; 483 | size_ += len; 484 | for (size_t i = 0; i < len; i++) 485 | { 486 | *pos = *b; 487 | pos++; 488 | b++; 489 | } 490 | delete v; 491 | } 492 | 493 | T* erase(T* pos) 494 | { 495 | pos->~T(); 496 | memmove(pos, pos + 1, (end() - pos - 1) * sizeof(T)); 497 | size_--; 498 | return pos; 499 | } 500 | 501 | protected: 502 | T* data_; 503 | size_t size_; 504 | size_t capacity_; 505 | void try_alloc(size_t new_size) 506 | { 507 | if (new_size * 3 / 2 > capacity_ / 2) 508 | { 509 | capacity_ = new_size * 2; 510 | T* new_data = (T*)new char[capacity_ * sizeof(T)]; 511 | memset(new_data, 0, capacity_ * sizeof(T)); 512 | if (data_) 513 | { 514 | memmove(new_data, data_, sizeof(T) * size_); 515 | delete[](char*) data_; 516 | } 517 | data_ = new_data; 518 | } 519 | } 520 | }; 521 | 522 | struct NCNN_EXPORT string : public vector 523 | { 524 | string() 525 | { 526 | } 527 | string(const char* str) 528 | { 529 | size_t len = strlen(str); 530 | resize(len); 531 | memcpy(data_, str, len); 532 | } 533 | const char* c_str() const 534 | { 535 | return (const char*)data_; 536 | } 537 | bool operator==(const string& str2) const 538 | { 539 | return strcmp(data_, str2.data_) == 0; 540 | } 541 | bool operator==(const char* str2) const 542 | { 543 | return strcmp(data_, str2) == 0; 544 | } 545 | bool operator!=(const char* str2) const 546 | { 547 | return strcmp(data_, str2) != 0; 548 | } 549 | string& operator+=(const string& str1) 550 | { 551 | insert(end(), str1.begin(), str1.end()); 552 | return *this; 553 | } 554 | }; 555 | 556 | inline string operator+(const string& str1, const string& str2) 557 | { 558 | string str(str1); 559 | str.insert(str.end(), str2.begin(), str2.end()); 560 | return str; 561 | } 562 | 563 | } // namespace std 564 | 565 | #endif // NCNN_SIMPLESTL_H 566 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/include/ncnn/vulkan_header_fix.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making ncnn available. 2 | // 3 | // Copyright (C) 2020 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 NCNN_VULKAN_HEADER_FIX_H 16 | #define NCNN_VULKAN_HEADER_FIX_H 17 | 18 | #include 19 | 20 | // This header contains new structure and function declearation to fix build with old vulkan sdk 21 | 22 | #if VK_HEADER_VERSION < 70 23 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES (VkStructureType)1000094000 24 | typedef enum VkSubgroupFeatureFlagBits 25 | { 26 | VK_SUBGROUP_FEATURE_BASIC_BIT = 0x00000001, 27 | VK_SUBGROUP_FEATURE_VOTE_BIT = 0x00000002, 28 | VK_SUBGROUP_FEATURE_ARITHMETIC_BIT = 0x00000004, 29 | VK_SUBGROUP_FEATURE_BALLOT_BIT = 0x00000008, 30 | VK_SUBGROUP_FEATURE_SHUFFLE_BIT = 0x00000010, 31 | VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT = 0x00000020, 32 | VK_SUBGROUP_FEATURE_CLUSTERED_BIT = 0x00000040, 33 | VK_SUBGROUP_FEATURE_QUAD_BIT = 0x00000080, 34 | VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV = 0x00000100, 35 | VK_SUBGROUP_FEATURE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF 36 | } VkSubgroupFeatureFlagBits; 37 | typedef VkFlags VkSubgroupFeatureFlags; 38 | typedef struct VkPhysicalDeviceSubgroupProperties 39 | { 40 | VkStructureType sType; 41 | void* pNext; 42 | uint32_t subgroupSize; 43 | VkShaderStageFlags supportedStages; 44 | VkSubgroupFeatureFlags supportedOperations; 45 | VkBool32 quadOperationsInAllStages; 46 | } VkPhysicalDeviceSubgroupProperties; 47 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES (VkStructureType)1000168000 48 | #define VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_SUPPORT (VkStructureType)1000168001 49 | typedef struct VkPhysicalDeviceMaintenance3Properties 50 | { 51 | VkStructureType sType; 52 | void* pNext; 53 | uint32_t maxPerSetDescriptors; 54 | VkDeviceSize maxMemoryAllocationSize; 55 | } VkPhysicalDeviceMaintenance3Properties; 56 | typedef struct VkDescriptorSetLayoutSupport 57 | { 58 | VkStructureType sType; 59 | void* pNext; 60 | VkBool32 supported; 61 | } VkDescriptorSetLayoutSupport; 62 | typedef VkPhysicalDeviceMaintenance3Properties VkPhysicalDeviceMaintenance3PropertiesKHR; 63 | typedef VkDescriptorSetLayoutSupport VkDescriptorSetLayoutSupportKHR; 64 | typedef void(VKAPI_PTR* PFN_vkGetDescriptorSetLayoutSupportKHR)(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, VkDescriptorSetLayoutSupport* pSupport); 65 | #endif // VK_HEADER_VERSION < 70 66 | 67 | #if VK_HEADER_VERSION < 80 68 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR (VkStructureType)1000177000 69 | typedef struct VkPhysicalDevice8BitStorageFeaturesKHR 70 | { 71 | VkStructureType sType; 72 | void* pNext; 73 | VkBool32 storageBuffer8BitAccess; 74 | VkBool32 uniformAndStorageBuffer8BitAccess; 75 | VkBool32 storagePushConstant8; 76 | } VkPhysicalDevice8BitStorageFeaturesKHR; 77 | #define VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2_KHR (VkStructureType)1000109000 78 | #define VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR (VkStructureType)1000109001 79 | #define VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2_KHR (VkStructureType)1000109002 80 | #define VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2_KHR (VkStructureType)1000109003 81 | #define VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR (VkStructureType)1000109004 82 | #define VK_STRUCTURE_TYPE_SUBPASS_BEGIN_INFO_KHR (VkStructureType)1000109005 83 | #define VK_STRUCTURE_TYPE_SUBPASS_END_INFO_KHR (VkStructureType)1000109006 84 | typedef struct VkAttachmentDescription2KHR 85 | { 86 | VkStructureType sType; 87 | const void* pNext; 88 | VkAttachmentDescriptionFlags flags; 89 | VkFormat format; 90 | VkSampleCountFlagBits samples; 91 | VkAttachmentLoadOp loadOp; 92 | VkAttachmentStoreOp storeOp; 93 | VkAttachmentLoadOp stencilLoadOp; 94 | VkAttachmentStoreOp stencilStoreOp; 95 | VkImageLayout initialLayout; 96 | VkImageLayout finalLayout; 97 | } VkAttachmentDescription2KHR; 98 | typedef struct VkAttachmentReference2KHR 99 | { 100 | VkStructureType sType; 101 | const void* pNext; 102 | uint32_t attachment; 103 | VkImageLayout layout; 104 | VkImageAspectFlags aspectMask; 105 | } VkAttachmentReference2KHR; 106 | typedef struct VkSubpassDescription2KHR 107 | { 108 | VkStructureType sType; 109 | const void* pNext; 110 | VkSubpassDescriptionFlags flags; 111 | VkPipelineBindPoint pipelineBindPoint; 112 | uint32_t viewMask; 113 | uint32_t inputAttachmentCount; 114 | const VkAttachmentReference2KHR* pInputAttachments; 115 | uint32_t colorAttachmentCount; 116 | const VkAttachmentReference2KHR* pColorAttachments; 117 | const VkAttachmentReference2KHR* pResolveAttachments; 118 | const VkAttachmentReference2KHR* pDepthStencilAttachment; 119 | uint32_t preserveAttachmentCount; 120 | const uint32_t* pPreserveAttachments; 121 | } VkSubpassDescription2KHR; 122 | typedef struct VkSubpassDependency2KHR 123 | { 124 | VkStructureType sType; 125 | const void* pNext; 126 | uint32_t srcSubpass; 127 | uint32_t dstSubpass; 128 | VkPipelineStageFlags srcStageMask; 129 | VkPipelineStageFlags dstStageMask; 130 | VkAccessFlags srcAccessMask; 131 | VkAccessFlags dstAccessMask; 132 | VkDependencyFlags dependencyFlags; 133 | int32_t viewOffset; 134 | } VkSubpassDependency2KHR; 135 | typedef struct VkRenderPassCreateInfo2KHR 136 | { 137 | VkStructureType sType; 138 | const void* pNext; 139 | VkRenderPassCreateFlags flags; 140 | uint32_t attachmentCount; 141 | const VkAttachmentDescription2KHR* pAttachments; 142 | uint32_t subpassCount; 143 | const VkSubpassDescription2KHR* pSubpasses; 144 | uint32_t dependencyCount; 145 | const VkSubpassDependency2KHR* pDependencies; 146 | uint32_t correlatedViewMaskCount; 147 | const uint32_t* pCorrelatedViewMasks; 148 | } VkRenderPassCreateInfo2KHR; 149 | typedef struct VkSubpassBeginInfoKHR 150 | { 151 | VkStructureType sType; 152 | const void* pNext; 153 | VkSubpassContents contents; 154 | } VkSubpassBeginInfoKHR; 155 | 156 | typedef struct VkSubpassEndInfoKHR 157 | { 158 | VkStructureType sType; 159 | const void* pNext; 160 | } VkSubpassEndInfoKHR; 161 | typedef VkResult(VKAPI_PTR* PFN_vkCreateRenderPass2KHR)(VkDevice device, const VkRenderPassCreateInfo2KHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass); 162 | typedef void(VKAPI_PTR* PFN_vkCmdBeginRenderPass2KHR)(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, const VkSubpassBeginInfoKHR* pSubpassBeginInfo); 163 | typedef void(VKAPI_PTR* PFN_vkCmdNextSubpass2KHR)(VkCommandBuffer commandBuffer, const VkSubpassBeginInfoKHR* pSubpassBeginInfo, const VkSubpassEndInfoKHR* pSubpassEndInfo); 164 | typedef void(VKAPI_PTR* PFN_vkCmdEndRenderPass2KHR)(VkCommandBuffer commandBuffer, const VkSubpassEndInfoKHR* pSubpassEndInfo); 165 | #endif // VK_HEADER_VERSION < 80 166 | 167 | #if VK_HEADER_VERSION < 95 168 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR (VkStructureType)1000082000 169 | typedef struct VkPhysicalDeviceFloat16Int8FeaturesKHR 170 | { 171 | VkStructureType sType; 172 | void* pNext; 173 | VkBool32 shaderFloat16; 174 | VkBool32 shaderInt8; 175 | } VkPhysicalDeviceFloat16Int8FeaturesKHR; 176 | #endif // VK_HEADER_VERSION < 95 177 | 178 | #if VK_HEADER_VERSION < 97 179 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT (VkStructureType)1000237000 180 | typedef struct VkPhysicalDeviceMemoryBudgetPropertiesEXT 181 | { 182 | VkStructureType sType; 183 | void* pNext; 184 | VkDeviceSize heapBudget[VK_MAX_MEMORY_HEAPS]; 185 | VkDeviceSize heapUsage[VK_MAX_MEMORY_HEAPS]; 186 | } VkPhysicalDeviceMemoryBudgetPropertiesEXT; 187 | #endif // VK_HEADER_VERSION < 97 188 | 189 | #endif // NCNN_VULKAN_HEADER_FIX_H 190 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/lib/cmake/ncnn/ncnn-release.cmake: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------- 2 | # Generated CMake target import file for configuration "release". 3 | #---------------------------------------------------------------- 4 | 5 | # Commands may need to know the format version. 6 | set(CMAKE_IMPORT_FILE_VERSION 1) 7 | 8 | # Import target "ncnn" for configuration "release" 9 | set_property(TARGET ncnn APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) 10 | set_target_properties(ncnn PROPERTIES 11 | IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX" 12 | IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libncnn.a" 13 | ) 14 | 15 | list(APPEND _IMPORT_CHECK_TARGETS ncnn ) 16 | list(APPEND _IMPORT_CHECK_FILES_FOR_ncnn "${_IMPORT_PREFIX}/lib/libncnn.a" ) 17 | 18 | # Commands beyond this point should not need to know the version. 19 | set(CMAKE_IMPORT_FILE_VERSION) 20 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/lib/cmake/ncnn/ncnn.cmake: -------------------------------------------------------------------------------- 1 | # Generated by CMake 2 | 3 | if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5) 4 | message(FATAL_ERROR "CMake >= 2.6.0 required") 5 | endif() 6 | cmake_policy(PUSH) 7 | cmake_policy(VERSION 2.6...3.18) 8 | #---------------------------------------------------------------- 9 | # Generated CMake target import file. 10 | #---------------------------------------------------------------- 11 | 12 | # Commands may need to know the format version. 13 | set(CMAKE_IMPORT_FILE_VERSION 1) 14 | 15 | # Protect against multiple inclusion, which would fail when already imported targets are added once more. 16 | set(_targetsDefined) 17 | set(_targetsNotDefined) 18 | set(_expectedTargets) 19 | foreach(_expectedTarget ncnn) 20 | list(APPEND _expectedTargets ${_expectedTarget}) 21 | if(NOT TARGET ${_expectedTarget}) 22 | list(APPEND _targetsNotDefined ${_expectedTarget}) 23 | endif() 24 | if(TARGET ${_expectedTarget}) 25 | list(APPEND _targetsDefined ${_expectedTarget}) 26 | endif() 27 | endforeach() 28 | if("${_targetsDefined}" STREQUAL "${_expectedTargets}") 29 | unset(_targetsDefined) 30 | unset(_targetsNotDefined) 31 | unset(_expectedTargets) 32 | set(CMAKE_IMPORT_FILE_VERSION) 33 | cmake_policy(POP) 34 | return() 35 | endif() 36 | if(NOT "${_targetsDefined}" STREQUAL "") 37 | message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n") 38 | endif() 39 | unset(_targetsDefined) 40 | unset(_targetsNotDefined) 41 | unset(_expectedTargets) 42 | 43 | 44 | # Compute the installation prefix relative to this file. 45 | get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) 46 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 47 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 48 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 49 | if(_IMPORT_PREFIX STREQUAL "/") 50 | set(_IMPORT_PREFIX "") 51 | endif() 52 | 53 | # Create imported target ncnn 54 | add_library(ncnn STATIC IMPORTED) 55 | 56 | set_target_properties(ncnn PROPERTIES 57 | INTERFACE_COMPILE_OPTIONS "-fno-rtti;-fno-exceptions" 58 | INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/ncnn" 59 | INTERFACE_LINK_LIBRARIES "m;gcc" 60 | INTERFACE_POSITION_INDEPENDENT_CODE "ON" 61 | ) 62 | 63 | if(CMAKE_VERSION VERSION_LESS 2.8.12) 64 | message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.") 65 | endif() 66 | 67 | # Load information for each installed configuration. 68 | get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) 69 | file(GLOB CONFIG_FILES "${_DIR}/ncnn-*.cmake") 70 | foreach(f ${CONFIG_FILES}) 71 | include(${f}) 72 | endforeach() 73 | 74 | # Cleanup temporary variables. 75 | set(_IMPORT_PREFIX) 76 | 77 | # Loop over all imported files and verify that they actually exist 78 | foreach(target ${_IMPORT_CHECK_TARGETS} ) 79 | foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} ) 80 | if(NOT EXISTS "${file}" ) 81 | message(FATAL_ERROR "The imported target \"${target}\" references the file 82 | \"${file}\" 83 | but this file does not exist. Possible reasons include: 84 | * The file was deleted, renamed, or moved to another location. 85 | * An install or uninstall procedure did not complete successfully. 86 | * The installation package was faulty and contained 87 | \"${CMAKE_CURRENT_LIST_FILE}\" 88 | but not all the files it references. 89 | ") 90 | endif() 91 | endforeach() 92 | unset(_IMPORT_CHECK_FILES_FOR_${target}) 93 | endforeach() 94 | unset(_IMPORT_CHECK_TARGETS) 95 | 96 | # This file does not depend on other imported targets which have 97 | # been exported from the same project but in a separate export set. 98 | 99 | # Commands beyond this point should not need to know the version. 100 | set(CMAKE_IMPORT_FILE_VERSION) 101 | cmake_policy(POP) 102 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/lib/cmake/ncnn/ncnnConfig.cmake: -------------------------------------------------------------------------------- 1 | set(NCNN_OPENMP OFF) 2 | set(NCNN_THREADS OFF) 3 | set(NCNN_VULKAN OFF) 4 | set(NCNN_SHARED_LIB OFF) 5 | set(NCNN_SYSTEM_GLSLANG OFF) 6 | 7 | if(NCNN_OPENMP) 8 | find_package(OpenMP) 9 | endif() 10 | 11 | if(NCNN_THREADS) 12 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 13 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) 14 | find_package(Threads REQUIRED) 15 | endif() 16 | 17 | if(NCNN_VULKAN) 18 | find_package(Vulkan REQUIRED) 19 | 20 | if(NOT NCNN_SHARED_LIB) 21 | if(NCNN_SYSTEM_GLSLANG) 22 | set(GLSLANG_TARGET_DIR "") 23 | else() 24 | set(GLSLANG_TARGET_DIR "${CMAKE_CURRENT_LIST_DIR}/../../..//cmake") 25 | endif(NCNN_SYSTEM_GLSLANG) 26 | 27 | include(${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake) 28 | include(${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake) 29 | if(EXISTS "${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") 30 | # hlsl support can be optional 31 | include("${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") 32 | endif() 33 | include(${GLSLANG_TARGET_DIR}/glslangTargets.cmake) 34 | include(${GLSLANG_TARGET_DIR}/SPIRVTargets.cmake) 35 | endif() 36 | endif(NCNN_VULKAN) 37 | 38 | include(${CMAKE_CURRENT_LIST_DIR}/ncnn.cmake) 39 | -------------------------------------------------------------------------------- /ncnn_demo/src/ncnn/lib/libncnn.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nihui/ncnn_on_xr806/c4a2dc4979554364316486971b4d274d055a5630/ncnn_demo/src/ncnn/lib/libncnn.a -------------------------------------------------------------------------------- /xr806.toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Generic) 2 | set(CMAKE_SYSTEM_PROCESSOR xtensa) 3 | 4 | set(CMAKE_C_COMPILER "/home/nihui/osd/xr806/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc") 5 | set(CMAKE_CXX_COMPILER "/home/nihui/osd/xr806/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-g++") 6 | 7 | set(CMAKE_FIND_ROOT_PATH "/home/nihui/osd/xr806/gcc-arm-none-eabi-10-2020-q4-major/arm-none-eabi") 8 | 9 | set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) 10 | 11 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 13 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 14 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) 15 | 16 | set(CMAKE_C_FLAGS "-mcpu=cortex-m33 -mtune=cortex-m33 -march=armv8-m.main+dsp -mfpu=fpv5-sp-d16 -mfloat-abi=softfp -mcmse -mthumb -fno-common") 17 | set(CMAKE_CXX_FLAGS "-mcpu=cortex-m33 -mtune=cortex-m33 -march=armv8-m.main+dsp -mfpu=fpv5-sp-d16 -mfloat-abi=softfp -mcmse -mthumb -fno-common") 18 | 19 | # cache flags 20 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags") 21 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags") 22 | 23 | option(NCNN_INSTALL_SDK "" ON) 24 | option(NCNN_PIXEL_ROTATE "" OFF) 25 | option(NCNN_PIXEL_AFFINE "" OFF) 26 | option(NCNN_PIXEL_DRAWING "" OFF) 27 | option(NCNN_BUILD_BENCHMARK "" OFF) 28 | option(NCNN_BUILD_TESTS "" OFF) 29 | option(NCNN_BUILD_TOOLS "" OFF) 30 | option(NCNN_BUILD_EXAMPLES "" OFF) 31 | option(NCNN_DISABLE_RTTI "" ON) 32 | option(NCNN_DISABLE_EXCEPTION "" ON) 33 | option(NCNN_BF16 "" OFF) 34 | option(NCNN_INT8 "" OFF) 35 | option(NCNN_THREADS "" OFF) 36 | option(NCNN_OPENMP "" OFF) 37 | option(NCNN_STDIO "" OFF) 38 | option(NCNN_STRING "" OFF) 39 | option(NCNN_PLATFORM_API "" OFF) 40 | option(NCNN_RUNTIME_CPU "" OFF) 41 | 42 | option(NCNN_SIMPLESTL "" ON) 43 | 44 | option(WITH_LAYER_absval "" OFF) 45 | option(WITH_LAYER_argmax "" OFF) 46 | option(WITH_LAYER_batchnorm "" OFF) 47 | option(WITH_LAYER_bias "" OFF) 48 | option(WITH_LAYER_bnll "" OFF) 49 | option(WITH_LAYER_concat "" OFF) 50 | option(WITH_LAYER_convolution "" ON) 51 | option(WITH_LAYER_crop "" OFF) 52 | option(WITH_LAYER_deconvolution "" OFF) 53 | option(WITH_LAYER_dropout "" OFF) 54 | option(WITH_LAYER_eltwise "" OFF) 55 | option(WITH_LAYER_elu "" OFF) 56 | option(WITH_LAYER_embed "" OFF) 57 | option(WITH_LAYER_exp "" OFF) 58 | option(WITH_LAYER_flatten "" ON) 59 | option(WITH_LAYER_innerproduct "" ON) 60 | option(WITH_LAYER_input "" ON) 61 | option(WITH_LAYER_log "" OFF) 62 | option(WITH_LAYER_lrn "" OFF) 63 | option(WITH_LAYER_memorydata "" ON) 64 | option(WITH_LAYER_mvn "" OFF) 65 | option(WITH_LAYER_pooling "" ON) 66 | option(WITH_LAYER_power "" OFF) 67 | option(WITH_LAYER_prelu "" OFF) 68 | option(WITH_LAYER_proposal "" OFF) 69 | option(WITH_LAYER_reduction "" OFF) 70 | option(WITH_LAYER_relu "" ON) 71 | option(WITH_LAYER_reshape "" ON) 72 | option(WITH_LAYER_roipooling "" OFF) 73 | option(WITH_LAYER_scale "" OFF) 74 | option(WITH_LAYER_sigmoid "" OFF) 75 | option(WITH_LAYER_slice "" OFF) 76 | option(WITH_LAYER_softmax "" OFF) 77 | option(WITH_LAYER_split "" ON) 78 | option(WITH_LAYER_spp "" OFF) 79 | option(WITH_LAYER_tanh "" OFF) 80 | option(WITH_LAYER_threshold "" OFF) 81 | option(WITH_LAYER_tile "" OFF) 82 | option(WITH_LAYER_rnn "" OFF) 83 | option(WITH_LAYER_lstm "" OFF) 84 | option(WITH_LAYER_binaryop "" ON) 85 | option(WITH_LAYER_unaryop "" OFF) 86 | option(WITH_LAYER_convolutiondepthwise "" OFF) 87 | option(WITH_LAYER_padding "" ON) 88 | option(WITH_LAYER_squeeze "" OFF) 89 | option(WITH_LAYER_expanddims "" OFF) 90 | option(WITH_LAYER_normalize "" OFF) 91 | option(WITH_LAYER_permute "" OFF) 92 | option(WITH_LAYER_priorbox "" OFF) 93 | option(WITH_LAYER_detectionoutput "" OFF) 94 | option(WITH_LAYER_interp "" OFF) 95 | option(WITH_LAYER_deconvolutiondepthwise "" OFF) 96 | option(WITH_LAYER_shufflechannel "" OFF) 97 | option(WITH_LAYER_instancenorm "" OFF) 98 | option(WITH_LAYER_clip "" OFF) 99 | option(WITH_LAYER_reorg "" OFF) 100 | option(WITH_LAYER_yolodetectionoutput "" OFF) 101 | option(WITH_LAYER_quantize "" OFF) 102 | option(WITH_LAYER_dequantize "" OFF) 103 | option(WITH_LAYER_yolov3detectionoutput "" OFF) 104 | option(WITH_LAYER_psroipooling "" OFF) 105 | option(WITH_LAYER_roialign "" OFF) 106 | option(WITH_LAYER_packing "" ON) 107 | option(WITH_LAYER_requantize "" OFF) 108 | option(WITH_LAYER_cast "" ON) 109 | option(WITH_LAYER_hardsigmoid "" OFF) 110 | option(WITH_LAYER_selu "" OFF) 111 | option(WITH_LAYER_hardswish "" OFF) 112 | option(WITH_LAYER_noop "" OFF) 113 | option(WITH_LAYER_pixelshuffle "" OFF) 114 | option(WITH_LAYER_deepcopy "" OFF) 115 | option(WITH_LAYER_mish "" OFF) 116 | option(WITH_LAYER_statisticspooling "" OFF) 117 | option(WITH_LAYER_swish "" OFF) 118 | option(WITH_LAYER_gemm "" ON) 119 | option(WITH_LAYER_groupnorm "" OFF) 120 | option(WITH_LAYER_layernorm "" OFF) 121 | option(WITH_LAYER_softplus "" OFF) 122 | option(WITH_LAYER_gru "" OFF) 123 | option(WITH_LAYER_multiheadattention "" OFF) 124 | option(WITH_LAYER_gelu "" OFF) 125 | option(WITH_LAYER_convolution1d "" OFF) 126 | option(WITH_LAYER_pooling1d "" OFF) 127 | option(WITH_LAYER_convolutiondepthwise1d "" OFF) 128 | option(WITH_LAYER_convolution3d "" OFF) 129 | option(WITH_LAYER_convolutiondepthwise3d "" OFF) 130 | option(WITH_LAYER_pooling3d "" OFF) 131 | 132 | # cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/xr806.toolchain.cmake -DCMAKE_BUILD_TYPE=Release .. 133 | --------------------------------------------------------------------------------