Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check for conv implementation #1155

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
57 changes: 26 additions & 31 deletions hls4ml/backends/catapult/passes/conv_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class GenerateConvStreamingInstructions(OptimizerPass):
'''Generates the instructions for streaming implementation of CNNs'''

def match(self, node):
return isinstance(node, (Conv1D, SeparableConv1D, Conv2D, SeparableConv2D))
is_match = (
isinstance(node, (Conv1D, SeparableConv1D, Conv2D, SeparableConv2D))
and node.model.config.get_config_value('IOType').casefold() == 'io_stream'
jmitrevs marked this conversation as resolved.
Show resolved Hide resolved
and node.get_attr('implementation').casefold() == 'encoded'
)
return is_match

def transform(self, model, node):
node_class = node.__class__.__name__
Expand All @@ -18,35 +23,25 @@ def transform(self, model, node):
raise Exception(f'Cannot generate instructions for node {node.name} ({node_class})')

def _generate_1d_instructions(self, node):
if node.model.config.get_config_value('IOType') == 'io_stream':
min_w, instructions = node.model.config.backend.compute_conv1d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_attr('filt_width'),
node.get_attr('stride_width'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
else:
# these are unused; just put dummy values
node.set_attr('min_width', node.get_attr('in_width'))
node.set_attr('instructions', '0')
min_w, instructions = node.model.config.backend.compute_conv1d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_attr('filt_width'),
node.get_attr('stride_width'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)

def _generate_2d_instructions(self, node):
if node.model.config.get_config_value('IOType') == 'io_stream':
min_h, min_w, instructions = node.model.config.backend.compute_conv2d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_input_variable().shape[2],
node.get_attr('filt_height'),
node.get_attr('stride_height'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_height', min_h)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
else:
node.set_attr('min_height', node.get_attr('in_height'))
node.set_attr('min_width', node.get_attr('in_width'))
node.set_attr('instructions', '0')
min_h, min_w, instructions = node.model.config.backend.compute_conv2d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_input_variable().shape[2],
node.get_attr('filt_height'),
node.get_attr('stride_height'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_height', min_h)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
17 changes: 17 additions & 0 deletions hls4ml/backends/catapult/passes/convolution_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def format(self, node):
else:
params['fill_fn'] = 'FillConv1DBuffer'

if (
node.get_attr('implementation').casefold() == 'linebuffer'
or node.model.config.get_config_value('IOType').casefold() == 'io_parallel'
):
# these are unused; just put dummy values
params['min_width'] = node.get_attr('in_width')
params['instructions'] = '0'

conv_config = self.template.format(**params)

mult_params = self._default_config_params(node)
Expand Down Expand Up @@ -210,6 +218,15 @@ def format(self, node):
else:
params['fill_fn'] = 'FillConv2DBuffer'

if (
jmitrevs marked this conversation as resolved.
Show resolved Hide resolved
node.get_attr('implementation').casefold() == 'linebuffer'
or node.model.config.get_config_value('IOType').casefold() == 'io_parallel'
):
# these are unused; just put dummy values
params['min_height'] = node.get_attr('in_height')
params['min_width'] = node.get_attr('in_width')
params['instructions'] = '0'

conv_config = self.template.format(**params)

mult_params = self._default_config_params(node)
Expand Down
57 changes: 26 additions & 31 deletions hls4ml/backends/vivado/passes/conv_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class GenerateConvStreamingInstructions(OptimizerPass):
'''Generates the instructions for streaming implementation of CNNs'''

def match(self, node):
return isinstance(node, (Conv1D, SeparableConv1D, Conv2D, SeparableConv2D))
is_match = (
isinstance(node, (Conv1D, SeparableConv1D, Conv2D, SeparableConv2D))
and node.model.config.get_config_value('IOType').casefold() == 'io_stream'
and node.get_attr('implementation').casefold() == 'encoded'
)
return is_match

def transform(self, model, node):
node_class = node.__class__.__name__
Expand All @@ -18,35 +23,25 @@ def transform(self, model, node):
raise Exception(f'Cannot generate instructions for node {node.name} ({node_class})')

def _generate_1d_instructions(self, node):
if node.model.config.get_config_value('IOType') == 'io_stream':
min_w, instructions = node.model.config.backend.compute_conv1d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_attr('filt_width'),
node.get_attr('stride_width'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
else:
# these are unused; just put dummy values
node.set_attr('min_width', node.get_attr('in_width'))
node.set_attr('instructions', '0')
min_w, instructions = node.model.config.backend.compute_conv1d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_attr('filt_width'),
node.get_attr('stride_width'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)

def _generate_2d_instructions(self, node):
if node.model.config.get_config_value('IOType') == 'io_stream':
min_h, min_w, instructions = node.model.config.backend.compute_conv2d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_input_variable().shape[2],
node.get_attr('filt_height'),
node.get_attr('stride_height'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_height', min_h)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
else:
node.set_attr('min_height', node.get_attr('in_height'))
node.set_attr('min_width', node.get_attr('in_width'))
node.set_attr('instructions', '0')
min_h, min_w, instructions = node.model.config.backend.compute_conv2d_instructions(
node.get_input_variable().shape[0],
node.get_input_variable().shape[1],
node.get_input_variable().shape[2],
node.get_attr('filt_height'),
node.get_attr('stride_height'),
)
instructions_str = ','.join(str(i) for i in instructions)
node.set_attr('min_height', min_h)
node.set_attr('min_width', min_w)
node.set_attr('instructions', instructions_str)
17 changes: 17 additions & 0 deletions hls4ml/backends/vivado/passes/convolution_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def format(self, node):
else:
params['conv_fn'] = 'Conv1DResource'

if (
node.get_attr('implementation').casefold() == 'linebuffer'
or node.model.config.get_config_value('IOType').casefold() == 'io_parallel'
):
# these are unused; just put dummy values
params['min_width'] = node.get_attr('in_width')
params['instructions'] = '0'

conv_config = self.template.format(**params)

mult_params = self._default_config_params(node)
Expand Down Expand Up @@ -239,6 +247,15 @@ def format(self, node):
else:
params['fill_fn'] = 'FillConv2DBuffer'

if (
node.get_attr('implementation').casefold() == 'linebuffer'
or node.model.config.get_config_value('IOType').casefold() == 'io_parallel'
):
# these are unused; just put dummy values
params['min_height'] = node.get_attr('in_height')
params['min_width'] = node.get_attr('in_width')
params['instructions'] = '0'

conv_config = self.template.format(**params)

mult_params = self._default_config_params(node)
Expand Down
4 changes: 4 additions & 0 deletions test/pytest/test_conv1d_narrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def model():
('io_stream', 'resource', 'Encoded'),
('io_stream', 'latency', 'LineBuffer'),
('io_stream', 'resource', 'LineBuffer'),
('io_parallel', 'resource', 'Encoded'),
('io_parallel', 'latency', 'Encoded'),
('io_parallel', 'resource', 'LineBuffer'),
('io_parallel', 'latency', 'LineBuffer'),
],
)
@pytest.mark.filterwarnings("error")
Expand Down
4 changes: 4 additions & 0 deletions test/pytest/test_conv2d_narrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def model():
('io_stream', 'resource', 'Encoded'),
('io_stream', 'latency', 'LineBuffer'),
('io_stream', 'resource', 'LineBuffer'),
('io_parallel', 'resource', 'Encoded'),
('io_parallel', 'latency', 'Encoded'),
('io_parallel', 'resource', 'LineBuffer'),
('io_parallel', 'latency', 'LineBuffer'),
],
)
@pytest.mark.filterwarnings("error")
Expand Down
Loading