Skip to content

Commit

Permalink
Merge pull request #998 from vloncar/split_fpga_types
Browse files Browse the repository at this point in the history
Split fpga_types into separate files
  • Loading branch information
jmitrevs authored Apr 16, 2024
2 parents 1756cba + df8e1f1 commit ccf17c6
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 191 deletions.
3 changes: 2 additions & 1 deletion hls4ml/backends/catapult/catapult_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import numpy as np

from hls4ml.backends import FPGABackend
from hls4ml.backends.fpga.fpga_types import ACTypeConverter, CatapultArrayVariableConverter, HLSTypeConverter
from hls4ml.backends.catapult.catapult_types import CatapultArrayVariableConverter
from hls4ml.backends.fpga.fpga_types import ACTypeConverter, HLSTypeConverter
from hls4ml.model.attributes import ChoiceAttribute, ConfigurableAttribute, TypeAttribute
from hls4ml.model.flow import register_flow
from hls4ml.model.layers import (
Expand Down
92 changes: 92 additions & 0 deletions hls4ml/backends/catapult/catapult_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from hls4ml.backends.fpga.fpga_types import (
ArrayVariableConverter,
InplaceStreamVariableConverter,
StreamVariableConverter,
StructMemberVariableConverter,
VariableDefinition,
)

# region ArrayVariable


class CatapultArrayVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}] /* {pragma} */'.format(
type=self.type.name, name=self.name, suffix=name_suffix, shape=self.size_cpp(), pragma=self.pragma
)


class CatapultInplaceArrayVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class CatapultArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Catapult', definition_cls=CatapultArrayVariableDefinition)


class CatapultInplaceArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultInplaceArrayVariableDefinition
)


# endregion

# region StructMemberVariable


class CatapultStructMemberVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}]'.format(
type=self.type.name, name=self.member_name, suffix=name_suffix, shape=self.size_cpp()
)


class CatapultStructMemberVariableConverter(StructMemberVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultStructMemberVariableDefinition
)


# endregion

# region StreamVariable


class CatapultStreamVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
if as_reference: # Function parameter
return f'ac_channel<{self.type.name}> &{self.name}{name_suffix}'
else: # Declaration (string name arg not implemented in ac_channel)
return 'ac_channel<{type}> {name}{suffix}/*("{name}")*/'.format(
type=self.type.name, name=self.name, suffix=name_suffix
)


class CatapultStreamVariableConverter(StreamVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Catapult', definition_cls=CatapultStreamVariableDefinition)


# endregion

# region InplaceStreamVariable


class CatapultInplaceStreamVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class CatapultInplaceStreamVariableConverter(InplaceStreamVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultInplaceStreamVariableDefinition
)


# endregion
6 changes: 2 additions & 4 deletions hls4ml/backends/catapult/passes/transform_types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from hls4ml.backends.fpga.fpga_types import (
ACTypeConverter,
from hls4ml.backends.catapult.catapult_types import (
CatapultArrayVariableConverter,
CatapultInplaceArrayVariableConverter,
CatapultInplaceStreamVariableConverter,
CatapultStreamVariableConverter,
HLSTypeConverter,
StaticWeightVariableConverter,
)
from hls4ml.backends.fpga.fpga_types import ACTypeConverter, HLSTypeConverter, StaticWeightVariableConverter
from hls4ml.model.optimizer import GlobalOptimizerPass
from hls4ml.model.types import InplaceTensorVariable

Expand Down
177 changes: 0 additions & 177 deletions hls4ml/backends/fpga/fpga_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,42 +234,6 @@ def definition_cpp(self, name_suffix='', as_reference=False):
# region ArrayVariable


class VivadoArrayVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}]'.format(
type=self.type.name, name=self.name, suffix=name_suffix, shape=self.size_cpp()
)


class QuartusArrayVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}] {pragma}'.format(
type=self.type.name, name=self.name, suffix=name_suffix, shape=self.size_cpp(), pragma=self.pragma
)


class CatapultArrayVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}] /* {pragma} */'.format(
type=self.type.name, name=self.name, suffix=name_suffix, shape=self.size_cpp(), pragma=self.pragma
)


class VivadoInplaceArrayVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class QuartusInplaceArrayVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class CatapultInplaceArrayVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class ArrayVariableConverter:
def __init__(self, type_converter, prefix, definition_cls):
self.type_converter = type_converter
Expand All @@ -287,59 +251,11 @@ def convert(self, tensor_var, pragma='partition'):
return tensor_var


class VivadoArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Vivado', definition_cls=VivadoArrayVariableDefinition)


class QuartusArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Quartus', definition_cls=QuartusArrayVariableDefinition)


class CatapultArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Catapult', definition_cls=CatapultArrayVariableDefinition)


class VivadoInplaceArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Vivado', definition_cls=VivadoInplaceArrayVariableDefinition)


class QuartusInplaceArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Quartus', definition_cls=QuartusInplaceArrayVariableDefinition
)


class CatapultInplaceArrayVariableConverter(ArrayVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultInplaceArrayVariableDefinition
)


# endregion

# region StructMemberVariable


class QuartusStructMemberVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}]'.format(
type=self.type.name, name=self.member_name, suffix=name_suffix, shape=self.size_cpp()
)


class CatapultStructMemberVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
return '{type} {name}{suffix}[{shape}]'.format(
type=self.type.name, name=self.member_name, suffix=name_suffix, shape=self.size_cpp()
)


class StructMemberVariableConverter:
def __init__(self, type_converter, prefix, definition_cls):
self.type_converter = type_converter
Expand All @@ -362,68 +278,11 @@ def convert(self, tensor_var, pragma='partition', struct_name=None):
return tensor_var


class QuartusStructMemberVariableConverter(StructMemberVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Quartus', definition_cls=QuartusStructMemberVariableDefinition
)


class CatapultStructMemberVariableConverter(StructMemberVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultStructMemberVariableDefinition
)


# endregion

# region StreamVariable


class VivadoStreamVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
if as_reference: # Function parameter
return f'hls::stream<{self.type.name}> &{self.name}{name_suffix}'
else: # Declaration
return 'hls::stream<{type}> {name}{suffix}("{name}")'.format(
type=self.type.name, name=self.name, suffix=name_suffix
)


class VivadoInplaceStreamVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class QuartusStreamVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
if as_reference: # Function parameter
return f'stream<{self.type.name}> &{self.name}{name_suffix}'
else: # Declaration
return f'stream<{self.type.name}> {self.name}{name_suffix}'


class QuartusInplaceStreamVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class CatapultStreamVariableDefinition(VariableDefinition):
def definition_cpp(self, name_suffix='', as_reference=False):
if as_reference: # Function parameter
return f'ac_channel<{self.type.name}> &{self.name}{name_suffix}'
else: # Declaration (string name arg not implemented in ac_channel)
return 'ac_channel<{type}> {name}{suffix}/*("{name}")*/'.format(
type=self.type.name, name=self.name, suffix=name_suffix
)


class CatapultInplaceStreamVariableDefinition(VariableDefinition):
def definition_cpp(self):
return f'auto& {self.name} = {self.input_var.name}'


class StreamVariableConverter:
def __init__(self, type_converter, prefix, definition_cls):
self.type_converter = type_converter
Expand All @@ -445,21 +304,6 @@ def convert(self, tensor_var, n_pack=1, depth=0):
return tensor_var


class VivadoStreamVariableConverter(StreamVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Vivado', definition_cls=VivadoStreamVariableDefinition)


class QuartusStreamVariableConverter(StreamVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Quartus', definition_cls=QuartusStreamVariableDefinition)


class CatapultStreamVariableConverter(StreamVariableConverter):
def __init__(self, type_converter):
super().__init__(type_converter=type_converter, prefix='Catapult', definition_cls=CatapultStreamVariableDefinition)


# endregion

# region InplaceStreamVariable
Expand All @@ -479,27 +323,6 @@ def convert(self, tensor_var, n_pack=1, depth=0):
return tensor_var


class VivadoInplaceStreamVariableConverter(InplaceStreamVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Vivado', definition_cls=VivadoInplaceStreamVariableDefinition
)


class QuartusInplaceStreamVariableConverter(InplaceStreamVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Quartus', definition_cls=QuartusInplaceStreamVariableDefinition
)


class CatapultInplaceStreamVariableConverter(InplaceStreamVariableConverter):
def __init__(self, type_converter):
super().__init__(
type_converter=type_converter, prefix='Catapult', definition_cls=CatapultInplaceStreamVariableDefinition
)


# endregion

# region WeightsVariable
Expand Down
6 changes: 2 additions & 4 deletions hls4ml/backends/quartus/passes/transform_types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from hls4ml.backends.fpga.fpga_types import (
ACTypeConverter,
HLSTypeConverter,
from hls4ml.backends.fpga.fpga_types import ACTypeConverter, HLSTypeConverter, StaticWeightVariableConverter
from hls4ml.backends.quartus.quartus_types import (
QuartusArrayVariableConverter,
QuartusInplaceArrayVariableConverter,
QuartusInplaceStreamVariableConverter,
QuartusStreamVariableConverter,
QuartusStructMemberVariableConverter,
StaticWeightVariableConverter,
)
from hls4ml.model.optimizer import GlobalOptimizerPass
from hls4ml.model.types import InplaceTensorVariable
Expand Down
Loading

0 comments on commit ccf17c6

Please sign in to comment.