1/*******************************************************************************
2* Copyright 2019-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
20#include <sstream>
21
22#include "dnnl_common.hpp"
23#include "utils/parser.hpp"
24
25#include "binary/binary.hpp"
26
27namespace binary {
28
29void check_correctness(const settings_t &s) {
30 for_(const auto &i_sdt : s.sdt)
31 for_(const auto &i_ddt : s.ddt)
32 for_(const auto &i_stag : s.stag)
33 for_(const auto &i_dtag : s.dtag)
34 for_(const auto &i_alg : s.alg)
35 for_(const auto &i_scales : s.scales)
36 for_(const auto &i_post_ops : s.post_ops)
37 for_(const auto &i_scratchpad_mode : s.scratchpad_mode)
38 for_(const auto &i_ctx_init : s.ctx_init)
39 for_(const auto &i_ctx_exe : s.ctx_exe)
40 for (auto i_inplace : s.inplace) {
41
42 // Expect exactly two inputs for problem dimensions.
43 static constexpr int n_inputs = 2;
44 if (s.prb_vdims.n_inputs() != n_inputs) {
45 fprintf(stderr,
46 "ERROR: binary driver: expect problem dimensions in format "
47 "`A0xA1x...:B0xB1x...`.\n");
48 SAFE_V(FAIL);
49 }
50 if (i_sdt.size() != n_inputs) {
51 fprintf(stderr,
52 "ERROR: binary driver: expect data types in format "
53 "`DT:DT`.\n");
54 SAFE_V(FAIL);
55 }
56 if (i_stag.size() != n_inputs) {
57 fprintf(stderr,
58 "ERROR: binary driver: expect format tags in format "
59 "`TAG:TAG`.\n");
60 SAFE_V(FAIL);
61 }
62 if (!(i_alg > alg_t::BINARY_START && i_alg < alg_t::BINARY_END)) {
63 fprintf(stderr,
64 "ERROR: binary driver: algorithm `%s` does not belong to "
65 "binary algorithm type.\n",
66 attr_t::post_ops_t::kind2str(i_alg));
67 SAFE_V(FAIL);
68 }
69
70 auto attr
71 = settings_t::get_attr(i_scales, i_post_ops, i_scratchpad_mode);
72
73 const prb_t prb(s.prb_vdims, i_sdt, i_ddt, i_stag, i_dtag, i_alg,
74 i_inplace, attr, i_ctx_init, i_ctx_exe);
75 std::stringstream ss;
76 ss << prb;
77 const std::string cpp_pstr = ss.str();
78 const char *pstr = cpp_pstr.c_str();
79 BENCHDNN_PRINT(1, "run: %s\n", pstr);
80
81 res_t res {};
82 doit(&prb, &res);
83
84 parse_result(res, pstr);
85
86 if (is_bench_mode(PERF)) {
87 perf_report_t pr(&prb, s.perf_template);
88 pr.report(&res, pstr);
89 }
90 }
91}
92
93int bench(int argc, char **argv) {
94 driver_name = "binary";
95 using namespace parser;
96 static settings_t s;
97 static const settings_t def {};
98 for (; argc > 0; --argc, ++argv) {
99 const bool parsed_options = parse_bench_settings(argv[0])
100 || parse_batch(bench, argv[0])
101 || parse_multi_dt(s.sdt, def.sdt, argv[0])
102 || parse_dt(s.ddt, def.ddt, argv[0], "ddt")
103 || parse_multi_tag(s.stag, def.stag, argv[0])
104 || parse_tag(s.dtag, def.dtag, argv[0], "dtag")
105 || parse_alg(
106 s.alg, def.alg, attr_t::post_ops_t::str2kind, argv[0])
107 || parse_inplace(s.inplace, def.inplace, argv[0])
108 || parse_attr_scales(s.scales, argv[0])
109 || parse_attr_post_ops(s.post_ops, argv[0])
110 || parse_attr_scratchpad_mode(
111 s.scratchpad_mode, def.scratchpad_mode, argv[0])
112 || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0])
113 || parse_ctx_exe(s.ctx_exe, def.ctx_exe, argv[0])
114 || parse_perf_template(s.perf_template, s.perf_template_def,
115 s.perf_template_csv(), argv[0])
116 || parse_reset(s, argv[0]) || parse_help(argv[0]);
117 if (!parsed_options) {
118 catch_unknown_options(argv[0]);
119
120 parse_prb_vdims(s.prb_vdims, argv[0]);
121 check_correctness(s);
122 }
123 }
124
125 return parse_last_argument();
126}
127
128} // namespace binary
129