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 "softmax/softmax.hpp"
26
27namespace softmax {
28
29void check_correctness(const settings_t &s) {
30 for_(const auto &i_dir : s.dir)
31 for_(const auto &i_sdt : s.sdt)
32 for_(const auto &i_ddt : s.ddt)
33 for_(const auto &i_stag : s.stag)
34 for_(const auto &i_dtag : s.dtag)
35 for_(const auto &i_alg : s.alg)
36 for_(const auto &i_axis : s.axis)
37 for_(const auto &i_mb : s.mb)
38 for_(const auto &i_scales : s.scales)
39 for_(const auto &i_scratchpad_mode : s.scratchpad_mode)
40 for_(const auto &i_ctx_init : s.ctx_init)
41 for_(const auto &i_ctx_exe : s.ctx_exe)
42 for (auto i_inplace : s.inplace) {
43 auto attr = settings_t::get_attr(i_scales, i_scratchpad_mode);
44
45 const prb_t prb(s.prb_dims, i_dir, i_sdt, i_ddt, i_stag, i_dtag, i_alg,
46 i_axis, i_inplace, attr, i_ctx_init, i_ctx_exe, i_mb);
47 std::stringstream ss;
48 ss << prb;
49 const std::string cpp_pstr = ss.str();
50 const char *pstr = cpp_pstr.c_str();
51 BENCHDNN_PRINT(1, "run: %s\n", pstr);
52
53 res_t res {};
54 doit(&prb, &res);
55
56 parse_result(res, pstr);
57
58 if (is_bench_mode(PERF)) {
59 perf_report_t pr(&prb, s.perf_template);
60 pr.report(&res, pstr);
61 }
62 }
63}
64
65int verify_input(const settings_t &s) {
66 for_(const auto &i_scales : s.scales)
67 for (const auto &e : i_scales.scales) {
68 if (e.second.policy != policy_t::COMMON) {
69 BENCHDNN_PRINT(
70 0, "%s\n", "ERROR: scales support only `common` policy.");
71 return FAIL;
72 }
73 }
74 return OK;
75}
76
77int bench(int argc, char **argv) {
78 driver_name = "softmax";
79 using namespace parser;
80 static settings_t s;
81 static const settings_t def {};
82 for (; argc > 0; --argc, ++argv) {
83 const bool parsed_options = parse_bench_settings(argv[0])
84 || parse_batch(bench, argv[0])
85 || parse_dir(s.dir, def.dir, argv[0])
86 || parse_dt(s.sdt, def.sdt, argv[0], "sdt")
87 || parse_dt(s.ddt, def.ddt, argv[0], "ddt")
88 || parse_tag(s.stag, def.stag, argv[0], "stag")
89 || parse_tag(s.dtag, def.dtag, argv[0], "dtag")
90 || parse_alg(s.alg, def.alg, str2alg, argv[0])
91 || parse_axis(s.axis, def.axis, argv[0])
92 || parse_inplace(s.inplace, def.inplace, argv[0])
93 || parse_mb(s.mb, def.mb, argv[0])
94 || parse_attr_scales(s.scales, argv[0])
95 || parse_attr_scratchpad_mode(
96 s.scratchpad_mode, def.scratchpad_mode, argv[0])
97 || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0])
98 || parse_ctx_exe(s.ctx_exe, def.ctx_exe, 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 parse_prb_dims(s.prb_dims, argv[0]);
106
107 SAFE(verify_input(s), WARN);
108 check_correctness(s);
109 }
110 }
111
112 return parse_last_argument();
113}
114} // namespace softmax
115