Debian Codegraph @

tensilelite/Tensile/Source/TensileTypes.h

raw symbols SCIP
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 *******************************************************************************/
26
27 #ifndef TENSILETYPES_H
28 #define TENSILETYPES_H
29 #include "tensile_bfloat16.h"
30
31 // Using hip header for both NANOO and OCP data types
32 #if defined(__HIPCC__)
33 #include <hip/hip_fp8.h>
34 #endif
35
36 #include "tensile_float8_bfloat8.h"
37
38 #include <algorithm>
39 #include <iostream>
40 #include <math.h>
41 #include <stdio.h>
42 #include <vector>
43
44 #include <cstddef>
45
46 // OpenCL
47 #if Tensile_RUNTIME_LANGUAGE_OCL
48 #include "CL/cl.h"
49 #define TensileStatus cl_int
50 #define tensileStatusSuccess CL_SUCCESS
51 #define tensileStatusFailure -1
52 #define tensileStatusAssertFailure -2
53 #define TensileComplexFloat cl_float2
54 #define TensileComplexDouble cl_double2
55 #define TensileHalf cl_half
56
57 // HIP
58 #else
59 #include <hip/hip_runtime.h>
60 #define TensileStatus hipError_t
61 #define tensileStatusSuccess hipSuccess
62 // FIXME - steal hip error encodings since rocBLAS expects hip error codes to be
63 // returned...
64 #define tensileStatusFailure hipErrorUnknown
65 #define tensileStatusAssertFailure hipErrorRuntimeOther
66 #define TensileHalf _Float16
67 inline std::ostream& operator<<(std::ostream& os, const _Float16& dt)
68 {
69 os << (float)(dt);
70 return os;
71 }
72
73 template <typename t>
74 struct tensile_complex
75 {
76 t x;
77 t y;
78
79 __host__ __device__ tensile_complex() = default;
80
81 constexpr __host__ __device__ tensile_complex(t real, t imag = 0)
82 : x{real}
83 , y{imag}
84 {
85 }
86
87 template <typename u, typename std::enable_if<std::is_arithmetic<u>{}>::type* = nullptr>
88 tensile_complex<t>& operator=(const u a)
89 {
90 x = a;
91 y = 0;
92 return (*this);
93 }
94 };
95
96 template <typename T>
97 inline std::ostream& operator<<(std::ostream& os, const tensile_complex<T>& data)
98 {
99 if(data.y >= 0)
100 {
101 os << data.x << "+" << data.y << "i";
102 }
103 else
104 {
105 os << data.x << data.y << "i";
106 }
107
108 return os;
109 }
110
111 template <typename T>
112 constexpr __host__ __device__ tensile_complex<T> operator+(tensile_complex<T> const& data)
113 {
114 return data;
115 }
116
117 template <typename T>
118 constexpr __host__ __device__ tensile_complex<T> operator-(tensile_complex<T> const& data)
119 {
120 return tensile_complex<T>{-data.x, -data.y};
121 }
122
123 template <typename T>
124 constexpr __host__ __device__ tensile_complex<T> operator+(tensile_complex<T> const& a,
125 tensile_complex<T> const& b)
126 {
127 return tensile_complex<T>{a.x + b.x, a.y + b.y};
128 }
129
130 template <typename T>
131 constexpr __host__ __device__ tensile_complex<T> operator-(tensile_complex<T> const& a,
132 tensile_complex<T> const& b)
133 {
134 return tensile_complex<T>{a.x - b.x, a.y - b.y};
135 }
136
137 template <typename T>
138 constexpr __host__ __device__ tensile_complex<T> operator*(tensile_complex<T> const& a,
139 tensile_complex<T> const& b)
140 {
141 return tensile_complex<T>{a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x};
142 }
143
144 template <typename T>
145 constexpr __host__ __device__ tensile_complex<T> operator/(tensile_complex<T> const& a,
146 tensile_complex<T> const& b)
147 {
148 return tensile_complex<T>{(a.x * b.x + a.y * b.y) / (b.x * b.x + b.y * b.y),
149 (a.y * b.x - a.x * b.y) / (b.x * b.x + b.y * b.y)};
150 }
151
152 template <typename T>
153 constexpr __host__ __device__ tensile_complex<T>& operator+=(tensile_complex<T>& a,
154 tensile_complex<T> const& b)
155 {
156 return (a = a + b);
157 }
158
159 template <typename T>
160 constexpr __host__ __device__ tensile_complex<T>& operator-=(tensile_complex<T>& a,
161 tensile_complex<T> const& b)
162 {
163 return (a = a - b);
164 }
165
166 template <typename T>
167 constexpr __host__ __device__ tensile_complex<T>& operator*=(tensile_complex<T>& a,
168 tensile_complex<T> const& b)
169 {
170 return (a = a * b);
171 }
172
173 template <typename T>
174 constexpr __host__ __device__ tensile_complex<T>& operator/=(tensile_complex<T>& a,
175 tensile_complex<T> const& b)
176 {
177 return (a = a / b);
178 }
179 template <typename T>
180 constexpr __host__ __device__ bool operator==(tensile_complex<T> const& a,
181 tensile_complex<T> const& b)
182 {
183 return (a.x == b.x) && (a.y == b.y);
184 }
185
186 using tensile_float_complex = tensile_complex<float>;
187 using tensile_double_complex = tensile_complex<double>;
188
189 #define TensileComplexFloat tensile_float_complex
190 #define TensileComplexDouble tensile_double_complex
191 #endif // HIP
192
193 #define TensileInt8x4 uint32_t
194 #define TensileInt32 int32_t
195
196 /*******************************************************************************
197 * tensileSetup
198 ******************************************************************************/
199 TensileStatus tensileSetup();
200
201 /*******************************************************************************
202 * tensileTeardown
203 ******************************************************************************/
204 TensileStatus tensileTeardown();
205
206 /*******************************************************************************
207 * tensileCheckStatus
208 ******************************************************************************/
209 #define tensileStatusCheck(RET) \
210 { \
211 TensileStatus tensileCheckStatusTmp = RET; \
212 if(tensileCheckStatusTmp != tensileStatusSuccess) \
213 { \
214 fprintf(stderr, \
215 "ERROR: TensileStatusFailure %i on line %u of %s\n", \
216 tensileCheckStatusTmp, \
217 __LINE__, \
218 __FILE__); \
219 abort(); \
220 } \
221 }
222
223 // class ProblemDims
224 // - stores all dimensions of the problems (sizes and strides)
225 // TensileCreateLibrary.cpp will create a typedef for each specific problem, ie
226 // ProblemDims_Cijk_Ailk_Bljk_SB.
227 template <int FirstStride,
228 int LastStrideD,
229 int LastStrideC,
230 int LastStrideA,
231 int LastStrideB,
232 int NumSizes>
233 class ProblemDims
234 {
235 public:
236 using SizeType = unsigned int;
237
238 // Constructor accepts variable number of sizes:
239 template <typename... Ts>
240 explicit ProblemDims(Ts... args)
241 {
242 init<NumSizes>(args...);
243 };
244
245 const SizeType strideD(int idx) const
246 {
247 return _dims[idx];
248 }
249
250 const SizeType strideC(int idx) const
251 {
252 return _dims[LastStrideD - FirstStride + idx];
253 }
254
255 const SizeType strideA(int idx) const
256 {
257 return _dims[LastStrideD - FirstStride + LastStrideC - FirstStride + idx];
258 }
259
260 const SizeType strideB(int idx) const
261 {
262 return _dims[LastStrideD - FirstStride + LastStrideC - FirstStride + LastStrideA
263 - FirstStride + idx];
264 }
265
266 const SizeType sizes(int idx = 0) const
267 {
268 return _dims[LastStrideD - FirstStride + LastStrideC - FirstStride + LastStrideA
269 - FirstStride + LastStrideB - FirstStride + idx];
270 }
271
272 static int numSizes()
273 {
274 return NumSizes;
275 };
276
277 std::ostream& print(std::ostream& os) const
278 {
279 for(int i = 0; i < NumSizes; i++)
280 {
281 if(i != 0)
282 {
283 os << ", ";
284 };
285 os << _dims[i];
286 };
287 return os;
288 };
289
290 private:
291 template <SizeType I, typename T>
292 void init(T v)
293 {
294 _dims[NumSizes - I] = v;
295 }
296
297 template <SizeType I, typename T, typename... Ts>
298 void init(T v, Ts... args)
299 {
300 _dims[NumSizes - I] = v;
301 init<I - 1>(args...);
302 }
303
304 private:
305 // order: Dstride, Cstride, Astride, Bstrides, sizes
306 SizeType _dims[LastStrideD - FirstStride + LastStrideC - FirstStride + LastStrideA - FirstStride
307 + LastStrideB - FirstStride + NumSizes];
308 };
309
310 // Base template for ProblemKey
311 // - stores the sizes and strides
312 // - supports hash generation and comparison for lookup
313 // TensileCreateLibrary.cpp will create a typedef for each specific problem, ie
314 // ProblemKey_Cijk_Ailk_Bljk_SB.
315 // Some templates below use a parm called ProblemKeyType which can be any of
316 // these generated types.
317 template <int NumSizes>
318 class ProblemKey
319 {
320 public:
321 using SizeType = unsigned int;
322
323 // Constructor accepts variable number of sizes:
324 template <typename... Ts>
325 explicit ProblemKey(Ts... args)
326 : _db(0)
327 {
328 init<NumSizes - 1>(args...);
329 }
330
331 template <int FirstStride,
332 int LastStrideD,
333 int LastStrideC,
334 int LastStrideA,
335 int LastStrideB,
336 int NumDimSizes>
337 explicit ProblemKey(const ProblemDims<FirstStride,
338 LastStrideD,
339 LastStrideC,
340 LastStrideA,
341 LastStrideB,
342 NumDimSizes>& pdims)
343 : _db(0)
344 , _equalStrides(true)
345 {
346 for(int i = 0; i < NumSizes; i++)
347 {
348 _sizes[i] = pdims.sizes(i);
349 }
350
351 _equalStrides
352 = ((pdims.strideD(0) == pdims.strideC(0)) && (pdims.strideD(1) == pdims.strideC(1)));
353 }
354
355 bool operator<(const ProblemKey<NumSizes>& p) const
356 {
357 if(p._equalStrides != this->_equalStrides)
358 return true;
359 for(int i = 0; i < NumSizes; i++)
360 {
361 if(p._sizes[i] < this->_sizes[i])
362 return true;
363 else if(p._sizes[i] > this->_sizes[i])
364 return false;
365 // if equal, continue to next index
366 }
367 return false; // get here if all indices are equal
368 };
369
370 bool operator==(const ProblemKey<NumSizes>& p) const
371 {
372 if(p._equalStrides != this->_equalStrides)
373 return false;
374 for(int i = 0; i < NumSizes; i++)
375 {
376 if(p._sizes[i] != this->_sizes[i])
377 return false;
378 }
379 return true;
380 };
381
382 size_t hash() const
383 {
384 size_t h = 0;
385 for(int i = 0; i < NumSizes; i++)
386 {
387 h ^= _sizes[i] + 0x9b9773e99e3779b9 + (h << 6) + (h >> 2);
388 }
389 return h;
390 }
391
392 const SizeType sizes(int i) const
393 {
394 return _sizes[i];
395 };
396
397 static int numSizes()
398 {
399 return NumSizes;
400 };
401
402 std::ostream& print(std::ostream& os) const
403 {
404 for(int i = 0; i < NumSizes; i++)
405 {
406 if(i != 0)
407 {
408 os << ", ";
409 };
410 os << _sizes[i];
411 };
412 return os;
413 };
414
415 private:
416 template <int I>
417 void init(const SizeType& v)
418 {
419 _sizes[NumSizes - I - 1] = v;
420 }
421
422 template <int I, typename... Ts>
423 void init(const SizeType& v, Ts... args)
424 {
425 _sizes[NumSizes - I - 1] = v;
426 init<I - 1>(args...);
427 }
428
429 private:
430 // Data members:
431 SizeType _sizes[NumSizes];
432 bool _equalStrides;
433 uint32_t _db;
434 };
435
436 //-------------
437 // Distance Functions between two problem sizes - used to find nearest neighbor
438 // or closest solution Assumes p1 and p2 have same number of sizes
439 //-------------
440
441 template <class ProblemKeyType>
442 struct RatioDistance
443 {
444 double operator()(const ProblemKeyType& p1, const ProblemKeyType& p2) const
445 {
446 double distance = 1.0;
447 for(int i = 0; i < p1.numSizes(); i++)
448 {
449 distance += ::fabs(::log(double(p1.sizes(i)) / double(p2.sizes(i))));
450 }
451 return distance;
452 }
453 };
454
455 template <class ProblemKeyType>
456 struct ManhattanDistance
457 {
458 double operator()(const ProblemKeyType& p1, const ProblemKeyType& p2) const
459 {
460 double distance = 0;
461 for(int i = 0; i < p1.numSizes(); i++)
462 {
463 distance += ::fabs(double(p1.sizes(i)) - double(p2.sizes(i)));
464 }
465 return distance;
466 }
467 };
468
469 template <class ProblemKeyType>
470 struct EuclideanDistance
471 {
472 double operator()(const ProblemKeyType& p1, const ProblemKeyType& p2) const
473 {
474 double distance = 0;
475 for(int i = 0; i < p1.numSizes(); i++)
476 {
477 if(p1.sizes(i) > p2.sizes(i))
478 distance += pow(p1.sizes(i) - p2.sizes(i), 2);
479 else
480 distance += pow(p2.sizes(i) - p1.sizes(i), 2);
481 }
482 // distance = sqrt(distance);
483 return distance;
484 }
485 };
486
487 template <class ProblemKeyType>
488 struct RandomDistance
489 {
490 double operator()(const ProblemKeyType& p1, const ProblemKeyType& p2) const
491 {
492 return double(rand());
493 }
494 };
495
496 // ProblemType
497 // Stores the different dimensions (free, index, batch) and other
498 // problem-specific info A single ProblemType may have many ProblemDims
499 // Combination of a ProblemType and ProblemDim defines a Problem
500 class ProblemType
501 {
502 public:
503 ProblemType(const std::vector<unsigned>& indicesFree,
504 const std::vector<unsigned>& indicesSummation,
505 const std::vector<unsigned>& indicesBatch,
506 const std::vector<unsigned>& indexAssignmentsA,
507 const std::vector<unsigned>& indexAssignmentsB)
508 : _indicesFree(indicesFree)
509 , _indicesSummation(indicesSummation)
510 , _indicesBatch(indicesBatch)
511 , _indexAssignmentsA(indexAssignmentsA)
512 , _indexAssignmentsB(indexAssignmentsB)
513 {
514 }
515
516 unsigned lastSummationIdx() const
517 {
518 return _indicesSummation.back();
519 };
520 unsigned free0Idx() const
521 {
522 return _indicesFree[0];
523 };
524 unsigned free1Idx() const
525 {
526 return _indicesFree[1];
527 };
528 bool isBatchIdx(unsigned idx) const
529 {
530 return std::find(_indicesBatch.begin(), _indicesBatch.end(), idx) != _indicesBatch.end();
531 };
532 unsigned numIndicesC() const
533 {
534 return _indicesFree.size() + _indicesBatch.size();
535 };
536
537 const std::vector<unsigned> indexAssignmentsA() const
538 {
539 return _indexAssignmentsA;
540 }
541 const std::vector<unsigned> indexAssignmentsB() const
542 {
543 return _indexAssignmentsB;
544 }
545
546 private:
547 const std::vector<unsigned> _indicesFree;
548 const std::vector<unsigned> _indicesSummation;
549 const std::vector<unsigned> _indicesBatch;
550 const std::vector<unsigned> _indexAssignmentsA;
551 const std::vector<unsigned> _indexAssignmentsB;
552 };
553
554 // These are properties of the problemDims and problemType which drive the
555 // 'assertions'
556 // - Solutions may be optimized so they only work for a subset of
557 // ProblemProperties -
558 // - At runtime, the ProblemProperties are computed for the given ProblemType
559 // and
560 // ProblemDims, and then checked against the Solution requirements.
561 // Must be checked by the runtime before launching the solution
562 struct ProblemProperties
563 {
564
565 // Constructor used in solution tables-
566 // See writeSolutionAndExactTable in TensileCreateLibrary - this constructor
567 // must be in-sync with the table written there.
568 ProblemProperties(unsigned summationElementMultiple,
569 unsigned free0ElementMultiple,
570 unsigned free1ElementMultiple,
571 int approxSize,
572 bool equalStrides,
573 bool allBatchAStridesAreZero,
574 bool allBatchBStridesAreZero)
575 : _summationElementMultiple(summationElementMultiple)
576 , _free0ElementMultiple(free0ElementMultiple)
577 , _free1ElementMultiple(free1ElementMultiple)
578 , _approxSize(approxSize)
579 , _equalStrides(equalStrides)
580 , _allBatchAStridesAreZero(allBatchAStridesAreZero)
581 , _allBatchBStridesAreZero(allBatchBStridesAreZero)
582 , _db(0)
583 {
584 const char* db = std::getenv("TENSILE_DB");
585 if(db)
586 {
587 _db = strtol(db, nullptr, 0);
588 }
589 }
590
591 // Constructor used to compute assertions for a specified problem size
592 template <class ProblemDimsType>
593 ProblemProperties(const ProblemDimsType& pdims, const ProblemType* props)
594 : _db(0)
595 {
596 _summationElementMultiple = 1; // problem summation element multiple
597 auto sumSize = pdims.sizes(props->lastSummationIdx());
598 if((sumSize & 0x7) == 0)
599 _summationElementMultiple = 8;
600 else if((sumSize & 0x3) == 0)
601 _summationElementMultiple = 4;
602 else if((sumSize & 0x1) == 0)
603 _summationElementMultiple = 2;
604
605 auto free0Size = pdims.sizes(props->free0Idx());
606 _free0ElementMultiple = 1; // problem free0 element multiple
607 if((free0Size & 0x7) == 0)
608 _free0ElementMultiple = 8;
609 else if((free0Size & 0x3) == 0)
610 _free0ElementMultiple = 4;
611 else if((free0Size & 0x1) == 0)
612 _free0ElementMultiple = 2;
613
614 auto free1Size = pdims.sizes(props->free1Idx());
615 _free1ElementMultiple = 1; // problem free1 element multiple
616 if((free1Size & 0x7) == 0)
617 _free1ElementMultiple = 8;
618 else if((free1Size & 0x3) == 0)
619 _free1ElementMultiple = 4;
620 else if((free1Size & 0x1) == 0)
621 _free1ElementMultiple = 2;
622
623 bool allBelow1 = true;
624 bool anyBelow1 = false;
625 bool anyBelow4 = false;
626 for(int si = 0; si != pdims.numSizes(); si++)
627 {
628 if(!props->isBatchIdx(si))
629 {
630 auto size = pdims.sizes(si);
631 if(size > 1)
632 allBelow1 = false;
633 if(size == 1)
634 anyBelow1 = true;
635 else if(size < 4)
636 anyBelow4 = true;
637 }
638 }
639 if(allBelow1)
640 _approxSize = 1; // really small
641 else if(anyBelow1 || anyBelow4)
642 _approxSize = 2; // one dim not big enough
643 else
644 _approxSize = 99; // big enough
645
646 _equalStrides = true;
647 // Would need to fix for UseInitialStrides, if UseInitialStrides==1 then
648 // don't subtract 1
649 for(int i = 0; i < props->numIndicesC() - 1; i++)
650 {
651 if(pdims.strideD(i) != pdims.strideC(i))
652 {
653 _equalStrides = false;
654 break;
655 }
656 }
657
658 _allBatchAStridesAreZero = 1;
659 int strideIdx = 0;
660 for(auto& idx : props->indexAssignmentsA())
661 {
662 bool isb = props->isBatchIdx(idx);
663 // Would need to fix for UseInitialStrides
664 auto stride = strideIdx == 0 ? 1 : pdims.strideA(strideIdx - 1);
665 if(isb && stride != 0)
666 {
667 _allBatchAStridesAreZero = 0;
668 }
669 strideIdx++;
670 }
671
672 _allBatchBStridesAreZero = 1;
673 strideIdx = 0;
674 for(auto& idx : props->indexAssignmentsB())
675 {
676 bool isb = props->isBatchIdx(idx);
677 // Would need to fix for UseInitialStrides
678 auto stride = strideIdx == 0 ? 1 : pdims.strideB(strideIdx - 1);
679 if(isb && stride != 0)
680 {
681 _allBatchBStridesAreZero = 0;
682 }
683 strideIdx++;
684 }
685 };
686
687 // Returns True if this AsssertionProperties meet the requirements for the
688 // specified soluition (this object represents the 'Problem')
689 bool validForSolution(const ProblemProperties& solutionRequirements) const
690 {
691 bool rv
692 = (this->_summationElementMultiple >= solutionRequirements._summationElementMultiple)
693 && (this->_free0ElementMultiple >= solutionRequirements._free0ElementMultiple)
694 && (this->_free1ElementMultiple >= solutionRequirements._free1ElementMultiple)
695 && ((this->_approxSize) >= solutionRequirements._approxSize)
696 && ((this->_equalStrides) == solutionRequirements._equalStrides)
697 && ((this->_allBatchAStridesAreZero) >= solutionRequirements._allBatchAStridesAreZero)
698 && ((this->_allBatchBStridesAreZero)
699 >= solutionRequirements._allBatchBStridesAreZero);
700
701 #if 0
702 if(this->_db & 0x10)
703 {
704 if(!rv)
705 {
706 #define check_print(name, oper) \
707 if(!(this->name oper solutionRequirements.name)) \
708 { \
709 std::cout << #name << "failed: !(" << this->name << #oper << solutionRequirements.name \
710 << ")" << std::endl; \
711 }
712
713 std::cout << "Assertion failed:" << std::endl;
714 check_print(_summationElementMultiple, >=);
715 check_print(_free0ElementMultiple, >=);
716 check_print(_free1ElementMultiple, >=);
717 check_print(_approxSize, >=);
718 check_print(_equalStrides, ==);
719 check_print(_allBatchAStridesAreZero, >=);
720 check_print(_allBatchBStridesAreZero, >=);
721 #undef check_print
722 }
723 else
724 {
725 std::cout << "Satisfied asserts!" << std::endl;
726 }
727 }
728 #endif
729
730 return rv;
731 }
732
733 unsigned _summationElementMultiple;
734 unsigned _free0ElementMultiple;
735 unsigned _free1ElementMultiple;
736 int _approxSize;
737 bool _equalStrides;
738 bool _allBatchAStridesAreZero; // for problems: true if all batchA strides are
739 // 0. Solutions: true if all batchA strides
740 // must be 0 to run the kernel. Prob,Soln: x,0:
741 // ok: 0,1: FAILED_ASSERT, 1,1: OK . P>=S
742 bool _allBatchBStridesAreZero; // true if all batchB strides are 0
743 uint32_t _db;
744 };
745
746 #endif