I have written gemma3 inference in pure C

(github.com)

64 points | by robitec97 4 days ago ago

24 comments

  • austinvhuang 2 days ago

    My first implementation of gemma.cpp was kind of like this.

    There's such a massive performance differential vs. SIMD though that I learned to appreciate SIMD (via highway) as one sweet spot of low-dependency portability that sits between C loops and the messy world of GPUs + their fat tree of dependencies.

    If anyone want to learn the basics - whip out your favorite LLM pair programmer and ask it to help you study the kernels in the ops/ library of gemma.cpp:

    https://github.com/google/gemma.cpp/tree/main/ops

    • janwas 2 days ago

      :D Your code was nicely written and it was a pleasure to port to SIMD because it was already very data-parallel.

  • rao-v 2 days ago

    I'm really charmed by this project (I know there are a few like it).

    In particular it's got a single ~600 line file (https://github.com/robitec97/gemma3.c/blob/main/gemma3_kerne...) with a clear straightforward implementation of every major function used in inferencing (google's models) from gelu to rope.

    I'm curious how many more functions you'd need to add to have full coverage of every publically available LLM innovation (e.g. QK-Norm from Qwen3, SwiGLU etc.).

    Obviously llama.cpp has a much bigger library but it's lovely to see everything in one clean file.

  • w4yai 2 days ago

    > It proves that modern LLMs can run without Python, PyTorch, or GPUs.

    Did we need any proof of that ?

    • jdefr89 2 days ago

      Python and PyTorch all call out to C libraries… I don’t get what he means by “proving LLMs can run without Python and PyTorch” at all. Seems like they don’t understand basic fundamentals about things here…

    • jasonjmcghee 2 days ago

      I guess llama.cpp isn't quite as popular as I had assumed.

      • avadodin a day ago

        llama.cpp being the best choice doesn't make it popular.

        When I got started, I was led to ollama and other local-llm freemium.

        I didn't necessarily assume that they weren't c++(I don't even know) but I do think that –as implied– Python duct-tape solutions are more popular than llama.cpp.

    • christianqchung 2 days ago

      A bizarre claim like that would be what happens when you let an LLM write the README without reading it first.

    • skybrian 2 days ago

      Knowing the performance is interesting. Apparently it's 1-3 tokens/second.

    • tolerance 2 days ago

      I imagine so regarding GPUs, right? Is this is a legitimate project then doesn’t it provide a proof of concept for performance constraints that relate to them? Couldn't the environmentally concerned take this as an indicator that the technology can progress without relying on as much energy is potentially spent now? Shouldn’t researchers in the industry be thinking of ways to prevent the future capabilities of the technology from outrunning the capacity of the infrastructure?

      I know very little about AI but these are things that come to mind here for me.

      • yorwba 2 days ago

        GPUs are more efficient than CPUs for LLM inference, using less energy per token and being cheaper overall. Yes, a single data center GPU draws a lot of power and costs a fortune, but it can also serve a lot more people in the time your CPU or consumer GPU needs to respond to a single prompt.

  • pacman1337 a day ago

    Anyone using this model for something useful? For now I only have use cases for top performing models...

  • 4 days ago
    [deleted]
  • behnamoh 2 days ago

    but why tho? next gemma is coming and no one uses gemma 3 in prod anyway.

    • uncognic 2 days ago

      I think /* */ single-line comments is a pretty good indication.

    • NitpickLawyer 2 days ago

      > no one uses gemma 3 in prod anyway.

      Umm, we do. It's still one of the best for eu countries support / help chatbot style. It's got good (best?) multilingual support ootb, it's very "safe" (won't swear, won't display chinese characters, etc) and it's pretty fast.

      • gunalx 2 days ago

        Yep. Before gemma3 we where struggling with multilinguality on smaller European languages, and it is still one of the batter ones in that regard (even large open or closed models struggle with this to some extent). Gemma3 also is still pretty decent multi modal wise.

        • avadodin a day ago

          I didn't know this was a thing until I read this thread but I can confirm that it does fine(not perfect by any means just like the average casual non-native fluent speaker) and it is one of the reasons I use it as my local model.

      • behnamoh 2 days ago

        but it lacks system prompt support.

        • NitpickLawyer a day ago

          It lacks a deducated system prompt, but it was trained with and in practice works with the system prompt be the first message from the user.

    • austinvhuang a day ago

      I don't have firsthand knowledge, but r/SesameAI seems to believe Maya/Miles products are based on a Gemma3 backbone.

    • data-ottawa 2 days ago

      Gemma3 is probably the best supported fine tunable model.