From 11a950fda687fa2b199fec2f11d6955e9341204e Mon Sep 17 00:00:00 2001 From: cmoineau <cyril.moineau@cea.fr> Date: Fri, 26 Apr 2024 12:21:40 +0000 Subject: [PATCH] Remove all str path and use Pathlib module instead. --- aidge_core/aidge_export_aidge/__init__.py | 1 - aidge_core/aidge_export_aidge/export.py | 11 ++++++----- .../aidge_export_aidge/operator_export/conv.py | 13 ++++++------- .../aidge_export_aidge/operator_export/fc.py | 14 +++++++------- .../operator_export/maxpooling.py | 13 ++++++------- .../aidge_export_aidge/operator_export/producer.py | 8 +++++--- .../aidge_export_aidge/operator_export/relu.py | 6 +++--- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/aidge_core/aidge_export_aidge/__init__.py b/aidge_core/aidge_export_aidge/__init__.py index 6dc611fc4..c5d6f96b3 100644 --- a/aidge_core/aidge_export_aidge/__init__.py +++ b/aidge_core/aidge_export_aidge/__init__.py @@ -1,4 +1,3 @@ - from pathlib import Path # Constants diff --git a/aidge_core/aidge_export_aidge/export.py b/aidge_core/aidge_export_aidge/export.py index d2320aa0c..417e3e953 100644 --- a/aidge_core/aidge_export_aidge/export.py +++ b/aidge_core/aidge_export_aidge/export.py @@ -1,6 +1,7 @@ import aidge_core import shutil import os +from pathlib import Path from .utils import supported_operators, OPERATORS_REGISTRY from . import ROOT_EXPORT @@ -9,12 +10,12 @@ from . import ROOT_EXPORT def export(export_folder: str, graphview: aidge_core.GraphView, scheduler: aidge_core.Scheduler): - + export_folder_path = Path(export_folder) # Create export directory os.makedirs(export_folder, exist_ok=True) # Cpy static files - shutil.copytree(ROOT_EXPORT / "static/", export_folder, dirs_exist_ok=True) + shutil.copytree(ROOT_EXPORT / "static/", export_folder_path, dirs_exist_ok=True) ordered_nodes = scheduler.get_static_scheduling() @@ -27,7 +28,7 @@ def export(export_folder: str, op = OPERATORS_REGISTRY[node.type()](node) # Export the configuration - list_configs = op.export(export_folder, list_configs) + list_configs = op.export(export_folder_path, list_configs) # Add forward kernel list_actions = op.forward(list_actions) @@ -38,8 +39,8 @@ def export(export_folder: str, # Generate full dnn.cpp aidge_core.generate_file( - f"{export_folder}/src/dnn.cpp", - str(ROOT_EXPORT) + "/templates/dnn.jinja", + export_folder_path / "src/dnn.cpp", + ROOT_EXPORT / "templates/dnn.jinja", headers=list_configs, operators=set_operator, actions=list_actions, diff --git a/aidge_core/aidge_export_aidge/operator_export/conv.py b/aidge_core/aidge_export_aidge/operator_export/conv.py index e3ce0ff98..52558980b 100644 --- a/aidge_core/aidge_export_aidge/operator_export/conv.py +++ b/aidge_core/aidge_export_aidge/operator_export/conv.py @@ -2,7 +2,7 @@ from aidge_core.aidge_export_aidge.utils import operator_register, parse_node_in from aidge_core.aidge_export_aidge import ROOT_EXPORT from aidge_core import ExportNode, generate_file, generate_str import os - +from pathlib import Path @operator_register("Conv") class Conv(ExportNode): @@ -10,11 +10,10 @@ class Conv(ExportNode): super().__init__(node) - def export(self, export_folder:str, list_configs:list): + def export(self, export_folder:Path, list_configs:list): - name = f"{self.name}" - include_path = f"attributes/{name}.hpp" - filepath = f"{export_folder}/include/{include_path}" + include_path = f"attributes/{self.name}.hpp" + filepath = export_folder / f"include/{include_path}" dirname = os.path.dirname(filepath) # If directory doesn't exist, create it @@ -22,7 +21,7 @@ class Conv(ExportNode): generate_file( filepath, - str(ROOT_EXPORT) + "/templates/attributes/conv.jinja", + ROOT_EXPORT / "templates/attributes/conv.jinja", name=self.name, **self.attributes ) @@ -31,7 +30,7 @@ class Conv(ExportNode): def forward(self, list_actions:list): list_actions.append(generate_str( - str(ROOT_EXPORT) + "/templates/graph_ctor/conv.jinja", + ROOT_EXPORT /"templates/graph_ctor/conv.jinja", name=self.name, inputs=parse_node_input(self.node.inputs()), **self.attributes diff --git a/aidge_core/aidge_export_aidge/operator_export/fc.py b/aidge_core/aidge_export_aidge/operator_export/fc.py index 0d0d60ea1..af1748691 100644 --- a/aidge_core/aidge_export_aidge/operator_export/fc.py +++ b/aidge_core/aidge_export_aidge/operator_export/fc.py @@ -2,7 +2,7 @@ from aidge_core.aidge_export_aidge.utils import operator_register,parse_node_inp from aidge_core.aidge_export_aidge import ROOT_EXPORT from aidge_core import ExportNode, generate_file, generate_str import os - +from pathlib import Path @operator_register("FC") class FC(ExportNode): @@ -10,11 +10,11 @@ class FC(ExportNode): super().__init__(node) - def export(self, export_folder:str, list_configs:list): + def export(self, export_folder:Path, list_configs:list): + - name = f"{self.name}" - include_path = f"attributes/{name}.hpp" - filepath = f"{export_folder}/include/{include_path}" + include_path = f"attributes/{self.name}.hpp" + filepath = export_folder / f"include/{include_path}" dirname = os.path.dirname(filepath) # If directory doesn't exist, create it @@ -22,7 +22,7 @@ class FC(ExportNode): generate_file( filepath, - str(ROOT_EXPORT) + "/templates/attributes/fc.jinja", + ROOT_EXPORT / "templates/attributes/fc.jinja", name=self.name, InChannels=self.inputs_dims[1][1], **self.attributes @@ -32,7 +32,7 @@ class FC(ExportNode): def forward(self, list_actions:list): list_actions.append(generate_str( - str(ROOT_EXPORT) + "/templates/graph_ctor/fc.jinja", + ROOT_EXPORT / "templates/graph_ctor/fc.jinja", name=self.name, inputs=parse_node_input(self.node.inputs()), **self.attributes diff --git a/aidge_core/aidge_export_aidge/operator_export/maxpooling.py b/aidge_core/aidge_export_aidge/operator_export/maxpooling.py index ad26fa3c6..a88d31b1a 100644 --- a/aidge_core/aidge_export_aidge/operator_export/maxpooling.py +++ b/aidge_core/aidge_export_aidge/operator_export/maxpooling.py @@ -2,7 +2,7 @@ from aidge_core.aidge_export_aidge.utils import operator_register, parse_node_in from aidge_core.aidge_export_aidge import ROOT_EXPORT from aidge_core import ExportNode, generate_file, generate_str import os - +from pathlib import Path @operator_register("MaxPooling") class MaxPooling(ExportNode): @@ -10,11 +10,10 @@ class MaxPooling(ExportNode): super().__init__(node) - def export(self, export_folder:str, list_configs:list): + def export(self, export_folder:Path, list_configs:list): - name = f"{self.name}" - include_path = f"attributes/{name}.hpp" - filepath = f"{export_folder}/include/{include_path}" + include_path = f"attributes/{self.name}.hpp" + filepath = export_folder / f"include/{include_path}" dirname = os.path.dirname(filepath) # If directory doesn't exist, create it @@ -22,7 +21,7 @@ class MaxPooling(ExportNode): generate_file( filepath, - str(ROOT_EXPORT) + "/templates/attributes/maxpooling.jinja", + ROOT_EXPORT / "templates/attributes/maxpooling.jinja", name=self.name, **self.attributes ) @@ -31,7 +30,7 @@ class MaxPooling(ExportNode): def forward(self, list_actions:list): list_actions.append(generate_str( - str(ROOT_EXPORT) + "/templates/graph_ctor/maxpooling.jinja", + ROOT_EXPORT / "templates/graph_ctor/maxpooling.jinja", name=self.name, inputs=parse_node_input(self.node.inputs()), **self.attributes diff --git a/aidge_core/aidge_export_aidge/operator_export/producer.py b/aidge_core/aidge_export_aidge/operator_export/producer.py index c8435db17..4d8e6fe1c 100644 --- a/aidge_core/aidge_export_aidge/operator_export/producer.py +++ b/aidge_core/aidge_export_aidge/operator_export/producer.py @@ -3,6 +3,7 @@ from aidge_core.aidge_export_aidge import ROOT_EXPORT from aidge_core import ExportNode, generate_file, generate_str import numpy as np import os +from pathlib import Path @operator_register("Producer") @@ -17,20 +18,21 @@ class Producer(ExportNode): self.tensor_name = f"{child.name()}_{in_idx}" self.values = np.array(self.operator.get_output(0)) - def export(self, export_folder:str, list_configs:list): + def export(self, export_folder:Path, list_configs:list): assert(len(self.node.output(0)) == 1) include_path = f"parameters/{self.tensor_name}.hpp" - filepath = f"{export_folder}/include/{include_path}" + filepath = export_folder / f"include/{include_path}" dirname = os.path.dirname(filepath) # If directory doesn't exist, create it if not os.path.exists(dirname): os.makedirs(dirname) + aidge_tensor = self.operator.get_output(0) generate_file( filepath, - str(ROOT_EXPORT) + "/templates/parameter.jinja", + ROOT_EXPORT / "templates/parameter.jinja", dims = aidge_tensor.dims(), data_t = "float", # TODO : get data from producer name = self.tensor_name, diff --git a/aidge_core/aidge_export_aidge/operator_export/relu.py b/aidge_core/aidge_export_aidge/operator_export/relu.py index 0d18e6a74..c0f4f6afd 100644 --- a/aidge_core/aidge_export_aidge/operator_export/relu.py +++ b/aidge_core/aidge_export_aidge/operator_export/relu.py @@ -1,19 +1,19 @@ from aidge_core.aidge_export_aidge.utils import operator_register, parse_node_input from aidge_core import ExportNode, generate_str from aidge_core.aidge_export_aidge import ROOT_EXPORT +from pathlib import Path @operator_register("ReLU") class ReLU(ExportNode): def __init__(self, node): super().__init__(node) - - def export(self, export_folder:str, list_configs:list): + def export(self, export_folder:Path, list_configs:list): return list_configs def forward(self, list_actions:list): list_actions.append(generate_str( - str(ROOT_EXPORT) + "/templates/graph_ctor/relu.jinja", + ROOT_EXPORT / "templates/graph_ctor/relu.jinja", name=self.name, inputs=parse_node_input(self.node.inputs()), **self.attributes -- GitLab