Skip to content
Snippets Groups Projects
Commit 467eb8ea authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Merge branch 'dev' into 'main'

0.0.1

See merge request eclipse/aidge/aidge_export_arm_cortexm!4
parents 697e692b 9a9b23d9
Branches main
Tags v0.0.1
No related merge requests found
Showing
with 268 additions and 5 deletions
void aidge_relu_float32 (float* inputs,
float* outputs,
unsigned int size)
{
for (unsigned int i = 0; i < size; ++i) {
if (inputs[i] < 0.0f)
outputs[i] = 0.0f;
outputs[i] = (inputs[i] < 0.0f) ? 0.0f : inputs[i];
}
}
\ No newline at end of file
#include "include/aidge_supportfunctions.h"
void aidge_conv2d_hwc_float32(float* inputs,
float* weights,
float* biases,
float* outputs,
const int nb_channels,
const int channel_width, const int channel_height,
const int kernel_width, const int kernel_height,
const int nb_outputs,
const int output_width, const int output_height,
const int padding_width, const int padding_height,
const int stride_width, const int stride_height,
const int dilation_width, const int dilation_height)
{
int outputOffset = 0;
const int dilated_kernel_width
= kernel_width + (dilation_width - 1) * (kernel_width - 1);
const int dilated_kernel_height
= kernel_height + (dilation_height - 1) * (kernel_height - 1);
int iy = 0;
for (int oy = 0; oy < output_height; ++oy) {
const int syMin = (padding_height == 0)
? 0 : max(padding_height - iy, 0);
const int syMax = (padding_height == 0)
? dilated_kernel_height
: clamp(channel_height + padding_height - iy,
0, dilated_kernel_height);
int ix = 0;
for (int ox = 0; ox < output_width; ++ox) {
const int sxMin = (padding_width == 0)
? 0 : max(padding_width - ix, 0);
const int sxMax = (padding_width == 0)
? dilated_kernel_width
: clamp(channel_width + padding_width - ix,
0, dilated_kernel_width);
for (int och = 0; och < nb_outputs; ++och) {
float weightedSum = biases[och];
for (int sy = 0; sy < kernel_height; ++sy) {
if (padding_height != 0 && (sy*dilation_height < syMin || sy*dilation_height >= syMax)) {
continue;
}
const int inputsOffset = (iy + sy*dilation_height - padding_height)*channel_width*nb_channels +
(ix - padding_width)*nb_channels;
const int weightsOffset = och*kernel_height*kernel_width*nb_channels +
sy*kernel_width*nb_channels;
for (int sx = 0; sx < kernel_width; ++sx) {
if(sx*dilation_width < sxMin || sx*dilation_width >= sxMax) {
continue;
}
for (int ch = 0; ch < nb_channels; ++ch) {
weightedSum += inputs[inputsOffset + sx*dilation_width*nb_channels + ch]
* weights[weightsOffset + sx*nb_channels + ch];
}
}
}
outputs[outputOffset] = weightedSum;
++outputOffset;
}
ix += stride_width;
}
iy += stride_height;
}
}
void aidge_fc_chw_float32 (float* inputs,
float* weights,
float* biases,
float* outputs,
unsigned int nb_channels,
unsigned int channels_height,
unsigned int channels_width,
unsigned int nb_outputs)
{
for (unsigned int out = 0; out < nb_outputs; ++out) {
// Init with bias
float accum = biases[out];
for (int iy = 0; iy < channels_height; ++iy) {
for (int ix = 0; ix < channels_width; ++ix) {
for (int ch = 0; ch < nb_channels; ++ch) {
accum += inputs[channels_width*nb_channels*iy + nb_channels*ix + ch]
* weights[channels_height*channels_width*nb_channels*out + channels_height*channels_width*ch + channels_height*iy + ix];
}
}
}
// Store result
outputs[out] = accum;
}
}
#include "include/aidge_supportfunctions.h"
void aidge_maxpool2d_float32(float* inputs,
float* outputs,
const int nb_channels,
const int channel_width, const int channel_height,
const int kernel_width, const int kernel_height,
const int nb_outputs,
const int output_width, const int output_height,
const int padding_width, const int padding_height,
const int stride_width, const int stride_height)
{
const int OUTPUTS_HEIGHT_NOPAD
= (channel_height - kernel_height + stride_height) / stride_height;
const int OUTPUTS_WIDTH_NOPAD
= (channel_width - kernel_width + stride_width) / stride_width;
for (int oy = 0; oy < output_height; ++oy) {
const int syMin = (padding_height == 0) ? 0
: max(padding_height - (oy * stride_height), 0);
const int syMax = (padding_height == 0
&& output_height == OUTPUTS_HEIGHT_NOPAD) ? kernel_height
: clamp(channel_height + padding_height - (oy * stride_height),
0, kernel_height);
const int iy = (oy * stride_height) - padding_height;
for (int ox = 0; ox < output_width; ++ox) {
for (int output = 0; output < nb_outputs; ++output) {
const int sxMin = (padding_width == 0) ? 0
: max(padding_width - (ox * stride_width), 0);
const int sxMax = (padding_width == 0
&& output_width == OUTPUTS_WIDTH_NOPAD)
? kernel_width
: clamp(channel_width + padding_width - (ox * stride_width),
0, kernel_width);
const int ix = (ox * stride_width) - padding_width;
const int oPos = (ox + output_width * oy);
int oOffset = nb_outputs * oPos;
float maxVal = -1000.f;
for (int sy = 0; sy < kernel_height; ++sy) {
if ((padding_height != 0
|| output_height != OUTPUTS_HEIGHT_NOPAD)
&& sy >= syMax - syMin)
{
break;
}
const int iPos = ((sxMin + ix)
+ channel_width * (iy + syMin + sy));
int iOffset = nb_channels * iPos;
for (int sx = 0; sx < kernel_width; ++sx) {
if ((padding_width != 0
|| output_width != OUTPUTS_WIDTH_NOPAD)
&& sx >= sxMax - sxMin)
{
break;
}
int iOffsetInRange = iOffset + output + sx * nb_channels;
if (inputs[iOffsetInRange] > maxVal)
maxVal = inputs[iOffsetInRange];
}
}
outputs[oOffset + output] = maxVal;
}
}
}
}
#ifndef __AIDGE_SUPPORTFUNCTIONS_H__
#define __AIDGE_SUPPORTFUNCTIONS_H__
/**
* @brief Integer clamping
* @param[in] v Value to be clamped
* @param[in] lo Saturating lower bound
* @param[in] hi Saturating higher bound
* @returns Value clamped between lo and hi
*
*/
static inline int clamp (int v, int lo, int hi)
{
if(v < lo) {
return lo;
}
else if(v > hi) {
return hi;
}
else {
return v;
}
}
/**
* @brief Maximum of two integer values
*/
static inline int max (int lhs, int rhs)
{
return (lhs >= rhs) ? lhs : rhs;
}
/**
* @brief Minimum of two integer values
*/
static inline int min (int lhs, int rhs)
{
return (lhs <= rhs) ? lhs : rhs;
}
#endif // __AIDGE_SUPPORTFUNCTIONS_H__
{#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H
{# For layer configuration -#}
#define {{ name|upper }}_NB_CHANNELS {{ input_dims[0] }}
#define {{ name|upper }}_CHANNELS_HEIGHT {{ input_dims[1] }}
#define {{ name|upper }}_CHANNELS_WIDTH {{ input_dims[2] }}
#define {{ name|upper }}_NB_OUTPUTS {{ output_dims[0] }}
#define {{ name|upper }}_OUTPUTS_HEIGHT {{ output_dims[1] }}
#define {{ name|upper }}_OUTPUTS_WIDTH {{ output_dims[2] }}
#define {{ name|upper }}_KERNEL_Y {{ kernel[1] }}
#define {{ name|upper }}_KERNEL_X {{ kernel[0] }}
#define {{ name|upper }}_PADDING_Y {{ padding[1] }}
#define {{ name|upper }}_PADDING_X {{ padding[0] }}
#define {{ name|upper }}_STRIDE_Y {{ stride[1] }}
#define {{ name|upper }}_STRIDE_X {{ stride[0] }}
#define {{ name|upper }}_DILATION_Y {{ dilation[1] }}
#define {{ name|upper }}_DILATION_X {{ dilation[0] }}
{#- Calculate sizes #}
{%- set weights_size = output_dims[0] * input_dims[0] * kernel[1] * kernel[0] %}
#define {{ name|upper }}_WEIGHTS_SIZE {{ weights_size }}
#define {{ name|upper }}_BIASES_SIZE {{ output_dims[0] }}
#endif /* {{ name|upper }}_LAYER_H */
......@@ -4,12 +4,14 @@
{# For layer configuration -#}
#define {{ name|upper }}_NB_CHANNELS {{ nb_channels }}
#define {{ name|upper }}_CHANNEL_HEIGHT {{ channel_height }}
#define {{ name|upper }}_CHANNEL_WIDTH {{ channel_width }}
#define {{ name|upper }}_NB_OUTPUTS {{ nb_outputs }}
{#- Calculate sizes #}
{%- set weights_size = nb_channels * nb_outputs %}
{%- set weights_size = nb_channels * channel_height * channel_width * nb_outputs %}
#define {{ name|upper }}_WEIGHTS_SIZE {{ weights_size }}
#define {{ name|upper }}_BIASES_SIZE {{ biases_size }}
#define {{ name|upper }}_BIASES_SIZE {{ nb_outputs }}
#endif /* {{ name|upper }}_LAYER_H */
{#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H
{# For layer configuration -#}
#define {{ name|upper }}_NB_CHANNELS {{ input_dims[0] }}
#define {{ name|upper }}_CHANNELS_HEIGHT {{ input_dims[1] }}
#define {{ name|upper }}_CHANNELS_WIDTH {{ input_dims[2] }}
#define {{ name|upper }}_NB_OUTPUTS {{ output_dims[0] }}
#define {{ name|upper }}_OUTPUTS_HEIGHT {{ output_dims[1] }}
#define {{ name|upper }}_OUTPUTS_WIDTH {{ output_dims[2] }}
#define {{ name|upper }}_PADDING_Y {{ padding[1] }}
#define {{ name|upper }}_PADDING_X {{ padding[0] }}
#define {{ name|upper }}_STRIDE_Y {{ stride[1] }}
#define {{ name|upper }}_STRIDE_X {{ stride[0] }}
#define {{ name|upper }}_KERNEL_Y {{ kernel[1] }}
#define {{ name|upper }}_KERNEL_X {{ kernel[0] }}
#define {{ name|upper }}_POOLING_TYPE {{ pool_type }}
#endif /* {{ name|upper }}_LAYER_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment