ggml/examples/simple
Jeff Bolz e02fb860cc
Rewrite simple-backend to use sched and ggml_backend_load_all (#1376)
* Rewrite simple-backend to use sched and ggml_backend_load_all

* address slaren's feedback

* move the storage to the model class
2025-10-29 18:10:19 +01:00
..
CMakeLists.txt examples : more CUDA leftovers (#0) 2024-03-27 13:20:00 +02:00
README.md examples : fix simple (#770) 2024-03-22 09:17:34 +02:00
simple-backend.cpp Rewrite simple-backend to use sched and ggml_backend_load_all (#1376) 2025-10-29 18:10:19 +01:00
simple-ctx.cpp update tests and examples 2024-11-04 19:42:09 +02:00

Simple

This example simply performs a matrix multiplication, solely for the purpose of demonstrating a basic usage of ggml and backend handling. The code is commented to help understand what each part does.

Traditional matrix multiplication goes like this (multiply row-by-column):


A \times B = C

\begin{bmatrix}
2 & 8 \\
5 & 1 \\
4 & 2 \\
8 & 6 \\
\end{bmatrix}
\times
\begin{bmatrix}
10 & 9 & 5 \\
5 & 9 & 4 \\
\end{bmatrix}
\=
\begin{bmatrix}
60 & 90 & 42 \\
55 & 54 & 29 \\
50 &  54 & 28 \\
110 & 126 & 64 \\
\end{bmatrix}

In ggml, we pass the matrix B in transposed form and multiply row-by-row. The result C is also transposed:


ggml\\_mul\\_mat(A, B^T) = C^T

ggml\\_mul\\_mat(
\begin{bmatrix}
2 & 8 \\
5 & 1 \\
4 & 2 \\
8 & 6 \\
\end{bmatrix}
,
\begin{bmatrix}
10 & 5 \\
9 & 9 \\
5 & 4 \\
\end{bmatrix}
)
\=
\begin{bmatrix}
60 & 55 & 50 & 110 \\
90 & 54 & 54 & 126 \\
42 & 29 & 28 & 64 \\
\end{bmatrix}

The simple-ctx doesn't support gpu acceleration. simple-backend demonstrates how to use other backends like CUDA and Metal.