1namespace dnnl {
2namespace impl {
3namespace gpu {
4namespace ocl {
5const char *ocl_math_utils_header = R"==(/******************************************************************************* )==""\n"
6R"==(* Copyright 2019-2022 Intel Corporation )==""\n"
7R"==(* )==""\n"
8R"==(* Licensed under the Apache License, Version 2.0 (the "License"); )==""\n"
9R"==(* you may not use this file except in compliance with the License. )==""\n"
10R"==(* You may obtain a copy of the License at )==""\n"
11R"==(* )==""\n"
12R"==(* http: )==""\n"
13R"==(* )==""\n"
14R"==(* Unless required by applicable law or agreed to in writing, software )==""\n"
15R"==(* distributed under the License is distributed on an "AS IS" BASIS, )==""\n"
16R"==(* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. )==""\n"
17R"==(* See the License for the specific language governing permissions and )==""\n"
18R"==(* limitations under the License. )==""\n"
19R"==(*******************************************************************************/ )==""\n"
20R"==(#ifndef GPU_OCL_OCL_MATH_UTILS_H )==""\n"
21R"==(#define GPU_OCL_OCL_MATH_UTILS_H )==""\n"
22R"==(#pragma OPENCL EXTENSION cl_khr_fp16 : enable )==""\n"
23R"==(#if DT_BF16 || SRC_DT_BF16 || WEI_DT_BF16 || DST_DT_BF16 || BIA_DT_BF16 \ )==""\n"
24R"==(|| A_DT_BF16 || B_DT_BF16 || C_DT_BF16 || SUM_DT_BF16 \ )==""\n"
25R"==(|| POST_OP_USING_BF16 )==""\n"
26R"==(#define MATH_UTILS_DECLARE_BF16 1 )==""\n"
27R"==(#endif )==""\n"
28R"==(ulong8 __builtin_IB_simd_block_read_8_global_l(const __global ulong *); )==""\n"
29R"==(ushort16 __builtin_IB_simd_block_read_16_global_h(const __global ushort *); )==""\n"
30R"==(void __builtin_IB_simd_block_write_8_global_l(__global ulong *, ulong8); )==""\n"
31R"==(void __builtin_IB_simd_block_write_16_global_h(__global ushort *, ushort16); )==""\n"
32R"==(#if MATH_UTILS_DECLARE_BF16 )==""\n"
33R"==(#ifdef cl_future_bf16_cvt )==""\n"
34R"==(short __builtin_IB_ftobf_1(float a) __attribute__((const)); )==""\n"
35R"==(short2 __builtin_IB_ftobf_2(float2 a) __attribute__((const)); )==""\n"
36R"==(short4 __builtin_IB_ftobf_4(float4 a) __attribute__((const)); )==""\n"
37R"==(short8 __builtin_IB_ftobf_8(float8 a) __attribute__((const)); )==""\n"
38R"==(short16 __builtin_IB_ftobf_16(float16 a) __attribute__((const)); )==""\n"
39R"==(float __builtin_IB_bftof_1(short a) __attribute__((const)); )==""\n"
40R"==(float2 __builtin_IB_bftof_2(short2 a) __attribute__((const)); )==""\n"
41R"==(float4 __builtin_IB_bftof_4(short4 a) __attribute__((const)); )==""\n"
42R"==(float8 __builtin_IB_bftof_8(short8 a) __attribute__((const)); )==""\n"
43R"==(float16 __builtin_IB_bftof_16(short16 a) __attribute__((const)); )==""\n"
44R"==(ushort __attribute__((overloadable)) cvt_f32_to_bf16(float a) { return as_ushort (__builtin_IB_ftobf_1 (a)); } )==""\n"
45R"==(ushort2 __attribute__((overloadable)) cvt_f32_to_bf16(float2 a) { return as_ushort2 (__builtin_IB_ftobf_2 (a)); } )==""\n"
46R"==(ushort4 __attribute__((overloadable)) cvt_f32_to_bf16(float4 a) { return as_ushort4 (__builtin_IB_ftobf_4 (a)); } )==""\n"
47R"==(ushort8 __attribute__((overloadable)) cvt_f32_to_bf16(float8 a) { return as_ushort8 (__builtin_IB_ftobf_8 (a)); } )==""\n"
48R"==(ushort16 __attribute__((overloadable)) cvt_f32_to_bf16(float16 a) { return as_ushort16(__builtin_IB_ftobf_16(a)); } )==""\n"
49R"==(float __attribute__((overloadable)) cvt_bf16_to_f32(ushort a) { return __builtin_IB_bftof_1 (as_short (a)); } )==""\n"
50R"==(float2 __attribute__((overloadable)) cvt_bf16_to_f32(ushort2 a) { return __builtin_IB_bftof_2 (as_short2 (a)); } )==""\n"
51R"==(float4 __attribute__((overloadable)) cvt_bf16_to_f32(ushort4 a) { return __builtin_IB_bftof_4 (as_short4 (a)); } )==""\n"
52R"==(float8 __attribute__((overloadable)) cvt_bf16_to_f32(ushort8 a) { return __builtin_IB_bftof_8 (as_short8 (a)); } )==""\n"
53R"==(float16 __attribute__((overloadable)) cvt_bf16_to_f32(ushort16 a) { return __builtin_IB_bftof_16(as_short16(a)); } )==""\n"
54R"==(#else )==""\n"
55R"==(ushort __attribute__((overloadable)) cvt_f32_to_bf16(float f) { )==""\n"
56R"==(uint i = as_uint(f); )==""\n"
57R"==(i += 0x00007FFF + ((i & 0x10000) >> 16); )==""\n"
58R"==(ushort2 r = as_ushort2(i); )==""\n"
59R"==(return r[1]; )==""\n"
60R"==(} )==""\n"
61R"==(ushort2 __attribute__((overloadable)) cvt_f32_to_bf16(float2 f) { )==""\n"
62R"==(ushort2 r; )==""\n"
63R"==(for (int i = 0; i < 2; i++) { )==""\n"
64R"==(r[i] = cvt_f32_to_bf16(f[i]); )==""\n"
65R"==(} )==""\n"
66R"==(return r; )==""\n"
67R"==(} )==""\n"
68R"==(ushort4 __attribute__((overloadable)) cvt_f32_to_bf16(float4 f) { )==""\n"
69R"==(ushort4 r; )==""\n"
70R"==(for (int i = 0; i < 4; i++) { )==""\n"
71R"==(r[i] = cvt_f32_to_bf16(f[i]); )==""\n"
72R"==(} )==""\n"
73R"==(return r; )==""\n"
74R"==(} )==""\n"
75R"==(ushort8 __attribute__((overloadable)) cvt_f32_to_bf16(float8 f) { )==""\n"
76R"==(ushort8 r; )==""\n"
77R"==(for (int i = 0; i < 8; i++) { )==""\n"
78R"==(r[i] = cvt_f32_to_bf16(f[i]); )==""\n"
79R"==(} )==""\n"
80R"==(return r; )==""\n"
81R"==(} )==""\n"
82R"==(ushort16 __attribute__((overloadable)) cvt_f32_to_bf16(float16 f) { )==""\n"
83R"==(ushort16 r; )==""\n"
84R"==(for (int i = 0; i < 16; i++) { )==""\n"
85R"==(r[i] = cvt_f32_to_bf16(f[i]); )==""\n"
86R"==(} )==""\n"
87R"==(return r; )==""\n"
88R"==(} )==""\n"
89R"==(float __attribute__((overloadable)) cvt_bf16_to_f32(ushort b) { )==""\n"
90R"==(ushort2 r = {0, b}; )==""\n"
91R"==(float f = as_float(r); )==""\n"
92R"==(return f; )==""\n"
93R"==(} )==""\n"
94R"==(float2 __attribute__((overloadable)) cvt_bf16_to_f32(ushort2 b) { )==""\n"
95R"==(float2 f; )==""\n"
96R"==(for (int i = 0; i < 2; i++) { )==""\n"
97R"==(f[i] = cvt_bf16_to_f32(b[i]); )==""\n"
98R"==(} )==""\n"
99R"==(return f; )==""\n"
100R"==(} )==""\n"
101R"==(float4 __attribute__((overloadable)) cvt_bf16_to_f32(ushort4 b) { )==""\n"
102R"==(float4 f; )==""\n"
103R"==(for (int i = 0; i < 4; i++) { )==""\n"
104R"==(f[i] = cvt_bf16_to_f32(b[i]); )==""\n"
105R"==(} )==""\n"
106R"==(return f; )==""\n"
107R"==(} )==""\n"
108R"==(float8 __attribute__((overloadable)) cvt_bf16_to_f32(ushort8 b) { )==""\n"
109R"==(float8 f; )==""\n"
110R"==(for (int i = 0; i < 8; i++) { )==""\n"
111R"==(f[i] = cvt_bf16_to_f32(b[i]); )==""\n"
112R"==(} )==""\n"
113R"==(return f; )==""\n"
114R"==(} )==""\n"
115R"==(float16 __attribute__((overloadable)) cvt_bf16_to_f32(ushort16 b) { )==""\n"
116R"==(float16 f; )==""\n"
117R"==(for (int i = 0; i < 16; i++) { )==""\n"
118R"==(f[i] = cvt_bf16_to_f32(b[i]); )==""\n"
119R"==(} )==""\n"
120R"==(return f; )==""\n"
121R"==(} )==""\n"
122R"==(#endif )==""\n"
123R"==(#endif )==""\n"
124R"==(int __attribute__((overloadable)) idot4(char4 a, char4 b, int c) { )==""\n"
125R"==(c += a[0] * b[0]; )==""\n"
126R"==(c += a[1] * b[1]; )==""\n"
127R"==(c += a[2] * b[2]; )==""\n"
128R"==(c += a[3] * b[3]; )==""\n"
129R"==(return c; )==""\n"
130R"==(} )==""\n"
131R"==(int __attribute__((overloadable)) idot4(uchar4 a, uchar4 b, int c) { )==""\n"
132R"==(c += a[0] * b[0]; )==""\n"
133R"==(c += a[1] * b[1]; )==""\n"
134R"==(c += a[2] * b[2]; )==""\n"
135R"==(c += a[3] * b[3]; )==""\n"
136R"==(return c; )==""\n"
137R"==(} )==""\n"
138R"==(int __attribute__((overloadable)) idot4(char4 a, uchar4 b, int c) { )==""\n"
139R"==(c += a[0] * b[0]; )==""\n"
140R"==(c += a[1] * b[1]; )==""\n"
141R"==(c += a[2] * b[2]; )==""\n"
142R"==(c += a[3] * b[3]; )==""\n"
143R"==(return c; )==""\n"
144R"==(} )==""\n"
145R"==(int __attribute__((overloadable)) idot4(uchar4 a, char4 b, int c) { )==""\n"
146R"==(c += a[0] * b[0]; )==""\n"
147R"==(c += a[1] * b[1]; )==""\n"
148R"==(c += a[2] * b[2]; )==""\n"
149R"==(c += a[3] * b[3]; )==""\n"
150R"==(return c; )==""\n"
151R"==(} )==""\n"
152R"==(int __attribute__((overloadable)) idot4(int a, int b, int c) { )==""\n"
153R"==(return idot4(as_char4(a), as_char4(b), c); )==""\n"
154R"==(} )==""\n"
155R"==(int __attribute__((overloadable)) idot4(uint a, int b, int c) { )==""\n"
156R"==(return idot4(as_uchar4(a), as_char4(b), c); )==""\n"
157R"==(} )==""\n"
158R"==(float __attribute__((overloadable)) f16_dot2(int a, int b, float c) { )==""\n"
159R"==(half2 _a = as_half2(a); )==""\n"
160R"==(half2 _b = as_half2(b); )==""\n"
161R"==(return c + _a[0] * _b[0] + _a[1] * _b[1]; )==""\n"
162R"==(} )==""\n"
163R"==(#if MATH_UTILS_DECLARE_BF16 )==""\n"
164R"==(float __attribute__((overloadable)) bf16_dot2(int a, int b, float c) { )==""\n"
165R"==(ushort2 _a = as_ushort2(a); )==""\n"
166R"==(ushort2 _b = as_ushort2(b); )==""\n"
167R"==(c += cvt_bf16_to_f32(_a[0]) * cvt_bf16_to_f32(_b[0]); )==""\n"
168R"==(c += cvt_bf16_to_f32(_a[1]) * cvt_bf16_to_f32(_b[1]); )==""\n"
169R"==(return c; )==""\n"
170R"==(} )==""\n"
171R"==(#endif )==""\n"
172R"==(#define DECLARE_BLOCK_READ(suffix, func, data_type, addr_space, p_type) \ )==""\n"
173R"==(data_type __attribute__((overloadable)) \ )==""\n"
174R"==(block_read##suffix(const addr_space p_type *p) { \ )==""\n"
175R"==(return func(p); \ )==""\n"
176R"==(} )==""\n"
177R"==(#define DECLARE_BLOCK_READ_EMU(suffix, data_type, addr_space, p_type) \ )==""\n"
178R"==(data_type __attribute__((overloadable)) \ )==""\n"
179R"==(block_read##suffix##_emu(const addr_space p_type *p) { \ )==""\n"
180R"==(data_type ret; \ )==""\n"
181R"==(uint idx = get_sub_group_local_id(); \ )==""\n"
182R"==(for (int i = 0; i < sizeof(data_type) / sizeof(p_type); i++) { \ )==""\n"
183R"==(((p_type *)&ret)[i] = p[idx]; \ )==""\n"
184R"==(idx += get_max_sub_group_size(); \ )==""\n"
185R"==(} \ )==""\n"
186R"==(return ret; \ )==""\n"
187R"==(} )==""\n"
188R"==(#define DECLARE_BLOCK_WRITE(suffix, func, data_type, addr_space, p_type) \ )==""\n"
189R"==(void __attribute__((overloadable)) \ )==""\n"
190R"==(block_write##suffix(addr_space p_type *p, data_type data) { \ )==""\n"
191R"==(func(p, data); \ )==""\n"
192R"==(} )==""\n"
193R"==(#define DECLARE_BLOCK_WRITE_EMU(suffix, data_type, addr_space, p_type) \ )==""\n"
194R"==(void __attribute__((overloadable)) \ )==""\n"
195R"==(block_write##suffix##_emu(addr_space p_type *p, data_type data) { \ )==""\n"
196R"==(uint idx = get_sub_group_local_id(); \ )==""\n"
197R"==(for (int i = 0; i < sizeof(data_type) / sizeof(p_type); i++) { \ )==""\n"
198R"==(p[idx] = ((p_type *)&data)[i]; \ )==""\n"
199R"==(p += get_max_sub_group_size(); \ )==""\n"
200R"==(} \ )==""\n"
201R"==(} )==""\n"
202R"==(DECLARE_BLOCK_READ(, intel_sub_group_block_read, uint, __global, uint) )==""\n"
203R"==(DECLARE_BLOCK_READ(2, intel_sub_group_block_read2, uint2, __global, uint) )==""\n"
204R"==(DECLARE_BLOCK_READ(4, intel_sub_group_block_read4, uint4, __global, uint) )==""\n"
205R"==(DECLARE_BLOCK_READ(8, intel_sub_group_block_read8, uint8, __global, uint) )==""\n"
206R"==(DECLARE_BLOCK_WRITE(, intel_sub_group_block_write, uint, __global, uint) )==""\n"
207R"==(DECLARE_BLOCK_WRITE(2, intel_sub_group_block_write2, uint2, __global, uint) )==""\n"
208R"==(DECLARE_BLOCK_WRITE(4, intel_sub_group_block_write4, uint4, __global, uint) )==""\n"
209R"==(DECLARE_BLOCK_WRITE(8, intel_sub_group_block_write8, uint8, __global, uint) )==""\n"
210R"==(#ifdef cl_intel_subgroups_char )==""\n"
211R"==(void __attribute__((overloadable)) )==""\n"
212R"==(intel_sub_group_block_write_uc16(__global uchar *p, uchar16 data); )==""\n"
213R"==(uchar16 __attribute__((overloadable)) )==""\n"
214R"==(intel_sub_group_block_read_uc16(const __global uchar *p); )==""\n"
215R"==(#endif )==""\n"
216R"==(DECLARE_BLOCK_READ_EMU(, uint, __local, uint) )==""\n"
217R"==(DECLARE_BLOCK_READ_EMU(2, uint2, __local, uint) )==""\n"
218R"==(DECLARE_BLOCK_READ_EMU(4, uint4, __local, uint) )==""\n"
219R"==(DECLARE_BLOCK_READ_EMU(8, uint8, __local, uint) )==""\n"
220R"==(DECLARE_BLOCK_WRITE_EMU(, uint, __local, uint) )==""\n"
221R"==(DECLARE_BLOCK_WRITE_EMU(2, uint2, __local, uint) )==""\n"
222R"==(DECLARE_BLOCK_WRITE_EMU(4, uint4, __local, uint) )==""\n"
223R"==(DECLARE_BLOCK_WRITE_EMU(8, uint8, __local, uint) )==""\n"
224R"==(DECLARE_BLOCK_WRITE_EMU(_us, ushort, __local, ushort) )==""\n"
225R"==(DECLARE_BLOCK_WRITE_EMU(_us2, ushort2, __local, ushort) )==""\n"
226R"==(DECLARE_BLOCK_WRITE_EMU(_us4, ushort4, __local, ushort) )==""\n"
227R"==(DECLARE_BLOCK_WRITE_EMU(_us8, ushort8, __local, ushort) )==""\n"
228R"==(#ifdef cl_intel_subgroup_local_block_io )==""\n"
229R"==(DECLARE_BLOCK_READ(, intel_sub_group_block_read, uint, __local, uint) )==""\n"
230R"==(DECLARE_BLOCK_READ(2, intel_sub_group_block_read2, uint2, __local, uint) )==""\n"
231R"==(DECLARE_BLOCK_READ(4, intel_sub_group_block_read4, uint4, __local, uint) )==""\n"
232R"==(DECLARE_BLOCK_READ(8, intel_sub_group_block_read8, uint8, __local, uint) )==""\n"
233R"==(DECLARE_BLOCK_WRITE(, intel_sub_group_block_write, uint, __local, uint) )==""\n"
234R"==(DECLARE_BLOCK_WRITE(2, intel_sub_group_block_write2, uint2, __local, uint) )==""\n"
235R"==(DECLARE_BLOCK_WRITE(4, intel_sub_group_block_write4, uint4, __local, uint) )==""\n"
236R"==(DECLARE_BLOCK_WRITE(8, intel_sub_group_block_write8, uint8, __local, uint) )==""\n"
237R"==(DECLARE_BLOCK_WRITE( )==""\n"
238R"==(_us, intel_sub_group_block_write_us, ushort, __local, ushort) )==""\n"
239R"==(#else )==""\n"
240R"==(DECLARE_BLOCK_READ(, block_read_emu, uint, __local, uint) )==""\n"
241R"==(DECLARE_BLOCK_READ(2, block_read2_emu, uint2, __local, uint) )==""\n"
242R"==(DECLARE_BLOCK_READ(4, block_read4_emu, uint4, __local, uint) )==""\n"
243R"==(DECLARE_BLOCK_READ(8, block_read8_emu, uint8, __local, uint) )==""\n"
244R"==(DECLARE_BLOCK_WRITE(, block_write_emu, uint, __local, uint) )==""\n"
245R"==(DECLARE_BLOCK_WRITE(2, block_write2_emu, uint2, __local, uint) )==""\n"
246R"==(DECLARE_BLOCK_WRITE(4, block_write4_emu, uint4, __local, uint) )==""\n"
247R"==(DECLARE_BLOCK_WRITE(8, block_write8_emu, uint8, __local, uint) )==""\n"
248R"==(DECLARE_BLOCK_WRITE(_us, block_write_us_emu, ushort, __local, ushort) )==""\n"
249R"==(#endif )==""\n"
250R"==(#define DECLARE_MMAD_EMU(name, dot, K, m, a_type, b_type, acc_type) \ )==""\n"
251R"==(acc_type __attribute__((overloadable)) \ )==""\n"
252R"==(name(a_type A_vectors, b_type B_vectors, acc_type acc) { \ )==""\n"
253R"==(for (uint i = 0; i < (m); ++i) { \ )==""\n"
254R"==(for (uint j = 0; j < (K); ++j) \ )==""\n"
255R"==(acc[i] = dot(sub_group_broadcast(A_vectors[i], j), \ )==""\n"
256R"==(B_vectors[j], acc[i]); \ )==""\n"
257R"==(} \ )==""\n"
258R"==(return acc; \ )==""\n"
259R"==(} )==""\n"
260R"==(DECLARE_MMAD_EMU(mmad8x4, idot4, 8, 4, uint4, int8, int4) )==""\n"
261R"==(DECLARE_MMAD_EMU(mmad8x4, idot4, 8, 4, int4, int8, int4) )==""\n"
262R"==(DECLARE_MMAD_EMU(mmad8x8, idot4, 8, 8, uint8, int8, int8) )==""\n"
263R"==(DECLARE_MMAD_EMU(mmad8x8, idot4, 8, 8, int8, int8, int8) )==""\n"
264R"==(DECLARE_MMAD_EMU(mmad8x8, idot4, 8, 8, ushort8, int8, int8) )==""\n"
265R"==(DECLARE_MMAD_EMU(mmad8x8, idot4, 8, 8, short8, int8, int8) )==""\n"
266R"==(DECLARE_MMAD_EMU(mmad8x4_f16, f16_dot2, 8, 4, uint4, int8, float4) )==""\n"
267R"==(DECLARE_MMAD_EMU(mmad8x4_f16, f16_dot2, 8, 4, short4, int8, float4) )==""\n"
268R"==(DECLARE_MMAD_EMU(mmad8x8_f16, f16_dot2, 8, 8, uint8, int8, float8) )==""\n"
269R"==(DECLARE_MMAD_EMU(mmad8x8_f16, f16_dot2, 8, 8, short8, int8, float8) )==""\n"
270R"==(#if MATH_UTILS_DECLARE_BF16 )==""\n"
271R"==(DECLARE_MMAD_EMU(mmad8x4_bf16, bf16_dot2, 8, 4, uint4, int8, float4) )==""\n"
272R"==(DECLARE_MMAD_EMU(mmad8x8_bf16, bf16_dot2, 8, 8, uint8, int8, float8) )==""\n"
273R"==(DECLARE_MMAD_EMU(mmad8x4_bf16, bf16_dot2, 8, 4, ushort4, int8, float4) )==""\n"
274R"==(DECLARE_MMAD_EMU(mmad8x8_bf16, bf16_dot2, 8, 8, ushort8, int8, float8) )==""\n"
275R"==(DECLARE_MMAD_EMU(mmad8x8_bf16, bf16_dot2, 8, 8, short8, int8, float8) )==""\n"
276R"==(#endif )==""\n"
277R"==(#if __OPENCL_C_VERSION__ >= 200 )==""\n"
278R"==(#ifdef cl_intel_global_float_atomics )==""\n"
279R"==(inline void atomic_add_global( )==""\n"
280R"==(volatile global atomic_float *source, float operand) { )==""\n"
281R"==(atomic_fetch_add_explicit(source, operand, memory_order_relaxed); )==""\n"
282R"==(} )==""\n"
283R"==(#else )==""\n"
284R"==(inline void atomic_add_global( )==""\n"
285R"==(volatile __global atomic_float *source, float operand) { )==""\n"
286R"==(float old_val = atomic_load_explicit( )==""\n"
287R"==(source, memory_order_relaxed, memory_scope_device); )==""\n"
288R"==(bool success = false; )==""\n"
289R"==(do { )==""\n"
290R"==(float new_val = old_val + operand; )==""\n"
291R"==(success = atomic_compare_exchange_strong_explicit(source, &old_val, )==""\n"
292R"==(new_val, memory_order_acq_rel, memory_order_relaxed, )==""\n"
293R"==(memory_scope_device); )==""\n"
294R"==(} while (!success); )==""\n"
295R"==(} )==""\n"
296R"==(#endif )==""\n"
297R"==(#endif )==""\n"
298R"==(#endif )==""\n"
299R"==()==";
300}
301}
302}
303}