1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_layer_normalization_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_types.h" )==" "\n" |
21 | R"==(#undef SRC_OFF )==" "\n" |
22 | R"==(#undef DST_OFF )==" "\n" |
23 | R"==(#define SRC_OFF(x0, x1, x2, x3, x4, x5) OFF_MD(SRC, x0, x1, x2, x3, x4, x5) )==" "\n" |
24 | R"==(#define DST_OFF(x0, x1, x2, x3, x4, x5) OFF_MD(DST, x0, x1, x2, x3, x4, x5) )==" "\n" |
25 | R"==(#define STAT_OFF(x0, x1, x2, x3, x4, x5) OFF_MD(STAT, x0, x1, x2, x3, x4, x5) )==" "\n" |
26 | R"==(#if IS_FWD )==" "\n" |
27 | R"==(#if VECTORIZE_CALC_STATS == 1 )==" "\n" |
28 | R"==(KERNEL_ATTR )==" "\n" |
29 | R"==(__kernel void ref_lnorm_fwd(__global DATA_T *src, __global float *mean, )==" "\n" |
30 | R"==(__global float *variance, __global DATA_T *dst, __global float *scale, )==" "\n" |
31 | R"==(__global float *shift, float eps) { )==" "\n" |
32 | R"==(int x[6] = {0}; )==" "\n" |
33 | R"==(x[0] = GWS_GET_X0(); )==" "\n" |
34 | R"==(x[1] = GWS_GET_X1(); )==" "\n" |
35 | R"==(x[2] = GWS_GET_X2(); )==" "\n" |
36 | R"==(x[3] = GWS_GET_X3(); )==" "\n" |
37 | R"==(if (x[0] >= DST_D0 || x[1] >= DST_D1 || x[2] >= DST_D2 || x[3] >= DST_D3) { )==" "\n" |
38 | R"==(int local_id = get_sub_group_local_id(); )==" "\n" |
39 | R"==(for (int c = 0; c < C; c += SUB_GROUP_SIZE) { )==" "\n" |
40 | R"==(x[NDIMS - 1] = c + local_id; )==" "\n" |
41 | R"==(int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
42 | R"==(dst[dst_off] = CONVERT_DATA_T(0.f); )==" "\n" |
43 | R"==(} )==" "\n" |
44 | R"==(return; )==" "\n" |
45 | R"==(} )==" "\n" |
46 | R"==(int s_off = STAT_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
47 | R"==(float v_mean = CALCULATE_STATS ? 0 : mean[s_off]; )==" "\n" |
48 | R"==(float v_variance = CALCULATE_STATS ? 0 : variance[s_off]; )==" "\n" |
49 | R"==(if (CALCULATE_STATS) { )==" "\n" |
50 | R"==(VECT_FLOAT_T v_acc = 0; )==" "\n" |
51 | R"==(for (int c = 0; c < C; c += SUB_GROUP_SIZE * VECT_DT_N) { )==" "\n" |
52 | R"==(x[NDIMS - 1] = c; )==" "\n" |
53 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
54 | R"==(v_acc += CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T(VECT_BLOCK_READ( )==" "\n" |
55 | R"==((const __global BLOCK_DATA_T *)&src[src_off]))); )==" "\n" |
56 | R"==(} )==" "\n" |
57 | R"==(#if VECT_DT_N == 1 )==" "\n" |
58 | R"==(v_mean = v_acc; )==" "\n" |
59 | R"==(#else )==" "\n" |
60 | R"==(v_mean = 0; )==" "\n" |
61 | R"==(for (int i = 0; i < VECT_DT_N; ++i) { )==" "\n" |
62 | R"==(v_mean += v_acc[i]; )==" "\n" |
63 | R"==(} )==" "\n" |
64 | R"==(#endif )==" "\n" |
65 | R"==(float total_sum = sub_group_reduce_add(v_mean); )==" "\n" |
66 | R"==(v_mean = total_sum / C; )==" "\n" |
67 | R"==(v_acc = 0; )==" "\n" |
68 | R"==(VECT_FLOAT_T m = 0; )==" "\n" |
69 | R"==(for (int c = 0; c < C; c += SUB_GROUP_SIZE * VECT_DT_N) { )==" "\n" |
70 | R"==(x[NDIMS - 1] = c; )==" "\n" |
71 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
72 | R"==(m = CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T(VECT_BLOCK_READ( )==" "\n" |
73 | R"==((const __global BLOCK_DATA_T *)&src[src_off]))); )==" "\n" |
74 | R"==(m -= v_mean; )==" "\n" |
75 | R"==(v_acc += m * m; )==" "\n" |
76 | R"==(} )==" "\n" |
77 | R"==(#if VECT_DT_N == 1 )==" "\n" |
78 | R"==(v_variance = v_acc; )==" "\n" |
79 | R"==(#else )==" "\n" |
80 | R"==(v_variance = 0; )==" "\n" |
81 | R"==(for (int i = 0; i < VECT_DT_N; ++i) { )==" "\n" |
82 | R"==(v_variance += v_acc[i]; )==" "\n" |
83 | R"==(} )==" "\n" |
84 | R"==(#endif )==" "\n" |
85 | R"==(total_sum = sub_group_reduce_add(v_variance); )==" "\n" |
86 | R"==(v_variance = total_sum / C; )==" "\n" |
87 | R"==(} )==" "\n" |
88 | R"==(float sqrt_variance = sqrt(v_variance + eps); )==" "\n" |
89 | R"==(int local_id = get_sub_group_local_id(); )==" "\n" |
90 | R"==(for (int c = 0; c < C; c += SUB_GROUP_SIZE) { )==" "\n" |
91 | R"==(float sm = (scale ? scale[c + local_id] : 1.0f) / sqrt_variance; )==" "\n" |
92 | R"==(float sv = shift ? shift[c + local_id] : 0.0f; )==" "\n" |
93 | R"==(x[NDIMS - 1] = c + local_id; )==" "\n" |
94 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
95 | R"==(int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
96 | R"==(dst[dst_off] = TO_DST(sm * (SRC_TO_REF(src[src_off]) - v_mean) + sv); )==" "\n" |
97 | R"==(} )==" "\n" |
98 | R"==(if (CALCULATE_STATS) { )==" "\n" |
99 | R"==(if (SAVE_STATS) { )==" "\n" |
100 | R"==(mean[s_off] = v_mean; )==" "\n" |
101 | R"==(variance[s_off] = v_variance; )==" "\n" |
102 | R"==(} )==" "\n" |
103 | R"==(} )==" "\n" |
104 | R"==(} )==" "\n" |
105 | R"==(#else )==" "\n" |
106 | R"==(KERNEL_ATTR )==" "\n" |
107 | R"==(__kernel void ref_lnorm_fwd(__global DATA_T *src, __global float *mean, )==" "\n" |
108 | R"==(__global float *variance, __global DATA_T *dst, __global float *scale, )==" "\n" |
109 | R"==(__global float *shift, float eps) { )==" "\n" |
110 | R"==(int x[6] = {0}; )==" "\n" |
111 | R"==(x[0] = GWS_GET_X0(); )==" "\n" |
112 | R"==(x[1] = GWS_GET_X1(); )==" "\n" |
113 | R"==(x[2] = GWS_GET_X2(); )==" "\n" |
114 | R"==(x[3] = GWS_GET_X3(); )==" "\n" |
115 | R"==(if (x[0] >= DST_D0 || x[1] >= DST_D1 || x[2] >= DST_D2 || x[3] >= DST_D3) { )==" "\n" |
116 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
117 | R"==(x[NDIMS - 1] = c; )==" "\n" |
118 | R"==(int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
119 | R"==(dst[dst_off] = CONVERT_DATA_T(0.f); )==" "\n" |
120 | R"==(} )==" "\n" |
121 | R"==(return; )==" "\n" |
122 | R"==(} )==" "\n" |
123 | R"==(int s_off = STAT_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
124 | R"==(float v_mean = CALCULATE_STATS ? 0 : mean[s_off]; )==" "\n" |
125 | R"==(float v_variance = CALCULATE_STATS ? 0 : variance[s_off]; )==" "\n" |
126 | R"==(if (CALCULATE_STATS) { )==" "\n" |
127 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
128 | R"==(x[NDIMS - 1] = c; )==" "\n" |
129 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
130 | R"==(v_mean += SRC_TO_REF(src[src_off]); )==" "\n" |
131 | R"==(} )==" "\n" |
132 | R"==(v_mean /= C; )==" "\n" |
133 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
134 | R"==(x[NDIMS - 1] = c; )==" "\n" |
135 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
136 | R"==(float m = SRC_TO_REF(src[src_off]) - v_mean; )==" "\n" |
137 | R"==(v_variance += m * m; )==" "\n" |
138 | R"==(} )==" "\n" |
139 | R"==(v_variance /= C; )==" "\n" |
140 | R"==(} )==" "\n" |
141 | R"==(float sqrt_variance = sqrt(v_variance + eps); )==" "\n" |
142 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
143 | R"==(float sm = (scale ? scale[c] : 1.0f) / sqrt_variance; )==" "\n" |
144 | R"==(float sv = shift ? shift[c] : 0.0f; )==" "\n" |
145 | R"==(x[NDIMS - 1] = c; )==" "\n" |
146 | R"==(int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
147 | R"==(int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
148 | R"==(dst[dst_off] = TO_DST(sm * (SRC_TO_REF(src[src_off]) - v_mean) + sv); )==" "\n" |
149 | R"==(} )==" "\n" |
150 | R"==(if (CALCULATE_STATS) { )==" "\n" |
151 | R"==(if (SAVE_STATS) { )==" "\n" |
152 | R"==(mean[s_off] = v_mean; )==" "\n" |
153 | R"==(variance[s_off] = v_variance; )==" "\n" |
154 | R"==(} )==" "\n" |
155 | R"==(} )==" "\n" |
156 | R"==(} )==" "\n" |
157 | R"==(#endif )==" "\n" |
158 | R"==(#endif )==" "\n" |
159 | R"==(#if IS_BWD )==" "\n" |
160 | R"==(#if USE_SCALE || USE_SHIFT )==" "\n" |
161 | R"==(#if VECTORIZE_BWD_SCALESHIFT )==" "\n" |
162 | R"==(#if VECTOR_SIZE_SCALESHIFT == 1 )==" "\n" |
163 | R"==(#define VECTORIZED_VERSION(x) x )==" "\n" |
164 | R"==(#define vector_load(x) (x); )==" "\n" |
165 | R"==(#else )==" "\n" |
166 | R"==(#define VECTORIZED_VERSION(x) CONCAT2(x, VECTOR_SIZE_SCALESHIFT) )==" "\n" |
167 | R"==(#define vector_load(x) CONCAT2(vload, VECTOR_SIZE_SCALESHIFT)(0, &x); )==" "\n" |
168 | R"==(#endif )==" "\n" |
169 | R"==(#if DT_BF16 == 1 )==" "\n" |
170 | R"==(#define convert_vector_to_float cvt_bf16_to_f32 )==" "\n" |
171 | R"==(#else )==" "\n" |
172 | R"==(#define convert_vector_to_float VECTORIZED_VERSION(convert_float) )==" "\n" |
173 | R"==(#endif )==" "\n" |
174 | R"==(#define as_vector_data_t VECTORIZED_VERSION(AS_DATA_T) )==" "\n" |
175 | R"==(#define sub_group_read VECTORIZED_VERSION(BLOCK_READ) )==" "\n" |
176 | R"==(#define vector_float VECTORIZED_VERSION(float) )==" "\n" |
177 | R"==(NAMED_KERNEL_ATTR(SCALESHIFT) )==" "\n" |
178 | R"==(__kernel void ref_lnorm_bwd_scaleshift(__global DATA_T *src, )==" "\n" |
179 | R"==(__global float *mean, __global float *variance, )==" "\n" |
180 | R"==(__global DATA_T *diff_dst, __global float *diff_scale, )==" "\n" |
181 | R"==(__global float *diff_shift, float eps) { )==" "\n" |
182 | R"==(const int c = GWS_GET_C(); )==" "\n" |
183 | R"==(const int n_chunk_idx = GWS_GET_N(); )==" "\n" |
184 | R"==(const int n_start = n_chunk_idx * N_CHUNK_SIZE; )==" "\n" |
185 | R"==(const int n_end = n_start + N_CHUNK_SIZE; )==" "\n" |
186 | R"==(const int shift_off = N_CHUNKS * C; )==" "\n" |
187 | R"==(diff_shift += shift_off; )==" "\n" |
188 | R"==(vector_float diff_gamma_vect = 0; )==" "\n" |
189 | R"==(vector_float diff_beta_vect = 0; )==" "\n" |
190 | R"==(for (int n_off = n_start; n_off < n_end; n_off += VECTOR_SIZE_SCALESHIFT) { )==" "\n" |
191 | R"==(const vector_float mean_vect = vector_load(mean[n_off]); )==" "\n" |
192 | R"==(const vector_float variance_vect = vector_load(variance[n_off]); )==" "\n" |
193 | R"==(const vector_float inv_sqrt_variance = 1.0f / sqrt(variance_vect + eps); )==" "\n" |
194 | R"==(#if NDIMS == 2 )==" "\n" |
195 | R"==(const int src_off = SRC_OFF(n_off, c, 0, 0, 0, 0); )==" "\n" |
196 | R"==(const int dst_off = DST_OFF(n_off, c, 0, 0, 0, 0); )==" "\n" |
197 | R"==(#else )==" "\n" |
198 | R"==(const int src_off = SRC_OFF(0, n_off, c, 0, 0, 0); )==" "\n" |
199 | R"==(const int dst_off = DST_OFF(0, n_off, c, 0, 0, 0); )==" "\n" |
200 | R"==(#endif )==" "\n" |
201 | R"==(const vector_float src_vect = convert_vector_to_float(as_vector_data_t( )==" "\n" |
202 | R"==(sub_group_read((const __global BLOCK_DATA_T *)&src[src_off]))); )==" "\n" |
203 | R"==(const vector_float diff_dst_vect )==" "\n" |
204 | R"==(= convert_vector_to_float(as_vector_data_t(sub_group_read( )==" "\n" |
205 | R"==((const __global BLOCK_DATA_T *)&diff_dst[dst_off]))); )==" "\n" |
206 | R"==(diff_gamma_vect )==" "\n" |
207 | R"==(+= (src_vect - mean_vect) * diff_dst_vect * inv_sqrt_variance; )==" "\n" |
208 | R"==(diff_beta_vect += diff_dst_vect; )==" "\n" |
209 | R"==(} )==" "\n" |
210 | R"==(float diff_gamma = 0, diff_beta = 0; )==" "\n" |
211 | R"==(#if VECTOR_SIZE_SCALESHIFT == 1 )==" "\n" |
212 | R"==(diff_gamma = diff_gamma_vect; )==" "\n" |
213 | R"==(diff_beta = diff_beta_vect; )==" "\n" |
214 | R"==(#else )==" "\n" |
215 | R"==(for (int elem_idx = 0; elem_idx < VECTOR_SIZE_SCALESHIFT; elem_idx++) { )==" "\n" |
216 | R"==(diff_gamma += diff_gamma_vect[elem_idx]; )==" "\n" |
217 | R"==(diff_beta += diff_beta_vect[elem_idx]; )==" "\n" |
218 | R"==(} )==" "\n" |
219 | R"==(#endif )==" "\n" |
220 | R"==(const int result_offset = n_chunk_idx * C + c; )==" "\n" |
221 | R"==(if (USE_SCALE) )==" "\n" |
222 | R"==(intel_sub_group_block_write((__global uint *)&diff_scale[result_offset], )==" "\n" |
223 | R"==(as_uint(diff_gamma)); )==" "\n" |
224 | R"==(if (USE_SHIFT) )==" "\n" |
225 | R"==(intel_sub_group_block_write((__global uint *)&diff_shift[result_offset], )==" "\n" |
226 | R"==(as_uint(diff_beta)); )==" "\n" |
227 | R"==(} )==" "\n" |
228 | R"==(NAMED_KERNEL_ATTR(SCALESHIFT_FINALIZE) )==" "\n" |
229 | R"==(__kernel void ref_lnorm_bwd_scaleshift_final(__global float *tmp_reduce_mem, )==" "\n" |
230 | R"==(__global float *diff_scale, __global float *diff_shift) { )==" "\n" |
231 | R"==(const int c = GWS_GET_C_finalize(); )==" "\n" |
232 | R"==(const int diff_shift_off = N_CHUNKS * C; )==" "\n" |
233 | R"==(__global float *tmp_diff_scale = tmp_reduce_mem; )==" "\n" |
234 | R"==(__global float *tmp_diff_shift = tmp_reduce_mem + diff_shift_off; )==" "\n" |
235 | R"==(float diff_gamma = 0; )==" "\n" |
236 | R"==(float diff_beta = 0; )==" "\n" |
237 | R"==(for (int n_chunk_idx = 0; n_chunk_idx < N_CHUNKS; n_chunk_idx++) { )==" "\n" |
238 | R"==(const int result_off = n_chunk_idx * C + c; )==" "\n" |
239 | R"==(diff_gamma += tmp_diff_scale[result_off]; )==" "\n" |
240 | R"==(diff_beta += tmp_diff_shift[result_off]; )==" "\n" |
241 | R"==(} )==" "\n" |
242 | R"==(if (diff_scale) diff_scale[c] = diff_gamma; )==" "\n" |
243 | R"==(if (diff_shift) diff_shift[c] = diff_beta; )==" "\n" |
244 | R"==(} )==" "\n" |
245 | R"==(#else )==" "\n" |
246 | R"==(NAMED_KERNEL_ATTR(SCALESHIFT) )==" "\n" |
247 | R"==(__kernel void ref_lnorm_bwd_scaleshift(__global DATA_T *src, )==" "\n" |
248 | R"==(__global float *mean, __global float *variance, )==" "\n" |
249 | R"==(__global DATA_T *diff_dst, __global float *diff_scale, )==" "\n" |
250 | R"==(__global float *diff_shift, float eps) { )==" "\n" |
251 | R"==(const int c = GWS_GET_C(); )==" "\n" |
252 | R"==(int x[6] = {0}; )==" "\n" |
253 | R"==(float diff_gamma = 0; )==" "\n" |
254 | R"==(float diff_beta = 0; )==" "\n" |
255 | R"==(for (x[0] = 0; x[0] < max(1, STAT_D0); ++x[0]) { )==" "\n" |
256 | R"==(for (x[1] = 0; x[1] < max(1, STAT_D1); ++x[1]) { )==" "\n" |
257 | R"==(for (x[2] = 0; x[2] < max(1, STAT_D2); ++x[2]) { )==" "\n" |
258 | R"==(for (x[3] = 0; x[3] < max(1, STAT_D3); ++x[3]) { )==" "\n" |
259 | R"==(x[NDIMS - 1] = 0; )==" "\n" |
260 | R"==(const int s_off )==" "\n" |
261 | R"==(= STAT_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
262 | R"==(x[NDIMS - 1] = c; )==" "\n" |
263 | R"==(const int src_off )==" "\n" |
264 | R"==(= SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
265 | R"==(const int dst_off )==" "\n" |
266 | R"==(= DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
267 | R"==(const float inv_sqrt_variance )==" "\n" |
268 | R"==(= 1.0f / sqrt(variance[s_off] + eps); )==" "\n" |
269 | R"==(const float dd = DST_TO_REF(diff_dst[dst_off]); )==" "\n" |
270 | R"==(diff_gamma += (SRC_TO_REF(src[src_off]) - mean[s_off]) * dd )==" "\n" |
271 | R"==(* inv_sqrt_variance; )==" "\n" |
272 | R"==(diff_beta += dd; )==" "\n" |
273 | R"==(} )==" "\n" |
274 | R"==(} )==" "\n" |
275 | R"==(} )==" "\n" |
276 | R"==(} )==" "\n" |
277 | R"==(if (diff_scale) diff_scale[c] = diff_gamma; )==" "\n" |
278 | R"==(if (diff_shift) diff_shift[c] = diff_beta; )==" "\n" |
279 | R"==(} )==" "\n" |
280 | R"==(#endif )==" "\n" |
281 | R"==(#endif )==" "\n" |
282 | R"==(#if VECTORIZE_BWD )==" "\n" |
283 | R"==(KERNEL_ATTR )==" "\n" |
284 | R"==(__kernel void ref_lnorm_bwd(__global DATA_T *src, __global float *mean, )==" "\n" |
285 | R"==(__global float *variance, __global DATA_T *diff_dst, )==" "\n" |
286 | R"==(__global float *scale, __global DATA_T *diff_src, float eps) { )==" "\n" |
287 | R"==(int x[6] = {0}; )==" "\n" |
288 | R"==(x[0] = GWS_GET_X0(); )==" "\n" |
289 | R"==(x[1] = GWS_GET_X1(); )==" "\n" |
290 | R"==(x[2] = GWS_GET_X2(); )==" "\n" |
291 | R"==(x[3] = GWS_GET_X3(); )==" "\n" |
292 | R"==(const int s_off = STAT_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
293 | R"==(const float mean_val = mean[s_off]; )==" "\n" |
294 | R"==(const float inv_sqrt_variance = 1.0f / sqrt(variance[s_off] + eps); )==" "\n" |
295 | R"==(float dd_gamma = 0, dd_gamma_x = 0; )==" "\n" |
296 | R"==(VECT_FLOAT_T dd_gamma_vect = 0; )==" "\n" |
297 | R"==(VECT_FLOAT_T dd_gamma_x_vect = 0; )==" "\n" |
298 | R"==(if (CALCULATE_STATS) { )==" "\n" |
299 | R"==(for (int c = 0; c < C; c += VECT_DT_N * SUB_GROUP_SIZE) { )==" "\n" |
300 | R"==(VECT_FLOAT_T gamma = 1.0f; )==" "\n" |
301 | R"==(if (scale) { )==" "\n" |
302 | R"==(gamma = AS_VECT_FLOAT_T( )==" "\n" |
303 | R"==(VECT_UINT_READ((const __global uint *)&scale[c])); )==" "\n" |
304 | R"==(} )==" "\n" |
305 | R"==(x[NDIMS - 1] = c; )==" "\n" |
306 | R"==(const int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
307 | R"==(const int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
308 | R"==(const VECT_FLOAT_T src_vect )==" "\n" |
309 | R"==(= CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T(VECT_BLOCK_READ( )==" "\n" |
310 | R"==((const __global BLOCK_DATA_T *)&src[src_off]))); )==" "\n" |
311 | R"==(const VECT_FLOAT_T dst_vect )==" "\n" |
312 | R"==(= CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T(VECT_BLOCK_READ(( )==" "\n" |
313 | R"==(const __global BLOCK_DATA_T *)&diff_dst[dst_off]))); )==" "\n" |
314 | R"==(dd_gamma_vect += dst_vect * gamma; )==" "\n" |
315 | R"==(dd_gamma_x_vect += dst_vect * gamma * (src_vect - mean_val); )==" "\n" |
316 | R"==(} )==" "\n" |
317 | R"==(#if VECT_DT_N == 1 )==" "\n" |
318 | R"==(dd_gamma = dd_gamma_vect; )==" "\n" |
319 | R"==(dd_gamma_x = dd_gamma_x_vect; )==" "\n" |
320 | R"==(#else )==" "\n" |
321 | R"==(for (int i = 0; i < VECT_DT_N; ++i) { )==" "\n" |
322 | R"==(dd_gamma += dd_gamma_vect[i]; )==" "\n" |
323 | R"==(dd_gamma_x += dd_gamma_x_vect[i]; )==" "\n" |
324 | R"==(} )==" "\n" |
325 | R"==(#endif )==" "\n" |
326 | R"==(dd_gamma = sub_group_reduce_add(dd_gamma); )==" "\n" |
327 | R"==(dd_gamma_x = sub_group_reduce_add(dd_gamma_x); )==" "\n" |
328 | R"==(dd_gamma_x *= inv_sqrt_variance; )==" "\n" |
329 | R"==(} )==" "\n" |
330 | R"==(for (int c = 0; c < C; c += VECT_DT_N * SUB_GROUP_SIZE) { )==" "\n" |
331 | R"==(VECT_FLOAT_T gamma = 1.0f; )==" "\n" |
332 | R"==(if (scale) { )==" "\n" |
333 | R"==(gamma = AS_VECT_FLOAT_T( )==" "\n" |
334 | R"==(VECT_UINT_READ((const __global uint *)&scale[c])); )==" "\n" |
335 | R"==(} )==" "\n" |
336 | R"==(x[NDIMS - 1] = c; )==" "\n" |
337 | R"==(const int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
338 | R"==(const int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
339 | R"==(const VECT_FLOAT_T src_vect = CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T( )==" "\n" |
340 | R"==(VECT_BLOCK_READ((const __global BLOCK_DATA_T *)&src[src_off]))); )==" "\n" |
341 | R"==(VECT_FLOAT_T v_diff_src_vect )==" "\n" |
342 | R"==(= CONVERT_VECT_FLOAT_T(AS_VECT_DATA_T(VECT_BLOCK_READ( )==" "\n" |
343 | R"==((const __global BLOCK_DATA_T *)&diff_dst[dst_off]))); )==" "\n" |
344 | R"==(v_diff_src_vect *= gamma; )==" "\n" |
345 | R"==(if (CALCULATE_STATS) { )==" "\n" |
346 | R"==(v_diff_src_vect -= dd_gamma / C )==" "\n" |
347 | R"==(+ (src_vect - mean_val) * dd_gamma_x * inv_sqrt_variance )==" "\n" |
348 | R"==(/ C; )==" "\n" |
349 | R"==(} )==" "\n" |
350 | R"==(v_diff_src_vect *= inv_sqrt_variance; )==" "\n" |
351 | R"==(VECT_BLOCK_WRITE((__global BLOCK_DATA_T *)&diff_src[src_off], )==" "\n" |
352 | R"==(AS_VECT_BLOCK_DATA_T(CONVERT_VECTOR_DATA_T(v_diff_src_vect))); )==" "\n" |
353 | R"==(} )==" "\n" |
354 | R"==(} )==" "\n" |
355 | R"==(#else )==" "\n" |
356 | R"==(KERNEL_ATTR )==" "\n" |
357 | R"==(__kernel void ref_lnorm_bwd(__global DATA_T *src, __global float *mean, )==" "\n" |
358 | R"==(__global float *variance, __global DATA_T *diff_dst, )==" "\n" |
359 | R"==(__global float *scale, __global DATA_T *diff_src, float eps) { )==" "\n" |
360 | R"==(int x[6] = {0}; )==" "\n" |
361 | R"==(x[0] = GWS_GET_X0(); )==" "\n" |
362 | R"==(x[1] = GWS_GET_X1(); )==" "\n" |
363 | R"==(x[2] = GWS_GET_X2(); )==" "\n" |
364 | R"==(x[3] = GWS_GET_X3(); )==" "\n" |
365 | R"==(const int s_off = STAT_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
366 | R"==(const float mean_val = mean[s_off]; )==" "\n" |
367 | R"==(const float inv_sqrt_variance = 1.0f / sqrt(variance[s_off] + eps); )==" "\n" |
368 | R"==(float dd_gamma = 0; )==" "\n" |
369 | R"==(float dd_gamma_x = 0; )==" "\n" |
370 | R"==(if (CALCULATE_STATS) { )==" "\n" |
371 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
372 | R"==(const float gamma = scale ? scale[c] : 1.0f; )==" "\n" |
373 | R"==(x[NDIMS - 1] = c; )==" "\n" |
374 | R"==(const int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
375 | R"==(const int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
376 | R"==(const float dd = DST_TO_REF(diff_dst[dst_off]); )==" "\n" |
377 | R"==(dd_gamma += dd * gamma; )==" "\n" |
378 | R"==(dd_gamma_x += dd * gamma * (SRC_TO_REF(src[src_off]) - mean_val); )==" "\n" |
379 | R"==(} )==" "\n" |
380 | R"==(dd_gamma_x *= inv_sqrt_variance; )==" "\n" |
381 | R"==(} )==" "\n" |
382 | R"==(for (int c = 0; c < C; ++c) { )==" "\n" |
383 | R"==(const float gamma = scale ? scale[c] : 1.0f; )==" "\n" |
384 | R"==(x[NDIMS - 1] = c; )==" "\n" |
385 | R"==(const int src_off = SRC_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
386 | R"==(const int dst_off = DST_OFF(x[0], x[1], x[2], x[3], x[4], x[5]); )==" "\n" |
387 | R"==(float v_diff_src = DST_TO_REF(diff_dst[dst_off]) * gamma; )==" "\n" |
388 | R"==(if (CALCULATE_STATS) { )==" "\n" |
389 | R"==(v_diff_src -= dd_gamma / C )==" "\n" |
390 | R"==(+ (SRC_TO_REF(src[src_off]) - mean_val) * dd_gamma_x )==" "\n" |
391 | R"==(* inv_sqrt_variance / C; )==" "\n" |
392 | R"==(} )==" "\n" |
393 | R"==(v_diff_src *= inv_sqrt_variance; )==" "\n" |
394 | R"==(diff_src[src_off] = TO_SRC(v_diff_src); )==" "\n" |
395 | R"==(} )==" "\n" |
396 | R"==(} )==" "\n" |
397 | R"==(#endif )==" "\n" |
398 | R"==(#endif )==" "\n" |
399 | R"==()==" ; |
400 | } |
401 | } |
402 | } |
403 | } |