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

Function testMatmul

experimental/legacy/transformer/test_kernels.cpp:71–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

69}
70
71void testMatmul(Context &ctx) {
72 static constexpr size_t M = 4;
73 static constexpr size_t K = 5;
74 static constexpr size_t N = 4;
75 auto gen = std::mt19937(31415);
76 std::array<float, M * K> input1Arr;
77 std::array<float, K * N> input2Arr;
78 std::array<float, M * N> outputArr;
79 randint(input1Arr, gen, 0, 5);
80 range(input2Arr);
81 Tensor input1 = createTensor(ctx, {M, K}, kf32, input1Arr.data());
82 Tensor input2 = createTensor(ctx, {K, N}, kf32, input2Arr.data());
83 Tensor output = createTensor(ctx, {M, N}, kf32, outputArr.data());
84 Kernel op = createKernel(
85 ctx, MatmulShader(256, kShaderMatMul1, kf32, M, K, N),
86 Bindings{input1, input2, output}, {cdiv(M * N, 256), 1, 1});
87 std::promise<void> promise;
88 std::future<void> future = promise.get_future();
89 dispatchKernel(ctx, op, promise);
90 wait(ctx, future);
91 toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
92 LOG(kDefLog, kInfo, "%s", show<float, M, K>(input1Arr, "A").c_str());
93 LOG(kDefLog, kInfo, "%s", show<float, K, N>(input2Arr, "B").c_str());
94 LOG(kDefLog, kInfo, "%s", show<float, M, N>(outputArr, "C").c_str());
95
96 std::array<float, M * N> refOutputArr;
97 std::array<float, K * N> input2ArrT;
98 transpose(input2Arr.data(), input2ArrT.data(), K, N);
99 LOG(kDefLog, kInfo, "%s", show<float, N, K>(input2ArrT, "B'").c_str());
100 ref::matmul_forward_cpu(refOutputArr.data(), input1Arr.data(),
101 input2ArrT.data(), nullptr, 1, M, K, N);
102 LOG(kDefLog, kInfo, show<float, M, N>(refOutputArr, "C (reference)").c_str());
103
104 LOG(kDefLog, kInfo, "Done with Matmul Test");
105 bool passed = isclose(outputArr.data(), refOutputArr.data(), N);
106 assert(passed);
107}
108
109void testTensorPool(Context &ctx) {
110 LOG(kDefLog, kInfo, "Starting Tensor Pool Test");

Callers 1

mainFunction · 0.85

Calls 12

randintFunction · 0.85
rangeFunction · 0.85
createTensorFunction · 0.85
createKernelFunction · 0.85
MatmulShaderFunction · 0.85
cdivFunction · 0.85
dispatchKernelFunction · 0.85
waitFunction · 0.85
toCPUFunction · 0.85
LOGFunction · 0.85
transposeFunction · 0.85
iscloseFunction · 0.85

Tested by

no test coverage detected