| 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 | import sys |
| 16 | import time |
| 17 | import ncnn |
| 18 | |
| 19 | param_root = "../benchmark/" |
| 20 | |
| 21 | g_warmup_loop_count = 8 |
| 22 | g_loop_count = 4 |
| 23 | g_enable_cooling_down = True |
| 24 | |
| 25 | g_vkdev = None |
| 26 | g_blob_vkallocator = None |
| 27 | g_staging_vkallocator = None |
| 28 | |
| 29 | g_blob_pool_allocator = ncnn.UnlockedPoolAllocator() |
| 30 | g_workspace_pool_allocator = ncnn.PoolAllocator() |
| 31 | |
| 32 | |
| 33 | def benchmark(comment, _in, opt): |
| 34 | _in.fill(0.01) |
| 35 | |
| 36 | g_blob_pool_allocator.clear() |
| 37 | g_workspace_pool_allocator.clear() |
| 38 | |
| 39 | if opt.use_vulkan_compute: |
| 40 | g_blob_vkallocator.clear() |
| 41 | g_staging_vkallocator.clear() |
| 42 | |
| 43 | net = ncnn.Net() |
| 44 | net.opt = opt |
| 45 | |
| 46 | if net.opt.use_vulkan_compute: |
| 47 | net.set_vulkan_device(g_vkdev) |
| 48 | |
| 49 | net.load_param(param_root + comment + ".param") |
| 50 | |
| 51 | dr = ncnn.DataReaderFromEmpty() |
| 52 | net.load_model(dr) |
| 53 | |
| 54 | input_names = net.input_names() |
| 55 | output_names = net.output_names() |
| 56 | |
| 57 | if g_enable_cooling_down: |
| 58 | time.sleep(10) |
| 59 | |
| 60 | # warm up |
| 61 | for i in range(g_warmup_loop_count): |
| 62 | # test with statement |
| 63 | with net.create_extractor() as ex: |
| 64 | ex.input(input_names[0], _in) |
| 65 | ex.extract(output_names[0]) |
| 66 | |
| 67 | time_min = sys.float_info.max |
| 68 | time_max = -sys.float_info.max |
| 69 | time_avg = 0.0 |
| 70 | |
| 71 | for i in range(g_loop_count): |
| 72 | start = time.time() |
| 73 | |
| 74 | # test net keep alive until ex freed |
| 75 | ex = net.create_extractor() |
| 76 | ex.input(input_names[0], _in) |
| 77 | ex.extract(output_names[0]) |
| 78 | |
| 79 | end = time.time() |
| 80 | |
| 81 | timespan = end - start |
| 82 | |
| 83 | time_min = timespan if timespan < time_min else time_min |
| 84 | time_max = timespan if timespan > time_max else time_max |
| 85 | time_avg += timespan |
| 86 | |
| 87 | time_avg /= g_loop_count |
| 88 | |
| 89 | print( |
| 90 | "%20s min = %7.2f max = %7.2f avg = %7.2f" |
| 91 | % (comment, time_min * 1000, time_max * 1000, time_avg * 1000) |
| 92 | ) |
| 93 | |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | loop_count = 4 |
| 97 | num_threads = ncnn.get_cpu_count() |
| 98 | powersave = 0 |
| 99 | gpu_device = -1 |
| 100 | cooling_down = 1 |
| 101 | |
| 102 | argc = len(sys.argv) |
| 103 | if argc >= 2: |
| 104 | loop_count = int(sys.argv[1]) |
| 105 | if argc >= 3: |
| 106 | num_threads = int(sys.argv[2]) |
| 107 | if argc >= 4: |
| 108 | powersave = int(sys.argv[3]) |
| 109 | if argc >= 5: |
| 110 | gpu_device = int(sys.argv[4]) |
| 111 | if argc >= 6: |
| 112 | cooling_down = int(sys.argv[5]) |
| 113 | |
| 114 | use_vulkan_compute = gpu_device != -1 |
| 115 | |
| 116 | g_enable_cooling_down = cooling_down != 0 |
| 117 | |
| 118 | g_loop_count = loop_count |
| 119 | |
| 120 | g_blob_pool_allocator.set_size_compare_ratio(0.0) |
| 121 | g_workspace_pool_allocator.set_size_compare_ratio(0.5) |
| 122 | |
| 123 | if use_vulkan_compute: |
| 124 | g_warmup_loop_count = 10 |
| 125 | |
| 126 | g_vkdev = ncnn.get_gpu_device(gpu_device) |
| 127 | |
| 128 | g_blob_vkallocator = ncnn.VkBlobAllocator(g_vkdev) |
| 129 | g_staging_vkallocator = ncnn.VkStagingAllocator(g_vkdev) |
| 130 | |
| 131 | opt = ncnn.Option() |
| 132 | opt.lightmode = True |
| 133 | opt.num_threads = num_threads |
| 134 | opt.blob_allocator = g_blob_pool_allocator |
| 135 | opt.workspace_allocator = g_workspace_pool_allocator |
| 136 | if use_vulkan_compute: |
| 137 | opt.blob_vkallocator = g_blob_vkallocator |
| 138 | opt.workspace_vkallocator = g_blob_vkallocator |
| 139 | opt.staging_vkallocator = g_staging_vkallocator |
| 140 | opt.use_winograd_convolution = True |
| 141 | opt.use_sgemm_convolution = True |
| 142 | opt.use_int8_inference = True |
| 143 | opt.use_vulkan_compute = use_vulkan_compute |
| 144 | opt.use_fp16_packed = True |
| 145 | opt.use_fp16_storage = True |
| 146 | opt.use_fp16_arithmetic = True |
| 147 | opt.use_int8_storage = True |
| 148 | opt.use_int8_arithmetic = True |
| 149 | opt.use_packing_layout = True |
| 150 | opt.use_shader_pack8 = False |
| 151 | opt.use_image_storage = False |
| 152 | |
| 153 | ncnn.set_cpu_powersave(powersave) |
| 154 | ncnn.set_omp_dynamic(0) |
| 155 | ncnn.set_omp_num_threads(num_threads) |
| 156 | |
| 157 | print("loop_count =", loop_count) |
| 158 | print("num_threads =", num_threads) |
| 159 | print("powersave =", ncnn.get_cpu_powersave()) |
| 160 | print("gpu_device =", gpu_device) |
| 161 | print("cooling_down =", g_enable_cooling_down) |
| 162 | |
| 163 | benchmark("squeezenet", ncnn.Mat((227, 227, 3)), opt) |
| 164 | benchmark("squeezenet_int8", ncnn.Mat((227, 227, 3)), opt) |
| 165 | benchmark("mobilenet", ncnn.Mat((224, 224, 3)), opt) |
| 166 | benchmark("mobilenet_int8", ncnn.Mat((224, 224, 3)), opt) |
| 167 | benchmark("mobilenet_v2", ncnn.Mat((224, 224, 3)), opt) |
| 168 | # benchmark("mobilenet_v2_int8", ncnn.Mat(w=224, h=224, c=3), opt) |
| 169 | benchmark("mobilenet_v3", ncnn.Mat((224, 224, 3)), opt) |
| 170 | benchmark("shufflenet", ncnn.Mat((224, 224, 3)), opt) |
| 171 | benchmark("shufflenet_v2", ncnn.Mat((224, 224, 3)), opt) |
| 172 | benchmark("mnasnet", ncnn.Mat((224, 224, 3)), opt) |
| 173 | benchmark("proxylessnasnet", ncnn.Mat((224, 224, 3)), opt) |
| 174 | benchmark("efficientnet_b0", ncnn.Mat((224, 224, 3)), opt) |
| 175 | benchmark("regnety_400m", ncnn.Mat((224, 224, 3)), opt) |
| 176 | benchmark("blazeface", ncnn.Mat((128, 128, 3)), opt) |
| 177 | benchmark("googlenet", ncnn.Mat((224, 224, 3)), opt) |
| 178 | benchmark("googlenet_int8", ncnn.Mat((224, 224, 3)), opt) |
| 179 | benchmark("resnet18", ncnn.Mat((224, 224, 3)), opt) |
| 180 | benchmark("resnet18_int8", ncnn.Mat((224, 224, 3)), opt) |
| 181 | benchmark("alexnet", ncnn.Mat((227, 227, 3)), opt) |
| 182 | benchmark("vgg16", ncnn.Mat((224, 224, 3)), opt) |
| 183 | benchmark("vgg16_int8", ncnn.Mat((224, 224, 3)), opt) |
| 184 | benchmark("resnet50", ncnn.Mat((224, 224, 3)), opt) |
| 185 | benchmark("resnet50_int8", ncnn.Mat((224, 224, 3)), opt) |
| 186 | benchmark("squeezenet_ssd", ncnn.Mat((300, 300, 3)), opt) |
| 187 | benchmark("squeezenet_ssd_int8", ncnn.Mat((300, 300, 3)), opt) |
| 188 | benchmark("mobilenet_ssd", ncnn.Mat((300, 300, 3)), opt) |
| 189 | benchmark("mobilenet_ssd_int8", ncnn.Mat((300, 300, 3)), opt) |
| 190 | benchmark("mobilenet_yolo", ncnn.Mat((416, 416, 3)), opt) |
| 191 | benchmark("mobilenetv2_yolov3", ncnn.Mat((352, 352, 3)), opt) |
| 192 | benchmark("yolov4-tiny", ncnn.Mat((416, 416, 3)), opt) |