Skip to content

For encoded convolution, add check for when min_width would have been larger than in_width #610

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

Merged
merged 19 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions hls4ml/backends/fpga/fpga_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ def compute_conv1d_instructions(self, in_W, in_C, kernel_size=3, stride=1, pad=0
else:
min_W = (math.ceil(stride / kernel_size) - 1) * stride + kernel_size

# if the standard min_W is smaller than the in_W, then use unscaled
if min_W > in_W:
min_W = in_W

min_oW = int((min_W - kernel_size) // stride + 1)

out_W = int((in_W - kernel_size) // stride + 1)
Expand Down Expand Up @@ -442,11 +446,17 @@ def compute_conv2d_instructions(self, in_H, in_W, in_C, kernel_size=3, stride=1,
else:
min_H = (math.ceil(stride_height / kernel_height) - 1) * stride_height + kernel_height

if min_H > in_H:
min_H = in_H

if kernel_width >= stride_width:
min_W = (math.ceil(kernel_width / stride_width) - 1) * stride_width + kernel_width
else:
min_W = (math.ceil(stride_width / kernel_width) - 1) * stride_width + kernel_width

if min_W > in_W:
min_W = in_W

min_oH = int((min_H - kernel_height) // stride_height + 1)
min_oW = int((min_W - kernel_width) // stride_width + 1)

Expand All @@ -461,9 +471,25 @@ def compute_conv2d_instructions(self, in_H, in_W, in_C, kernel_size=3, stride=1,
min_W += 1

# Let's hardcode a few common cases:
if kernel_height == 1 and kernel_width == 1 and stride == 1 and scaled_H == in_H and scaled_W == in_W:
if (
min_H == 1
and min_W == 1
and kernel_height == 1
and kernel_width == 1
and stride == 1
and scaled_H == in_H
and scaled_W == in_W
):
return (1, 1, map(str, [1]))
if kernel_height == 3 and kernel_width == 3 and stride == 1 and scaled_H == in_H and scaled_W == in_W:
if (
min_H == 5
and min_W == 5
and kernel_height == 3
and kernel_width == 3
and stride == 1
and scaled_H == in_H
and scaled_W == in_W
):
return (
5,
5,
Expand Down Expand Up @@ -498,7 +524,15 @@ def compute_conv2d_instructions(self, in_H, in_W, in_C, kernel_size=3, stride=1,
],
),
)
if kernel_height == 5 and kernel_width == 5 and stride == 1 and scaled_H == in_H and scaled_W == in_W:
if (
min_H == 9
and min_W == 9
and kernel_height == 5
and kernel_width == 5
and stride == 1
and scaled_H == in_H
and scaled_W == in_W
):
return (
9,
9,
Expand Down
17 changes: 11 additions & 6 deletions hls4ml/backends/vivado/passes/conv_stream.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from hls4ml.model.layers import Conv1D, Conv2D, SeparableConv1D, SeparableConv2D
from hls4ml.model.optimizer import OptimizerPass
from hls4ml.model.layers import Conv1D, SeparableConv1D, Conv2D, SeparableConv2D


class GenerateConvStreamingInstructions(OptimizerPass):
''' Generates the instructions for streaming implementation of CNNs '''
'''Generates the instructions for streaming implementation of CNNs'''

def match(self, node):
return isinstance(node, (Conv1D, SeparableConv1D, Conv2D, SeparableConv2D))

Expand All @@ -13,19 +15,21 @@ def transform(self, model, node):
elif '2D' in node_class:
self._generate_2d_instructions(node)
else:
raise Exception('Cannot generate instructions for node {} ({})'.format(node.name, node_class))
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'))
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')

Expand All @@ -36,7 +40,8 @@ def _generate_2d_instructions(self, node):
node.get_input_variable().shape[1],
node.get_input_variable().shape[2],
node.get_attr('filt_height'),
node.get_attr('stride_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)
Expand Down
Loading