story time: tiramisu (ft. quantization)
when I finished training my 10M gpt model, the weights were 40MB.
i needed it to load in the browser.
so i cut it down to 11MB.
now it loads in seconds. no retraining necessary
here's how: 🧵
end result? 4x smaller model. and loads in the browser in a few seconds.
check it out at https://t.co/3SrSys4tcS
check out the code at https://t.co/Ktl4RqwIRS
the solution is int8 quantization
fancy word for saying "map the entire range of float32 vals to 256 integers (-128 to 127)"
scale = max|weights| / 127
quantized = round(weight / scale)
this gets us from 4 bytes per weight to 1 byte per weight.
4x smaller just like that
@_evilbisexual_ you're not crazy, going ikj results in the compiler auto vectorizing it since the strides are all 1. at large N though, this is slower than tiling + intrinsics cuz of the cache
story time: tiramisu (ft. matmul)
my matrix multiply took 14 minutes to train a neural network.
i was able to cut it down to 1 min 40.
this is what the naive implementation looks like 🧵
2nd fix is AVX2 SIMD
the cpu has 256-bit registers == 8 floats at once
one instruction:
_mm256_fmadd_ps(a, b, c) // c += a * b, 8 floats
performing a fused multiply-add op.
when this is combined with tiling this is 8x faster total.
look at the code: https://t.co/uelwBDC7Ol
every iteration of k jumps to a completely different spot in memory. the CPU loads a cache line and uses one float. everything else gets evicted
the first optimization is cache tiling. just work on small blocks that fit in the l2 cache. so you can reuse the same data again
@hiiinternet i've been building tiramisu: an ML framework in raw C++, no external ML libraries. wrote my own autograd engine, SIMD matmul, and a full GPT implementation. plus a CUDA backend. run it live in your browser: https://t.co/W9jdBndrbQ