diff --git a/aidge_export_cpp/static/utils.hpp b/aidge_export_cpp/static/utils.hpp index 721e2c1ee6ccd34152baef7844422667c4f73252..2fc3a8860b22a2632df84f1fdaf19e301637d154 100644 --- a/aidge_export_cpp/static/utils.hpp +++ b/aidge_export_cpp/static/utils.hpp @@ -160,7 +160,10 @@ int get_ofst_from_fmt(int out, int h, int w) { template<int NB_OUTPUTS, int OUT_WIDTH, int OUT_HEIGHT, Format AIDGE_FMT, Format DEV_FMT, typename AidgeOutput_T, typename DevOutput_T> void aidge_cmp(std::string layer_name, AidgeOutput_T* aidge_output, DevOutput_T* dev_output) { - printf("[AIDGE COMPARE] - %s\n", layer_name.c_str()); + printf("[NOTICE] - Comparing with Aidge ref for node : %s -> ", layer_name.c_str()); + + const float atol = 1e-5f; // Absolute + const float rtol = 1e-3f; // Relative for (auto out = 0; out < NB_OUTPUTS; ++out) { for (auto h = 0; h < OUT_HEIGHT; ++h) { @@ -170,20 +173,34 @@ void aidge_cmp(std::string layer_name, AidgeOutput_T* aidge_output, DevOutput_T* const int dev_ofst = get_ofst_from_fmt<NB_OUTPUTS, OUT_WIDTH, OUT_HEIGHT, DEV_FMT>(out, h, w); if (aidge_ofst == -1 || dev_ofst == -1) { + printf("[FAILURE]\n"); printf("[ERROR] - Aborting this layer comparison...\n"); return; } - - if (aidge_output[aidge_ofst] != dev_output[dev_ofst]) { - if (std::is_floating_point<DevOutput_T>::value) { - printf("[ERROR] - First error detected at %dx%dx%d (out x h x w) : aidge_out = %f vs dev_out = %f\n", - out, h, w, static_cast<double>(aidge_output[aidge_ofst]), static_cast<double>(dev_output[dev_ofst])); - } else { + + // Float Comparison + if (std::is_floating_point<DevOutput_T>::value) { + + const float diff = std::abs(aidge_output[aidge_ofst] - dev_output[dev_ofst]); + const float tolerance = atol + rtol * std::abs(dev_output[dev_ofst]); + + if (diff > tolerance) { + printf("[FAILURE]\n"); + printf("[ERROR] - First error detected at %dx%dx%d (out x h x w) : aidge_out = %f vs dev_out = %f\n", + out, h, w, static_cast<double>(aidge_output[aidge_ofst]), static_cast<double>(dev_output[dev_ofst])); + printf("Abort program.\n"); + exit(1); + } + + // Int Comparison + } else { + if (aidge_output[aidge_ofst] != dev_output[dev_ofst]) { + printf("[FAILURE]\n"); printf("[ERROR] - First error detected at %dx%dx%d (out x h x w) : aidge_out = %d vs dev_out = %d\n", - out, h, w, static_cast<int>(aidge_output[aidge_ofst]), static_cast<int>(dev_output[dev_ofst])); + out, h, w, static_cast<int>(aidge_output[aidge_ofst]), static_cast<int>(dev_output[dev_ofst])); + printf("[ERROR] - Abort program.\n"); + exit(1); } - printf("Abort program.\n"); - exit(1); } } } diff --git a/aidge_export_cpp/templates/data/aidge_tensor.jinja b/aidge_export_cpp/templates/data/aidge_tensor.jinja index d682863a935c246a1ca41a3ceb459f5611832350..d717ad2cdd61764baf78ab039190d1986cce78b1 100644 --- a/aidge_export_cpp/templates/data/aidge_tensor.jinja +++ b/aidge_export_cpp/templates/data/aidge_tensor.jinja @@ -2,6 +2,12 @@ #define {{name|upper}}_FMT Format::{{ dformat | upper }} +{%- set format_map = { + "int8_t": "%4d", + "int32_t": "%6d", + "float": "%.9f" +} %} + static const {{ dtype }} {{ name }} {%- for dim in dims -%} [{{ dim }}] @@ -13,7 +19,7 @@ static const {{ dtype }} {{ name }} {%- if dims | length == 1 -%} {{ '{' }} {%- for x in range(dims[0]) -%} -{{ "%4d" | format(values[x]) }}, +{{ format_map[dtype] | format(values[x]) }}, {%- endfor -%} {{ '}' }}; {%- endif -%} @@ -23,7 +29,7 @@ static const {{ dtype }} {{ name }} {%- for y in range(dims[0]) -%} {{ '{' }} {%- for x in range(dims[1]) -%} - {{ "%4d" | format(values[y][x]) }}, + {{ format_map[dtype] | format(values[y][x]) }}, {%- endfor -%} {{ '}' }}, {%- endfor -%} @@ -36,7 +42,7 @@ static const {{ dtype }} {{ name }} {%- for y in range(dims[1]) %} {{ '{' }} {%- for x in range(dims[2]) -%} - {{ "%4d" | format(values[z][y][x]) }}, + {{ format_map[dtype] | format(values[z][y][x]) }}, {%- endfor -%} {{ '}' }}, {%- endfor %} @@ -53,7 +59,7 @@ static const {{ dtype }} {{ name }} {%- for y in range(dims[2]) %} {{ '{' }} {%- for x in range(dims[3]) -%} - {{ "%4d" | format(values[n][z][y][x]) }}, + {{ format_map[dtype] | format(values[n][z][y][x]) }}, {%- endfor -%} {{ '}' }}, {%- endfor %} diff --git a/aidge_export_cpp/templates/data/parameters.jinja b/aidge_export_cpp/templates/data/parameters.jinja index 944c76e7e8ae818b9a84a82a35e6fc7f9775f7b8..69b0e7560a0fb7b77e5f43021d096b21a974f881 100644 --- a/aidge_export_cpp/templates/data/parameters.jinja +++ b/aidge_export_cpp/templates/data/parameters.jinja @@ -4,7 +4,7 @@ {%- set format_map = { "int8_t": "%4d", "int32_t": "%6d", - "float": "%8.3f" + "float": "%.9f" } %} {# Design header of the array -#}