Remix.run Logo
Show HN: Open-source engine running Gemma 4 26B in 2 GB RAM on any M-series Mac(github.com)
97 points by gitpusher42 an hour ago | 21 comments

Hi HN,

I built a specialized inference engine for running 4-bit Gemma 4 26B-A4B-IT on any M-series Mac using about 2 GB of RAM. It is called TurboFieldfare and is written in Swift and Metal.

I have always adored on-device AI. It feels like magic that you can run a powerful NN on your Mac or iPhone. So I wanted to push the limits a bit and run a model whose weights don’t fit in memory.

The model’s 4-bit quantized weights occupy roughly 14 GB, which makes running it with conventional inference tools almost impossible on an 8 GB or even 16 GB Mac once the OS, applications, and KV cache are included.

The trick is to keep the shared part of the model and the KV cache in RAM, then stream only the routed experts needed for each token from SSD. An SSD is way slower than RAM, so the runtime uses a small expert cache and bounded parallel `pread`. While those reads are in flight, the GPU runs the shared part of the layer.

I ran more than 100 experiments. Most didn’t work. A few got me here. The experiments are described in the GitHub repo.

It currently generates 5–6 tok/s on an 8 GB M2 MacBook Air and 31–35 tok/s on an M5 MacBook Pro.

I also added an experimental OpenAI-compatible local server. It supports streaming and tool calls, and reuses one prompt prefix from the KV cache.

Try it! The Mac app is easy to install. On the first run, it will download 15 GB of weights from Hugging Face. The model is surprisingly capable.

I would love any kind of feedback!

tredre3 8 minutes ago | parent | next [-]

I'm curious how your project compares to plain mmap!

Because llama.cpp will already run 26B in 2GB of RAM if you really want to (mmap enabled, repacking disabled).

It seems like the main difference is that your project synchronizes the SSD reads with inference activity, which you've presumably tuned to cause the least latency possible? Whereas the OS wouldn't care about any of that.

addaon 35 minutes ago | parent | prev | next [-]

> It currently generates 5–6 tok/s on an 8 GB M2 MacBook Air and 31–35 tok/s on an M5 MacBook Pro.

Where does this big a performance spread come from? I wouldn't naïvely expect SSD performance difference to be that big, and I would expect SSD performance to dominate...

GeekyBear 20 minutes ago | parent | next [-]

The M5 SSD's performance uplift was fairly substantial, even when compared to the prior generation.

> In the Blackmagic Disk Speed Test, the SSD in the M5 MacBook Pro achieved read speeds of up to 6,323 MB/s, compared to just 2,031 MB/s on the M4 MacBook Pro. It's not like the M4 is "slow" in a vacuum, but the M5 SSD is over three times faster, which is a great generation uplift.

https://www.tomshardware.com/laptops/macbooks/m5-macbook-pro...

wongarsu 29 minutes ago | parent | prev | next [-]

My suspicion is that this is simply due to the M5 having more memory, and the OS already having most of the file cached. The M2 has more memory pressure and would cache fewer of the SSD reads

If that's true, inference speed would be even lower if you have only 2GB total, including OS caches

afzalive 31 minutes ago | parent | prev [-]

The M5 MBP has 24GB of RAM, more context in RAM perhaps?

gitpusher42 4 minutes ago | parent [-]

The process stays at around 2 GB with 16 slots and a 4K context on both the M5 and M2. But yeah, Apple might be doing some magic under the hood

hsienchuc 9 minutes ago | parent | prev | next [-]

I've run local video generation models on an 8GB graphics card and know firsthand that nothing runs smoothly when memory is insufficient. So seeing 14GB of weights crammed into 2GB of RAM is impressive.

If running continuously for over an hour (like an overnight batch task), will a fanless MacBook Air overheat and throttle? Can the SSD handle the continuous weight reads and sustained output speeds?

Great work, congratulations on the release!

greggh 5 minutes ago | parent | prev | next [-]

It does exactly what it says it does. On my Mac mini M4 with 16GB of ram it is running at just over 5 tok/s. That jump from M4 to M5 is crazy.

mxmlnkn 32 minutes ago | parent | prev | next [-]

This sounds really cool. My intuition was that the selected experts might change heavily for each token, resulting in slow SSD loads for each token. This seems to be wrong. Did you create some statistics on how often the experts need to be changed? What is the longest token run without any expert change? What does such a token run look like? In which cases do experts change frequently?

touwer 21 minutes ago | parent | prev | next [-]

Cool! Is there any info on this doing harm to the SSD? (Or other parts?)

wtallis 19 minutes ago | parent [-]

Reads don't wear out flash memory to any meaningful extent.

h2aichat 28 minutes ago | parent | prev | next [-]

Hope you can do it for Windows users also (and small graphics cards). Thanks

gitpusher42 17 minutes ago | parent [-]

Uh, I’m afraid it is Apple only. It is written using Apple’s GPU language, Metal, and heavily relies on the Apples’s shared memory architecture

Windows PCs would require a completely different approach

sscarduzio 17 minutes ago | parent | prev | next [-]

How does this compare to DwarfStar4?

yakupov_bulat 36 minutes ago | parent | prev | next [-]

Wow, amazing!

What if there is enough RAM to fully load the model? I assume in that case I shouldn’t use your engine.

gitpusher42 26 minutes ago | parent | next [-]

It depends on the use case.

I measured this exact model with a 4k context on the mlx engine. It runs at 75 tok/s on my M5 Mac Pro and using 14 GB of RAM. For my engine the same model uses 2 GB of RAM and produces 31–35 tok/s.

The project is still experimental so performance may vary as it continues to improve. If you want to save around 12 GB of RAM for other tasks and you are ok with 35 tok/s (afaik it is roughly comparable to ChatGPT’s speed for basic responses) my engine may be a good fit.

If you need maximum speed and flexibility just use MLX

0gs 33 minutes ago | parent | prev [-]

you could use mine ... github.com/0gsd/enough (it has other stuff too)

WithinReason 29 minutes ago | parent | prev | next [-]

Nice job implementing expert caching!

gitpusher42 19 minutes ago | parent [-]

Thank you! Under good conditions it achieves approx a 67% cache hit rate with 16 expert slots

znpy 32 minutes ago | parent | prev [-]

I wonder if i can run this on my MacBook Neo!

gitpusher42 22 minutes ago | parent [-]

I haven't tried it but it should work! You can try it and share your results, it would be really appreciated

I tried it on my wife's M1 MacBook Air 512GB and it gets 4–5 tok/s

Also, it must be easy to adjust for iPhones and iPads in theory