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 / mnist-common.h

166 lines · 7098 bytes Raw Blame History
1#include <algorithm>
2#include <cstdint>
3#include <random>
4#include <string>
5#include <thread>
6#include <vector>
7
8#include "ggml-alloc.h"
9#include "ggml-backend.h"
10#include "ggml.h"
11#include "gguf.h"
12#include "ggml-cpu.h"
13#include "ggml-opt.h"
14
15#define MNIST_NTRAIN 60000
16#define MNIST_NTEST 10000
17
18// Gradient accumulation can be achieved by setting the logical batch size to a multiple of the physical one.
19// The logical batch size determines how many datapoints are used for a gradient update.
20// The physical batch size determines how many datapoints are processed in parallel, larger values utilize compute better but need more memory.
21#define MNIST_NBATCH_LOGICAL 1000
22#define MNIST_NBATCH_PHYSICAL 500
23
24static_assert(MNIST_NBATCH_LOGICAL % MNIST_NBATCH_PHYSICAL == 0, "MNIST_NBATCH_LOGICAL % MNIST_NBATCH_PHYSICAL != 0");
25static_assert(MNIST_NTRAIN % MNIST_NBATCH_LOGICAL == 0, "MNIST_NTRAIN % MNIST_NBATCH_LOGICAL != 0");
26static_assert(MNIST_NTEST % MNIST_NBATCH_LOGICAL == 0, "MNIST_NTRAIN % MNIST_NBATCH_LOGICAL != 0");
27
28#define MNIST_HW 28
29#define MNIST_NINPUT (MNIST_HW*MNIST_HW)
30#define MNIST_NCLASSES 10
31
32#define MNIST_NHIDDEN 500
33
34// NCB = number of channels base
35#define MNIST_CNN_NCB 8
36
37struct mnist_model {
38 std::string arch;
39 ggml_backend_sched_t backend_sched;
40 std::vector<ggml_backend_t> backends;
41 const int nbatch_logical;
42 const int nbatch_physical;
43
44 struct ggml_tensor * images = nullptr;
45 struct ggml_tensor * logits = nullptr;
46
47 struct ggml_tensor * fc1_weight = nullptr;
48 struct ggml_tensor * fc1_bias = nullptr;
49 struct ggml_tensor * fc2_weight = nullptr;
50 struct ggml_tensor * fc2_bias = nullptr;
51
52 struct ggml_tensor * conv1_kernel = nullptr;
53 struct ggml_tensor * conv1_bias = nullptr;
54 struct ggml_tensor * conv2_kernel = nullptr;
55 struct ggml_tensor * conv2_bias = nullptr;
56 struct ggml_tensor * dense_weight = nullptr;
57 struct ggml_tensor * dense_bias = nullptr;
58
59 struct ggml_context * ctx_gguf = nullptr;
60 struct ggml_context * ctx_static = nullptr;
61 struct ggml_context * ctx_compute = nullptr;
62 ggml_backend_buffer_t buf_gguf = nullptr;
63 ggml_backend_buffer_t buf_static = nullptr;
64
65 mnist_model(const std::string & backend_name, const int nbatch_logical, const int nbatch_physical)
66 : nbatch_logical(nbatch_logical), nbatch_physical(nbatch_physical) {
67 std::vector<ggml_backend_dev_t> devices;
68 const int ncores_logical = std::thread::hardware_concurrency();
69 const int nthreads = std::min(ncores_logical, (ncores_logical + 4) / 2);
70
71 // Add primary backend:
72 if (!backend_name.empty()) {
73 ggml_backend_dev_t dev = ggml_backend_dev_by_name(backend_name.c_str());
74 if (dev == nullptr) {
75 fprintf(stderr, "%s: ERROR: backend %s not found, available:\n", __func__, backend_name.c_str());
76 for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
77 ggml_backend_dev_t dev_i = ggml_backend_dev_get(i);
78 fprintf(stderr, " - %s (%s)\n", ggml_backend_dev_name(dev_i), ggml_backend_dev_description(dev_i));
79 }
80 exit(1);
81 }
82
83 ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
84 GGML_ASSERT(backend);
85
86 if (ggml_backend_is_cpu(backend)) {
87 ggml_backend_cpu_set_n_threads(backend, nthreads);
88 }
89
90 backends.push_back(backend);
91 devices.push_back(dev);
92 }
93
94 // Add all available backends as fallback.
95 // A "backend" is a stream on a physical device so there is no problem with adding multiple backends for the same device.
96 for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
97 ggml_backend_dev_t dev = ggml_backend_dev_get(i);
98
99 ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
100 GGML_ASSERT(backend);
101
102 if (ggml_backend_is_cpu(backend)) {
103 ggml_backend_cpu_set_n_threads(backend, nthreads);
104 }
105
106 backends.push_back(backend);
107 devices.push_back(dev);
108 }
109
110 // The order of the backends passed to ggml_backend_sched_new determines which backend is given priority.
111 backend_sched = ggml_backend_sched_new(backends.data(), nullptr, backends.size(), GGML_DEFAULT_GRAPH_SIZE, false, true);
112 fprintf(stderr, "%s: using %s (%s) as primary backend\n",
113 __func__, ggml_backend_name(backends[0]), ggml_backend_dev_description(devices[0]));
114 if (backends.size() >= 2) {
115 fprintf(stderr, "%s: unsupported operations will be executed on the following fallback backends (in order of priority):\n", __func__);
116 for (size_t i = 1; i < backends.size(); ++i) {
117 fprintf(stderr, "%s: - %s (%s)\n", __func__, ggml_backend_name(backends[i]), ggml_backend_dev_description(devices[i]));
118 }
119 }
120
121 {
122 const size_t size_meta = 1024*ggml_tensor_overhead();
123 struct ggml_init_params params = {
124 /*.mem_size =*/ size_meta,
125 /*.mem_buffer =*/ nullptr,
126 /*.no_alloc =*/ true,
127 };
128 ctx_static = ggml_init(params);
129 }
130
131 {
132 // The compute context needs a total of 3 compute graphs: forward pass + backwards pass (with/without optimizer step).
133 const size_t size_meta = GGML_DEFAULT_GRAPH_SIZE*ggml_tensor_overhead() + 3*ggml_graph_overhead();
134 struct ggml_init_params params = {
135 /*.mem_size =*/ size_meta,
136 /*.mem_buffer =*/ nullptr,
137 /*.no_alloc =*/ true,
138 };
139 ctx_compute = ggml_init(params);
140 }
141 }
142
143 ~mnist_model() {
144 ggml_free(ctx_gguf);
145 ggml_free(ctx_static);
146 ggml_free(ctx_compute);
147
148 ggml_backend_buffer_free(buf_gguf);
149 ggml_backend_buffer_free(buf_static);
150 ggml_backend_sched_free(backend_sched);
151 for (ggml_backend_t backend : backends) {
152 ggml_backend_free(backend);
153 }
154 }
155};
156
157bool mnist_image_load(const std::string & fname, ggml_opt_dataset_t dataset);
158void mnist_image_print(FILE * f, ggml_opt_dataset_t dataset, const int iex);
159bool mnist_label_load(const std::string & fname, ggml_opt_dataset_t dataset);
160
161mnist_model mnist_model_init_from_file(const std::string & fname, const std::string & backend, const int nbatch_logical, const int nbatch_physical);
162mnist_model mnist_model_init_random(const std::string & arch, const std::string & backend, const int nbatch_logical, const int nbatch_physical);
163void mnist_model_build(mnist_model & model);
164ggml_opt_result_t mnist_model_eval(mnist_model & model, ggml_opt_dataset_t dataset);
165void mnist_model_train(mnist_model & model, ggml_opt_dataset_t dataset, const int nepoch, const float val_split);
166void mnist_model_save(mnist_model & model, const std::string & fname);