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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*******************************************************************************
* Copyright 2023 Intel Corporation
*
* 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_GENERIC_SYCL_REDUCTION_KERNELS_HPP
#define GPU_GENERIC_SYCL_REDUCTION_KERNELS_HPP

#include "common/c_types_map.hpp"
#include "common/dnnl_thread.hpp"
#include "common/primitive_exec_types.hpp"
#include "common/utils.hpp"
#include "gpu/generic/sycl/sycl_io_helper.hpp"
#include "gpu/generic/sycl/sycl_math_utils.hpp"
#include "gpu/generic/sycl/sycl_primitive_conf.hpp"
#include "xpu/sycl/memory_storage_base.hpp"
#include "xpu/sycl/types.hpp"

namespace dnnl {
namespace impl {
namespace gpu {
namespace generic {
namespace sycl {

struct init_kernel_t {
    init_kernel_t(xpu::sycl::out_memory_arg_t &out_arg, alg_kind_t alg)
        : out_arg_(out_arg), alg_(alg) {}

    void operator()(::sycl::item<1> item) const {
        auto *out = reinterpret_cast<float *>(out_arg_.get_pointer());
        const int idx = item.get_linear_id();
        float val = 0;
        if (alg_ == alg_kind::reduction_min)
            val = std::numeric_limits<float>::max();
        else if (alg_ == alg_kind::reduction_max)
            val = std::numeric_limits<float>::lowest();
        else if (alg_ == alg_kind::reduction_mul)
            val = 1;
        else
            val = 0;

        out[idx] = val;
    }

private:
    xpu::sycl::out_memory_arg_t out_arg_;
    alg_kind_t alg_;
};

struct atomic_finalize_kernel_t {
    atomic_finalize_kernel_t(::sycl::handler &cgh, const exec_ctx_t &ctx,
            data_type_t dt, xpu::sycl::out_memory_arg_t &out_arg,
            alg_kind_t alg, float p, float eps, sycl_post_ops_t &post_ops,
            xpu::sycl::md_t dst_md, int reduce_size)
        : dt_(dt)
        , out_arg_(out_arg)
        , alg_(alg)
        , p_(p)
        , eps_(eps)
        , post_ops_(post_ops)
        , po_args_(cgh, ctx, post_ops)
        , dst_md_(dst_md)
        , reduce_size_(reduce_size) {}

    void operator()(::sycl::item<1> item) const {
        void *out_ptr = out_arg_.get_pointer();
        auto idx = item.get_linear_id();
        auto val = load_float_value(dt_, out_ptr, idx);
        if (alg_ == alg_kind::reduction_norm_lp_max) {
            val = ::sycl::rootn(::sycl::max(val, eps_), p_);
        } else if (alg_ == alg_kind::reduction_norm_lp_sum) {
            val = ::sycl::rootn(val + eps_, p_);
        } else if (alg_ == alg_kind::reduction_norm_lp_power_p_max) {
            val = ::sycl::max(val, eps_);
        } else if (alg_ == alg_kind::reduction_norm_lp_power_p_sum) {
            val = val + eps_;
        } else if (alg_ == alg_kind::reduction_mean) {
            val = val / reduce_size_;
        }

        auto prev_val = val;
        auto l_offset = idx;
        dims_t pos;
        for (int i = 0; i < dst_md_.ndims(); i++) {
            const int d = dst_md_.ndims() - 1 - i;
            const dim_t cur_dim = dst_md_.dims()[d];
            pos[d] = l_offset % cur_dim;
            l_offset = l_offset / cur_dim;
        }
        val = post_ops_.apply(val, prev_val, po_args_, pos);

        store_float_value(dt_, val, out_ptr, idx);
    }

private:
    data_type_t dt_;
    xpu::sycl::out_memory_arg_t out_arg_;
    alg_kind_t alg_;
    float p_, eps_;
    sycl_post_ops_t post_ops_;
    post_op_input_args po_args_;
    xpu::sycl::md_t dst_md_;
    int reduce_size_;
};

struct Reducer {
    alg_kind_t alg_;
    float p_, eps_;
    bool needs_prepare_;
    bool iter_needs_finalize_;

    Reducer(alg_kind_t alg, float p, float eps, bool needs_prepare,
            bool iter_needs_finalize)
        : alg_(alg)
        , p_(p)
        , eps_(eps)
        , needs_prepare_(needs_prepare)
        , iter_needs_finalize_(iter_needs_finalize) {}

    inline float identity() const {
        if (alg_ == alg_kind::reduction_min) {
            return std::numeric_limits<float>::max();
        } else if (alg_ == alg_kind::reduction_max) {
            return std::numeric_limits<float>::lowest();
        } else if (alg_ == alg_kind::reduction_mul) {
            return 1.f;
        }

        return 0.f;
    }

    inline bool needs_finalize() const {
        return (alg_ == alg_kind::reduction_mean
                || alg_ == alg_kind::reduction_norm_lp_max
                || alg_ == alg_kind::reduction_norm_lp_sum
                || alg_ == alg_kind::reduction_norm_lp_power_p_max
                || alg_ == alg_kind::reduction_norm_lp_power_p_sum);
    }

    inline float subgroup_reduce(
            ::sycl::sub_group &subgroup, float sg_input) const {
        if (alg_ == alg_kind::reduction_sum || alg_ == alg_kind::reduction_mean
                || alg_ == alg_kind::reduction_norm_lp_max
                || alg_ == alg_kind::reduction_norm_lp_sum
                || alg_ == alg_kind::reduction_norm_lp_power_p_max
                || alg_ == alg_kind::reduction_norm_lp_power_p_sum) {
            return ::sycl::reduce_over_group(
                    subgroup, sg_input, ::sycl::plus<float> {});
        } else if (alg_ == alg_kind::reduction_min) {
            return ::sycl::reduce_over_group(
                    subgroup, sg_input, ::sycl::minimum<float> {});
        } else if (alg_ == alg_kind::reduction_max) {
            return ::sycl::reduce_over_group(
                    subgroup, sg_input, ::sycl::maximum<float> {});
        } else if (alg_ == alg_kind::reduction_mul) {
            return ::sycl::reduce_over_group(
                    subgroup, sg_input, ::sycl::multiplies<float> {});
        }

        return ::sycl::nan(0U);
    }

    inline float reduce(float lhs, float rhs) const {
        if (alg_ == alg_kind::reduction_sum || alg_ == alg_kind::reduction_mean
                || alg_ == alg_kind::reduction_norm_lp_max
                || alg_ == alg_kind::reduction_norm_lp_sum
                || alg_ == alg_kind::reduction_norm_lp_power_p_max
                || alg_ == alg_kind::reduction_norm_lp_power_p_sum) {
            return lhs + rhs;
        } else if (alg_ == alg_kind::reduction_min) {
            return ::sycl::min(lhs, rhs);
        } else if (alg_ == alg_kind::reduction_max) {
            return ::sycl::max(lhs, rhs);
        } else if (alg_ == alg_kind::reduction_mul) {
            return lhs * rhs;
        }

        return ::sycl::nan(0U);
    }

    template <::sycl::memory_order Order, ::sycl::memory_scope Scope,
            ::sycl::access::address_space Space>
    void atomic_op(data_type_t dt, void *ref, int idx, float val, int size) {
        auto atomic_out = ::sycl::atomic_ref<float, Order, Scope, Space>(
                reinterpret_cast<float *>(ref)[idx]);
        if (alg_ == alg_kind::reduction_sum || alg_ == alg_kind::reduction_mean
                || alg_ == alg_kind::reduction_norm_lp_max
                || alg_ == alg_kind::reduction_norm_lp_sum
                || alg_ == alg_kind::reduction_norm_lp_power_p_max
                || alg_ == alg_kind::reduction_norm_lp_power_p_sum) {
            atomic_out.fetch_add(val);
        } else if (alg_ == alg_kind::reduction_min) {
            atomic_out.fetch_min(val);
        } else if (alg_ == alg_kind::reduction_max) {
            atomic_out.fetch_max(val);
        }
    }

    inline void prepare(float &val) {
        if (needs_prepare_
                && (alg_ == alg_kind::reduction_norm_lp_max
                        || alg_ == alg_kind::reduction_norm_lp_sum
                        || alg_ == alg_kind::reduction_norm_lp_power_p_max
                        || alg_ == alg_kind::reduction_norm_lp_power_p_sum)) {
            val = ::sycl::pow(::sycl::fabs(val), p_);
        }
    }

    inline void finalize(float &val, int size) {
        if (alg_ == alg_kind::reduction_mean) {
            val /= size;
        } else if (alg_ == alg_kind::reduction_norm_lp_max
                && iter_needs_finalize_) {
            val = ::sycl::rootn(::sycl::max(val, eps_), p_);
        } else if (alg_ == alg_kind::reduction_norm_lp_sum
                && iter_needs_finalize_) {
            val = ::sycl::rootn(val + eps_, p_);
        } else if (alg_ == alg_kind::reduction_norm_lp_power_p_max
                && iter_needs_finalize_) {
            val = ::sycl::max(val, eps_);
        } else if (alg_ == alg_kind::reduction_norm_lp_power_p_sum
                && iter_needs_finalize_) {
            val = val + eps_;
        }
    }
};

struct LocalMemTile {
    using T = float;
    using Index = int;

    static constexpr Index Dim = 3;
    // XXX: Set this depending on reduction size for optimisation
    static constexpr bool CheckBounds = true;
    static constexpr Index RowDim = Dim == 3 ? 1 : 0;
    static constexpr Index ColDim = Dim == 3 ? 2 : 1;
    Index row_tile_;
    Index col_tile_;
    Index row_lim_;
    Index col_lim_;
    Index local_id_;
    Index local_row_id_;
    Index local_col_id_;
    Index wg_id_;
    ::sycl::nd_item<Dim> &nd_item_;
    bool bank_offset_;
    T pad_val_;

    LocalMemTile(data_type_t src_dt, data_type_t dst_dt, Index row_tile,
            Index col_tile, Index row_lim, Index col_lim,
            ::sycl::nd_item<Dim> &nd_item, bool bank_offset, T pad_val = 0)
        : row_tile_ {row_tile}
        , col_tile_ {col_tile}
        , row_lim_ {row_lim}
        , col_lim_ {col_lim}
        , local_id_ {static_cast<Index>(nd_item.get_local_linear_id())}
        , local_row_id_ {static_cast<Index>(nd_item.get_local_id(RowDim))}
        , local_col_id_ {static_cast<Index>(nd_item.get_local_id(ColDim))}
        , wg_id_ {static_cast<Index>(nd_item.get_group(0))}
        , nd_item_(nd_item)
        , bank_offset_(bank_offset)
        , pad_val_ {pad_val} {}

    Index get_row_tile() { return row_tile_; }
    Index get_col_tile() { return col_tile_; }

    Index get_local_id(bool is_transposed = false) {
        if (!is_transposed) {
            return local_row_id_ * (col_tile_ + bank_offset_) + local_col_id_;
        } else {
            return local_col_id_ * (row_tile_ + bank_offset_) + local_row_id_;
        }
    }

    Index get_local_row_id() { return local_row_id_; }
    Index get_local_col_id() { return local_col_id_; }
    Index get_wg_id() { return wg_id_; }
    Index get_global_row_id() { return nd_item_.get_global_id(RowDim); }
    Index get_global_col_id() { return nd_item_.get_global_id(ColDim); }

    void load_memory(data_type_t in_dt, void *global_in, data_type_t local_dt,
            const xpu::sycl::md_t &in_md, void *local_out, Reducer &reducer,
            bool is_first_red_iter) {
        auto const local_id = get_local_id();
        auto const wg_batch_id = get_wg_id();
        auto const global_row_id = get_global_row_id();
        auto const global_col_id = get_global_col_id();

        auto const global_id = wg_batch_id * row_lim_ * col_lim_
                + global_row_id * col_lim_ + global_col_id;

        if constexpr (CheckBounds) {
            const auto within_bounds
                    = global_row_id < row_lim_ && global_col_id < col_lim_;
            int idx = is_first_red_iter ? in_md.off_l(global_id) : global_id;
            float val = within_bounds ? load_float_value(in_dt, global_in, idx)
                                      : pad_val_;
            reducer.prepare(val);
            store_float_value(local_dt, val, local_out, local_id);
        } else {
            // maybe do subgroup load
            auto val = load_float_value(in_dt, global_in, global_id);
            reducer.prepare(val);
            store_float_value(local_dt, val, local_out, local_id);
        }
        group_barrier(nd_item_.get_group());
    }

    void store_memory(data_type_t in_dt, void *local_in, data_type_t out_dt,
            void *global_out, const xpu::sycl::md_t &out_md, bool is_reduced,
            bool is_transposed, bool is_last_red_iter,
            const sycl_post_ops_t &post_ops,
            const post_op_input_args &po_args) {
        auto const local_id = get_local_id(is_transposed);
        auto const wg_batch_id = get_wg_id();
        auto const col_id = get_global_col_id();

        Index row_id;
        Index row_lim;
        Index global_id;
        if (is_reduced) {
            row_id = get_local_row_id();
            row_lim = get_row_tile();
            global_id = wg_batch_id * col_lim_ + col_id;
        } else {
            row_id = get_global_row_id();
            row_lim = row_lim_;
            if (is_transposed) {
                global_id
                        = (wg_batch_id * col_lim_ + col_id) * row_lim_ + row_id;
            } else {
                global_id
                        = (wg_batch_id * row_lim_ + row_id) * col_lim_ + col_id;
            }
        }

        if (row_id < row_lim && col_id < col_lim_) {
            auto val = load_float_value(in_dt, local_in, local_id);
            auto idx = is_last_red_iter ? out_md.off_l(global_id) : global_id;
            float prev_val = load_float_value(out_dt, global_out, idx);

            if (is_last_red_iter) {
                auto l_offset = global_id;
                dims_t pos;
                for (int i = 0; i < out_md.ndims(); i++) {
                    const int d = out_md.ndims() - 1 - i;
                    const dim_t cur_dim = out_md.dims()[d];
                    pos[d] = l_offset % cur_dim;
                    l_offset = l_offset / cur_dim;
                }
                val = post_ops.apply(val, prev_val, po_args, pos);
            }

            store_float_value(out_dt, val, global_out, idx);
        }
    }

    T load_local(data_type_t dt, void *input, int index) {
        return load_float_value(dt, input, index); // ((T *)input)[index];
    }

    void store_local(data_type_t dt, void *output, int index, T val) {
        store_float_value(dt, val, output, index);
    }

private:
    template <bool Inplace>
    void transpose_impl_(
            data_type_t in_dt, void *input, data_type_t out_dt, void *output) {
        auto const local_id = get_local_id(false);
        auto const trans_local_id = get_local_id(true);
        auto group = nd_item_.get_group();
        const auto val = load_local(in_dt, input, local_id);
        if constexpr (Inplace) { group_barrier(group); }
        store_local(out_dt, output, trans_local_id, val);
        group_barrier(group);
    }

public:
    void transpose(
            data_type_t in_dt, void *input, data_type_t out_dt, void *output) {
        transpose_impl_<false>(in_dt, input, out_dt, output);
    }

    void transpose(data_type_t dt, void *input) {
        transpose_impl_<true>(dt, input, dt, input);
    }

    void sg_reduce_impl(data_type_t in_dt, void *input, data_type_t out_dt,
            void *output, bool is_transposed, Reducer &reducer) {
        Index const reduce_lim = row_tile_;
        Index const stride_lim = col_tile_;
        if (row_tile_ == 1) { return; }

        auto group = nd_item_.get_group();
        auto subgroup = nd_item_.get_sub_group();

        Index const sg_size = subgroup.get_max_local_range()[0];
        Index const sg_group_id = subgroup.get_group_linear_id();
        Index const sg_local_id = subgroup.get_local_linear_id();

        // The tile is read in the shape [row_tile, col_tile] ->
        // [num_row_blocks*max_subgroup_size, col_tile]. Thus the local range has
        // the same shape (num_row_blocks*max_subgroup_size, col_tile), using this
        // we can assign num_row_blocks*col_tile subgroups to reduce a chunk of
        // memory size max_subgroup_size

        auto const num_reduce_blocks
                = ::sycl::max((reduce_lim) / sg_size, Index {1});
        auto const reduce_block_id = sg_group_id % num_reduce_blocks;
        auto const local_reduce_id = reduce_block_id * sg_size + sg_local_id;
        auto const local_stride_id = sg_group_id / num_reduce_blocks;

        Index input_id;
        Index output_id;
        if (!is_transposed) {
            input_id = local_reduce_id * (stride_lim + bank_offset_)
                    + local_stride_id;
            output_id = reduce_block_id * (stride_lim + bank_offset_)
                    + local_stride_id;
        } else {
            input_id = local_stride_id * (reduce_lim + bank_offset_)
                    + local_reduce_id;
            output_id = local_stride_id * (num_reduce_blocks + bank_offset_)
                    + reduce_block_id;
        }
        auto const sg_input = local_reduce_id < reduce_lim
                ? load_local(in_dt, input, input_id)
                : reducer.identity();
        float sg_output = reducer.subgroup_reduce(subgroup, sg_input);

        group_barrier(group);

        if (subgroup.leader()) {
            store_local(out_dt, output, output_id, sg_output);
        }
        row_tile_ = num_reduce_blocks;
        group_barrier(group);
    }

    void sg_reduce(data_type_t in_dt, void *input, data_type_t out_dt,
            void *output, int num_sg_reductions, bool is_transposed,
            Reducer &reducer) {
        for (auto i = 0; i < num_sg_reductions; i++) {
            sg_reduce_impl(
                    in_dt, input, out_dt, output, is_transposed, reducer);
        }
    }

    void sg_reduce(data_type_t dt, void *input, int num_sg_reductions,
            bool is_transposed, Reducer &reducer) {
        for (auto i = 0; i < num_sg_reductions; i++) {
            sg_reduce_impl(dt, input, dt, input, is_transposed, reducer);
        }
    }

    void wi_reduce_impl(data_type_t in_dt, void *input, data_type_t out_dt,
            void *output, bool is_transposed, Reducer &reducer) {
        if (row_lim_ == 1) { return; }

        auto output_id = get_local_id(is_transposed);
        if (get_local_row_id() == 0) {
            auto input_id = output_id;
            for (auto row_id = 1; row_id < row_tile_; row_id++) {
                if (is_transposed) {
                    input_id++;
                } else {
                    input_id += (col_tile_ + bank_offset_);
                }
                auto lhs = load_float_value(in_dt, input, input_id);
                auto rhs = load_float_value(out_dt, output, output_id);
                auto val = reducer.reduce(lhs, rhs);
                store_float_value(out_dt, val, output, output_id);
            }
        }

        group_barrier(nd_item_.get_group());
        row_tile_ = 1;

        if (get_local_row_id() == 0) {
            auto final_output_id
                    = get_local_col_id() * (row_tile_ + bank_offset_);
            auto val = load_float_value(out_dt, output, output_id);
            store_float_value(out_dt, val, output, final_output_id);
        }
        group_barrier(nd_item_.get_group());
    }

    void wi_reduce(
            data_type_t dt, void *input, bool is_transposed, Reducer &reducer) {
        wi_reduce_impl(dt, input, dt, input, is_transposed, reducer);
    }

    void finalize_reduce(data_type_t dt, void *input, int finalize_param,
            bool is_transposed, Reducer &reducer) {
        if (reducer.needs_finalize()) {
            const auto local_row_id = get_local_row_id();
            const auto local_id = get_local_id(is_transposed);
            if (local_row_id == 0) {
                auto val = load_float_value(dt, input, local_id);
                reducer.finalize(val, finalize_param);
                store_float_value(dt, val, input, local_id);
            }
        }
    }

    template <::sycl::memory_order Order, ::sycl::memory_scope Scope,
            ::sycl::access::address_space Space>
    void atomic_reduce(data_type_t in_dt, void *input, data_type_t out_dt,
            void *output, const xpu::sycl::md_t &out_md, Index finalize_param,
            Index batch_groups, bool is_transposed, Reducer &reducer) {
        constexpr bool GlobalReduce
                = Space == ::sycl::access::address_space::global_space;

        // Get local indexes
        const auto local_row_id = get_local_row_id();
        const auto local_col_id = get_local_col_id();

        // Get global column id and limit
        const auto global_col_id
                = GlobalReduce ? get_global_col_id() : local_col_id;
        const auto global_col_lim = GlobalReduce ? col_lim_ : col_tile_;

        const auto row_id_ok = local_row_id < row_tile_;
        const auto col_id_ok
                = GlobalReduce ? global_col_id < global_col_lim : true;

        if (row_id_ok && col_id_ok) {
            const auto outer_id = GlobalReduce ? get_wg_id() : 0;
            const auto output_id = out_md.off_l(
                    (outer_id % batch_groups) * global_col_lim + global_col_id);
            const auto input_id = get_local_id(is_transposed);
            reducer.atomic_op<Order, Scope, Space>(out_dt, output, output_id,
                    load_float_value(in_dt, input, input_id), finalize_param);
        }

        // Atomics do a full reduction of the row.
        row_tile_ = 1;
    }
};

struct reduction_kernel_fwd_t {
    using LocalMem = ::sycl::local_accessor<uint8_t, 1>;

    static auto constexpr Order = ::sycl::memory_order::relaxed;
    static auto constexpr DeviceScope = ::sycl::memory_scope::device;
    static auto constexpr WGScope = ::sycl::memory_scope::work_group;
    static auto constexpr GlobalSpace
            = ::sycl::access::address_space::global_space;
    static auto constexpr LocalSpace
            = ::sycl::access::address_space::local_space;

    reduction_kernel_fwd_t(const sycl_reduction_conf_t &conf, int row_tile,
            int col_tile, int batch_groups, bool needs_atomic_reduce,
            LocalMem &local_mem, ::sycl::handler &cgh, const exec_ctx_t &ctx)
        : conf_(conf)
        , src_(CTX_IN_SYCL_KERNEL_MEMORY(DNNL_ARG_SRC))
        , dst_(CTX_OUT_SYCL_KERNEL_MEMORY(DNNL_ARG_DST))
        , local_mem_(local_mem)
        , row_tile_(row_tile)
        , col_tile_(col_tile)
        , batch_groups_(batch_groups)
        , needs_atomic_reduce_(needs_atomic_reduce)
        , po_args_(cgh, ctx, conf_.post_ops) {}

    reduction_kernel_fwd_t(xpu::sycl::in_memory_arg_t &src_arg,
            xpu::sycl::out_memory_arg_t &dst_arg,
            const sycl_reduction_conf_t &conf, bool needs_atomic_reduce,
            LocalMem &local_mem, ::sycl::handler &cgh, const exec_ctx_t &ctx)
        : conf_(conf)
        , src_(src_arg)
        , dst_(dst_arg)
        , local_mem_(local_mem)
        , row_tile_(conf.tile_row)
        , col_tile_(conf.tile_col)
        , batch_groups_(conf.batch_groups)
        , needs_atomic_reduce_(needs_atomic_reduce)
        , po_args_(cgh, ctx, conf_.post_ops) {}

    void operator()(::sycl::nd_item<3> nd_item) const {
        Reducer reducer(conf_.alg, conf_.p, conf_.eps, conf_.is_first_iter,
                conf_.is_last_iter);

        LocalMemTile tile(conf_.src_dt, conf_.dst_dt, row_tile_, col_tile_,
                conf_.reduce_size, conf_.stride_size, nd_item,
                conf_.bank_offset, reducer.identity());

        // Copy values from global memory to local
        tile.load_memory(conf_.src_dt, src_ptr(), conf_.local_mem_dt,
                conf_.src_md, local_ptr(), reducer, conf_.is_first_iter);

        if (conf_.transpose) {
            // Transpose data in local memory
            tile.transpose(conf_.local_mem_dt, local_ptr());
        }

        // Reduce values using subgroup reducer
        tile.sg_reduce(conf_.local_mem_dt, local_ptr(), conf_.num_sg_reductions,
                conf_.transpose, reducer);

        if (conf_.alg == alg_kind::reduction_mean
                && conf_.num_sg_reductions == 0) {
            // Reduce values using work-item reducer
            tile.wi_reduce(
                    conf_.local_mem_dt, local_ptr(), conf_.transpose, reducer);
        }

        if (needs_atomic_reduce_) {
            // Reduce remaining values into global memory using global atomics
            tile.atomic_reduce<Order, DeviceScope, GlobalSpace>(
                    conf_.local_mem_dt, local_ptr(), conf_.dst_dt, dst_ptr(),
                    conf_.dst_md,
                    conf_.reduce_size * conf_.batch_size / batch_groups_,
                    batch_groups_, conf_.transpose, reducer);
        } else {
            // Finalize reduction
            tile.finalize_reduce(conf_.local_mem_dt, local_ptr(),
                    conf_.reduce_size, conf_.transpose, reducer);
            tile.store_memory(conf_.local_mem_dt, local_ptr(), conf_.dst_dt,
                    dst_ptr(), conf_.dst_md, true, conf_.transpose,
                    conf_.is_last_iter, conf_.post_ops, po_args_);
        }
    }

private:
    void *src_ptr() const { return src_.get_pointer(); }
    void *dst_ptr() const { return dst_.get_pointer(); }
    void *local_ptr() const {
        return local_mem_.get_multi_ptr<::sycl::access::decorated::no>().get();
    }

    sycl_reduction_conf_t conf_;
    xpu::sycl::in_memory_arg_t src_;
    xpu::sycl::out_memory_arg_t dst_;
    LocalMem local_mem_;
    int row_tile_;
    int col_tile_;
    int batch_groups_;
    bool needs_atomic_reduce_;
    post_op_input_args po_args_;
};

} // namespace sycl
} // namespace generic
} // namespace gpu
} // namespace impl
} // namespace dnnl

#endif