1 | namespace dnnl { |
2 | namespace impl { |
3 | namespace gpu { |
4 | namespace ocl { |
5 | const char *ref_matmul_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"==(#define offset6D(d0, d1, d2, d3, d4, d5, s0, s1, s2, s3, s4, s5) \ )==" "\n" |
23 | R"==(((d0) * (s0) + (d1) * (s1) + (d2) * (s2) + (d3) * (s3) + (d4) * (s4) \ )==" "\n" |
24 | R"==(+ (d5) * (s5)) )==" "\n" |
25 | R"==(__kernel void ref_matmul(__global SRC_DATA_T *A, __global WEI_DATA_T *B, )==" "\n" |
26 | R"==(__global DST_DATA_T *C, __global BIA_DATA_T *bia, __global int *a0, )==" "\n" |
27 | R"==(__global int *b0, __global int *c0, __global float *src_scales, )==" "\n" |
28 | R"==(__global float *wei_scales, long wei_scale_stride, )==" "\n" |
29 | R"==(__global float *dst_scales, long K, long N, long M, long D0, long D1, )==" "\n" |
30 | R"==(long D2, long bia_stride_d3, long bia_stride_d2, long bia_stride_d1, )==" "\n" |
31 | R"==(long bia_stride_d0, long bia_stride_m, long bia_stride_n, )==" "\n" |
32 | R"==(long a_stride_d3, long a_stride_d2, long a_stride_d1, long a_stride_d0, )==" "\n" |
33 | R"==(long a_stride_m, long a_stride_k, long b_stride_d3, long b_stride_d2, )==" "\n" |
34 | R"==(long b_stride_d1, long b_stride_d0, long b_stride_k, long b_stride_n, )==" "\n" |
35 | R"==(long c_stride_d3, long c_stride_d2, long c_stride_d1, long c_stride_d0, )==" "\n" |
36 | R"==(long c_stride_m, long c_stride_n POST_OP_ARGS) { )==" "\n" |
37 | R"==(int n = get_global_id(1); )==" "\n" |
38 | R"==(int mb = get_global_id(2); )==" "\n" |
39 | R"==(#if WITH_SRC_ZPOINTS )==" "\n" |
40 | R"==(int src_zp = a0[0]; )==" "\n" |
41 | R"==(#else )==" "\n" |
42 | R"==(int src_zp = 0; )==" "\n" |
43 | R"==(#endif )==" "\n" |
44 | R"==(#if WITH_WEI_ZPOINTS )==" "\n" |
45 | R"==(int wei_zp = b0[0]; )==" "\n" |
46 | R"==(#else )==" "\n" |
47 | R"==(int wei_zp = 0; )==" "\n" |
48 | R"==(#endif )==" "\n" |
49 | R"==(#if WITH_DST_ZPOINTS )==" "\n" |
50 | R"==(int dst_zp = c0[0]; )==" "\n" |
51 | R"==(#else )==" "\n" |
52 | R"==(int dst_zp = 0; )==" "\n" |
53 | R"==(#endif )==" "\n" |
54 | R"==(long d3 = mb / D0 / D1 / D2; )==" "\n" |
55 | R"==(long d2 = (mb / D0 / D1) % D2; )==" "\n" |
56 | R"==(long d1 = (mb / D0) % D1; )==" "\n" |
57 | R"==(long d0 = mb % D0; )==" "\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 src_off )==" "\n" |
62 | R"==(= offset6D(m, k, d0, d1, d2, d3, a_stride_m, a_stride_k, )==" "\n" |
63 | R"==(a_stride_d0, a_stride_d1, a_stride_d2, a_stride_d3); )==" "\n" |
64 | R"==(long wei_off )==" "\n" |
65 | R"==(= offset6D(k, n, d0, d1, d2, d3, b_stride_k, b_stride_n, )==" "\n" |
66 | R"==(b_stride_d0, b_stride_d1, b_stride_d2, b_stride_d3); )==" "\n" |
67 | R"==(acc += TO_ACC(SRC_TO_REF(A[src_off]) - src_zp) )==" "\n" |
68 | R"==(* TO_ACC(WEI_TO_REF(B[wei_off]) - wei_zp); )==" "\n" |
69 | R"==(} )==" "\n" |
70 | R"==(long dst_off = offset6D(m, n, d0, d1, d2, d3, c_stride_m, c_stride_n, )==" "\n" |
71 | R"==(c_stride_d0, c_stride_d1, c_stride_d2, c_stride_d3); )==" "\n" |
72 | R"==(#if WITH_BIAS || NON_DEFAULT_ATTRS )==" "\n" |
73 | R"==(POST_OP_DATA_T temp = (POST_OP_DATA_T)acc; )==" "\n" |
74 | R"==(#if WITH_SRC_SCALES )==" "\n" |
75 | R"==(temp *= src_scales[0]; )==" "\n" |
76 | R"==(#endif )==" "\n" |
77 | R"==(#if WITH_WEI_SCALES )==" "\n" |
78 | R"==(temp *= wei_scales[wei_scale_stride * n]; )==" "\n" |
79 | R"==(#endif )==" "\n" |
80 | R"==(#if WITH_BIAS )==" "\n" |
81 | R"==(long bia_off = offset6D(m, n, d0, d1, d2, d3, bia_stride_m, )==" "\n" |
82 | R"==(bia_stride_n, bia_stride_d0, bia_stride_d1, bia_stride_d2, )==" "\n" |
83 | R"==(bia_stride_d3); )==" "\n" |
84 | R"==(temp += bia[bia_off]; )==" "\n" |
85 | R"==(#endif )==" "\n" |
86 | R"==(float dst_data; )==" "\n" |
87 | R"==(#if WITH_SUM )==" "\n" |
88 | R"==(dst_data = convert_float(DATA_TO_REF(C[dst_off])); )==" "\n" |
89 | R"==(#endif )==" "\n" |
90 | R"==(float po_acc = convert_float(temp); )==" "\n" |
91 | R"==(if (DST_NDIMS == 2) )==" "\n" |
92 | R"==(APPLY_POST_OPS_SERIAL(po_acc, float, dst_data, float, m, 1, n, 1, 0, )==" "\n" |
93 | R"==(1, 0, 1, 0, 1, 0, 1); )==" "\n" |
94 | R"==(if (DST_NDIMS == 3) )==" "\n" |
95 | R"==(APPLY_POST_OPS_SERIAL(po_acc, float, dst_data, float, d0, 1, m, 1, )==" "\n" |
96 | R"==(n, 1, 0, 1, 0, 1, 0, 1); )==" "\n" |
97 | R"==(if (DST_NDIMS == 4) )==" "\n" |
98 | R"==(APPLY_POST_OPS_SERIAL(po_acc, float, dst_data, float, d1, 1, d0, 1, )==" "\n" |
99 | R"==(m, 1, n, 1, 0, 1, 0, 1); )==" "\n" |
100 | R"==(if (DST_NDIMS == 5) )==" "\n" |
101 | R"==(APPLY_POST_OPS_SERIAL(po_acc, float, dst_data, float, d2, 1, d1, 1, )==" "\n" |
102 | R"==(d0, 1, m, 1, n, 1, 0, 1); )==" "\n" |
103 | R"==(if (DST_NDIMS == 6) )==" "\n" |
104 | R"==(APPLY_POST_OPS_SERIAL(po_acc, float, dst_data, float, d3, 1, d2, 1, )==" "\n" |
105 | R"==(d1, 1, d0, 1, m, 1, n, 1); )==" "\n" |
106 | R"==(#if WITH_DST_SCALES )==" "\n" |
107 | R"==(po_acc /= dst_scales[0]; )==" "\n" |
108 | R"==(#endif )==" "\n" |
109 | R"==(po_acc += dst_zp; )==" "\n" |
110 | R"==(C[dst_off] = TO_DST(po_acc); )==" "\n" |
111 | R"==(#else )==" "\n" |
112 | R"==(C[dst_off] = TO_DST(acc); )==" "\n" |
113 | R"==(#endif )==" "\n" |
114 | R"==(} )==" "\n" |
115 | R"==(} )==" "\n" |
116 | R"==()==" ; |
117 | } |
118 | } |
119 | } |
120 | } |