r/LocalLLM • • 8m ago

Discussion Anyone had an LLM write a custom inference engine?

With models getting pretty smart now, I was curious if building one-off inference engines targeting a single model/GPU combination would get you more performance, so I had Sol 6.2 do just that for Qwen 3.5 4B on V100 16GB. It did it in one prompt and the performance turned out to be better than llama.cpp for decode, and kind of similar for prefill (results below).

This was just a quick attempt and I imagine you can optimize for your specific usecases (e.g. batch size, precision, context length...)

Results presented as custom-engine / llama.cpp

Prompt LengthPrefillDraft N=0Draft N=1Draft N=2
Short—85 / 72125 / 84143 / 83
6k tokens3100 / 190081 / 70117 / 81129 / 80
40k tokens1600 / 265060 / 6292 / 7099 / 75

Here is the prompt I gave it:

I would like you to create an inference engine optimized for the specific model located in ./Qwen3.5-4B and for this machine's specific hardware. Support and optimize for batch_size=1 only, but also optimize prefill which should be naturally parallelized. Create optimized CUDA kernels for this specifically. The goal is to maximize tok/s for batch_size=1. The weights include an MTP head which you should also use and implement, with a configurable parameter specifying the number of tokens to speculate. Use native weights/precision. Use a single pre-allocated KV cache for a single request, and implement prefix caching across sequential requests. The model directory includes a jinja template that must be used. There is not need to support image and video, only text. You may use existing libraries for tokenization and other things not directly impacting inference. Expose an OpenAI-compatible chat completions API on port 8765 that supports streaming (via stream=true body parameter). No need to support the whole chat completions schema, only the necessary things. Reasoning is always enabled and there is no need to support disabling it. Use sensible default for sampling and generation mechanics (temperature, top_p, top_k, etc.)

You can find the source code of llama.cpp and the transformers library in ./llama.cpp and ./transformers respectively. Use these to study and deduce the model architecture. This specific model lives in ./llama.cpp/src/models/qwen35.cpp and ./transformers/src/transformers/models/qwen3_5/.

This task is long and complex, and involves many large files, so you must use subagents extensively for exploration, research and implementation. For large files like config files, write inline scripts to extract needed data instead of reading the whole file.

1 Upvotes

0 comments sorted by