-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmma.cuh
319 lines (279 loc) · 13.7 KB
/
mma.cuh
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
//
// Created by oja7 on 12/18/24.
//
#ifndef MMA_CUH
#define MMA_CUH
#include <cuda/std/type_traits>
#include <cublasdx.hpp>
#include <cute/tensor.hpp>
#include "processor/tiling.cuh"
#include "util.cuh"
enum class ResultType {
local,
network
};
#define MULTIPLE_TIMING 0
template<class BlockGEMM, unsigned int sharedSize = 16 * 1024, ResultType r = ResultType::local,
typename MatrixA, typename MatrixB, typename MatrixC, typename MatrixD>
requires (cute::is_tensor_v<MatrixA>
&& cute::is_tensor_v<MatrixB>
&& cute::is_tensor_v<MatrixC>
&& cute::is_tensor_v<MatrixD>
&& sharedSize % THREADS == 0)
__global__ __maxnreg__(128) void deviceCollectiveMMA(
const MatrixA mA, const MatrixB mB, const MatrixC mC, const MatrixD mD, const bool skip = true) {
using ElementD = typename decltype(mD)::value_type;
static_assert(sharedSize % sizeof(ElementD) == 0);
__shared__ __align__(16) ElementD scratch[sharedSize / sizeof(ElementD)];
#if MULTIPLE_TIMING
float clocked = 0.0f;
constexpr auto rounds = 8;
for (uint k = 0; k < rounds; ++k) {
uint64_t start = 0, end = 0;
asm volatile("mov.u64 %0, %%globaltimer;": "=l"(start)::);
#endif
static_assert(rank(mA) == 2 && rank(mB) == 2 && rank(mC) == 2 && rank(mD) == 2);
using ElementC = typename BlockGEMM::MatrixCType;
constexpr auto bM = cute::get<0>(typename BlockGEMM::BlockTiler{});
constexpr auto bN = cute::get<0>(typename BlockGEMM::BlockTiler{});
constexpr typename BlockGEMM::MMA tiledMMA{};
auto accum = cute::partition_fragment_C(tiledMMA, typename BlockGEMM::TilerOut{});
static_assert(cuda::std::is_same_v<ElementC, typename decltype(accum)::value_type>);
// Get the appropriate blocks for this thread block
// use problem shape instead, p_MNK = (cute::ceil_div(M, bM), cute::ceil_div(N, bN), K)
//auto M = cute::get<0>(mC.shape());
//auto N = cute::get<1>(mC.shape());
const auto cta_coordX = cute::idx2crd(blockIdx.x, cute::Shape(cute::ceil_div(cute::get<0>(mC.shape()), bM),
cute::ceil_div(cute::get<1>(mC.shape()), bN)));
const auto cta_coord = cute::make_coord(cute::get<0>(cta_coordX), cute::get<1>(cta_coordX), cute::_);
const auto gA = local_tile(mA, typename BlockGEMM::BlockTiler{}, cta_coord, cute::Step<cute::_1, cute::X,cute::_1>{}); // (BLK_M,BLK_K,k)
const auto gB = local_tile(mB, typename BlockGEMM::BlockTiler{}, cta_coord, cute::Step< cute::X,cute::_1,cute::_1>{}); // (BLK_N,BLK_K,k)
const auto gC = local_tile(mC, typename BlockGEMM::BlockTiler{}, cta_coord, cute::Step<cute::_1,cute::_1, cute::X>{}); // (BLK_M,BLK_N)
const auto gD = local_tile(mD, typename BlockGEMM::BlockTiler{}, cta_coord, cute::Step<cute::_1,cute::_1, cute::X>{}); // (BLK_M,BLK_N)
auto k_tile_iter = cute::make_coord_iterator(size<2>(gA));
int k_tile_count = size<2>(gA);
cute::clear(accum);
typename BlockGEMM::CollectiveMainloop collective_mma{};
collective_mma(
accum,
gA,
gB,
accum,
k_tile_iter, k_tile_count,
cute::Underscore{},
threadIdx.x,
CAST_TO(char, scratch));
// Epilogue
//const auto tCgC = tiledMMA.get_slice(threadIdx.x).partition_C(gC);
const auto tDgD = tiledMMA.get_slice(threadIdx.x).partition_C(gD);
constexpr auto gCStoreOp = cutlass::NumericConverter<typename decltype(gC)::value_type, ElementC>{};
constexpr auto gDLoadOp = cutlass::NumericConverter<ElementC, ElementD>{};
// Assume unary operator
constexpr typename BlockGEMM::FusedEpilogue epilogueOp{};
constexpr auto elems = sharedSize / (THREADS * sizeof(ElementD));
static_assert(size(accum) % elems == 0);
constexpr auto trips = size(accum) / elems;
// Leverage compiler packing for half-precision values into one register
cutlass::AlignedArray<ElementD, elems> rScratch{};
// Prefetch from global to shared memory
#pragma unroll
for (int j = 0; j < elems; ++j) {
scratch[threadIdx.x + j * THREADS] = tDgD(j);
}
#pragma unroll
for (unsigned int i = 0; i < trips; ++i) {
#pragma unroll
for (unsigned int j = 0; j < elems; ++j) {
rScratch[j] = scratch[threadIdx.x + j * THREADS];
if (i + 1 < trips) {
// Eagerly start loads for the next batch, if needed
scratch[threadIdx.x + j * THREADS] = tDgD(j + (i + 1) * elems);
}
}
// Fused Bias Add and Activation Function on register fragment
// Also fuses copy to GMEM.
#pragma unroll
for (int j = 0; j < elems; ++j) {
accum(j + i * elems) = epilogueOp(accum(j + i * elems), gDLoadOp(rScratch[j]));
}
}
__syncthreads();
constexpr auto sCLay = cute::make_layout(cute::Shape<cute::Int<bM>, cute::Int<elems>>{}, cute::LayoutRight{});
const auto sC = cute::make_tensor(cute::make_smem_ptr(scratch), sCLay);
const auto tCsC = tiledMMA.get_slice(threadIdx.x).partition_C(sC);
const auto rIdx = threadIdx.x / elems * elems;
const auto cIdx = threadIdx.x % elems;
#pragma unroll
for (unsigned int i = 0; i < trips; ++i) {
#pragma unroll
for (unsigned int j = 0; j < elems; ++j) {
tCsC(j) = gCStoreOp(accum(j + i * elems));
}
__syncthreads();
#pragma unroll
for (unsigned int j = 0; j < elems; ++j) {
gC(rIdx + j, cIdx + i * elems) = sC(rIdx + j, cIdx);
}
}
__syncthreads();
if (!threadIdx.x) {
if constexpr (r == ResultType::local) {
__threadfence();
}
else {
__threadfence_system();
}
}
__syncthreads();
#if MULTIPLE_TIMING
asm volatile("mov.u64 %0, %%globaltimer;": "=l"(end)::);
clocked += static_cast<float>(end - start) / static_cast<float>(rounds);
}
if (!threadIdx.x && !skip) {
printf("Duration was %fus\n", clocked / 1000.0f);
}
#endif
#if 0
if (!threadIdx.x && !skip) {
cute::print_tensor(mD);
cute::print_tensor(mA);
cute::print_tensor(mB);
cute::print_tensor(mC);
}
#endif
}
__host__ __forceinline__
void testCollective() {
const auto playStream = cudaStreamPerThread;
constexpr auto M = 128;
constexpr auto N = 64;
constexpr auto K = 64;
using inputValueType = cute::half_t;
using outValueType = inputValueType;
using weightValueType = cute::half_t;
using accumulateType = float;
using activation = cutlass::epilogue::thread::ReLU<accumulateType>;
using Operation = BlockMM<inputValueType, weightValueType, accumulateType, 800, activation>;
constexpr auto aSize = (sizeof(inputValueType) * M * K);
constexpr auto abSize = aSize + (sizeof(weightValueType) * N * K);
constexpr auto abcSize = abSize + (sizeof(outValueType) * M * N);
constexpr auto len = abcSize + (sizeof(inputValueType) * cute::max(N, K));
cuda::std::byte* abc;
CHECK_ERROR_EXIT(cudaMallocAsync(&abc, len, playStream));
CHECK_ERROR_EXIT(cudaMemsetAsync(abc, 0, len, playStream));
auto* data = static_cast<cuda::std::byte*>(calloc(len, sizeof(cuda::std::byte)));
auto mAHost = make_tensor(CAST_TO(inputValueType, data),
make_layout(cute::make_shape(M, K), cute::make_stride(K, 1)));
auto mBHost = make_tensor(CAST_TO(weightValueType, data + aSize),
make_layout(cute::make_shape(N, K), cute::make_stride(K, 1)));
// Populate bias vector
CAST_TO(inputValueType, data + abcSize)[0] = static_cast<inputValueType>(1.0);
CAST_TO(inputValueType, data + abcSize)[1] = static_cast<inputValueType>(2.0);
mAHost(0, 0) = static_cast<inputValueType>(0.0);
mAHost(0, 1) = static_cast<inputValueType>(1.0);
mAHost(1, 0) = static_cast<inputValueType>(2.0);
mAHost(1, 1) = static_cast<inputValueType>(3.0);
mBHost(0, 0) = static_cast<weightValueType>(4.0);
mBHost(0, 1) = static_cast<weightValueType>(5.0);
mBHost(1, 0) = static_cast<weightValueType>(6.0);
mBHost(1, 1) = static_cast<weightValueType>(7.0);
CHECK_ERROR_EXIT(cudaMemcpyAsync(abc, data, len, cudaMemcpyHostToDevice, playStream));
const auto mA = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, abc)),
make_layout(cute::make_shape(M, K), cute::make_stride(K, 1)));
const auto mB = make_tensor(cute::make_gmem_ptr(
CAST_TO(weightValueType, abc + aSize)), make_layout(cute::make_shape(N, K), cute::make_stride(K, 1)));
const auto mC = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, abc + abSize)),
make_layout(cute::make_shape(M, N), cute::make_stride(N, 1)));
// bias vector (1, N) broadcast to (M, N)
const auto mD = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, abc + abcSize)),
make_layout(cute::make_shape(M, N), cute::make_stride(0, 1)));
constexpr auto gemmSharedSize = (sizeof(inputValueType) * Operation::GEMM::a_size)
+ (sizeof(weightValueType) + Operation::GEMM::b_size);
constexpr auto sharedSize = cute::max(gemmSharedSize * PIPELINE_STAGES, 128 * 32 * 4);
deviceCollectiveMMA<Operation, sharedSize><<<1, 128, 0, playStream>>>(mA, mB, mC, mD);
CHECK_LAST();
CHECK_ERROR_EXIT(cudaFree(abc));
free(data);
}
__host__ __forceinline__
void testP2PCollective() {
CHECK_ERROR_EXIT(cudaSetDevice(0));
CHECK_ERROR_EXIT(cudaDeviceEnablePeerAccess(1, 0));
// use peer memory for transfer
const auto playStream = cudaStreamPerThread;
constexpr auto M = 128;
constexpr auto N = 64;
constexpr auto K = 1024;
using inputValueType = cute::half_t;
using outValueType = inputValueType;
using weightValueType = cute::half_t;
using accumulateType = float;
using activation = cutlass::epilogue::thread::ReLU<accumulateType>;
using Operation = BlockMM<inputValueType, weightValueType, accumulateType, 800, activation>;
constexpr auto aSize = sizeof(inputValueType) * M * K;
constexpr auto abSize = aSize + sizeof(weightValueType) * N * K;
constexpr auto cSize = sizeof(outValueType) * M * N;
constexpr auto len = abSize + sizeof(inputValueType) * cute::max(N, K);
cuda::std::byte* abc;
CHECK_ERROR_EXIT(cudaMallocAsync(&abc, len, playStream));
CHECK_ERROR_EXIT(cudaMemsetAsync(abc, 0, len, playStream));
cuda::std::byte* peerMemory;
// Switch to PE 1
CHECK_ERROR_EXIT(cudaSetDevice(1));
CHECK_ERROR_EXIT(cudaDeviceEnablePeerAccess(0, 0));
CHECK_ERROR_EXIT(cudaMalloc(&peerMemory, cSize));
CHECK_ERROR_EXIT(cudaPeekAtLastError());
// Switch back to PE 0
CHECK_ERROR_EXIT(cudaSetDevice(0));
auto* data = static_cast<cuda::std::byte*>(calloc(len, sizeof(cuda::std::byte)));
const auto mAHost = make_tensor(CAST_TO(inputValueType, data),
make_layout(cute::make_shape(M, K), cute::make_stride(K, 1)));
const auto mBHost = make_tensor(CAST_TO(weightValueType, data + aSize),
make_layout(cute::make_shape(N, K), cute::make_stride(K, 1)));
// Populate bias vector
CAST_TO(inputValueType, data + abSize)[0] = static_cast<inputValueType>(1.0);
CAST_TO(inputValueType, data + abSize)[1] = static_cast<inputValueType>(2.0);
mAHost(0, 0) = static_cast<inputValueType>(0.0);
mAHost(0, 1) = static_cast<inputValueType>(1.0);
mAHost(1, 0) = static_cast<inputValueType>(2.0);
mAHost(1, 1) = static_cast<inputValueType>(3.0);
mBHost(0, 0) = static_cast<weightValueType>(4.0);
mBHost(0, 1) = static_cast<weightValueType>(5.0);
mBHost(1, 0) = static_cast<weightValueType>(6.0);
mBHost(1, 1) = static_cast<weightValueType>(7.0);
CHECK_ERROR_EXIT(cudaMemcpyAsync(abc, data, len, cudaMemcpyHostToDevice, playStream));
const auto mA = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, abc)),
make_layout(cute::make_shape(M, K), cute::make_stride(K, 1)));
const auto mB = make_tensor(cute::make_gmem_ptr(
CAST_TO(weightValueType, abc + aSize)), make_layout(cute::make_shape(N, K), cute::make_stride(K, 1)));
// This is peer memory
const auto mC = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, peerMemory)),
make_layout(cute::make_shape(M, N), cute::make_stride(N, 1)));
// bias vector (1, N) broadcast to (M, N)
const auto mD = make_tensor(cute::make_gmem_ptr(CAST_TO(inputValueType, abc + abSize)),
make_layout(cute::make_shape(M, N), cute::make_stride(0, 1)));
constexpr auto gemmSharedSize = sizeof(inputValueType) * Operation::GEMM::a_size
+ (sizeof(weightValueType) + Operation::GEMM::b_size);
constexpr auto sharedSize = cute::max(gemmSharedSize * PIPELINE_STAGES, 128 * 32 * 4);
#if MULTIPLE_TIMING
for (uint i = 0; i < 128; ++i) {
deviceCollectiveMMA<Operation, sharedSize, ResultType::network><<<1, 128, sharedSize, playStream>>>
(mA, mB, mC, mD);
}
#endif
deviceCollectiveMMA<Operation, sharedSize, ResultType::network><<<1, 128, sharedSize, playStream>>>
(mA, mB, mC, mD, false);
CHECK_ERROR_EXIT(cudaPeekAtLastError());
// wait for kernel to complete
CHECK_ERROR_EXIT(cudaStreamSynchronize(playStream));
CHECK_ERROR_EXIT(cudaFreeAsync(abc, playStream));
CHECK_ERROR_EXIT(cudaSetDevice(1));
CHECK_ERROR_EXIT(cudaFree(peerMemory));
CHECK_ERROR_EXIT(cudaPeekAtLastError());
CHECK_ERROR_EXIT(cudaDeviceSynchronize());
CHECK_ERROR_EXIT(cudaSetDevice(0));
CHECK_ERROR_EXIT(cudaPeekAtLastError());
CHECK_ERROR_EXIT(cudaStreamSynchronize(playStream));
free(data);
}
#endif //MMA_CUH