-
Notifications
You must be signed in to change notification settings - Fork 713
/
matrix-tensorcores.hpp
657 lines (619 loc) · 30.8 KB
/
matrix-tensorcores.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//===-------- matrix-tensorcores.hpp - matrix ext impl ---*- C++ -*-------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// ===-------------------------------------------------------------------=== //
#pragma once
#include "matrix-unified-utils.hpp"
#include <sycl/aliases.hpp> // for half
#include <sycl/ext/oneapi/bfloat16.hpp> // for bfloat16
#include <sycl/half_type.hpp> // for half
#include <sycl/marray.hpp> // for marray
#include <stddef.h> // for size_t
#include <stdint.h> // for int8_t, uint8_t, int32_t
#include <type_traits> // for enable_if_t
namespace sycl {
inline namespace _V1 {
namespace ext {
namespace oneapi {
namespace experimental {
namespace matrix {
template <typename Group, typename T, use Use, size_t Rows, size_t Cols,
layout Layout = layout::dynamic>
struct joint_matrix;
} // namespace matrix
} // namespace experimental
namespace detail {
template <typename T, sycl::ext::oneapi::experimental::matrix::use Use,
size_t Rows, size_t Cols,
sycl::ext::oneapi::experimental::matrix::layout Layout =
sycl::ext::oneapi::experimental::matrix::layout::dynamic,
typename Cond = void>
struct joint_matrix_cuda;
#define __SYCL_JOINT_MATRIX_OVERLOAD_ARR(TYPE, USE, M, N, SIZE) \
template <sycl::ext::oneapi::experimental::matrix::layout Layout> \
struct joint_matrix_cuda< \
TYPE, sycl::ext::oneapi::experimental::matrix::use::USE, M, N, Layout, \
typename std::enable_if_t< \
Layout == \
sycl::ext::oneapi::experimental::matrix::layout::row_major || \
Layout == \
sycl::ext::oneapi::experimental::matrix::layout::col_major>> { \
marray<TYPE, SIZE> wi_marray; \
};
// m8n32k16
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, a, 8, 16, 4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, b, 16, 32, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, a, 8, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, b, 16, 32, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, a, 8, 16, 4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, b, 16, 32, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, a, 8, 16, 4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, b, 16, 32, 16)
// m32n8k16
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, a, 32, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, b, 16, 8, 4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, a, 32, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, b, 16, 8, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, a, 32, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, b, 16, 8, 4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, a, 32, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, b, 16, 8, 4)
// m16n16k16
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, a, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(sycl::ext::oneapi::bfloat16, b, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, a, 16, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(half, b, 16, 16, 16)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, a, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(int8_t, b, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, a, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(uint8_t, b, 16, 16, 8)
// m8n8k4 double only
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(double, a, 8, 4, 1)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR(double, b, 4, 8, 1)
#undef __SYCL_JOINT_MATRIX_OVERLOAD_ARR
#define __SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(TYPE, M, N, SIZE) \
template <> \
struct joint_matrix_cuda< \
TYPE, sycl::ext::oneapi::experimental::matrix::use::accumulator, M, N, \
sycl::ext::oneapi::experimental::matrix::layout::dynamic> { \
marray<TYPE, SIZE> wi_marray; \
};
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(half, 8, 32, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(float, 8, 32, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(int32_t, 8, 32, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(half, 32, 8, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(float, 32, 8, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(int32_t, 32, 8, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(half, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(float, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(int32_t, 16, 16, 8)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC(double, 8, 8, 2)
#undef __SYCL_JOINT_MATRIX_OVERLOAD_ARR_ACC
#define __SYCL_JOINT_MATRIX_OVERLOAD_ARR_PRECISION(PRECISION, USE, M, N, TYPE, \
SIZE) \
template <sycl::ext::oneapi::experimental::matrix::layout Layout> \
struct joint_matrix_cuda< \
PRECISION, sycl::ext::oneapi::experimental::matrix::use::USE, M, N, \
Layout, \
typename std::enable_if_t< \
Layout == \
sycl::ext::oneapi::experimental::matrix::layout::row_major || \
Layout == \
sycl::ext::oneapi::experimental::matrix::layout::col_major>> { \
marray<TYPE, SIZE> wi_marray; \
};
// m16n16k8 tf32 only
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_PRECISION(
sycl::ext::oneapi::experimental::matrix::precision::tf32, a, 16, 8, float,
4)
__SYCL_JOINT_MATRIX_OVERLOAD_ARR_PRECISION(
sycl::ext::oneapi::experimental::matrix::precision::tf32, b, 8, 16, float,
4)
#undef __SYCL_JOINT_MATRIX_OVERLOAD_ARR_PRECISION
#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__)
template <sycl::ext::oneapi::experimental::matrix::layout Layout>
constexpr int get_layout_id();
template <>
constexpr int
get_layout_id<sycl::ext::oneapi::experimental::matrix::layout::row_major>() {
return 0;
}
template <>
constexpr int
get_layout_id<sycl::ext::oneapi::experimental::matrix::layout::col_major>() {
return 1;
}
template <sycl::ext::oneapi::experimental::matrix::layout Layout, typename S,
typename T, size_t NumRows, size_t NumCols,
access::address_space Space, access::decorated IsDecorated>
void load_accumulator_layoutT(
joint_matrix_cuda<
S, sycl::ext::oneapi::experimental::matrix::use::accumulator, NumRows,
NumCols, sycl::ext::oneapi::experimental::matrix::layout::dynamic> &res,
multi_ptr<T, Space, IsDecorated> src, size_t stride) {
if constexpr (std::is_same_v<S, int32_t>) {
auto destptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
__imma_m16n16k16_ld_c(destptr, src.get(), stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 8 && NumCols == 32) {
__imma_m8n32k16_ld_c(destptr, src.get(), stride, get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 8) {
__imma_m32n8k16_ld_c(destptr, src.get(), stride, get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, float>) {
auto dstptr = reinterpret_cast<float *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
__hmma_m16n16k16_ld_c_f32(dstptr, src.get(), stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 8 && NumCols == 32) {
__hmma_m8n32k16_ld_c_f32(dstptr, src.get(), stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 8) {
__hmma_m32n8k16_ld_c_f32(dstptr, src.get(), stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, half>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto dstptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 32 && NumCols == 8) {
__hmma_m32n8k16_ld_c_f16(dstptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 8 && NumCols == 32) {
__hmma_m8n32k16_ld_c_f16(dstptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 16) {
__hmma_m16n16k16_ld_c_f16(dstptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, double>) {
__dmma_m8n8k4_ld_c(reinterpret_cast<double *>(&res.wi_marray), src.get(),
stride, get_layout_id<Layout>());
}
};
template <typename S, typename T, size_t NumRows, size_t NumCols,
access::address_space Space, access::decorated IsDecorated>
void load_accumulator_cuda(
joint_matrix_cuda<
S, sycl::ext::oneapi::experimental::matrix::use::accumulator, NumRows,
NumCols, sycl::ext::oneapi::experimental::matrix::layout::dynamic> &res,
multi_ptr<T, Space, IsDecorated> src, size_t stride,
sycl::ext::oneapi::experimental::matrix::layout Layout) {
switch (Layout) {
case sycl::ext::oneapi::experimental::matrix::layout::row_major:
load_accumulator_layoutT<
sycl::ext::oneapi::experimental::matrix::layout::row_major>(res, src,
stride);
break;
case sycl::ext::oneapi::experimental::matrix::layout::col_major:
load_accumulator_layoutT<
sycl::ext::oneapi::experimental::matrix::layout::col_major>(res, src,
stride);
break;
default:
assert(false && "Invalid layout specified!");
}
}
template <
typename S, typename T, size_t NumRows, size_t NumCols,
sycl::ext::oneapi::experimental::matrix::use Use,
sycl::ext::oneapi::experimental::matrix::layout Layout,
access::address_space Space, access::decorated IsDecorated,
std::enable_if_t<
Layout == sycl::ext::oneapi::experimental::matrix::layout::row_major ||
Layout ==
sycl::ext::oneapi::experimental::matrix::layout::col_major,
bool> = true>
void load_multiplicand_cuda(
joint_matrix_cuda<S, Use, NumRows, NumCols, Layout> &res,
multi_ptr<T, Space, IsDecorated> src, size_t stride) {
if constexpr (std::is_same_v<S, sycl::ext::oneapi::bfloat16>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto destptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
if constexpr (Use == sycl::ext::oneapi::experimental::matrix::use::a) {
__mma_bf16_m16n16k16_ld_a(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (Use ==
sycl::ext::oneapi::experimental::matrix::use::b) {
__mma_bf16_m16n16k16_ld_b(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (NumRows == 8 && NumCols == 16) {
__mma_bf16_m8n32k16_ld_a(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 32) {
__mma_bf16_m8n32k16_ld_b(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 16) {
__mma_bf16_m32n8k16_ld_a(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 8) {
__mma_bf16_m32n8k16_ld_b(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, uint8_t>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto destptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
if constexpr (Use == sycl::ext::oneapi::experimental::matrix::use::a) {
__imma_m16n16k16_ld_a_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (Use ==
sycl::ext::oneapi::experimental::matrix::use::b) {
__imma_m16n16k16_ld_b_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (NumRows == 8 && NumCols == 16) {
__imma_m8n32k16_ld_a_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 32) {
__imma_m8n32k16_ld_b_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 16) {
__imma_m32n8k16_ld_a_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 8) {
__imma_m32n8k16_ld_b_u8(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, int8_t>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto destptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
if constexpr (Use == sycl::ext::oneapi::experimental::matrix::use::a) {
__imma_m16n16k16_ld_a_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (Use ==
sycl::ext::oneapi::experimental::matrix::use::b) {
__imma_m16n16k16_ld_b_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (NumRows == 8 && NumCols == 16) {
__imma_m8n32k16_ld_a_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 32) {
__imma_m8n32k16_ld_b_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 16) {
__imma_m32n8k16_ld_a_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 8) {
__imma_m32n8k16_ld_b_s8(destptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, half>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto dstptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 16) {
if constexpr (Use == sycl::ext::oneapi::experimental::matrix::use::a) {
__hmma_m16n16k16_ld_a(dstptr, tileptr, stride, get_layout_id<Layout>());
} else if constexpr (Use ==
sycl::ext::oneapi::experimental::matrix::use::b) {
__hmma_m16n16k16_ld_b(dstptr, tileptr, stride, get_layout_id<Layout>());
}
} else if constexpr (NumRows == 8 && NumCols == 16) {
__hmma_m8n32k16_ld_a(dstptr, tileptr, stride, get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 32) {
__hmma_m8n32k16_ld_b(dstptr, tileptr, stride, get_layout_id<Layout>());
} else if constexpr (NumRows == 32 && NumCols == 16) {
__hmma_m32n8k16_ld_a(dstptr, tileptr, stride, get_layout_id<Layout>());
} else if constexpr (NumRows == 16 && NumCols == 8) {
__hmma_m32n8k16_ld_b(dstptr, tileptr, stride, get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, sycl::ext::oneapi::experimental::
matrix::precision::tf32>) {
auto tileptr = reinterpret_cast<const int32_t *>(src.get());
auto dstptr = reinterpret_cast<int32_t *>(&res.wi_marray);
if constexpr (NumRows == 16 && NumCols == 8) {
__mma_tf32_m16n16k8_ld_a(dstptr, tileptr, stride,
get_layout_id<Layout>());
} else if constexpr (NumRows == 8 && NumCols == 16) {
__mma_tf32_m16n16k8_ld_b(dstptr, tileptr, stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<S, double>) {
auto dstptr = reinterpret_cast<double *>(&res.wi_marray);
if constexpr (Use == sycl::ext::oneapi::experimental::matrix::use::a) {
__dmma_m8n8k4_ld_a(dstptr, src.get(), stride, get_layout_id<Layout>());
} else if constexpr (Use ==
sycl::ext::oneapi::experimental::matrix::use::b) {
__dmma_m8n8k4_ld_b(dstptr, src.get(), stride, get_layout_id<Layout>());
}
}
}
template <sycl::ext::oneapi::experimental::matrix::layout Layout, typename T,
size_t NumRows, size_t NumCols, access::address_space Space,
access::decorated IsDecorated>
void store_layoutT(
const joint_matrix_cuda<
T, sycl::ext::oneapi::experimental::matrix::use::accumulator, NumRows,
NumCols, sycl::ext::oneapi::experimental::matrix::layout::dynamic> &src,
multi_ptr<T, Space, IsDecorated> dst, size_t stride) {
if constexpr (NumRows == 16 && NumCols == 16) {
if constexpr (std::is_same_v<T, float>) {
__hmma_m16n16k16_st_c_f32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, int32_t>) {
__imma_m16n16k16_st_c_i32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, half>) {
__hmma_m16n16k16_st_c_f16(
reinterpret_cast<int32_t *>(dst.get()),
reinterpret_cast<const int32_t *>(&src.wi_marray[0]), stride,
get_layout_id<Layout>());
}
} else if constexpr (NumRows == 8 && NumCols == 32) {
if constexpr (std::is_same_v<T, float>) {
__hmma_m8n32k16_st_c_f32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, int32_t>) {
__imma_m8n32k16_st_c_i32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, half>) {
__hmma_m8n32k16_st_c_f16(
reinterpret_cast<int32_t *>(dst.get()),
reinterpret_cast<const int32_t *>(&src.wi_marray[0]), stride,
get_layout_id<Layout>());
}
} else if constexpr (NumRows == 32 && NumCols == 8) {
if constexpr (std::is_same_v<T, float>) {
__hmma_m32n8k16_st_c_f32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, int32_t>) {
__imma_m32n8k16_st_c_i32(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
} else if constexpr (std::is_same_v<T, half>) {
__hmma_m32n8k16_st_c_f16(
reinterpret_cast<int32_t *>(dst.get()),
reinterpret_cast<const int32_t *>(&src.wi_marray[0]), stride,
get_layout_id<Layout>());
}
} else if constexpr (std::is_same_v<T, double>) {
__dmma_m8n8k4_st_c_f64(dst.get(), &src.wi_marray[0], stride,
get_layout_id<Layout>());
}
}
template <typename T, size_t NumRows, size_t NumCols,
access::address_space Space, access::decorated IsDecorated>
void joint_matrix_store_cuda(
const joint_matrix_cuda<
T, sycl::ext::oneapi::experimental::matrix::use::accumulator, NumRows,
NumCols, sycl::ext::oneapi::experimental::matrix::layout::dynamic> &src,
multi_ptr<T, Space, IsDecorated> dst, size_t stride,
sycl::ext::oneapi::experimental::matrix::layout Layout) {
switch (Layout) {
case sycl::ext::oneapi::experimental::matrix::layout::row_major:
store_layoutT<sycl::ext::oneapi::experimental::matrix::layout::row_major>(
src, dst, stride);
break;
case sycl::ext::oneapi::experimental::matrix::layout::col_major:
store_layoutT<sycl::ext::oneapi::experimental::matrix::layout::col_major>(
src, dst, stride);
break;
default:
assert(false && "Invalid layout specified!");
}
}
template <sycl::ext::oneapi::experimental::matrix::layout LayoutA,
sycl::ext::oneapi::experimental::matrix::layout LayoutB>
constexpr int get_layout_pair_id();
template <>
constexpr int get_layout_pair_id<
sycl::ext::oneapi::experimental::matrix::layout::row_major,
sycl::ext::oneapi::experimental::matrix::layout::row_major>() {
return 0;
}
template <>
constexpr int get_layout_pair_id<
sycl::ext::oneapi::experimental::matrix::layout::row_major,
sycl::ext::oneapi::experimental::matrix::layout::col_major>() {
return 1;
}
template <>
constexpr int get_layout_pair_id<
sycl::ext::oneapi::experimental::matrix::layout::col_major,
sycl::ext::oneapi::experimental::matrix::layout::row_major>() {
return 2;
}
template <>
constexpr int get_layout_pair_id<
sycl::ext::oneapi::experimental::matrix::layout::col_major,
sycl::ext::oneapi::experimental::matrix::layout::col_major>() {
return 3;
}
template <
typename Tm, typename Tc, typename Td, std::size_t M, std::size_t K,
std::size_t N, sycl::ext::oneapi::experimental::matrix::layout LayoutA,
sycl::ext::oneapi::experimental::matrix::layout LayoutB,
std::enable_if_t<
(LayoutA ==
sycl::ext::oneapi::experimental::matrix::layout::row_major ||
LayoutA ==
sycl::ext::oneapi::experimental::matrix::layout::col_major) &&
(LayoutB ==
sycl::ext::oneapi::experimental::matrix::layout::row_major ||
LayoutB ==
sycl::ext::oneapi::experimental::matrix::layout::col_major),
bool> = true>
void joint_matrix_mad_cuda(
joint_matrix_cuda<
Td, sycl::ext::oneapi::experimental::matrix::use::accumulator, M, N,
sycl::ext::oneapi::experimental::matrix::layout::dynamic> &D,
const joint_matrix_cuda<Tm, sycl::ext::oneapi::experimental::matrix::use::a,
M, K, LayoutA> &A,
const joint_matrix_cuda<Tm, sycl::ext::oneapi::experimental::matrix::use::b,
K, N, LayoutB> &B,
const joint_matrix_cuda<
Tc, sycl::ext::oneapi::experimental::matrix::use::accumulator, M, N,
sycl::ext::oneapi::experimental::matrix::layout::dynamic> &C) {
if constexpr (M == 16 && N == 16 && K == 16) {
if constexpr (std::is_same_v<Tc, int32_t>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
auto ptrC = reinterpret_cast<const int32_t *>(&C.wi_marray);
auto ptrD = reinterpret_cast<int32_t *>(&D.wi_marray);
if constexpr (std::is_same_v<Tm, int8_t>) {
__imma_m16n16k16_mma_s8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else if constexpr (std::is_same_v<Tm, uint8_t>) {
__imma_m16n16k16_mma_u8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tm, half>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
if constexpr (std::is_same_v<Tc, float>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m16n16k16_mma_f32f32(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m16n16k16_mma_f16f32(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tc, half>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m16n16k16_mma_f32f16(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m16n16k16_mma_f16f16(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
}
} else if constexpr (std::is_same_v<Tm, sycl::ext::oneapi::bfloat16>) {
__mma_bf16_m16n16k16_mma_f32(
reinterpret_cast<float *>(&D.wi_marray),
reinterpret_cast<const int32_t *>(&A.wi_marray),
reinterpret_cast<const int32_t *>(&B.wi_marray),
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (M == 8 && N == 32 && K == 16) {
if constexpr (std::is_same_v<Tc, int32_t>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
auto ptrC = reinterpret_cast<const int32_t *>(&C.wi_marray);
auto ptrD = reinterpret_cast<int32_t *>(&D.wi_marray);
if constexpr (std::is_same_v<Tm, int8_t>) {
__imma_m8n32k16_mma_s8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else if constexpr (std::is_same_v<Tm, uint8_t>) {
__imma_m8n32k16_mma_u8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tm, half>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
if constexpr (std::is_same_v<Tc, float>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m8n32k16_mma_f32f32(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m8n32k16_mma_f16f32(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tc, half>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m8n32k16_mma_f32f16(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m8n32k16_mma_f16f16(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
}
} else if constexpr (std::is_same_v<Tm, sycl::ext::oneapi::bfloat16>) {
__mma_bf16_m8n32k16_mma_f32(
reinterpret_cast<float *>(&D.wi_marray),
reinterpret_cast<const int32_t *>(&A.wi_marray),
reinterpret_cast<const int32_t *>(&B.wi_marray),
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (M == 32 && N == 8 && K == 16) {
if constexpr (std::is_same_v<Tc, int32_t>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
auto ptrC = reinterpret_cast<const int32_t *>(&C.wi_marray);
auto ptrD = reinterpret_cast<int32_t *>(&D.wi_marray);
if constexpr (std::is_same_v<Tm, int8_t>) {
__imma_m32n8k16_mma_s8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else if constexpr (std::is_same_v<Tm, uint8_t>) {
__imma_m32n8k16_mma_u8(ptrD, ptrA, ptrB, ptrC,
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tm, sycl::ext::oneapi::bfloat16>) {
__mma_bf16_m32n8k16_mma_f32(
reinterpret_cast<float *>(&D.wi_marray),
reinterpret_cast<const int32_t *>(&A.wi_marray),
reinterpret_cast<const int32_t *>(&B.wi_marray),
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else if constexpr (std::is_same_v<Tm, half>) {
auto ptrA = reinterpret_cast<const int32_t *>(&A.wi_marray);
auto ptrB = reinterpret_cast<const int32_t *>(&B.wi_marray);
if constexpr (std::is_same_v<Tc, float>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m32n8k16_mma_f32f32(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m32n8k16_mma_f16f32(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
} else if constexpr (std::is_same_v<Tc, half>) {
if constexpr (std::is_same<Td, float>::value) {
__hmma_m32n8k16_mma_f32f16(
reinterpret_cast<float *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else {
__hmma_m32n8k16_mma_f16f16(
reinterpret_cast<int32_t *>(&D.wi_marray), ptrA, ptrB,
reinterpret_cast<const int32_t *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
}
}
} else if constexpr (M == 16 && N == 16 && K == 8) {
__mma_tf32_m16n16k8_mma_f32(reinterpret_cast<float *>(&D.wi_marray),
reinterpret_cast<const int32_t *>(&A.wi_marray),
reinterpret_cast<const int32_t *>(&B.wi_marray),
reinterpret_cast<const float *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
} else if constexpr (std::is_same_v<Tm, double>) {
__dmma_m8n8k4_mma_f64(reinterpret_cast<double *>(&D.wi_marray),
reinterpret_cast<const double *>(&A.wi_marray),
reinterpret_cast<const double *>(&B.wi_marray),
reinterpret_cast<const double *>(&C.wi_marray),
get_layout_pair_id<LayoutA, LayoutB>(), 0);
}
}
#endif // defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__)
} // namespace detail
} // namespace oneapi
} // namespace ext
} // namespace _V1
} // namespace sycl