Skip to content
Snippets Groups Projects
Commit f774822c authored by Racim Boumbar's avatar Racim Boumbar Committed by Cyril Moineau
Browse files

Fix some operators

parent 716ed4ac
No related branches found
No related tags found
1 merge request!30Fix some operators
Pipeline #74705 passed
Showing
with 166 additions and 35 deletions
#include <math.h>
void aidge_batchnorm2d_chw_float32 (float* inputs,
float* outputs,
float* input_mean,
float* input_var,
float* scale,
float* bias,
float epsilon,
const int nb_channels,
const int channel_width, const int channel_height)
{
int featureMapSize = channel_width * channel_height;
for (int ch = 0; ch < nb_channels; ++ch)
{
int ioIndex = ch * featureMapSize;
for (int i = ioIndex; i < ioIndex + featureMapSize; i++){
outputs[i] = bias[ch];
}
float var =sqrt(input_var[ch] + epsilon);
for (int feature = 0; feature<featureMapSize; ++feature) {
outputs[ioIndex + feature] += scale[ch] * (inputs[ioIndex + feature]-input_mean[ch]) / var;
}
}
}
\ No newline at end of file
template <unsigned int SIZE, typename Input_T, typename Output_T>
__attribute__((always_inline)) inline static
void aidge_div(Input_T* __restrict input_a, Input_T* __restrict input_b, Output_T* __restrict output) {
for (unsigned int i = 0; i < SIZE; ++i) {
// Handle division by zero case
if(input_b[i] != static_cast<Input_T>(0)) {
output[i] = input_a[i] / input_b[i];
} else {
output[i] = static_cast<Output_T>(0); // or some other error handling ?
}
}
}
\ No newline at end of file
void aidge_div_float32(float* input_a,
float* input_b,
float* output,
unsigned int size)
{
for (unsigned int i = 0; i < size; ++i) {
output[i] = input_a[i] / input_b[i];
}
}
\ No newline at end of file
void aidge_matmul_chw_float32(float* input_a, void aidge_matmul_chw_float32(const float* input_a,
float* input_b, const float* input_b,
float* output, float* output,
int dim_a[], const int dim_a[],
int dim_b[], const int dim_b[],
int output_Dim[], const int output_Dim[],
int size_dima, const int size_dima,
int size_dimb, const int size_dimb,
int size_outputDim) const int size_outputDim)
{ {
//initialize arrays storing broadcasted(or not) dims //initialize arrays storing broadcasted(or not) dims
int ndim_a[size_outputDim]; int ndim_a[size_outputDim];
......
#include <cstring>
template <unsigned int SIZE, typename Input_T, typename Output_T>
__attribute__((always_inline)) inline static
void aidge_reshape(Input_T* __restrict input, Output_T* __restrict output, unsigned int size) {
// Copy the input data to the output data
std::memcpy(output, input, size * sizeof(float));
}
\ No newline at end of file
template <unsigned int SIZE, typename Input_T, typename Output_T>
__attribute__((always_inline)) inline static
void aidge_sigmoid(Input_T* __restrict input, Output_T* __restrict output) {
for (unsigned int i = 0; i < SIZE; ++i) {
output[i] = 1 / ( 1 + exp(-input[i]) );
}
}
#include <math.h> #include <math.h>
void aidge_softmax_chw_float32(float* inputs, void aidge_softmax_chw_float32(const float* inputs,
float* outputs, float* outputs,
const int inputDims[], const int inputDims[],
int axis, int axis,
......
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
#define {{ name|upper }}_OUTPUTS_SIZE {{ out_chan[0] * out_height[0] * out_width[0] }} #define {{ name|upper }}_OUTPUTS_SIZE {{out_size[0]}}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
#define {{ name|upper }}_LAYER_H #define {{ name|upper }}_LAYER_H
{# For layer configuration -#} {# For layer configuration -#}
#define {{ name|upper }}_NB_BATCH {{ input_dims[0] }} #define {{ name|upper }}_NB_BATCH {{ in_dims[0] }}
#define {{ name|upper }}_NB_CHANNELS {{ input_dims[1] }} #define {{ name|upper }}_NB_CHANNELS {{ in_dims[1] }}
#define {{ name|upper }}_CHANNELS_HEIGHT {{ input_dims[2] }} #define {{ name|upper }}_CHANNELS_HEIGHT {{ in_dims[2] }}
#define {{ name|upper }}_CHANNELS_WIDTH {{ input_dims[3] }} #define {{ name|upper }}_CHANNELS_WIDTH {{ in_dims[3] }}
#define {{ name|upper }}_EPSILON {{ epsilon }} #define {{ name|upper }}_EPSILON {{ epsilon }}
......
{#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H
/* ElemWise - sub layer */
{% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %}
{# For layer configuration -#}
#define {{ name|upper }}_INPUTS_SIZE {{ in_size[0] }}
#define {{ name|upper }}_OUTPUTS_SIZE {{ out_size[0] }}
#define {{in_name[0]|upper}}_NB_DIM {{ in_dims[0]|length}}
#define {{in_name[1]|upper}}_NB_DIM {{ in_dims[1]|length}}
#define {{out_name[0]|upper}}_NB_DIM {{ out_dims[0]|length}}
static const int {{ in_name[0]|upper }}_DIMS[] = { {{ in_dims[0]|join(", ") }} };
static const int {{ in_name[1]|upper }}_DIMS[] = { {{ in_dims[1]|join(", ") }} };
static const int {{ out_name[0]|upper }}_DIMS[] = { {{ out_dims[0]|join(", ") }} };
#endif /* {{ name|upper }}_LAYER_H */
...@@ -2,12 +2,14 @@ ...@@ -2,12 +2,14 @@
#ifndef {{ name|upper }}_LAYER_H #ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H #define {{ name|upper }}_LAYER_H
{# For layer configuration -#} {% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %}
#define {{ in_name[0]|upper }}_DIMS {{ in_dims[0] }} {# For layer configuration -#}
#define {{ in_name[1]|upper }}_DIMS {{ in_dims[1] }} static const int {{ in_name[0]|upper }}_DIMS[] = { {{ in_dims[0]|join(", ") }} };
#define {{ out_name[0]|upper }}_DIMS {{ out_dims[0] }} static const int {{ in_name[1]|upper}}_DIMS[] = { {{ in_dims[0]|join(", ") }} };
static const int {{ out_name[0]|upper }}_DIMS[] = { {{ out_dims[0]|join(", ") }} };
#define {{name|upper}}_INPUT_A_DIMS_SIZE {{ in_dims[0]|length}} #define {{name|upper}}_INPUT_A_DIMS_SIZE {{ in_dims[0]|length}}
#define {{name|upper}}_INPUT_B_DIMS_SIZE {{ in_dims[1]|length}} #define {{name|upper}}_INPUT_B_DIMS_SIZE {{ in_dims[1]|length}}
......
{#- For name header -#} {#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H #ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H #define {{ name|upper }}_LAYER_H
{% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %}
{# For layer configuration -#} {# For layer configuration -#}
#define {{ name|upper }}_INPUTS_SIZE {{ nb_inputs }} #define {{ name|upper }}_INPUTS_SIZE {{ nb_inputs }}
#define {{ name|upper }}_OUTPUTS_SIZE {{ nb_outputs }} #define {{ name|upper }}_OUTPUTS_SIZE {{ nb_outputs }}
......
{#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H
/* ElemWise - sub layer */
{% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %}
{# For layer configuration -#}
#define {{ name|upper }}_INPUTS_SIZE {{ in_size[0] }}
#define {{ name|upper }}_OUTPUTS_SIZE {{ out_size[0] }}
#define {{in_name[0]|upper}}_NB_DIM {{ in_dims[0]|length}}
#define {{out_name[0]|upper}}_NB_DIM {{ out_dims[0]|length}}
static const int {{ in_name[0]|upper }}_DIMS[] = { {{ in_dims[0]|join(", ") }} };
static const int {{ out_name[0]|upper }}_DIMS[] = { {{ out_dims[0]|join(", ") }} };
#endif /* {{ name|upper }}_LAYER_H */
{% include "./_def_io.jinja" %} {% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %}
{#- For name header -#} {#- For name header -#}
#ifndef {{ name|upper }}_LAYER_H #ifndef {{ name|upper }}_LAYER_H
#define {{ name|upper }}_LAYER_H #define {{ name|upper }}_LAYER_H
......
aidge_batchnorm2d_chw_{{dataformat}} ({{input_name}}, {{output_name}}, {{running_mean_name}}, {{running_var_name}}, {{weight_name}}, {{bias_name}}, {{ name|upper }}_EPSILON, {{ name|upper }}_NB_CHANNELS, {{ name|upper }}_CHANNELS_WIDTH, {{ name|upper }}_CHANNELS_HEIGHT); aidge_batchnorm2d_chw_{{dataformat|default("float32") }}
({{in_name}},
{{out_name}},
{{running_mean_name}},
{{running_var_name}},
{{weight_name}},
{{bias_name}},
{{ name|upper }}_EPSILON,
{{ name|upper }}_NB_CHANNELS,
{{ name|upper }}_CHANNELS_WIDTH,
{{ name|upper }}_CHANNELS_HEIGHT);
{% filter indent(width=4, first=False) %}
{% include "./_mem_offset.jinja" %}
aidge_div <{{name|upper}}_OUTPUTS_SIZE>
({{in_name[0]}},
{{in_name[1]}},
{{out_name[0]}});
{% endfilter %}
aidge_matmul_chw_{{dataformat}} ({{in_name[0]}}, {{in_name[1]}}, {{out_name[0]}}, {{in_name[0]}}_DIMS, {{in_name[1]}}_DIMS, {{out_name[0]}}_DIMS, {{name|upper}}_INPUT_A_DIMS_SIZE, {{name|upper}}_INPUT_B_DIMS_SIZE, {{name|upper}}_OUTPUT_DIMS_SIZE); {% filter indent(width=4, first=False) %}
{% include "./_mem_offset.jinja" %}
aidge_matmul_chw_{{dataformat| default("float32") }}
({{in_name[0]}},
{{in_name[1]}},
{{out_name[0]}},
{{in_name[0]|upper}}_DIMS,
{{in_name[1]|upper}}_DIMS,
{{out_name[0]|upper}}_DIMS,
{{name|upper}}_INPUT_A_DIMS_SIZE,
{{name|upper}}_INPUT_B_DIMS_SIZE,
{{name|upper}}_OUTPUT_DIMS_SIZE);
{% endfilter %}
\ No newline at end of file
aidge_reshape_chw_{{dataformat}}({{input_name}}, {{output_name}}, {{name|upper}}_OUTPUTS_SIZE); {% filter indent(width=4, first=False) %}
\ No newline at end of file {% include "./_mem_offset.jinja" %}
aidge_reshape<{{name|upper}}_OUTPUTS_SIZE>
({{input_name}}, {{output_name}}, {{name|upper}}_OUTPUTS_SIZE);
{% endfilter %}
\ No newline at end of file
{% filter indent(width=4, first=False) %}
{% include "./_mem_offset.jinja" %}
aidge_sigmoid<{{name|upper}}_OUTPUTS_SIZE>
({{in_name[0]}},
{{out_name[0]}});
{% endfilter %}
{% filter indent(width=4, first=False) %} {% filter indent(width=4, first=False) %}
{% include "./_mem_offset.jinja" %} {% include "./_mem_offset.jinja" %}
aidge_sub_float32({{in_name[0]}}, {{in_name[1]}}, {{out_name[0]}}, {{name|upper}}_OUTPUTS_SIZE); aidge_sub<{{name|upper}}_OUTPUTS_SIZE>
({{in_name[0]}},
{{in_name[1]}},
{{out_name[0]}});
{% endfilter %} {% endfilter %}
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