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 | #include <string.h> |
20 | |
21 | #include <sstream> |
22 | |
23 | #include "dnnl_common.hpp" |
24 | #include "utils/parser.hpp" |
25 | |
26 | #include "bnorm/bnorm.hpp" |
27 | #include "lnorm/lnorm.hpp" |
28 | |
29 | using namespace bnorm; |
30 | |
31 | namespace lnorm { |
32 | |
33 | void check_correctness(const settings_t &s) { |
34 | for_(const auto &i_dir : s.dir) |
35 | for_(const auto &i_dt : s.dt) |
36 | for_(const auto &i_tag : s.tag) |
37 | for_(const auto &i_stat_tag : s.stat_tag) |
38 | for_(const auto &i_flags : s.flags) |
39 | for_(const auto &i_scales : s.scales) |
40 | for_(const auto &i_scratchpad_mode : s.scratchpad_mode) |
41 | for_(const auto &i_ctx_init : s.ctx_init) |
42 | for_(const auto &i_ctx_exe : s.ctx_exe) |
43 | for (auto i_inplace : s.inplace) { |
44 | auto attr = settings_t::get_attr(i_scales, i_scratchpad_mode); |
45 | |
46 | const prb_t prb(s.prb_dims, i_tag, i_stat_tag, i_dir, i_dt, i_flags, |
47 | attr, i_ctx_init, i_ctx_exe, i_inplace, s.check_alg); |
48 | std::stringstream ss; |
49 | ss << prb; |
50 | const std::string cpp_pstr = ss.str(); |
51 | const char *pstr = cpp_pstr.c_str(); |
52 | |
53 | if (s.pattern && !match_regex(pstr, s.pattern)) return; |
54 | BENCHDNN_PRINT(1, "run: %s\n" , pstr); |
55 | |
56 | res_t res {}; |
57 | doit(&prb, &res); |
58 | |
59 | parse_result(res, pstr); |
60 | |
61 | if (is_bench_mode(PERF)) { |
62 | perf_report_t pr(&prb, s.perf_template); |
63 | pr.report(&res, pstr); |
64 | } |
65 | } |
66 | } |
67 | |
68 | int verify_input(const settings_t &s) { |
69 | for_(const auto &i_scales : s.scales) |
70 | for (const auto &e : i_scales.scales) { |
71 | if (e.second.policy != policy_t::COMMON) { |
72 | BENCHDNN_PRINT( |
73 | 0, "%s\n" , "ERROR: scales support only `common` policy." ); |
74 | return FAIL; |
75 | } |
76 | } |
77 | |
78 | static constexpr int n_inputs = 2; |
79 | for (const auto &i_dt : s.dt) { |
80 | if (i_dt.size() != 1 && i_dt.size() != n_inputs) { |
81 | BENCHDNN_PRINT(0, "%s%d%s%ld%s\n" , |
82 | "ERROR: `dt` option expects either 1 or " , n_inputs, |
83 | " inputs in SRC:DST format. Current size is: \"" , |
84 | (long)i_dt.size(), "\"." ); |
85 | return FAIL; |
86 | } |
87 | } |
88 | |
89 | return OK; |
90 | } |
91 | |
92 | static const std::string help_flags |
93 | = "FLAGS (Default: not specified)\n Specifies normalization " |
94 | "flags. `FLAGS` values are:\n * `G` for global_stats.\n * `C` " |
95 | "for scale.\n * `H` for shift.\n" ; |
96 | |
97 | int bench(int argc, char **argv) { |
98 | driver_name = "lnorm" ; |
99 | using namespace parser; |
100 | static settings_t s; |
101 | static const settings_t def {}; |
102 | for (; argc > 0; --argc, ++argv) { |
103 | const bool parsed_options = parse_bench_settings(argv[0]) |
104 | || parse_batch(bench, argv[0]) |
105 | || parse_dir(s.dir, def.dir, argv[0]) |
106 | || parse_multi_dt(s.dt, def.dt, argv[0], "dt" ) |
107 | || parse_multi_tag(s.tag, def.tag, argv[0], "tag" ) |
108 | || parse_tag(s.stat_tag, def.stat_tag, argv[0], "stat_tag" ) |
109 | || parse_vector_option(s.flags, def.flags, str2flags, argv[0], |
110 | "flags" , help_flags) |
111 | || parse_inplace(s.inplace, def.inplace, argv[0]) |
112 | || parse_attr_scales(s.scales, argv[0]) |
113 | || parse_attr_scratchpad_mode( |
114 | s.scratchpad_mode, def.scratchpad_mode, argv[0]) |
115 | || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0]) |
116 | || parse_ctx_exe(s.ctx_exe, def.ctx_exe, argv[0]) |
117 | || parse_test_pattern_match(s.pattern, argv[0]) |
118 | || parse_perf_template(s.perf_template, s.perf_template_def, |
119 | s.perf_template_csv(), argv[0]) |
120 | || parse_reset(s, argv[0]) || parse_help(argv[0]); |
121 | if (!parsed_options) { |
122 | catch_unknown_options(argv[0]); |
123 | |
124 | parse_prb_dims(s.prb_dims, argv[0]); |
125 | |
126 | SAFE(verify_input(s), WARN); |
127 | check_correctness(s); |
128 | } |
129 | } |
130 | |
131 | return parse_last_argument(); |
132 | } |
133 | |
134 | } // namespace lnorm |
135 | |