|
| 1 | +================================== |
| 2 | +Loading weights from external BRAM |
| 3 | +================================== |
| 4 | + |
| 5 | +.. note:: |
| 6 | + This feature is being evaluated for re-implementation. We welcome feedback from users how to make the implementation more flexible. |
| 7 | + |
| 8 | +``hls4ml`` can optionally store weights in BRAMs external to the design. This is supported in Vivado/Vitis and Catapult backends. It is the responsibility of the user to ensure the weights are properly loaded during the operation of the design. |
| 9 | + |
| 10 | +The feature works as a threshold, exposed through a ``BramFactor`` config parameter. Layers with more weights above the threshold will be exposed as BRAM interface. Consider the following code: |
| 11 | + |
| 12 | +.. code-block:: Python |
| 13 | +
|
| 14 | + model = tf.keras.models.Sequential() |
| 15 | + model.add(Dense(10, activation="relu", input_shape=(12,), name="dense_1")) |
| 16 | + model.add(Dense(20, activation="relu", name="dense_2")) |
| 17 | + model.add(Dense(5, activation="softmax", name="dense_3")) |
| 18 | + model.compile(optimizer='adam', loss='mse') |
| 19 | +
|
| 20 | + config = hls4ml.utils.config_from_keras_model(model) |
| 21 | + config["Model"]["Strategy"] = "Resource" |
| 22 | + config["Model"]["BramFactor"] = 100 |
| 23 | +
|
| 24 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 25 | + model, hls_config=config, output_dir=output_dir, io_type=io_type, backend=backend |
| 26 | + ) |
| 27 | +
|
| 28 | +Having set ``BramFactor=100``, only layers with more than 100 weights will be exposed as external BRAM, in this case layers ``dense_1`` and ``dense_2``. ``BramFactor`` can currently be only set at the model level. The generated code will now have weights as part of the interface. |
| 29 | + |
| 30 | +.. code-block:: C++ |
| 31 | + |
| 32 | + void myproject( |
| 33 | + hls::stream<input_t> &dense_1_input, |
| 34 | + hls::stream<result_t> &layer7_out, |
| 35 | + model_default_t w2[120], |
| 36 | + model_default_t w4[200] |
| 37 | + ) { |
| 38 | + #pragma HLS INTERFACE axis port=dense_1_input,layer7_out |
| 39 | + #pragma HLS INTERFACE bram port=w2,w4 |
| 40 | + ... |
| 41 | + |
| 42 | +When integrating the design, users can use the exposed interface to implement weight reloading scheme. |
0 commit comments