1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *combined_reduction_kernel = R"==(/******************************************************************************* )==" "\n" |
6 | R"==(* Copyright 2021-2022 Intel Corporation )==" "\n" |
7 | R"==(* )==" "\n" |
8 | R"==(* Licensed under the Apache License, Version 2.0 (the "License"); )==" "\n" |
9 | R"==(* you may not use this file except in compliance with the License. )==" "\n" |
10 | R"==(* You may obtain a copy of the License at )==" "\n" |
11 | R"==(* )==" "\n" |
12 | R"==(* http: )==" "\n" |
13 | R"==(* )==" "\n" |
14 | R"==(* Unless required by applicable law or agreed to in writing, software )==" "\n" |
15 | R"==(* distributed under the License is distributed on an "AS IS" BASIS, )==" "\n" |
16 | R"==(* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. )==" "\n" |
17 | R"==(* See the License for the specific language governing permissions and )==" "\n" |
18 | R"==(* limitations under the License. )==" "\n" |
19 | R"==(*******************************************************************************/ )==" "\n" |
20 | R"==(#include "gpu/ocl/ocl_post_ops.h" )==" "\n" |
21 | R"==(#include "gpu/ocl/ocl_types.h" )==" "\n" |
22 | R"==(#if defined(IS_MAX) )==" "\n" |
23 | R"==(#define INIT_ACC TO_DEF_ACC_DATA_T(DATA_MIN) )==" "\n" |
24 | R"==(#elif defined(IS_MIN) )==" "\n" |
25 | R"==(#define INIT_ACC TO_DEF_ACC_DATA_T(DATA_MAX) )==" "\n" |
26 | R"==(#elif defined(IS_MUL) )==" "\n" |
27 | R"==(#define INIT_ACC TO_DEF_ACC_DATA_T(DATA_ONE) )==" "\n" |
28 | R"==(#else )==" "\n" |
29 | R"==(#define INIT_ACC TO_DEF_ACC_DATA_T(DATA_ZERO) )==" "\n" |
30 | R"==(#endif )==" "\n" |
31 | R"==(#if defined(SRC_DT_S8) || defined(SRC_DT_U8) || defined(SRC_DT_S32) )==" "\n" |
32 | R"==(#define MAX_FUNC max )==" "\n" |
33 | R"==(#define MIN_FUNC min )==" "\n" |
34 | R"==(#else )==" "\n" |
35 | R"==(#define MAX_FUNC fmax )==" "\n" |
36 | R"==(#define MIN_FUNC fmin )==" "\n" |
37 | R"==(#endif )==" "\n" |
38 | R"==(#if defined(IS_MAX) )==" "\n" |
39 | R"==(#define ACCUMULATE_INITIAL(x, y) MAX_FUNC(x, y) )==" "\n" |
40 | R"==(#elif defined(IS_MIN) )==" "\n" |
41 | R"==(#define ACCUMULATE_INITIAL(x, y) MIN_FUNC(x, y) )==" "\n" |
42 | R"==(#elif defined(IS_MEAN) || defined(IS_SUM) )==" "\n" |
43 | R"==(#define ACCUMULATE_INITIAL(x, y) (x + y) )==" "\n" |
44 | R"==(#elif defined(IS_MUL) )==" "\n" |
45 | R"==(#define ACCUMULATE_INITIAL(x, y) (x * y) )==" "\n" |
46 | R"==(#else )==" "\n" |
47 | R"==(#define ACCUMULATE_INITIAL(x, y) (x + pow(fabs(y), POWER)) )==" "\n" |
48 | R"==(#endif )==" "\n" |
49 | R"==(#if defined(IS_MAX) || defined(IS_MIN) || defined(IS_MEAN) || defined(IS_MUL) )==" "\n" |
50 | R"==(#define ACCUMULATE_FURTHER ACCUMULATE_INITIAL )==" "\n" |
51 | R"==(#else )==" "\n" |
52 | R"==(#define ACCUMULATE_FURTHER(x, y) (x + y) )==" "\n" |
53 | R"==(#endif )==" "\n" |
54 | R"==(#if IS_FIRST )==" "\n" |
55 | R"==(#define ACCUMULATE ACCUMULATE_INITIAL )==" "\n" |
56 | R"==(#else )==" "\n" |
57 | R"==(#define ACCUMULATE ACCUMULATE_FURTHER )==" "\n" |
58 | R"==(#endif )==" "\n" |
59 | R"==(#if defined(IS_MEAN) )==" "\n" |
60 | R"==(#define FINALIZE(x) (x / DIV) )==" "\n" |
61 | R"==(#elif defined(IS_LP_MAX) )==" "\n" |
62 | R"==(#define FINALIZE(x) rootn(fmax(x, EPS), POWER) )==" "\n" |
63 | R"==(#elif defined(IS_LP_SUM) )==" "\n" |
64 | R"==(#define FINALIZE(x) rootn(x + EPS, POWER) )==" "\n" |
65 | R"==(#elif defined(IS_P_MAX) )==" "\n" |
66 | R"==(#define FINALIZE(x) fmax(x, EPS) )==" "\n" |
67 | R"==(#elif defined(IS_P_SUM) )==" "\n" |
68 | R"==(#define FINALIZE(x) (x + EPS) )==" "\n" |
69 | R"==(#else )==" "\n" |
70 | R"==(#define FINALIZE(x) (x) )==" "\n" |
71 | R"==(#endif )==" "\n" |
72 | R"==(#define _SRC_OFF(outer, reduction, inner) \ )==" "\n" |
73 | R"==((outer) * REDUCTION_SIZE *INNER_DIM_SIZE + (reduction)*INNER_DIM_SIZE \ )==" "\n" |
74 | R"==(+ (inner) )==" "\n" |
75 | R"==(#define _DST_OFF(outer, reduction_chunk, inner) \ )==" "\n" |
76 | R"==((outer) * INNER_DIM_SIZE *REDUCTION_END_SIZE \ )==" "\n" |
77 | R"==(+ (reduction_chunk)*INNER_DIM_SIZE + inner )==" "\n" |
78 | R"==(KERNEL_ATTR )==" "\n" |
79 | R"==(__kernel void combined_reduce( )==" "\n" |
80 | R"==(__global SRC_DATA_T *src, __global DST_DATA_T *dst) { )==" "\n" |
81 | R"==(const int outer_idx = (get_global_id(0) / OUTER_DIM_STRIDE); )==" "\n" |
82 | R"==(const int reduction_chunk )==" "\n" |
83 | R"==(= (get_global_id(0) / PADDED_INNER_DIM_SIZE) % REDUCTION_CHUNK_SIZE; )==" "\n" |
84 | R"==(const int inner = (get_global_id(0) % PADDED_INNER_DIM_SIZE); )==" "\n" |
85 | R"==(if (outer_idx >= OUTER_DIM_SIZE) return; )==" "\n" |
86 | R"==(const int inner_idx = inner % INNER_DIM_SIZE; )==" "\n" |
87 | R"==(const int red_off = inner / INNER_DIM_SIZE; )==" "\n" |
88 | R"==(const int reduction_idx )==" "\n" |
89 | R"==(= reduction_chunk * REDUCTIONS_PER_WI * INNER_DIMS_PER_WI + red_off; )==" "\n" |
90 | R"==(const int reduction_idx_start = reduction_idx - red_off; )==" "\n" |
91 | R"==(const int inner_idx_start = inner - get_sub_group_local_id(); )==" "\n" |
92 | R"==(if (inner / INNER_DIMS_PER_WI >= INNER_DIM_SIZE) { return; } )==" "\n" |
93 | R"==(const int dst_off )==" "\n" |
94 | R"==(= _DST_OFF(outer_idx, reduction_chunk + red_off, inner_idx); )==" "\n" |
95 | R"==(DEF_ACC_DATA_T acc = INIT_ACC; )==" "\n" |
96 | R"==(for (int off = 0; off < REDUCTIONS_PER_WI )==" "\n" |
97 | R"==(&& off * INNER_DIMS_PER_WI + reduction_idx < REDUCTION_SIZE; )==" "\n" |
98 | R"==(off++) { )==" "\n" |
99 | R"==(#if WITH_BLOCK_READ )==" "\n" |
100 | R"==(const int src_off = _SRC_OFF(outer_idx, )==" "\n" |
101 | R"==(off * INNER_DIMS_PER_WI + reduction_idx_start, inner_idx_start); )==" "\n" |
102 | R"==(const SRC_DATA_T src_val = AS_DATA_T( )==" "\n" |
103 | R"==(BLOCK_READ((const __global BLOCK_DATA_T *)&src[src_off])); )==" "\n" |
104 | R"==(#else )==" "\n" |
105 | R"==(const int src_off = _SRC_OFF( )==" "\n" |
106 | R"==(outer_idx, off * INNER_DIMS_PER_WI + reduction_idx, inner_idx); )==" "\n" |
107 | R"==(const SRC_DATA_T src_val = src[src_off]; )==" "\n" |
108 | R"==(#endif )==" "\n" |
109 | R"==(const DEF_ACC_DATA_T prev = acc; )==" "\n" |
110 | R"==(acc = ACCUMULATE(acc, TO_DEF_ACC_DATA_T(src_val)); )==" "\n" |
111 | R"==(} )==" "\n" |
112 | R"==(for (int i = 1; i < INNER_DIMS_PER_WI; i++) { )==" "\n" |
113 | R"==(const DEF_ACC_DATA_T other )==" "\n" |
114 | R"==(= intel_sub_group_shuffle_down(acc, INIT_ACC, INNER_DIM_SIZE); )==" "\n" |
115 | R"==(if (get_sub_group_local_id() < INNER_DIM_SIZE) { )==" "\n" |
116 | R"==(acc = ACCUMULATE_FURTHER(acc, other); )==" "\n" |
117 | R"==(} else { )==" "\n" |
118 | R"==(acc = other; )==" "\n" |
119 | R"==(} )==" "\n" |
120 | R"==(} )==" "\n" |
121 | R"==(if (get_sub_group_local_id() < INNER_DIM_SIZE) { )==" "\n" |
122 | R"==(#if IS_FINAL )==" "\n" |
123 | R"==(float res = convert_float(acc); )==" "\n" |
124 | R"==(res = FINALIZE(res); )==" "\n" |
125 | R"==(dst[dst_off] = TO_DST(res); )==" "\n" |
126 | R"==(#else )==" "\n" |
127 | R"==(dst[dst_off] = acc; )==" "\n" |
128 | R"==(#endif )==" "\n" |
129 | R"==(} )==" "\n" |
130 | R"==(} )==" "\n" |
131 | R"==()==" ; |
132 | } |
133 | } |
134 | } |
135 | } |