1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_convolution_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"==(#undef SRC_OFF )==" "\n" |
23 | R"==(#undef WEI_OFF )==" "\n" |
24 | R"==(#undef DST_OFF )==" "\n" |
25 | R"==(#define SRC_OFF CONV_SRC_OFF )==" "\n" |
26 | R"==(#define WEI_OFF CONV_WEI_OFF )==" "\n" |
27 | R"==(#define DST_OFF CONV_DST_OFF )==" "\n" |
28 | R"==(#if IS_FWD )==" "\n" |
29 | R"==(KERNEL_ATTR )==" "\n" |
30 | R"==(__kernel void ref_convolution_fwd(const __global SRC_DATA_T *src, )==" "\n" |
31 | R"==(const __global WEI_DATA_T *wei, const __global BIA_DATA_T *bias, )==" "\n" |
32 | R"==(__global DST_DATA_T *dst POST_OP_ARGS, const __global float *src_scales, )==" "\n" |
33 | R"==(const __global float *wei_scales, const __global float *dst_scales, )==" "\n" |
34 | R"==(const __global int *src_zpoints, const __global int *dst_zpoints) { )==" "\n" |
35 | R"==(src += SRC_OFFSET0; )==" "\n" |
36 | R"==(dst += DST_OFFSET0; )==" "\n" |
37 | R"==(const int n = GWS_GET_MB(); )==" "\n" |
38 | R"==(const int oc = GWS_GET_OC(); )==" "\n" |
39 | R"==(const int g = GWS_GET_G(); )==" "\n" |
40 | R"==(const int od = GWS_GET_OD(); )==" "\n" |
41 | R"==(const int oh = GWS_GET_OH(); )==" "\n" |
42 | R"==(const int ow = GWS_GET_OW(); )==" "\n" |
43 | R"==(ACC_DATA_T d = 0; )==" "\n" |
44 | R"==(for (int ic = 0; ic < IC; ++ic) )==" "\n" |
45 | R"==(for (int kd = 0; kd < KD; ++kd) )==" "\n" |
46 | R"==(for (int kh = 0; kh < KH; ++kh) )==" "\n" |
47 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
48 | R"==(const int id = od * SD - PD + kd * (1 + DD); )==" "\n" |
49 | R"==(const int ih = oh * SH - PH + kh * (1 + DH); )==" "\n" |
50 | R"==(const int iw = ow * SW - PW + kw * (1 + DW); )==" "\n" |
51 | R"==(if (id < 0 || id >= ID || ih < 0 || ih >= IH || iw < 0 )==" "\n" |
52 | R"==(|| iw >= IW) )==" "\n" |
53 | R"==(continue; )==" "\n" |
54 | R"==(const uint src_off = SRC_OFF(n, g * IC + ic, id, ih, iw); )==" "\n" |
55 | R"==(const uint wei_off = WEI_OFF(g, oc, ic, kd, kh, kw); )==" "\n" |
56 | R"==(d += SRC_TO_REF(src[src_off]) * WEI_TO_REF(wei[wei_off]); )==" "\n" |
57 | R"==(#if WITH_SRC_ZPOINTS )==" "\n" |
58 | R"==(const int src_zp )==" "\n" |
59 | R"==(= src_zpoints[WITH_SRC_ZPOINTS_PER_IC ? g * IC + ic )==" "\n" |
60 | R"==(: 0]; )==" "\n" |
61 | R"==(d -= src_zp * WEI_TO_REF(wei[wei_off]); )==" "\n" |
62 | R"==(#endif )==" "\n" |
63 | R"==(} )==" "\n" |
64 | R"==(POST_OP_DATA_T tmp = d; )==" "\n" |
65 | R"==(#if WITH_SRC_SCALES )==" "\n" |
66 | R"==(tmp *= src_scales[0]; )==" "\n" |
67 | R"==(#endif )==" "\n" |
68 | R"==(#if WITH_WEI_SCALES )==" "\n" |
69 | R"==(#if WEI_SCALES_MASK == 0 )==" "\n" |
70 | R"==(tmp *= wei_scales[0]; )==" "\n" |
71 | R"==(#else )==" "\n" |
72 | R"==(tmp *= wei_scales[g * OC + oc]; )==" "\n" |
73 | R"==(#endif )==" "\n" |
74 | R"==(#endif )==" "\n" |
75 | R"==(#if WITH_BIAS )==" "\n" |
76 | R"==(tmp += (POST_OP_DATA_T)BIA_TO_REF(bias[g * OC + oc]); )==" "\n" |
77 | R"==(#endif )==" "\n" |
78 | R"==(POST_OP_DATA_T sum_src; )==" "\n" |
79 | R"==(#if WITH_SUM )==" "\n" |
80 | R"==(sum_src = (POST_OP_DATA_T)SUM_TO_REF( )==" "\n" |
81 | R"==(AS_SUM_DATA_T(dst[DST_OFF(n, g * OC + oc, od, oh, ow)])); )==" "\n" |
82 | R"==(#endif )==" "\n" |
83 | R"==(#if NDIMS == 3 )==" "\n" |
84 | R"==(const unsigned po_d2 = ow; )==" "\n" |
85 | R"==(const unsigned po_d3 = 0; )==" "\n" |
86 | R"==(const unsigned po_d4 = 0; )==" "\n" |
87 | R"==(#elif NDIMS == 4 )==" "\n" |
88 | R"==(const unsigned po_d2 = oh; )==" "\n" |
89 | R"==(const unsigned po_d3 = ow; )==" "\n" |
90 | R"==(const unsigned po_d4 = 0; )==" "\n" |
91 | R"==(#elif NDIMS == 5 )==" "\n" |
92 | R"==(const unsigned po_d2 = od; )==" "\n" |
93 | R"==(const unsigned po_d3 = oh; )==" "\n" |
94 | R"==(const unsigned po_d4 = ow; )==" "\n" |
95 | R"==(#else )==" "\n" |
96 | R"==(const unsigned po_d2 = 0; )==" "\n" |
97 | R"==(const unsigned po_d3 = 0; )==" "\n" |
98 | R"==(const unsigned po_d4 = 0; )==" "\n" |
99 | R"==(#endif )==" "\n" |
100 | R"==(APPLY_POST_OPS_SERIAL(tmp, POST_OP_DATA_T, sum_src, POST_OP_DATA_T, n, 1, )==" "\n" |
101 | R"==(g * OC + oc, 1, po_d2, 1, po_d3, 1, po_d4, 1, 0, 1); )==" "\n" |
102 | R"==(#if WITH_DST_SCALES )==" "\n" |
103 | R"==(tmp /= dst_scales[0]; )==" "\n" |
104 | R"==(#endif )==" "\n" |
105 | R"==(#if WITH_DST_ZPOINTS )==" "\n" |
106 | R"==(const int dst_zp = dst_zpoints[WITH_DST_ZPOINTS_PER_OC ? g * OC + oc : 0]; )==" "\n" |
107 | R"==(tmp += dst_zp; )==" "\n" |
108 | R"==(#endif )==" "\n" |
109 | R"==(dst[DST_OFF(n, g * OC + oc, od, oh, ow)] = TO_DST(tmp); )==" "\n" |
110 | R"==(} )==" "\n" |
111 | R"==(#endif )==" "\n" |
112 | R"==(#if IS_BWD_D )==" "\n" |
113 | R"==(KERNEL_ATTR )==" "\n" |
114 | R"==(__kernel void ref_convolution_bwd_data(__global SRC_DATA_T *diff_src, )==" "\n" |
115 | R"==(const __global WEI_DATA_T *wei, const __global DST_DATA_T *diff_dst, )==" "\n" |
116 | R"==(const __global BIA_DATA_T *bias POST_OP_ARGS, )==" "\n" |
117 | R"==(const __global float *src_scales, const __global float *wei_scales, )==" "\n" |
118 | R"==(const __global float *dst_scales, const __global int *src_zpoints, )==" "\n" |
119 | R"==(const __global int *dst_zpoints) { )==" "\n" |
120 | R"==(const int n = GWS_GET_MB(); )==" "\n" |
121 | R"==(const int ic = GWS_GET_IC(); )==" "\n" |
122 | R"==(const int g = GWS_GET_G(); )==" "\n" |
123 | R"==(const int id = GWS_GET_ID(); )==" "\n" |
124 | R"==(const int ih = GWS_GET_IH(); )==" "\n" |
125 | R"==(const int iw = GWS_GET_IW(); )==" "\n" |
126 | R"==(ACC_DATA_T d = WITH_BIAS ? BIA_TO_REF(bias[g * IC + ic]) : 0.0; )==" "\n" |
127 | R"==(for_(int oc = 0; oc < OC; ++oc) )==" "\n" |
128 | R"==(for_(int kd = 0; kd < KD; ++kd) )==" "\n" |
129 | R"==(for_(int kh = 0; kh < KH; ++kh) )==" "\n" |
130 | R"==(for (int kw = 0; kw < KW; ++kw) { )==" "\n" |
131 | R"==(if (iw + PW < kw * (1 + DW) || ih + PH < kh * (1 + DH) )==" "\n" |
132 | R"==(|| id + PD < kd * (1 + DD)) )==" "\n" |
133 | R"==(continue; )==" "\n" |
134 | R"==(int ow = iw - kw * (1 + DW) + PW; )==" "\n" |
135 | R"==(int oh = ih - kh * (1 + DH) + PH; )==" "\n" |
136 | R"==(int od = id - kd * (1 + DD) + PD; )==" "\n" |
137 | R"==(if (ow % SW != 0 || oh % SH != 0 || od % SD != 0) continue; )==" "\n" |
138 | R"==(ow /= SW; )==" "\n" |
139 | R"==(oh /= SH; )==" "\n" |
140 | R"==(od /= SD; )==" "\n" |
141 | R"==(if (oh < OH && ow < OW && od < OD) { )==" "\n" |
142 | R"==(const uint dst_off = DST_OFF(n, g * OC + oc, od, oh, ow); )==" "\n" |
143 | R"==(const uint wei_off = WEI_OFF(g, oc, ic, kd, kh, kw); )==" "\n" |
144 | R"==(d += DST_TO_REF(diff_dst[dst_off]) * WEI_TO_REF(wei[wei_off]); )==" "\n" |
145 | R"==(#if WITH_SRC_ZPOINTS )==" "\n" |
146 | R"==(const int src_zp )==" "\n" |
147 | R"==(= src_zpoints[WITH_SRC_ZPOINTS_PER_IC ? g * OC + oc : 0]; )==" "\n" |
148 | R"==(d -= src_zp * WEI_TO_REF(wei[wei_off]); )==" "\n" |
149 | R"==(#endif )==" "\n" |
150 | R"==(} )==" "\n" |
151 | R"==(} )==" "\n" |
152 | R"==(float sum_src; )==" "\n" |
153 | R"==(#if WITH_SUM )==" "\n" |
154 | R"==(sum_src = convert_float( )==" "\n" |
155 | R"==(SRC_TO_REF(diff_src[SRC_OFF(n, g * IC + ic, id, ih, iw)])); )==" "\n" |
156 | R"==(#endif )==" "\n" |
157 | R"==(float accumulator = convert_float(d); )==" "\n" |
158 | R"==(#if WITH_SRC_SCALES )==" "\n" |
159 | R"==(accumulator *= src_scales[0]; )==" "\n" |
160 | R"==(#endif )==" "\n" |
161 | R"==(#if WITH_WEI_SCALES )==" "\n" |
162 | R"==(#if WEI_SCALES_MASK == 0 )==" "\n" |
163 | R"==(accumulator *= wei_scales[0]; )==" "\n" |
164 | R"==(#else )==" "\n" |
165 | R"==(accumulator *= wei_scales[g * IC + ic]; )==" "\n" |
166 | R"==(#endif )==" "\n" |
167 | R"==(#endif )==" "\n" |
168 | R"==(#if NDIMS == 3 )==" "\n" |
169 | R"==(const unsigned po_d2 = iw; )==" "\n" |
170 | R"==(const unsigned po_d3 = 0; )==" "\n" |
171 | R"==(const unsigned po_d4 = 0; )==" "\n" |
172 | R"==(#elif NDIMS == 4 )==" "\n" |
173 | R"==(const unsigned po_d2 = ih; )==" "\n" |
174 | R"==(const unsigned po_d3 = iw; )==" "\n" |
175 | R"==(const unsigned po_d4 = 0; )==" "\n" |
176 | R"==(#elif NDIMS == 5 )==" "\n" |
177 | R"==(const unsigned po_d2 = id; )==" "\n" |
178 | R"==(const unsigned po_d3 = ih; )==" "\n" |
179 | R"==(const unsigned po_d4 = iw; )==" "\n" |
180 | R"==(#else )==" "\n" |
181 | R"==(const unsigned po_d2 = 0; )==" "\n" |
182 | R"==(const unsigned po_d3 = 0; )==" "\n" |
183 | R"==(const unsigned po_d4 = 0; )==" "\n" |
184 | R"==(#endif )==" "\n" |
185 | R"==(APPLY_POST_OPS_SERIAL(accumulator, float, sum_src, float, n, 1, g *IC + ic, )==" "\n" |
186 | R"==(1, po_d2, 1, po_d3, 1, po_d4, 1, 0, 1); )==" "\n" |
187 | R"==(#if WITH_DST_SCALES )==" "\n" |
188 | R"==(accumulator /= dst_scales[0]; )==" "\n" |
189 | R"==(#endif )==" "\n" |
190 | R"==(#if WITH_DST_ZPOINTS )==" "\n" |
191 | R"==(const int dst_zp = dst_zpoints[WITH_DST_ZPOINTS_PER_OC ? g * IC + ic : 0]; )==" "\n" |
192 | R"==(accumulator += dst_zp; )==" "\n" |
193 | R"==(#endif )==" "\n" |
194 | R"==(diff_src[SRC_OFF(n, g * IC + ic, id, ih, iw)] = TO_SRC(accumulator); )==" "\n" |
195 | R"==(} )==" "\n" |
196 | R"==(#endif )==" "\n" |
197 | R"==(#if IS_BWD_W )==" "\n" |
198 | R"==(KERNEL_ATTR )==" "\n" |
199 | R"==(__kernel void ref_convolution_bwd_weights(const __global SRC_DATA_T *src, )==" "\n" |
200 | R"==(__global WEI_DATA_T *diff_wei, __global BIA_DATA_T *diff_bias, )==" "\n" |
201 | R"==(const __global DST_DATA_T *diff_dst) { )==" "\n" |
202 | R"==(const int g = GWS_GET_G(); )==" "\n" |
203 | R"==(const int ic = GWS_GET_IC(); )==" "\n" |
204 | R"==(const int oc = GWS_GET_OC(); )==" "\n" |
205 | R"==(const int kd = GWS_GET_KD(); )==" "\n" |
206 | R"==(const int kh = GWS_GET_KH(); )==" "\n" |
207 | R"==(const int kw = GWS_GET_KW(); )==" "\n" |
208 | R"==(#if WITH_BIAS )==" "\n" |
209 | R"==(if (ic == 0 && kh == 0 && kw == 0 & kd == 0) { )==" "\n" |
210 | R"==(ACC_DATA_T d = 0.0; )==" "\n" |
211 | R"==(for (int n = 0; n < MB; ++n) )==" "\n" |
212 | R"==(for (int od = 0; od < OD; ++od) )==" "\n" |
213 | R"==(for (int oh = 0; oh < OH; ++oh) )==" "\n" |
214 | R"==(for (int ow = 0; ow < OW; ++ow) { )==" "\n" |
215 | R"==(d += DST_TO_REF( )==" "\n" |
216 | R"==(diff_dst[DST_OFF(n, g * OC + oc, od, oh, ow)]); )==" "\n" |
217 | R"==(} )==" "\n" |
218 | R"==(diff_bias[g * OC + oc] = TO_BIA(d); )==" "\n" |
219 | R"==(} )==" "\n" |
220 | R"==(#endif )==" "\n" |
221 | R"==(ACC_DATA_T dw = 0.0; )==" "\n" |
222 | R"==(for (int n = 0; n < MB; ++n) )==" "\n" |
223 | R"==(for (int od = 0; od < OD; ++od) )==" "\n" |
224 | R"==(for (int oh = 0; oh < OH; ++oh) )==" "\n" |
225 | R"==(for (int ow = 0; ow < OW; ++ow) { )==" "\n" |
226 | R"==(if (ow * SW + kw * (1 + DW) < PW )==" "\n" |
227 | R"==(|| oh * SH + kh * (1 + DH) < PH )==" "\n" |
228 | R"==(|| od * SD + kd * (1 + DD) < PD )==" "\n" |
229 | R"==(|| ow * SW + kw * (1 + DW) >= IW + PW )==" "\n" |
230 | R"==(|| oh * SH + kh * (1 + DH) >= IH + PH )==" "\n" |
231 | R"==(|| od * SD + kd * (1 + DD) >= ID + PD) )==" "\n" |
232 | R"==(continue; )==" "\n" |
233 | R"==(int id = od * SD - PD + kd * (1 + DD); )==" "\n" |
234 | R"==(int ih = oh * SH - PH + kh * (1 + DH); )==" "\n" |
235 | R"==(int iw = ow * SW - PW + kw * (1 + DW); )==" "\n" |
236 | R"==(dw += DST_TO_REF( )==" "\n" |
237 | R"==(diff_dst[DST_OFF(n, g * OC + oc, od, oh, ow)]) )==" "\n" |
238 | R"==(* SRC_TO_REF( )==" "\n" |
239 | R"==(src[SRC_OFF(n, g * IC + ic, id, ih, iw)]); )==" "\n" |
240 | R"==(} )==" "\n" |
241 | R"==(diff_wei[WEI_OFF(g, oc, ic, kd, kh, kw)] = TO_WEI(dw); )==" "\n" |
242 | R"==(} )==" "\n" |
243 | R"==(#endif )==" "\n" |
244 | R"==()==" ; |
245 | } |
246 | } |
247 | } |
248 | } |