Skip to content

Vivado LSTM / GRU Bugs - PR #576 #594

Closed
@bo3z

Description

@bo3z

Prerequisites

Please make sure to check off these prerequisites before submitting a bug report.

  • Test that the bug appears on the current version of the master branch. Make sure to include the commit hash of the commit you checked out.
  • Check that the issue hasn't already been reported, by checking the currently open issues.
  • If there are steps to reproduce the problem, make sure to write them down below.
  • If relevant, please include the hls4ml project files, which were created directly before and/or after the bug.

Quick summary

The recently merged PR #576 which added support for LSTM/GRU layers on Vivado has the following issues:

  • Fails to compile - Minor errors, possible solution on this branch
  • GRU Incorrect output for non-zero bias - test cases were included to reproduce this behaviour on the above mentioned branch. Alternatively, the code below will reproduce the error as well.

Details

  • Both GRU & LSTM fail to compile with the current master branch of hls4ml, commit link. This occurs due to an incorrect template instantiation of nnet::product and incorrect function call for streaming LSTM. Both have been addressed on this branch

  • Incorrect GRU output for non-zero bias - when using a random bias initialiser for GRU, the error, when compared to Keras, is quite significant.

Steps to Reproduce

  1. Clone the hls4ml repository
  2. Checkout the master branch, with commit hash: 59ed824
  3. Run conversion on model and verify its output file with code (note, a similar approach should be done with LSTM, as well as io_stream):
input_shape = (5, 8)
X = np.random.rand(50, *input_shape) 

keras_model = Sequential()
keras_model.add(GRU(units=32, input_shape=input_shape, kernel_initializer='lecun_uniform', bias_initializer='lecun_uniform'))
keras_model.compile()

default_precision = 'ap_fixed<32, 16>'
hls_config = hls4ml.utils.config_from_keras_model(keras_model, granularity='name', default_precision=default_precision)
output_dir = 'gru_test'

hls_model = hls4ml.converters.convert_from_keras_model(
                        keras_model, 
                        hls_config=hls_config,
                        output_dir=output_dir, 
                        backend='Vivado',
                        io_type='io_parallel'
)
hls_model.compile()

keras_prediction = keras_model.predict(X)
hls_prediction = hls_model.predict(X)
np.testing.assert_allclose(hls_prediction.flatten(), keras_prediction.flatten(), rtol=0.0, atol=3e-2)

Expected behaviour

  • Model compiles successfully
  • Output is approximately equal to Keras output

Actual behaviour

  • Model fails to compile when using master branch
  • Once compilation issues are fixed, most layers perform correctly, except for GRU with non-zero behaviour

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions