diff --git a/aidge_export_cpp/kernels/reshape.hpp b/aidge_export_cpp/kernels/reshape.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a5828da39553222ef933f0355424e50956ca8490 --- /dev/null +++ b/aidge_export_cpp/kernels/reshape.hpp @@ -0,0 +1,27 @@ +#ifndef __AIDGE_EXPORT_CPP_KERNELS_RESHAPE__ +#define __AIDGE_EXPORT_CPP_KERNELS_RESHAPE__ + +#include "network/typedefs.hpp" + +// Generic function for reshape and activation + +template<int M, + typename Input_T, typename Output_T> +__attribute__((always_inline)) inline +void reshape_forward ( + const Input_T* __restrict, // First input is useless as it only dictate the resulting layout of the reshape + const Input_T* __restrict inputs2, + Output_T* __restrict outputs) +{ + // If inputs and outputs pointers are the same, the memory manager has already optimized this function so it is a no-op ! + if (inputs2 == outputs) + return; + + // A reshape in c++ world should equal to a Noop + // We only need to copy the input buffer to the output + for (int m = 0; m < M; ++m) { + outputs[m] = inputs2[m]; + } +} + +#endif // __AIDGE_EXPORT_CPP_KERNELS_RESHAPE__ \ No newline at end of file diff --git a/aidge_export_cpp/operators.py b/aidge_export_cpp/operators.py index 54c38055b2c622aa1796bc1e7ff2dd46e60afcba..59ce94ad789b8f10c25cfe3ae03a4f865fcc2c78 100644 --- a/aidge_export_cpp/operators.py +++ b/aidge_export_cpp/operators.py @@ -94,6 +94,19 @@ class ReLUCPP(ExportNodeCpp): str(ROOT / "kernels" / "rescaling.hpp") ] +@ExportLibCpp.register("Reshape", aidge_core.ImplSpec(aidge_core.IOSpec(aidge_core.dtype.float32))) +class ReshapeCPP(ExportNodeCpp): + def __init__(self, node, mem_info): + super().__init__(node, mem_info) + self.config_template = str( + ROOT / "templates" / "configuration" / "reshape_config.jinja") + self.forward_template = str( + ROOT / "templates" / "kernel_forward" / "reshape_forward.jinja") + self.include_list = [] + self.kernels_to_copy = [ + str(ROOT / "kernels" / "reshape.hpp"), + ] + @ExportLibCpp.register("Conv2D", aidge_core.ImplSpec(aidge_core.IOSpec(aidge_core.dtype.float32))) class ConvCPP(ExportNodeCpp): def __init__(self, node, mem_info): diff --git a/aidge_export_cpp/templates/configuration/reshape_config.jinja b/aidge_export_cpp/templates/configuration/reshape_config.jinja new file mode 100644 index 0000000000000000000000000000000000000000..041cf8ae898ed561e050354d137c1c676723a81f --- /dev/null +++ b/aidge_export_cpp/templates/configuration/reshape_config.jinja @@ -0,0 +1,8 @@ +{#- For name header -#} +#ifndef {{ name|upper }}_LAYER_H +#define {{ name|upper }}_LAYER_H + +{% include "./_def_io.jinja" %} +{% include "./_meminfo.jinja" %} +{# For layer configuration -#} +#define {{ name|upper }}_NB_ELTS {{ in_dims[0]|join('*') }} diff --git a/aidge_export_cpp/templates/kernel_forward/reshape_forward.jinja b/aidge_export_cpp/templates/kernel_forward/reshape_forward.jinja new file mode 100644 index 0000000000000000000000000000000000000000..f9752bcc85255ba321082fbf5cf599f45b3ab4c4 --- /dev/null +++ b/aidge_export_cpp/templates/kernel_forward/reshape_forward.jinja @@ -0,0 +1,6 @@ +{% filter indent(width=4, first=False) %} +{% include "./_mem_offset.jinja" %} +reshape_forward<{{name|upper}}_NB_ELTS> + ({{in_name[0]}}, {{in_name[1]}}, {{out_name[0]}}); +{% include "./_save_outputs.jinja" %} +{% endfilter %}