Aussie AI
Vector Min and Max
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Vector Min and Max
Finding the maximum or minimum element of a vector is useful, and somewhat relevant to the L1/L2 norms. The maximum is a kind of “metric” of the size of a vector. Also, the maximum function over a vector is used in “greedy decoding” to pick the word with the highest predicted probability, which is then output. The minimum function would give us the least likely word, which might also be interesting if useless.
The simple linear code for vector max is:
float aussie_vector_max(float v[], int n) // Maximum { float vmax = v[0]; for (int i = 1 /*not 0*/; i < n; i++) { if (v[i] > vmax) vmax = v[i]; } return vmax; }
The vector minimum function looks similar in sequential C++ code:
float aussie_vector_min(float v[], int n) // Mininum { float vmin = v[0]; for (int i = 1 /*not 0*/; i < n; i++) { if (v[i] < vmin) vmin = v[i]; } return vmin; }
These functions are crying out for optimizations: loop unrolling, pointer arithmetic, etc. However, what they really need is vectorization. There are parallelized max and min primitives in GPUs and CPU-based AVX intrinsics that you can use.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |