by Merlin Warner-Huish

Large language models (LLMs) are typically associated with vast racks of GPUs. However, increasingly engineers are running AI inference on ordinary CPUs: laptops, servers without accelerators, and especially edge devices. One of the most widely used tools for this is LLaMA.cpp, an open source engine that runs LLMs on (almost) any hardware with a C compiler.
RISC-V hardware has matured to the point where language model inference is now a realistic workload. In this series we will bring LLaMA.cpp up on a 64-core RISC-V machine, starting from the first build attempts through to an understanding of its performance and limitations. Along the way we will show anything that goes wrong as well as what works, as on a new platform, often the problems are the most useful pieces of information.
This first post answers three questions:
There are two stages in the life of a language model: training and inference. Training is where the model learns; it is shown vast quantities of text, and its weights (typically billions of numbers) are gradually adjusted until it becomes good at predicting the next token. Training is only done once, but it is extremely expensive and typically requires large clusters of GPUs. Inference is everything that happens afterwards; each time the model is used, the trained weights are loaded and applied to a new input (such as a prompt) to produce an output. As inference happens far more often than training and requires much less computing power, it can run on ordinary CPUs.

A trained model consists of two parts: its weights, and an architecture that explains how those weights are combined to predict the next token. Alone, a model cannot achieve anything. Software is required in order to load the weights and perform calculations; that software is the inference engine. LLaMA.cpp is an inference engine; instead of training models, it runs them.
Georgi Gerganov started LLaMA.cpp in March 2023, just after Meta’s LLaMA model weights were released. Georgi’s aim was to run LLaMA on a laptop CPU using plain C/C++. The project gained traction and momentum quickly. Today, LLaMA.cpp is maintained by a large open-source community, supports dozens of model families and is utilized both directly and as the engine inside numerous local AI tools.
LLaMA.cpp has three layers:

One key feature of LLaMA.cpp aids immensely in its practicality; quantization. Models are typically trained with weights stored as either 16 or 32-bit floating-point numbers. Quantization allows LLaMA.cpp to store weights in 8 or 4 bits: in small blocks that share a scale factor. This makes the model several times smaller and (as a running model spends much of its time reading weights from memory) often faster as well. There are numerous quantization formats: from simple ones like Q8_0, to more complex “K-quants” such as Q4_K. Each format requires its own optimized code, therefore, your chosen format can influence your results significantly, especially on newer hardware.
The choice of C/C++ is what makes LLaMA.cpp an excellent candidate for a new platform. The most common way to run a model is through Python using a framework such as PyTorch. The heavy lifting in these frameworks is not undertaken by Python itself, but by highly optimized C/C++ libraries beneath. The key issue is that on new architectures those optimized libraries often do not yet exist, or are far less mature, therefore the framework falls back on more generic (and slower) code. Getting all of that working on a new architecture can be a massively time consuming task. Conversely, LLaMA.cpp does one job and has few dependencies; a C/C++ compiler along with CMake is enough to build it. Its speed comes from hand-written kernels for each CPU architecture, utilizing SIMD and vector instructions (such as AVX on x86, NEON on Arm, and the vector extension on RISC-V). Therefore, a basic port is relatively easy, however, its running speed is dependent on the quality of the kernels for that particular architecture. That gap is where the bring-up work occurs.
Lastly, one quick clarification regarding the name (which often causes confusion): LLaMA is a family of models from Meta, LLaMA.cpp is the engine which is named after the first model it ran. Despite the name, LLaMA.cpp can run many other model families; examples include Qwen, Mistral, and Gemma.
RISC-V processors now range from small microcontrollers to many-core server chips, but RISC-V has only relatively recently moved into the server space. The Milk-V Pioneer was one of the first generally available machines with a server-class RISC-V processor, so much of the software that is mature on x86 and Arm servers still needs work on RISC-V. LLaMA.cpp is a good example: on x86 and Arm it has years of tuned kernels and testing behind it, while its RISC-V support is newer and yet not as complete.
This series will be written so that it is useful beyond one board. Most of what we will cover (such as toolchain issues, choosing a model format, and how memory layout affects performance) will be transferable to other RISC-V hardware, and much of it to any machine with many cores. Throughout, we will make clear which parts are specific to our platform and which are general.
RISC-V is an open instruction set architecture. Anyone can design and build a RISC-V processor without paying for a license. The architecture is made up of a small base along with optional extensions (and vendors are welcome to add custom extensions of their own).
That openness is a key reason why RISC-V is interesting for AI: companies can tailor a design to their individual workloads. That said, an instruction set is only as useful as the software that runs on it. As RISC-V hardware develops, widely used software such as LLaMA.cpp also needs to follow.
RISC-V vectors add a further layer of complication. The standard vector extension (RVV 1.0) was ratified in 2021, but some processors designed before then implement an earlier draft (which T-Head calls XTheadVector). The two are not compatible at the instruction level, and software, compilers, and build systems typically assume one or the other. Defaults written with the standard extension in mind may not build (or run) on hardware which implements the draft.
Our platform is a Milk-V Pioneer, a desktop computer built around Sophgo’s SG2042 processor. The SG2042 has 64 RISC-V cores, T-Head’s C920, and the Pioneer runs a standard Linux distribution (in this case Fedora). Overall, it is one of the better-known RISC-V machines with a high core count.
Two key features of the SG2042 will influence this series:
These quirks make the SG2042 a good test case. It has enough cores to show how LLaMA.cpp scales, as well as exposing issues that a smoother platform may hide. Many of these problems are ones that anybody bringing up software on newer RISC-V hardware is likely to find.

We will treat this as a bring-up, building understanding one step at a time:
LLaMA.cpp changes quickly, so reproducibility is super important. For every result we will state the LLaMA.cpp commit, the compiler versions, build options and the benchmark method, and we aim to publish our scripts and logs so that readers can repeat what we do.
Over the course of the series we will cover:
In the next post we will look at the platform in more detail: what the SG2042 is, what makes its vector unit different from the RISC-V standard, and what that means for anyone building software for it. Further parts of the series will follow over the coming weeks.
Thanks for reading and we look forward to your company in the next post!