Skip to content
Snippets Groups Projects
Commit 2e78a155 authored by Axel Farrugia's avatar Axel Farrugia
Browse files

[Refactor] save_outputs function now adapt to the actual format of the tensor...

[Refactor] save_outputs function now adapt to the actual format of the tensor (it was previously fixed to NHWC)
Also the result is more readable.
parent f31d0f36
No related branches found
No related tags found
2 merge requests!710.4.0,!58Fix the aidge_cmp feature
Showing
with 88 additions and 78 deletions
...@@ -66,9 +66,10 @@ static inline OutputIt copy_n(InputIt first, Size count, OutputIt result) { ...@@ -66,9 +66,10 @@ static inline OutputIt copy_n(InputIt first, Size count, OutputIt result) {
return result; return result;
} }
#if SAVE_OUTPUTS #if SAVE_OUTPUTS || AIDGE_CMP
enum class Format { enum class Format {
Default, DEFAULT,
NCHW, NCHW,
NHWC, NHWC,
CHWN, CHWN,
...@@ -77,79 +78,67 @@ enum class Format { ...@@ -77,79 +78,67 @@ enum class Format {
CDHWN CDHWN
}; };
#endif // SAVE_OUTPUTS || AIDGE_CMP
#if SAVE_OUTPUTS
template<int NB_OUTPUTS, int OUT_HEIGHT, int OUT_WIDTH, Format FMT, typename Output_T>
inline void saveOutputs(const Output_T* __restrict outputs, FILE* pFile) {
auto offset = 0;
// NCHW
if (FMT == Format::NCHW || FMT == Format::DEFAULT) {
fprintf(pFile, "{");
for (auto out = 0; out < NB_OUTPUTS; ++out) {
fprintf(pFile, "{");
for (auto h = 0; h < OUT_HEIGHT; ++h) {
fprintf(pFile, "{");
for (auto w = 0; w < OUT_WIDTH; ++w) {
template<typename Output_T>
inline void saveOutputs(
int NB_OUTPUTS,
int OUTPUTS_HEIGHT, int OUTPUTS_WIDTH,
// int OUTPUT_MEM_CONT_OFFSET,
// int OUTPUT_MEM_CONT_SIZE,
// int OUTPUT_MEM_WRAP_OFFSET,
// int OUTPUT_MEM_WRAP_SIZE,
// int OUTPUT_MEM_STRIDE,
const Output_T* __restrict outputs,
FILE* pFile,
Format format)
{
// default is NHCW !
if (format == Format::NHWC) {
fprintf(pFile, "(");
auto oOffset = 0;
for(int oy = 0; oy < OUTPUTS_HEIGHT; oy++) {
fprintf(pFile, "(");
for(int ox = 0; ox < OUTPUTS_WIDTH; ox++) {
fprintf(pFile, "(");
// const int oPos = (ox + OUTPUTS_WIDTH * oy);
// int oOffset = OUTPUT_MEM_STRIDE * oPos;
// if (OUTPUT_MEM_WRAP_SIZE > 0
// && oOffset >= OUTPUT_MEM_CONT_SIZE)
// {
// oOffset += OUTPUT_MEM_WRAP_OFFSET - OUTPUT_MEM_CONT_OFFSET
// - OUTPUT_MEM_CONT_SIZE;
// }
for (int output = 0; output < NB_OUTPUTS; output++) {
if (std::is_floating_point<Output_T>::value) if (std::is_floating_point<Output_T>::value)
fprintf(pFile, "%f", static_cast<float>(outputs[oOffset])); fprintf(pFile, "%f", static_cast<float>(outputs[offset]));
else else
fprintf(pFile, "%d", static_cast<int>(outputs[oOffset])); fprintf(pFile, "%d", static_cast<int>(outputs[offset]));
oOffset += 1; offset += 1;
fprintf(pFile, ", "); fprintf(pFile, ", ");
}
fprintf(pFile, "), \n"); }
fprintf(pFile, "}\n");
} }
fprintf(pFile, "}\n");
fprintf(pFile, "), \n");
} }
fprintf(pFile, "}\n");
fprintf(pFile, ")\n"); // NHWC
} } else if (FMT == Format::NHWC) {
else if (format == Format::NCHW || format == Format::Default) { fprintf(pFile, "{\n"); // Start outer brace
auto ofst = 0; for (auto h = 0; h < OUT_HEIGHT; ++h) {
for(int output = 0; output < NB_OUTPUTS; output++) { fprintf(pFile, " {\n"); // Indent level 1
fprintf(pFile, "%d:\n", output); for (auto w = 0; w < OUT_WIDTH; ++w) {
for(int oy = 0; oy < OUTPUTS_HEIGHT; oy++) { fprintf(pFile, " { "); // Indent level 2 and open inner brace
for(int ox = 0; ox < OUTPUTS_WIDTH; ox++) { for (auto out = 0; out < NB_OUTPUTS; ++out) {
fprintf(pFile, "%d", static_cast<int>(outputs[ofst])); if (std::is_floating_point<Output_T>::value)
fprintf(pFile, " "); fprintf(pFile, "%f", static_cast<float>(outputs[offset]));
ofst += 1; else
} fprintf(pFile, "%4d", static_cast<int>(outputs[offset]));
offset += 1;
fprintf(pFile, "\n"); // Add comma except for last element
if (out != NB_OUTPUTS - 1)
fprintf(pFile, ",");
}
fprintf(pFile, " },\n"); // Close inner brace and newline
} }
fprintf(pFile, " },\n"); // Close w-loop brace and newline
fprintf(pFile, "\n");
} }
fprintf(pFile, "}\n"); // Close outer brace
fprintf(pFile, "\n"); } else {
} printf("[ERROR] - Format is not supported.\n");
else { printf("[ERROR] - Aborting save outputs...\n");
printf("Warning unsupported dataformat.\n"); return;
} }
} }
#endif // SAVE_OUTPUTS #endif // SAVE_OUTPUTS
......
#define {{ out_name[0] | upper }}_DEV_FMT Format::{{ out_format[0] | upper }}
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
{%- set nb_data = in_chan[0] * in_height[0] * in_width[0] %} {%- set nb_data = in_chan[0] * in_height[0] * in_width[0] %}
#define {{ name|upper }}_NB_DATA {{ nb_data }} #define {{ name|upper }}_NB_DATA {{ nb_data }}
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
{% include "./_save_outputs.jinja" %}
{% include "./_def_io.jinja" %} {% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
#define {{ name|upper }}_EPSILON {{ epsilon }} #define {{ name|upper }}_EPSILON {{ epsilon }}
{% include "./_save_outputs.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -15,4 +15,6 @@ ...@@ -15,4 +15,6 @@
#define {{ name|upper }}_AXIS_SIZE_POST {{ axis_size_post }} #define {{ name|upper }}_AXIS_SIZE_POST {{ axis_size_post }}
#define {{ name|upper }}_AXIS_SIZE_PRE {{ axis_size_pre }} #define {{ name|upper }}_AXIS_SIZE_PRE {{ axis_size_pre }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#define {{ name|upper }}_KERNEL_HEIGHT {{ kernel_dims[0] if kernel_dims|length > 1 else 1 }} #define {{ name|upper }}_KERNEL_HEIGHT {{ kernel_dims[0] if kernel_dims|length > 1 else 1 }}
#define {{ name|upper }}_KERNEL_WIDTH {{ kernel_dims[1] if kernel_dims|length > 1 else kernel_dims[0] }} #define {{ name|upper }}_KERNEL_WIDTH {{ kernel_dims[1] if kernel_dims|length > 1 else kernel_dims[0] }}
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
{% include "./_save_outputs.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
{#- Calculate sizes #} {#- Calculate sizes #}
......
...@@ -16,6 +16,8 @@ constexpr int {{name|upper}}_OFFSET_IN2[] = { {{ offset_in2|join(", ") }} }; ...@@ -16,6 +16,8 @@ constexpr int {{name|upper}}_OFFSET_IN2[] = { {{ offset_in2|join(", ") }} };
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
#define {{ name|upper }}_ELEM_OP {{ elemwise_op }} #define {{ name|upper }}_ELEM_OP {{ elemwise_op }}
{% include "./_save_outputs.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
{% include "./_def_io.jinja" %} {% include "./_def_io.jinja" %}
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
#define {{ name|upper }}_NB_ELTS {{ in_dims[0]|join('*') }} #define {{ name|upper }}_NB_ELTS {{ in_dims[0]|join('*') }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
\ No newline at end of file
...@@ -13,4 +13,6 @@ ...@@ -13,4 +13,6 @@
#define {{ name|upper }}_WEIGHTS_SIZE {{ weights_size }} #define {{ name|upper }}_WEIGHTS_SIZE {{ weights_size }}
#define {{ name|upper }}_BIASES_SIZE {{ out_chan[0] }} #define {{ name|upper }}_BIASES_SIZE {{ out_chan[0] }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -10,5 +10,6 @@ ...@@ -10,5 +10,6 @@
#define {{ name|upper }}_AXIS_STRIDE {{ axis_stride }} #define {{ name|upper }}_AXIS_STRIDE {{ axis_stride }}
#define {{ name|upper }}_POSTAXIS_STRIDE {{ postaxis_stride }} #define {{ name|upper }}_POSTAXIS_STRIDE {{ postaxis_stride }}
#define {{ name|upper }}_INOUT_NB_ELTS {{ out_nb_elts }} #define {{ name|upper }}_INOUT_NB_ELTS {{ out_nb_elts }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -7,5 +7,6 @@ ...@@ -7,5 +7,6 @@
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
#define {{ name|upper }}_NB_DATA {{ nb_data }} #define {{ name|upper }}_NB_DATA {{ nb_data }}
#define {{ name|upper }}_ALPHA {{ alpha }} #define {{ name|upper }}_ALPHA {{ alpha }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -16,6 +16,8 @@ constexpr int {{name|upper}}_OFFSET_IN2[] = { {{ offset_in2|join(", ") }} }; ...@@ -16,6 +16,8 @@ constexpr int {{name|upper}}_OFFSET_IN2[] = { {{ offset_in2|join(", ") }} };
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
{% include "./_save_outputs.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
{#- Calculate sizes #} {#- Calculate sizes #}
......
...@@ -9,5 +9,6 @@ ...@@ -9,5 +9,6 @@
#define {{ name|upper }}_PADDING_TOP {{ padding[0] }} #define {{ name|upper }}_PADDING_TOP {{ padding[0] }}
#define {{ name|upper }}_PADDING_LEFT {{ padding[1] }} #define {{ name|upper }}_PADDING_LEFT {{ padding[1] }}
#define {{ name|upper }}_BORDER_VALUE {{ border_value }} #define {{ name|upper }}_BORDER_VALUE {{ border_value }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -13,5 +13,6 @@ ...@@ -13,5 +13,6 @@
#define {{ name|upper }}_KERNEL_WIDTH {{ kernel_dims[1] }} #define {{ name|upper }}_KERNEL_WIDTH {{ kernel_dims[1] }}
#define {{ name|upper }}_POOLING_TYPE {{ pool_type }} #define {{ name|upper }}_POOLING_TYPE {{ pool_type }}
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -18,5 +18,7 @@ static const unsigned int {{ name|upper }}_PREAXIS_STRIDES[{{ in_nb_dims }}] = ...@@ -18,5 +18,7 @@ static const unsigned int {{ name|upper }}_PREAXIS_STRIDES[{{ in_nb_dims }}] =
static const unsigned int {{ name|upper }}_POSTAXIS_STRIDES[{{ in_nb_dims }}] = static const unsigned int {{ name|upper }}_POSTAXIS_STRIDES[{{ in_nb_dims }}] =
{ {{ post_axis_strides|join(", ") }} }; { {{ post_axis_strides|join(", ") }} };
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
// Activation // Activation
#define {{ name|upper }}_ACTIVATION {{ activation }} #define {{ name|upper }}_ACTIVATION {{ activation }}
{% include "./_save_outputs.jinja" %}
{% include "./_rescaling.jinja" %} {% include "./_rescaling.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
{% include "./_meminfo.jinja" %} {% include "./_meminfo.jinja" %}
{# For layer configuration -#} {# For layer configuration -#}
#define {{ name|upper }}_NB_ELTS {{ in_dims[0]|join('*') }} #define {{ name|upper }}_NB_ELTS {{ in_dims[0]|join('*') }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -8,5 +8,6 @@ ...@@ -8,5 +8,6 @@
#define {{ name|upper }}_AXIS_SIZE {{ axis_size }} #define {{ name|upper }}_AXIS_SIZE {{ axis_size }}
#define {{ name|upper }}_AXIS_SIZE_POST {{ axis_size_post }} #define {{ name|upper }}_AXIS_SIZE_POST {{ axis_size_post }}
#define {{ name|upper }}_AXIS_SIZE_PRE {{ axis_size_pre }} #define {{ name|upper }}_AXIS_SIZE_PRE {{ axis_size_pre }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
...@@ -13,5 +13,6 @@ constexpr int {{ name|upper }}_OUT_STRIDES[] = { {{ out_strides | join(', ') }} ...@@ -13,5 +13,6 @@ constexpr int {{ name|upper }}_OUT_STRIDES[] = { {{ out_strides | join(', ') }}
constexpr int {{ name|upper }}_PERMUTE[] = { {{ output_dims_order | join(', ') }} }; constexpr int {{ name|upper }}_PERMUTE[] = { {{ output_dims_order | join(', ') }} };
constexpr int {{ name|upper }}_DIMS[] = { {{ in_dims[0] | join(', ') }} }; constexpr int {{ name|upper }}_DIMS[] = { {{ in_dims[0] | join(', ') }} };
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */ #endif /* {{ name|upper }}_LAYER_H */
\ No newline at end of file
#if SAVE_OUTPUTS #if SAVE_OUTPUTS
{% for outidx in range(nb_out) -%} printf("[NOTICE] Saving outputs of node {{ name }}\n");
FILE* {{out_name[outidx]|upper}}_STREAM = fopen("data/export_outputs/{{out_name[outidx]}}.txt", "w"); FILE* {{ out_name[0] | upper }}_STREAM = fopen("data/export_outputs/{{out_name[0]}}.txt", "w");
saveOutputs<{{out_cdtype[outidx]}}>( saveOutputs<{{ out_name[0] | upper }}_NB_OUTPUTS,
{{out_name[outidx]|upper}}_NB_OUTPUTS, {{ out_name[0] | upper }}_OUT_HEIGHT,
{{out_name[outidx]|upper}}_OUT_HEIGHT, {{ out_name[0] | upper }}_OUT_WIDTH,
{{out_name[outidx]|upper}}_OUT_WIDTH, {{ out_name[0] | upper }}_DEV_FMT>
{#- {{out_name[outidx]|upper}}_CONT_OFFSET, ({#- {{ out_name[0] | upper }}_CONT_OFFSET,
{{out_name[outidx]|upper}}_CONT_SIZE, {{ out_name[0] | upper }}_CONT_SIZE,
{{out_name[outidx]|upper}}_WRAP_OFFSET, {{ out_name[0] | upper }}_WRAP_OFFSET,
{{out_name[outidx]|upper}}_WRAP_SIZE, {{ out_name[0] | upper }}_WRAP_SIZE,
{{out_name[outidx]|upper}}_STRIDE, #} {{ out_name[0] | upper }}_STRIDE, -#}
{{out_name[outidx]}}, {{ out_name[0] }},
{{out_name[outidx]|upper}}_STREAM, {{ out_name[0] | upper }}_STREAM);
Format::NHWC); fclose({{ out_name[0] | upper }}_STREAM);
fclose({{out_name[outidx]|upper}}_STREAM);
{% endfor %}
#endif #endif
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