Skip to content

Commit bb708cc

Browse files
authored
remove padding attribute (#1061)
* remove padding attribute * remote checking the padding attribute in pytorch tests
1 parent 1520518 commit bb708cc

File tree

8 files changed

+20
-50
lines changed

8 files changed

+20
-50
lines changed

hls4ml/backends/catapult/passes/conv_same_pad.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ class InsertZeroPaddingBeforeConv1D(OptimizerPass):
66
name = 'insert_zero_padding_before_conv1d'
77

88
def match(self, node):
9-
is_match = (
10-
isinstance(node, (Conv1D, SeparableConv1D))
11-
and ((node.get_attr('padding') == 'same') or (node.get_attr('padding') == 'causal'))
12-
and node.get_attr('filt_width') != 1
9+
is_match = isinstance(node, (Conv1D, SeparableConv1D)) and (
10+
(node.get_attr('pad_left') != 0) or (node.get_attr('pad_right') != 0)
1311
)
1412
return is_match
1513

@@ -37,7 +35,6 @@ def transform(self, model, node):
3735
}
3836

3937
# Switch Conv1D layer padding to 'valid'
40-
node.set_attr('padding', 'valid')
4138
node.set_attr('pad_left', 0)
4239
node.set_attr('pad_right', 0)
4340
node.set_attr('in_width', out_width)
@@ -54,11 +51,11 @@ class InsertZeroPaddingBeforeConv2D(OptimizerPass):
5451
name = 'insert_zero_padding_before_conv2d'
5552

5653
def match(self, node):
57-
is_match = (
58-
isinstance(node, (Conv2D, SeparableConv2D))
59-
and node.get_attr('padding') == 'same'
60-
and node.get_attr('filt_height') != 1
61-
and node.get_attr('filt_width') != 1
54+
is_match = isinstance(node, (Conv2D, SeparableConv2D)) and (
55+
(node.get_attr('pad_left') != 0)
56+
or (node.get_attr('pad_right') != 0)
57+
or (node.get_attr('pad_top') != 0)
58+
or (node.get_attr('pad_bottom') != 0)
6259
)
6360
return is_match
6461

@@ -93,7 +90,6 @@ def transform(self, model, node):
9390
}
9491

9592
# Switch Conv2D layer padding to 'valid'
96-
node.set_attr('padding', 'valid')
9793
node.set_attr('pad_top', 0)
9894
node.set_attr('pad_bottom', 0)
9995
node.set_attr('pad_left', 0)

hls4ml/backends/vivado/passes/conv_same_pad.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ class InsertZeroPaddingBeforeConv1D(OptimizerPass):
66
name = 'insert_zero_padding_before_conv1d'
77

88
def match(self, node):
9-
is_match = (
10-
isinstance(node, (Conv1D, SeparableConv1D))
11-
and ((node.get_attr('padding') == 'same') or (node.get_attr('padding') == 'causal'))
12-
and node.get_attr('filt_width') != 1
9+
is_match = isinstance(node, (Conv1D, SeparableConv1D)) and (
10+
(node.get_attr('pad_left') != 0) or (node.get_attr('pad_right') != 0)
1311
)
1412
return is_match
1513

@@ -37,7 +35,6 @@ def transform(self, model, node):
3735
}
3836

3937
# Switch Conv1D layer padding to 'valid'
40-
node.set_attr('padding', 'valid')
4138
node.set_attr('pad_left', 0)
4239
node.set_attr('pad_right', 0)
4340
node.set_attr('in_width', out_width)
@@ -54,11 +51,11 @@ class InsertZeroPaddingBeforeConv2D(OptimizerPass):
5451
name = 'insert_zero_padding_before_conv2d'
5552

5653
def match(self, node):
57-
is_match = (
58-
isinstance(node, (Conv2D, SeparableConv2D))
59-
and node.get_attr('padding') == 'same'
60-
and node.get_attr('filt_height') != 1
61-
and node.get_attr('filt_width') != 1
54+
is_match = isinstance(node, (Conv2D, SeparableConv2D)) and (
55+
(node.get_attr('pad_left') != 0)
56+
or (node.get_attr('pad_right') != 0)
57+
or (node.get_attr('pad_top') != 0)
58+
or (node.get_attr('pad_bottom') != 0)
6259
)
6360
return is_match
6461

@@ -93,7 +90,6 @@ def transform(self, model, node):
9390
}
9491

9592
# Switch Conv2D layer padding to 'valid'
96-
node.set_attr('padding', 'valid')
9793
node.set_attr('pad_top', 0)
9894
node.set_attr('pad_bottom', 0)
9995
node.set_attr('pad_left', 0)

hls4ml/converters/keras/convolution.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ def parse_conv1d_layer(keras_layer, input_names, input_shapes, data_reader):
3030
layer['n_filt'] = layer['n_chan'] * layer.get('depth_multiplier')
3131
layer['filt_width'] = keras_layer['config']['kernel_size'][0]
3232
layer['stride_width'] = keras_layer['config']['strides'][0]
33-
layer['padding'] = keras_layer['config']['padding']
3433

3534
(layer['out_width'], layer['pad_left'], layer['pad_right']) = compute_padding_1d(
36-
layer['padding'], layer['in_width'], layer['stride_width'], layer['filt_width']
35+
keras_layer['config']['padding'], layer['in_width'], layer['stride_width'], layer['filt_width']
3736
)
3837

3938
if layer['data_format'] == 'channels_last':
@@ -74,7 +73,6 @@ def parse_conv2d_layer(keras_layer, input_names, input_shapes, data_reader):
7473
layer['filt_width'] = keras_layer['config']['kernel_size'][1]
7574
layer['stride_height'] = keras_layer['config']['strides'][0]
7675
layer['stride_width'] = keras_layer['config']['strides'][1]
77-
layer['padding'] = keras_layer['config']['padding']
7876

7977
(
8078
layer['out_height'],
@@ -84,7 +82,7 @@ def parse_conv2d_layer(keras_layer, input_names, input_shapes, data_reader):
8482
layer['pad_left'],
8583
layer['pad_right'],
8684
) = compute_padding_2d(
87-
layer['padding'],
85+
keras_layer['config']['padding'],
8886
layer['in_height'],
8987
layer['in_width'],
9088
layer['stride_height'],

hls4ml/converters/keras/pooling.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ def parse_pooling_layer(keras_layer, input_names, input_shapes, data_reader):
1515

1616
layer['pool_width'] = keras_layer['config']['pool_size'][0]
1717
layer['stride_width'] = keras_layer['config']['strides'][0]
18-
layer['padding'] = keras_layer['config']['padding']
1918

2019
(layer['n_out'], layer['pad_left'], layer['pad_right']) = compute_padding_1d(
21-
layer['padding'], layer['n_in'], layer['stride_width'], layer['pool_width']
20+
keras_layer['config']['padding'], layer['n_in'], layer['stride_width'], layer['pool_width']
2221
)
2322

2423
if layer['data_format'] == 'channels_last':
@@ -32,7 +31,6 @@ def parse_pooling_layer(keras_layer, input_names, input_shapes, data_reader):
3231
layer['stride_width'] = keras_layer['config']['strides'][1]
3332
layer['pool_height'] = keras_layer['config']['pool_size'][0]
3433
layer['pool_width'] = keras_layer['config']['pool_size'][1]
35-
layer['padding'] = keras_layer['config']['padding']
3634

3735
(
3836
layer['out_height'],
@@ -42,7 +40,7 @@ def parse_pooling_layer(keras_layer, input_names, input_shapes, data_reader):
4240
layer['pad_left'],
4341
layer['pad_right'],
4442
) = compute_padding_2d(
45-
layer['padding'],
43+
keras_layer['config']['padding'],
4644
layer['in_height'],
4745
layer['in_width'],
4846
layer['stride_height'],

hls4ml/converters/pytorch/convolution.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ def parse_conv1d_layer(operation, layer_name, input_names, input_shapes, node, c
3535
else:
3636
padding = class_object.padding
3737

38-
if padding == 0: # No padding, i.e., 'VALID' padding in Keras/Tensorflow
39-
layer['padding'] = 'valid'
40-
else: # Only 'valid' and 'same' padding are available in Keras
41-
layer['padding'] = 'same'
42-
4338
# Ouput info
4439
(layer['out_width'], pad_left, pad_right) = compute_padding_1d_pytorch(
4540
padding, layer['in_width'], layer['stride_width'], layer['filt_width'], layer['dilation']
@@ -84,11 +79,6 @@ def parse_conv2d_layer(operation, layer_name, input_names, input_shapes, node, c
8479
layer['pad_top'] = layer['pad_bottom'] = class_object.padding[0]
8580
layer['pad_left'] = layer['pad_right'] = class_object.padding[1]
8681

87-
if all(x == 0 for x in class_object.padding): # No padding, i.e., 'VALID' padding in Keras/Tensorflow
88-
layer['padding'] = 'valid'
89-
else: # Only 'valid' and 'same' padding are available in Keras
90-
layer['padding'] = 'same'
91-
9282
# Ouput info
9383
(layer['out_height'], layer['out_width'], _, _, _, _) = compute_padding_2d_pytorch(
9484
class_object.padding,

hls4ml/model/optimizer/passes/multi_dense.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def transform(self, model, node):
2020

2121
conv_attrs = {
2222
'data_format': 'channels_last',
23-
'padding': 'valid',
2423
'n_chan': input_shape[-1],
2524
'n_filt': node.get_attr('n_out'),
2625
'weight_data': np.expand_dims(node.get_attr('weight_data'), axis=tuple(range(dim))),

hls4ml/model/optimizer/passes/seperable_to_dw_conv.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class SeperableToDepthwiseAndConv(OptimizerPass):
3333
'data_format',
3434
'depthwise_data',
3535
'depthwise_quantizer',
36-
'padding',
3736
)
3837

3938
_pw_attributes = ('out_width', 'n_filt', 'dilation_width', 'out_height', 'dilation_height', 'data_format', 'use_bias')

test/pytest/test_pytorch_api.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ def test_conv1d(padds, backend, io_type):
277277
assert list(hls_model.get_layers())[conv_index].attributes['n_chan'] == class_object_conv.in_channels
278278
assert list(hls_model.get_layers())[conv_index].attributes['n_filt'] == class_object_conv.out_channels
279279
assert list(hls_model.get_layers())[conv_index].attributes['stride_width'] == class_object_conv.stride[0]
280-
if list(hls_model.get_layers())[conv_index].attributes['padding'] == 'valid':
281-
padding = 0
282-
else:
283-
padding = 1
280+
padding = padds
284281
if io_type == "io_stream" and (backend == "Vivado" or backend == "Vitis") and padds == 1:
285282
padding = 1
286283
padds = 0
@@ -424,10 +421,7 @@ def test_conv2d(padds, backend, io_type):
424421
assert list(hls_model.get_layers())[conv_index].attributes['n_filt'] == class_object_conv.out_channels
425422
assert list(hls_model.get_layers())[conv_index].attributes['stride_width'] == class_object_conv.stride[1]
426423
assert list(hls_model.get_layers())[conv_index].attributes['stride_height'] == class_object_conv.stride[0]
427-
if list(hls_model.get_layers())[conv_index].attributes['padding'] == 'valid':
428-
padding = 0
429-
else:
430-
padding = 1
424+
padding = padds
431425
assert padding == class_object_conv.padding[0]
432426
assert list(hls_model.get_layers())[conv_index].attributes['data_format'] == 'channels_last'
433427

0 commit comments

Comments
 (0)