Skip to content
Snippets Groups Projects
Commit f69e540b authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Merged with dev

parents c42b54b4 10ca7e16
No related branches found
No related tags found
2 merge requests!710.4.0,!59Continuous improvement of export_cpp
......@@ -8,5 +8,6 @@
#define {{ name|upper }}_AXIS_SIZE {{ axis_size }}
#define {{ name|upper }}_AXIS_SIZE_POST {{ axis_size_post }}
#define {{ name|upper }}_AXIS_SIZE_PRE {{ axis_size_pre }}
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */
......@@ -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 }}_DIMS[] = { {{ in_dims[0] | join(', ') }} };
{% include "./_save_outputs.jinja" %}
#endif /* {{ name|upper }}_LAYER_H */
\ No newline at end of file
#include <stdint.h>
static const {{ data_t }} {{ name }}
#define {{name|upper}}_FMT Format::{{ dformat | upper }}
{%- set format_map = {
"int8_t": "%4d",
"int16_t": "%6d",
"int32_t": "%6d",
"int64_t": "%8d",
"uint8_t": "%4d",
"uint16_t": "%6d",
"uint32_t": "%6d",
"uint64_t": "%8d",
"float": "%.9f",
"half_float::half": "%.9f",
"double": "%.17f"
} %}
static const {{ dtype }} {{ name }}
{%- for dim in dims -%}
[{{ dim }}]
{%- endfor %} =
{{ values }};
{{ '{' -}}
{# 1D #}
{%- if dims | length == 1 -%}
{{ '{' }}
{%- for x in range(dims[0]) -%}
{{ format_map[dtype] | format(values[x]) }},
{%- endfor -%}
{{ '}' }};
{%- endif -%}
{#- 2D #}
{%- if dims | length == 2 -%}
{%- for y in range(dims[0]) -%}
{{ '{' }}
{%- for x in range(dims[1]) -%}
{{ format_map[dtype] | format(values[y][x]) }},
{%- endfor -%}
{{ '}' }},
{%- endfor -%}
{%- endif -%}
{#- 3D #}
{%- if dims | length == 3 -%}
{%- for z in range(dims[0]) %}
{{ '{' }}
{%- for y in range(dims[1]) %}
{{ '{' }}
{%- for x in range(dims[2]) -%}
{{ format_map[dtype] | format(values[z][y][x]) }},
{%- endfor -%}
{{ '}' }},
{%- endfor %}
{{ '}' }},
{%- endfor -%}
{%- endif -%}
{#- 4D #}
{%- if dims | length == 4 -%}
{%- for n in range(dims[0]) %}
{{ '{' }}
{%- for z in range(dims[1]) %}
{{ '{' }}
{%- for y in range(dims[2]) %}
{{ '{' }}
{%- for x in range(dims[3]) -%}
{{ format_map[dtype] | format(values[n][z][y][x]) }},
{%- endfor -%}
{{ '}' }},
{%- endfor %}
{{ '}' }},
{%- endfor %}
{{ '}' }},
{%- endfor %}
{%- endif -%}
{{ '};' }}
{#- For libraries -#}
#include <stdint.h>
{%- set format_map = {
"int8_t": "%4d",
"int16_t": "%6d",
"int32_t": "%6d",
"int64_t": "%8d",
"uint8_t": "%4d",
"uint16_t": "%6d",
"uint32_t": "%6d",
"uint64_t": "%8d",
"float": "%.9f",
"half_float::half": "%.9f",
"double": "%.17f"
} %}
{# Design header of the array -#}
static const {{ data_t }} {{ name }}[{{ values|length }}] __attribute__((section(".nn_data"))) =
static const {{ dtype }} {{ name }}[{{ dims | join("*") }}] __attribute__((section(".nn_data"))) =
{
{# For loop to add new elements -#}
{%- for i in range(values|length) %}
{# 1D #}
{%- if dims | length == 1 -%}
{%- for x in range(dims[0]) -%}
{{ format_map[dtype] | format(values[x]) }},
{%- endfor -%}
{%- endif -%}
{#- 2D #}
{%- if dims | length == 2 -%}
{%- for y in range(dims[0]) %}
{{ ' ' }}
{%- for x in range(dims[1]) -%}
{{ format_map[dtype] | format(values[y][x]) }},
{%- endfor %}
{%- endfor -%}
{%- endif -%}
{#- 3D #}
{%- if dims | length == 3 -%}
{%- for z in range(dims[0]) %}
{{ ' ' }}
{%- for y in range(dims[1]) %}
{{ ' ' }}
{%- for x in range(dims[2]) -%}
{{ format_map[dtype] | format(values[z][y][x]) }},
{%- endfor -%}
{%- endfor %}
{%- endfor -%}
{%- endif -%}
{#- Last value -#}
{%- if (i+1) == values|length -%}
{{ values[i]|string }}
{%- else -%}
{%- if (i+1) % 5 == 0 -%}
{{ values[i]|string + ",\n\t" }}
{%- else -%}
{{ values[i]|string + ", " }}
{%- endif -%}
{%- endif -%}
{#- 4D #}
{%- if dims | length == 4 -%}
{%- for n in range(dims[0]) %}
{{ ' ' }}
{%- for z in range(dims[1]) %}
{{ ' ' }}
{%- for y in range(dims[2]) %}
{{ ' ' }}
{%- for x in range(dims[3]) -%}
{{ format_map[dtype] | format(values[n][z][y][x]) }},
{%- endfor -%}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endif %}
};
......@@ -2,7 +2,9 @@
#if AIDGE_CMP
aidge_cmp<{{ out_name[0] | upper }}_NB_OUTPUTS,
{{ out_name[0] | upper }}_OUT_HEIGHT,
{{ out_name[0] | upper }}_OUT_WIDTH>
{{ out_name[0] | upper }}_OUT_WIDTH,
{{ out_name[0] | upper }}_AIDGE_FMT,
{{ out_name[0] | upper }}_DEV_FMT>
("{{ name }}", ({{out_cdtype[0]}}*) {{ out_name[0] }}_aidge, {{ out_name[0] }});
#endif
{%- endif %}
\ No newline at end of file
#if SAVE_OUTPUTS
{% for outidx in range(nb_out) -%}
FILE* {{out_name[outidx]|upper}}_STREAM = fopen("data/export_outputs/{{out_name[outidx]}}.txt", "w");
saveOutputs<{{out_cdtype[outidx]}}>(
{{out_name[outidx]|upper}}_NB_OUTPUTS,
{{out_name[outidx]|upper}}_OUT_HEIGHT,
{{out_name[outidx]|upper}}_OUT_WIDTH,
{#- {{out_name[outidx]|upper}}_CONT_OFFSET,
{{out_name[outidx]|upper}}_CONT_SIZE,
{{out_name[outidx]|upper}}_WRAP_OFFSET,
{{out_name[outidx]|upper}}_WRAP_SIZE,
{{out_name[outidx]|upper}}_STRIDE, #}
{{out_name[outidx]}},
{{out_name[outidx]|upper}}_STREAM,
Format::NHWC);
fclose({{out_name[outidx]|upper}}_STREAM);
{% endfor %}
printf("[NOTICE] Saving outputs of node {{ name }}\n");
FILE* {{ out_name[0] | upper }}_STREAM = fopen("data/export_outputs/{{out_name[0]}}.txt", "w");
saveOutputs<{{ out_name[0] | upper }}_NB_OUTPUTS,
{{ out_name[0] | upper }}_OUT_HEIGHT,
{{ out_name[0] | upper }}_OUT_WIDTH,
{{ out_name[0] | upper }}_DEV_FMT>
({#- {{ out_name[0] | upper }}_CONT_OFFSET,
{{ out_name[0] | upper }}_CONT_SIZE,
{{ out_name[0] | upper }}_WRAP_OFFSET,
{{ out_name[0] | upper }}_WRAP_SIZE,
{{ out_name[0] | upper }}_STRIDE, -#}
{{ out_name[0] }},
{{ out_name[0] | upper }}_STREAM);
fclose({{ out_name[0] | upper }}_STREAM);
#endif
......@@ -4,3 +4,4 @@ log_outputs/*
assets/*
data/*
log.txt
*.json
......@@ -18,11 +18,7 @@ import aidge_backend_cpu
import aidge_quantization
import aidge_export_cpp
from aidge_export_cpp.export_utils import (
cpp_fuse_to_metaops,
set_nodes_names,
set_nodes_datatypes,
exclude_unwanted_producers)
from aidge_export_cpp.export_utils import *
from aidge_core.export_utils import remove_optional_inputs, get_node_from_metaop
......@@ -310,8 +306,9 @@ if quantize_model:
tensors[i] = aidge_core.Tensor(array)
tensors[i].set_datatype(TARGET_TYPE)
tensors[i].set_backend("cpu")
# Setting modele to CPU for export
model.set_backend("cpu")
# Setting model to CPU for export
model.set_backend("cpu")
# --------------------------------------------------------------
......@@ -442,9 +439,8 @@ Once the tensors have been casted, the log_outputs() function can be
called to store their values into log files.
"""
if os.path.isdir("log_outputs"):
shutil.rmtree("log_outputs")
model.log_outputs("log_outputs")
if AIDGE_CMP:
generate_aidge_ifmaps(model)
# --------------------------------------------------------------
# TEST MODE
......@@ -461,29 +457,10 @@ changing the inputs of the layer, to isolate the source of the issue.
for node in model.get_nodes():
node.attributes().dev_mode = DEV_MODE
# --------------------------------------------------------------
# AIDGE CMP
# --------------------------------------------------------------
"""
If the --aidge_cmp option is enabled, the feature maps generated by aidge with the
backend cpu will be exported in the generated export. It will be used as reference
to verify that the results with the optimized kernels are correct for the exported
model.
This option has to be passed to each node in order to be used within the Export Nodes.
(JConv, JPad, ...) that you can find in the "operators" folder.
"""
if AIDGE_CMP:
for node in model.get_nodes():
node.attributes().aidge_cmp = True
# --------------------------------------------------------------
# EXPORT THE MODEL
# --------------------------------------------------------------
model.save("exported_model")
aidge_export_cpp.export(EXPORT_FOLDER,
model,
scheduler,
......@@ -503,4 +480,4 @@ try:
for std_line in aidge_core.utils.run_command(["./bin/run_export"], cwd=EXPORT_FOLDER):
print(std_line, end="")
except subprocess.CalledProcessError as e:
raise RuntimeError(0, f"An error occurred, failed to run export.") from e
\ No newline at end of file
raise RuntimeError(0, f"An error occurred, failed to run export.") from e
......@@ -23,12 +23,7 @@ import aidge_backend_cpu
import aidge_quantization
import aidge_export_cpp
from aidge_export_cpp.export_utils import (
cpp_fuse_to_metaops,
exclude_unwanted_producers,
set_nodes_names,
set_nodes_datatypes,
normalize)
from aidge_export_cpp.export_utils import *
from aidge_core.export_utils import remove_optional_inputs, get_node_from_metaop
......@@ -506,16 +501,6 @@ tensors' datatypes to Int8, except for biases which should remain in Int32.
if quantize_model:
set_nodes_datatypes(model)
# Store tensors values into log files
"""
Once the tensors has been casted, the log_outputs() function can be
called to store their values into log files.
"""
if os.path.isdir("log_outputs"):
shutil.rmtree("log_outputs")
model.log_outputs("log_outputs")
# --------------------------------------------------------------
# TEST MODE
# --------------------------------------------------------------
......@@ -545,15 +530,12 @@ This option has to be passed to each node in order to be used within the Export
"""
if AIDGE_CMP:
for node in model.get_nodes():
node.attributes().aidge_cmp = True
generate_aidge_ifmaps(model)
# --------------------------------------------------------------
# EXPORT THE MODEL
# --------------------------------------------------------------
model.save("exported_model")
aidge_export_cpp.export(EXPORT_FOLDER,
model,
scheduler,
......
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