Hey there this is my first post in X π . I'm Sahil currently pursuing my https://t.co/lEoNkteVCw. I'm learning about Ml and it's application. Hope to connect with like minded people who share the same vision.
4/4
> Next is W8A8: both the weights and the activations are 8-bit.
> The general approach is the same as W8A16, with the addition now of activation quantisation and the associated INT8 computation.
Day 5/30 of tinyinfra
After the smaller optimisations, memory bandwidth is the next bottleneck.
1/4
> My GPU is limited to 112gb/s so the next step in terms of getting better throughput is to reduce the amount of data that has to move between memory and the compute units.
3/4
> W8A16 = 8-bit weights, 16-bit activations.
> I've implemented the INT8 kernel, built the loader for the quantised weights and integrated in a bespoke linear layer.
- Decode: retains the loop based implementation, inserting one token at a time.
> This maintains decode throughput whilst still reducing prefill TTFT.
A fairly small change in how the two paths deal with cache insertion, but a far better trade off.
Day 4/30 of tinyinfra
I tried optimising the KV cache layer by vectorising allocate_and_insert (the function that does the memory allocation and KV cache insertion).
1/2
> The original implementation used simple loops which introduced a massive amount of sync overhead.
> Vectorising it reduced TTFT, at the expense of a 9 tokens/sec drop in decode throughput.
2/2
> Instead of using one implementation for both paths, I split it into two methods:
- Prefill: uses a vectorised implementation to insert all the tokens into the KV cache at once.
2/2
To deal with this I wrote a kernel warmup script and have that integrated into the server startup.
Now kernel compilation and all the other init tasks are complete and we're ready to start serving requests.
A minor change, but one which makes a real difference to TTFT.
Day 3/30 of tinyinfra
Switching over to Triton rather than CUDA introduced a very significant cold start penalty.
1/2
Previously the kernels were already compiled but with Triton they need to be compiled on first run. That adds startup overhead and directly affects TTFT.
2/2
> To parallelize it, represent each step as an affine transformation (a, b) with an associative operator (aβ, bβ) β (aβ, bβ) = (aβaβ, aβbβ + bβ)
> Using tl.associative_scan in Triton we do a parallel prefix scan (turning a sequential recurrence into a parallel op)
Wrote a Linear Recurrence kernel (which is a fundamental operation in State Space Models).
1/2
> The challenge is that each hidden state depends on the previous one: hβ = aβΒ·hβββ + bβ
> This makes the naive implementation fundamentally sequential.
2/2
Previously the kernel only supported the decode path. It now supports both:
- Decode
- Speculative decoding using causal masking
The aim was to make the kernel more flexible, improve its execution and get it as close as possible to a production ready implementation.
Day 2/30:
1/2
Ported the PagedAttention kernel from CUDA to Triton and did a few things to improve it in the process.
The kernel is now more general purpose with support for FP16, BF16 and FP32 inputs. I've also changed the softmax computation from per-token to per-block.
My second blog is finally here!
This is something I've wanted to write for a long time
It dives into TokenBPE, breaking down each component, how it works, and how every piece contributes to making the tokenizer faster.
https://t.co/G3PEnHEM0y
2/2
The key thing in GQA is that each KV head has a group of Q heads associated with it. That's where the group size comes into play.
This reduces the number of KV heads by a factor of N, which in turn reduces the KV cache requirements by a factor of N.
Wrote a GQA kernel in Triton.
1/2
At first it was a bit confusing as we're dealing with 3d here. Once you've got the tensor layout worked out though, it's just plain old softmax attention, which you can implement in either the naive or the online softmax fashion.
2/2
> The first kernel port is RoPE (Rotary Positional Embeddings).
> Implemented an in-place Triton kernel that applies RoPE to both the Query and Key tensors.
> The aim is to build reusable kernels which can support a variety of model architectures as TinyInfra matures.
1/2
Day 1/30 of TinyInfra
> Start with the basics: restructure the repo, clean up the codebase and move to a single project config with a pyproject.toml
> I'm also porting over the existing CUDA kernels to Triton and making them more general so that they can support future model
It's been a while since I last worked on my inference engine. I've learned a lot in the months since, and now I'm back to refine it into a complete, shippable package.
I'll be documenting the entire process here on X.
Post: 20
The request serving layer has now been incorporated into my inference engine.
Every request is given a slot, the requests are continuously grouped into batches, and the generation takes place within the batch that is active.
I also added:
β Streaming & non-streaming APIs
β Request cancellation mid-generation
β Structured logging instead of print statements
β Request lifecycle + failure tracking
β Token throughput logging
The demonstration illustrates it in action.
It remains crude: serious work is needed to optimize throughput, the architecture contains too many hard-coded elements, and it currently supports only one model.
What's interesting now is starting to turn this into a modular and efficient inference runtime and it's getting more and more messy especially the codebaseπ.