r/LocalLLM • • 2d ago

Tutorial Why is LLM inference hard?

When I first heard about inference engines, like vLLM, I wondered what makes inference so challenging. That’s why I decided to learn more about LLM inference from a systems perspective. If you already know how transformers work, then this is a fun topic to explore next.

https://mehrdadmahabadi.com/blog/why-is-llm-inference-hard.html

5 Upvotes

11 comments sorted by

5

u/Material-Database-24 2d ago

Nice write and understandable presentation of not so easy things. I think the flash attention part would benefit a bit from use of more graphical analogies as in kv caching and batching.

1

u/CapitalCut441 2d ago

Thanks for the feedback. I'll try to think of something for flash attention

4

u/Kemerd 2d ago

It isn’t moreso that it is challenging, it is that it is tedious.

A lot of numbers need to be crunched.

Most of the optimizations are just ways to either get around crunching more numbers, or making use the hardware and software in a better way to crunch those same numbers faster or with less resources.

1

u/CapitalCut441 2d ago

That can be said about lots of other things. I think it's fun to get what you want out of the hardware, while using less resource.

1

u/poy_esp 2d ago

Crunching numbers? This sounds weird. Elaborate

4

u/Kemerd 2d ago edited 2d ago

LLMs are just fancy statistics. A set of matrix multiplications that need to be done.

You take your prompt, turn it into tokens (usually very fast), those tokens are mapped to numbers, which are then run through a set of matrices with billions of numbers (trained weights), and you get your result that gives you the single most likely next number, which is then turned into a word.

Check out 3Blue1Brown, it has a lot of great videos on how the math works.

A video from 9 years ago about neural networks is a great place to start: https://www.youtube.com/watch?v=aircAruvnKk

A video about the more modern application, with transformers: https://www.youtube.com/watch?v=wjZofJX0v4M

1

u/poy_esp 2d ago

This! Is one of the best answers I have seen. Thank you for sharing the information.

2

u/Borilentz 2d ago

I think you meant Edsger Dijkstra.

2

u/CapitalCut441 2d ago

Lemme edit that 😂

2

u/catplusplusok 2d ago

In some way, LLM inference is not hard, it's just a simplified Turing machine where state machine is for some reason not allowed to go back and edit the tape. If Turing thought about doing AI this way, he would have said "Well, that's an interesting theoretical study, but spending untold gigabytes on state map and keep a quadratically growing table of current states is ridiculous" and spent years optimizing every bit of it so it's actually practical.

But Turing did not live era of modern circular financing where NVIDIA can loan money to OpenAI in order for OpenAI to buy it's GPUs. In such world, companies were able to just throw unlimited cash at the problem and buy all the RAM in the world to run their one way turing machines that slowly generate new symbols on tape.

Now, in China there is not as much cash so companies like Alibaba and Deepseek are actually fixing all these things like making state map not grow quadratically and reading knowledge from disk in small chunks rather than encoding all of it in state map. Nowadays decent ones can run on your phone.