1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_lrn_kernel = R"==(/******************************************************************************* )==" "\n" |
6 | R"==(* Copyright 2019-2020 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 IS_FWD == 1 )==" "\n" |
22 | R"==(KERNEL_ATTR )==" "\n" |
23 | R"==(__kernel void ref_lrn_fwd(__global const DATA_T *src, )==" "\n" |
24 | R"==(#if IS_TRAINING == 1 )==" "\n" |
25 | R"==(__global DEF_ACC_DATA_T *ws, )==" "\n" |
26 | R"==(#endif )==" "\n" |
27 | R"==(__global DATA_T *dst) { )==" "\n" |
28 | R"==(const uint mb = GWS_GET_MB(); )==" "\n" |
29 | R"==(const uint ic = GWS_GET_IC(); )==" "\n" |
30 | R"==(const uint id = GWS_GET_ID(); )==" "\n" |
31 | R"==(const uint ih = GWS_GET_IH(); )==" "\n" |
32 | R"==(const uint iw = GWS_GET_IW(); )==" "\n" |
33 | R"==(const uint src_index = SRC_OFF(mb, ic, id, ih, iw); )==" "\n" |
34 | R"==(const uint dst_index = DST_OFF(mb, ic, id, ih, iw); )==" "\n" |
35 | R"==(DEF_ACC_DATA_T sum = 0.0f; )==" "\n" |
36 | R"==(#if ACROSS_CHANNEL )==" "\n" |
37 | R"==(for (int j = 0; j < LOCAL_SIZE; j++) { )==" "\n" |
38 | R"==(const int z_idx = (j + ic - PADDING); )==" "\n" |
39 | R"==(bool zero = (z_idx < 0 || z_idx >= IC); )==" "\n" |
40 | R"==(DEF_ACC_DATA_T val = zero )==" "\n" |
41 | R"==(? 0.0f )==" "\n" |
42 | R"==(: TO_DEF_ACC_DATA_T(src[SRC_OFF(mb, z_idx, id, ih, iw)]); )==" "\n" |
43 | R"==(sum += val * val; )==" "\n" |
44 | R"==(} )==" "\n" |
45 | R"==(#else )==" "\n" |
46 | R"==(const int d = (int)id - PADDING; )==" "\n" |
47 | R"==(const int h = (int)ih - PADDING; )==" "\n" |
48 | R"==(const int w = (int)iw - PADDING; )==" "\n" |
49 | R"==(const int d_start = max(d, 0); )==" "\n" |
50 | R"==(const int h_start = max(h, 0); )==" "\n" |
51 | R"==(const int w_start = max(w, 0); )==" "\n" |
52 | R"==(const int d_end = min(d + LOCAL_SIZE, ID); )==" "\n" |
53 | R"==(const int h_end = min(h + LOCAL_SIZE, IH); )==" "\n" |
54 | R"==(const int w_end = min(w + LOCAL_SIZE, IW); )==" "\n" |
55 | R"==(for (int k = d_start; k < d_end; ++k) { )==" "\n" |
56 | R"==(for (int j = h_start; j < h_end; ++j) { )==" "\n" |
57 | R"==(for (int i = w_start; i < w_end; ++i) { )==" "\n" |
58 | R"==(DEF_ACC_DATA_T val )==" "\n" |
59 | R"==(= TO_DEF_ACC_DATA_T(src[SRC_OFF(mb, ic, k, j, i)]); )==" "\n" |
60 | R"==(sum += val * val; )==" "\n" |
61 | R"==(} )==" "\n" |
62 | R"==(} )==" "\n" |
63 | R"==(} )==" "\n" |
64 | R"==(#endif )==" "\n" |
65 | R"==(const DEF_ACC_DATA_T num_elements_div = NUM_ELEMENTS_DIV; )==" "\n" |
66 | R"==(const DEF_ACC_DATA_T base = (DEF_ACC_DATA_T)LRN_K )==" "\n" |
67 | R"==(+ (DEF_ACC_DATA_T)LRN_ALPHA * sum * num_elements_div; )==" "\n" |
68 | R"==(const DEF_ACC_DATA_T normalization_factor )==" "\n" |
69 | R"==(= native_powr(base, (DEF_ACC_DATA_T)(-LRN_BETA)); )==" "\n" |
70 | R"==(const DEF_ACC_DATA_T val = TO_DEF_ACC_DATA_T(src[src_index]); )==" "\n" |
71 | R"==(const DEF_ACC_DATA_T normres = val * normalization_factor; )==" "\n" |
72 | R"==(#if IS_TRAINING == 1 )==" "\n" |
73 | R"==(ws[dst_index] = base; )==" "\n" |
74 | R"==(#endif )==" "\n" |
75 | R"==(dst[dst_index] = TO_DATA_T(normres); )==" "\n" |
76 | R"==(} )==" "\n" |
77 | R"==(#endif )==" "\n" |
78 | R"==(#if IS_BWD == 1 )==" "\n" |
79 | R"==(KERNEL_ATTR )==" "\n" |
80 | R"==(__kernel void ref_lrn_bwd(__global const DATA_T *src, )==" "\n" |
81 | R"==(__global const DATA_T *diff_dst, __global DEF_ACC_DATA_T *ws, )==" "\n" |
82 | R"==(__global DATA_T *diff_src) { )==" "\n" |
83 | R"==(const uint mb = GWS_GET_MB(); )==" "\n" |
84 | R"==(const uint ic = GWS_GET_IC(); )==" "\n" |
85 | R"==(const uint id = GWS_GET_ID(); )==" "\n" |
86 | R"==(const uint ih = GWS_GET_IH(); )==" "\n" |
87 | R"==(const uint iw = GWS_GET_IW(); )==" "\n" |
88 | R"==(const uint src_index = SRC_OFF(mb, ic, id, ih, iw); )==" "\n" |
89 | R"==(const uint dst_index = DST_OFF(mb, ic, id, ih, iw); )==" "\n" |
90 | R"==(const DEF_ACC_DATA_T num_elements_div = NUM_ELEMENTS_DIV; )==" "\n" |
91 | R"==(DEF_ACC_DATA_T B = 0; )==" "\n" |
92 | R"==(#if ACROSS_CHANNEL )==" "\n" |
93 | R"==(for (int j = 0; j < LOCAL_SIZE; j++) { )==" "\n" |
94 | R"==(const int z_idx = (j + ic - PADDING); )==" "\n" |
95 | R"==(bool zero = (z_idx < 0 || z_idx >= IC); )==" "\n" |
96 | R"==(if (!zero) { )==" "\n" |
97 | R"==(DEF_ACC_DATA_T val )==" "\n" |
98 | R"==(= TO_DEF_ACC_DATA_T(src[SRC_OFF(mb, z_idx, id, ih, iw)]); )==" "\n" |
99 | R"==(DEF_ACC_DATA_T omega = ws[SRC_OFF(mb, z_idx, id, ih, iw)]; )==" "\n" |
100 | R"==(DEF_ACC_DATA_T tmp = (DEF_ACC_DATA_T)1.0f )==" "\n" |
101 | R"==(/ native_powr(omega, (DEF_ACC_DATA_T)LRN_BETA + 1); )==" "\n" |
102 | R"==(B += tmp * val )==" "\n" |
103 | R"==(* TO_DEF_ACC_DATA_T( )==" "\n" |
104 | R"==(diff_dst[DST_OFF(mb, z_idx, id, ih, iw)]); )==" "\n" |
105 | R"==(} )==" "\n" |
106 | R"==(} )==" "\n" |
107 | R"==(#else )==" "\n" |
108 | R"==(const int d = (int)id - PADDING; )==" "\n" |
109 | R"==(const int h = (int)ih - PADDING; )==" "\n" |
110 | R"==(const int w = (int)iw - PADDING; )==" "\n" |
111 | R"==(const int d_start = max(d, 0); )==" "\n" |
112 | R"==(const int h_start = max(h, 0); )==" "\n" |
113 | R"==(const int w_start = max(w, 0); )==" "\n" |
114 | R"==(const int d_end = min(d + LOCAL_SIZE, ID); )==" "\n" |
115 | R"==(const int h_end = min(h + LOCAL_SIZE, IH); )==" "\n" |
116 | R"==(const int w_end = min(w + LOCAL_SIZE, IW); )==" "\n" |
117 | R"==(for (int k = d_start; k < d_end; ++k) { )==" "\n" |
118 | R"==(for (int j = h_start; j < h_end; ++j) { )==" "\n" |
119 | R"==(for (int i = w_start; i < w_end; ++i) { )==" "\n" |
120 | R"==(int data_off = SRC_OFF(mb, ic, k, j, i); )==" "\n" |
121 | R"==(DEF_ACC_DATA_T val = TO_DEF_ACC_DATA_T(src[data_off]); )==" "\n" |
122 | R"==(DEF_ACC_DATA_T omega = ws[data_off]; )==" "\n" |
123 | R"==(DEF_ACC_DATA_T tmp = (DEF_ACC_DATA_T)1.0f )==" "\n" |
124 | R"==(/ native_powr(omega, (DEF_ACC_DATA_T)(LRN_BETA + 1)); )==" "\n" |
125 | R"==(B += tmp * val * TO_DEF_ACC_DATA_T(diff_dst[data_off]); )==" "\n" |
126 | R"==(} )==" "\n" |
127 | R"==(} )==" "\n" |
128 | R"==(} )==" "\n" |
129 | R"==(#endif )==" "\n" |
130 | R"==(const DEF_ACC_DATA_T A )==" "\n" |
131 | R"==(= native_powr(ws[src_index], (DEF_ACC_DATA_T)-LRN_BETA) )==" "\n" |
132 | R"==(* TO_DEF_ACC_DATA_T(diff_dst[dst_index]); )==" "\n" |
133 | R"==(diff_src[src_index] = TO_DATA_T(A )==" "\n" |
134 | R"==(- TO_DEF_ACC_DATA_T(src[src_index]) * 2 * (DEF_ACC_DATA_T)LRN_ALPHA )==" "\n" |
135 | R"==(* (DEF_ACC_DATA_T)LRN_BETA * num_elements_div * B); )==" "\n" |
136 | R"==(} )==" "\n" |
137 | R"==(#endif )==" "\n" |
138 | R"==()==" ; |
139 | } |
140 | } |
141 | } |
142 | } |