interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / examples / mnist / README.md

206 lines · 9633 bytes Raw Blame History Rendered
1# MNIST Examples for GGML
2
3This directory contains simple examples of how to use GGML for training and inference using the [MNIST dataset](https://yann.lecun.com/exdb/mnist/).
4All commands listed in this README assume the working directory to be `examples/mnist`.
5Please note that training in GGML is a work-in-progress and not production ready.
6
7## Obtaining the data
8
9A description of the dataset can be found on [Yann LeCun's website](https://yann.lecun.com/exdb/mnist/).
10While it is also in principle possible to download the dataset from this website these downloads are frequently throttled and
11it is recommended to use [HuggingFace](https://huggingface.co/datasets/ylecun/mnist) instead.
12The dataset will be downloaded automatically when running `mnist-train-fc.py`.
13
14## Fully connected network
15
16For our first example we will train a fully connected network.
17To train a fully connected model in PyTorch and save it as a GGUF file, run:
18
19```bash
20$ python3 mnist-train-fc.py mnist-fc-f32.gguf
21
22...
23
24Test loss: 0.066377+-0.010468, Test accuracy: 97.94+-0.14%
25
26Model tensors saved to mnist-fc-f32.gguf:
27fc1.weight (500, 784)
28fc1.bias (500,)
29fc2.weight (10, 500)
30fc2.bias (10,)
31```
32
33The training script includes an evaluation of the model on the test set.
34To evaluate the model on the CPU using GGML, run:
35
36```bash
37$ ../../build/bin/mnist-eval mnist-fc-f32.gguf data/MNIST/raw/t10k-images-idx3-ubyte data/MNIST/raw/t10k-labels-idx1-ubyte
38
39________________________________________________________
40________________________________________________________
41________________________________________________________
42________________________________________________________
43__________________________________####__________________
44______________________________########__________________
45__________________________##########____________________
46______________________##############____________________
47____________________######________####__________________
48__________________________________####__________________
49__________________________________####__________________
50________________________________####____________________
51______________________________####______________________
52________________________##########______________________
53______________________########__####____________________
54________________________##__________##__________________
55____________________________________##__________________
56__________________________________##____________________
57__________________________________##____________________
58________________________________##______________________
59____________________________####________________________
60__________##____________######__________________________
61__________##############________________________________
62________________####____________________________________
63________________________________________________________
64________________________________________________________
65________________________________________________________
66________________________________________________________
67ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no
68ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
69ggml_cuda_init: found 1 CUDA devices:
70 Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes
71mnist_model: using CUDA0 (NVIDIA GeForce RTX 3090) as primary backend
72mnist_model: unsupported operations will be executed on the following fallback backends (in order of priority):
73mnist_model: - CPU (AMD Ryzen 9 5950X 16-Core Processor)
74mnist_model_init_from_file: loading model weights from 'mnist-fc-f32.gguf'
75mnist_model_init_from_file: model arch is mnist-fc
76mnist_model_init_from_file: successfully loaded weights from mnist-fc-f32.gguf
77main: loaded model in 109.44 ms
78mnist_model_eval: model evaluation on 10000 images took 76.92 ms, 7.69 us/image
79main: predicted digit is 3
80main: test_loss=0.066379+-0.009101
81main: test_acc=97.94+-0.14%
82```
83
84In addition to the evaluation on the test set the GGML evaluation also prints a random image from the test set as well as the model prediction for said image.
85To train a fully connected model on the CPU using GGML run:
86
87``` bash
88$ ../../build/bin/mnist-train mnist-fc mnist-fc-f32.gguf data/MNIST/raw/train-images-idx3-ubyte data/MNIST/raw/train-labels-idx1-ubyte
89```
90
91It can then be evaluated with the same binary as above.
92
93## Convolutional network
94
95To train a convolutional network using TensorFlow run:
96
97```bash
98$ python3 mnist-train-cnn.py mnist-cnn-f32.gguf
99
100...
101
102Test loss: 0.047947
103Test accuracy: 98.46%
104GGUF model saved to 'mnist-cnn-f32.gguf'
105```
106
107The saved model can be evaluated on the CPU using the `mnist-eval` binary:
108
109```bash
110$ ../../build/bin/mnist-eval mnist-fc-f32.gguf data/MNIST/raw/t10k-images-idx3-ubyte data/MNIST/raw/t10k-labels-idx1-ubyte
111
112________________________________________________________
113________________________________________________________
114________________________________________________________
115________________________________________________________
116________________________________________________________
117______________________________________##________________
118______________________________________##________________
119______________________________________##________________
120____________________________________##__________________
121__________________________________####__________________
122__________________________________##____________________
123________________________________##______________________
124______________________________##________________________
125____________________________####________________________
126____________________________##__________________________
127__________________________##____________________________
128________________________##______________________________
129______________________##________________________________
130____________________####________________________________
131____________________##__________________________________
132__________________##____________________________________
133________________##______________________________________
134________________________________________________________
135________________________________________________________
136________________________________________________________
137________________________________________________________
138________________________________________________________
139________________________________________________________
140ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no
141ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
142ggml_cuda_init: found 1 CUDA devices:
143 Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes
144mnist_model: using CUDA0 (NVIDIA GeForce RTX 3090) as primary backend
145mnist_model: unsupported operations will be executed on the following fallback backends (in order of priority):
146mnist_model: - CPU (AMD Ryzen 9 5950X 16-Core Processor)
147mnist_model_init_from_file: loading model weights from 'mnist-cnn-f32.gguf'
148mnist_model_init_from_file: model arch is mnist-cnn
149mnist_model_init_from_file: successfully loaded weights from mnist-cnn-f32.gguf
150main: loaded model in 91.99 ms
151mnist_model_eval: model evaluation on 10000 images took 267.61 ms, 26.76 us/image
152main: predicted digit is 1
153main: test_loss=0.047955+-0.007029
154main: test_acc=98.46+-0.12%
155```
156
157Like with the fully connected network the convolutional network can also be trained using GGML:
158
159``` bash
160$ ../../build/bin/mnist-train mnist-cnn mnist-cnn-f32.gguf data/MNIST/raw/train-images-idx3-ubyte data/MNIST/raw/train-labels-idx1-ubyte
161```
162
163As always, the evaluation is done using `mnist-eval` and like with the fully connected network the GGML graph is exported to `mnist-cnn-f32.ggml`.
164
165## Hardware Acceleration
166
167Both the training and evaluation code is agnostic in terms of hardware as long as the corresponding GGML backend has implemented the necessary operations.
168A specific backend can be selected by appending the above commands with a backend name.
169The compute graphs then schedule the operations to preferentially use the specified backend.
170Note that if a backend does not implement some of the necessary operations a CPU fallback is used instead which may result in bad performance.
171
172## Web demo
173
174The evaluation code can be compiled to WebAssembly using [Emscripten](https://emscripten.org/) (may need to re-login to update `$PATH` after installation).
175First, copy the GGUF file of either of the trained models to `examples/mnist` and name it `mnist-f32.gguf`.
176Copy the test set to `examples/mnist` and name it `t10k-images-idx3-ubyte`.
177Symlinking these files will *not* work!
178Compile the code like so:
179
180```bash
181$ cd ../../
182$ mkdir -p build-em
183$ emcmake cmake .. -DGGML_BUILD_EXAMPLES=ON \
184 -DCMAKE_C_FLAGS="-pthread -matomics -mbulk-memory" \
185 -DCMAKE_CXX_FLAGS="-pthread -matomics -mbulk-memory"
186$ make mnist
187```
188
189The compilation output is copied into `examples/mnist/web`.
190To run it, you need an HTTP server.
191For example:
192
193``` bash
194$ python3 examples/mnist/server.py
195
196Serving directory '/home/danbev/work/ai/ggml/examples/mnist/web' at http://localhost:8000
197Application context root: http://localhost:8000/
198```
199
200The web demo can then be accessed via the link printed on the console.
201Simply draw a digit on the canvas and the model will try to predict what it's supposed to be.
202Alternatively, click the "Random" button to retrieve a random digit from the test set.
203Be aware that like all neural networks the one we trained is susceptible to distributional shift:
204if the numbers you draw look different than the ones in the training set
205(e.g. because they're not centered) the model will perform comparatively worse.
206An online demo can be accessed [here](https://mnist.ggerganov.com).