Lookup Table (LUT)
This is a technique that Evgenii used for Fp8 at Tesla, also saw this at NVIDIA.
https://federico-busato.github.io/Modern-CPP-Programming/htmls/23.Optimization_II.html
Lookup table (LUT) is a memoization technique which allows replacing runtime computation with precomputed value.
You essentially precompute values at compile time, by making use of constexpr
.
template<int SIZE, typename Lambda>
constexpr std::array<float, SIZE> build(Lambda lambda) {
std::array<float, SIZE> array{};
for (int i = 0; i < SIZE; i++)
array[i] = lambda(i);
return array;
}
float log10(int value) {
constexpr auto lamba = [](int i) { return std::log10f((float) i); };
static constexpr auto table = build<100>(lambda);
return table[value];
}