Skip to content

Commit 7803e45

Browse files
Merge pull request #3471 from tonymindbeamai/learning-path/litespark-inference
Add Learning Path: Accelerate LLM inference on Arm CPUs with Litespark-Inference
2 parents e840ab4 + ebc1502 commit 7803e45

12 files changed

Lines changed: 608 additions & 3 deletions

File tree

assets/contributors.csv

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ author,company,github,linkedin,twitter,website
22
Jason Andrews,Arm,jasonrandrews,jason-andrews-7b05a8,,
33
Doug Anson,Arm,DougAnsonAustinTx,douganson,,
44
Pareena Verma,Arm,pareenaverma,pareena-verma-7853607,,
5-
Jaidev Singh Chadha,Arm,jaidev17,jaidevsinghchadha,,
5+
Jaidev Singh Chadha,Arm,jaidev17,jaidevsinghchadha,,
66
Ronan Synnott,Arm,,ronansynnott,,
77
Florent Lebeau,Arm,,,,
88
Brenda Strech,Remote.It,bstrech,bstrech,@remote_it,www.remote.it
@@ -89,7 +89,7 @@ Geremy Cohen,Arm,geremyCohen,geremyinanutshell,,
8989
Barbara Corriero,Arm,,,,
9090
Nina Drozd,Arm,NinaARM,ninadrozd,,
9191
Jun He,Arm,JunHe77,jun-he-91969822,,
92-
Henry Wang,Arm,MrXinWang,xin-wang-930b4b141,,
92+
Henry Wang,Arm,MrXinWang,xin-wang-930b4b141,,
9393
Gian Marco Iodice,Arm,,,,
9494
Aude Vuilliomenet,Arm,,,,
9595
Andrew Kilroy,Arm,,,,
@@ -131,4 +131,7 @@ Anupras Mohapatra,Arm,,,,
131131
Tomas Agustin Gonzalez Orlando,Arm,tgonzalezorlandoarm,tgorlando,,
132132
Jai Adam Schrem,Arm,jai-adam-schrem,jai-adam-s-395bb1233,,
133133
Steve Jordahl,Rafay,,,,
134-
Dave Neary,Ampere,,,,
134+
Nii Osae Osae Dade,Mindbeam,StarkOsae,,,
135+
Tony Morri,Mindbeam,tonymindbeamai,,,
136+
Sayandip Pal,Mindbeam,sayandip-pal,,,
137+
Dave Neary,Ampere,,,,
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
additional_search_terms:
3+
- BitNet
4+
- ternary
5+
- LLM
6+
- inference
7+
- CPU
8+
- Arm
9+
- Graviton
10+
- Apple silicon
11+
- Python
12+
13+
layout: installtoolsall
14+
minutes_to_complete: 10
15+
author:
16+
- Nii Osae Osae Dade
17+
- Tony Morri
18+
- Sayandip Pal
19+
multi_install: false
20+
multitool_install_part: false
21+
official_docs: https://github.com/Mindbeam-AI/Litespark-Inference
22+
test_images:
23+
- ubuntu:latest
24+
test_link: null
25+
test_maintenance: true
26+
title: Litespark-Inference
27+
description: Install Litespark-Inference on Arm or x86 Linux and Apple silicon macOS to run BitNet ternary LLMs on the CPU.
28+
tool_install: true
29+
weight: 1
30+
---
31+
32+
[Litespark-Inference](https://github.com/Mindbeam-AI/Litespark-Inference)
33+
is an open-source CPU inference runtime for
34+
[BitNet b1.58](https://arxiv.org/abs/2402.17764) ternary-weight LLMs. A
35+
single `pip install` reads your CPU's feature flags and compiles the
36+
right C++ kernel for it - NEON + SDOT on Arm, AVX-512/VNNI or AVX2+FMA
37+
on x86. You do not pick the kernel; it picks itself.
38+
39+
## What do I need before installing Litespark-Inference?
40+
41+
- **Python 3.10 or newer.** Check with `python3 --version`.
42+
- **A C++ toolchain** (`clang` or `g++`). Already present on most
43+
developer machines; install steps per platform are below.
44+
- **About 5 GB of free disk.** The BitNet-2B model downloads from
45+
Hugging Face on first run.
46+
47+
It is good practice to install into a clean virtual environment so the
48+
install does not conflict with anything else on your machine:
49+
50+
```console
51+
python3 -m venv .venv
52+
source .venv/bin/activate
53+
python -m pip install --upgrade pip wheel setuptools
54+
```
55+
56+
Then follow the section for your platform.
57+
58+
## How do I install Litespark-Inference on Arm or x86 Linux?
59+
60+
The released package builds the correct kernel for your CPU
61+
automatically - AVX-512 with an AVX2+FMA fallback on x86, and NEON +
62+
SDOT on Arm (Graviton 2/3/4, Ampere, Neoverse N1/N2/V1/V2, Raspberry
63+
Pi 5). Install the C++ toolchain, then the package:
64+
65+
```bash
66+
sudo apt-get update
67+
sudo apt-get install -y build-essential clang ninja-build git python3-pip
68+
pip install litespark-inference
69+
```
70+
71+
For Red Hat, Fedora, or RHEL, install the toolchain with `dnf` instead:
72+
73+
```console
74+
sudo dnf install -y gcc-c++ clang ninja-build git python3-pip
75+
```
76+
77+
Confirm the package imports and reports the kernel selected for your CPU:
78+
79+
```bash
80+
python3 -c "import litespark_inference; print(litespark_inference.__version__)"
81+
```
82+
83+
To inspect which kernel was built, run:
84+
85+
```console
86+
python -m litespark_inference.torchless info
87+
```
88+
89+
The output ends with one of the following, depending on your CPU:
90+
91+
```output
92+
kernel : avx512 (torchless, extern "C" AVX-512/VNNI + OMP)
93+
kernel : avx2 (torchless, extern "C" AVX2+FMA fallback + OMP)
94+
kernel : neon (torchless, extern "C" NEON SDOT)
95+
```
96+
97+
## How do I install Litespark-Inference on Apple silicon macOS?
98+
99+
Apple's CPUs have NEON SDOT and Litespark-Inference uses it directly.
100+
The only extra step versus Linux is installing `libomp` - Apple's
101+
toolchain does not ship a built-in OpenMP runtime, and Litespark uses
102+
OpenMP for multi-threading inside the kernel:
103+
104+
```console
105+
# Xcode command-line tools (one-time setup; no full Xcode required)
106+
xcode-select --install
107+
108+
# libomp via Homebrew
109+
brew install libomp
110+
111+
# Install the released package from PyPI
112+
pip install litespark-inference
113+
```
114+
115+
Verify the install:
116+
117+
```console
118+
python -m litespark_inference.torchless info
119+
```
120+
121+
Expected output includes:
122+
123+
```output
124+
kernel : neon (torchless, extern "C" NEON SDOT)
125+
OpenMP : True (max_threads=...)
126+
```
127+
128+
If `OpenMP : False`, the build did not find Homebrew's `libomp`. The
129+
most common cause is that Homebrew is installed under `/opt/homebrew`
130+
(the Apple silicon default) but `pip install` ran in an environment that
131+
hides it. Re-running `pip install litespark-inference` from a normal
132+
shell usually fixes it.
133+
134+
## How do I install from source?
135+
136+
To modify the runtime or kernels, install from source instead of from
137+
PyPI:
138+
139+
```console
140+
git clone https://github.com/Mindbeam-AI/Litespark-Inference.git
141+
cd Litespark-Inference
142+
pip install -e .
143+
```
144+
145+
## Sanity-check
146+
147+
On all platforms, the same one-liner generates text. The first run
148+
downloads `microsoft/bitnet-b1.58-2B-4T-bf16` from Hugging Face (around
149+
4.5 GB into `~/.cache/huggingface/hub`); later runs start instantly:
150+
151+
```console
152+
litespark-inference generate "Hello, world!" --max-tokens 16
153+
```
154+
155+
You are now ready to run BitNet-2B. Continue with the
156+
[Accelerate LLM inference on Arm CPUs with Litespark-Inference](/learning-paths/laptops-and-desktops/litespark-inference/)
157+
Learning Path.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: What is Litespark-Inference?
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## The short version
10+
11+
Litespark-Inference is an open-source runtime that runs
12+
ternary-weight language models - such as
13+
[BitNet b1.58](https://arxiv.org/abs/2402.17764) - on the CPU you
14+
already have.
15+
No GPU. No CUDA wheel. No PyTorch installation. The dependency
16+
footprint after `pip install` is `numpy`, `safetensors`, and
17+
`tokenizers`.
18+
19+
It runs on:
20+
21+
| Platform | Kernel selected automatically |
22+
|---|---|
23+
| **Linux on x86_64** with AVX-512 (Intel Ice Lake+ / AMD Zen 4+) | AVX-512 + VNNI |
24+
| **Linux on x86_64**, Intel Core Ultra (no AVX-512) | AVX-VNNI (256-bit) |
25+
| **Linux on x86_64** without VNNI (AMD Zen 2-3, pre-Skylake-X Intel) | AVX2 + FMA fallback |
26+
| **Linux on Arm (Graviton 2/3/4, Ampere, etc.)** | NEON + SDOT |
27+
| **macOS on Apple silicon (M1/M2/M3/M4/M5)** | NEON + SDOT |
28+
29+
A single `pip install` reads your CPU's feature flags and compiles the
30+
right C++ source for you. You don't pick the kernel; it picks itself.
31+
The [Litespark-Inference install guide](/install-guides/litespark-inference/)
32+
covers the install step for each platform.
33+
34+
## Why BitNet on the CPU
35+
36+
BitNet b1.58 stores each weight as a value in `{-1, 0, +1}` and packs
37+
four weights into one byte. That means:
38+
39+
- The model file is around 6x smaller than the equivalent `bfloat16`
40+
model (around 497 MB packed versus around 4,600 MB unpacked).
41+
- Every matmul reduces to `int8` activation x ternary weight - exactly
42+
what a CPU's SIMD dot-product instructions (SDOT on Arm, VNNI on
43+
x86) are designed for.
44+
45+
The net effect: a 2-billion-parameter model that fits in under 1 GB of
46+
RAM and generates tokens at interactive speed on a normal laptop or
47+
cloud CPU instance.
48+
49+
The charts below show Litespark-Inference against a PyTorch baseline
50+
across several Arm and x86 CPUs. Token-generation throughput is roughly
51+
an order of magnitude higher, and resident memory is around 6x smaller,
52+
on every platform tested.
53+
54+
![Token-generation throughput, Litespark-Inference versus PyTorch, on Apple M5 Max, AMD Zen 4, and Intel Core Ultra 9#center](throughput-comparison.png "Cross-platform throughput comparison")
55+
56+
![Resident memory, Litespark-Inference versus PyTorch, on Apple M5 Max, AMD Zen 4, and Intel Core Ultra 9#center](memory-comparison.png "Cross-platform memory usage comparison")
57+
58+
## What you'll do in this Learning Path
59+
60+
1. **Run BitNet-2B from the CLI in one line:**
61+
`litespark-inference generate "Why is BitNet fast on CPU?" --max-tokens 64`.
62+
63+
2. **Run the same model from a short Python script** using the
64+
high-level `BitNet.from_pretrained(...).generate(...)` API.
65+
66+
3. **Tune the embedding dtype** (`bf16` / `int8` / `int4`) to trade
67+
memory for quality.
68+
69+
4. **(Optional)** Run a head-to-head benchmark against `transformers`
70+
with `torch.bfloat16` on the same machine, with memory, TTFT,
71+
throughput, and energy-per-token numbers.
72+
73+
The next chapter runs BitNet-2B from the CLI and from Python.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Run BitNet-2B
3+
weight: 3
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
With Litespark-Inference installed (see the
10+
[install guide](/install-guides/litespark-inference/)), you can generate
11+
text right away.
12+
13+
## The CLI (one line)
14+
15+
```bash
16+
litespark-inference generate "Why is BitNet fast on CPU?" --max-tokens 64
17+
```
18+
19+
What this does:
20+
21+
1. Loads `microsoft/bitnet-b1.58-2B-4T-bf16` from the Hugging Face cache
22+
(downloaded on first run, around 4.5 GB).
23+
2. Tokenizes the prompt and runs it through the model in one batched
24+
**prefill** call.
25+
3. Greedy-decodes 64 new tokens, streaming each one to your terminal
26+
as it is produced.
27+
28+
Other useful CLI commands:
29+
30+
```bash
31+
# Quick health-check - what kernel, what threads, what model
32+
litespark-inference info
33+
34+
# Interactive chat (multi-turn, reuses KV cache across turns)
35+
litespark-inference chat
36+
```
37+
38+
## From Python (a few lines)
39+
40+
If you want to embed Litespark-Inference in an application, the
41+
recommended API is the high-level `BitNet` class - load the model, then
42+
call `generate`:
43+
44+
```python
45+
from litespark_inference.torchless import BitNet
46+
47+
# Auto-downloads from Hugging Face on first use.
48+
bn = BitNet.from_pretrained("bitnet-2b")
49+
50+
# chat=True applies the system + chat template and returns a clean
51+
# instruction-following answer (omit it for a raw continuation).
52+
print(bn.generate("Why is BitNet fast on CPU?", max_new_tokens=64, chat=True))
53+
```
54+
55+
That is the whole surface: `from_pretrained` loads the packed model and
56+
owns the KV cache; `generate` runs the batched prefill plus
57+
autoregressive decoding for you. There is no `torch` import at inference.
58+
59+
## Embed dtype: pick your memory / quality trade-off
60+
61+
The weight matrices are always 2-bit packed (that is what BitNet b1.58
62+
means). The one thing you can vary is the token-embedding table's
63+
dtype - and that controls a big chunk of the model's resident memory:
64+
65+
| Embed dtype | Resident memory | Quality | When to use |
66+
|---|---|---|---|
67+
| `bf16` | around 813 MB | Reference | Default for accuracy-sensitive work |
68+
| `int8` | around 656 MB | Indistinguishable from `bf16` in practice | Good balance |
69+
| **`int4`** | **around 573 MB** | Slight quality cost on rare tokens | **Recommended** - fastest load, smallest footprint |
70+
71+
CLI:
72+
73+
```bash
74+
litespark-inference generate "Why is BitNet fast on CPU?" --embed-dtype int4 --max-tokens 64
75+
```
76+
77+
Python:
78+
79+
```python
80+
from litespark_inference.torchless import BitNet
81+
82+
bn = BitNet.from_pretrained("bitnet-2b", embed_dtype="int4")
83+
print(bn.generate("Why is BitNet fast on CPU?", max_new_tokens=64, chat=True))
84+
```
85+
86+
## What's next
87+
88+
If you just wanted to run BitNet on the CPU, you are done - go build
89+
something with it.
90+
91+
If you want to **know how fast it actually is** versus PyTorch on the
92+
same machine (and how much energy it uses), continue to the next chapter
93+
on benchmarking.

0 commit comments

Comments
 (0)