1 /*******************************************************************************
2 * Copyright 2020-2021 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 #ifndef REDUCTION_HPP
18 #define REDUCTION_HPP
19 
20 #include "oneapi/dnnl/dnnl.hpp"
21 
22 #include "dnn_types.hpp"
23 #include "dnnl_memory.hpp"
24 #include "perf_report.hpp"
25 
26 namespace reduction {
27 
28 enum alg_t {
29     undef,
30     min,
31     max,
32     mul,
33     sum,
34     mean,
35     norm_lp_max,
36     norm_lp_sum,
37     norm_lp_power_p_max,
38     norm_lp_power_p_sum,
39     reduction_min = min,
40     reduction_max = max,
41     reduction_mul = mul,
42     reduction_sum = sum,
43     reduction_mean = mean,
44     reduction_norm_lp_max = norm_lp_max,
45     reduction_norm_lp_sum = norm_lp_sum,
46     reduction_norm_lp_power_p_max = norm_lp_power_p_max,
47     reduction_norm_lp_power_p_sum = norm_lp_power_p_sum,
48 };
49 
50 alg_t str2alg(const char *str);
51 const char *alg2str(alg_t alg);
52 dnnl_alg_kind_t alg2alg_kind(alg_t alg);
53 
54 struct settings_t {
55     settings_t() = default;
56 
57     // ctor to save certain fields from resetting
settings_treduction::settings_t58     settings_t(const char *perf_template) : settings_t() {
59         this->perf_template = perf_template;
60     }
61 
62     std::vector<dims_t> dims;
63     std::vector<dnnl_data_type_t> sdt {dnnl_f32};
64     std::vector<dnnl_data_type_t> ddt {dnnl_f32};
65     std::vector<std::string> stag {tag::abx};
66     std::vector<std::string> dtag {tag::any};
67     std::vector<attr_t::post_ops_t> post_ops {attr_t::post_ops_t()};
68     std::vector<alg_t> alg {alg_t::sum};
69     std::vector<float> p {1.0f}, eps {0.0f};
70     std::vector<int64_t> mb {0};
71 
72     const char *perf_template_csv
73             = "perf,%engine%,%impl%,%sdt%,%ddt%,%stag%,%dtag%,%alg%,%attr%,"
74               "%DESC%,%-time%,%0time%";
75     const char *perf_template_def
76             = "perf,%engine%,%impl%,%prb%,%-time%,%0time%";
77     const char *perf_template = perf_template_def;
78 
resetreduction::settings_t79     void reset() { *this = settings_t(perf_template); }
80 };
81 
82 struct prb_t {
prb_treduction::prb_t83     prb_t(const std::vector<dims_t> &dims, dnnl_data_type_t sdt,
84             dnnl_data_type_t ddt, const std::string &stag,
85             const std::string &dtag, alg_t alg, float p, float eps,
86             const attr_t &attr)
87         : src_dims(dims[0])
88         , dst_dims(dims[1])
89         , ndims(static_cast<int>(dims[0].size()))
90         , sdt(sdt)
91         , ddt(ddt)
92         , stag(stag)
93         , dtag(dtag)
94         , alg(alg)
95         , p(p)
96         , eps(eps)
97         , attr(attr) {}
98 
99     dims_t src_dims, dst_dims;
100     int ndims;
101     dnnl_data_type_t sdt, ddt;
102     std::string stag, dtag;
103     alg_t alg;
104     float p, eps;
105     attr_t attr;
106 };
107 
108 std::ostream &operator<<(std::ostream &s, const prb_t &prb);
109 
110 struct perf_report_t : public base_perf_report_t {
111     using base_perf_report_t::base_perf_report_t;
112 
reportreduction::perf_report_t113     void report(const prb_t *prb, const res_t *r, const char *prb_str) {
114         prb_ = prb;
115         sdt_.push_back(prb_->sdt);
116         stag_.push_back(normalize_tag(prb_->stag, prb_->ndims));
117         dtag_ = normalize_tag(prb_->dtag, prb_->ndims);
118         base_report(r, prb_str);
119     }
120 
dump_algreduction::perf_report_t121     void dump_alg(std::ostream &s) const override { s << alg2str(prb_->alg); }
122 
dump_descreduction::perf_report_t123     void dump_desc(std::ostream &s) const override {
124         s << prb_->src_dims << ":" << prb_->dst_dims;
125     }
126 
attrreduction::perf_report_t127     const attr_t *attr() const override { return &prb_->attr; }
sdtreduction::perf_report_t128     const std::vector<dnnl_data_type_t> *sdt() const override { return &sdt_; }
ddtreduction::perf_report_t129     const dnnl_data_type_t *ddt() const override { return &prb_->ddt; }
stagreduction::perf_report_t130     const std::vector<std::string> *stag() const override { return &stag_; }
dtagreduction::perf_report_t131     const std::string *dtag() const override { return &dtag_; }
132 
133 private:
134     const prb_t *prb_ = NULL;
135     std::vector<dnnl_data_type_t> sdt_;
136     std::vector<std::string> stag_;
137     std::string dtag_;
138 };
139 
140 void compute_ref(const prb_t *prb, const dnn_mem_t &src,
141         const std::vector<dnn_mem_t> &binary_po, dnn_mem_t &dst);
142 
143 int doit(const prb_t *prb, res_t *res);
144 int bench(int argc, char **argv);
145 
146 } // namespace reduction
147 
148 #endif
149