From c3403a4d12fb8fd493437e8855e77e7321b09155 Mon Sep 17 00:00:00 2001 From: cmoineau <cyril.moineau@cea.fr> Date: Mon, 9 Sep 2024 14:20:16 +0000 Subject: [PATCH] Add generate_input_files function in aigde_core. --- .../export_utils/templates/c_data.jinja | 6 +++ aidge_core/export_utils/tensor_export.py | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 aidge_core/export_utils/templates/c_data.jinja create mode 100644 aidge_core/export_utils/tensor_export.py diff --git a/aidge_core/export_utils/templates/c_data.jinja b/aidge_core/export_utils/templates/c_data.jinja new file mode 100644 index 000000000..3709379e5 --- /dev/null +++ b/aidge_core/export_utils/templates/c_data.jinja @@ -0,0 +1,6 @@ +{#- For libraries #} +#include <stdint.h> + {# Design header of the array -#} +static const {{ data_t }} {{ name }}[{{ dims |join("*") }}] __attribute__((section("nn_data"))) = { +{{ values |join(", ") }} +}; diff --git a/aidge_core/export_utils/tensor_export.py b/aidge_core/export_utils/tensor_export.py new file mode 100644 index 000000000..43f013dd0 --- /dev/null +++ b/aidge_core/export_utils/tensor_export.py @@ -0,0 +1,40 @@ +import os + +from aidge_core.export_utils.code_generation import generate_file +from aidge_core.export_utils.data_conversion import aidge2c +from aidge_core import Tensor +from pathlib import Path + +def tensor_to_c(tensor:Tensor)->str: + """Given a :py:class:``aigd_core.Tensor``, return a C description of the tensor. + For example: + { + {1, 2}, + {3, 4} + } + + :param tensor: Tensor to transform to a string + :type tensor: Tensor + :return: String representation of a C array + :rtype: str + """ + return str(tensor) + +def generate_input_file(export_folder:str, + array_name:str, + tensor:Tensor): + + # If directory doesn't exist, create it + if not os.path.exists(export_folder): + os.makedirs(export_folder) + print(f"gen : {export_folder}/{array_name}.h") + ROOT = Path(__file__).resolve().parents[0] + generate_file( + file_path=f"{export_folder}/{array_name}.h", + template_path=str(ROOT / "templates" / "c_data.jinja"), + dims = tensor.dims(), + data_t = aidge2c(tensor.dtype()), + name = array_name, + values = list(tensor) + ) + -- GitLab