Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions hls4ml/model/optimizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'fuse_batch_normalization',
'replace_multidimensional_dense_with_conv',
'enforce_proxy_model_embedded_config',
'absorb_reshape_into_input',
'bit_exact',
'fuse_fixed_point_quantizer',
'fix_input_precision',
Expand Down
28 changes: 28 additions & 0 deletions hls4ml/model/optimizer/passes/absorb_reshape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from hls4ml.model.layers import Input, Reshape
from hls4ml.model.optimizer import OptimizerPass


class AbsorbReshapeIntoInput(OptimizerPass):
def match(self, node):
# Looking for a Reshape layer whose input is an Input layer
if isinstance(node, Reshape):
inp_node = node.get_input_node()
if isinstance(inp_node, Input):
return True
return False

def transform(self, model, node):
# node == Reshape layer that matched
inp_node = node.get_input_node()
out_nodes = node.get_output_nodes()
if len(out_nodes) > 1:
raise Exception('Reshape node has multiple outputs')

target_shape = node.get_attr('target_shape')

input_output_var = inp_node.get_output_variable()
input_output_var.shape = target_shape

model.remove_node(node)

return True # because we modified the graph