1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *gen9_conv_nhwc_bwd_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_types.h" )==" "\n" |
21 | R"==(#if ID > 1 )==" "\n" |
22 | R"==(#define CASE_3D 1 )==" "\n" |
23 | R"==(#else )==" "\n" |
24 | R"==(#define CASE_3D 0 )==" "\n" |
25 | R"==(#endif )==" "\n" |
26 | R"==(#define TRANSPOSE_1(_block, _col) \ )==" "\n" |
27 | R"==((DATA_T)(intel_sub_group_shuffle(_block, _col)) )==" "\n" |
28 | R"==(#define FMA1(a, b, c) fma((DATA_T)(a), (DATA_T)b, (DATA_T)c) )==" "\n" |
29 | R"==(#define MULTIPLY_BLOCKS_8x8(_result, _blockA, _blockB, _blockB1) \ )==" "\n" |
30 | R"==({ \ )==" "\n" |
31 | R"==(_result = FMA1(_blockB.s0, TRANSPOSE_1(_blockA, 0), _result); \ )==" "\n" |
32 | R"==(_result = FMA1(_blockB.s1, TRANSPOSE_1(_blockA, 1), _result); \ )==" "\n" |
33 | R"==(_result = FMA1(_blockB.s2, TRANSPOSE_1(_blockA, 2), _result); \ )==" "\n" |
34 | R"==(_result = FMA1(_blockB.s3, TRANSPOSE_1(_blockA, 3), _result); \ )==" "\n" |
35 | R"==(_result = FMA1(_blockB.s4, TRANSPOSE_1(_blockA, 4), _result); \ )==" "\n" |
36 | R"==(_result = FMA1(_blockB.s5, TRANSPOSE_1(_blockA, 5), _result); \ )==" "\n" |
37 | R"==(_result = FMA1(_blockB.s6, TRANSPOSE_1(_blockA, 6), _result); \ )==" "\n" |
38 | R"==(_result = FMA1(_blockB.s7, TRANSPOSE_1(_blockA, 7), _result); \ )==" "\n" |
39 | R"==(_result = FMA1(_blockB1.s0, TRANSPOSE_1(_blockA, 8), _result); \ )==" "\n" |
40 | R"==(_result = FMA1(_blockB1.s1, TRANSPOSE_1(_blockA, 9), _result); \ )==" "\n" |
41 | R"==(_result = FMA1(_blockB1.s2, TRANSPOSE_1(_blockA, 10), _result); \ )==" "\n" |
42 | R"==(_result = FMA1(_blockB1.s3, TRANSPOSE_1(_blockA, 11), _result); \ )==" "\n" |
43 | R"==(_result = FMA1(_blockB1.s4, TRANSPOSE_1(_blockA, 12), _result); \ )==" "\n" |
44 | R"==(_result = FMA1(_blockB1.s5, TRANSPOSE_1(_blockA, 13), _result); \ )==" "\n" |
45 | R"==(_result = FMA1(_blockB1.s6, TRANSPOSE_1(_blockA, 14), _result); \ )==" "\n" |
46 | R"==(_result = FMA1(_blockB1.s7, TRANSPOSE_1(_blockA, 15), _result); \ )==" "\n" |
47 | R"==(} )==" "\n" |
48 | R"==(inline DATA_T read_oc_block(const __global DATA_T *ptr, int off) { )==" "\n" |
49 | R"==(const int local_id = get_sub_group_local_id(); )==" "\n" |
50 | R"==(#if OC_WO_PADDING % OC_BLOCK != 0 )==" "\n" |
51 | R"==(int tail = OC_WO_PADDING - off; )==" "\n" |
52 | R"==(if (tail < OC_BLOCK) { )==" "\n" |
53 | R"==(return (local_id < tail) ? AS_DATA_T(ptr[local_id]) : DATA_ZERO; )==" "\n" |
54 | R"==(} )==" "\n" |
55 | R"==(#endif )==" "\n" |
56 | R"==(if ((OC_WO_PADDING * sizeof(DATA_T)) % 4 != 0) )==" "\n" |
57 | R"==(return ptr[local_id]; )==" "\n" |
58 | R"==(else )==" "\n" |
59 | R"==(return AS_DATA_T(BLOCK_READ((const __global BLOCK_DATA_T *)(ptr))); )==" "\n" |
60 | R"==(} )==" "\n" |
61 | R"==(inline void write_ic_block(__global DATA_T *ptr, int off, DATA_T value) { )==" "\n" |
62 | R"==(const int local_id = get_sub_group_local_id(); )==" "\n" |
63 | R"==(#if IC_WO_PADDING % IC_BLOCK != 0 )==" "\n" |
64 | R"==(int tail = IC_WO_PADDING - off; )==" "\n" |
65 | R"==(if (tail < IC_BLOCK) { )==" "\n" |
66 | R"==(if (local_id < tail) ptr[local_id] = value; )==" "\n" |
67 | R"==(return; )==" "\n" |
68 | R"==(} )==" "\n" |
69 | R"==(#endif )==" "\n" |
70 | R"==(if ((IC_WO_PADDING * sizeof(DATA_T)) % 16 != 0) )==" "\n" |
71 | R"==(ptr[local_id] = value; )==" "\n" |
72 | R"==(else )==" "\n" |
73 | R"==(BLOCK_WRITE((__global BLOCK_DATA_T *)ptr, AS_BLOCK_DATA_T(value)); )==" "\n" |
74 | R"==(return; )==" "\n" |
75 | R"==(} )==" "\n" |
76 | R"==(__attribute__((reqd_work_group_size(LWS_0, LWS_1, LWS_2))) )==" "\n" |
77 | R"==(__attribute__((intel_reqd_sub_group_size(SUB_GROUP_SIZE))) )==" "\n" |
78 | R"==(__kernel void )==" "\n" |
79 | R"==(gen9_conv_nhwc_bwd_data(__global DATA_T *diff_src, __global DATA_T *wei, )==" "\n" |
80 | R"==(__global DATA_T *diff_dst, __global DATA_T *bias) { )==" "\n" |
81 | R"==(MAYBE_SKIP_NON_UNIFORM_WG(); )==" "\n" |
82 | R"==(const int sp = get_group_id(1); )==" "\n" |
83 | R"==(const int local_id = get_sub_group_local_id(); )==" "\n" |
84 | R"==(const int icb_mb = get_group_id(2); )==" "\n" |
85 | R"==(const int mb = icb_mb / (G * IC_PADDED / ICB); )==" "\n" |
86 | R"==(const int icb = icb_mb % (G * IC_PADDED / ICB); )==" "\n" |
87 | R"==(const int ic = (icb * ICB) / IC_BLOCK + get_group_id(0); )==" "\n" |
88 | R"==(const int g = ic / (IC_PADDED / IC_BLOCK); )==" "\n" |
89 | R"==(const int gic = ic % (IC_PADDED / IC_BLOCK); )==" "\n" |
90 | R"==(#if CASE_3D )==" "\n" |
91 | R"==(const int id = sp / (IWB * IH); )==" "\n" |
92 | R"==(const int ihw = sp % (IWB * IH); )==" "\n" |
93 | R"==(#else )==" "\n" |
94 | R"==(const int id = 0; )==" "\n" |
95 | R"==(const int ihw = sp; )==" "\n" |
96 | R"==(#endif )==" "\n" |
97 | R"==(const int ih = ihw / IWB; )==" "\n" |
98 | R"==(const int iw = (ihw % IWB) * IW_BLOCK; )==" "\n" |
99 | R"==(diff_dst += mb * OC_WO_PADDING * G * OD * OH * OW + g * OC_WO_PADDING; )==" "\n" |
100 | R"==(DATA_T blockC00[IW_BLOCK] = {DATA_ZERO}; )==" "\n" |
101 | R"==(if (WITH_BIAS) { )==" "\n" |
102 | R"==(const int bg_off = g * IC; )==" "\n" |
103 | R"==(const int bc_off = gic * IC_BLOCK + local_id; )==" "\n" |
104 | R"==(DATA_T b = (IC_WO_PADDING % IC_BLOCK == 0 || bc_off < IC_WO_PADDING) )==" "\n" |
105 | R"==(? bias[bg_off + bc_off] )==" "\n" |
106 | R"==(: DATA_ZERO; )==" "\n" |
107 | R"==(unroll_for(int i = 0; i < IW_BLOCK; ++i) { blockC00[i] = b; } )==" "\n" |
108 | R"==(} )==" "\n" |
109 | R"==(wei += gic * KD * KH * KW * OC_BLOCK * IC_BLOCK )==" "\n" |
110 | R"==(+ g * IC_PADDED * OC_PADDED * KD * KH * KW; )==" "\n" |
111 | R"==(#if KH != 1 || KW != 1 || KD != 1 )==" "\n" |
112 | R"==(for (int kd = 0; kd < KD; ++kd) )==" "\n" |
113 | R"==(for (int kh = 0; kh < KH; ++kh) )==" "\n" |
114 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
115 | R"==(if (ih + PH < kh * (1 + DH)) continue; )==" "\n" |
116 | R"==(#if CASE_3D )==" "\n" |
117 | R"==(if (id + PD < kd * (1 + DD)) continue; )==" "\n" |
118 | R"==(int od = id - kd * (1 + DD) + PD; )==" "\n" |
119 | R"==(if (od % SD != 0) continue; )==" "\n" |
120 | R"==(od /= SD; )==" "\n" |
121 | R"==(if (od >= OD) continue; )==" "\n" |
122 | R"==(#endif )==" "\n" |
123 | R"==(int oh = ih - kh * (1 + DH) + PH; )==" "\n" |
124 | R"==(if (oh % SH != 0) continue; )==" "\n" |
125 | R"==(oh /= SH; )==" "\n" |
126 | R"==(if (oh >= OH) continue; )==" "\n" |
127 | R"==(const __global DATA_T *diff_dst1 )==" "\n" |
128 | R"==(= diff_dst + oh * OW * G * OC_WO_PADDING; )==" "\n" |
129 | R"==(#if CASE_3D )==" "\n" |
130 | R"==(diff_dst1 += od * OH * OW * G * OC_WO_PADDING; )==" "\n" |
131 | R"==(#endif )==" "\n" |
132 | R"==(const __global DATA_T *wei1 = wei )==" "\n" |
133 | R"==(#if CASE_3D )==" "\n" |
134 | R"==(+ kd * KH * KW * OC_BLOCK * IC_BLOCK )==" "\n" |
135 | R"==(#endif )==" "\n" |
136 | R"==(+ kh * KW * OC_BLOCK * IC_BLOCK )==" "\n" |
137 | R"==(+ kw * OC_BLOCK * IC_BLOCK; )==" "\n" |
138 | R"==(#else )==" "\n" |
139 | R"==(int oh = (ih + PH); )==" "\n" |
140 | R"==(#if CASE_3D )==" "\n" |
141 | R"==(int od = (id + PD); )==" "\n" |
142 | R"==(#endif )==" "\n" |
143 | R"==(bool do_ker = true; )==" "\n" |
144 | R"==(#if SW != 1 || SH != 1 || SD != 1 )==" "\n" |
145 | R"==(do_ker = oh % SH == 0; )==" "\n" |
146 | R"==(oh /= SH; )==" "\n" |
147 | R"==(#if CASE_3D )==" "\n" |
148 | R"==(do_ker = do_ker && od % SD == 0; )==" "\n" |
149 | R"==(od /= SD; )==" "\n" |
150 | R"==(#endif )==" "\n" |
151 | R"==(#endif )==" "\n" |
152 | R"==(#if PH != 0 || PW != 0 || PD != 0 )==" "\n" |
153 | R"==(do_ker = do_ker && (oh < OH); )==" "\n" |
154 | R"==(#if CASE_3D )==" "\n" |
155 | R"==(do_ker = do_ker && (od < OD); )==" "\n" |
156 | R"==(#endif )==" "\n" |
157 | R"==(#endif )==" "\n" |
158 | R"==(#if SW != 1 || SH != 1 || SD != 1 || PH != 0 || PW != 0 || PD != 0 )==" "\n" |
159 | R"==(if (do_ker) { )==" "\n" |
160 | R"==(#endif )==" "\n" |
161 | R"==(const __global DATA_T *diff_dst1 )==" "\n" |
162 | R"==(= diff_dst + oh * OW * G * OC_WO_PADDING; )==" "\n" |
163 | R"==(#if CASE_3D )==" "\n" |
164 | R"==(diff_dst1 += od * OH * OW * G * OC_WO_PADDING; )==" "\n" |
165 | R"==(#endif )==" "\n" |
166 | R"==(const __global DATA_T *wei1 = wei; )==" "\n" |
167 | R"==(#endif )==" "\n" |
168 | R"==(int ocb = 0; )==" "\n" |
169 | R"==(do { )==" "\n" |
170 | R"==(DATA8_T blockB00 = AS_DATA8_T( )==" "\n" |
171 | R"==(BLOCK_READ8((const __global BLOCK_DATA_T *)wei1)); )==" "\n" |
172 | R"==(DATA8_T blockB01 = AS_DATA8_T( )==" "\n" |
173 | R"==(BLOCK_READ8((const __global BLOCK_DATA_T *)(wei1 )==" "\n" |
174 | R"==(+ 8 * IC_BLOCK))); )==" "\n" |
175 | R"==(DATA_T blockA[IW_BLOCK]; )==" "\n" |
176 | R"==(__attribute__(( )==" "\n" |
177 | R"==(opencl_unroll_hint(IW_BLOCK))) )==" "\n" |
178 | R"==(for (int i = 0; i < IW_BLOCK; i++) { )==" "\n" |
179 | R"==(#if KW != 1 )==" "\n" |
180 | R"==(if (iw + i + PW < kw * (1 + DW)) { )==" "\n" |
181 | R"==(blockA[i] = 0.0; )==" "\n" |
182 | R"==(continue; )==" "\n" |
183 | R"==(} )==" "\n" |
184 | R"==(int ow = iw + i - kw * (1 + DW) + PW; )==" "\n" |
185 | R"==(#else )==" "\n" |
186 | R"==(int ow = iw + i + PW; )==" "\n" |
187 | R"==(#endif )==" "\n" |
188 | R"==(#if SW != 1 )==" "\n" |
189 | R"==(if (ow % SW != 0) { )==" "\n" |
190 | R"==(blockA[i] = 0.0; )==" "\n" |
191 | R"==(continue; )==" "\n" |
192 | R"==(} )==" "\n" |
193 | R"==(ow /= SW; )==" "\n" |
194 | R"==(#endif )==" "\n" |
195 | R"==(if (ow >= OW) { )==" "\n" |
196 | R"==(blockA[i] = 0.0; )==" "\n" |
197 | R"==(continue; )==" "\n" |
198 | R"==(} )==" "\n" |
199 | R"==(blockA[i] = read_oc_block( )==" "\n" |
200 | R"==(&diff_dst1[ow * G * OC_WO_PADDING], ocb); )==" "\n" |
201 | R"==(} )==" "\n" |
202 | R"==(__attribute__(( )==" "\n" |
203 | R"==(opencl_unroll_hint(IW_BLOCK))) )==" "\n" |
204 | R"==(for (int i = 0; i < IW_BLOCK; i++) { )==" "\n" |
205 | R"==(MULTIPLY_BLOCKS_8x8( )==" "\n" |
206 | R"==(blockC00[i], blockA[i], blockB00, blockB01); )==" "\n" |
207 | R"==(} )==" "\n" |
208 | R"==(diff_dst1 += OC_BLOCK; )==" "\n" |
209 | R"==(wei1 += KD * KH * KW * OC_BLOCK * IC_PADDED; )==" "\n" |
210 | R"==(ocb += OC_BLOCK; )==" "\n" |
211 | R"==(} while (ocb < OC); )==" "\n" |
212 | R"==(#if SW != 1 || SH != 1 || SD != 1 || PH != 0 || PW != 0 || PD != 0 )==" "\n" |
213 | R"==(} )==" "\n" |
214 | R"==(#else )==" "\n" |
215 | R"==(#if KH != 1 || KW != 1 || KD != 1 )==" "\n" |
216 | R"==(} )==" "\n" |
217 | R"==(#endif )==" "\n" |
218 | R"==(#endif )==" "\n" |
219 | R"==(__global DATA_T *src_write0 = diff_src )==" "\n" |
220 | R"==(+ mb * IC_WO_PADDING * G * ID * IH * IW )==" "\n" |
221 | R"==(+ id * IH * IW * G * IC_WO_PADDING + ih * IW * G * IC_WO_PADDING )==" "\n" |
222 | R"==(+ iw * G * IC_WO_PADDING + g * IC_WO_PADDING + gic * IC_BLOCK; )==" "\n" |
223 | R"==(for (int i = 0; i < min(IW_BLOCK, IW - iw); i++) { )==" "\n" |
224 | R"==(write_ic_block(&src_write0[i * G * IC_WO_PADDING], gic * IC_BLOCK, )==" "\n" |
225 | R"==(blockC00[i]); )==" "\n" |
226 | R"==(} )==" "\n" |
227 | R"==(} )==" "\n" |
228 | R"==(#undef TRANSPOSE_BLOCK_8 )==" "\n" |
229 | R"==(#undef MULTIPLY_BLOCKS_8x8 )==" "\n" |
230 | R"==()==" ; |
231 | } |
232 | } |
233 | } |
234 | } |