Skip to content
Snippets Groups Projects
Commit e1c3120e authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Update aidge.export -> aidge_core.export_utils.

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