Skip to content
Navigation Menu
{{ message }}
forked from AnswerDotAI/gpu.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernels.cpp
More file actions
283 lines (265 loc) · 11.1 KB
/
Copy pathtest_kernels.cpp
File metadata and controls
283 lines (265 loc) · 11.1 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
#include <array>
#include <future>
#include <memory>
#include <random>
#include "gpu.h"
#include "utils/array_utils.h"
#include "utils/logging.h"
#include "llmc/reference_impls.h"
#include "shaders.h"
using namespace gpu;
void testResidual(Context &ctx) {
constexpr size_t N = 200000;
constexpr size_t workgroupSize = 256;
std::array<float, N> input1Arr;
std::array<float, N> input2Arr;
range(input1Arr);
range(input2Arr);
std::array<float, N> outputArr;
Tensor input1 = createTensor(ctx, {N}, kf32, input1Arr.data());
Tensor input2 = createTensor(ctx, {N}, kf32, input2Arr.data());
Tensor output = createTensor(ctx, {N}, kf32, outputArr.data());
std::promise<void> promise;
std::future<void> future = promise.get_future();
KernelCode shaderCode = {kShaderResidual, workgroupSize, kf32};
LOG(kDefLog, kInfo, "Shader Code :\n%s", shaderCode.data.c_str());
Kernel op =
createKernel(ctx, {kShaderResidual, workgroupSize, kf32},
Bindings{input1, input2, output}, {cdiv(N, workgroupSize), 1, 1});
dispatchKernel(ctx, op, promise);
wait(ctx, future);
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
LOG(kDefLog, kInfo, "%s",
show<float, N, 1>(outputArr, "Residual Output").c_str());
std::array<float, N> outputRef;
ref::residual_forward_cpu(outputRef.data(), input1Arr.data(),
input2Arr.data(), N);
LOG(kDefLog, kInfo, "%s",
show<float, N, 1>(outputRef, "Residual Reference Output").c_str());
assert(isclose(outputArr.data(), outputRef.data(), N));
LOG(kDefLog, kInfo, "Done with Residual Test");
}
void testHadamard(Context &ctx) {
constexpr size_t N = 200000;
constexpr size_t workgroupSize = 256;
std::array<float, N> input1Arr;
std::array<float, N> input2Arr;
range(input1Arr);
range(input2Arr);
std::array<float, N> outputArr;
Tensor input1 = createTensor(ctx, {N}, kf32, input1Arr.data());
Tensor input2 = createTensor(ctx, {N}, kf32, input2Arr.data());
Tensor output = createTensor(ctx, {N}, kf32, outputArr.data());
KernelCode shaderCode = {kShaderHadamard, workgroupSize, kf32};
LOG(kDefLog, kInfo, "Shader Code :\n%s", shaderCode.data.c_str());
std::promise<void> promise;
std::future<void> future = promise.get_future();
Kernel op =
createKernel(ctx, {kShaderHadamard, workgroupSize, kf32},
Bindings{input1, input2, output}, {cdiv(N, workgroupSize), 1, 1});
dispatchKernel(ctx, op, promise);
wait(ctx, future);
LOG(kDefLog, kInfo, "%s",
show<float, N, 1>(outputArr, "Hadamard Output").c_str());
}
void testMatmul(Context &ctx) {
static constexpr size_t M = 4;
static constexpr size_t K = 5;
static constexpr size_t N = 4;
auto gen = std::mt19937(31415);
std::array<float, M * K> input1Arr;
std::array<float, K * N> input2Arr;
std::array<float, M * N> outputArr;
randint(input1Arr, gen, 0, 5);
range(input2Arr);
Tensor input1 = createTensor(ctx, {M, K}, kf32, input1Arr.data());
Tensor input2 = createTensor(ctx, {K, N}, kf32, input2Arr.data());
Tensor output = createTensor(ctx, {M, N}, kf32, outputArr.data());
Kernel op = createKernel(
ctx, MatmulShader(256, kShaderMatMul1, kf32, M, K, N),
Bindings{input1, input2, output}, {cdiv(M * N, 256), 1, 1});
std::promise<void> promise;
std::future<void> future = promise.get_future();
dispatchKernel(ctx, op, promise);
wait(ctx, future);
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
LOG(kDefLog, kInfo, "%s", show<float, M, K>(input1Arr, "A").c_str());
LOG(kDefLog, kInfo, "%s", show<float, K, N>(input2Arr, "B").c_str());
LOG(kDefLog, kInfo, "%s", show<float, M, N>(outputArr, "C").c_str());
std::array<float, M * N> refOutputArr;
std::array<float, K * N> input2ArrT;
transpose(input2Arr.data(), input2ArrT.data(), K, N);
LOG(kDefLog, kInfo, "%s", show<float, N, K>(input2ArrT, "B'").c_str());
ref::matmul_forward_cpu(refOutputArr.data(), input1Arr.data(),
input2ArrT.data(), nullptr, 1, M, K, N);
LOG(kDefLog, kInfo, show<float, M, N>(refOutputArr, "C (reference)").c_str());
LOG(kDefLog, kInfo, "Done with Matmul Test");
bool passed = isclose(outputArr.data(), refOutputArr.data(), N);
assert(passed);
}
void testTensorPool(Context &ctx) {
LOG(kDefLog, kInfo, "Starting Tensor Pool Test");
// Test using the tensor pool to prepare tensor buffers for kernel invocation
TensorPool pool = ctx.pool;
std::array<float, 6> inputArr = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
Tensor input = createTensor(ctx, {2, 3}, kf32, inputArr.data());
Tensor output = createTensor(ctx, {2, 3}, kf32);
for (int i = 0; i < 10; i++) {
Tensor t = createTensor(ctx, {2, 3}, kf32);
}
// initializing a gpu buffer w/ value and then copy it back to CPU
std::array<float, 6> initValue = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
LOG(kDefLog, kInfo, "making tensors with init");
Tensor tInit = createTensor(ctx, {2, 3}, kf32, initValue.data());
LOG(kDefLog, kInfo, "Done with Tensor Pool Test");
std::array<float, 6> targetValue;
toCPU(ctx, tInit, targetValue.data(), sizeof(initValue));
LOG(kDefLog, kInfo, "%s",
show<float, 2, 3>(initValue, "initialized GPU value").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, 2, 3>(targetValue, "To CPU from GPU").c_str());
LOG(kDefLog, kInfo, "Done with Tensor Pool Test");
}
void testGelu(Context &ctx) {
static constexpr size_t N = 3072;
std::array<float, N> inputArr;
// range(inputArr);
auto gen = std::mt19937(31415);
// TODO(avh): investigate - on metal tanh seems to produce nan for values > 10
randint(inputArr, gen, 0, 10); // for debugging
std::array<float, N> outputArr;
Tensor geluIn = createTensor(ctx, {N}, kf32, inputArr.data());
Tensor geluOut = createTensor(ctx, {N}, kf32, outputArr.data());
LOG(kDefLog, kInfo, "Creating GELU Shader");
KernelCode shader = {kShaderGelu, 256, kf32};
Kernel op = createKernel(ctx, shader, Bindings{geluIn, geluOut},
{cdiv(N, 256), 1, 1});
LOG(kDefLog, kInfo, "Workgroup size: %s",
toString(shader.workgroupSize).c_str());
LOG(kDefLog, kInfo, "dispatching GELU Shader");
std::promise<void> promise;
std::future<void> future = promise.get_future();
dispatchKernel(ctx, op, promise);
wait(ctx, future);
toCPU(ctx, geluOut, outputArr.data(), sizeof(outputArr));
LOG(kDefLog, kInfo, "%s", show<float, N, 1>(inputArr, "GELU Input").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, N, 1>(outputArr, "GELU Output").c_str());
std::array<float, N> refOutputArr;
ref::gelu_forward_cpu(refOutputArr.data(), inputArr.data(), N);
LOG(kDefLog, kInfo, "%s",
show<float, N, 1>(refOutputArr, "GELU Reference Output").c_str());
bool passed = isclose(outputArr.data(), refOutputArr.data(), N);
assert(passed);
LOG(kDefLog, kInfo, "Gelu passed? %d", passed);
LOG(kDefLog, kInfo, "Done with Gelu Test");
}
void testLayerNorm(Context &ctx) {
struct LNParam {
uint32_t N; // check
uint32_t C;
};
constexpr size_t N = 6;
constexpr size_t C = 3072;
std::mt19937 gen(31415);
std::array<float, N * C> inputArr;
randint(inputArr, gen, 0, 3);
// range(inputArr);
std::array<float, N * C> outputArr;
std::array<float, C> weightArr;
std::array<float, C> biasArr;
Tensor input = createTensor(ctx, {N, C}, kf32, inputArr.data());
LNParam params = {N, C};
randint(weightArr, gen, 0, 5); // populate randomly
randint(biasArr, gen, 0, 5);
Tensor weight = createTensor(ctx, {C}, kf32, weightArr.data());
Tensor bias = createTensor(ctx, {C}, kf32, biasArr.data());
Tensor output = createTensor(ctx, {N, C}, kf32, outputArr.data());
std::promise<void> promise;
std::future<void> future = promise.get_future();
Kernel op = createKernel(ctx, {kShaderLayerNorm1, 256, kf32},
Bindings{input, weight, bias, output},
/* n threads */ {N, 1, 1}, params);
dispatchKernel(ctx, op, promise);
wait(ctx, future);
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
LOG(kDefLog, kInfo, "%s",
show<float, N, C>(inputArr, "LayerNorm Input").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, 1, C>(weightArr, "LayerNorm Weight").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, 1, C>(biasArr, "LayerNorm Bias").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, N, C>(outputArr, "LayerNorm Output").c_str());
std::array<float, N * C> refOutputArr;
ref::layernorm_forward_cpu(refOutputArr.data(), inputArr.data(),
weightArr.data(), biasArr.data(), N, 1, C);
LOG(kDefLog, kInfo, "%s",
show<float, N, C>(refOutputArr,
"LayerNorm Reference Implementation Output")
.c_str());
bool passed = isclose(outputArr.data(), refOutputArr.data(), N * C);
assert(passed);
LOG(kDefLog, kInfo, "LayerNorm passed? %d", passed);
LOG(kDefLog, kInfo, "Done with LayerNorm Test");
}
void testSoftmax(Context &ctx) {
struct SoftmaxParam {
uint32_t N;
uint32_t C;
};
static constexpr size_t B = 6; // batch size
static constexpr size_t T = 8; // token index
static constexpr size_t C = 3072; // input channels
std::array<float, B * T * C> inputArr;
std::array<float, B * T * C> outputArr;
std::mt19937 gen(31415);
randint(inputArr, gen, 0, 3);
Tensor input = createTensor(ctx, {B * T, C}, kf32, inputArr.data());
Tensor output = createTensor(ctx, {B * T, C}, kf32, outputArr.data());
LOG(kDefLog, kInfo, "num threads: %d", B * T);
std::promise<void> promise;
std::future<void> future = promise.get_future();
Kernel op = createKernel(
ctx, {kShaderSoftmax1, 256, kf32}, Bindings{input, output},
Shape{cdiv(B * T, 256), 1, 1}, SoftmaxParam{B * T, C});
dispatchKernel(ctx, op, promise);
wait(ctx, future);
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
LOG(kDefLog, kInfo, "%s",
show<float, B * T, C>(inputArr, "Softmax Input").c_str());
LOG(kDefLog, kInfo, "%s",
show<float, B * T, C>(outputArr, "Softmax Output").c_str());
std::array<float, B * T * C> refOutputArr;
ref::softmax_forward_cpu(refOutputArr.data(), inputArr.data(), B * T, C);
LOG(kDefLog, kInfo, "%s",
show<float, B * T, C>(refOutputArr, "Softmax reference Output").c_str());
LOG(kDefLog, kInfo, "number of elements: %d", B * T * C);
bool passed = isclose(outputArr.data(), refOutputArr.data(), B * T * C);
assert(passed);
LOG(kDefLog, kInfo, "Softmax passed? %d", passed);
LOG(kDefLog, kInfo, "Done with Softmax Test");
}
void testAttention(Context &ctx) {
static constexpr size_t B = 6;
static constexpr size_t T = 32; // token index
static constexpr size_t C = 3072; // input channels
static constexpr size_t QKV_DIM = 256;
static constexpr size_t N_HEADS = 12;
static constexpr size_t OC =
QKV_DIM * N_HEADS * 3; // output channels, 3 for Q, K, V
std::array<float, B * T * C> inputArr;
std::array<float, B * OC> outputArr;
std::array<float, C * OC> weightArr;
}
int main(int argc, char **argv) {
Context ctx = createContext();
testTensorPool(ctx);
testResidual(ctx);
testHadamard(ctx);
testMatmul(ctx);
testGelu(ctx);
testLayerNorm(ctx);
testSoftmax(ctx);
LOG(kDefLog, kInfo, "Done with all tests");
}
You can’t perform that action at this time.
