AIfES 2  2.0.0
aifes_core.h
Go to the documentation of this file.
1 
31 #ifndef AIFES_CORE
32 #define AIFES_CORE
33 
34 #include "aifes_math.h"
35 
36 #define TRUE 1
37 #define FALSE 0
38 
39 // ToDo: Make enum from the values
40 #define AILAYER_RESULT_LOWER_BOUND 0
41 #define AILAYER_RESULT_UPPER_BOUND 1
42 #define AILAYER_DELTAS_LOWER_BOUND 2
43 #define AILAYER_DELTAS_UPPER_BOUND 3
44 
45 typedef struct ailayer ailayer_t;
46 typedef struct ailoss ailoss_t;
47 typedef struct aimodel aimodel_t;
48 typedef struct aiopti aiopti_t;
49 
51 typedef struct aicore_losstype aicore_losstype_t;
52 typedef struct aicore_optitype aicore_optitype_t;
53 
54 
82  const char *name;
91  void (*print_specs)(const ailayer_t *self, int (*print)(const char *format, ...));
92 };
93 
121  const char *name;
130  void (*print_specs)(const ailoss_t *self, int (*print)(const char *format, ...));
131 };
132 
160  const char *name;
169  void (*print_specs)(const aiopti_t *self, int (*print)(const char *format, ...));
170 };
171 
180 struct aimodel {
184  uint16_t layer_count;
188 };
189 
190 
251 struct ailayer {
259  ailayer_t *input_layer;
260  //ailayer_t *brother_input_layer; /**< (NOT_IN_USE) Chained list if multiple input layer are present else NULL. */
261 
262  ailayer_t *output_layer;
263  //ailayer_t *brother_output_layer; /**< (NOT_IN_USE) Chained list if multiple output layer are present else NULL. */
265 
270  ailayer_t *next_scheduled;
271  ailayer_t *prev_scheduled;
273 
286  uint8_t (*get_result_bound)(const ailayer_t *self, const uint8_t selector, void *result_bound);
287 
294  void (*calc_result_shape)(ailayer_t *self);
295 
300  void (*forward)(ailayer_t *self);
301 
302  // Maybe for later purpose
303  //void (*sizeof_infmem)(struct aifes_layer_t *, void *);
304  //void (*set_infmem)(struct aifes_layer_t *, void *);
305 
306  // ------------------ Used for training only: -----------------------
307 
319  void **optimem;
321 
326  void (*backward)(ailayer_t *self);
327 
336  uint32_t (*sizeof_paramem)(const ailayer_t *self);
337  void (*set_paramem)(ailayer_t *self, void* memory_ptr);
339 
347  uint32_t (*sizeof_trainmem)(const ailayer_t *self);
348  void (*set_trainmem)(ailayer_t *self, void* memory_ptr);
350 };
351 
352 
360 struct ailoss {
372  void (*calc_loss)(ailoss_t *self, const aitensor_t *target_data, void *result);
373 
379  void (*calc_delta)(ailoss_t *self, const aitensor_t *target_data);
380 };
381 
382 
413 struct aiopti {
425  uint32_t (*sizeof_optimem)(aiopti_t *self, const aitensor_t *params);
426 
434  void (*init_optimem)(aiopti_t *self, const aitensor_t *params, const aitensor_t *gradients, void *optimem);
435 
441  void (*zero_gradients)(aiopti_t *self, aitensor_t *gradients);
442 
447  void (*begin_step)(aiopti_t *self);
448 
456  void (*update_params)(aiopti_t *self, aitensor_t *params, const aitensor_t *gradients, void *optimem);
457 
462  void (*end_step)(aiopti_t *self);
463 };
464 
465 
466 #endif // AIFES_CORE
AIfES 2 math interface.
Type indicator of the layer.
Definition: aifes_core.h:81
void(* print_specs)(const ailayer_t *self, int(*print)(const char *format,...))
Set a function to print specs of the layer (for example size, constants)
Definition: aifes_core.h:91
const char * name
Name of the layer type (for example "Dense")
Definition: aifes_core.h:82
Type indicator of the loss to check for the loss type.
Definition: aifes_core.h:120
void(* print_specs)(const ailoss_t *self, int(*print)(const char *format,...))
Set a function to print specs of the loss.
Definition: aifes_core.h:130
const char * name
Name of the loss type (for example "Mean Squared Error")
Definition: aifes_core.h:121
Type indicator of the optimizer to check for the optimizer type.
Definition: aifes_core.h:159
void(* print_specs)(const aiopti_t *self, int(*print)(const char *format,...))
Set a function to print specs of the optimizer.
Definition: aifes_core.h:169
const char * name
Name of the optimizer type (for example "ADAM")
Definition: aifes_core.h:160
AIfES layer interface.
Definition: aifes_core.h:251
void ** optimem
Array of memory pointers with length trainable_params_count.
Definition: aifes_core.h:319
aitensor_t result
The result of the forward function is stored here.
Definition: aifes_core.h:274
void(* set_paramem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:337
void(* backward)(ailayer_t *self)
Calculate the backward pass and write the result to the deltas tensor.
Definition: aifes_core.h:326
const aicore_layertype_t * layer_type
Type of the layer (for example ailayer_dense_type)
Definition: aifes_core.h:252
void * layer_configuration
Layer specific configurations (back-link from abstract layer class to implementation)
Definition: aifes_core.h:253
uint8_t trainable_params_count
Number of trainable parameter tensors.
Definition: aifes_core.h:316
void(* set_trainmem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:348
void(* forward)(ailayer_t *self)
Calculate the forward pass and write the result to the result tensor.
Definition: aifes_core.h:300
void(* calc_result_shape)(ailayer_t *self)
Calculate and write the shape to the result tensor.
Definition: aifes_core.h:294
uint8_t(* get_result_bound)(const ailayer_t *self, const uint8_t selector, void *result_bound)
Result and delta min and max values.
Definition: aifes_core.h:286
aitensor_t deltas
The result of the backward function is stored here.
Definition: aifes_core.h:308
aitensor_t ** trainable_params
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:317
uint32_t(* sizeof_paramem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:336
aitensor_t ** gradients
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:318
uint32_t(* sizeof_trainmem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:347
AIfES loss interface.
Definition: aifes_core.h:360
void * loss_configuration
Loss specific configurations (back-link from abstract loss class to implementation)
Definition: aifes_core.h:362
const aicore_losstype_t * loss_type
Type of the loss (for example ailoss_mse_type)
Definition: aifes_core.h:361
ailayer_t connection_layer
Dummy layer for docking to the layer structure.
Definition: aifes_core.h:364
void(* calc_delta)(ailoss_t *self, const aitensor_t *target_data)
Calculate the error on the target data and write it to the deltas tensor of connection layer.
Definition: aifes_core.h:379
void(* calc_loss)(ailoss_t *self, const aitensor_t *target_data, void *result)
Calculate the loss / cost for the model on the given targets.
Definition: aifes_core.h:372
Indicator for the used datatype.
Definition: aifes_math.h:53
AIfES artificial neural network model.
Definition: aifes_core.h:180
uint16_t trainable_params_count
Total number of trainable parameter tensors.
Definition: aifes_core.h:185
uint16_t layer_count
Total number of layers of the model (usually autogenerated).
Definition: aifes_core.h:184
ailayer_t * input_layer
Input layer of the model that gets the input data.
Definition: aifes_core.h:181
ailayer_t * output_layer
Output layer of the model.
Definition: aifes_core.h:182
ailoss_t * loss
The loss or cost function of the model (only for training).
Definition: aifes_core.h:187
AIfES optimizer interface.
Definition: aifes_core.h:413
void * optimizer_configuration
Optimizer specific configurations (back-link from abstract aiopti class to implementation)
Definition: aifes_core.h:415
void(* init_optimem)(aiopti_t *self, const aitensor_t *params, const aitensor_t *gradients, void *optimem)
Initialize the optimization memory for a trainable parameter tensor.
Definition: aifes_core.h:434
void(* begin_step)(aiopti_t *self)
Called in the beginning of every model optimization step for parameter initialization.
Definition: aifes_core.h:447
const aicore_optitype_t * optimizer_type
Type of the optimizer (for example aiopti_sgd_type)
Definition: aifes_core.h:414
void(* update_params)(aiopti_t *self, aitensor_t *params, const aitensor_t *gradients, void *optimem)
Performs an optimization step on the given tensor.
Definition: aifes_core.h:456
uint32_t(* sizeof_optimem)(aiopti_t *self, const aitensor_t *params)
Calculates the optimization memory size for a trainable parameter tensor.
Definition: aifes_core.h:425
void * learning_rate
The learning rate configures the training speed.
Definition: aifes_core.h:418
const aimath_dtype_t * dtype
The data-type of the parameter that the optimizer can optimize and the learning rate.
Definition: aifes_core.h:416
void(* zero_gradients)(aiopti_t *self, aitensor_t *gradients)
Set the gradient tensor to zero.
Definition: aifes_core.h:441
void(* end_step)(aiopti_t *self)
Called in the end of every model optimization step.
Definition: aifes_core.h:462
A tensor in AIfES.
Definition: aifes_math.h:98