Skip to content

Commit 896951a

Browse files
committed
Add docs on BramFactor
1 parent e69a392 commit 896951a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

docs/advanced/bramfactor.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
advanced/fifo_depth
5050
advanced/extension
5151
advanced/model_optimization
52+
advanced/bramfactor
5253

5354
.. toctree::
5455
:hidden:

0 commit comments

Comments
 (0)