1/*******************************************************************************
2* Copyright 2017-2022 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 <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <sstream>
22
23#include "dnnl_common.hpp"
24#include "utils/parser.hpp"
25
26#include "conv/conv.hpp"
27#include "conv/conv_dw_fusion.hpp"
28
29namespace conv {
30
31void check_correctness(const settings_t &s) {
32 for_(const auto &i_dir : s.dir)
33 for_(const auto &i_cfg : s.cfg)
34 for_(const auto &i_stag : s.stag)
35 for_(const auto &i_wtag : s.wtag)
36 for_(const auto &i_dtag : s.dtag)
37 for_(const auto &i_alg : s.alg)
38 for_(const auto &i_scales : s.scales)
39 for_(const auto &i_zero_points : s.zero_points)
40 for_(const auto &i_post_ops : s.post_ops)
41 for_(const auto &i_scratchpad_mode : s.scratchpad_mode)
42 for_(const auto &i_fpmath_mode : s.fpmath_mode)
43 for_(const auto &i_ctx_init : s.ctx_init)
44 for_(const auto &i_ctx_exe : s.ctx_exe)
45 for (const auto &i_mb : s.mb) {
46 auto attr = settings_t::get_attr(i_scales, i_zero_points, i_post_ops,
47 i_scratchpad_mode, i_fpmath_mode);
48
49 const prb_t prb(s.desc, i_dir, i_cfg, i_stag, i_wtag, i_dtag, i_alg,
50 attr, i_ctx_init, i_ctx_exe, i_mb);
51 std::stringstream ss;
52 ss << prb;
53 const std::string cpp_pstr = ss.str();
54 const char *pstr = cpp_pstr.c_str();
55
56 if (s.pattern && !match_regex(pstr, s.pattern)) return;
57 BENCHDNN_PRINT(1, "run: %s\n", pstr);
58
59 res_t res {};
60 if (attr.post_ops.convolution_index() != -1)
61 conv_dw_fusion::doit(&prb, &res);
62 else
63 conv::doit(&prb, &res);
64
65 parse_result(res, pstr);
66
67 if (is_bench_mode(PERF)) {
68 perf_report_t pr(&prb, s.perf_template);
69 pr.report(&res, pstr);
70 }
71 }
72}
73
74int bench(int argc, char **argv) {
75 driver_name = "conv";
76 using namespace parser;
77 static settings_t s;
78 static const settings_t def {};
79 for (; argc > 0; --argc, ++argv) {
80 const bool parsed_options = parse_bench_settings(argv[0])
81 || parse_batch(bench, argv[0])
82 || parse_dir(s.dir, def.dir, argv[0])
83 || parse_cfg(s.cfg, def.cfg, str2cfg, argv[0])
84 || parse_tag(s.stag, def.stag, argv[0], "stag")
85 || parse_tag(s.wtag, def.wtag, argv[0], "wtag")
86 || parse_tag(s.dtag, def.dtag, argv[0], "dtag")
87 || parse_alg(s.alg, def.alg, str2alg, argv[0])
88 || parse_mb(s.mb, def.mb, argv[0])
89 || parse_attr_scales(s.scales, argv[0])
90 || parse_attr_zero_points(s.zero_points, argv[0])
91 || parse_attr_post_ops(s.post_ops, argv[0])
92 || parse_attr_scratchpad_mode(
93 s.scratchpad_mode, def.scratchpad_mode, argv[0])
94 || parse_attr_fpmath_mode(
95 s.fpmath_mode, def.fpmath_mode, argv[0])
96 || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0])
97 || parse_ctx_exe(s.ctx_exe, def.ctx_exe, argv[0])
98 || parse_test_pattern_match(s.pattern, argv[0])
99 || parse_perf_template(s.perf_template, s.perf_template_def,
100 s.perf_template_csv(), argv[0])
101 || parse_reset(s, argv[0]) || parse_help(argv[0]);
102 if (!parsed_options) {
103 catch_unknown_options(argv[0]);
104
105 SAFE(str2desc(&s.desc, argv[0]), CRIT);
106 check_correctness(s);
107 }
108 }
109
110 return parse_last_argument();
111}
112
113} // namespace conv
114