The instructions in this Learning Path are for any Arm server running Ubuntu 22.04 LTS. You need an Arm server instance with at least four cores and 8GB of RAM to run this example. Configure disk storage up to at least 32 GB. The instructions have been tested on an AWS Graviton3 c7g.16xlarge instance.
Arm CPUs are widely used in traditional ML and AI use cases. In this Learning Path, you learn how to run generative AI inference-based use cases like a LLM chatbot on Arm-based CPUs. You do this by deploying the
Llama-3.1-8B model
on your Arm-based CPU using llama.cpp.
llama.cpp is an open source C/C++ project developed by Georgi Gerganov that enables efficient LLM inference on a variety of hardware - both locally, and in the cloud.
The Llama-3.1-8B model from Meta belongs to the Llama 3.1 model family and is free to use for research and commercial purposes. Before you use the model, visit the Llama website and fill in the form to request access.
The Meta Llama 3.1 collection of models perform general natural language processing (NLP) tasks such as text generation. The Llama 3.1 family of models range in size from 8 billion to 405 billion parameters. The greater the number of parameters, the more information the model can store. This directly affects how well the model understands language and the model’s general capabilities. LLMs that run efficiently on CPUs typically have lower numbers of parameters. For this example, the 8 billion (8B) model is ideal for retaining quality chatbot capability while also running efficiently on your Arm-based CPU.
Traditionally, the training and inference of LLMs has been done on GPUs using full-precision 32-bit (FP32) or half-precision 16-bit (FP16) data type formats for the model parameter and weights. Recently, a new binary model format called GGUF was introduced by the llama.cpp team. This new GGUF model format uses compression and quantization techniques that remove the dependency on using FP32 and FP16 data type formats. For example, GGUF supports quantization where model weights that are generally stored as FP16 data types are scaled down to 4-bit integers. This significantly reduces the need for computational resources and the amount of RAM required. These advancements made in the model format and the data types used make Arm CPUs a great fit for running LLM inferences.
Install the following packages on your Arm based server instance:
You also need to install gcc on your machine:
You are now ready to start building llama.cpp.
Clone the source repository for llama.cpp:
By default, llama.cpp builds for CPU only on Linux and Windows. You don’t need to provide any extra switches to build it for the Arm CPU that you run it on.
Run make to build it:
Check that llama.cpp has built correctly by running the help command:
If llama.cpp has built correctly on your machine, you will see the help options being displayed. A snippet of the output is shown below:
There are a few different ways you can download the Meta Llama-3.1 8B model. In this Learning Path, you download the model from Hugging Face.
Use of Llama 3.1 8B model is governed by the Meta license. Before you proceed to download the model, please visit the Llama website and fill in the form.
Hugging Face is an open source AI community where you can host your own AI models, train them and collaborate with others in the community. You can browse through the thousands of models that are available for a variety of use cases like NLP, audio, and computer vision.
The huggingface_hub library provides APIs and tools that let you easily download and fine-tune pre-trained models. You will use huggingface-cli to download the
Llama-3.1 8B model
.
Install the required Python packages:
Create and activate a Python virtual environment:
Your terminal prompt now has the (venv) prefix indicating the virtual environment is active. Use this virtual environment for the remaining commands.
Install the huggingface_hub python library using pip:
You can now download the model using the huggingface cli:
Before you proceed and run this model, take a quick look at what Q4_0 in the model name denotes.
Q4_0 in the model name refers to the quantization method the model uses. The goal of quantization is to reduce the size of the model (to reduce the memory space required) and faster (to reduce memory bandwidth bottlenecks transferring large amounts of data from memory to a processor). The primary trade-off to keep in mind when reducing a model’s size is maintaining quality of performance. Ideally, a model is quantized to meet size and speed requirements while not having a negative impact on performance.
This model is llama3.1-8b-Q4_0.gguf, so what does each component mean in relation to the quantization level? The main thing to note is the number of bits per parameter, which is denoted by ‘Q4’ in this case or 4-bit integer. As a result, by only using 4 bits per parameter for 8 billion parameters, the model drops to be 4.7Gb in size.
Here is a quick lookup to the rest of the quantization parts for the Llama-2 model family as it exists today:
| quantization-method | # of bits per parameter | quantization format (does not apply to quantization method ‘IQ’) | quantization method specifics |
|---|---|---|---|
| Q, IQ, F, FP | 2,3,4,5,6,7,8,16,32 | _0, _1, _K | _XXS, _XS, _S, _M, _L |
Some examples:
Each quantization method has a unique approach to quantizing parameters. The deeper technical details of different quantization methodologies are outside the scope of this guide. The main takeaway is that selecting the right model quantization is critical to running an LLM effectively on your hardware, and the most impactful quantization decision is the number of bits per parameter. You will need also need to check you have enough system memory before deploying larger models or models with higher precision/quantization.
In this guide, you will not use any other quantization methods, because Arm has not made kernel optimizations for other quantization types.
To see improvements for Arm optimized kernels, you need to generate a new weights file with rearranged Q4_0 weights. As of llama.cpp commit 0f1a39f3 , Arm has contributed code for three types of GEMV/GEMM kernels corresponding to three processor types:
To re-quantize optimally for Graviton3, run
This will output a new file, dolphin-2.9.4-llama3.1-8b-Q4_0_8_8.gguf, which contains reconfigured weights that allow llama-cli to use SVE 256 and MATMUL_INT8 support.
This requantization is optimal only for Graviton3. For Graviton2, requantization should optimally be done in Q4_0_4_4 format, and for Graviton4, Q4_0_4_8 is the optimal requantization format.
First, run the pre-quantized llama-3.1-8b model exactly as the weights were downloaded from huggingface:
This command will use the downloaded model (-m flag), with the specified prompt (-p flag), and target a 512 token completion (-n flag), using 64 threads (-t flag).
You will see lots of interesting statistics being printed from llama.cpp about the model and the system, followed by the prompt and completion. The tail of the output from running this model on an AWS Graviton3 c7g.16xlarge instance is shown below:
The system_info printed from llama.cpp highlights important architectural features present on your hardware that improve the performance of the model execution. In the output shown above from running on an AWS Graviton3 instance, you will see:
The end of the output shows several model timings:
You can compare these timings to the optimized model weights by running:
This is the same command as before, but with the model file swapped out for the re-quantized file.
The timings on this one look like:
As you can see, load time improves, but the biggest improvement can be seen in prompt eval times.
You have successfully run a LLM chatbot with Arm optimizations, all running on your Arm AArch64 CPU on your server. You can continue experimenting and trying out the model with different prompts.