-
Notifications
You must be signed in to change notification settings - Fork 424
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
Add LayerNorm support for Vivado #1110
Open
rianbrooksflynn
wants to merge
61
commits into
fastmachinelearning:main
Choose a base branch
from
rianbrooksflynn:layernorm
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 54 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
c4c818b
paser_mht
Ethan0Jiang 3ee64d1
change parser and modify keras_to_hls
Ethan0Jiang 5626a1a
IR_mutihead_attention
Ethan0Jiang d51f8a9
IR done
Ethan0Jiang 89025a2
create mha file in template
Ethan0Jiang d76cf60
mha .h file dummy algo
Ethan0Jiang 56811de
config of mha
Ethan0Jiang 45cd493
update mha config
Ethan0Jiang 1402f48
dummy mha
Ethan0Jiang 430b9ea
add transpose into mha
Ethan0Jiang 97f3e8d
projection_of_qkv_in_mha
Ethan0Jiang 52cc7e8
mha_first_draft
Ethan0Jiang 3961f97
able to predict model correct
Ethan0Jiang 3533999
delete some unnassary comments
Ethan0Jiang d2f0df6
delete comments
Ethan0Jiang 6aaa5ed
resource strategy of transformer
Ethan0Jiang 3b7a288
change sm lagacy
Ethan0Jiang 130092d
update MHA, optimized
Ethan0Jiang 09b0ba0
support resource
Ethan0Jiang b49fffd
update
Ethan0Jiang 5324a11
dense_muti_dim_support
Ethan0Jiang bf8c788
parallel execute dense
Ethan0Jiang b6be2c4
updates
Ethan0Jiang 2472b7d
add_layerNorm_support
Ethan0Jiang 97e71e9
MHA updated
Ethan0Jiang 5ed4a76
LayerNorm_bug_fix
Ethan0Jiang 5d28f58
update bit precision
Ethan0Jiang 2fc68d0
config update
Ethan0Jiang b5c95cf
add some comment
Ethan0Jiang 3b8aa8d
run pre-commit
JanFSchulte d28b24c
Added support on QMultiHeadAttention, QLayerNormalization, and quanti…
LostEcho365 de79bb9
updated on hls4ml transformer
LostEcho365 6c23326
trying to clean the diff
JanFSchulte 20a0199
trying to clean the diff
JanFSchulte ddccde2
trying to clean the diff
afbe00b
trying to clean the diff
dedf96c
trying to clean the diff
a9de9cb
undo vhdl -> verilog change
49313d3
halfway working layernorm + test
1156ba5
layernorm is now pretty functional
17e0048
layernorm on pytorch also
63891fd
minor cleanup
8dccac6
more cleanup, pre-commit
595cc71
test for mha which kinda works maybe if you squint
5f3ec00
multihead attention working on keras and pytorch
5697334
fiddly precision / accuracy changes for layernorm
d2e27b8
Merge remote-tracking branch 'upstream/main' into transformer
rianbrooksflynn a149f2e
fix lookup table and label loops
rianbrooksflynn 552fa83
remove dense_seq
rianbrooksflynn 69f26bc
Merge remote-tracking branch 'upstream/main' into transformer
rianbrooksflynn be5f5a4
undo qkeras changes
rianbrooksflynn adf7356
fix merge conflict residue
rianbrooksflynn 8437581
Merge remote-tracking branch 'upstream/main' into transformer
rianbrooksflynn 39ab36c
remove non-layernorm changes
rianbrooksflynn b5b82e2
change to uniform LUT and fix precision
rianbrooksflynn f3ff077
Merge remote-tracking branch 'upstream/main' into layernorm
rianbrooksflynn 0f08e7a
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] 21049e7
Merge remote-tracking branch 'upstream/main' into layernorm
rianbrooksflynn cbd88bd
fix encodings issue with dos2unix
rianbrooksflynn 0fe0ec3
Merge branch 'main' into layernorm
JanFSchulte 0d96cb0
add Vitis as another tested backend
rianbrooksflynn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from hls4ml.backends.backend import get_backend | ||
from hls4ml.backends.template import FunctionCallTemplate, LayerConfigTemplate | ||
from hls4ml.model.layers import Activation, BatchNormalization, Dense, HardActivation, ParametrizedActivation, PReLU, Softmax | ||
from hls4ml.model.layers import ( | ||
Activation, | ||
BatchNormalization, | ||
Dense, | ||
HardActivation, | ||
LayerNormalization, | ||
ParametrizedActivation, | ||
PReLU, | ||
Softmax, | ||
) | ||
from hls4ml.model.optimizer.passes.hgq_proxy_model import UnaryLUT | ||
|
||
# Dense templates | ||
|
@@ -119,6 +128,59 @@ def format(self, node): | |
return self.template.format(**params) | ||
|
||
|
||
# LayerNormalization templates | ||
|
||
layernorm_config_template = """struct config{index} : nnet::layernorm_config {{ | ||
static const unsigned n_in = {n_in}; | ||
static const unsigned seq_len = {seq_len}; | ||
static const unsigned table_size = {table_size}; | ||
static constexpr double table_range = {table_range}; | ||
static const unsigned io_type = nnet::{iotype}; | ||
static const unsigned reuse_factor = {reuse}; | ||
static const bool store_weights_in_bram = false; | ||
static constexpr double epsilon = {epsilon}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, this will only ever be used to store values in |
||
typedef {bias_t.name} bias_t; | ||
typedef {scale_t.name} scale_t; | ||
typedef {mean_t.name} mean_t; | ||
typedef {table_t.name} table_t; | ||
template<class x_T, class y_T> | ||
using product = nnet::product::{product_type}<x_T, y_T>; | ||
}};\n""" | ||
|
||
layernorm_function_template = 'nnet::layernormalize<{input_t}, {output_t}, {config}>({input}, {output}, {scale}, {bias});' | ||
|
||
layernorm_include_list = ['nnet_utils/nnet_layernorm.h'] | ||
|
||
|
||
class LayerNormalizationConfigTemplate(LayerConfigTemplate): | ||
def __init__(self): | ||
super().__init__(LayerNormalization) | ||
self.template = layernorm_config_template | ||
|
||
def format(self, node): | ||
params = self._default_config_params(node) | ||
params['n_in'] = node.get_input_variable().size_cpp() | ||
params['seq_len'] = node.get_attr('seq_len') | ||
params['product_type'] = get_backend('vivado').product_type( | ||
node.get_input_variable().type.precision, node.get_weights('scale').type.precision | ||
) | ||
|
||
return self.template.format(**params) | ||
|
||
|
||
class LayerNormalizationFunctionTemplate(FunctionCallTemplate): | ||
def __init__(self): | ||
super().__init__(LayerNormalization, include_header=layernorm_include_list) | ||
self.template = layernorm_function_template | ||
|
||
def format(self, node): | ||
params = self._default_function_params(node) | ||
params['scale'] = node.get_weights('scale').name | ||
params['bias'] = node.get_weights('bias').name | ||
|
||
return self.template.format(**params) | ||
|
||
|
||
# Activation templates | ||
|
||
activ_config_template = """struct {type}_config{index} : nnet::activ_config {{ | ||
|
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
Oops, something went wrong.
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.
Any reason this is a double? It is not used as such, and breaks Vivado synthesis.