1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *gen9_conv_nhwc_fwd_data_kernel = R"==(/******************************************************************************* )==" "\n" |
6 | R"==(* Copyright 2020-2021 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"==(#define _BLOCK_READ8(ptr) \ )==" "\n" |
23 | R"==(AS_DATA8_T(BLOCK_READ8((const __global BLOCK_DATA_T *)(ptr))) )==" "\n" |
24 | R"==(#define _BLOCK_READ4(ptr) \ )==" "\n" |
25 | R"==(AS_DATA4_T(BLOCK_READ4((const __global BLOCK_DATA_T *)(ptr))) )==" "\n" |
26 | R"==(#define _BLOCK_READ2(ptr) \ )==" "\n" |
27 | R"==(AS_DATA2_T(BLOCK_READ2((const __global BLOCK_DATA_T *)(ptr))) )==" "\n" |
28 | R"==(#define _BLOCK_READ(ptr) \ )==" "\n" |
29 | R"==(AS_DATA_T(BLOCK_READ((const __global BLOCK_DATA_T *)(ptr))) )==" "\n" |
30 | R"==(#define _BLOCK_WRITE8(ptr, v) \ )==" "\n" |
31 | R"==(BLOCK_WRITE8((__global BLOCK_DATA_T *)(ptr), AS_BLOCK_DATA8_T(v)) )==" "\n" |
32 | R"==(#define _BLOCK_WRITE4(ptr, v) \ )==" "\n" |
33 | R"==(BLOCK_WRITE4((__global BLOCK_DATA_T *)(ptr), AS_BLOCK_DATA4_T(v)) )==" "\n" |
34 | R"==(#define _BLOCK_WRITE2(ptr, v) \ )==" "\n" |
35 | R"==(BLOCK_WRITE2((__global BLOCK_DATA_T *)(ptr), AS_BLOCK_DATA2_T(v)) )==" "\n" |
36 | R"==(#define _BLOCK_WRITE(ptr, v) \ )==" "\n" |
37 | R"==(BLOCK_WRITE((__global BLOCK_DATA_T *)(ptr), AS_BLOCK_DATA_T(v)) )==" "\n" |
38 | R"==(#define ENABLE_KW_BUF (KW >= 5) )==" "\n" |
39 | R"==(#define IS_3D (OD > 1) )==" "\n" |
40 | R"==(#define KDHW_SIZE (KD * KH * KW) )==" "\n" |
41 | R"==(#define HAS_PAD_D (PD > 0 || OD * SD - PD + (KD - 1) * (1 + DD) >= ID) )==" "\n" |
42 | R"==(#define HAS_PAD_H (PH > 0 || OH * SH - PH + (KH - 1) * (1 + DH) >= IH) )==" "\n" |
43 | R"==(#define HAS_PAD_W (PW > 0 || OW * SW - PW + (KW - 1) * (1 + DW) >= IW) )==" "\n" |
44 | R"==(#define OC_PAD_BLOCK (OC % OC_BLOCK ? (OC / OC_BLOCK + 1) * OC_BLOCK : OC) )==" "\n" |
45 | R"==(#if DT_F32 )==" "\n" |
46 | R"==(#define BLOCK_READ_BOUND 1 )==" "\n" |
47 | R"==(#define BLOCK_WRITE_BOUND 4 )==" "\n" |
48 | R"==(#elif DT_F16 )==" "\n" |
49 | R"==(#define BLOCK_READ_BOUND 2 )==" "\n" |
50 | R"==(#define BLOCK_WRITE_BOUND 8 )==" "\n" |
51 | R"==(#else )==" "\n" |
52 | R"==(#error "Wrong Data Type" )==" "\n" |
53 | R"==(#endif )==" "\n" |
54 | R"==(inline DATA_T read_ic_block(const __global DATA_T *ptr, int off) { )==" "\n" |
55 | R"==(const int local_id = get_local_id(0); )==" "\n" |
56 | R"==(#if IC == 3 )==" "\n" |
57 | R"==(return (local_id < IC) ? *ptr : 0; )==" "\n" |
58 | R"==(#else )==" "\n" |
59 | R"==(#if (IS_DW ? G_WO_PADDING : IC_WO_PADDING) % IC_BLOCK != 0 )==" "\n" |
60 | R"==(int tail = (IS_DW ? G_WO_PADDING : IC_WO_PADDING) - off; )==" "\n" |
61 | R"==(if (tail < IC_BLOCK) { return (local_id < tail) ? ptr[local_id] : 0; } )==" "\n" |
62 | R"==(#endif )==" "\n" |
63 | R"==(#if (IS_DW ? G_WO_PADDING : IC_WO_PADDING) % BLOCK_READ_BOUND != 0 )==" "\n" |
64 | R"==(return ptr[local_id]; )==" "\n" |
65 | R"==(#else )==" "\n" |
66 | R"==(return _BLOCK_READ(ptr); )==" "\n" |
67 | R"==(#endif )==" "\n" |
68 | R"==(#endif )==" "\n" |
69 | R"==(} )==" "\n" |
70 | R"==(inline DATA_T read_oc_block(const __global DATA_T *ptr, int off) { )==" "\n" |
71 | R"==(const int local_id = get_local_id(0); )==" "\n" |
72 | R"==(#if (IS_DW ? G_WO_PADDING : OC_WO_PADDING) % OC_BLOCK != 0 )==" "\n" |
73 | R"==(int tail = (IS_DW ? G_WO_PADDING : OC_WO_PADDING) - off; )==" "\n" |
74 | R"==(if (tail < OC_BLOCK) { return (local_id < tail) ? ptr[local_id] : 0; } )==" "\n" |
75 | R"==(#endif )==" "\n" |
76 | R"==(#if (IS_DW ? G_WO_PADDING : OC_WO_PADDING) % BLOCK_READ_BOUND != 0 )==" "\n" |
77 | R"==(return ptr[local_id]; )==" "\n" |
78 | R"==(#else )==" "\n" |
79 | R"==(return _BLOCK_READ(ptr); )==" "\n" |
80 | R"==(#endif )==" "\n" |
81 | R"==(} )==" "\n" |
82 | R"==(inline void write_oc_block(__global DATA_T *ptr, int off, DATA_T value) { )==" "\n" |
83 | R"==(const int local_id = get_local_id(0); )==" "\n" |
84 | R"==(#if (IS_DW ? G_WO_PADDING : OC_WO_PADDING) % OC_BLOCK != 0 )==" "\n" |
85 | R"==(int tail = (IS_DW ? G_WO_PADDING : OC_WO_PADDING) - off; )==" "\n" |
86 | R"==(if (tail < OC_BLOCK) { )==" "\n" |
87 | R"==(if (local_id < tail) ptr[local_id] = value; )==" "\n" |
88 | R"==(return; )==" "\n" |
89 | R"==(} )==" "\n" |
90 | R"==(#endif )==" "\n" |
91 | R"==(#if (IS_DW ? G_WO_PADDING : OC_WO_PADDING) % BLOCK_WRITE_BOUND != 0 )==" "\n" |
92 | R"==(ptr[local_id] = value; )==" "\n" |
93 | R"==(return; )==" "\n" |
94 | R"==(#else )==" "\n" |
95 | R"==(return _BLOCK_WRITE(ptr, value); )==" "\n" |
96 | R"==(#endif )==" "\n" |
97 | R"==(} )==" "\n" |
98 | R"==(void multiply_blocks_8x8_ic3(DATA_T *res, DATA_T blockA, const DATA_T *blockB) { )==" "\n" |
99 | R"==(*res = fma(blockB[0], intel_sub_group_shuffle(blockA, 0), *res); )==" "\n" |
100 | R"==(*res = fma(blockB[1], intel_sub_group_shuffle(blockA, 1), *res); )==" "\n" |
101 | R"==(*res = fma(blockB[2], intel_sub_group_shuffle(blockA, 2), *res); )==" "\n" |
102 | R"==(} )==" "\n" |
103 | R"==(void multiply_blocks_8x8( )==" "\n" |
104 | R"==(DATA_T *res, DATA_T blockA, DATA8_T blockB0, DATA8_T blockB1) { )==" "\n" |
105 | R"==(for (int i = 0; i < 8; i++) { )==" "\n" |
106 | R"==(*res = fma(blockB0[i], intel_sub_group_shuffle(blockA, i), *res); )==" "\n" |
107 | R"==(} )==" "\n" |
108 | R"==(for (int i = 0; i < 8; i++) { )==" "\n" |
109 | R"==(*res = fma(blockB1[i], intel_sub_group_shuffle(blockA, 8 + i), *res); )==" "\n" |
110 | R"==(} )==" "\n" |
111 | R"==(} )==" "\n" |
112 | R"==(__attribute__((reqd_work_group_size(LWS_0, LWS_1, LWS_2))) )==" "\n" |
113 | R"==(__attribute__((intel_reqd_sub_group_size(SUB_GROUP_SIZE))) __kernel void )==" "\n" |
114 | R"==(gen9_conv_nhwc_fwd(const __global DATA_T *src, const __global DATA_T *wei, )==" "\n" |
115 | R"==(const __global DATA_T *bias, __global DATA_T *dst POST_OP_ARGS) { )==" "\n" |
116 | R"==(MAYBE_SKIP_NON_UNIFORM_WG(); )==" "\n" |
117 | R"==(const int sp = get_group_id(1); )==" "\n" |
118 | R"==(const int local_id = get_sub_group_local_id(); )==" "\n" |
119 | R"==(const int ocb_mb = get_group_id(2); )==" "\n" |
120 | R"==(const int ocb = ocb_mb / (MB); )==" "\n" |
121 | R"==(const int mb = ocb_mb % (MB); )==" "\n" |
122 | R"==(#if IS_DW )==" "\n" |
123 | R"==(const int oc = get_group_id(0); )==" "\n" |
124 | R"==(const int g = 0; )==" "\n" |
125 | R"==(const int goc = oc; )==" "\n" |
126 | R"==(#else )==" "\n" |
127 | R"==(const int oc = (ocb * OCB) / OC_BLOCK + get_group_id(0); )==" "\n" |
128 | R"==(const int g = oc / (OC_PAD_BLOCK / OC_BLOCK); )==" "\n" |
129 | R"==(const int goc = oc % (OC_PAD_BLOCK / OC_BLOCK); )==" "\n" |
130 | R"==(#endif )==" "\n" |
131 | R"==(const int od = IS_3D ? sp / (OWB * OHB) : 0; )==" "\n" |
132 | R"==(const int ohw = IS_3D ? sp % (OWB * OHB) : sp; )==" "\n" |
133 | R"==(const int id = IS_3D ? od * SD - PD : 0; )==" "\n" |
134 | R"==(const int oh = (ohw / OWB) * OH_BLOCK; )==" "\n" |
135 | R"==(const int ow = (ohw % OWB) * OW_BLOCK; )==" "\n" |
136 | R"==(DATA_T blockC00[OW_BLOCK] = {0}; )==" "\n" |
137 | R"==(if (WITH_BIAS) { )==" "\n" |
138 | R"==(const int bc_off = oc * OC_BLOCK + local_id; )==" "\n" |
139 | R"==(#if IS_DW )==" "\n" |
140 | R"==(DATA_T b = (G_WO_PADDING % OC_BLOCK == 0 || bc_off < G_WO_PADDING) )==" "\n" |
141 | R"==(#else )==" "\n" |
142 | R"==(DATA_T b = (OC_WO_PADDING % OC_BLOCK == 0 || bc_off < OC_WO_PADDING) )==" "\n" |
143 | R"==(#endif )==" "\n" |
144 | R"==(? bias[bc_off] )==" "\n" |
145 | R"==(: DATA_ZERO; )==" "\n" |
146 | R"==(unroll_for(int i = 0; i < OW_BLOCK; i++) { blockC00[i] = b; } )==" "\n" |
147 | R"==(} )==" "\n" |
148 | R"==(int ih = oh * SH - PH; )==" "\n" |
149 | R"==(int iw = ow * SW - PW; )==" "\n" |
150 | R"==(src += mb * ID * IH * IW * G * IC_WO_PADDING; )==" "\n" |
151 | R"==(src += (id * IH * IW + ih * IW + iw) * G * IC_WO_PADDING; )==" "\n" |
152 | R"==(src += g * IC_WO_PADDING; )==" "\n" |
153 | R"==(src += (IS_DW ? oc * OC_BLOCK : 0); )==" "\n" |
154 | R"==(wei += goc * KDHW_SIZE * OC_BLOCK * IC + g * IC * OC_PAD_BLOCK * KDHW_SIZE; )==" "\n" |
155 | R"==(#if (KD == 1 && KH == 1) && (HAS_PAD_D || HAS_PAD_H) )==" "\n" |
156 | R"==(const bool dh_out_of_range = (id < 0 || id >= ID || ih < 0 || ih >= IH); )==" "\n" |
157 | R"==(#else )==" "\n" |
158 | R"==(const bool dh_out_of_range = false; )==" "\n" |
159 | R"==(#endif )==" "\n" |
160 | R"==(#if IS_DW )==" "\n" |
161 | R"==(const int icb_min = goc * OC_BLOCK; )==" "\n" |
162 | R"==(const int icb_max = icb_min + OC_BLOCK; )==" "\n" |
163 | R"==(#else )==" "\n" |
164 | R"==(const int icb_min = 0; )==" "\n" |
165 | R"==(const int icb_max = dh_out_of_range ? 0 : (IC == 3 ? 1 : IC); )==" "\n" |
166 | R"==(#endif )==" "\n" |
167 | R"==(for (int icb = icb_min; icb < icb_max; icb += IC_BLOCK) { )==" "\n" |
168 | R"==(__attribute__((opencl_unroll_hint(1))) )==" "\n" |
169 | R"==(for (int kd = 0; kd < KD; ++kd) { )==" "\n" |
170 | R"==(#if HAS_PAD_D )==" "\n" |
171 | R"==(if (id + kd * (1 + DD) < 0 || id + kd * (1 + DD) >= ID) continue; )==" "\n" |
172 | R"==(#endif )==" "\n" |
173 | R"==(__attribute__((opencl_unroll_hint(1))) )==" "\n" |
174 | R"==(for (int kh = 0; kh < KH; ++kh) { )==" "\n" |
175 | R"==(#if HAS_PAD_H )==" "\n" |
176 | R"==(if (ih + kh * (1 + DH) < 0 || ih + kh * (1 + DH) >= IH) )==" "\n" |
177 | R"==(continue; )==" "\n" |
178 | R"==(#endif )==" "\n" |
179 | R"==(const __global DATA_T *src1 = src )==" "\n" |
180 | R"==(+ kd * (1 + DD) * IH * IW * G * IC_WO_PADDING )==" "\n" |
181 | R"==(+ kh * (1 + DH) * IW * G * IC_WO_PADDING; )==" "\n" |
182 | R"==(if (IC == 3) src1 += local_id; )==" "\n" |
183 | R"==(#if ENABLE_KW_BUF )==" "\n" |
184 | R"==(DATA_T tempA[SW * OW_BLOCK + KW * (1 + DW)] = {0}; )==" "\n" |
185 | R"==(__attribute__((opencl_unroll_hint( )==" "\n" |
186 | R"==(SW * OW_BLOCK + KW * (1 + DW)))) )==" "\n" |
187 | R"==(for (int i = 0; i < SW * OW_BLOCK + KW * (1 + DW); i++) { )==" "\n" |
188 | R"==(if ((i + iw) >= 0 && (i + iw) < IW) { )==" "\n" |
189 | R"==(tempA[i] = read_ic_block( )==" "\n" |
190 | R"==(&src1[i * G * IC_WO_PADDING], icb); )==" "\n" |
191 | R"==(} )==" "\n" |
192 | R"==(} )==" "\n" |
193 | R"==(#endif )==" "\n" |
194 | R"==(__attribute__((opencl_unroll_hint(KW))) )==" "\n" |
195 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
196 | R"==(#if IC == 3 )==" "\n" |
197 | R"==(const __global DATA_T *wei1 = wei )==" "\n" |
198 | R"==(+ (kd * KH * KW + kh * KW + kw) * IC * OC_BLOCK; )==" "\n" |
199 | R"==(#elif IS_DW )==" "\n" |
200 | R"==(const __global DATA_T *wei1 )==" "\n" |
201 | R"==(= wei + (kd * KH * KW + kh * KW + kw) * OC_BLOCK; )==" "\n" |
202 | R"==(#else )==" "\n" |
203 | R"==(const __global DATA_T *wei1 = wei )==" "\n" |
204 | R"==(+ (kd * KH * KW + kh * KW + kw) * IC_BLOCK )==" "\n" |
205 | R"==(* OC_BLOCK; )==" "\n" |
206 | R"==(#endif )==" "\n" |
207 | R"==(DATA_T blockA[OW_BLOCK] = {0}; )==" "\n" |
208 | R"==(#if ENABLE_KW_BUF )==" "\n" |
209 | R"==(__attribute__(( )==" "\n" |
210 | R"==(opencl_unroll_hint(OW_BLOCK))) )==" "\n" |
211 | R"==(for (int i = 0; i < OW_BLOCK; i++) { )==" "\n" |
212 | R"==(blockA[i] = tempA[i * SW + kw * (1 + DW)]; )==" "\n" |
213 | R"==(} )==" "\n" |
214 | R"==(#else )==" "\n" |
215 | R"==(__attribute__(( )==" "\n" |
216 | R"==(opencl_unroll_hint(OW_BLOCK))) )==" "\n" |
217 | R"==(for (int i = 0; i < OW_BLOCK; i++) { )==" "\n" |
218 | R"==(int iw_off = i * SW + kw * (1 + DW); )==" "\n" |
219 | R"==(if (iw + iw_off >= 0 && iw + iw_off < IW) { )==" "\n" |
220 | R"==(blockA[i] = read_ic_block( )==" "\n" |
221 | R"==(&src1[iw_off * G * IC_WO_PADDING], icb); )==" "\n" |
222 | R"==(} )==" "\n" |
223 | R"==(} )==" "\n" |
224 | R"==(#endif )==" "\n" |
225 | R"==(#if IC == 3 )==" "\n" |
226 | R"==(DATA_T blockB[IC]; )==" "\n" |
227 | R"==(__attribute__((opencl_unroll_hint(IC))) )==" "\n" |
228 | R"==(for (int i = 0; i < IC; i++) { )==" "\n" |
229 | R"==(blockB[i] = _BLOCK_READ(wei1 + i * OC_BLOCK); )==" "\n" |
230 | R"==(} )==" "\n" |
231 | R"==(__attribute__(( )==" "\n" |
232 | R"==(opencl_unroll_hint(OW_BLOCK))) )==" "\n" |
233 | R"==(for (int i = 0; i < OW_BLOCK; i++) { )==" "\n" |
234 | R"==(multiply_blocks_8x8_ic3( )==" "\n" |
235 | R"==(&blockC00[i], blockA[i], blockB); )==" "\n" |
236 | R"==(} )==" "\n" |
237 | R"==(#elif IS_DW )==" "\n" |
238 | R"==(DATA_T blockB = _BLOCK_READ(wei1); )==" "\n" |
239 | R"==(for (int i = 0; i < OW_BLOCK; i++) { )==" "\n" |
240 | R"==(blockC00[i] = fma(blockA[i], blockB, blockC00[i]); )==" "\n" |
241 | R"==(} )==" "\n" |
242 | R"==(#else )==" "\n" |
243 | R"==(DATA8_T blockB00 = _BLOCK_READ8(wei1); )==" "\n" |
244 | R"==(DATA8_T blockB01 = _BLOCK_READ8(wei1 + 8 * OC_BLOCK); )==" "\n" |
245 | R"==(__attribute__(( )==" "\n" |
246 | R"==(opencl_unroll_hint(OW_BLOCK))) )==" "\n" |
247 | R"==(for (int i = 0; i < OW_BLOCK; i++) { )==" "\n" |
248 | R"==(multiply_blocks_8x8( )==" "\n" |
249 | R"==(&blockC00[i], blockA[i], blockB00, blockB01); )==" "\n" |
250 | R"==(} )==" "\n" |
251 | R"==(#endif )==" "\n" |
252 | R"==(} )==" "\n" |
253 | R"==(} )==" "\n" |
254 | R"==(} )==" "\n" |
255 | R"==(src += IC_BLOCK; )==" "\n" |
256 | R"==(wei += KDHW_SIZE * IC_BLOCK * OC_BLOCK; )==" "\n" |
257 | R"==(} )==" "\n" |
258 | R"==(__global DATA_T *dst_write0 = dst + mb * OD * OH * OW * G * OC_WO_PADDING; )==" "\n" |
259 | R"==(dst_write0 += (od * OH * OW + oh * OW + ow) * G * OC_WO_PADDING; )==" "\n" |
260 | R"==(dst_write0 += g * OC_WO_PADDING + goc * OC_BLOCK; )==" "\n" |
261 | R"==(DATA_T blockS00[OW_BLOCK]; )==" "\n" |
262 | R"==(#if WITH_SUM )==" "\n" |
263 | R"==(for (int i = 0; i < min(OW_BLOCK, OW - ow); i++) { )==" "\n" |
264 | R"==(blockS00[i] = read_oc_block( )==" "\n" |
265 | R"==(&dst_write0[i * G * OC_WO_PADDING], goc * OC_BLOCK); )==" "\n" |
266 | R"==(} )==" "\n" |
267 | R"==(#endif )==" "\n" |
268 | R"==(const int po_mb = (mb) % MB; )==" "\n" |
269 | R"==(const int po_oc = (oc * OC_BLOCK + local_id) % (OC * G); )==" "\n" |
270 | R"==(APPLY_POST_OPS_SERIAL_BINARY_2D( )==" "\n" |
271 | R"==(blockC00, DATA_T, blockS00, DATA_T, po_mb, 1, po_oc, 1); )==" "\n" |
272 | R"==(for (int i = 0; i < min(OW_BLOCK, OW - ow); i++) { )==" "\n" |
273 | R"==(write_oc_block(&dst_write0[i * G * OC_WO_PADDING], goc * OC_BLOCK, )==" "\n" |
274 | R"==(blockC00[i]); )==" "\n" |
275 | R"==(} )==" "\n" |
276 | R"==(} )==" "\n" |
277 | R"==()==" ; |
278 | } |
279 | } |
280 | } |
281 | } |