| AIfES 2 2.0.0

aifes_core.h
Go to the documentation of this file.
1
28 #ifndef AIFES_CORE
29 #define AIFES_CORE
30
31 #include "aifes_math.h"
32 #include "aifes_config.h"
33
34 #define TRUE 1
35 #define FALSE 0
36
37 #define AILAYER_SETTINGS_TRAINING_MODE 0
38 #define AILAYER_SETTINGS_TRAINABLE 1
39 #define AILAYER_SETTINGS_BATCH_MODE 2 // When true, a whole batch is processed in a single forward pass
40 #define AILAYER_SETTINGS_NO_INPUT_GRADIENT 3 // When true, no input gradient is calculated
41 #define AILAYER_SETTINGS_KEEP_INPUT_BUFFER_FOR_RESULT 4 // When true, no input gradient is calculated
42
43 #define AILAYER_SETTINGS_SET(settings, mask, selector, value) (settings = ((settings) & ~(mask << (selector))) | ((value) << (selector)))
44 #define AILAYER_SETTINGS_IS(settings, mask, selector) (((settings) >> (selector)) & mask)
45
46 typedef struct ailayer ailayer_t;
47 typedef struct ailoss ailoss_t;
48 typedef struct aimodel aimodel_t;
49 typedef struct aiopti aiopti_t;
50
51 typedef struct aicore_layertype aicore_layertype_t;
52 typedef struct aicore_losstype aicore_losstype_t;
53 typedef struct aicore_optitype aicore_optitype_t;
54
55
82 struct aicore_layertype {
83 const char *name;
92 void (*print_specs)(const ailayer_t *self);
93 };
94
121 struct aicore_losstype {
122 const char *name;
131 void (*print_specs)(const ailoss_t *self);
132 };
133
160 struct aicore_optitype {
161 const char *name;
170 void (*print_specs)(const aiopti_t *self);
171 };
172
181 struct aimodel {
182 ailayer_t *input_layer;
183 ailayer_t *output_layer;
185 uint16_t layer_count;
186 uint16_t trainable_params_count;
188 ailoss_t *loss;
189 };
190
191
252 struct ailayer {
253 const aicore_layertype_t *layer_type;
254 void *layer_configuration;
271 uint32_t settings;
277 ailayer_t *input_layer;
278 //ailayer_t *brother_input_layer; /**< (NOT_IN_USE) Chained list if multiple input layer are present else NULL. */
279
280 ailayer_t *output_layer;
281 //ailayer_t *brother_output_layer; /**< (NOT_IN_USE) Chained list if multiple output layer are present else NULL. */
283
288 ailayer_t *next_scheduled;
289 ailayer_t *prev_scheduled;
291
292 aitensor_t result;
300 void (*calc_result_shape)(ailayer_t *self);
301
309 void (*calc_result_tensor_params)(ailayer_t *self);
310
315 void (*forward)(ailayer_t *self);
316
317 // Maybe for later purpose
318 //void (*sizeof_infmem)(struct aifes_layer_t *, void *);
319 //void (*set_infmem)(struct aifes_layer_t *, void *);
320
321 // ------------------ Used for training only: -----------------------
322
323 aitensor_t deltas;
331 uint8_t trainable_params_count;
332 aitensor_t **trainable_params;
333 aitensor_t **gradients;
334 void **optimem;
336
341 void (*backward)(ailayer_t *self);
342
351 uint32_t (*sizeof_paramem)(const ailayer_t *self);
352 void (*set_paramem)(ailayer_t *self, void* memory_ptr);
353 void (*init_params)(ailayer_t *self);
355
360 uint32_t (*sizeof_fwdmem)(const ailayer_t *self);
361 uint32_t (*sizeof_bwdmem)(const ailayer_t *self);
362 void *tempmem;
364
372 uint32_t (*sizeof_trainmem)(const ailayer_t *self);
373 void (*set_trainmem)(ailayer_t *self, void* memory_ptr);
375 };
376
377
385 struct ailoss {
386 const aicore_losstype_t *loss_type;
387 void *loss_configuration;
389 ailayer_t connection_layer;
397 void (*calc_loss)(ailoss_t *self, const aitensor_t *target_data, void *result);
398
404 void (*calc_delta)(ailoss_t *self, const aitensor_t *target_data);
405 };
406
407
438 struct aiopti {
439 const aicore_optitype_t *optimizer_type;
440 void *optimizer_configuration;
441 const aimath_dtype_t *dtype;
443 void *learning_rate;
450 uint32_t (*sizeof_optimem)(aiopti_t *self, const aitensor_t *params);
451
459 void (*init_optimem)(aiopti_t *self, const aitensor_t *params, const aitensor_t *gradients, void *optimem);
460
466 void (*zero_gradients)(aiopti_t *self, aitensor_t *gradients);
467
472 void (*begin_step)(aiopti_t *self);
473
481 void (*update_params)(aiopti_t *self, aitensor_t *params, const aitensor_t *gradients, void *optimem);
482
487 void (*end_step)(aiopti_t *self);
488 };
489
490
491 #endif // AIFES_CORE
aifes_math.h
AIfES 2 math interface.
aicore_layertype
Type indicator of the layer.
Definition: aifes_core.h:82
aicore_layertype::print_specs
void(* print_specs)(const ailayer_t *self)
Set a function to print specs of the layer (for example size, constants)
Definition: aifes_core.h:92
aicore_layertype::name
const char * name
Name of the layer type (for example "Dense")
Definition: aifes_core.h:83
aicore_losstype
Type indicator of the loss to check for the loss type.
Definition: aifes_core.h:121
aicore_losstype::print_specs
void(* print_specs)(const ailoss_t *self)
Set a function to print specs of the loss.
Definition: aifes_core.h:131
aicore_losstype::name
const char * name
Name of the loss type (for example "Mean Squared Error")
Definition: aifes_core.h:122
aicore_optitype
Type indicator of the optimizer to check for the optimizer type.
Definition: aifes_core.h:160
aicore_optitype::print_specs
void(* print_specs)(const aiopti_t *self)
Set a function to print specs of the optimizer.
Definition: aifes_core.h:170
aicore_optitype::name
const char * name
Name of the optimizer type (for example "ADAM")
Definition: aifes_core.h:161
ailayer
AIfES layer interface.
Definition: aifes_core.h:252
ailayer::optimem
void ** optimem
Array of memory pointers with length trainable_params_count.
Definition: aifes_core.h:334
ailayer::result
aitensor_t result
The result of the forward function is stored here.
Definition: aifes_core.h:292
ailayer::set_paramem
void(* set_paramem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:352
ailayer::backward
void(* backward)(ailayer_t *self)
Calculate the backward pass and write the result to the deltas tensor.
Definition: aifes_core.h:341
ailayer::layer_type
const aicore_layertype_t * layer_type
Type of the layer (for example ailayer_dense_type)
Definition: aifes_core.h:253
ailayer::layer_configuration
void * layer_configuration
Layer specific configurations (back-link from abstract layer class to implementation)
Definition: aifes_core.h:254
ailayer::trainable_params_count
uint8_t trainable_params_count
Number of trainable parameter tensors.
Definition: aifes_core.h:331
ailayer::set_trainmem
void(* set_trainmem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:373
ailayer::tempmem
void * tempmem
Pointer to the memory for the forward pass, backward pass and the optimizer.
Definition: aifes_core.h:362
ailayer::forward
void(* forward)(ailayer_t *self)
Calculate the forward pass and write the result to the result tensor.
Definition: aifes_core.h:315
ailayer::calc_result_shape
void(* calc_result_shape)(ailayer_t *self)
Calculate and write the shape to the result tensor.
Definition: aifes_core.h:300
ailayer::deltas
aitensor_t deltas
The result of the backward function is stored here.
Definition: aifes_core.h:323
ailayer::trainable_params
aitensor_t ** trainable_params
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:332
ailayer::sizeof_bwdmem
uint32_t(* sizeof_bwdmem)(const ailayer_t *self)
Size of required memory for the backward pass (in bytes).
Definition: aifes_core.h:361
ailayer::calc_result_tensor_params
void(* calc_result_tensor_params)(ailayer_t *self)
If available, calculate and set the tensor_params of the result tensor.
Definition: aifes_core.h:309
ailayer::sizeof_paramem
uint32_t(* sizeof_paramem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:351
ailayer::settings
uint32_t settings
General layer settings like freezing weights or switching between training and evaluation mode.
Definition: aifes_core.h:271
ailayer::sizeof_fwdmem
uint32_t(* sizeof_fwdmem)(const ailayer_t *self)
Size of required memory for the forward pass (in bytes).
Definition: aifes_core.h:360
ailayer::gradients
aitensor_t ** gradients
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:333
ailayer::init_params
void(* init_params)(ailayer_t *self)
Initialize the (trainable and not trainable) parameters of the layer with default initializers.
Definition: aifes_core.h:353
ailayer::sizeof_trainmem
uint32_t(* sizeof_trainmem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:372
ailoss
AIfES loss interface.
Definition: aifes_core.h:385
ailoss::loss_configuration
void * loss_configuration
Loss specific configurations (back-link from abstract loss class to implementation)
Definition: aifes_core.h:387
ailoss::loss_type
const aicore_losstype_t * loss_type
Type of the loss (for example ailoss_mse_type)
Definition: aifes_core.h:386
ailoss::connection_layer
ailayer_t connection_layer
Dummy layer for docking to the layer structure.
Definition: aifes_core.h:389
ailoss::calc_delta
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:404
ailoss::calc_loss
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:397
aimath_dtype
Indicator for the used datatype.
Definition: aifes_math.h:44
aimodel
AIfES artificial neural network model.
Definition: aifes_core.h:181
aimodel::trainable_params_count
uint16_t trainable_params_count
Total number of trainable parameter tensors.
Definition: aifes_core.h:186
aimodel::layer_count
uint16_t layer_count
Total number of layers of the model (usually autogenerated).
Definition: aifes_core.h:185
aimodel::input_layer
ailayer_t * input_layer
Input layer of the model that gets the input data.
Definition: aifes_core.h:182
aimodel::output_layer
ailayer_t * output_layer
Output layer of the model.
Definition: aifes_core.h:183
aimodel::loss
ailoss_t * loss
The loss or cost function of the model (only for training).
Definition: aifes_core.h:188
aiopti
AIfES optimizer interface.
Definition: aifes_core.h:438
aiopti::optimizer_configuration
void * optimizer_configuration
Optimizer specific configurations (back-link from abstract aiopti class to implementation)
Definition: aifes_core.h:440
aiopti::init_optimem
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:459
aiopti::begin_step
void(* begin_step)(aiopti_t *self)
Called in the beginning of every model optimization step for parameter initialization.
Definition: aifes_core.h:472
aiopti::optimizer_type
const aicore_optitype_t * optimizer_type
Type of the optimizer (for example aiopti_sgd_type)
Definition: aifes_core.h:439
aiopti::update_params
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:481
aiopti::sizeof_optimem
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:450
aiopti::learning_rate
void * learning_rate
The learning rate configures the training speed.
Definition: aifes_core.h:443
aiopti::dtype
const aimath_dtype_t * dtype
The data-type of the parameter that the optimizer can optimize and the learning rate.
Definition: aifes_core.h:441
aiopti::zero_gradients
void(* zero_gradients)(aiopti_t *self, aitensor_t *gradients)
Set the gradient tensor to zero.
Definition: aifes_core.h:466
aiopti::end_step
void(* end_step)(aiopti_t *self)
Called in the end of every model optimization step.
Definition: aifes_core.h:487
aitensor
A tensor in AIfES.
Definition: aifes_math.h:89