dev(ir): add the basis for the frontend IR

- add IntermediateNode base class
- add Add
- add Input
This commit is contained in:
Arthur Meyre
2021-07-21 10:41:01 +02:00
parent 29c1641f48
commit b45944b66a
3 changed files with 56 additions and 1 deletions

View File

@@ -1,2 +1,2 @@
"""HDK's module for shared data structures and code"""
from . import data_types
from . import data_types, representation

View File

@@ -0,0 +1,2 @@
"""HDK's representation module to represent source programs"""
from . import intermediate

View File

@@ -0,0 +1,53 @@
"""File containing HDK's intermdiate representation of source programs operations"""
from abc import ABC
from copy import deepcopy
from typing import Any, Dict, Iterable, List, Optional, Tuple
from ..data_types import BaseValue
class IntermediateNode(ABC):
"""Abstract Base Class to derive from to represent source program operations"""
inputs: List[BaseValue]
outputs: List[BaseValue]
op_args: Optional[Tuple[Any, ...]]
op_kwargs: Optional[Dict[str, Any]]
def __init__(
self,
inputs: Iterable[BaseValue],
op_args: Optional[Tuple[Any, ...]] = None,
op_kwargs: Optional[Dict[str, Any]] = None,
) -> None:
self.inputs = list(inputs)
self.op_args = op_args
self.op_kwargs = op_kwargs
class Add(IntermediateNode):
"""Addition between two values"""
def __init__(
self,
inputs: Iterable[BaseValue],
) -> None:
super().__init__(inputs)
assert len(self.inputs) == 2
# For now copy the first input type for the output type
# We don't perform checks or enforce consistency here for now, so this is OK
self.outputs = [deepcopy(self.inputs[0])]
class Input(IntermediateNode):
"""Node representing an input of the numpy program"""
def __init__(
self,
inputs: Iterable[BaseValue],
) -> None:
super().__init__(inputs)
assert len(self.inputs) == 1
self.outputs = [deepcopy(self.inputs[0])]