1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_pooling_kernel = R"==(/******************************************************************************* )==" "\n" |
6 | R"==(* Copyright 2019-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 DST_DT_F32 )==" "\n" |
23 | R"==(#define DST_DATA_MIN -FLT_MAX )==" "\n" |
24 | R"==(#elif DST_DT_S8 )==" "\n" |
25 | R"==(#define DST_DATA_MIN CHAR_MIN )==" "\n" |
26 | R"==(#elif DST_DT_U8 )==" "\n" |
27 | R"==(#define DST_DATA_MIN 0 )==" "\n" |
28 | R"==(#elif DST_DT_F16 )==" "\n" |
29 | R"==(#define DST_DATA_MIN -HALF_MAX )==" "\n" |
30 | R"==(#else )==" "\n" |
31 | R"==(#define DST_DATA_MIN DATA_MIN )==" "\n" |
32 | R"==(#endif )==" "\n" |
33 | R"==(#if IS_FWD )==" "\n" |
34 | R"==(KERNEL_ATTR )==" "\n" |
35 | R"==(__kernel void ref_pooling_fwd(__global DATA_T *src, __global int *ws, )==" "\n" |
36 | R"==(__global DST_DATA_T *dst POST_OP_ARGS) { )==" "\n" |
37 | R"==(const int mb = GWS_GET_MB(); )==" "\n" |
38 | R"==(const int oc = GWS_GET_OC(); )==" "\n" |
39 | R"==(const int od = GWS_GET_OD(); )==" "\n" |
40 | R"==(const int oh = GWS_GET_OH(); )==" "\n" |
41 | R"==(const int ow = GWS_GET_OW(); )==" "\n" |
42 | R"==(if (mb >= SRC_D0 || oc >= SRC_D1) { )==" "\n" |
43 | R"==(const uint dst_off = DST_OFF(mb, oc, od, oh, ow); )==" "\n" |
44 | R"==(dst[dst_off] = TO_DST(0.f); )==" "\n" |
45 | R"==(#if ALG_MAX && IS_TRAINING )==" "\n" |
46 | R"==(ws[dst_off] = 0; )==" "\n" |
47 | R"==(#endif )==" "\n" |
48 | R"==(return; )==" "\n" |
49 | R"==(} )==" "\n" |
50 | R"==(const uint dst_off = DST_OFF(mb, oc, od, oh, ow); )==" "\n" |
51 | R"==(#if ALG_MAX && IS_TRAINING )==" "\n" |
52 | R"==(ws[dst_off] = -1; )==" "\n" |
53 | R"==(#endif )==" "\n" |
54 | R"==(#if ALG_AVG_P || ALG_AVG_NP )==" "\n" |
55 | R"==(float d = 0; )==" "\n" |
56 | R"==(#endif )==" "\n" |
57 | R"==(#if ALG_MAX && DT_BF16 )==" "\n" |
58 | R"==(DEF_ACC_DATA_T d = TO_DEF_ACC_DATA_T(DST_DATA_MIN); )==" "\n" |
59 | R"==(#elif ALG_MAX )==" "\n" |
60 | R"==(float d = DST_DATA_MIN; )==" "\n" |
61 | R"==(#endif )==" "\n" |
62 | R"==(for (int kd = 0; kd < KD; ++kd) { )==" "\n" |
63 | R"==(const int id = od * SD - PD + kd * (DD + 1); )==" "\n" |
64 | R"==(if (id < 0 || id >= ID) continue; )==" "\n" |
65 | R"==(for (int kh = 0; kh < KH; ++kh) { )==" "\n" |
66 | R"==(const int ih = oh * SH - PH + kh * (DH + 1); )==" "\n" |
67 | R"==(if (ih < 0 || ih >= IH) continue; )==" "\n" |
68 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
69 | R"==(const int iw = ow * SW - PW + kw * (DW + 1); )==" "\n" |
70 | R"==(if (iw < 0 || iw >= IW) continue; )==" "\n" |
71 | R"==(int src_off = SRC_OFF(mb, oc, id, ih, iw); )==" "\n" |
72 | R"==(#if ALG_MAX )==" "\n" |
73 | R"==(#if IS_TRAINING )==" "\n" |
74 | R"==(if (ws[dst_off] < 0) ws[dst_off] = kd * KH * KW + kh * KW + kw; )==" "\n" |
75 | R"==(#endif )==" "\n" |
76 | R"==(#if DT_BF16 )==" "\n" |
77 | R"==(DEF_ACC_DATA_T s = DATA_TO_REF(src[src_off]); )==" "\n" |
78 | R"==(#else )==" "\n" |
79 | R"==(float s = DATA_TO_REF(src[src_off]); )==" "\n" |
80 | R"==(#endif )==" "\n" |
81 | R"==(if (s > d) { )==" "\n" |
82 | R"==(d = s; )==" "\n" |
83 | R"==(#if IS_TRAINING )==" "\n" |
84 | R"==(ws[dst_off] = kd * KH * KW + kh * KW + kw; )==" "\n" |
85 | R"==(#endif )==" "\n" |
86 | R"==(} )==" "\n" |
87 | R"==(#else )==" "\n" |
88 | R"==(d += DATA_TO_REF(src[src_off]); )==" "\n" |
89 | R"==(#endif )==" "\n" |
90 | R"==(} )==" "\n" |
91 | R"==(} )==" "\n" |
92 | R"==(} )==" "\n" |
93 | R"==(#if ALG_MAX )==" "\n" |
94 | R"==(#if IS_TRAINING )==" "\n" |
95 | R"==(if (ws[dst_off] < 0) ws[dst_off] = 0; )==" "\n" |
96 | R"==(#endif )==" "\n" |
97 | R"==(#else )==" "\n" |
98 | R"==(#if ALG_AVG_P )==" "\n" |
99 | R"==(const int num_summands = KD * KW * KH; )==" "\n" |
100 | R"==(#else )==" "\n" |
101 | R"==(const int id_start = od * SD - PD; )==" "\n" |
102 | R"==(const int ih_start = oh * SH - PH; )==" "\n" |
103 | R"==(const int iw_start = ow * SW - PW; )==" "\n" |
104 | R"==(const int id_end = od * SD - PD + (KD - 1) * DD + KD; )==" "\n" |
105 | R"==(const int ih_end = oh * SH - PH + (KH - 1) * DH + KH; )==" "\n" |
106 | R"==(const int iw_end = ow * SW - PW + (KW - 1) * DW + KW; )==" "\n" |
107 | R"==(const int id_start_excluded )==" "\n" |
108 | R"==(= id_start < 0 ? (0 - id_start - 1) / (DD + 1) + 1 : 0; )==" "\n" |
109 | R"==(const int ih_start_excluded )==" "\n" |
110 | R"==(= ih_start < 0 ? (0 - ih_start - 1) / (DH + 1) + 1 : 0; )==" "\n" |
111 | R"==(const int iw_start_excluded )==" "\n" |
112 | R"==(= iw_start < 0 ? (0 - iw_start - 1) / (DW + 1) + 1 : 0; )==" "\n" |
113 | R"==(const int id_end_excluded )==" "\n" |
114 | R"==(= id_end > ID ? (id_end - ID - 1) / (DD + 1) + 1 : 0; )==" "\n" |
115 | R"==(const int ih_end_excluded )==" "\n" |
116 | R"==(= ih_end > IH ? (ih_end - IH - 1) / (DH + 1) + 1 : 0; )==" "\n" |
117 | R"==(const int iw_end_excluded )==" "\n" |
118 | R"==(= iw_end > IW ? (iw_end - IW - 1) / (DW + 1) + 1 : 0; )==" "\n" |
119 | R"==(const int num_summands = (KD - id_start_excluded - id_end_excluded) )==" "\n" |
120 | R"==(* (KH - ih_start_excluded - ih_end_excluded) )==" "\n" |
121 | R"==(* (KW - iw_start_excluded - iw_end_excluded); )==" "\n" |
122 | R"==(#endif )==" "\n" |
123 | R"==(d /= num_summands; )==" "\n" |
124 | R"==(#endif )==" "\n" |
125 | R"==(#if DT_BF16 )==" "\n" |
126 | R"==(POST_OP_DATA_T tmp = d; )==" "\n" |
127 | R"==(#else )==" "\n" |
128 | R"==(POST_OP_DATA_T tmp = DATA_TO_REF(d); )==" "\n" |
129 | R"==(#endif )==" "\n" |
130 | R"==(POST_OP_DATA_T sum_src; )==" "\n" |
131 | R"==(#if WITH_SUM )==" "\n" |
132 | R"==(sum_src = DATA_TO_REF(dst[dst_off]); )==" "\n" |
133 | R"==(#endif )==" "\n" |
134 | R"==(#if NDIMS == 3 )==" "\n" |
135 | R"==(const unsigned po_d2 = ow; )==" "\n" |
136 | R"==(const unsigned po_d3 = 0; )==" "\n" |
137 | R"==(const unsigned po_d4 = 0; )==" "\n" |
138 | R"==(#elif NDIMS == 4 )==" "\n" |
139 | R"==(const unsigned po_d2 = oh; )==" "\n" |
140 | R"==(const unsigned po_d3 = ow; )==" "\n" |
141 | R"==(const unsigned po_d4 = 0; )==" "\n" |
142 | R"==(#elif NDIMS == 5 )==" "\n" |
143 | R"==(const unsigned po_d2 = od; )==" "\n" |
144 | R"==(const unsigned po_d3 = oh; )==" "\n" |
145 | R"==(const unsigned po_d4 = ow; )==" "\n" |
146 | R"==(#else )==" "\n" |
147 | R"==(const unsigned po_d2 = 0; )==" "\n" |
148 | R"==(const unsigned po_d3 = 0; )==" "\n" |
149 | R"==(const unsigned po_d4 = 0; )==" "\n" |
150 | R"==(#endif )==" "\n" |
151 | R"==(APPLY_POST_OPS_SERIAL(tmp, POST_OP_DATA_T, sum_src, POST_OP_DATA_T, mb, 1, )==" "\n" |
152 | R"==(oc, 1, po_d2, 1, po_d3, 1, po_d4, 1, 0, 1); )==" "\n" |
153 | R"==(dst[dst_off] = TO_DST(tmp); )==" "\n" |
154 | R"==(} )==" "\n" |
155 | R"==(#endif )==" "\n" |
156 | R"==(#if IS_BWD )==" "\n" |
157 | R"==(KERNEL_ATTR )==" "\n" |
158 | R"==(__kernel void ref_pooling_bwd(__global DATA_T *diff_src, __global int *ws, )==" "\n" |
159 | R"==(__global DST_DATA_T *diff_dst) { )==" "\n" |
160 | R"==(const int mb = GWS_GET_MB(); )==" "\n" |
161 | R"==(const int oc = GWS_GET_OC(); )==" "\n" |
162 | R"==(const int id = GWS_GET_ID(); )==" "\n" |
163 | R"==(const int ih = GWS_GET_IH(); )==" "\n" |
164 | R"==(const int iw = GWS_GET_IW(); )==" "\n" |
165 | R"==(float s = 0; )==" "\n" |
166 | R"==(for (int kd = 0; kd < KD; ++kd) { )==" "\n" |
167 | R"==(int _od = id + PD - kd * (DD + 1); )==" "\n" |
168 | R"==(if (_od % SD != 0) continue; )==" "\n" |
169 | R"==(int od = _od / SD; )==" "\n" |
170 | R"==(if (od < 0 || od >= OD) continue; )==" "\n" |
171 | R"==(for (int kh = 0; kh < KH; ++kh) { )==" "\n" |
172 | R"==(int _oh = ih + PH - kh * (DH + 1); )==" "\n" |
173 | R"==(if (_oh % SH != 0) continue; )==" "\n" |
174 | R"==(int oh = _oh / SH; )==" "\n" |
175 | R"==(if (oh < 0 || oh >= OH) continue; )==" "\n" |
176 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
177 | R"==(int _ow = iw + PW - kw * (DW + 1); )==" "\n" |
178 | R"==(if (_ow % SW != 0) continue; )==" "\n" |
179 | R"==(int ow = _ow / SW; )==" "\n" |
180 | R"==(if (ow < 0 || ow >= OW) continue; )==" "\n" |
181 | R"==(const uint dst_off = DST_OFF(mb, oc, od, oh, ow); )==" "\n" |
182 | R"==(#if ALG_MAX )==" "\n" |
183 | R"==(const int index = ws[dst_off]; )==" "\n" |
184 | R"==(const int hw = index % (KW * KH); )==" "\n" |
185 | R"==(const int w_kd = index / (KW * KH); )==" "\n" |
186 | R"==(const int w_kw = hw % KW; )==" "\n" |
187 | R"==(const int w_kh = hw / KW; )==" "\n" |
188 | R"==(if (w_kd != kd || w_kh != kh || w_kw != kw) continue; )==" "\n" |
189 | R"==(#endif )==" "\n" |
190 | R"==(#if ALG_MAX )==" "\n" |
191 | R"==(const int denom = 1; )==" "\n" |
192 | R"==(#elif ALG_AVG_P )==" "\n" |
193 | R"==(const int denom = KD * KH * KW; )==" "\n" |
194 | R"==(#elif ALG_AVG_NP )==" "\n" |
195 | R"==(const int id_start = od * SD - PD; )==" "\n" |
196 | R"==(const int ih_start = oh * SH - PH; )==" "\n" |
197 | R"==(const int iw_start = ow * SW - PW; )==" "\n" |
198 | R"==(const int id_end = od * SD - PD + (KD - 1) * DD + KD; )==" "\n" |
199 | R"==(const int ih_end = oh * SH - PH + (KH - 1) * DH + KH; )==" "\n" |
200 | R"==(const int iw_end = ow * SW - PW + (KW - 1) * DW + KW; )==" "\n" |
201 | R"==(const int id_start_excluded )==" "\n" |
202 | R"==(= id_start < 0 ? (0 - id_start - 1) / (DD + 1) + 1 : 0; )==" "\n" |
203 | R"==(const int ih_start_excluded )==" "\n" |
204 | R"==(= ih_start < 0 ? (0 - ih_start - 1) / (DH + 1) + 1 : 0; )==" "\n" |
205 | R"==(const int iw_start_excluded )==" "\n" |
206 | R"==(= iw_start < 0 ? (0 - iw_start - 1) / (DW + 1) + 1 : 0; )==" "\n" |
207 | R"==(const int id_end_excluded )==" "\n" |
208 | R"==(= id_end > ID ? (id_end - ID - 1) / (DD + 1) + 1 : 0; )==" "\n" |
209 | R"==(const int ih_end_excluded )==" "\n" |
210 | R"==(= ih_end > IH ? (ih_end - IH - 1) / (DH + 1) + 1 : 0; )==" "\n" |
211 | R"==(const int iw_end_excluded )==" "\n" |
212 | R"==(= iw_end > IW ? (iw_end - IW - 1) / (DW + 1) + 1 : 0; )==" "\n" |
213 | R"==(const int denom = (KD - id_start_excluded - id_end_excluded) )==" "\n" |
214 | R"==(* (KH - ih_start_excluded - ih_end_excluded) )==" "\n" |
215 | R"==(* (KW - iw_start_excluded - iw_end_excluded); )==" "\n" |
216 | R"==(#endif )==" "\n" |
217 | R"==(s += DATA_TO_REF(diff_dst[dst_off]) / denom; )==" "\n" |
218 | R"==(} )==" "\n" |
219 | R"==(} )==" "\n" |
220 | R"==(} )==" "\n" |
221 | R"==(uint diff_src_offset = SRC_OFF(mb, oc, id, ih, iw); )==" "\n" |
222 | R"==(diff_src[diff_src_offset] = CONVERT_DATA_T(s); )==" "\n" |
223 | R"==(} )==" "\n" |
224 | R"==(#endif )==" "\n" |
225 | R"==()==" ; |
226 | } |
227 | } |
228 | } |
229 | } |