Skip to content
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
wants to merge 61 commits into
base: main
Choose a base branch
from

Conversation

rianbrooksflynn
Copy link

@rianbrooksflynn rianbrooksflynn commented Nov 4, 2024

Description

This PR adds support for Layer Normalization using either Keras or PyTorch with the Vivado backend in io_parallel mode.

This implementation uses a lookup table for inverse square root; the inputs to the lookup table follow a logarithmic distribution for better accuracy.

Tests have been added for both Keras and Pytorch parsing.

Credit is due to @Ethan0Jiang and @LostEcho365 (Zhixing Jiang and Dennis Yin) for their Vivado implementation and Keras parsing support; my contributions were making a change to the inverse square root lookup table implementation, implementing PyTorch support, and adding unit tests. (Here's a link to their pre-print.) The original code authors have given permission for their code to be merged into hls4ml.

Linked issue: #1109

Type of change

  • New feature (non-breaking change which adds functionality)
  • A new research paper code implementation

Tests

Two unit tests added: test/pytest/test_layernorm.py and test/pytest/test_layernorm_pytorch.py

Checklist

  • I have read the guidelines for contributing.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have installed and run pre-commit on the files I edited or added.
  • I have added tests that prove my fix is effective or that my feature works.

@JanFSchulte JanFSchulte added the please test Trigger testing by creating local PR branch label Nov 4, 2024
@JanFSchulte
Copy link
Contributor

The pytest failure is the QKeras tests timing out. There's 299 tests being run in that batch, which I guess is too many. Is there a why to reshuffle the batches to avoid the timeout?

@rianbrooksflynn
Copy link
Author

pre-commit.ci autofix

@jmitrevs jmitrevs added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels Dec 18, 2024
@jmitrevs
Copy link
Contributor

For the "broken" diffs, we should look to see what the whitespace error is and fix it before merging.

@JanFSchulte
Copy link
Contributor

I did a bit of digging and it's not a whitespace problem but rather the file endings are improperly encoded. Likely the person we got the code from was using a Windows machine. @rianbrooksflynn, you can install the dos2unix package and just do dos2unix file.name to fix this.

@JanFSchulte JanFSchulte added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels Jan 6, 2025
@jmitrevs
Copy link
Contributor

jmitrevs commented Jan 9, 2025

We should squash the commits when we merge this.

Copy link
Contributor

@vloncar vloncar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, could use some minor cosmetics.

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};
Copy link
Contributor

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.

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};
Copy link
Contributor

Choose a reason for hiding this comment

The 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 table_t, so we should ensure epsilon is compatible

'table_t', NamedType(name=layer.name + '_table_t', precision=FixedPrecisionType(width=16, integer=6))
)
if 'table_size' not in layer.attributes:
layer.set_attr('table_size', 4096) # table size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These attributes should be set as default in _register_layer_attributes() not here.

Also, is 4096 necessary for this implementation to work? All other tables are 1024.

if layer['epsilon'] <= 0:
raise Exception('epsilon must be positive')

return layer, [shape for shape in input_shapes[0]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should also parse axis parameter to avoid misparsing. Sure, we only support axis=-1 and we can raise exceptions about it, but should be handled.

layer['n_in'] = layer['n_out'] = in_size

if not ((len(input_shapes[0])) == 3):
raise Exception('input size is not currently supported by hls4ml, only dim3 is supported')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is dim3? This sentence could be made a bit nicer :-)

_expected_attributes = [
Attribute('n_in'),
Attribute('seq_len'),
Attribute('epsilon', value_type=float, default=1e-3),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axis too

// Resource reuse info
static const unsigned io_type = io_parallel;
static const unsigned reuse_factor = 1;
static const bool store_weights_in_bram = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we tidy up this example config so that it has the fields that are actually expected (like all types and ranges) and not the ones that are not used (like weights_in_bram)

@pytest.fixture(scope='module')
def model():
model = Sequential()
model.add(LayerNormalization(input_shape=in_shape))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a more complex configuration that ensures we actually parsed correctly and not just used default values from hls4ml/Keras?

# Currently only Vivado/Vitis in io_parallel mode is supported
@pytest.mark.parametrize('backend', ['Vivado', 'Vitis'])
def test_layernorm(model, data, backend):
config = hls4ml.utils.config_from_keras_model(model, granularity='name', backend=backend)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we tweak the config to override ranges and table sizes to validate backend is picking them up correctly?


@pytest.fixture(scope='module')
def model():
model = nn.Sequential(nn.LayerNorm(in_shape[-1]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a more complex configuration that ensures we actually parsed correctly and not just used default values from hls4ml/PyTorch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
please test Trigger testing by creating local PR branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants