diff --git a/aidge_export_arm_cortexm/export.py b/aidge_export_arm_cortexm/export.py index 2041ca3ad6545607ef37ac3114c7d2a3be24fe2f..5179937e86974de4e3134f9652e99ef2918751bc 100644 --- a/aidge_export_arm_cortexm/export.py +++ b/aidge_export_arm_cortexm/export.py @@ -4,7 +4,7 @@ import shutil from pathlib import Path import numpy as np -from aidge_core.export.code_generation import * +from aidge_core.export_utils.code_generation import * from aidge_export_arm_cortexm.utils import (ROOT, AVAILABLE_BOARDS, has_board, \ OPERATORS_REGISTRY, supported_operators) import aidge_export_arm_cortexm.operators @@ -15,13 +15,13 @@ from aidge_export_arm_cortexm.memory import * -def export(export_folder_name, +def export(export_folder_name, graphview, scheduler = None, board:str ="stm32h7", library:str = "aidge", mem_wrapping = False): - + # Create export directory export_folder = Path().absolute() / export_folder_name os.makedirs(str(export_folder), exist_ok=True) @@ -36,7 +36,7 @@ def export(export_folder_name, board_path = AVAILABLE_BOARDS[board] else: raise ValueError(f"{board} not found in the package. Please among those boards: {list(AVAILABLE_BOARDS.keys())}") - + # Copy all static files in the export shutil.copytree(board_path, str(export_folder), dirs_exist_ok=True) diff --git a/aidge_export_arm_cortexm/operators.py b/aidge_export_arm_cortexm/operators.py index dbcfc096dceae1144564579712791c6e4223d239..b0f4f60b68d7ca414c50b821551e7664d05ca27a 100644 --- a/aidge_export_arm_cortexm/operators.py +++ b/aidge_export_arm_cortexm/operators.py @@ -8,7 +8,7 @@ from typing import Tuple, List, Union, Dict import aidge_core from aidge_core import ExportNode -from aidge_core.export.code_generation import * +from aidge_core.export_utils.code_generation import * from aidge_export_arm_cortexm.utils import ROOT, operator_register from aidge_export_arm_cortexm.utils.converter import numpy_dtype2ctype, aidge_datatype2dataformat, aidge_datatype2ctype from aidge_export_arm_cortexm.utils.generation import * @@ -78,23 +78,23 @@ class Producer_ARMCortexM: def export(self, export_file:Path, format:str = "NHWC"): - if (len(self.values.shape) == 4): + if (len(self.values.shape) == 4): # Suppose original dataformat is NCHW if format == "NCHW": - export_params(self.name, - self.values.reshape(-1), + export_params(self.name, + self.values.reshape(-1), str(export_file)) elif format == "NHWC": - export_params(self.name, - np.transpose(self.values, (0, 2, 3, 1)).reshape(-1), + export_params(self.name, + np.transpose(self.values, (0, 2, 3, 1)).reshape(-1), str(export_file)) else: raise RuntimeError("Producer format export not supported.") else: - export_params(self.name, - self.values.reshape(-1), + export_params(self.name, + self.values.reshape(-1), str(export_file)) - + class Scaling(): class ScalingMode: @@ -158,7 +158,7 @@ class Scaling(): assert precision >= 1.0 return power_of_2_divs, precision - + def __call__(self, mode:str) -> dict: """Get dictionnary of scale values in function of the mode @@ -168,9 +168,9 @@ class Scaling(): - fixed_point (16 or 32 bits) - single_shift - double_shift - + """ - + if mode == "floating_point": self.scaling = {"scaling_type": "floating_point", "scaling_value": self.scaling_factor} @@ -189,7 +189,7 @@ class Scaling(): self.scaling = {"scaling_type": "single_shift", "shift_value": shift_value[0]} - + elif mode == "double_shift": shift_value, _ = self.approximate_shift_scaling(self.scaling_factor, 2) @@ -200,7 +200,7 @@ class Scaling(): self.scaling = {"scaling_type": "no_scaling"} return self.scaling - + @operator_register("ReLU") class ReLU_ARMCortexM(ExportNode): @@ -300,7 +300,7 @@ class Conv_ARMCortexM(ExportNode): # Convert the biases to int32 if self.dataformat != "float32": self.producers[1].values = self.producers[1].values.astype(np.int32) - + self.producers[1].export(export_folder / "parameters" / f"{self.producers[1].name}.h") list_configs.append(f"parameters/{self.producers[1].name}.h") @@ -323,7 +323,7 @@ class Conv_ARMCortexM(ExportNode): stride=self.stride, padding=self.padding, dilation=self.dilation) - + elif self.library == "n2d2": # Export configuration file generate_file( @@ -368,9 +368,9 @@ class Conv_ARMCortexM(ExportNode): biases_name=self.inputs[2].name(), outputs_name=self.name )) - + return list_actions - + @operator_register("PaddedConv") class PaddedConv_ARMCortexM(Conv_ARMCortexM): @@ -406,7 +406,7 @@ class PaddedConv_ARMCortexM(Conv_ARMCortexM): if len(self.outputs_dims[0]) == 4: # if dims == [batch, nb_outputs] # transform to [nb_outputs, 1, 1] - self.outputs_dims[0] = self.outputs_dims[0][1:] + self.outputs_dims[0] = self.outputs_dims[0][1:] @operator_register("ConvReluScaling") @@ -431,7 +431,7 @@ class ConvReluScaling_ARMCortexM(Conv_ARMCortexM): # Impose Single Shift (perhaps change it to have a more modular system) self.scaling = Scaling(self.operator.get_attr("scalingFactor"), self.operator.get_attr("quantizedNbBits"))("floating_point") - + class Pooling_ARMCortexM(ExportNode): def __init__(self, node, board, library): @@ -474,7 +474,7 @@ class Pooling_ARMCortexM(ExportNode): copyfile(str(ROOT / "_Aidge_Arm" / "kernels" / "SupportFunctions" / "aidge_supportfunctions.h"), str(Path(export_folder) / "include")) - # Export configuration file + # Export configuration file generate_file( str(export_folder / "layers" / f"{self.name}.h"), str(ROOT / "_Aidge_Arm" / "templates" / "configuration" / "pooling.jinja"), @@ -485,8 +485,8 @@ class Pooling_ARMCortexM(ExportNode): stride=self.stride, padding=self.padding, pool_type=self.pool_type) - - + + elif self.library == "n2d2": # Nothing to copy @@ -572,7 +572,7 @@ class FC_ARMCortexM(ExportNode): # if dims == [batch, nb_channels, height, width] # transform to [nb_channels, height, width] self.inputs_dims[0] = self.inputs_dims[0][1:] - + # It also means that we need to change the dataformat of the weights weights = self.producers[0].values if len(weights.shape) == 2: @@ -623,7 +623,7 @@ class FC_ARMCortexM(ExportNode): channel_height=self.inputs_dims[0][1], channel_width=self.inputs_dims[0][2], nb_outputs=self.outputs_dims[0][0]) - + elif self.library == "n2d2": # Export configuration file @@ -635,7 +635,7 @@ class FC_ARMCortexM(ExportNode): output_dims=self.outputs_dims[0], activation=self.activation, **self.scaling) - + return list_configs def forward(self, list_actions:list): @@ -666,7 +666,7 @@ class FC_ARMCortexM(ExportNode): return list_actions - + @operator_register("FcScaling") class FCScaling_ARMCortexM(FC_ARMCortexM):