1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *gemm_with_post_ops_kernel = R"==(/******************************************************************************* )==" "\n" |
6 | R"==(* Copyright 2021-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_math_utils.h" )==" "\n" |
21 | R"==(#include "gpu/ocl/ocl_post_ops.h" )==" "\n" |
22 | R"==(#include "gpu/ocl/ocl_types.h" )==" "\n" |
23 | R"==(#if defined(DST_DT_BF16) )==" "\n" |
24 | R"==(#define DST_TO_ACC(x) cvt_bf16_to_f32(x) )==" "\n" |
25 | R"==(#else )==" "\n" |
26 | R"==(#define DST_TO_ACC(x) (x) )==" "\n" |
27 | R"==(#endif )==" "\n" |
28 | R"==(#if defined(BIA_DT_BF16) )==" "\n" |
29 | R"==(#define BIA_TO_ACC(x) cvt_bf16_to_f32(x) )==" "\n" |
30 | R"==(#else )==" "\n" |
31 | R"==(#define BIA_TO_ACC(x) (x) )==" "\n" |
32 | R"==(#endif )==" "\n" |
33 | R"==(#if defined(SRC_DT_BF16) )==" "\n" |
34 | R"==(#define SRC_TO_ACC(x) cvt_bf16_to_f32(x) )==" "\n" |
35 | R"==(#else )==" "\n" |
36 | R"==(#define SRC_TO_ACC(x) (x) )==" "\n" |
37 | R"==(#endif )==" "\n" |
38 | R"==(#ifndef BIA_D2 )==" "\n" |
39 | R"==(#define BIA_D2 1 )==" "\n" |
40 | R"==(#endif )==" "\n" |
41 | R"==(#ifndef BIA_D3 )==" "\n" |
42 | R"==(#define BIA_D3 1 )==" "\n" |
43 | R"==(#endif )==" "\n" |
44 | R"==(#if BIA_NDIMS == 4 )==" "\n" |
45 | R"==(#define BIA_OFF(x0, x1, d, h, w) \ )==" "\n" |
46 | R"==((((x0 % BIA_D0) % BIA_B0) * BIA_SB0 + ((x0 % BIA_D0) / BIA_B0) * BIA_S0 \ )==" "\n" |
47 | R"==(+ ((x1 % BIA_D1) % BIA_B1) * BIA_SB1 \ )==" "\n" |
48 | R"==(+ ((x1 % BIA_D1) / BIA_B1) * BIA_S1 \ )==" "\n" |
49 | R"==(+ ((h % BIA_D2) % BIA_B2) * BIA_SB2 \ )==" "\n" |
50 | R"==(+ ((h % BIA_D2) / BIA_B2) * BIA_S2 \ )==" "\n" |
51 | R"==(+ ((w % BIA_D3) % BIA_B3) * BIA_SB3 \ )==" "\n" |
52 | R"==(+ ((w % BIA_D3) / BIA_B3) * BIA_S3) )==" "\n" |
53 | R"==(#elif BIA_NDIMS == 3 )==" "\n" |
54 | R"==(#define BIA_OFF(x0, x1, d, h, w) \ )==" "\n" |
55 | R"==((((x0 % BIA_D0) % BIA_B0) * BIA_SB0 + ((x0 % BIA_D0) / BIA_B0) * BIA_S0 \ )==" "\n" |
56 | R"==(+ ((x1 % BIA_D1) % BIA_B1) * BIA_SB1 \ )==" "\n" |
57 | R"==(+ ((x1 % BIA_D1) / BIA_B1) * BIA_S1 \ )==" "\n" |
58 | R"==(+ ((w % BIA_D2) % BIA_B2) * BIA_SB2 \ )==" "\n" |
59 | R"==(+ ((w % BIA_D2) / BIA_B2) * BIA_S2) )==" "\n" |
60 | R"==(#elif BIA_NDIMS == 2 )==" "\n" |
61 | R"==(#define BIA_OFF(x0, x1, d, h, w) \ )==" "\n" |
62 | R"==((((x0 % BIA_D0) % BIA_B0) * BIA_SB0 + ((x0 % BIA_D0) / BIA_B0) * BIA_S0 \ )==" "\n" |
63 | R"==(+ ((x1 % BIA_D1) % BIA_B1) * BIA_SB1 \ )==" "\n" |
64 | R"==(+ ((x1 % BIA_D1) / BIA_B1) * BIA_S1) )==" "\n" |
65 | R"==(#elif BIA_NDIMS == 1 )==" "\n" |
66 | R"==(#define BIA_OFF(x1, x0, d, h, w) (x0) )==" "\n" |
67 | R"==(#endif )==" "\n" |
68 | R"==(__kernel void gemm_post_ops(__global SRC_DATA_T *src, __global BIA_DATA_T *bias, )==" "\n" |
69 | R"==(__global DST_DATA_T *dst POST_OP_ARGS, __global SPAD_DATA_T *scratchpad, )==" "\n" |
70 | R"==(global float *a_scales, global float *b_scales, global float *c_scales, )==" "\n" |
71 | R"==(int scale_stride) { )==" "\n" |
72 | R"==(const uint d0 = GWS_GET_D0(); )==" "\n" |
73 | R"==(const uint d1 = GWS_GET_D1(); )==" "\n" |
74 | R"==(const uint d2 = GWS_GET_D2(); )==" "\n" |
75 | R"==(const uint d3 = GWS_GET_D3(); )==" "\n" |
76 | R"==(#if NDIMS == 4 )==" "\n" |
77 | R"==(size_t data_idx = DST_OFF(d0, d1, 0, d2, d3); )==" "\n" |
78 | R"==(#elif NDIMS == 3 )==" "\n" |
79 | R"==(size_t data_idx = DST_OFF(d0, d1, 0, 0, d2); )==" "\n" |
80 | R"==(#else )==" "\n" |
81 | R"==(size_t data_idx = DST_OFF(d0, d1, 0, 0, 0); )==" "\n" |
82 | R"==(#endif )==" "\n" |
83 | R"==(#if USE_TEMP_DST == 1 )==" "\n" |
84 | R"==(ACC_DATA_T acc = SRC_TO_ACC(scratchpad[data_idx]); )==" "\n" |
85 | R"==(#else )==" "\n" |
86 | R"==(ACC_DATA_T acc = SRC_TO_ACC(src[data_idx]); )==" "\n" |
87 | R"==(#endif )==" "\n" |
88 | R"==(float accumulator = acc; )==" "\n" |
89 | R"==(if ((d0 == D0_WO_PADDING && d1 == D1_WO_PADDING && d2 == D2_WO_PADDING )==" "\n" |
90 | R"==(&& d3 == D3_WO_PADDING) )==" "\n" |
91 | R"==(|| (d0 < D0_WO_PADDING && d1 < D1_WO_PADDING && d2 < D2_WO_PADDING )==" "\n" |
92 | R"==(&& d3 < D3_WO_PADDING)) { )==" "\n" |
93 | R"==(#if A_SCALES || B_SCALES )==" "\n" |
94 | R"==(#define A_SCALE (A_SCALES ? a_scales[0] : 1) )==" "\n" |
95 | R"==(#if NDIMS == 2 )==" "\n" |
96 | R"==(const float b_scale = B_SCALES ? b_scales[scale_stride * d1] : 1; )==" "\n" |
97 | R"==(#elif NDIMS == 3 )==" "\n" |
98 | R"==(const float b_scale = B_SCALES ? b_scales[scale_stride * d2] : 1; )==" "\n" |
99 | R"==(#elif NDIMS == 4 )==" "\n" |
100 | R"==(const float b_scale = B_SCALES ? b_scales[scale_stride * d3] : 1; )==" "\n" |
101 | R"==(#endif )==" "\n" |
102 | R"==(acc *= A_SCALE * b_scale; )==" "\n" |
103 | R"==(#endif )==" "\n" |
104 | R"==(#if WITH_BIAS == 1 )==" "\n" |
105 | R"==(#if NDIMS == 4 )==" "\n" |
106 | R"==(size_t bia_idx = BIA_OFF(d0, d1, 0, d2, d3); )==" "\n" |
107 | R"==(#elif NDIMS == 3 )==" "\n" |
108 | R"==(size_t bia_idx = BIA_OFF(d0, d1, 0, 0, d2); )==" "\n" |
109 | R"==(#else )==" "\n" |
110 | R"==(size_t bia_idx = BIA_OFF(d0, d1, 0, 0, 0); )==" "\n" |
111 | R"==(#endif )==" "\n" |
112 | R"==(acc += BIA_TO_ACC(bias[bia_idx]); )==" "\n" |
113 | R"==(#endif )==" "\n" |
114 | R"==(float sum_src = 0.0f; )==" "\n" |
115 | R"==(#if WITH_SUM )==" "\n" |
116 | R"==(sum_src = DST_TO_ACC(dst[data_idx]); )==" "\n" |
117 | R"==(#endif )==" "\n" |
118 | R"==(accumulator = acc; )==" "\n" |
119 | R"==(#if NDIMS == 2 )==" "\n" |
120 | R"==(APPLY_POST_OPS_SERIAL(accumulator, float, sum_src, float, d0, 1, d1, 1, )==" "\n" |
121 | R"==(0, 1, 0, 1, 0, 1, 0, 1); )==" "\n" |
122 | R"==(#elif NDIMS == 3 )==" "\n" |
123 | R"==(APPLY_POST_OPS_SERIAL(accumulator, float, sum_src, float, d0, 1, d1, 1, )==" "\n" |
124 | R"==(d2, 1, 0, 1, 0, 1, 0, 1); )==" "\n" |
125 | R"==(#elif NDIMS == 4 )==" "\n" |
126 | R"==(APPLY_POST_OPS_SERIAL(accumulator, float, sum_src, float, d0, 1, d1, 1, )==" "\n" |
127 | R"==(d2, 1, d3, 1, 0, 1, 0, 1); )==" "\n" |
128 | R"==(#endif )==" "\n" |
129 | R"==(} )==" "\n" |
130 | R"==(#if C_SCALES )==" "\n" |
131 | R"==(accumulator /= c_scales[0]; )==" "\n" |
132 | R"==(#endif )==" "\n" |
133 | R"==(dst[data_idx] = TO_DST(accumulator); )==" "\n" |
134 | R"==(} )==" "\n" |
135 | R"==()==" ; |
136 | } |
137 | } |
138 | } |
139 | } |