forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinalg.h
More file actions
576 lines (496 loc) · 20.8 KB
/
linalg.h
File metadata and controls
576 lines (496 loc) · 20.8 KB
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
// Header for HLSL Linear Algebra Matrix APIs.
#if ((__SHADER_TARGET_MAJOR > 6) || \
(__SHADER_TARGET_MAJOR == 6 && __SHADER_TARGET_MINOR >= 10)) && \
(__HLSL_VERSION >= 2021)
#pragma dxc diagnostic push
#pragma dxc diagnostic ignored "-Whlsl-groupshared-202x"
namespace hlsl {
#define SIZE_TYPE int
template <typename T> struct is_arithmetic {
static const bool value = false;
};
#define __ARITHMETIC_TYPE(type) \
template <> struct is_arithmetic<type> { \
static const bool value = true; \
};
#if __HLSL_ENABLE_16_BIT
__ARITHMETIC_TYPE(uint16_t)
__ARITHMETIC_TYPE(int16_t)
#endif
__ARITHMETIC_TYPE(uint)
__ARITHMETIC_TYPE(int)
__ARITHMETIC_TYPE(uint64_t)
__ARITHMETIC_TYPE(int64_t)
__ARITHMETIC_TYPE(half)
__ARITHMETIC_TYPE(float)
__ARITHMETIC_TYPE(double)
template <typename T> struct is_signed {
static const bool value = true;
};
#define __UNSIGNED_TYPE(type) \
template <> struct is_signed<type> { \
static const bool value = false; \
};
#if __HLSL_ENABLE_16_BIT
__UNSIGNED_TYPE(uint16_t)
#endif
__UNSIGNED_TYPE(uint)
__UNSIGNED_TYPE(uint64_t)
#undef __UNSIGNED_TYPE
template <bool B, typename T> struct enable_if {};
template <typename T> struct enable_if<true, T> {
using type = T;
};
} // namespace hlsl
namespace dxil {
// This enum must _exactly_ match the DXIL constants.
enum class ComponentType : uint32_t {
Invalid = 0,
I1 = 1,
I16 = 2,
U16 = 3,
I32 = 4,
U32 = 5,
I64 = 6,
U64 = 7,
F16 = 8,
F32 = 9,
F64 = 10,
SNormF16 = 11,
UNormF16 = 12,
SNormF32 = 13,
UNormF32 = 14,
SNormF64 = 15,
UNormF64 = 16,
PackedS8x32 = 17,
PackedU8x32 = 18,
// BEGIN NEW FOR SM 6.10
I8 = 19,
U8 = 20,
F8_E4M3FN = 21,
F8_E5M2 = 22,
// END
LastEntry
};
} // namespace dxil
namespace dx {
namespace linalg {
#define __COMPONENT_TYPE(type) type = (uint)dxil::ComponentType::type
// This enum only defines values that are valid for Matrix component types.
// Each enumeration's value matches the cooresponding DXIL constant.
struct ComponentType {
enum ComponentEnum {
// Signed integers.
__COMPONENT_TYPE(I8),
__COMPONENT_TYPE(I16),
__COMPONENT_TYPE(I32),
__COMPONENT_TYPE(I64),
// Unsigned integers.
__COMPONENT_TYPE(U8),
__COMPONENT_TYPE(U16),
__COMPONENT_TYPE(U32),
__COMPONENT_TYPE(U64),
// Floating point types.
__COMPONENT_TYPE(F8_E4M3FN),
__COMPONENT_TYPE(F8_E5M2),
__COMPONENT_TYPE(F16),
__COMPONENT_TYPE(F32),
__COMPONENT_TYPE(F64),
};
};
#undef __COMPONENT_TYPE
using ComponentEnum = ComponentType::ComponentEnum;
struct MatrixUse {
enum MatrixUseEnum {
A = 0,
B = 1,
Accumulator = 2,
};
};
using MatrixUseEnum = MatrixUse::MatrixUseEnum;
struct MatrixScope {
enum MatrixScopeEnum {
Thread = 0,
Wave = 1,
ThreadGroup = 2,
};
};
using MatrixScopeEnum = MatrixScope::MatrixScopeEnum;
struct MatrixLayout {
enum MatrixLayoutEnum {
RowMajor = 0,
ColMajor = 1,
MulOptimal = 2,
MulOptimalTranspose = 3,
OuterProductOptimal = 4,
OuterProductOptimalTranspose = 5,
};
};
using MatrixLayoutEnum = MatrixLayout::MatrixLayoutEnum;
namespace __detail {
template <ComponentEnum CompTy> struct ComponentTypeTraits {
using Type = uint;
static const bool IsNativeScalar = false;
static const uint ElementsPerScalar = 4;
};
#define __MATRIX_SCALAR_COMPONENT_MAPPING(enum_val, type) \
template <> struct ComponentTypeTraits<enum_val> { \
using Type = type; \
static const bool IsNativeScalar = true; \
static const uint ElementsPerScalar = 1; \
};
#if __HLSL_ENABLE_16_BIT
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::I16, int16_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::U16, uint16_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::F16, float16_t)
#endif
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::I32, int32_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::U32, uint32_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::F32, float)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::I64, int64_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::U64, uint64_t)
__MATRIX_SCALAR_COMPONENT_MAPPING(ComponentType::F64, double)
template <ComponentEnum DstTy, ComponentEnum SrcTy, int SrcN> struct DstN {
// Make sure to round up in case SrcN isn't an even multiple of the number of
// elements per scalar
static const int Value =
(SrcN * ComponentTypeTraits<SrcTy>::ElementsPerScalar +
ComponentTypeTraits<DstTy>::ElementsPerScalar - 1) /
ComponentTypeTraits<DstTy>::ElementsPerScalar;
};
template <SIZE_TYPE MVal, SIZE_TYPE NVal, bool Transposed> struct DimMN {
static const SIZE_TYPE M = MVal;
static const SIZE_TYPE N = NVal;
};
template <SIZE_TYPE MVal, SIZE_TYPE NVal> struct DimMN<MVal, NVal, true> {
static const SIZE_TYPE M = NVal;
static const SIZE_TYPE N = MVal;
};
} // namespace __detail
template <ComponentEnum ElementType, uint DimA> struct VectorRef {
ByteAddressBuffer Buf;
uint Offset;
};
template <typename T, int N, ComponentEnum DT> struct InterpretedVector {
vector<T, N> Data;
static const ComponentEnum Interpretation = DT;
static const SIZE_TYPE Size =
__detail::ComponentTypeTraits<DT>::ElementsPerScalar * N;
};
template <ComponentEnum DT, typename T, int N>
InterpretedVector<T, N, DT> MakeInterpretedVector(vector<T, N> Vec) {
InterpretedVector<T, N, DT> IV = {Vec};
return IV;
}
template <ComponentEnum DestTy, ComponentEnum OriginTy, typename T, int N>
InterpretedVector<typename __detail::ComponentTypeTraits<DestTy>::Type,
__detail::DstN<DestTy, OriginTy, N>::Value, DestTy>
Convert(vector<T, N> Vec) {
vector<typename __detail::ComponentTypeTraits<DestTy>::Type,
__detail::DstN<DestTy, OriginTy, N>::Value>
Result;
__builtin_LinAlg_Convert(Result, Vec, OriginTy, DestTy);
return MakeInterpretedVector<DestTy>(Result);
}
template <ComponentEnum ComponentTy, SIZE_TYPE M, SIZE_TYPE N,
MatrixUseEnum Use, MatrixScopeEnum Scope>
class Matrix {
using ElementType = typename __detail::ComponentTypeTraits<ComponentTy>::Type;
// If this isn't a native scalar, we have an 8-bit type, so we have 4 elements
// packed in each scalar value.
static const uint ElementsPerScalar =
__detail::ComponentTypeTraits<ComponentTy>::ElementsPerScalar;
static const bool IsNativeScalar =
__detail::ComponentTypeTraits<ComponentTy>::IsNativeScalar;
using HandleT = __builtin_LinAlgMatrix
[[__LinAlgMatrix_Attributes(ComponentTy, M, N, Use, Scope)]];
HandleT __handle;
template <ComponentEnum NewCompTy, MatrixUseEnum NewUse = Use,
bool Transpose = false>
Matrix<NewCompTy, __detail::DimMN<M, N, Transpose>::M,
__detail::DimMN<M, N, Transpose>::N, NewUse, Scope>
Cast() {
Matrix<NewCompTy, __detail::DimMN<M, N, Transpose>::M,
__detail::DimMN<M, N, Transpose>::N, NewUse, Scope>
Result;
__builtin_LinAlg_CopyConvertMatrix(Result.__handle, __handle, Transpose);
return Result;
}
template <typename T>
static typename hlsl::enable_if<hlsl::is_arithmetic<T>::value, Matrix>::type
Splat(T Val) {
Matrix Result;
__builtin_LinAlg_FillMatrix(Result.__handle, Val);
return Result;
}
static Matrix Load(ByteAddressBuffer Res, uint StartOffset, uint Stride,
MatrixLayoutEnum Layout, uint Align = 128) {
Matrix Result;
__builtin_LinAlg_MatrixLoadFromDescriptor(Result.__handle, Res, StartOffset,
Stride, Layout, Align);
return Result;
}
static Matrix Load(RWByteAddressBuffer Res, uint StartOffset, uint Stride,
MatrixLayoutEnum Layout, uint Align = 128) {
Matrix Result;
__builtin_LinAlg_MatrixLoadFromDescriptor(Result.__handle, Res, StartOffset,
Stride, Layout, Align);
return Result;
}
template <typename T, SIZE_TYPE Size>
static typename hlsl::enable_if<hlsl::is_arithmetic<T>::value &&
(M * N / ElementsPerScalar <= Size),
Matrix>::type
Load(groupshared T Arr[Size], uint StartIdx, uint Stride,
MatrixLayoutEnum Layout) {
Matrix Result;
__builtin_LinAlg_MatrixLoadFromMemory(Result.__handle, Arr, StartIdx,
Stride, Layout);
return Result;
}
template <ComponentEnum LocalComp = ComponentTy>
typename hlsl::enable_if<LocalComp == ComponentTy && IsNativeScalar,
uint>::type
Length() {
return __builtin_LinAlg_MatrixLength(__handle);
}
template <ComponentEnum LocalComp = ComponentTy>
typename hlsl::enable_if<LocalComp == ComponentTy && IsNativeScalar,
uint2>::type
GetCoordinate(uint Index) {
return __builtin_LinAlg_MatrixGetCoordinate(__handle, Index);
}
template <ComponentEnum LocalComp = ComponentTy>
typename hlsl::enable_if<LocalComp == ComponentTy && IsNativeScalar,
ElementType>::type
Get(uint Index) {
ElementType Result;
__builtin_LinAlg_MatrixGetElement(Result, __handle, Index);
return Result;
}
template <ComponentEnum LocalComp = ComponentTy>
typename hlsl::enable_if<LocalComp == ComponentTy && IsNativeScalar,
void>::type
Set(uint Index, ElementType Value) {
__builtin_LinAlg_MatrixSetElement(__handle, __handle, Index, Value);
}
void Store(RWByteAddressBuffer Res, uint StartOffset, uint Stride,
MatrixLayoutEnum Layout, uint Align = 128) {
__builtin_LinAlg_MatrixStoreToDescriptor(__handle, Res, StartOffset, Stride,
Layout, Align);
}
template <typename T, SIZE_TYPE Size>
typename hlsl::enable_if<hlsl::is_arithmetic<T>::value &&
(M * N / ElementsPerScalar <= Size),
void>::type
Store(groupshared T Arr[Size], uint StartIdx, uint Stride,
MatrixLayoutEnum Layout) {
__builtin_LinAlg_MatrixStoreToMemory(__handle, Arr, StartIdx, Stride,
Layout);
}
// Accumulate methods
template <MatrixUseEnum UseLocal = Use>
typename hlsl::enable_if<Use == MatrixUse::Accumulator && UseLocal == Use,
void>::type
InterlockedAccumulate(RWByteAddressBuffer Res, uint StartOffset, uint Stride,
MatrixLayoutEnum Layout, uint Align = 128) {
__builtin_LinAlg_MatrixAccumulateToDescriptor(__handle, Res, StartOffset,
Stride, Layout, Align);
}
template <typename T, MatrixUseEnum UseLocal = Use,
MatrixScopeEnum ScopeLocal = Scope, SIZE_TYPE Size>
typename hlsl::enable_if<
hlsl::is_arithmetic<T>::value && Use == MatrixUse::Accumulator &&
UseLocal == Use && (M * N / ElementsPerScalar <= Size) &&
Scope == MatrixScope::Wave && ScopeLocal == Scope,
void>::type
InterlockedAccumulate(groupshared T Arr[Size], uint StartIdx, uint Stride,
MatrixLayoutEnum Layout) {
__builtin_LinAlg_MatrixAccumulateToMemory(__handle, Arr, StartIdx, Stride,
Layout);
}
template <ComponentEnum CompTy, MatrixUseEnum UseLocal = Use>
typename hlsl::enable_if<Use == MatrixUse::Accumulator && UseLocal == Use,
void>::type
Accumulate(const Matrix<CompTy, M, N, MatrixUse::A, Scope> MatrixA) {
__builtin_LinAlg_MatrixAccumulate(__handle, __handle, MatrixA.__handle);
}
template <ComponentEnum CompTy, MatrixUseEnum UseLocal = Use>
typename hlsl::enable_if<Use == MatrixUse::Accumulator && UseLocal == Use,
void>::type
Accumulate(const Matrix<CompTy, M, N, MatrixUse::B, Scope> MatrixB) {
__builtin_LinAlg_MatrixAccumulate(__handle, __handle, MatrixB.__handle);
}
template <ComponentEnum LHSTy, ComponentEnum RHSTy, SIZE_TYPE K,
MatrixUseEnum UseLocal = Use>
typename hlsl::enable_if<Use == MatrixUse::Accumulator && UseLocal == Use,
void>::type
MultiplyAccumulate(const Matrix<LHSTy, M, K, MatrixUse::A, Scope> MatrixA,
const Matrix<RHSTy, K, N, MatrixUse::B, Scope> MatrixB) {
__builtin_LinAlg_MatrixMatrixMultiplyAccumulate(__handle, MatrixA.__handle,
MatrixB.__handle, __handle);
}
};
// Thread-scope Matrices are read-only. Using a template partial
// specialization for this simplifies the SFINAE-foo above.
template <ComponentEnum ComponentTy, SIZE_TYPE M, SIZE_TYPE N,
MatrixUseEnum Use>
class Matrix<ComponentTy, M, N, Use, MatrixScope::Thread> {
using ElementType = typename __detail::ComponentTypeTraits<ComponentTy>::Type;
using HandleT = __builtin_LinAlgMatrix [[__LinAlgMatrix_Attributes(
ComponentTy, M, N, Use, MatrixScope::Thread)]];
HandleT __handle;
template <MatrixLayoutEnum Layout, MatrixUseEnum UseLocal = Use>
static typename hlsl::enable_if<Use == MatrixUse::A && UseLocal == Use,
Matrix>::type
Load(ByteAddressBuffer Res, uint StartOffset, uint Stride, uint Align = 128) {
Matrix Result;
__builtin_LinAlg_MatrixLoadFromDescriptor(Result.__handle, Res, StartOffset,
Stride, Layout, Align);
return Result;
}
template <MatrixUseEnum UseLocal = Use>
typename hlsl::enable_if<Use == MatrixUse::Accumulator && UseLocal == Use,
void>::type
InterlockedAccumulate(RWByteAddressBuffer Res, uint StartOffset) {
__builtin_LinAlg_MatrixAccumulateToDescriptor(
__handle, Res, StartOffset, 0, MatrixLayout::OuterProductOptimal, 0);
}
};
MatrixUseEnum AccumulatorLayout() {
return (MatrixUseEnum)(__builtin_LinAlg_MatrixQueryAccumulatorLayout());
}
template <ComponentEnum OutTy, ComponentEnum ATy, ComponentEnum BTy,
SIZE_TYPE M, SIZE_TYPE N, SIZE_TYPE K>
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::Wave>
Multiply(const Matrix<ATy, M, K, MatrixUse::A, MatrixScope::Wave> MatrixA,
const Matrix<BTy, K, N, MatrixUse::B, MatrixScope::Wave> MatrixB) {
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::Wave> Result;
__builtin_LinAlg_MatrixMatrixMultiply(Result.__handle, MatrixA.__handle,
MatrixB.__handle);
return Result;
}
template <ComponentEnum CompTy, SIZE_TYPE M, SIZE_TYPE N, SIZE_TYPE K>
Matrix<CompTy, M, N, MatrixUse::Accumulator, MatrixScope::Wave>
Multiply(const Matrix<CompTy, M, K, MatrixUse::A, MatrixScope::Wave> MatrixA,
const Matrix<CompTy, K, N, MatrixUse::B, MatrixScope::Wave> MatrixB) {
Matrix<CompTy, M, N, MatrixUse::Accumulator, MatrixScope::Wave> Result;
__builtin_LinAlg_MatrixMatrixMultiply(Result.__handle, MatrixA.__handle,
MatrixB.__handle);
return Result;
}
template <ComponentEnum OutTy, ComponentEnum ATy, ComponentEnum BTy,
SIZE_TYPE M, SIZE_TYPE N, SIZE_TYPE K>
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::ThreadGroup> Multiply(
const Matrix<ATy, M, K, MatrixUse::A, MatrixScope::ThreadGroup> MatrixA,
const Matrix<BTy, K, N, MatrixUse::B, MatrixScope::ThreadGroup> MatrixB) {
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::ThreadGroup> Result;
__builtin_LinAlg_MatrixMatrixMultiply(Result.__handle, MatrixA.__handle,
MatrixB.__handle);
return Result;
}
template <ComponentEnum CompTy, SIZE_TYPE M, SIZE_TYPE N, SIZE_TYPE K>
Matrix<CompTy, M, N, MatrixUse::Accumulator, MatrixScope::ThreadGroup> Multiply(
const Matrix<CompTy, M, K, MatrixUse::A, MatrixScope::ThreadGroup> MatrixA,
const Matrix<CompTy, K, N, MatrixUse::B, MatrixScope::ThreadGroup>
MatrixB) {
Matrix<CompTy, M, N, MatrixUse::Accumulator, MatrixScope::ThreadGroup> Result;
__builtin_LinAlg_MatrixMatrixMultiply(Result.__handle, MatrixA.__handle,
MatrixB.__handle);
return Result;
}
// Cooperative Vector Replacement API
// Cooperative Vector operates on per-thread vectors multiplying against B
// matrices with thread scope.
template <typename OutputElTy, typename InputElTy, SIZE_TYPE M, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value, vector<OutputElTy, M> >::type
// clang-format on
Multiply(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec) {
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiply(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value, Vec,
MatrixDT);
return Result;
}
template <typename OutputElTy, typename InputElTy, typename BiasElTy,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value, vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, vector<BiasElTy, M> Bias) {
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, Bias, MatrixDT);
return Result;
}
template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
typename BiasElTy, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
InterpretedVector<InputElTy, VecK, InputInterp>::Size >= K,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
vector<BiasElTy, M> Bias) {
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, Bias, MatrixDT);
return Result;
}
template <typename OutputElTy, typename InputElTy, ComponentEnum BiasElTy,
SIZE_TYPE M, SIZE_TYPE K, ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<hlsl::is_arithmetic<InputElTy>::value,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
vector<InputElTy, K> Vec, VectorRef<BiasElTy, M> BiasRef) {
using BiasVecTy =
vector<typename __detail::ComponentTypeTraits<BiasElTy>::Type, M>;
BiasVecTy BiasVec = BiasRef.Buf.template Load<BiasVecTy>(BiasRef.Offset);
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, BiasVec, BiasElTy);
return Result;
}
template <typename OutputElTy, typename InputElTy, ComponentEnum InputInterp,
ComponentEnum BiasElTy, SIZE_TYPE M, SIZE_TYPE VecK, SIZE_TYPE K,
ComponentEnum MatrixDT>
// clang-format off
typename hlsl::enable_if<
InterpretedVector<InputElTy, VecK, InputInterp>::Size >= K,
vector<OutputElTy, M> >::type
// clang-format on
MultiplyAdd(Matrix<MatrixDT, M, K, MatrixUse::A, MatrixScope::Thread> MatrixA,
InterpretedVector<InputElTy, VecK, InputInterp> InterpVec,
VectorRef<BiasElTy, M> BiasRef) {
using BiasVecTy =
vector<typename __detail::ComponentTypeTraits<BiasElTy>::Type, M>;
BiasVecTy BiasVec = BiasRef.Buf.template Load<BiasVecTy>(BiasRef.Offset);
vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiplyAdd(
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, BiasVec, BiasElTy);
return Result;
}
// Outer product functions
template <ComponentEnum OutTy, typename InputElTy, SIZE_TYPE M, SIZE_TYPE N>
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::Thread>
OuterProduct(vector<InputElTy, M> VecA, vector<InputElTy, N> VecB) {
Matrix<OutTy, M, N, MatrixUse::Accumulator, MatrixScope::Thread> Result;
__builtin_LinAlg_MatrixOuterProduct(Result.__handle, VecA, VecB);
return Result;
}
} // namespace linalg
} // namespace dx
#pragma dxc diagnostic pop
#endif // SM 6.10 check and HV version check