Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/examples/mnist/README.md # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / examples / mnist / README.md` 206 lines ยท 9633 bytes Raw Blame History Rendered 1 # MNIST Examples for GGML 2 3 This directory contains simple examples of how to use GGML for training and inference using the [MNIST dataset](https://yann.lecun.com/exdb/mnist/). 4 All commands listed in this README assume the working directory to be `examples/mnist`. 5 Please note that training in GGML is a work-in-progress and not production ready. 6 7 ## Obtaining the data 8 9 A description of the dataset can be found on [Yann LeCun's website](https://yann.lecun.com/exdb/mnist/). 10 While it is also in principle possible to download the dataset from this website these downloads are frequently throttled and 11 it is recommended to use [HuggingFace](https://huggingface.co/datasets/ylecun/mnist) instead. 12 The dataset will be downloaded automatically when running `mnist-train-fc.py`. 13 14 ## Fully connected network 15 16 For our first example we will train a fully connected network. 17 To 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 24 Test loss: 0.066377+-0.010468, Test accuracy: 97.94+-0.14% 25 26 Model tensors saved to mnist-fc-f32.gguf: 27 fc1.weight (500, 784) 28 fc1.bias (500,) 29 fc2.weight (10, 500) 30 fc2.bias (10,) 31 ``` 32 33 The training script includes an evaluation of the model on the test set. 34 To 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 ________________________________________________________ 67 ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no 68 ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no 69 ggml_cuda_init: found 1 CUDA devices: 70 Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes 71 mnist_model: using CUDA0 (NVIDIA GeForce RTX 3090) as primary backend 72 mnist_model: unsupported operations will be executed on the following fallback backends (in order of priority): 73 mnist_model: - CPU (AMD Ryzen 9 5950X 16-Core Processor) 74 mnist_model_init_from_file: loading model weights from 'mnist-fc-f32.gguf' 75 mnist_model_init_from_file: model arch is mnist-fc 76 mnist_model_init_from_file: successfully loaded weights from mnist-fc-f32.gguf 77 main: loaded model in 109.44 ms 78 mnist_model_eval: model evaluation on 10000 images took 76.92 ms, 7.69 us/image 79 main: predicted digit is 3 80 main: test_loss=0.066379+-0.009101 81 main: test_acc=97.94+-0.14% 82 ``` 83 84 In 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. 85 To 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 91 It can then be evaluated with the same binary as above. 92 93 ## Convolutional network 94 95 To train a convolutional network using TensorFlow run: 96 97 ```bash 98 $ python3 mnist-train-cnn.py mnist-cnn-f32.gguf 99 100 ... 101 102 Test loss: 0.047947 103 Test accuracy: 98.46% 104 GGUF model saved to 'mnist-cnn-f32.gguf' 105 ``` 106 107 The 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 ________________________________________________________ 140 ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no 141 ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no 142 ggml_cuda_init: found 1 CUDA devices: 143 Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes 144 mnist_model: using CUDA0 (NVIDIA GeForce RTX 3090) as primary backend 145 mnist_model: unsupported operations will be executed on the following fallback backends (in order of priority): 146 mnist_model: - CPU (AMD Ryzen 9 5950X 16-Core Processor) 147 mnist_model_init_from_file: loading model weights from 'mnist-cnn-f32.gguf' 148 mnist_model_init_from_file: model arch is mnist-cnn 149 mnist_model_init_from_file: successfully loaded weights from mnist-cnn-f32.gguf 150 main: loaded model in 91.99 ms 151 mnist_model_eval: model evaluation on 10000 images took 267.61 ms, 26.76 us/image 152 main: predicted digit is 1 153 main: test_loss=0.047955+-0.007029 154 main: test_acc=98.46+-0.12% 155 ``` 156 157 Like 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 163 As 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 167 Both the training and evaluation code is agnostic in terms of hardware as long as the corresponding GGML backend has implemented the necessary operations. 168 A specific backend can be selected by appending the above commands with a backend name. 169 The compute graphs then schedule the operations to preferentially use the specified backend. 170 Note 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 174 The evaluation code can be compiled to WebAssembly using [Emscripten](https://emscripten.org/) (may need to re-login to update `$PATH` after installation). 175 First, copy the GGUF file of either of the trained models to `examples/mnist` and name it `mnist-f32.gguf`. 176 Copy the test set to `examples/mnist` and name it `t10k-images-idx3-ubyte`. 177 Symlinking these files will *not* work! 178 Compile 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 189 The compilation output is copied into `examples/mnist/web`. 190 To run it, you need an HTTP server. 191 For example: 192 193 ``` bash 194 $ python3 examples/mnist/server.py 195 196 Serving directory '/home/danbev/work/ai/ggml/examples/mnist/web' at http://localhost:8000 197 Application context root: http://localhost:8000/ 198 ``` 199 200 The web demo can then be accessed via the link printed on the console. 201 Simply draw a digit on the canvas and the model will try to predict what it's supposed to be. 202 Alternatively, click the "Random" button to retrieve a random digit from the test set. 203 Be aware that like all neural networks the one we trained is susceptible to distributional shift: 204 if 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. 206 An online demo can be accessed [here](https://mnist.ggerganov.com).