| AIfES 2 2.0.0

ailayer_dense.h
Go to the documentation of this file.
1
52 #ifndef AILAYER_DENSE
53 #define AILAYER_DENSE
54
55 #include "core/aifes_core.h"
56 #include "basic/default/aimath/aimath_f32_default.h"
57 #include "basic/default/aimath/aimath_q7_default.h"
58 #include "basic/base/aimath/aimath_q31.h"
59
60 #define DENSE_WEIGHTS_SIZE(INPUTS, OUTPUTS) ((INPUTS) * (OUTPUTS))
61 #define DENSE_BIAS_SIZE(OUTPUTS) (OUTPUTS)
62
63 #define DENSE_WEIGHTS_SHAPE(INPUTS, OUTPUTS) {INPUTS, OUTPUTS}
64 #define DENSE_BIAS_SHAPE(OUTPUTS) {1, OUTPUTS}
65
66 typedef struct ailayer_dense ailayer_dense_t;
67
71 struct ailayer_dense {
72 ailayer_t base;
80 uint32_t neurons;
82
87 aitensor_t weights;
88 aitensor_t bias;
90 uint16_t weights_shape[2];
91 uint16_t bias_shape[1];
93 aitensor_t *trainable_params[2];
94 aitensor_t *gradients[2];
95 void *optimem[2];
97
102
115 void (*linear)(const aitensor_t *a, const aitensor_t *b, const aitensor_t *c, aitensor_t *result);
116
128 void (*mat_mul_at)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result);
129
141 void (*mat_mul_bt)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result);
142
150 void (*tensor_add)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result);
151
159 void (*sum_channelwise)(const aitensor_t *a, int8_t channel_axis, aitensor_t *result);
160
162
163 uint16_t result_shape[2];
164 };
165
171 extern const aicore_layertype_t *ailayer_dense_type;
172
184 ailayer_t *ailayer_dense(ailayer_dense_t *layer, ailayer_t *input_layer);
185
208 void ailayer_dense_forward(ailayer_t *self);
209
246 void ailayer_dense_backward(ailayer_t *self);
247
256 void ailayer_dense_calc_result_shape(ailayer_t *self);
257
258 uint32_t ailayer_dense_sizeof_bwdmem(const ailayer_t *self);
259
270 uint32_t ailayer_dense_sizeof_paramem(const ailayer_t *self);
271
283 void ailayer_dense_set_paramem(ailayer_t *self, void *memory_ptr);
284
294 uint32_t ailayer_dense_sizeof_trainmem(const ailayer_t *self);
295
305 void ailayer_dense_set_trainmem(ailayer_t *self, void *memory_ptr);
306
307 #ifdef AIDEBUG_PRINT_MODULE_SPECS
312 void ailayer_dense_print_specs(const ailayer_t *self);
313 #endif // AIDEBUG_PRINT_MODULE_SPECS
314
315 #endif // AILAYER_DENSE
aifes_core.h
AIfES 2 core interface.
ailayer_dense_type
const aicore_layertype_t * ailayer_dense_type
Dense layer type.
ailayer_dense_print_specs
void ailayer_dense_print_specs(const ailayer_t *self)
Print the layer specification.
ailayer_dense_set_paramem
void ailayer_dense_set_paramem(ailayer_t *self, void *memory_ptr)
Distribute provided memory to the parameter pointers.
ailayer_dense
ailayer_t * ailayer_dense(ailayer_dense_t *layer, ailayer_t *input_layer)
Initialize and connect the given Dense layer.
ailayer_dense_backward
void ailayer_dense_backward(ailayer_t *self)
Calculate the backward pass for the given Dense layer.
ailayer_dense_forward
void ailayer_dense_forward(ailayer_t *self)
Calculate the forward pass for given Dense layer.
ailayer_dense_set_trainmem
void ailayer_dense_set_trainmem(ailayer_t *self, void *memory_ptr)
Distribute provided memory to the gradients pointers.
ailayer_dense_sizeof_trainmem
uint32_t ailayer_dense_sizeof_trainmem(const ailayer_t *self)
Calculate and return the memory size needed by this layer for training.
ailayer_dense_sizeof_paramem
uint32_t ailayer_dense_sizeof_paramem(const ailayer_t *self)
Calculate and return the parameter memory size needed for this layer.
ailayer_dense_calc_result_shape
void ailayer_dense_calc_result_shape(ailayer_t *self)
Calculate the shape of the result tensor (ailayer.result)
aimath_f32_default.h
Math functions for F32 data type, default implementation.
aimath_q31.h
Definition of the Q31 (aiq31) data-type.
aimath_q7_default.h
Math functions for Q7 data type, default implementation.
aicore_layertype
Type indicator of the layer.
Definition: aifes_core.h:82
ailayer_dense
General Dense layer structure.
Definition: ailayer_dense.h:71
ailayer_dense::weights_shape
uint16_t weights_shape[2]
Weights tensor shape (n x m matrix).
Definition: ailayer_dense.h:90
ailayer_dense::bias_shape
uint16_t bias_shape[1]
Bias weights tensor shape (n x m matrix).
Definition: ailayer_dense.h:91
ailayer_dense::gradients
aitensor_t * gradients[2]
Gradients structure for the back propagation algorithm.
Definition: ailayer_dense.h:94
ailayer_dense::mat_mul_bt
void(* mat_mul_bt)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result)
Required math function: Matrix multiplication with transposed b.
Definition: ailayer_dense.h:141
ailayer_dense::sum_channelwise
void(* sum_channelwise)(const aitensor_t *a, int8_t channel_axis, aitensor_t *result)
Required math function: Channel-wise sum.
Definition: ailayer_dense.h:159
ailayer_dense::optimem
void * optimem[2]
Memory field used by the trainings optimizer.
Definition: ailayer_dense.h:95
ailayer_dense::linear
void(* linear)(const aitensor_t *a, const aitensor_t *b, const aitensor_t *c, aitensor_t *result)
Required math function: Linear transformation.
Definition: ailayer_dense.h:115
ailayer_dense::tensor_add
void(* tensor_add)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result)
Required math function: Element wise tensor addition.
Definition: ailayer_dense.h:150
ailayer_dense::bias
aitensor_t bias
Tensor containing the layer bias weights.
Definition: ailayer_dense.h:88
ailayer_dense::mat_mul_at
void(* mat_mul_at)(const aitensor_t *a, const aitensor_t *b, aitensor_t *result)
Required math function: Matrix multiplication with transposed a.
Definition: ailayer_dense.h:128
ailayer_dense::neurons
uint32_t neurons
Layer neurons count (number of outputs).
Definition: ailayer_dense.h:80
ailayer_dense::base
ailayer_t base
Inherited field members from general ailayer struct.
Definition: ailayer_dense.h:72
ailayer_dense::result_shape
uint16_t result_shape[2]
Inference result tensor (ailayer.result) shape.
Definition: ailayer_dense.h:163
ailayer_dense::weights
aitensor_t weights
Tensor containing the layer weights.
Definition: ailayer_dense.h:87
ailayer_dense::trainable_params
aitensor_t * trainable_params[2]
Pointer to the weights and biases (which are the trainable parameters).
Definition: ailayer_dense.h:93
ailayer
AIfES layer interface.
Definition: aifes_core.h:252
aitensor
A tensor in AIfES.
Definition: aifes_math.h:89