GCC Code Coverage Report


Directory: ../src/
File: extensions/tinyml_extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 16.7% 34 0 204
Functions: 8.3% 2 0 24
Branches: 2.0% 3 0 150

Line Branch Exec Source
1 /*
2 Copyright 2026 Joel Svensson svenssonjoel@yahoo.se
3
4 This file is part of LispBM.
5
6 LispBM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 LispBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <extensions/tinyml_extensions.h>
21 #include <string.h>
22
23 #define TINYML_INSTANCE_MAGIC ((uint32_t)0x544D4C00) // "TML\0"
24 #define TINYML_MAX_TENSORS 8
25
26 typedef struct tinyml_model_entry {
27 const tinyml_model_t *model;
28 struct tinyml_model_entry *next;
29 } tinyml_model_entry_t;
30
31 static tinyml_model_entry_t *models = NULL;
32
33 727 bool lbm_tinyml_register(const tinyml_model_t *model) {
34
2/4
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 727 times.
727 if (!model || !model->name) return false;
35 727 tinyml_model_entry_t *e = lbm_malloc(sizeof(tinyml_model_entry_t));
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 727 times.
727 if (!e) return false;
37 727 e->model = model;
38 727 e->next = models;
39 727 models = e;
40 727 return true;
41 }
42
43 ✗ static const tinyml_model_t *find_model(const char *name) {
44 ✗ for (tinyml_model_entry_t *e = models; e; e = e->next) {
45 ✗ if (strcmp(e->model->name, name) == 0) return e->model;
46 }
47 ✗ return NULL;
48 }
49
50 ✗ static lbm_uint dtype_size(tinyml_dtype_t dt) {
51 ✗ switch (dt) {
52 ✗ case TINYML_I8: return 1;
53 ✗ case TINYML_U8: return 1;
54 ✗ case TINYML_I16: return 2;
55 ✗ case TINYML_F32: return 4;
56 }
57 ✗ return 0;
58 }
59
60 ✗ static lbm_uint tensor_bytes(const tinyml_tensor_info_t *t) {
61 ✗ lbm_uint n = 1;
62 ✗ for (uint8_t i = 0; i < t->ndim; i++) n *= t->shape[i];
63 ✗ return n * dtype_size(t->dtype);
64 }
65
66 // Symbols used by tinyml-info's alist output.
67 static lbm_uint symbol_f32 = 0;
68 static lbm_uint symbol_i8 = 0;
69 static lbm_uint symbol_u8 = 0;
70 static lbm_uint symbol_i16 = 0;
71 static lbm_uint symbol_name = 0;
72 static lbm_uint symbol_n_inputs = 0;
73 static lbm_uint symbol_n_outputs = 0;
74 static lbm_uint symbol_inputs = 0;
75 static lbm_uint symbol_outputs = 0;
76 static lbm_uint symbol_dtype = 0;
77 static lbm_uint symbol_shape = 0;
78 static lbm_uint symbol_scale = 0;
79 static lbm_uint symbol_zero_point = 0;
80
81 ✗ static lbm_value dtype_to_sym(tinyml_dtype_t dt) {
82 ✗ switch (dt) {
83 ✗ case TINYML_I8: return lbm_enc_sym(symbol_i8);
84 ✗ case TINYML_U8: return lbm_enc_sym(symbol_u8);
85 ✗ case TINYML_I16: return lbm_enc_sym(symbol_i16);
86 ✗ case TINYML_F32: return lbm_enc_sym(symbol_f32);
87 }
88 ✗ return ENC_SYM_NIL;
89 }
90
91 ✗ static lbm_value encode_string(const char *s) {
92 ✗ size_t len = strlen(s) + 1;
93 ✗ char *buf = lbm_malloc(len);
94 ✗ if (!buf) return ENC_SYM_MERROR;
95 ✗ memcpy(buf, s, len);
96 lbm_value res;
97 ✗ if (!lbm_lift_array(&res, buf, len)) {
98 ✗ lbm_free(buf);
99 ✗ return ENC_SYM_MERROR;
100 }
101 ✗ return res;
102 }
103
104 typedef struct {
105 uint32_t magic;
106 const tinyml_model_t *model;
107 uint8_t arena[];
108 } tinyml_instance_blob_t;
109
110 ✗ static bool tinyml_instance_destructor(lbm_uint value) {
111 ✗ lbm_free((void*)value);
112 ✗ return true;
113 }
114
115 ✗ static tinyml_instance_blob_t *get_instance_blob(lbm_value v) {
116 ✗ if (!lbm_is_custom(v)) return NULL;
117 ✗ lbm_uint value = lbm_get_custom_value(v);
118 ✗ if (!value) return NULL;
119 ✗ tinyml_instance_blob_t *blob = (tinyml_instance_blob_t*)value;
120 ✗ if (blob->magic != TINYML_INSTANCE_MAGIC) return NULL;
121 ✗ return blob;
122 }
123
124 // (tinyml-models) -> list of registered model name strings
125 ✗ static lbm_value ext_tinyml_models(lbm_value *args, lbm_uint argn) {
126 (void)args;
127 ✗ if (argn != 0) return ENC_SYM_TERROR;
128 ✗ lbm_value res = ENC_SYM_NIL;
129 ✗ for (tinyml_model_entry_t *e = models; e; e = e->next) {
130 ✗ lbm_value s = encode_string(e->model->name);
131 ✗ if (lbm_is_symbol(s)) return s;
132 ✗ res = lbm_cons(s, res);
133 }
134 ✗ return res;
135 }
136
137 // (tinyml-open "name") -> instance handle
138 ✗ static lbm_value ext_tinyml_open(lbm_value *args, lbm_uint argn) {
139 ✗ if (argn != 1 || !lbm_is_array_r(args[0])) return ENC_SYM_TERROR;
140 ✗ char *name = lbm_dec_str(args[0]);
141 ✗ if (!name) return ENC_SYM_TERROR;
142
143 ✗ const tinyml_model_t *model = find_model(name);
144 ✗ if (!model) return ENC_SYM_EERROR;
145
146 ✗ lbm_uint size = sizeof(tinyml_instance_blob_t) + model->arena_bytes;
147 ✗ uint8_t *buf = lbm_malloc(size);
148 ✗ if (!buf) return ENC_SYM_MERROR;
149
150 ✗ tinyml_instance_blob_t *blob = (tinyml_instance_blob_t*)buf;
151 ✗ blob->magic = TINYML_INSTANCE_MAGIC;
152 ✗ blob->model = model;
153
154 ✗ if (model->init &&
155 ✗ model->init(model, model->arena_bytes ? blob->arena : NULL) != 0) {
156 ✗ lbm_free(buf);
157 ✗ return ENC_SYM_EERROR;
158 }
159
160 lbm_value res;
161 ✗ if (!lbm_custom_type_create((lbm_uint)buf, tinyml_instance_destructor,
162 "tinyml-instance", &res)) {
163 ✗ lbm_free(buf);
164 ✗ return ENC_SYM_MERROR;
165 }
166 ✗ return res;
167 }
168
169 ✗ static lbm_value ext_tinyml_is_instance(lbm_value *args, lbm_uint argn) {
170 ✗ if (argn != 1) return ENC_SYM_TERROR;
171 ✗ return get_instance_blob(args[0]) ? ENC_SYM_TRUE : ENC_SYM_NIL;
172 }
173
174 // (tinyml-input-bytes h i) / (tinyml-output-bytes h i)
175 ✗ static lbm_value ext_tinyml_input_bytes(lbm_value *args, lbm_uint argn) {
176 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
177 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
178 ✗ if (!blob) return ENC_SYM_TERROR;
179 ✗ int32_t i = lbm_dec_as_i32(args[1]);
180 ✗ if (i < 0 || i >= blob->model->n_inputs) return ENC_SYM_TERROR;
181 ✗ return lbm_enc_i((lbm_int)tensor_bytes(&blob->model->inputs[i]));
182 }
183
184 ✗ static lbm_value ext_tinyml_output_bytes(lbm_value *args, lbm_uint argn) {
185 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
186 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
187 ✗ if (!blob) return ENC_SYM_TERROR;
188 ✗ int32_t i = lbm_dec_as_i32(args[1]);
189 ✗ if (i < 0 || i >= blob->model->n_outputs) return ENC_SYM_TERROR;
190 ✗ return lbm_enc_i((lbm_int)tensor_bytes(&blob->model->outputs[i]));
191 }
192
193 // //////////////////////////////////////////////////
194 // Info
195
196 ✗ static lbm_value encode_shape(const tinyml_tensor_info_t *t) {
197 ✗ lbm_value res = ENC_SYM_NIL;
198 ✗ for (uint8_t i = t->ndim; i > 0; i--) {
199 ✗ res = lbm_cons(lbm_enc_i((lbm_int)t->shape[i - 1]), res);
200 }
201 ✗ return res;
202 }
203
204 ✗ static lbm_value encode_tensor_info(const tinyml_tensor_info_t *t) {
205 ✗ lbm_value res = ENC_SYM_NIL;
206 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_zero_point), lbm_enc_i(t->zero_point)), res);
207 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_scale), lbm_enc_float(t->scale)), res);
208 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_shape), encode_shape(t)), res);
209 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_dtype), dtype_to_sym(t->dtype)), res);
210 ✗ return res;
211 }
212
213 ✗ static lbm_value encode_tensor_info_list(const tinyml_tensor_info_t *arr, uint8_t n) {
214 ✗ lbm_value res = ENC_SYM_NIL;
215 ✗ for (uint8_t i = n; i > 0; i--) {
216 ✗ res = lbm_cons(encode_tensor_info(&arr[i - 1]), res);
217 }
218 ✗ return res;
219 }
220
221 // (tinyml-info h) -> alist describing the model behind an open instance
222 ✗ static lbm_value ext_tinyml_info(lbm_value *args, lbm_uint argn) {
223 ✗ if (argn != 1) return ENC_SYM_TERROR;
224 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
225 ✗ if (!blob) return ENC_SYM_TERROR;
226 ✗ const tinyml_model_t *model = blob->model;
227
228 ✗ lbm_value res = ENC_SYM_NIL;
229 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_outputs),
230 ✗ encode_tensor_info_list(model->outputs, model->n_outputs)), res);
231 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_inputs),
232 ✗ encode_tensor_info_list(model->inputs, model->n_inputs)), res);
233 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_n_outputs), lbm_enc_i(model->n_outputs)), res);
234 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_n_inputs), lbm_enc_i(model->n_inputs)), res);
235 ✗ res = lbm_cons(lbm_cons(lbm_enc_sym(symbol_name), encode_string(model->name)), res);
236 ✗ return res;
237 }
238
239 // (tinyml-input-type h i) / (tinyml-output-type h i) -> one tensor's info alist
240 ✗ static lbm_value ext_tinyml_input_type(lbm_value *args, lbm_uint argn) {
241 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
242 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
243 ✗ if (!blob) return ENC_SYM_TERROR;
244 ✗ int32_t i = lbm_dec_as_i32(args[1]);
245 ✗ if (i < 0 || i >= blob->model->n_inputs) return ENC_SYM_TERROR;
246 ✗ return encode_tensor_info(&blob->model->inputs[i]);
247 }
248
249 ✗ static lbm_value ext_tinyml_output_type(lbm_value *args, lbm_uint argn) {
250 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
251 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
252 ✗ if (!blob) return ENC_SYM_TERROR;
253 ✗ int32_t i = lbm_dec_as_i32(args[1]);
254 ✗ if (i < 0 || i >= blob->model->n_outputs) return ENC_SYM_TERROR;
255 ✗ return encode_tensor_info(&blob->model->outputs[i]);
256 }
257
258 // (tinyml-n-inputs h) / (tinyml-n-outputs h) -> number of input/output tensors
259 ✗ static lbm_value ext_tinyml_n_inputs(lbm_value *args, lbm_uint argn) {
260 ✗ if (argn != 1) return ENC_SYM_TERROR;
261 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
262 ✗ if (!blob) return ENC_SYM_TERROR;
263 ✗ return lbm_enc_i(blob->model->n_inputs);
264 }
265
266 ✗ static lbm_value ext_tinyml_n_outputs(lbm_value *args, lbm_uint argn) {
267 ✗ if (argn != 1) return ENC_SYM_TERROR;
268 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
269 ✗ if (!blob) return ENC_SYM_TERROR;
270 ✗ return lbm_enc_i(blob->model->n_outputs);
271 }
272
273 ✗ static bool collect_buffers(lbm_value buffers, uint8_t n_expected,
274 const tinyml_tensor_info_t *infos,
275 void **out_ptrs) {
276 ✗ if (n_expected == 0) return true;
277 ✗ if (n_expected == 1 && lbm_is_array_r(buffers)) {
278 ✗ lbm_array_header_t *arr = lbm_dec_array_r(buffers);
279 ✗ if (!arr || arr->size != tensor_bytes(&infos[0])) return false;
280 ✗ out_ptrs[0] = arr->data;
281 ✗ return true;
282 }
283 ✗ if (!lbm_is_list(buffers) || lbm_list_length(buffers) != n_expected) return false;
284 ✗ lbm_value curr = buffers;
285 ✗ for (uint8_t i = 0; i < n_expected; i++) {
286 ✗ lbm_value elt = lbm_car(curr);
287 ✗ lbm_array_header_t *arr = lbm_dec_array_r(elt);
288 ✗ if (!arr || arr->size != tensor_bytes(&infos[i])) return false;
289 ✗ out_ptrs[i] = arr->data;
290 ✗ curr = lbm_cdr(curr);
291 }
292 ✗ return true;
293 }
294
295 // (tinyml-run h in out) -> t or an error symbol
296 ✗ static lbm_value ext_tinyml_run(lbm_value *args, lbm_uint argn) {
297 ✗ if (argn != 3) return ENC_SYM_TERROR;
298 ✗ tinyml_instance_blob_t *blob = get_instance_blob(args[0]);
299 ✗ if (!blob) return ENC_SYM_TERROR;
300
301 ✗ const tinyml_model_t *model = blob->model;
302 ✗ if (model->n_inputs > TINYML_MAX_TENSORS || model->n_outputs > TINYML_MAX_TENSORS) {
303 ✗ return ENC_SYM_EERROR;
304 }
305
306 void *in_ptrs[TINYML_MAX_TENSORS];
307 void *out_ptrs[TINYML_MAX_TENSORS];
308 ✗ if (!collect_buffers(args[1], model->n_inputs, model->inputs, in_ptrs)) {
309 ✗ return ENC_SYM_TERROR;
310 }
311 ✗ if (!collect_buffers(args[2], model->n_outputs, model->outputs, out_ptrs)) {
312 ✗ return ENC_SYM_TERROR;
313 }
314
315 ✗ int rc = model->run(model, (const void *const *)in_ptrs, out_ptrs);
316 ✗ return rc == 0 ? ENC_SYM_TRUE : ENC_SYM_EERROR;
317 }
318
319 727 void lbm_tinyml_extensions_init(void) {
320 727 lbm_add_symbol_const("f32", &symbol_f32);
321 727 lbm_add_symbol_const("i8", &symbol_i8);
322 727 lbm_add_symbol_const("u8", &symbol_u8);
323 727 lbm_add_symbol_const("i16", &symbol_i16);
324 727 lbm_add_symbol_const("name", &symbol_name);
325 727 lbm_add_symbol_const("n-inputs", &symbol_n_inputs);
326 727 lbm_add_symbol_const("n-outputs", &symbol_n_outputs);
327 727 lbm_add_symbol_const("inputs", &symbol_inputs);
328 727 lbm_add_symbol_const("outputs", &symbol_outputs);
329 727 lbm_add_symbol_const("dtype", &symbol_dtype);
330 727 lbm_add_symbol_const("shape", &symbol_shape);
331 727 lbm_add_symbol_const("scale", &symbol_scale);
332 727 lbm_add_symbol_const("zero-point", &symbol_zero_point);
333
334 727 lbm_add_extension("tinyml-models", ext_tinyml_models);
335 727 lbm_add_extension("tinyml-open", ext_tinyml_open);
336 727 lbm_add_extension("tinyml-instance?", ext_tinyml_is_instance);
337 727 lbm_add_extension("tinyml-input-bytes", ext_tinyml_input_bytes);
338 727 lbm_add_extension("tinyml-output-bytes", ext_tinyml_output_bytes);
339 727 lbm_add_extension("tinyml-input-type", ext_tinyml_input_type);
340 727 lbm_add_extension("tinyml-output-type", ext_tinyml_output_type);
341 727 lbm_add_extension("tinyml-n-inputs", ext_tinyml_n_inputs);
342 727 lbm_add_extension("tinyml-n-outputs", ext_tinyml_n_outputs);
343 727 lbm_add_extension("tinyml-info", ext_tinyml_info);
344 727 lbm_add_extension("tinyml-run", ext_tinyml_run);
345 727 }
346