1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_gemm_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/gemm/ocl_gemm_attrs.h" )==" "\n" |
21 | R"==(#include "gpu/ocl/ocl_post_ops.h" )==" "\n" |
22 | R"==(#include "gpu/ocl/ocl_types.h" )==" "\n" |
23 | R"==(void get_strides(int mask, long dim0, long dim1, long dim2, long *str0, )==" "\n" |
24 | R"==(long *str1, long *str2) { )==" "\n" |
25 | R"==(int is_3d = dim0 > 1; )==" "\n" |
26 | R"==(long dims[3]; )==" "\n" |
27 | R"==(dims[0] = (is_3d && mask & (1 << 0)) ? dim0 : 1; )==" "\n" |
28 | R"==(dims[1] = mask & (1 << 1) ? dim1 : 1; )==" "\n" |
29 | R"==(dims[2] = mask & (1 << 2) ? dim2 : 1; )==" "\n" |
30 | R"==(*str0 = dims[0] == 1 ? 0 : dims[1] * dims[2]; )==" "\n" |
31 | R"==(*str1 = dims[1] == 1 ? 0 : 1; )==" "\n" |
32 | R"==(*str2 = dims[2] == 1 ? 0 : dims[1]; )==" "\n" |
33 | R"==(} )==" "\n" |
34 | R"==(__kernel void ref_gemm(__global A_DATA_T *a, __global B_DATA_T *b, )==" "\n" |
35 | R"==(__global C_DATA_T *c, __global BIA_DATA_T *bias, long offset_a0, )==" "\n" |
36 | R"==(long offset_b0, long offset_c0, long offset_bias0, int transa, )==" "\n" |
37 | R"==(int transb, long MB, long M, long N, long K, long stride_a, )==" "\n" |
38 | R"==(long stride_b, long stride_c, long lda, long ldb, long ldc, )==" "\n" |
39 | R"==(float eltwise_alpha, float eltwise_beta, float eltwise_scale, )==" "\n" |
40 | R"==(int bias_mask, __global int *ao, __global int *bo, __global int *c0, )==" "\n" |
41 | R"==(int c0_mask, __global float *scales, long scale_stride, float beta) { )==" "\n" |
42 | R"==(int n = get_global_id(1); )==" "\n" |
43 | R"==(int mb = get_global_id(2); )==" "\n" |
44 | R"==(#if WITH_BIAS )==" "\n" |
45 | R"==(bias += offset_bias0; )==" "\n" |
46 | R"==(long b_strides[3]; )==" "\n" |
47 | R"==(get_strides( )==" "\n" |
48 | R"==(bias_mask, MB, M, N, &b_strides[0], &b_strides[1], &b_strides[2]); )==" "\n" |
49 | R"==(#endif )==" "\n" |
50 | R"==(a += offset_a0; )==" "\n" |
51 | R"==(b += offset_b0; )==" "\n" |
52 | R"==(c += offset_c0; )==" "\n" |
53 | R"==(#if WITH_DST_ZPOINTS )==" "\n" |
54 | R"==(long c0_strides[3]; )==" "\n" |
55 | R"==(get_strides( )==" "\n" |
56 | R"==(c0_mask, MB, M, N, &c0_strides[0], &c0_strides[1], &c0_strides[2]); )==" "\n" |
57 | R"==(#endif )==" "\n" |
58 | R"==(for (long m = 0; m < M; ++m) { )==" "\n" |
59 | R"==(ACC_DATA_T acc = 0; )==" "\n" |
60 | R"==(for (long k = 0; k < K; ++k) { )==" "\n" |
61 | R"==(long off_a = mb * stride_a + (transa ? m * lda + k : k * lda + m); )==" "\n" |
62 | R"==(long off_b = mb * stride_b + (transb ? k * ldb + n : n * ldb + k); )==" "\n" |
63 | R"==(acc += TO_ACC(A_TO_REF(a[off_a]) - ATTR_A0) )==" "\n" |
64 | R"==(* TO_ACC(B_TO_REF(b[off_b]) - ATTR_B0); )==" "\n" |
65 | R"==(} )==" "\n" |
66 | R"==(long off_c = mb * stride_c + n * ldc + m; )==" "\n" |
67 | R"==(#if WITH_BIAS || NON_DEFAULT_ATTRS )==" "\n" |
68 | R"==(POST_OP_DATA_T temp = (POST_OP_DATA_T)acc; )==" "\n" |
69 | R"==(#if WITH_BIAS )==" "\n" |
70 | R"==(long off_bias = mb * b_strides[0] + m * b_strides[1] + n * b_strides[2]; )==" "\n" |
71 | R"==(temp += BIA_TO_REF(bias[off_bias]); )==" "\n" |
72 | R"==(#endif )==" "\n" |
73 | R"==(#if WITH_SCALES )==" "\n" |
74 | R"==(temp *= scales[scale_stride * n]; )==" "\n" |
75 | R"==(#endif )==" "\n" |
76 | R"==(#if WITH_SUM )==" "\n" |
77 | R"==(temp += (POST_OP_DATA_T)(beta * C_TO_REF(c[off_c])); )==" "\n" |
78 | R"==(#endif )==" "\n" |
79 | R"==(#if WITH_ELTWISE )==" "\n" |
80 | R"==(temp = fwd_eltwise(temp, eltwise_alpha, eltwise_beta, eltwise_scale); )==" "\n" |
81 | R"==(#endif )==" "\n" |
82 | R"==(#if WITH_DST_ZPOINTS )==" "\n" |
83 | R"==(long off_c0 )==" "\n" |
84 | R"==(= mb * c0_strides[0] + m * c0_strides[1] + n * c0_strides[2]; )==" "\n" |
85 | R"==(temp += c0[off_c0]; )==" "\n" |
86 | R"==(#endif )==" "\n" |
87 | R"==(c[off_c] = TO_C(temp); )==" "\n" |
88 | R"==(#else )==" "\n" |
89 | R"==(c[off_c] = TO_C(acc); )==" "\n" |
90 | R"==(#endif )==" "\n" |
91 | R"==(} )==" "\n" |
92 | R"==(} )==" "\n" |
93 | R"==()==" ; |
94 | } |
95 | } |
96 | } |
97 | } |