diff --git a/aidge_export_cpp/kernels/pad.hpp b/aidge_export_cpp/kernels/pad.hpp new file mode 100644 index 0000000000000000000000000000000000000000..dc7629fc571ac722ed8d13937b454e96fcd59e21 --- /dev/null +++ b/aidge_export_cpp/kernels/pad.hpp @@ -0,0 +1,42 @@ +#ifndef __AIDGE_EXPORT_CPP_KERNELS_PAD2D__ +#define __AIDGE_EXPORT_CPP_KERNELS_PAD2D__ + +#include "network/typedefs.hpp" +#include "kernels/rescaling.hpp" +#include "network/utils.hpp" +#include "kernels/macs.hpp" +#include "kernels/activation.hpp" + +// Todo add border value and border type (Reflect, Constant, Wrap...) and add the two missing pad value (bottom and right) + +template<int NB_CHANNELS, + int CHANNELS_HEIGHT, int CHANNELS_WIDTH, + int NB_OUTPUTS, + int OUTPUTS_HEIGHT, int OUTPUTS_WIDTH, + int PADDING_Y, int PADDING_X, + typename Input_T, typename Output_T> +__attribute__((always_inline)) inline +void convolution_forward( + const Input_T* __restrict inputs, + Output_T* __restrict outputs + ) +{ + const I *input = static_cast<const I *>(input_); + O *output = static_cast<O *>(output_); + + const std::size_t oySize = CHANNELS_HEIGHT + PADDING_Y + PADDING_Y; + const std::size_t oxSize = CHANNELS_WIDTH + PADDING_X + PADDING_X; + + for (std::uint32_t oy = 0; oy < oySize; ++oy) { + for (std::uint32_t ox = 0; ox < oxSize; ++ox) { + if (oy < PADDING_Y or oy >= CHANNELS_HEIGHT + PADDING_Y or ox < PADDING_X or ox >= CHANNELS_WIDTH + PADDING_X) + { + outputs[oy * oySize + ox] = 0.0f; + } + + outputs[oy * oySize + ox] = input[(oy - PADDING_Y) * CHANNELS_HEIGHT + (ox - PADDING_X)]; + } + } +} + +#endif // __AIDGE_EXPORT_CPP_KERNELS_PAD2D__ diff --git a/aidge_export_cpp/operators.py b/aidge_export_cpp/operators.py index f49c501c9ea3f4bc38a35837d65e36bf8c394b5b..0e3749a3592cce219f9ce8fe6888b42fea587ed1 100644 --- a/aidge_export_cpp/operators.py +++ b/aidge_export_cpp/operators.py @@ -75,7 +75,18 @@ class ProducerCPP(ExportNode): @ExportLibCpp.register("Pad2D", aidge_core.ImplSpec(aidge_core.IOSpec(aidge_core.dtype.any))) class Pad_ARMCortexM(ExportNodeCpp): def __init__(self, node, mem_info): - raise NotImplementedError("Pad2D nodes is not implemented") + super().__init__(node, mem_info) + self.attributes["padding"] = node.get_operator().attr.begin_end_borders + + self.config_template = str( + ROOT / "templates" / "configuration" / "pad_config.jinja") + self.forward_template = str( + ROOT / "templates" / "kernel_forward" / "pad_forward.jinja") + self.include_list = [] + self.kernels_to_copy = [ + str(ROOT / "kernels" / "pad.hpp") + ] + @ExportLibCpp.register("ReLU", aidge_core.ImplSpec(aidge_core.IOSpec(aidge_core.dtype.float32))) diff --git a/aidge_export_cpp/templates/configuration/pad_config.jinja b/aidge_export_cpp/templates/configuration/pad_config.jinja new file mode 100644 index 0000000000000000000000000000000000000000..527e5c08e4e70370616e0794f3508f5c62583124 --- /dev/null +++ b/aidge_export_cpp/templates/configuration/pad_config.jinja @@ -0,0 +1,10 @@ +{#- For name header -#} +#ifndef {{ name|upper }}_LAYER_H +#define {{ name|upper }}_LAYER_H +{# For layer configuration -#} +{% include "./_def_io.jinja" %} +{% include "./_meminfo.jinja" %} +#define {{ name|upper }}_PADDING_Y {{ padding[1] }} +#define {{ name|upper }}_PADDING_X {{ padding[0] }} + +#endif /* {{ name|upper }}_LAYER_H */ diff --git a/aidge_export_cpp/templates/kernel_forward/pad_forward.jinja b/aidge_export_cpp/templates/kernel_forward/pad_forward.jinja new file mode 100644 index 0000000000000000000000000000000000000000..04976e9fbac4a268c02a94f6af6f846f50f783ae --- /dev/null +++ b/aidge_export_cpp/templates/kernel_forward/pad_forward.jinja @@ -0,0 +1,13 @@ +{% filter indent(width=4, first=False) %} +{% include "./_mem_offset.jinja" %} +convolution_forward<{{ in_name[0]|upper }}_NB_CHANNELS, + {{ in_name[0]|upper }}_IN_HEIGHT, + {{ in_name[0]|upper }}_IN_WIDTH, + {{ out_name[0]|upper }}_NB_OUTPUTS, + {{ out_name[0]|upper }}_OUT_HEIGHT, + {{ out_name[0]|upper }}_OUT_WIDTH, + {{name|upper}}_PADDING_Y, + {{name|upper}}_PADDING_X> + ({{in_name[0]}}, {{out_name[0]}}); +{% include "./_save_outputs.jinja" %} +{% endfilter %}