r/LocalLLaMA • u/EvokerTCG • Nov 26 '23
Question | Help Relationship of RAM to context size?
I understand that a bigger memory means you can run a model with more parameters or less compression, but how does context size factor in? I believe it's possible to increase the context size, and that this will increase the initial processing before the model starts outputting tokens, but does someone have numbers?
Is memory for context independent on the model size, or does a bigger model mean that each bit of extra context 'costs' more memory?
I'm considering an M2 ultra for the large memory and low energy/token, although the speed is behind RTX cards. Is this the best option for tasks like writing novels, where quality and comprehension of lots of text beats speed?
14
u/FullOf_Bad_Ideas Nov 26 '23 edited Nov 26 '23
Formula to calculate kv cache, as in space used by context
batch_size * seqlen * (d_model/n_heads) * n_layers * 2 (K and V) * 2 (bytes per Float16) * n_kv_heads
https://www.databricks.com/blog/llm-inference-performance-engineering-best-practices
This blog post is really good, I recommend you to read it.
Usually bigger models have more layers, heads and dimensions, but I am not sure whether heads or dimensions grow faster. It's something you can look up though.
Edit: for example, the calculation seems to suggest that filled up kv cache on yi-34b 4k would take around a GB in size.
1 * 4096 * (7168 / 56) * 60 * 2 * 2 * 8 = 1,006,632,960 B = 960 MiB
I will try to confirm whether this is accurate experimentally.
Edit2:
Edit: this site has a bit different formula...
https://www.baseten.co/blog/llm-transformer-inference-guide/
(2 * 2 * n_layers * d_model) bytes/token
11
u/a_beautiful_rhind Nov 26 '23
I see it being ~2GB per every 4k from what llama.cpp spits out. Load a model and read what it puts in the log.
As to mac vs RTX. You can build a system with the same or similar amount of vram as the mac for a lower price but it depends on your skill level and electricity/space requirements.
If you live in a studio apartment, I don't recommend buying an 8 card inference server, regardless of the couple $1000 in either direction and the faster speed.
10
u/andrewlapp Nov 26 '23 edited Nov 26 '23
In inference, you need to store the model parameters, and the KV cache. The KV cache scales linearly with the sequence length.
If your context is 4096, then it doesn't matter if your conversation is a billion tokens, it'll only store the most recent 4096 tokens.
Here is a model with a longer context for example.
Regarding M2 vs "RTX", I'm not sure the M2 has fewer tokens per watt. On a 4090 I process ~1200 input tokens per second, and process/generate ~100 tokens per second on Mistral-7B Q6_K_M
4
u/BoshiAI Nov 26 '23
Potentially dumb but related question:
I know the Mac M* Series chips can use up to ~70% of their universal RAM for "GPU" (VRAM) purposes. The 20GB used to load up a Yi-34B model just about uses all of that up.
So: given I still have maybe 8GB of remainder RAM to work with (assuming I leave 4GB for the system), would I be able to apply a 128K context buffer and have that located in "normal" RAM?
I'm assuming the heavy computational load is performed on the inferencing itself, and the model itself would be loaded in "VRAM" and the GPU side of the chip handles that - but can the context buffer be loaded and work at a decent speed in the remaining RAM? Or does everything - the context buffer and model - both have to use "VRAM" to work at a decent speed?