onednn-src 0.1.13

Source of oneAPI Deep Neural Network Library (oneDNN)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*******************************************************************************
* Copyright 2020 Intel Corporation
* Copyright 2020 Codeplay Software Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#ifndef GPU_NVIDIA_CUDNN_CONV_INNER_PRODUCT_HPP
#define GPU_NVIDIA_CUDNN_CONV_INNER_PRODUCT_HPP

#include "cudnn.h"

#include "common/c_types_map.hpp"
#include "common/inner_product_pd.hpp"
#include "common/primitive.hpp"
#include "gpu/nvidia/cudnn_conv_inner_product_impl.hpp"
#include "gpu/nvidia/cudnn_inner_product.hpp"
#include "gpu/nvidia/engine.hpp"
#include "gpu/nvidia/sycl_cuda_utils.hpp"

namespace dnnl {
namespace impl {
namespace gpu {
namespace nvidia {
namespace {
inline status_t init_mem_by_tag(format_tag_t tag, memory_desc_t &md) {
    if (tag == format_tag::undef) { return status::unimplemented; }
    CHECK(memory_desc_init_by_tag(md, tag));
    return status::success;
}

inline format_tag_t get_tag(const memory_desc_t &md) {
    using namespace format_tag;
    auto tag = memory_desc_matches_one_of_tag(md, ab, abc, abcd,
            abcde, // NCHW derivatives
            ba, bca, bcda, bcdea, cba, cdba,
            cdeba, // IO and spatial derivatives
            acb, acdb, acdeb, // NHWC derivatives
            aBcd16b, aBcde16b, aBcd8b, aBcde8b, aBcd4b,
            aBcde4b); // blocked layouts
    return tag;
}
} // namespace

struct cudnn_conv_inner_product_fwd_t : public cudnn_inner_product_fwd_t {
    using cudnn_inner_product_fwd_t::cudnn_inner_product_fwd_t;
    using parent_pd_t = cudnn_inner_product_fwd_t::pd_t;
    struct pd_t : public parent_pd_t {
        using parent_pd_t::parent_pd_t;

        DECLARE_COMMON_PD_T("cuda:cudnn:conv", cudnn_conv_inner_product_fwd_t);

        status_t init(impl::engine_t *engine) {
            using namespace data_type;
            using namespace prop_kind;
            using sm_t = primitive_attr_t::skip_mask_t;

            const auto attr_skip_mask = sm_t::scales | sm_t::post_ops;
            // Flag for checking if the fused routine can be used for the
            // blocked format case. If set to true, that implies ReLU and
            // blocking are used.
            bool use_fused_path_for_blocking = false;
            bool ok = true && set_default_params() == status::success;
            ok = ok
                    && utils::one_of(desc()->prop_kind, forward_training,
                            forward_inference)
                    && data_types_ok(engine) && memory_format_ok(src_md())
                    && memory_format_ok(weights_md(0))
                    && memory_format_ok(dst_md())
                    && blocking_ok(with_eltwise(), use_fused_path_for_blocking)
                    && IMPLICATION(with_bias(), memory_format_ok(weights_md(1)))
                    && attr()->has_default_values(attr_skip_mask)
                    && attr_post_ops_ok(attr())
                    && IMPLICATION(!attr()->scales_.has_default_values(),
                            utils::one_of(src_md_.data_type, s8)
                                    && attr_scales_ok());
            if (!ok) return status::unimplemented;
            if (has_zero_dim_memory()) return status::success;

            inner_product_impl_.reset(
                    new cudnn_conv_inner_product_fwd_impl_t());

            auto st = inner_product_impl_->init(engine, this, with_relu(),
                    with_eltwise(), with_sum(), use_fused_path_for_blocking,
                    false);
            return st;
        }
        bool with_eltwise() const {
            return attr()->post_ops_.find(primitive_kind::eltwise) != -1;
        }

        bool with_relu() const {
            auto idx = attr()->post_ops_.find(primitive_kind::eltwise);
            if (idx != -1) { return attr()->post_ops_.entry_[idx].is_relu(); }
            return false;
        }

        bool with_sum() const {
            return attr()->post_ops_.find(primitive_kind::sum) != -1;
        }

        status_t set_default_params() {
            using namespace format_tag;

            // Although cuDNN does support arbitrary striding in the src
            // and dst tensors, it does not support filters in any format
            // where the N dimension follows the C dimension. So transpose the
            // filter here if that is that case, and the src along with it.
            auto set_default = [&]() {
                if (ndims() < 5 && src_md_.data_type == data_type::s8) {
                    CHECK(init_mem_by_tag(
                            utils::pick(ndims() - 2, ab, acb, acdb, acdeb),
                            src_md_));
                } else {
                    CHECK(init_mem_by_tag(
                            utils::pick(ndims() - 2, ab, abc, abcd, abcde),
                            src_md_));
                }
                CHECK(init_mem_by_tag(get_tag(src_md_), weights_md_));

                return status::success;
            };

            if ((src_md()->format_kind == format_kind::any)
                    && (weights_md(0)->format_kind == format_kind::any)) {
                CHECK(set_default());
            } else if ((src_md()->format_kind == format_kind::any)
                    && (weights_md(0)->format_kind != format_kind::any)) {
                CHECK(init_mem_by_tag(get_tag(weights_md_), src_md_));
            } else if ((src_md()->format_kind != format_kind::any)
                    && (weights_md(0)->format_kind == format_kind::any)) {
                CHECK(init_mem_by_tag(get_tag(src_md_), weights_md_));
            }

            if (dst_md()->format_kind == format_kind::any)
                CHECK(memory_desc_init_by_tag(dst_md_, nc));
            if (weights_md(1)->format_kind == format_kind::any)
                CHECK(memory_desc_init_by_tag(bias_md_, x));
            return status::success;
        }

        bool blocking_ok(
                bool with_relu, bool &use_fused_path_for_blocking) const {
            // Bias and dst should not be blocked.
            if (weights_md(1)->format_desc.blocking.inner_nblks
                            + dst_md()->format_desc.blocking.inner_nblks
                    != 0)
                return false;
            // If the src and filter are not blocked, done.
            if (src_md()->format_desc.blocking.inner_nblks
                            + weights_md(0)->format_desc.blocking.inner_nblks
                    == 0)
                return true;

            use_fused_path_for_blocking = with_relu;
            // Otherwise check blocking is done on C dimension, that the block
            // size is 4, that INT8 is used, that both srcs are blocked, and
            // check whether ReLU is used (this enables the fast path).
            return memory_desc_matches_nchw_vect_c(src_md())
                    && memory_desc_matches_nchw_vect_c(weights_md(0));
        }

        bool data_types_ok(impl::engine_t *engine) const {
            using namespace data_type;
            dnnl_data_type_t src_type = src_md()->data_type;
            dnnl_data_type_t weights_type = weights_md(0)->data_type;
            dnnl_data_type_t bias_type = weights_md(1)->data_type;
            dnnl_data_type_t dst_type = dst_md()->data_type;
            dnnl_data_type_t acc_type = desc()->accum_data_type;

            auto *sycl_engine = utils::downcast<nvidia::engine_t *>(engine);

            if (!IMPLICATION(utils::one_of(bf16, src_type, weights_type,
                                     bias_type, dst_type, acc_type),
                        has_bf16_support(sycl_engine->device()))) {
                return false;
            }

            bool src_wei_match = src_type == weights_type;

            // If no bias used, there is no need to check it
            auto bias_may_use_type = with_bias() ? bias_type : src_type;
            bool bias_match = IMPLICATION(with_bias(),
                    bias_type == f32
                            || utils::everyone_is(f16, src_type, weights_type,
                                    bias_type, dst_type));

            bool acc_match = src_wei_match && src_type == s8 ? acc_type == s32
                    : bias_match && bias_may_use_type == f16 ? acc_type == f16
                                                             : acc_type == f32;

            switch (dst_type) {
                case f32:
                    return src_wei_match && bias_match && acc_match
                            && (src_type == f32 || src_type == bf16);
                case f16:
                    return bias_match && acc_match && bias_may_use_type == f16;
                case bf16:
                    return bias_match && acc_match
                            && (bias_may_use_type == bf16
                                    || bias_may_use_type == f32);
                case s8:
                    return src_wei_match && acc_match && weights_type == s8;
                default: return false;
            }
            return false;
        }
    };

    const pd_t *pd() const override {
        return (const pd_t *)primitive_t::pd().get();
    }
};

struct cudnn_conv_inner_product_bwd_data_t
    : public cudnn_inner_product_bwd_data_t {
    using cudnn_inner_product_bwd_data_t::cudnn_inner_product_bwd_data_t;
    using parent_pd_t = cudnn_inner_product_bwd_data_t::pd_t;
    struct pd_t : public parent_pd_t {
        using parent_pd_t::parent_pd_t;

        DECLARE_COMMON_PD_T(
                "cuda:cudnn:conv", cudnn_conv_inner_product_bwd_data_t);

        status_t init(impl::engine_t *engine) {
            using namespace data_type;
            using namespace prop_kind;

            bool ok = true && set_default_params() == status::success;
            ok = ok && desc()->prop_kind == backward_data
                    && data_types_ok(engine) && no_blocking()
                    && attr()->has_default_values()
                    && memory_format_ok(diff_src_md())
                    && memory_format_ok(weights_md(0))
                    && memory_format_ok(diff_dst_md());

            if (!ok) return status::unimplemented;
            if (has_zero_dim_memory()) return status::success;

            inner_product_impl_.reset(
                    new cudnn_conv_inner_product_bwd_data_impl_t());

            return inner_product_impl_->init(
                    engine, this, false, false, false, false, false);
        }

        status_t set_default_params() {
            using namespace format_tag;

            auto set_default_diff_src = [&]() {
                if (weights_md_.format_kind == format_kind::any) {
                    CHECK(init_mem_by_tag(
                            utils::pick(ndims() - 2, ab, abc, abcd, abcde),
                            diff_src_md_));
                } else {
                    CHECK(init_mem_by_tag(get_tag(weights_md_), diff_src_md_));
                }
                return status::success;
            };

            auto set_default_weights = [&]() {
                CHECK(init_mem_by_tag(get_tag(diff_src_md_), weights_md_));
                return status::success;
            };

            if (diff_src_md_.format_kind == format_kind::any)
                CHECK(set_default_diff_src());
            if (weights_md_.format_kind == format_kind::any)
                CHECK(set_default_weights());
            if (diff_dst_md_.format_kind == format_kind::any)
                CHECK(memory_desc_init_by_tag(diff_dst_md_, nc));
            return status::success;
        }

        bool no_blocking() const {
            return diff_src_md()->format_desc.blocking.inner_nblks
                    + weights_md(0)->format_desc.blocking.inner_nblks
                    + diff_dst_md()->format_desc.blocking.inner_nblks
                    == 0;
        }

        bool data_types_ok(impl::engine_t *engine) const {
            auto *sycl_engine = utils::downcast<nvidia::engine_t *>(engine);

            auto diff_src_dt = diff_src_md()->data_type;
            auto weights_dt = weights_md(0)->data_type;
            auto diff_dst_dt = diff_dst_md()->data_type;
            auto acc_dt = desc()->accum_data_type;

            if (!IMPLICATION(utils::one_of(data_type::bf16, diff_src_dt,
                                     weights_dt, diff_dst_dt, acc_dt),
                        has_bf16_support(sycl_engine->device()))) {
                return false;
            }

            return utils::everyone_is(data_type::f32, diff_src_dt, weights_dt,
                    diff_dst_dt, acc_dt);
        }
    };

    const pd_t *pd() const override {
        return (const pd_t *)primitive_t::pd().get();
    }
};

struct cudnn_conv_inner_product_bwd_weights_t
    : public cudnn_inner_product_bwd_weights_t {
    using cudnn_inner_product_bwd_weights_t::cudnn_inner_product_bwd_weights_t;
    using parent_pd_t = cudnn_inner_product_bwd_weights_t::pd_t;
    struct pd_t : public parent_pd_t {
        using parent_pd_t::parent_pd_t;
        DECLARE_COMMON_PD_T(
                "cuda:cudnn:conv", cudnn_conv_inner_product_bwd_weights_t);

        status_t init(impl::engine_t *engine) {
            using namespace data_type;
            using namespace prop_kind;
            bool ok = true && (set_default_params() == status::success);
            ok = ok && (desc()->prop_kind == backward_weights)
                    && data_types_ok(engine) && no_blocking()
                    && attr()->has_default_values()
                    && memory_format_ok(src_md())
                    && memory_format_ok(diff_weights_md(0))
                    && memory_format_ok(diff_dst_md())
                    && IMPLICATION(
                            with_bias(), memory_format_ok(diff_weights_md(1)));

            if (!ok) return status::unimplemented;
            if (has_zero_dim_memory()) return status::success;

            inner_product_impl_.reset(
                    new cudnn_conv_inner_product_bwd_weights_impl_t());

            return inner_product_impl_->init(
                    engine, this, false, false, false, false, false);
        }

        status_t set_default_params() {
            using namespace format_tag;

            auto set_default_src = [&]() {
                if (diff_weights_md_.format_kind == format_kind::any) {
                    CHECK(init_mem_by_tag(
                            utils::pick(ndims() - 2, ab, abc, abcd, abcde),
                            src_md_));
                } else {
                    CHECK(init_mem_by_tag(get_tag(diff_weights_md_), src_md_));
                }
                return status::success;
            };

            auto set_default_diff_weights = [&]() {
                CHECK(init_mem_by_tag(get_tag(src_md_), diff_weights_md_));
                return status::success;
            };

            if (src_md_.format_kind == format_kind::any)
                CHECK(set_default_src());
            if (diff_weights_md_.format_kind == format_kind::any)
                CHECK(set_default_diff_weights());
            if (diff_dst_md_.format_kind == format_kind::any)
                CHECK(memory_desc_init_by_tag(diff_dst_md_, nc));
            if (diff_bias_md_.format_kind == format_kind::any)
                CHECK(memory_desc_init_by_tag(diff_bias_md_, x));
            return status::success;
        }

        bool no_blocking() const {
            return src_md()->format_desc.blocking.inner_nblks
                    + diff_weights_md(0)->format_desc.blocking.inner_nblks
                    + diff_weights_md(1)->format_desc.blocking.inner_nblks
                    + diff_dst_md()->format_desc.blocking.inner_nblks
                    == 0;
        }

        bool data_types_ok(impl::engine_t *engine) const {
            auto *sycl_engine = utils::downcast<nvidia::engine_t *>(engine);

            if (!IMPLICATION(utils::one_of(data_type::bf16,
                                     diff_weights_md(1)->data_type,
                                     src_md()->data_type,
                                     diff_weights_md(0)->data_type,
                                     diff_dst_md()->data_type,
                                     desc()->accum_data_type),
                        has_bf16_support(sycl_engine->device()))) {
                return false;
            }
            return IMPLICATION(with_bias(),
                           diff_weights_md(1)->data_type == data_type::f32)
                    && utils::everyone_is(data_type::f32, src_md()->data_type,
                            diff_weights_md(0)->data_type,
                            diff_dst_md()->data_type, desc()->accum_data_type);
        }
    };

    const pd_t *pd() const override {
        return (const pd_t *)primitive_t::pd().get();
    }
};

} // namespace nvidia
} // namespace gpu
} // namespace impl
} // namespace dnnl

#endif