Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / tests / benchdnn / bnorm / bench_bnorm.cpp
1 /*******************************************************************************
2 * Copyright 2017-2018 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <float.h>
21 #include <math.h>
22
23 #include "mkldnn.h"
24
25 #include "mkldnn_common.hpp"
26 #include "mkldnn_memory.hpp"
27 #include "mkldnn_debug.hpp"
28
29 #include "bnorm/bnorm.hpp"
30
31 namespace bnorm {
32
33 /* global driver parameters */
34 check_alg_t check_alg = ALG_AUTO;
35 int mb = 0;
36 dir_t dir = FWD_D;
37 mkldnn_data_type_t dt = mkldnn_f32;
38 mkldnn_memory_format_t fmt = mkldnn_nchw;
39 flags_t flags = (flags_t)0;
40 attr_t attr;
41 const char *pattern = NULL;
42 const char *skip_impl = "";
43 bool allow_unimpl = false;
44 const char *perf_template = "perf,%n,%z,%F,%q,%f,%D,%-t,%0t";
45
46 void reset_parameters() {
47     check_alg = ALG_AUTO;
48     mb = 0;
49     dir = FWD_B;
50     dt = mkldnn_f32;
51     fmt = mkldnn_nchw;
52     flags = (flags_t)0;
53     attr = attr_t();
54     pattern = NULL;
55     skip_impl = "";
56     allow_unimpl = false;
57 }
58
59 void check_correctness(const desc_t *c) {
60     const prb_t p(*c, mb, dir, dt, fmt, flags, attr, check_alg);
61     char pstr[max_prb_len];
62     prb2str(&p, pstr);
63
64     if (pattern && !match_regex(pstr, pattern))
65         return;
66     print(1, "run: %s\n", pstr);
67
68     res_t res{};
69     const int status = bnorm::doit(&p, &res);
70
71     bool want_perf_report = false;
72     parse_result(res, want_perf_report, allow_unimpl, status, pstr);
73
74     if (want_perf_report && bench_mode & PERF)
75         perf_report(&p, &res, pstr);
76
77     benchdnn_stat.tests++;
78 }
79
80 int bench(int argc, char **argv, bool main_bench) {
81     for (int arg = 0; arg < argc; ++arg) {
82         if (!strncmp("--batch=", argv[arg], 8))
83             SAFE(batch(argv[arg] + 8, bench), CRIT);
84         else if (!strncmp("--check-alg=", argv[arg], 12))
85             check_alg = str2check_alg(argv[arg] + 12);
86         else if (!strncmp("--mb=", argv[arg], 5))
87             mb = atoi(argv[arg] + 5);
88         else if (!strncmp("--dir=", argv[arg], 6))
89             dir = str2dir(argv[arg] + 6);
90         else if (!strncmp("--dt=", argv[arg], 5))
91             dt = str2dt(argv[arg] + 5);
92         else if (!strncmp("--fmt=", argv[arg], 6))
93             fmt = str2fmt(argv[arg] + 6);
94         else if (!strncmp("--flags=", argv[arg], 8))
95             flags = str2flags(argv[arg] + 8);
96         else if (!strncmp("--attr=", argv[arg], 7))
97             SAFE(str2attr(&attr, argv[arg] + 7), CRIT);
98         else if (!strncmp("--match=", argv[arg], 8))
99             pattern = argv[arg] + 8;
100         else if (!strncmp("--skip-impl=", argv[arg], 12))
101             skip_impl = argv[arg] + 12;
102         else if (!strncmp("--allow-unimpl=", argv[arg], 15))
103             allow_unimpl = str2bool(argv[arg] + 15);
104         else if (!strncmp("--perf-template=", argv[arg], 16))
105             perf_template = argv[arg] + 16;
106         else if (!strcmp("--reset", argv[arg]))
107             reset_parameters();
108         else if (!strncmp("--mode=", argv[arg], 7))
109             bench_mode = str2bench_mode(argv[arg] + 7);
110         else if (!strncmp("-v", argv[arg], 2))
111             verbose = atoi(argv[arg] + 2);
112         else if (!strncmp("--verbose=", argv[arg], 10))
113             verbose = atoi(argv[arg] + 10);
114         else {
115             desc_t c;
116             if (str2desc(&c, argv[arg]) == FAIL) {
117                 fprintf(stderr, "driver: unknown option: `%s`, exiting...\n",
118                         argv[arg]);
119                 exit(2);
120             }
121             check_correctness(&c);
122         }
123     }
124
125     return OK;
126 }
127
128 }