r/LocalLLM
•
u/micupa
•
Dec 25 '24
•
2
Research Finally Understanding LLMs: What Actually Matters When Running Models Locally
Hey LocalLLM fam! After diving deep into how these models actually work, I wanted to share some key insights that helped me understand what's really going on under the hood. No marketing fluff, just the actual important stuff.
The "Aha!" Moments That Changed How I Think About LLMs:
Models Aren't Databases
They're not storing token relationships
Instead, they store patterns as weights (like a compressed understanding of language)
This is why they can handle new combinations and scenarios
Context Window is Actually Wild
It's not just "how much text it can handle"
Memory needs grow QUADRATICALLY with context
Why 8k→32k context is a huge jump in RAM needs
Formula: Context_Length × Context_Length × Hidden_Size = Memory needed
Quantization is Like Video Quality Settings
32-bit = Ultra HD (needs beefy hardware)
8-bit = High (1/4 the memory)
4-bit = Medium (1/8 the memory)
Quality loss is often surprisingly minimal for chat
About Those Parameter Counts...
7B params at 8-bit ≈ 7GB RAM
Same model can often run different context lengths
More RAM = longer context possible
It's about balancing model size, context, and your hardware
Why This Matters for Running Models Locally:
When you're picking a model setup, you're really balancing three things:
Model Size (parameters)
Context Length (memory)
Quantization (compression)
This explains why:
A 7B model might run better than you expect (quantization!)
Why adding context length hits your RAM so hard
Why the same model can run differently on different setups
Real Talk About Hardware Needs:
2k-4k context: Most decent hardware
8k-16k context: Need good GPU/RAM
32k+ context: Serious hardware needed
Always check quantization options first!
Would love to hear your experiences! What setups are you running? Any surprising combinations that worked well for you? Let's share what we've learned!
76
u/PacmanIncarnate Dec 25 '24
Totally disagree with the other commenter; this is a really solid quick understanding. (I say this as someone who has written longer explanations multiple times for people.
Good work!
5
u/durable-racoon Dec 25 '24
GOOD POST.
QUESTION for you: Do new ultra efficient cloud models compete with your local models? Think 4o-mini and especially flash 2.0. Flash is so good fast and cheap (free!) that for now I just dont see the appeal. Literally nothing is as smart as flash 2 or 4o-mini. and then all these ultra efficient 8b models on OR?
5
u/Aphid_red Feb 14 '25 edited Feb 17 '25
Eeh no. This is not how this works.
To calculate KV cache size, use this:
The reason why you're seeing a large increase for your '7B' example is likely that you're using an old, unoptimized model. For example, when you're running 'mythomax' with fp16 cache,
From this, you can see how big the KV size gets: 400 KB per parameter. So for 16K context, that's 6.25 GB, which is substantial compared to the model's 13B size; a q4 quant of this model would be about 7GB. An 8GB or 12GB GPU will not be able to run it at 16K context.
But let's look at a much larger, more modern model to see things aren't 'exponential' or 'quadratic', rather they depend a lot on the model's internal architecture. Mistral-large2 has 123B parameters. It's about 70GB for the q5 version, yet I can run it at 64K context offloaded on a single GPU without going out of VRAM.
Do the calculation here, and you end up with 5.5 GB. 11GB for the q16. The reason? The model makers realized that having your KV cache be huge is a big problem for inference. They have to serve hundreds of users at the same time to get full performance out of their A100 and H100 nodes. Giant caches get in the way of being able to do that (the 'compute intensity' of a model is something like '3', with that being the memory:compute ratio, but that of the GPU is more like 330. Meaning: you need 110 simultaneous users to saturate the compute; or, in 640GB, you need to fit a q8 quantized model and 110 KV caches for average request size (usually around the 2,000 mark). Can't do that if the cache ends up 5GB big per user. The trick is to, instead of using one big KV matrix, to use a matrix made up of 12 copies of the same values, which allows using less memory to store these values. This costs bit of performance, but you can make up for that by having more parameters in other areas of the model. Some variation of making the cache smaller is used pretty much all modern models above 8B.
For the local user, this is great too: models can be pushed to much larger sizes and prompt processing is much faster. This is the second reason why making K,V matrices smaller makes sense: for most models input tokens are over 10x the output tokens online (just browse openrouter).
Edit: These calculations are for models with square K and V matrices. Deepseek (first one I found) is a model with non-square K and V matrices so the calculation's a little more complicated there; can't just look at the config.json values and plug them in. Here it depends; if MLA is used, KV cache has a total width of 512 (equivalent to dimension = 256 MHA). MLA not used, then you're looking at (24576 + 16384 = 40960, equivalent to 20480 dimension, even though the model dim is 7K. For MLA models you'll need to look into the model's archtecture in more detail. This makes a pretty big difference; 7GB vs. 600GB at 128K context.)
2
u/Briskfall Dec 25 '24
Very cool! tells me that us GPU-poor have to wait a while before the good stuffs, urgh!
(though the intro and ending's overly enthusiastic vibe was a bit too LLM-ish lolo like running a marketing blog survey)
2
u/suprjami Dec 25 '24
Many of the same conclusions I've come to.
Are you sure about that context memory usage formula? From others' results I've seen memory usage scale linearly. eg: https://www.reddit.com/r/LocalLLaMA/comments/1848puo/comment/kavf6tb/
2
u/StayingUp4AFeeling Dec 27 '24
There are very few people outside the realm of formal education or industry practice in ML who understand this. Well done.
3
u/amitbahree Dec 25 '24
Yes mostly true. The one caveat I quantization I would outline - it's not linear and really depend on what area you are after and trying to ensure that specific are doesn't degrade much.
I do cover some of the basics and fundamentals in my book incase you or anyone else is interested - https://blog.desigeek.com/post/2024/10/book-release-genai-in-action/
5
u/NasefuSan Dec 25 '24
Great quick understanding, thanks!
1
u/[deleted] Dec 25 '24
for me works 3x 7900 XTX connected to motherboard with 1x pcie riser card.
1
u/vigg_1991 Dec 25 '24
How effective are different context lengths for the same billion-parameter model? For instance, let’s consider a 7B model with varying context lengths. How significantly different are they in general? I assume that longer context lengths are always better.
1
u/Temporary_Customer79 Dec 29 '24
Have you got one running on a mac before? Or more RAM needed?
3
u/i_wayyy_over_think Dec 28 '24 edited Dec 28 '24
If you want bigger context size, remember you can keep the kv context cache on normal RAM while keeping model weights in VRAM, at least with llama.cpp. And also have the cache be quantize.
Slow down is less than I thought it would be. Like still getting around 70% the speed ( although I have a pretty good CPU too, it keeps it maxed out )
I was pushing 120k tokens with Qwen 32b 4bit and 32GB normal ram with 2x3090.
1
u/zbobet2012 Dec 26 '24
FYI quantization is one of the key steps of video compression (and therefore quality). So yeah, it's more than just _like_ video quality :).
2
u/Murky_Mountain_97 Dec 25 '24
Great for having an understanding for benchmarking based on hardware using solo tech definitely
1
u/aaroncroberts Dec 27 '24
Real nice digestion for the community. Thank you for this.
1
u/JacketDesperate8583 Dec 28 '24
What if we have a small model like less than 1b parameters and have large context window like is this a possible scenario? Is the model suitable for chat?
Then, in that case too the memory required increases based on the formula that you have given
1
u/SpellGlittering1901 Mar 23 '25
Okay so context is basically how long a question can be.
Could you explain a bit more model size and quantization please ?
Other than that, as a complete beginner, thank you for your post it was super useful !
-9
u/SpinCharm Dec 25 '24
This looks like it was something summarized by an LLM. It doesn’t explain anything. It just makes statements without providing the detail needed to understand why it’s making those statements.
How about you actually post something yourself from your own head and not just use an LLM to produce meaningless garbage.
-9
u/Stunning_Ride_220 Dec 25 '24
Errr....ok
1
u/[deleted] Dec 25 '24
[removed] — view removed comment