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

Add exportable function to check if a node is exportable instead of having...

Add exportable function to check if a node is exportable instead of having complicated keys in registry.
parent 54121fb7
No related branches found
No related tags found
3 merge requests!279v0.4.0,!253v0.4.0,!163Export refactor
Pipeline #50832 failed
from typing import Dict, Tuple, Set from typing import Dict, List, Set
import aidge_core import aidge_core
from aidge_core.export_utils import ExportNode from aidge_core.export_utils import ExportNode
from enum import Enum from enum import Enum
# Language # Language
LANGUAGE = Enum('LANGUAGE', ['Cpp/C']) LANGUAGE = Enum('LANGUAGE', ['Cpp/C'])
# Define new type registry_key
registry_key = Tuple[str, aidge_core.dtype, aidge_core.dformat]
# TODO: very naive implementation ! # TODO: very naive implementation !
# error handling should be added ! # error handling should be added !
...@@ -17,7 +15,7 @@ class ExportLib(): # Should be abstract ? ...@@ -17,7 +15,7 @@ class ExportLib(): # Should be abstract ?
# Lib name usefull ? # Lib name usefull ?
_name:str = None _name:str = None
# Registry of exportNode # Registry of exportNode
_export_node_registry:Dict[registry_key, ExportNode] = {} _export_node_registry:Dict[str, List[ExportNode]] = {}
# The language type usefull ? # The language type usefull ?
_language: LANGUAGE = None _language: LANGUAGE = None
def __init__(self) -> None: def __init__(self) -> None:
...@@ -31,35 +29,42 @@ class ExportLib(): # Should be abstract ? ...@@ -31,35 +29,42 @@ class ExportLib(): # Should be abstract ?
:rtype: bool :rtype: bool
""" """
# TODO: should return usable error that can be catch to know if only some keys have not been respected ! # TODO: should return usable error that can be catch to know if only some keys have not been respected !
key: registry_key = (node.type(), node.dtype(), node.dformat()) if node.type() not in cls._export_node_registry:
return key in cls._export_node_registry return False
else:
for i in cls._export_node_registry[node.type()]:
if i.exportable(node):
return True
return False
@classmethod @classmethod
def supported_operators(cls)->Set[str]: def supported_operators(cls)->Set[str]:
""" """
:return: list of supported operator by this ExportLib :return: Set of operator supported by this ExportLib
:rtype: List[str] :rtype: Set[str]
""" """
operators = set() return cls._export_node_registry.keys()
for key in cls._export_node_registry.keys():
operators.add(key[0])
return operators
@classmethod @classmethod
def get_export_node(cls, node:aidge_core.Node)->ExportNode: def get_export_node(cls, node:aidge_core.Node)->ExportNode:
""" """
:param node: Node to transform :param node: Node to transform
:type node: aidge_core.Node :type node: :py:class:`aidge_core.Node`
:return: Corresponding export node. :return: Corresponding export node.
:rtype: ExportNode :rtype: ExportNode
""" """
if not cls.exportable(node): if not cls.exportable(node):
raise ValueError("Node is not exportable ...") raise ValueError(f"Node {node.type()} is not exportable by ExportLib {cls._name} !")
key: registry_key = (node.type(), node.dtype(), node.dformat()) if len(cls._export_node_registry[node.type()]) != 1:
return key in cls._export_node_registry raise RuntimeError("ExportLib registry doesn't support when multiple export node are available yet ...")
else:
return cls._export_node_registry[node.type()][0](node)
@classmethod @classmethod
def add_export_node(cls, key:registry_key, eNode:ExportNode)->None: def add_export_node(cls, key:str, eNode:ExportNode)->None:
cls._export_node_registry[key] = eNode if key not in cls._export_node_registry:
cls._export_node_registry[key] = [eNode]
else:
cls._export_node_registry[key].append(eNode)
def operator_register(lib: ExportLib, key:registry_key, *args): def operator_register(lib: ExportLib, key:str, *args):
"""Helper decorator to register an :py:class:`ExportNode` to an :py:class:`ExportLib` """Helper decorator to register an :py:class:`ExportNode` to an :py:class:`ExportLib`
""" """
def decorator(operator): def decorator(operator):
......
...@@ -181,7 +181,17 @@ class ExportNode(ABC): ...@@ -181,7 +181,17 @@ class ExportNode(ABC):
self.attributes["out_width"][idx] = get_width(tensor) self.attributes["out_width"][idx] = get_width(tensor)
else: else:
print(f"No output for {self.node.name()}") print(f"No output for {self.node.name()}")
@classmethod
@abstractmethod
def exportable(cls, node: aidge_core.Node)->bool:
"""Given a :py:class:`aidge_core.Node` return if the node can be exported or not.
:param node: Node to test the exportability
:type node: :py:class:`aidge_core.Node`
:return: True if the node is exportable, False oterhwise.
:rtype: bool
"""
pass
class ExportNodeCpp(ExportNode): class ExportNodeCpp(ExportNode):
# Path to the template defining how to export the node definition # Path to the template defining how to export the node definition
......
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