Aussie AI
Spot the Buggy MatMul
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Spot the Buggy MatMul
Have a look at this code for a matrix-vector multiplication using vector dot product. It took me a long time to realize what was wrong with this. Can you spot the bug?
void aussie_matmul_vector_basic1_buggy(ymatrix m, float v[], int n) { // Basic matrix-by-vector using vector dot products.. for (int i = 0; i < n; i++) { float* rowvector = &m[i][0]; float sum = aussie_vecdot_basic(rowvector, v, n); // Dot product v[i] = sum; } }
The bug is a kind of aliasing problem here:
v[i] = sum; // Bug!
It looks correct, but it's wrong.
The computation of v[i]
is setting its value in the middle of the loop,
and then going around for the next matrix row, which will then use that
newly calculated v[i]
value as if it was part of the input vector.
Because I'm misusing “v
” as both the input and output vector,
parts of the output vector will get used as the input vector.
It's a very insidious type of aliasing bug, and many of my simple unit tests
with zero matrices and identity matrices were still succeeding.
It's my fault for trying to do matrix-vector multiplication as an element-wise vector method.
The solution is simple: matrix-vector multiplication needs a third operand for the output vector.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |