MCPcopy Create free account
hub / github.com/AnswerDotAI/gpu.cpp / testLayerNorm

Function testLayerNorm

experimental/legacy/transformer/test_kernels.cpp:168–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

166}
167
168void testLayerNorm(Context &ctx) {
169 struct LNParam {
170 uint32_t N; // check
171 uint32_t C;
172 };
173 constexpr size_t N = 6;
174 constexpr size_t C = 3072;
175 std::mt19937 gen(31415);
176 std::array<float, N * C> inputArr;
177 randint(inputArr, gen, 0, 3);
178 // range(inputArr);
179 std::array<float, N * C> outputArr;
180 std::array<float, C> weightArr;
181 std::array<float, C> biasArr;
182 Tensor input = createTensor(ctx, {N, C}, kf32, inputArr.data());
183 LNParam params = {N, C};
184 randint(weightArr, gen, 0, 5); // populate randomly
185 randint(biasArr, gen, 0, 5);
186 Tensor weight = createTensor(ctx, {C}, kf32, weightArr.data());
187 Tensor bias = createTensor(ctx, {C}, kf32, biasArr.data());
188 Tensor output = createTensor(ctx, {N, C}, kf32, outputArr.data());
189 std::promise<void> promise;
190 std::future<void> future = promise.get_future();
191 Kernel op = createKernel(ctx, {kShaderLayerNorm1, 256, kf32},
192 Bindings{input, weight, bias, output},
193 /* n threads */ {N, 1, 1}, params);
194 dispatchKernel(ctx, op, promise);
195 wait(ctx, future);
196 toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
197 LOG(kDefLog, kInfo, "%s",
198 show<float, N, C>(inputArr, "LayerNorm Input").c_str());
199 LOG(kDefLog, kInfo, "%s",
200 show<float, 1, C>(weightArr, "LayerNorm Weight").c_str());
201 LOG(kDefLog, kInfo, "%s",
202 show<float, 1, C>(biasArr, "LayerNorm Bias").c_str());
203 LOG(kDefLog, kInfo, "%s",
204 show<float, N, C>(outputArr, "LayerNorm Output").c_str());
205 std::array<float, N * C> refOutputArr;
206 ref::layernorm_forward_cpu(refOutputArr.data(), inputArr.data(),
207 weightArr.data(), biasArr.data(), N, 1, C);
208 LOG(kDefLog, kInfo, "%s",
209 show<float, N, C>(refOutputArr,
210 "LayerNorm Reference Implementation Output")
211 .c_str());
212 bool passed = isclose(outputArr.data(), refOutputArr.data(), N * C);
213 assert(passed);
214 LOG(kDefLog, kInfo, "LayerNorm passed? %d", passed);
215 LOG(kDefLog, kInfo, "Done with LayerNorm Test");
216}
217
218void testSoftmax(Context &ctx) {
219

Callers 1

mainFunction · 0.85

Calls 8

randintFunction · 0.85
createTensorFunction · 0.85
createKernelFunction · 0.85
dispatchKernelFunction · 0.85
waitFunction · 0.85
toCPUFunction · 0.85
LOGFunction · 0.85
iscloseFunction · 0.85

Tested by

no test coverage detected