| AIfES 2 2.0.0

ailayer_batch_normalization.h
Go to the documentation of this file.
1
61 #ifndef AILAYER_BATCH_NORM
62 #define AILAYER_BATCH_NORM
63
64 #define AIFES_CHANNELS_FIRST 1
65 #define AIFES_CHANNELS_LAST -1
66
67 #include "core/aifes_core.h"
68 #include "basic/default/aimath/aimath_f32_default.h"
69
70 typedef struct ailayer_batch_norm ailayer_batch_norm_t;
71
75 struct ailayer_batch_norm {
76 ailayer_t base;
84 int8_t channel_axis;
85 void *momentum;
86 void *eps;
88
93 aitensor_t betas;
94 aitensor_t gammas;
96 aitensor_t moving_means;
97 aitensor_t moving_variances;
99 aitensor_t *trainable_params[2];
100 aitensor_t *gradients[2];
101 void *optimem[2];
103
109 uint16_t parameter_shape[1];
111 aitensor_t *means;
112 aitensor_t *variances;
114
119
131 void (*empirical_mean_channelwise)(const aitensor_t *x, int8_t channel_axis, aitensor_t *means);
132
145 void (*empirical_variance_channelwise)(const aitensor_t *x, int8_t channel_axis, const aitensor_t *means, aitensor_t *variances);
146
158 void (*exponential_moving_average)(const aitensor_t *new_data, const void *momentum, aitensor_t *average);
159
176 void (*batch_norm)(const aitensor_t *x,
177 int8_t channel_axis,
178 const aitensor_t *means,
179 const aitensor_t *variances,
180 const aitensor_t *offsets,
181 const aitensor_t *scales,
182 const void *eps,
183 aitensor_t *result);
184
204 void (*d_batch_norm)(const aitensor_t *x_in,
205 int8_t axis,
206 const aitensor_t *means,
207 const aitensor_t *vars,
208 const aitensor_t *betas,
209 const aitensor_t *gammas,
210 const aitensor_t *delta_out,
211 const void *eps,
212 aitensor_t *delta_in,
213 aitensor_t *d_betas,
214 aitensor_t *d_gammas);
215
217 };
218
224 extern const aicore_layertype_t *ailayer_batch_norm_type;
225
237 ailayer_t *ailayer_batch_norm(ailayer_batch_norm_t *layer, ailayer_t *input_layer);
238
273 void ailayer_batch_norm_forward(ailayer_t *self);
274
292 void ailayer_batch_norm_backward(ailayer_t *self);
293
302 void ailayer_batch_norm_calc_result_shape(ailayer_t *self);
303
304
316 uint32_t ailayer_batch_norm_sizeof_paramem(const ailayer_t *self);
317
329 void ailayer_batch_norm_set_paramem(ailayer_t *self, void *memory_ptr);
330
342 uint32_t ailayer_batch_norm_sizeof_trainmem(const ailayer_t *self);
343
353 void ailayer_batch_norm_set_trainmem(ailayer_t *self, void *memory_ptr);
354
355 #ifdef AIDEBUG_PRINT_MODULE_SPECS
360 void ailayer_batch_norm_print_specs(const ailayer_t *self);
361 #endif // AIDEBUG_PRINT_MODULE_SPECS
362
363 #endif // AILAYER_BATCH_NORM
aifes_core.h
AIfES 2 core interface.
ailayer_batch_norm_backward
void ailayer_batch_norm_backward(ailayer_t *self)
Calculate the backward pass for the given Batch Normalization layer.
ailayer_batch_norm_sizeof_trainmem
uint32_t ailayer_batch_norm_sizeof_trainmem(const ailayer_t *self)
Calculate and return the memory size needed by this layer for training.
ailayer_batch_norm_forward
void ailayer_batch_norm_forward(ailayer_t *self)
Calculate the forward pass for given Batch Normalization layer.
ailayer_batch_norm
ailayer_t * ailayer_batch_norm(ailayer_batch_norm_t *layer, ailayer_t *input_layer)
Initialize and connect the given Batch Normalization layer.
ailayer_batch_norm_set_trainmem
void ailayer_batch_norm_set_trainmem(ailayer_t *self, void *memory_ptr)
Distribute provided memory to the gradients pointers.
ailayer_batch_norm_print_specs
void ailayer_batch_norm_print_specs(const ailayer_t *self)
Print the layer specification.
ailayer_batch_norm_sizeof_paramem
uint32_t ailayer_batch_norm_sizeof_paramem(const ailayer_t *self)
Calculate and return the parameter memory size needed for this layer.
ailayer_batch_norm_set_paramem
void ailayer_batch_norm_set_paramem(ailayer_t *self, void *memory_ptr)
Distribute provided memory to the parameter pointers.
ailayer_batch_norm_calc_result_shape
void ailayer_batch_norm_calc_result_shape(ailayer_t *self)
Calculate the shape of the result tensor (ailayer.result)
ailayer_batch_norm_type
const aicore_layertype_t * ailayer_batch_norm_type
Batch Normalization layer type.
aimath_f32_default.h
Math functions for F32 data type, default implementation.
aicore_layertype
Type indicator of the layer.
Definition: aifes_core.h:82
ailayer_batch_norm
General Batch Normalization layer structure.
Definition: ailayer_batch_normalization.h:75
ailayer_batch_norm::empirical_variance_channelwise
void(* empirical_variance_channelwise)(const aitensor_t *x, int8_t channel_axis, const aitensor_t *means, aitensor_t *variances)
Required math function: Channel-wise empirical variance calculation.
Definition: ailayer_batch_normalization.h:145
ailayer_batch_norm::parameter_shape
uint16_t parameter_shape[1]
Shape of the parameter vectors ( ).
Definition: ailayer_batch_normalization.h:109
ailayer_batch_norm::gradients
aitensor_t * gradients[2]
Gradients of and for the back propagation algorithm.
Definition: ailayer_batch_normalization.h:100
ailayer_batch_norm::d_batch_norm
void(* d_batch_norm)(const aitensor_t *x_in, int8_t axis, const aitensor_t *means, const aitensor_t *vars, const aitensor_t *betas, const aitensor_t *gammas, const aitensor_t *delta_out, const void *eps, aitensor_t *delta_in, aitensor_t *d_betas, aitensor_t *d_gammas)
Required math function: Gradients of Batch Normalization.
Definition: ailayer_batch_normalization.h:204
ailayer_batch_norm::momentum
void * momentum
Momentum for the exponential moving average of means and variances.
Definition: ailayer_batch_normalization.h:85
ailayer_batch_norm::variances
aitensor_t * variances
Vector of the variances ( ).
Definition: ailayer_batch_normalization.h:112
ailayer_batch_norm::optimem
void * optimem[2]
Memory field used by the trainings optimizer.
Definition: ailayer_batch_normalization.h:101
ailayer_batch_norm::batch_norm
void(* batch_norm)(const aitensor_t *x, int8_t channel_axis, const aitensor_t *means, const aitensor_t *variances, const aitensor_t *offsets, const aitensor_t *scales, const void *eps, aitensor_t *result)
Required math function: Batch Normalization.
Definition: ailayer_batch_normalization.h:176
ailayer_batch_norm::eps
void * eps
Small constant for numeric stability.
Definition: ailayer_batch_normalization.h:86
ailayer_batch_norm::empirical_mean_channelwise
void(* empirical_mean_channelwise)(const aitensor_t *x, int8_t channel_axis, aitensor_t *means)
Required math function: Channel-wise empirical mean calculation.
Definition: ailayer_batch_normalization.h:131
ailayer_batch_norm::gammas
aitensor_t gammas
Vector of the scale parameters ( ).
Definition: ailayer_batch_normalization.h:94
ailayer_batch_norm::betas
aitensor_t betas
Vector of the shift parameters ( ).
Definition: ailayer_batch_normalization.h:93
ailayer_batch_norm::means
aitensor_t * means
Vector of the means ( ).
Definition: ailayer_batch_normalization.h:111
ailayer_batch_norm::base
ailayer_t base
Inherited field members from general ailayer struct.
Definition: ailayer_batch_normalization.h:76
ailayer_batch_norm::moving_variances
aitensor_t moving_variances
Vector of the moving averages of the variance (required for inference).
Definition: ailayer_batch_normalization.h:97
ailayer_batch_norm::exponential_moving_average
void(* exponential_moving_average)(const aitensor_t *new_data, const void *momentum, aitensor_t *average)
Required math function: Exponential moving average.
Definition: ailayer_batch_normalization.h:158
ailayer_batch_norm::moving_means
aitensor_t moving_means
Vector of the moving averages of the means (required for inference).
Definition: ailayer_batch_normalization.h:96
ailayer_batch_norm::trainable_params
aitensor_t * trainable_params[2]
Pointer to and (which are the trainable parameters).
Definition: ailayer_batch_normalization.h:99
ailayer_batch_norm::channel_axis
int8_t channel_axis
Index of the channel axis (1 for channels first, -1 for channels last)
Definition: ailayer_batch_normalization.h:84
ailayer
AIfES layer interface.
Definition: aifes_core.h:252
aitensor
A tensor in AIfES.
Definition: aifes_math.h:89