Baremetal-NN
Baremetal-NN API documentation
Loading...
Searching...
No Matches
nn.h
Go to the documentation of this file.
1
8#ifndef __NN_H
9#define __NN_H
10
11#include <assert.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <math.h>
16
17#include "float16.h"
18
19
20// http://elm-chan.org/junk/32bit/binclude.html
21#define INCLUDE_FILE(section, filename, symbol) asm (\
22 ".section "#section"\n" /* Change section */\
23 ".balign 4\n" /* Word alignment */\
24 ".global "#symbol"_start\n" /* Export the object start address */\
25 ".global "#symbol"_data\n" /* Export the object address */\
26 #symbol"_start:\n" /* Define the object start address label */\
27 #symbol"_data:\n" /* Define the object label */\
28 ".incbin \""filename"\"\n" /* Import the file */\
29 ".global "#symbol"_end\n" /* Export the object end address */\
30 #symbol"_end:\n" /* Define the object end address label */\
31 ".balign 4\n" /* Word alignment */\
32 ".section \".text\"\n") /* Restore section */
33
34
40typedef struct {
43
49typedef struct {
50 float data;
52
58typedef struct {
59 size_t shape[1];
62
68typedef struct {
69 size_t shape[1];
70 float *data;
72
78typedef struct {
79 size_t shape[2];
82
88typedef struct {
89 size_t shape[2];
90 float *data;
92
101static inline void nn_assert(int condition, char *message) {
102 if (!condition) {
103 printf("Assertion failed: ");
104 printf("%s\n", message);
105 exit(1);
106 }
107}
108
119static inline uint8_t float_equal(float golden, float actual, float rel_err) {
120 return (fabs(actual - golden) < rel_err) || (fabs((actual - golden) / actual) < rel_err);
121}
122
123
132
141
150Tensor1D_F16 *nn_tensor1d_f16(size_t shape[1], const float16_t *data);
151
160Tensor1D_F32 *nn_tensor1d_f32(size_t shape[1], const float *data);
161
170Tensor2D_F16 *nn_tensor2d_f16(size_t shape[2], const float16_t *data);
171
180Tensor2D_F32 *nn_tensor2d_f32(size_t shape[2], const float *data);
181
182
190void nn_print_u8(uint8_t v);
191
199void nn_print_i8(int8_t v);
200
208void nn_print_u16(uint16_t v);
209
217void nn_print_i16(int16_t v);
218
226void nn_print_u32(uint32_t v);
227
235void nn_print_i32(int32_t v);
236
245void nn_print_f16(float16_t v, int16_t num_digits);
246
255void nn_print_f32(float v, int16_t num_digits);
256
265void nn_print_shape(size_t ndim, const size_t *shape);
266
275
284
293
302
313uint8_t nn_equals0d_f16(const Tensor0D_F16 *a, const Tensor0D_F16 *b, float rel_err);
314
325uint8_t nn_equals0d_f32(const Tensor0D_F32 *a, const Tensor0D_F32 *b, float rel_err);
326
337uint8_t nn_equals1d_f16(const Tensor1D_F16 *a, const Tensor1D_F16 *b, float rel_err);
338
349uint8_t nn_equals1d_f32(const Tensor1D_F32 *a, const Tensor1D_F32 *b, float rel_err);
350
361uint8_t nn_equals2d_f16(const Tensor2D_F16 *a, const Tensor2D_F16 *b, float rel_err);
362
373uint8_t nn_equals2d_f32(const Tensor2D_F32 *a, const Tensor2D_F32 *b, float rel_err);
374
375
387void nn_add1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x1, const Tensor1D_F16 *x2);
388
400void nn_add1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x1, const Tensor1D_F32 *x2);
401
413void nn_add2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2);
414
426void nn_add2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2);
427
428
430
431void nn_addscalar1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x, float scalar);
432
434
435void nn_addscalar2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float scalar);
436
437
438void nn_addmm_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, const Tensor2D_F16 *weight, const Tensor1D_F16 *bias);
439
440void nn_addmm_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, const Tensor2D_F32 *weight, const Tensor1D_F32 *bias);
441
442
443
444void nn_elu2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, float alpha);
445
446void nn_elu2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float alpha);
447
448
449
451
453
454
456
458
459
461
463
465
467
468
470
472
474
476
477
478void nn_mm_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2);
479
480void nn_mm_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2);
481
482
483void nn_mul1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x1, const Tensor1D_F16 *x2);
484
485void nn_mul1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x1, const Tensor1D_F32 *x2);
486
487void nn_mul2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2);
488
489void nn_mul2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2);
490
492
493void nn_mulscalar1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x, float scalar);
494
496
497void nn_mulscalar2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float scalar);
498
499
500
501
502
503
504
505
506#endif // __NN_H
uint16_t float16_t
Definition: float16.h:33
Half-Precision Floating-Point (fp16) Definitions.
void nn_min2d_f16(Tensor0D_F16 *y, const Tensor2D_F16 *x)
void nn_mulscalar2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, float16_t scalar)
Tensor2D_F16 * nn_tensor2d_f16(size_t shape[2], const float16_t *data)
void nn_print_f32(float v, int16_t num_digits)
uint8_t nn_equals0d_f32(const Tensor0D_F32 *a, const Tensor0D_F32 *b, float rel_err)
void nn_mm_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2)
void nn_min1d_f16(Tensor0D_F16 *y, const Tensor1D_F16 *x)
void nn_print_shape(size_t ndim, const size_t *shape)
void nn_print_f16(float16_t v, int16_t num_digits)
void nn_mulscalar2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float scalar)
void nn_addscalar1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x, float16_t scalar)
void nn_print_u8(uint8_t v)
void nn_mul1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x1, const Tensor1D_F16 *x2)
static void nn_assert(int condition, char *message)
Definition: nn.h:101
static uint8_t float_equal(float golden, float actual, float rel_err)
Definition: nn.h:119
void nn_addscalar2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float scalar)
void nn_addscalar2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, float16_t scalar)
void nn_mul2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2)
void nn_print_i32(int32_t v)
void nn_max2d_f32(Tensor0D_F32 *y, const Tensor2D_F32 *x)
void nn_tanh2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x)
void nn_relu2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x)
Tensor2D_F32 * nn_tensor2d_f32(size_t shape[2], const float *data)
void nn_add2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2)
void nn_print_tensor1d_f16(const Tensor1D_F16 *tensor)
void nn_mm_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2)
void nn_min2d_f32(Tensor0D_F32 *y, const Tensor2D_F32 *x)
void nn_print_tensor2d_f16(const Tensor2D_F16 *tensor)
uint8_t nn_equals2d_f16(const Tensor2D_F16 *a, const Tensor2D_F16 *b, float rel_err)
uint8_t nn_equals0d_f16(const Tensor0D_F16 *a, const Tensor0D_F16 *b, float rel_err)
void nn_elu2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, float alpha)
void nn_addscalar1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x, float scalar)
void nn_tanh2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x)
void nn_print_u16(uint16_t v)
void nn_mul1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x1, const Tensor1D_F32 *x2)
void nn_print_u32(uint32_t v)
void nn_addmm_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, const Tensor2D_F16 *weight, const Tensor1D_F16 *bias)
void nn_mulscalar1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x, float scalar)
void nn_add1d_f32(Tensor1D_F32 *y, const Tensor1D_F32 *x1, const Tensor1D_F32 *x2)
uint8_t nn_equals1d_f32(const Tensor1D_F32 *a, const Tensor1D_F32 *b, float rel_err)
void nn_add1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x1, const Tensor1D_F16 *x2)
void nn_max2d_f16(Tensor0D_F16 *y, const Tensor2D_F16 *x)
void nn_mulscalar1d_f16(Tensor1D_F16 *y, const Tensor1D_F16 *x, float16_t scalar)
void nn_print_tensor2d_f32(const Tensor2D_F32 *tensor)
void nn_elu2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x, float alpha)
void nn_print_i8(int8_t v)
void nn_min1d_f32(Tensor0D_F32 *y, const Tensor1D_F32 *x)
void nn_mul2d_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x1, const Tensor2D_F32 *x2)
void nn_add2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x1, const Tensor2D_F16 *x2)
void nn_relu2d_f16(Tensor2D_F16 *y, const Tensor2D_F16 *x)
void nn_max1d_f16(Tensor0D_F16 *y, const Tensor1D_F16 *x)
uint8_t nn_equals1d_f16(const Tensor1D_F16 *a, const Tensor1D_F16 *b, float rel_err)
Tensor1D_F16 * nn_tensor1d_f16(size_t shape[1], const float16_t *data)
uint8_t nn_equals2d_f32(const Tensor2D_F32 *a, const Tensor2D_F32 *b, float rel_err)
Tensor0D_F32 * nn_tensor0d_f32(float data)
Tensor0D_F16 * nn_tensor0d_f16(float16_t data)
void nn_print_tensor1d_f32(const Tensor1D_F32 *tensor)
void nn_max1d_f32(Tensor0D_F32 *y, const Tensor1D_F32 *x)
Tensor1D_F32 * nn_tensor1d_f32(size_t shape[1], const float *data)
void nn_print_i16(int16_t v)
void nn_addmm_f32(Tensor2D_F32 *y, const Tensor2D_F32 *x, const Tensor2D_F32 *weight, const Tensor1D_F32 *bias)
float16_t data
Definition: nn.h:41
Definition: nn.h:40
float data
Definition: nn.h:50
Definition: nn.h:49
float16_t * data
Definition: nn.h:60
Definition: nn.h:58
float * data
Definition: nn.h:70
Definition: nn.h:68
float16_t * data
Definition: nn.h:80
Definition: nn.h:78
float * data
Definition: nn.h:90
Definition: nn.h:88