Skip to content

Commit 6a05587

Browse files
authored
Fix pytorch upsample parsing (fastmachinelearning#1186)
* Fix parsing Pytorch Upsample layer in 2D * Add test case for Upsample to have a layer after to ensure proper parsing
1 parent 9857da1 commit 6a05587

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

hls4ml/converters/pytorch/reshape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def handle_upsample(operation, layer_name, input_names, input_shapes, node, clas
152152
layer['out_height'] = int(layer['in_height'] * scale_height)
153153
layer['out_width'] = int(layer['in_width'] * scale_width)
154154

155-
output_shape = [layer['n_chan'], layer['out_height'], layer['out_width']]
155+
output_shape = [input_shapes[0][0], layer['n_chan'], layer['out_height'], layer['out_width']]
156156
else:
157157
raise Exception(f'Parsing "Upsample" with {len(input_shape)}-dimensional tensors is not yet supported.')
158158

test/pytest/test_upsampling_pytorch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ class Upsample1DModel(nn.Module):
3333
def __init__(self):
3434
super().__init__()
3535
self.upsample = nn.Upsample(scale_factor=2)
36+
self.relu = nn.ReLU()
3637

3738
def forward(self, x):
38-
return self.upsample(x)
39+
return self.relu(self.upsample(x))
3940

4041

4142
class Upsample2DModel(nn.Module):
4243
def __init__(self):
4344
super().__init__()
4445
# this scale_factor tests proper output shape calculation with fractional scaling and parsing per-axis scales
4546
self.upsample = nn.UpsamplingNearest2d(scale_factor=(1, 2.4)) # Would also work with Upsample(mode='nearest')
47+
self.relu = nn.ReLU()
4648

4749
def forward(self, x):
48-
return self.upsample(x)
50+
return self.relu(self.upsample(x))
4951

5052

5153
@pytest.mark.parametrize('io_type', ['io_stream', 'io_parallel'])

0 commit comments

Comments
 (0)