| 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 | #include "benchmark.h" |
| 16 | |
| 17 | #if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) && !defined(__riscv) && !NCNN_SIMPLESTL |
| 18 | #define USE_CXX11_CLOCK 1 |
| 19 | #else |
| 20 | #define USE_CXX11_CLOCK 0 |
| 21 | #endif |
| 22 | |
| 23 | #if USE_CXX11_CLOCK |
| 24 | #include <chrono> |
| 25 | #if NCNN_THREADS |
| 26 | #include <thread> |
| 27 | #endif |
| 28 | #include <numeric> |
| 29 | #include <algorithm> |
| 30 | #endif // USE_CXX11_CLOCK |
| 31 | |
| 32 | #ifdef _WIN32 |
| 33 | #define WIN32_LEAN_AND_MEAN |
| 34 | #include <windows.h> |
| 35 | #else // _WIN32 |
| 36 | #include <sys/time.h> //gettimeofday() |
| 37 | #include <unistd.h> // sleep() |
| 38 | #endif // _WIN32 |
| 39 | |
| 40 | #if NCNN_BENCHMARK |
| 41 | #include "layer/convolution.h" |
| 42 | #include "layer/convolutiondepthwise.h" |
| 43 | #include "layer/deconvolution.h" |
| 44 | #include "layer/deconvolutiondepthwise.h" |
| 45 | #include "layer/convolution3d.h" |
| 46 | #include "layer/convolutiondepthwise3d.h" |
| 47 | #include "layer/deconvolution3d.h" |
| 48 | #include "layer/deconvolutiondepthwise3d.h" |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #endif // NCNN_BENCHMARK |
| 52 | |
| 53 | namespace ncnn { |
| 54 | |
| 55 | double get_current_time() |
| 56 | { |
| 57 | #if USE_CXX11_CLOCK |
| 58 | auto now = std::chrono::high_resolution_clock::now(); |
| 59 | auto usec = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); |
| 60 | return usec.count() / 1000.0; |
| 61 | #else |
| 62 | #ifdef _WIN32 |
| 63 | LARGE_INTEGER freq; |
| 64 | LARGE_INTEGER pc; |
| 65 | QueryPerformanceFrequency(&freq); |
| 66 | QueryPerformanceCounter(&pc); |
| 67 | |
| 68 | return pc.QuadPart * 1000.0 / freq.QuadPart; |
| 69 | #else // _WIN32 |
| 70 | struct timeval tv; |
| 71 | gettimeofday(&tv, NULL); |
| 72 | |
| 73 | return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; |
| 74 | #endif // _WIN32 |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | void sleep(unsigned long long int milliseconds) |
| 79 | { |
| 80 | #if USE_CXX11_CLOCK && NCNN_THREADS |
| 81 | std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); |
| 82 | #else |
| 83 | #ifdef _WIN32 |
| 84 | Sleep(milliseconds); |
| 85 | #elif defined(__unix__) || defined(__APPLE__) |
| 86 | usleep(milliseconds * 1000); |
| 87 | #elif _POSIX_TIMERS |
| 88 | struct timespec ts; |
| 89 | ts.tv_sec = milliseconds * 0.001; |
| 90 | ts.tv_nsec = 0; |
| 91 | nanosleep(&ts, &ts); |
| 92 | #else |
| 93 | // TODO How to handle it ? |
| 94 | #endif |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | #if NCNN_BENCHMARK |
| 99 | |
| 100 | void benchmark(const Layer* layer, double start, double end) |
| 101 | { |
| 102 | fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start); |
| 103 | fprintf(stderr, " |"); |
| 104 | fprintf(stderr, "\n"); |
| 105 | } |
| 106 | |
| 107 | void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end) |
| 108 | { |
| 109 | fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start); |
| 110 | |
| 111 | char in_shape_str[64] = {'\0'}; |
| 112 | char out_shape_str[64] = {'\0'}; |
| 113 | |
| 114 | if (bottom_blob.dims == 1) |
| 115 | { |
| 116 | sprintf(in_shape_str, "[%3d *%d]", bottom_blob.w, bottom_blob.elempack); |
| 117 | } |
| 118 | if (bottom_blob.dims == 2) |
| 119 | { |
| 120 | sprintf(in_shape_str, "[%3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.elempack); |
| 121 | } |
| 122 | if (bottom_blob.dims == 3) |
| 123 | { |
| 124 | sprintf(in_shape_str, "[%3d, %3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.c, bottom_blob.elempack); |
| 125 | } |
| 126 | if (bottom_blob.dims == 4) |
| 127 | { |
| 128 | sprintf(in_shape_str, "[%3d, %3d, %3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.d, bottom_blob.c, bottom_blob.elempack); |
| 129 | } |
| 130 | |
| 131 | if (top_blob.dims == 1) |
| 132 | { |
| 133 | sprintf(out_shape_str, "[%3d *%d]", top_blob.w, top_blob.elempack); |
| 134 | } |
| 135 | if (top_blob.dims == 2) |
| 136 | { |
| 137 | sprintf(out_shape_str, "[%3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.elempack); |
| 138 | } |
| 139 | if (top_blob.dims == 3) |
| 140 | { |
| 141 | sprintf(out_shape_str, "[%3d, %3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.c, top_blob.elempack); |
| 142 | } |
| 143 | if (top_blob.dims == 4) |
| 144 | { |
| 145 | sprintf(out_shape_str, "[%3d, %3d, %3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.d, top_blob.c, top_blob.elempack); |
| 146 | } |
| 147 | |
| 148 | fprintf(stderr, " | %22s -> %-22s", in_shape_str, out_shape_str); |
| 149 | |
| 150 | if (layer->type == "Convolution") |
| 151 | { |
| 152 | fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d", |
| 153 | ((Convolution*)layer)->kernel_w, |
| 154 | ((Convolution*)layer)->kernel_h, |
| 155 | ((Convolution*)layer)->stride_w, |
| 156 | ((Convolution*)layer)->stride_h); |
| 157 | } |
| 158 | else if (layer->type == "ConvolutionDepthWise") |
| 159 | { |
| 160 | fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d", |
| 161 | ((ConvolutionDepthWise*)layer)->kernel_w, |
| 162 | ((ConvolutionDepthWise*)layer)->kernel_h, |
| 163 | ((ConvolutionDepthWise*)layer)->stride_w, |
| 164 | ((ConvolutionDepthWise*)layer)->stride_h); |
| 165 | } |
| 166 | else if (layer->type == "Deconvolution") |
| 167 | { |
| 168 | fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d", |
| 169 | ((Deconvolution*)layer)->kernel_w, |
| 170 | ((Deconvolution*)layer)->kernel_h, |
| 171 | ((Deconvolution*)layer)->stride_w, |
| 172 | ((Deconvolution*)layer)->stride_h); |
| 173 | } |
| 174 | else if (layer->type == "DeconvolutionDepthWise") |
| 175 | { |
| 176 | fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d", |
| 177 | ((DeconvolutionDepthWise*)layer)->kernel_w, |
| 178 | ((DeconvolutionDepthWise*)layer)->kernel_h, |
| 179 | ((DeconvolutionDepthWise*)layer)->stride_w, |
| 180 | ((DeconvolutionDepthWise*)layer)->stride_h); |
| 181 | } |
| 182 | else if (layer->type == "Convolution3D") |
| 183 | { |
| 184 | fprintf(stderr, " kernel: %1d x %1d x %1d stride: %1d x %1d x %1d", |
| 185 | ((Convolution3D*)layer)->kernel_w, |
| 186 | ((Convolution3D*)layer)->kernel_h, |
| 187 | ((Convolution3D*)layer)->kernel_d, |
| 188 | ((Convolution3D*)layer)->stride_w, |
| 189 | ((Convolution3D*)layer)->stride_h, |
| 190 | ((Convolution3D*)layer)->stride_d); |
| 191 | } |
| 192 | else if (layer->type == "ConvolutionDepthWise3D") |
| 193 | { |
| 194 | fprintf(stderr, " kernel: %1d x %1d x %1d stride: %1d x %1d x %1d", |
| 195 | ((ConvolutionDepthWise3D*)layer)->kernel_w, |
| 196 | ((ConvolutionDepthWise3D*)layer)->kernel_h, |
| 197 | ((ConvolutionDepthWise3D*)layer)->kernel_d, |
| 198 | ((ConvolutionDepthWise3D*)layer)->stride_w, |
| 199 | ((ConvolutionDepthWise3D*)layer)->stride_h, |
| 200 | ((ConvolutionDepthWise3D*)layer)->stride_d); |
| 201 | } |
| 202 | else if (layer->type == "Deconvolution3D") |
| 203 | { |
| 204 | fprintf(stderr, " kernel: %1d x %1d x %1d stride: %1d x %1d x %1d", |
| 205 | ((Deconvolution3D*)layer)->kernel_w, |
| 206 | ((Deconvolution3D*)layer)->kernel_h, |
| 207 | ((Deconvolution3D*)layer)->kernel_d, |
| 208 | ((Deconvolution3D*)layer)->stride_w, |
| 209 | ((Deconvolution3D*)layer)->stride_h, |
| 210 | ((Deconvolution3D*)layer)->stride_d); |
| 211 | } |
| 212 | else if (layer->type == "DeconvolutionDepthWise3D") |
| 213 | { |
| 214 | fprintf(stderr, " kernel: %1d x %1d x %1d stride: %1d x %1d x %1d", |
| 215 | ((DeconvolutionDepthWise3D*)layer)->kernel_w, |
| 216 | ((DeconvolutionDepthWise3D*)layer)->kernel_h, |
| 217 | ((DeconvolutionDepthWise3D*)layer)->kernel_d, |
| 218 | ((DeconvolutionDepthWise3D*)layer)->stride_w, |
| 219 | ((DeconvolutionDepthWise3D*)layer)->stride_h, |
| 220 | ((DeconvolutionDepthWise3D*)layer)->stride_d); |
| 221 | } |
| 222 | fprintf(stderr, "\n"); |
| 223 | } |
| 224 | |
| 225 | #endif // NCNN_BENCHMARK |
| 226 | |
| 227 | } // namespace ncnn |