-
Notifications
You must be signed in to change notification settings - Fork 402
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
Relu merge optimizer pass #586
Open
oliviaweng
wants to merge
8
commits into
fastmachinelearning:main
Choose a base branch
from
oliviaweng:relu-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6160696
WIP relu merge. Still need to make changes to
oliviaweng 6db0a46
add merged_relu params to conv and dense templates by retrieving them…
oliviaweng 5649532
WIP merge_relu does not catch the right layer ordering pattern becaus…
oliviaweng 6f59e18
Match supported merge relu layers by checking if it's a subclass. Fix…
oliviaweng 965737f
Merge branch 'fastmachinelearning:master' into relu-merge
oliviaweng 361553e
attempt to further restrict the matching function for the relu merge …
oliviaweng 979aed3
Merge branch 'relu-merge' of github.com:oliviaweng/hls4ml into relu-m…
oliviaweng 347a6bd
WIP trying to resolve out_t issues with the mult configs
oliviaweng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from hls4ml.model.optimizer import OptimizerPass | ||
from hls4ml.model.layers import Activation, Dense, Conv2D, Conv2DBatchnorm | ||
|
||
class MergeRelu(OptimizerPass): | ||
def match(self, node): | ||
supported_layers = (Dense, Conv2D, Conv2DBatchnorm) | ||
|
||
is_match = issubclass(node.get_input_node().__class__, supported_layers) | ||
# ReLU layers are of class Activation | ||
is_match = is_match and issubclass(node.__class__, Activation) | ||
return is_match | ||
|
||
def transform(self, model, node): | ||
# Merge ReLU and Convolution/Dense layer | ||
previous_node = node.get_input_node() | ||
previous_node.set_merged_relu(True) # Turn on merged_relu flag for this Conv/Dense layer | ||
if 'Conv2D' in previous_node.__class__.__name__: | ||
if previous_node.get_attr('data_format') == 'channels_last': | ||
shape = [previous_node.attributes['out_height'], previous_node.attributes['out_width'], previous_node.attributes['n_filt']] | ||
dims = ['OUT_HEIGHT_{}'.format(previous_node.index), 'OUT_WIDTH_{}'.format(previous_node.index), 'N_FILT_{}'.format(previous_node.index)] | ||
else: | ||
shape = [previous_node.attributes['n_filt'], previous_node.attributes['out_height'], previous_node.attributes['out_width']] | ||
dims = ['N_FILT_{}'.format(previous_node.index), 'OUT_HEIGHT_{}'.format(previous_node.index), 'OUT_WIDTH_{}'.format(previous_node.index)] | ||
activation_precision, _ = model.config.get_precision(node, var='result') | ||
previous_node.add_output_variable(shape, dims, precision=activation_precision) | ||
if not node.get_output_nodes(): | ||
print("WARNING: {} is the output layer! No rewiring performed.".format(node.name)) | ||
model.remove_node(node, rewire=False) | ||
else: | ||
model.remove_node(node, rewire=True) | ||
return True | ||
elif 'Dense' in previous_node.__class__.__name__: | ||
shape = previous_node.get_input_variable().shape[:] | ||
shape[-1] = previous_node.attributes['n_out'] | ||
if len(shape) > 1: | ||
dims = ['N_LAYER_{}_{}'.format(i, previous_node.index) for i in range(1, len(shape) + 1)] | ||
else: | ||
dims = ['N_LAYER_{}'.format(previous_node.index)] | ||
print('shape: {}'.format(shape)) | ||
print('dims: {}'.format(dims)) | ||
activation_precision, _ = model.config.get_precision(node, var='result') | ||
previous_node.add_output_variable(shape, dims, precision=activation_precision) | ||
if not node.get_output_nodes(): | ||
print("WARNING: {} is the output layer! No rewiring performed.".format(node.name)) | ||
model.remove_node(node, rewire=False) | ||
else: | ||
model.remove_node(node, rewire=True) | ||
return True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. For the missing
CONFIG_T::out_t
error, it looks like thismatch()
function is too generous. Since all activation layers are of subclassActivation
, any Dense/Conv2D layer that is followed by any activation function returns True, which is wrong. I'll look into tightening this up.