1 | /******************************************************************************* |
2 | * Copyright 2018-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 "oneapi/dnnl/dnnl.h" |
24 | |
25 | #include "dnnl_common.hpp" |
26 | #include "utils/parser.hpp" |
27 | |
28 | #include "rnn/rnn.hpp" |
29 | |
30 | namespace rnn { |
31 | |
32 | void check_correctness(const settings_t &s) { |
33 | for_(const auto &i_prop : s.prop) |
34 | for_(const auto &i_cfg : s.cfg) |
35 | for_(const auto &i_alg : s.alg) |
36 | for_(auto i_with_peephole : s.with_peephole) |
37 | for_(auto i_with_projection : s.with_projection) |
38 | for_(const auto &i_scale_policy : s.scale_policy) |
39 | for_(const auto &i_scale_proj_policy : s.scale_proj_policy) |
40 | for_(const auto &i_direction : s.direction) |
41 | for_(const auto &i_activation : s.activation) |
42 | for_(auto i_skip_nonlinear : s.skip_nonlinear) |
43 | for_(auto i_trivial_strides : s.trivial_strides) |
44 | for_(const auto &i_n_layer : s.n_layer) |
45 | for_(const auto &i_n_iter : s.n_iter) |
46 | for_(const auto &i_mb : s.mb) |
47 | for_(const auto &i_scratchpad_mode : s.scratchpad_mode) |
48 | for_(const auto &i_ctx_init : s.ctx_init) |
49 | for_(const auto &i_ctx_exe : s.ctx_exe) |
50 | for (const auto &i_fpmath_mode : s.fpmath_mode) { |
51 | if (i_with_peephole && i_alg != VANILLA_LSTM) continue; |
52 | |
53 | if (!(i_scale_policy == policy_t::COMMON |
54 | || i_scale_policy == policy_t::PER_OC)) { |
55 | std::stringstream ss; |
56 | ss << i_scale_policy; |
57 | const std::string cpp_pstr = ss.str(); |
58 | const char *policy_s = cpp_pstr.c_str(); |
59 | fprintf(stderr, |
60 | "ERROR: rnn driver: --scaling=%s is invalid, supported " |
61 | "values are `common` and `per_oc`.\n" , |
62 | policy_s), |
63 | fflush(stderr); |
64 | SAFE_V(FAIL); |
65 | } |
66 | |
67 | auto attr = settings_t::get_attr(i_scratchpad_mode, i_fpmath_mode); |
68 | |
69 | const prb_t prb(s.desc, dt_conf_t::create(i_cfg, attr), i_prop, i_alg, |
70 | i_with_peephole, i_with_projection, i_direction, i_scale_policy, |
71 | i_scale_proj_policy, s.flags, i_activation, attr, i_ctx_init, |
72 | i_ctx_exe, s.alpha, s.beta, i_skip_nonlinear, i_trivial_strides, |
73 | i_n_layer, i_n_iter, i_mb); |
74 | std::stringstream ss; |
75 | ss << prb; |
76 | const std::string cpp_pstr = ss.str(); |
77 | const char *pstr = cpp_pstr.c_str(); |
78 | BENCHDNN_PRINT(1, "run: %s\n" , pstr); |
79 | |
80 | res_t res {}; |
81 | doit(prb, &res); |
82 | |
83 | parse_result(res, pstr); |
84 | |
85 | if (is_bench_mode(PERF)) { |
86 | perf_report_t pr(&prb, s.perf_template); |
87 | pr.report(&res, pstr); |
88 | } |
89 | } |
90 | } |
91 | |
92 | static const std::string help_direction |
93 | = "DIRECTION (Default: `left2right`)\n Specifies evaluation " |
94 | "direction.\n `DIRECTION` values are: `left2right`, `right2left`, " |
95 | "`concat` and `sum`.\n" ; |
96 | |
97 | static const std::string help_activation |
98 | = "ACTIVATION (Default: `relu`)\n Specifies Vanilla RNN " |
99 | "activation function.\n `ACTIVATION` values are: `relu`, " |
100 | "`logistic`, and `tanh`.\n" ; |
101 | |
102 | static const std::string help_l |
103 | = "UINT (Default: `0`)\n Overrides number of layers value " |
104 | "specified in a problem descriptor with `UINT` value.\n When set " |
105 | "to `0`, takes no effect.\n" ; |
106 | |
107 | static const std::string help_t |
108 | = "UINT (Default: `0`)\n Overrides number of timestamps value " |
109 | "specified in a problem descriptor with `UINT` value.\n When set " |
110 | "to `0`, takes no effect.\n" ; |
111 | |
112 | static const std::string help_with_peephole |
113 | = "BOOL (Default: `false`)\n When set to `true`, enables LSTM " |
114 | "peephole extension.\n" ; |
115 | |
116 | static const std::string help_with_projection |
117 | = "BOOL (Default: `false`)\n When set to `true`, enables LSTM " |
118 | "projection extension.\n" ; |
119 | |
120 | int bench(int argc, char **argv) { |
121 | driver_name = "rnn" ; |
122 | using namespace parser; |
123 | static settings_t s; |
124 | static const settings_t def {}; |
125 | for (; argc > 0; --argc, ++argv) { |
126 | auto cstr2str = [](const char *str) { return std::string(str); }; |
127 | const bool parsed_options = parse_bench_settings(argv[0]) |
128 | || parse_batch(bench, argv[0]) |
129 | || parse_dir(s.prop, def.prop, argv[0], "prop" ) |
130 | || parse_cfg(s.cfg, def.cfg, cstr2str, argv[0]) |
131 | || parse_alg(s.alg, def.alg, str2alg, argv[0]) |
132 | || parse_vector_option(s.direction, def.direction, |
133 | str2direction, argv[0], "direction" , help_direction) |
134 | || parse_vector_option(s.activation, def.activation, |
135 | str2activation, argv[0], "activation" , help_activation) |
136 | || parse_scale_policy(s.scale_policy, def.scale_policy, argv[0]) |
137 | || parse_scale_policy(s.scale_proj_policy, |
138 | def.scale_proj_policy, argv[0], "scaling-proj" ) |
139 | || parse_mb(s.mb, def.mb, argv[0]) |
140 | || parse_vector_option( |
141 | s.n_layer, def.n_layer, atoi, argv[0], "l" , help_l) |
142 | || parse_vector_option( |
143 | s.n_iter, def.n_iter, atoi, argv[0], "t" , help_t) |
144 | || parse_skip_nonlinear( |
145 | s.skip_nonlinear, def.skip_nonlinear, argv[0]) |
146 | || parse_trivial_strides( |
147 | s.trivial_strides, def.trivial_strides, argv[0]) |
148 | || parse_vector_option(s.with_peephole, def.with_peephole, |
149 | str2bool, argv[0], "with-peephole" , help_with_peephole) |
150 | || parse_vector_option(s.with_projection, def.with_projection, |
151 | str2bool, argv[0], "with-projection" , |
152 | help_with_projection) |
153 | || parse_attr_scratchpad_mode( |
154 | s.scratchpad_mode, def.scratchpad_mode, argv[0]) |
155 | || parse_attr_fpmath_mode( |
156 | s.fpmath_mode, def.fpmath_mode, argv[0]) |
157 | || parse_ctx_init(s.ctx_init, def.ctx_init, argv[0]) |
158 | || parse_ctx_exe(s.ctx_exe, def.ctx_exe, argv[0]) |
159 | || parse_perf_template(s.perf_template, s.perf_template_def, |
160 | s.perf_template_csv(), argv[0]) |
161 | || parse_reset(s, argv[0]) || parse_help(argv[0]); |
162 | if (!parsed_options) { |
163 | catch_unknown_options(argv[0]); |
164 | |
165 | SAFE(str2desc(&s.desc, argv[0]), CRIT); |
166 | check_correctness(s); |
167 | } |
168 | } |
169 | |
170 | return parse_last_argument(); |
171 | } |
172 | |
173 | } // namespace rnn |
174 | |