Skip to content

Commit 55bf1a0

Browse files
authored
[ESIMD] Implement the new non-experimental low-level API for DPAS (#6834)
* The new DPAS API are added to the new esimd::xmx (Xe Matrix eXtension) namespace. * The old/experimental DPAS API is marked as deprecated and now it simply calls the new DPAS API. * The DPAS emulation sequences has got the automatic detection of the execution size instead of being defined through the macro ESIMD_XE_HPC. Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent f9d8059 commit 55bf1a0

File tree

7 files changed

+454
-369
lines changed

7 files changed

+454
-369
lines changed

sycl/include/sycl/ext/intel/esimd.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include <sycl/ext/intel/esimd/detail/half_type_traits.hpp>
8686
#include <sycl/ext/intel/esimd/simd.hpp>
8787
#include <sycl/ext/intel/esimd/simd_view.hpp>
88+
#include <sycl/ext/intel/esimd/xmx/dpas.hpp>
8889
#include <sycl/ext/intel/experimental/esimd/kernel_properties.hpp>
8990
#include <sycl/ext/intel/experimental/esimd/math.hpp>
9091
#include <sycl/ext/intel/experimental/esimd/memory.hpp>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//==-------------- xmx/common.hpp - DPC++ Explicit SIMD API ----------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Explicit SIMD API types used in ESIMD Intel Xe Matrix eXtension.
9+
//===----------------------------------------------------------------------===//
10+
11+
#pragma once
12+
13+
#include <sycl/detail/defines_elementary.hpp>
14+
15+
namespace sycl {
16+
__SYCL_INLINE_VER_NAMESPACE(_V1) {
17+
namespace ext::intel::esimd::xmx {
18+
19+
enum class dpas_argument_type {
20+
Invalid = 0,
21+
u1 = 1, // unsigned 1 bit
22+
U1 __SYCL_DEPRECATED("use u1") = u1,
23+
s1 = 2, // signed 1 bit
24+
S1 __SYCL_DEPRECATED("use s1") = s1,
25+
u2 = 3, // unsigned 2 bits
26+
U2 __SYCL_DEPRECATED("use u2") = u2,
27+
s2 = 4, // signed 2 bits
28+
S2 __SYCL_DEPRECATED("use s2") = s2,
29+
u4 = 5, // unsigned 4 bits
30+
U4 __SYCL_DEPRECATED("use u4") = u4,
31+
s4 = 6, // signed 4 bits
32+
S4 __SYCL_DEPRECATED("use s4") = s4,
33+
u8 = 7, // unsigned 8 bits
34+
U8 __SYCL_DEPRECATED("use u8") = u8,
35+
s8 = 8, // signed 8 bits
36+
S8 __SYCL_DEPRECATED("use s8") = s8,
37+
bf16 = 9, // bfloat 16
38+
BF16 __SYCL_DEPRECATED("use bf16") = bf16,
39+
fp16 = 10, // half float
40+
FP16 __SYCL_DEPRECATED("use fp16") = fp16,
41+
tf32 = 12, // tensorfloat 32
42+
TF32 __SYCL_DEPRECATED("use tf32") = tf32
43+
};
44+
45+
} // namespace ext::intel::esimd::xmx
46+
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
47+
} // namespace sycl
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
//==----------------- xmx/dpas.hpp - DPC++ Explicit SIMD API ---------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Explicit SIMD API for DPAS Intel Xe Matrix eXtension.
9+
//===----------------------------------------------------------------------===//
10+
11+
#pragma once
12+
13+
#include <sycl/detail/defines_elementary.hpp>
14+
#include <sycl/ext/intel/esimd/detail/types.hpp>
15+
#include <sycl/ext/intel/esimd/xmx/common.hpp>
16+
#include <sycl/ext/intel/experimental/esimd/detail/math_intrin.hpp>
17+
#include <sycl/ext/oneapi/experimental/bfloat16.hpp>
18+
19+
namespace sycl {
20+
__SYCL_INLINE_VER_NAMESPACE(_V1) {
21+
22+
namespace ext::intel::esimd::xmx {
23+
24+
namespace detail {
25+
26+
template <typename T> constexpr dpas_argument_type dpas_precision_from_type() {
27+
// TODO: add support for tfloat32 here.
28+
if constexpr (std::is_same_v<T, sycl::half>)
29+
return dpas_argument_type::FP16;
30+
else if constexpr (std::is_same_v<T,
31+
sycl::ext::oneapi::experimental::bfloat16>)
32+
return dpas_argument_type::BF16;
33+
else if constexpr (std::is_same_v<T, unsigned char>)
34+
return dpas_argument_type::U8;
35+
else if constexpr (__ESIMD_DNS::is_type<T, char, signed char>())
36+
return dpas_argument_type::S8;
37+
else
38+
return dpas_argument_type::Invalid;
39+
}
40+
41+
template <dpas_argument_type T> constexpr int dpas_bitsize_from_precision() {
42+
if constexpr (T == dpas_argument_type::U2 || T == dpas_argument_type::S2)
43+
return 2;
44+
else if constexpr (T == dpas_argument_type::U4 || T == dpas_argument_type::S4)
45+
return 4;
46+
else if constexpr (T == dpas_argument_type::U8 || T == dpas_argument_type::S8)
47+
return 8;
48+
else if constexpr (T == dpas_argument_type::BF16 ||
49+
T == dpas_argument_type::FP16)
50+
return 16;
51+
else if constexpr (T == dpas_argument_type::TF32)
52+
return 32;
53+
else
54+
return -1;
55+
}
56+
57+
template <int RepeatCount, int AElemBitSize, int BElemBitSize, bool IsDPASW>
58+
constexpr void verify_repeat_count() {
59+
static_assert(RepeatCount >= 1 && RepeatCount <= 8,
60+
"Repeat count must be within 1 to 8 range");
61+
62+
if constexpr (IsDPASW && RepeatCount != 8) {
63+
static_assert(!(AElemBitSize == 2 && BElemBitSize > 4),
64+
"Unsupported repeat count for DPASW operation");
65+
66+
static_assert(
67+
RepeatCount == 4 ||
68+
(AElemBitSize != 2 && (AElemBitSize != 4 || BElemBitSize <= 4)),
69+
"Unsupported repeat count for DPASW operation");
70+
}
71+
}
72+
73+
template <int SystolicDepth, int RepeatCount, typename T, typename CT,
74+
typename BT, typename AT, dpas_argument_type BPrecision,
75+
dpas_argument_type APrecision, int BN, int AN, bool IsDPASW = false>
76+
constexpr int verify_parameters_and_deduce_exec_size() {
77+
78+
static_assert(SystolicDepth == 8, "Systolic depth must be equal to 8");
79+
static_assert(
80+
APrecision != dpas_argument_type::Invalid &&
81+
BPrecision != dpas_argument_type::Invalid,
82+
"The types of dpas arguments are either incorrect or cannot be deduced."
83+
"Fix the types and/or explicitly specify them.");
84+
85+
constexpr int AElemBitSize = dpas_bitsize_from_precision<APrecision>();
86+
constexpr int BElemBitSize = dpas_bitsize_from_precision<BPrecision>();
87+
static_assert(AElemBitSize != -1 && BElemBitSize != -1,
88+
"Cannot deduce element size of input arguments");
89+
verify_repeat_count<RepeatCount, AElemBitSize, BElemBitSize, IsDPASW>();
90+
91+
constexpr int OpsPerChannel =
92+
std::min(32 / std::max(AElemBitSize, BElemBitSize), 8);
93+
94+
// A(_Mx_K) * B(_Kx_N) + C(_Mx_N)
95+
// where:
96+
// _M = RepeatCount;
97+
// _K = SystolicDepth * OpsPerChannel;
98+
// _N = ExecutionSize (unknown, but deducible), must be 8 or 16.
99+
constexpr int _M = RepeatCount;
100+
constexpr int _K = SystolicDepth * OpsPerChannel;
101+
102+
// Compute _N (aka ExecutionSize) from the matrix B.
103+
// It has _K*_N elements of BPrecision type, and BN elements of BT type
104+
// hold those _K*_N*BPrecision bits, which let's us compute _N.
105+
constexpr int BMatrixBitSize = sizeof(BT) * BN * 8;
106+
constexpr int BNumElems = BMatrixBitSize / BElemBitSize;
107+
constexpr int _N = BNumElems / _K;
108+
static_assert(_K * _N == BNumElems, "Cannot deduce the execution size.");
109+
110+
// Now verify that AN elements of AT type hold exactly _M*_K elements
111+
// of APrecision type/size. Similarly for B: BN elements of BT type must
112+
// hold _K*_N elements of BPrecision type/size.
113+
// DPASW accepts 2x less expected AN elements than regular DPAS.
114+
constexpr int AFactorForDPASW = IsDPASW ? 2 : 1;
115+
static_assert(_M * _K * AElemBitSize == AN * sizeof(AT) * 8 * AFactorForDPASW,
116+
"The first matrix multiplier has wrong size.");
117+
static_assert(_K * _N * BElemBitSize == BN * sizeof(BT) * 8,
118+
"The second matrix multiplier has wrong size.");
119+
120+
// Execution size may be 8 or 16 depending on the target device.
121+
// User must check if used execution size is supported before calling DPAS.
122+
constexpr int ExecutionSize = _N;
123+
124+
static_assert(ExecutionSize == 8 || (!IsDPASW && ExecutionSize == 16),
125+
"Execution size must be 8 or 16 for DPAS and 8 for DPASW.");
126+
127+
if constexpr (APrecision == dpas_argument_type::FP16 ||
128+
BPrecision == dpas_argument_type::FP16) {
129+
if constexpr (ExecutionSize == 8) {
130+
static_assert(APrecision == BPrecision &&
131+
__ESIMD_DNS::is_type<T, float>() &&
132+
__ESIMD_DNS::is_type<CT, float>(),
133+
"Unsupported DPAS types! The supported types are:\n"
134+
" Result | C | B | A \n"
135+
" f | f | hf | hf \n");
136+
} else {
137+
static_assert(APrecision == BPrecision &&
138+
__ESIMD_DNS::is_type<T, float, sycl::half>() &&
139+
__ESIMD_DNS::is_type<CT, float, sycl::half>(),
140+
"Unsupported DPAS types! The supported types are:\n"
141+
" Result | C | B | A \n"
142+
" f, hf | f, hf | hf | hf \n");
143+
}
144+
} else if constexpr (APrecision == dpas_argument_type::BF16 ||
145+
BPrecision == dpas_argument_type::BF16) {
146+
using bfloat16 = sycl::ext::oneapi::experimental::bfloat16;
147+
if constexpr (ExecutionSize == 8) {
148+
static_assert(APrecision == BPrecision &&
149+
__ESIMD_DNS::is_type<T, float, bfloat16>() &&
150+
__ESIMD_DNS::is_type<CT, float, bfloat16>(),
151+
"Unsupported DPAS types! The supported types are:\n"
152+
" Result | C | B | A \n"
153+
" f | f | bf | bf \n");
154+
} else {
155+
static_assert(APrecision == BPrecision &&
156+
__ESIMD_DNS::is_type<T, float, bfloat16>() &&
157+
__ESIMD_DNS::is_type<CT, float, bfloat16>(),
158+
"Unsupported DPAS types! The supported types are:\n"
159+
" Result | C | B | A \n"
160+
" f, bf | f, bf | bf | bf \n");
161+
}
162+
} else if constexpr (APrecision == dpas_argument_type::TF32 ||
163+
BPrecision == dpas_argument_type::TF32) {
164+
static_assert(ExecutionSize == 16,
165+
"tf32 type can be used only with ExecutionSize=16");
166+
static_assert(APrecision == BPrecision && std::is_same_v<T, float> &&
167+
std::is_same_v<CT, float>,
168+
"Unsupported DPAS types! The supported types are:\n"
169+
" Result | C | B | A \n"
170+
" f | f | tf32 | tf32 \n");
171+
} else {
172+
static_assert((APrecision == dpas_argument_type::U2 ||
173+
APrecision == dpas_argument_type::S2 ||
174+
APrecision == dpas_argument_type::U4 ||
175+
APrecision == dpas_argument_type::S4 ||
176+
APrecision == dpas_argument_type::U8 ||
177+
APrecision == dpas_argument_type::S8) &&
178+
(BPrecision == dpas_argument_type::U2 ||
179+
BPrecision == dpas_argument_type::S2 ||
180+
BPrecision == dpas_argument_type::U4 ||
181+
BPrecision == dpas_argument_type::S4 ||
182+
BPrecision == dpas_argument_type::U8 ||
183+
BPrecision == dpas_argument_type::S8),
184+
"Unsupported DPAS types! The supported types are:\n"
185+
" Result | C | B | A \n"
186+
" ud, d | ud, d | ub,b,u4,s4,u2,s2 | ub,b,u4,s4,u2,s2 \n");
187+
}
188+
return ExecutionSize;
189+
}
190+
191+
} // namespace detail
192+
193+
/// @defgroup sycl_esimd_xmx_systolic_array_api Systolic Array APIs.
194+
/// APIs below are used to implement dot product accumulate systolic functions
195+
/// @ingroup sycl_esimd
196+
197+
/// @addtogroup sycl_esimd_xmx_systolic_array_api
198+
/// @{
199+
/// DPAS (Dot Product Accumulate Systolic)
200+
/// Computes the result of matrix operations: Result = C + A x B;
201+
/// @param C represents DPAS accumulator operand.
202+
/// @param B represents the 2nd matrix multiplier. It must have the VNNI encoded
203+
/// layout.
204+
/// @param A represents the 1st matrix multiplier.
205+
/// @return the vector value of DPAS computation result.
206+
template <
207+
int SystolicDepth, int RepeatCount, typename T, typename CT, typename BT,
208+
typename AT,
209+
dpas_argument_type BPrecision = detail::dpas_precision_from_type<BT>(),
210+
dpas_argument_type APrecision = detail::dpas_precision_from_type<AT>(),
211+
int N, int BN, int AN>
212+
__ESIMD_NS::simd<T, N> dpas(__ESIMD_NS::simd<CT, N> C,
213+
__ESIMD_NS::simd<BT, BN> B,
214+
__ESIMD_NS::simd<AT, AN> A) {
215+
(void)detail::verify_parameters_and_deduce_exec_size<
216+
SystolicDepth, RepeatCount, T, CT, BT, AT, BPrecision, APrecision, BN,
217+
AN>();
218+
219+
constexpr int ANCasted = AN / (sizeof(int) / sizeof(AT));
220+
constexpr int BNCasted = BN / (sizeof(int) / sizeof(BT));
221+
__ESIMD_NS::simd<int, ANCasted> ACasted = A.template bit_cast_view<int>();
222+
__ESIMD_NS::simd<int, BNCasted> BCasted = B.template bit_cast_view<int>();
223+
using CRawT = typename __ESIMD_NS::simd<CT, N>::raw_element_type;
224+
return __esimd_dpas2<BPrecision, APrecision, SystolicDepth, RepeatCount, T,
225+
CRawT, int, int, N, BNCasted, ANCasted>(
226+
C.data(), BCasted.data(), ACasted.data());
227+
}
228+
229+
/// DPAS (Dot Product Accumulate Systolic)
230+
/// Computes the result of matrix operations: Result = A x B;
231+
/// @param B represents the 2nd matrix multiplier. It must have the VNNI encoded
232+
/// layout.
233+
/// @param A represents the 1st matrix multiplier.
234+
/// @return the vector value of DPAS computation result.
235+
template <
236+
int SystolicDepth, int RepeatCount, typename T, typename BT, typename AT,
237+
dpas_argument_type BPrecision = detail::dpas_precision_from_type<BT>(),
238+
dpas_argument_type APrecision = detail::dpas_precision_from_type<AT>(),
239+
int BN, int AN>
240+
auto dpas(__ESIMD_NS::simd<BT, BN> B, __ESIMD_NS::simd<AT, AN> A) {
241+
242+
constexpr int ExecutionSize =
243+
detail::verify_parameters_and_deduce_exec_size<SystolicDepth, RepeatCount,
244+
T, T, BT, AT, BPrecision,
245+
APrecision, BN, AN>();
246+
// Result(_Mx_N) = A(_Mx_K) * B(_Kx_N)
247+
// where:
248+
// _M = RepeatCount;
249+
// _K = SystolicDepth * OpsPerChannel;
250+
// _N = ExecutionSize (unknown, but deducible), must be 8 or 16.
251+
constexpr int ResultN = RepeatCount * ExecutionSize;
252+
253+
constexpr int ANCasted = AN / (sizeof(int) / sizeof(AT));
254+
constexpr int BNCasted = BN / (sizeof(int) / sizeof(BT));
255+
__ESIMD_NS::simd<int, ANCasted> ACasted = A.template bit_cast_view<int>();
256+
__ESIMD_NS::simd<int, BNCasted> BCasted = B.template bit_cast_view<int>();
257+
258+
constexpr int Info = (RepeatCount << 24) + (SystolicDepth << 16) +
259+
((int)APrecision << 8) + (int)BPrecision;
260+
__ESIMD_NS::simd<T, ResultN> Result =
261+
__esimd_dpas_nosrc0<Info, T, int, int, ResultN, BNCasted, ANCasted>(
262+
BCasted.data(), ACasted.data());
263+
return Result;
264+
}
265+
266+
/// DPAS (Dot Product Accumulate Systolic)
267+
/// Computes the result of matrix operations: Result = C + A x B;
268+
/// @param C represents DPAS accumulator operand.
269+
/// @param B represents the 2nd matrix multiplier. It must have the VNNI encoded
270+
/// layout.
271+
/// @param A represents the 1st matrix multiplier.
272+
/// @return the vector value of DPAS computation result.
273+
template <
274+
int SystolicDepth, int RepeatCount, typename T, typename BT, typename AT,
275+
dpas_argument_type BPrecision = detail::dpas_precision_from_type<BT>(),
276+
dpas_argument_type APrecision = detail::dpas_precision_from_type<AT>(),
277+
int N, int BN, int AN>
278+
__ESIMD_NS::simd<T, N> dpasw(__ESIMD_NS::simd<T, N> C,
279+
__ESIMD_NS::simd<BT, BN> B,
280+
__ESIMD_NS::simd<AT, AN> A) {
281+
282+
constexpr bool IsDPASW = true;
283+
(void)detail::verify_parameters_and_deduce_exec_size<
284+
SystolicDepth, RepeatCount, T, T, BT, AT, BPrecision, APrecision, BN, AN,
285+
IsDPASW>();
286+
287+
constexpr int ANCasted = AN / (sizeof(int) / sizeof(AT));
288+
constexpr int BNCasted = BN / (sizeof(int) / sizeof(BT));
289+
__ESIMD_NS::simd<int, ANCasted> ACasted = A.template bit_cast_view<int>();
290+
__ESIMD_NS::simd<int, BNCasted> BCasted = B.template bit_cast_view<int>();
291+
292+
constexpr int Info = (RepeatCount << 24) + (SystolicDepth << 16) +
293+
((int)APrecision << 8) + (int)BPrecision;
294+
return __esimd_dpasw<Info, T, int, int, N, BNCasted, ANCasted>(
295+
C.data(), BCasted.data(), ACasted.data());
296+
}
297+
298+
/// DPAS (Dot Product Accumulate Systolic)
299+
/// Computes the result of matrix operations: Result = A x B;
300+
/// @param B represents the 2nd matrix multiplier. It must have the VNNI encoded
301+
/// layout.
302+
/// @param A represents the 1st matrix multiplier.
303+
/// @return the vector value of DPAS computation result.
304+
template <
305+
int SystolicDepth, int RepeatCount, typename T, typename BT, typename AT,
306+
dpas_argument_type BPrecision = detail::dpas_precision_from_type<BT>(),
307+
dpas_argument_type APrecision = detail::dpas_precision_from_type<AT>(),
308+
int BN, int AN>
309+
auto dpasw(__ESIMD_NS::simd<BT, BN> B, __ESIMD_NS::simd<AT, AN> A) {
310+
311+
constexpr bool IsDPASW = true;
312+
constexpr int ExecutionSize = detail::verify_parameters_and_deduce_exec_size<
313+
SystolicDepth, RepeatCount, T, T, BT, AT, BPrecision, APrecision, BN, AN,
314+
IsDPASW>();
315+
316+
// Result(_Mx_N) = A(_Mx_K) * B(_Kx_N)
317+
// where:
318+
// _M = RepeatCount;
319+
// _K = SystolicDepth * OpsPerChannel;
320+
// _N = ExecutionSize (unknown, but deducible), must be 8 or 16.
321+
constexpr int ResultN = RepeatCount * ExecutionSize;
322+
323+
constexpr int ANCasted = AN / (sizeof(int) / sizeof(AT));
324+
constexpr int BNCasted = BN / (sizeof(int) / sizeof(BT));
325+
__ESIMD_NS::simd<int, ANCasted> ACasted = A.template bit_cast_view<int>();
326+
__ESIMD_NS::simd<int, BNCasted> BCasted = B.template bit_cast_view<int>();
327+
328+
constexpr int Info = (RepeatCount << 24) + (SystolicDepth << 16) +
329+
((int)APrecision << 8) + (int)BPrecision;
330+
__ESIMD_NS::simd<T, ResultN> Result =
331+
__esimd_dpasw_nosrc0<Info, T, int, int, ResultN, BNCasted, ANCasted>(
332+
BCasted.data(), ACasted.data());
333+
return Result;
334+
}
335+
336+
/// @} sycl_esimd_xmx_systolic_array_api
337+
338+
} // namespace ext::intel::esimd::xmx
339+
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
340+
} // namespace sycl

0 commit comments

Comments
 (0)