Debian Codegraph @

tensilelite/Tensile/Source/ReductionTemplate.h

raw symbols SCIP
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2022-2023 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 #pragma once
28 #include "memory_gfx.h"
29 #include <hip/hip_runtime.h>
30
31 template <size_t Begin, size_t End, size_t Increment = 1>
32 struct static_for
33 {
34 template <typename F>
35 __host__ __device__ constexpr void operator()(F const& f) const
36 {
37 if constexpr(Begin < End)
38 {
39 f(Begin);
40 static_for<Begin + Increment, End>()(f);
41 }
42 }
43 };
44
45 constexpr size_t max(size_t a, size_t b)
46 {
47 return (a > b) ? a : b;
48 }
49
50 template <typename DataTypeCompute, typename DataTypeOut, size_t MT0, size_t MT1, size_t VW>
51 __device__ inline void
52 reductionKernel_ijk(DataTypeCompute* in, DataTypeOut* out, int m, int n, int strideJ)
53 {
54 __shared__ DataTypeCompute block[MT0 * MT1 * VW];
55
56 int mod = hipThreadIdx_x % MT0;
57 int row = hipThreadIdx_x / MT0;
58
59 int soffset = hipBlockIdx_x * MT0 * VW;
60 int voffset = mod * VW;
61 int idx = voffset + soffset;
62
63 int soffsetBytes = soffset * sizeof(DataTypeCompute);
64 int soffsetBytes_bias = soffset * sizeof(DataTypeOut);
65 int num_records = strideJ * n * sizeof(DataTypeCompute);
66 int num_records_bias = m * sizeof(DataTypeOut);
67
68 constexpr size_t sumLength = max((size_t)1, VW - 1);
69 DataTypeCompute sum[sumLength] = {0};
70 if(idx + (VW - 1) < m)
71 {
72 for(int i = 0; i < n; i += MT1)
73 {
74 int currRow = row + i;
75 int rowStride = currRow * strideJ + voffset;
76 DataTypeCompute tmp[VW];
77 static_for<0, VW>()([&](int vw) {
78 buffer_load<DataTypeCompute, sizeof(DataTypeCompute)>(
79 tmp[vw],
80 reinterpret_cast<void*>(const_cast<DataTypeCompute*>(in)),
81 (uint32_t)((rowStride + vw) * sizeof(DataTypeCompute)),
82 (uint32_t)(soffsetBytes),
83 num_records);
84 });
85 static_for<0, VW>()([&](int vw) { sum[vw] += tmp[vw]; });
86 }
87 static_for<0, VW>()([&](int vw) { block[(mod * MT1 + row) * VW + vw] = sum[vw]; });
88 __syncthreads();
89 if(hipThreadIdx_x < MT0)
90 {
91 for(int i = 1; i < MT1; i += 1)
92 {
93 static_for<0, VW>()(
94 [&](int vw) { sum[vw] += block[(i + hipThreadIdx_x * MT1) * VW + vw]; });
95 }
96 // This is a 1D bias
97 static_for<0, VW>()([&](int vw) {
98 buffer_store<DataTypeOut, sizeof(DataTypeOut)>(
99 (DataTypeOut)sum[vw],
100 reinterpret_cast<void*>(const_cast<DataTypeOut*>(out)),
101 (uint32_t)(voffset + vw) * sizeof(DataTypeOut),
102 (uint32_t)(soffsetBytes_bias),
103 num_records_bias);
104 });
105 }
106 }
107 else if(idx < m)
108 {
109 for(int i = 0; i < n; i += MT1)
110 {
111 int currRow = row + i;
112 int rowStride = currRow * strideJ + voffset;
113 constexpr size_t tmpLength = max((size_t)1, VW - 1);
114 DataTypeCompute tmp[tmpLength];
115 static_for<0, VW - 1>()([&](int vw) {
116 buffer_load<DataTypeCompute, sizeof(DataTypeCompute)>(
117 tmp[vw],
118 reinterpret_cast<void*>(const_cast<DataTypeCompute*>(in)),
119 (uint32_t)((rowStride + vw) * sizeof(DataTypeCompute)),
120 (uint32_t)(soffsetBytes),
121 num_records);
122 });
123 static_for<0, VW - 1>()([&](int vw) { sum[vw] += tmp[vw]; });
124 }
125 static_for<0, VW - 1>()([&](int vw) { block[(mod * MT1 + row) * VW + vw] = sum[vw]; });
126 __syncthreads();
127 if(hipThreadIdx_x < MT0)
128 {
129
130 for(int i = 1; i < MT1; i += 1)
131 {
132 static_for<0, VW - 1>()(
133 [&](int vw) { sum[vw] += block[(i + hipThreadIdx_x * MT1) * VW + vw]; });
134 }
135 // This is a 1D bias
136 static_for<0, VW - 1>()([&](int vw) {
137 buffer_store<DataTypeOut, sizeof(DataTypeOut)>(
138 (DataTypeOut)sum[vw],
139 reinterpret_cast<void*>(const_cast<DataTypeOut*>(out)),
140 (uint32_t)(voffset + vw) * sizeof(DataTypeOut),
141 (uint32_t)(soffsetBytes_bias),
142 num_records_bias);
143 });
144 }
145 }
146 else
147 {
148 static_for<0, VW>()([&](int vw) { block[(mod * MT1 + row) * VW + vw] = 0; });
149 __syncthreads();
150 }
151 }