From 27810d071e1697db8f25074be82e437fd8571d7c Mon Sep 17 00:00:00 2001 From: Jan-Frederik Schulte Date: Tue, 16 Sep 2025 16:29:53 -0400 Subject: [PATCH 1/3] manually restrict precision of dense layer ouputs before a softmax --- part1_getting_started.ipynb | 2 +- part2_advanced_config.ipynb | 7 +++++-- part3_compression.ipynb | 2 +- part4.1_HG_quantization.ipynb | 2 +- part4_quantization.ipynb | 2 +- part6_cnns.ipynb | 20 ++++++++++++-------- part7a_bitstream.ipynb | 2 +- part8_symbolic_regression.ipynb | 2 +- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/part1_getting_started.ipynb b/part1_getting_started.ipynb index 955cae81..930fe537 100644 --- a/part1_getting_started.ipynb +++ b/part1_getting_started.ipynb @@ -399,7 +399,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part2_advanced_config.ipynb b/part2_advanced_config.ipynb index 8701cee5..974b51b9 100644 --- a/part2_advanced_config.ipynb +++ b/part2_advanced_config.ipynb @@ -148,7 +148,9 @@ "metadata": {}, "source": [ "## Customize\n", - "Let's just try setting the precision of the first layer weights to something more narrow than 16 bits. Using fewer bits can save resources in the FPGA. After inspecting the profiling plot above, let's try 8 bits with 1 integer bit.\n", + "Let's just try setting the precision of the first layer weights to something more narrow than 16 bits. Using fewer bits can save resources in the FPGA. After inspecting the profiling plot above, let's try 8 bits with 2 integer bit.\n", + "\n", + "**NOTE** Using `auto` precision can lead to undesired side effects. In case of this model, the bit width used for the output of the last fully connected layer is larger than can be reasonably represented with the look-up table in the softmax implementation. We therefore need to restrict it by hand to achieve proper results. \n", "\n", "Then create a new `HLSModel`, and display the profiling with the new config. This time, just display the weight profile by not providing any data '`X`'. Then create the `HLSModel` and display the architecture. Notice the box around the weights of the first layer reflects the different precision." ] @@ -160,6 +162,7 @@ "outputs": [], "source": [ "config['LayerName']['fc1']['Precision']['weight'] = 'ap_fixed<8,2>'\n", + "config['LayerName']['output']['Precision']['result'] = 'fixed<16,6,RND,SAT>'\n", "hls_model = hls4ml.converters.convert_from_keras_model(\n", " model, hls_config=config, output_dir='model_1/hls4ml_prj_2', part='xcu250-figd2104-2L-e'\n", ")\n", @@ -395,7 +398,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part3_compression.ipynb b/part3_compression.ipynb index 368882bd..97f38088 100644 --- a/part3_compression.ipynb +++ b/part3_compression.ipynb @@ -312,7 +312,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part4.1_HG_quantization.ipynb b/part4.1_HG_quantization.ipynb index 4f24acd3..01778f39 100644 --- a/part4.1_HG_quantization.ipynb +++ b/part4.1_HG_quantization.ipynb @@ -474,7 +474,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part4_quantization.ipynb b/part4_quantization.ipynb index 80f64c57..830ca203 100644 --- a/part4_quantization.ipynb +++ b/part4_quantization.ipynb @@ -397,7 +397,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part6_cnns.ipynb b/part6_cnns.ipynb index 04e2236f..94921ca3 100644 --- a/part6_cnns.ipynb +++ b/part6_cnns.ipynb @@ -658,7 +658,9 @@ "\n", "![alt text](images/conv2d_animation.gif \"The implementation of convolutional layers in hls4ml.\")\n", "\n", - "Lastly, we will use ``['Strategy'] = 'Latency'`` for all the layers in the hls4ml configuration. If one layer would have >4096 elements, we sould set ``['Strategy'] = 'Resource'`` for that layer, or increase the reuse factor by hand. You can find examples of how to do this below." + "Lastly, we will use ``['Strategy'] = 'Latency'`` for all the layers in the hls4ml configuration. If one layer would have >4096 elements, we sould set ``['Strategy'] = 'Resource'`` for that layer, or increase the reuse factor by hand. You can find examples of how to do this below.\n", + "\n", + "**NOTE** Using `auto` precision can lead to undesired side effects. In case of this model, the bit width used for the output of the last fully connected layer is larger than can be reasonably represented with the look-up table in the softmax implementation. We therefore need to restrict it by hand to achieve proper results.\n" ] }, { @@ -674,7 +676,7 @@ "hls_config = hls4ml.utils.config_from_keras_model(\n", " model, granularity='name', backend='Vitis', default_precision='ap_fixed<16,6>'\n", ")\n", - "\n", + "hls_config['LayerName']['output_dense']['Precision']['result'] = 'fixed<16,6,RND,SAT>'\n", "plotting.print_dict(hls_config)\n", "\n", "\n", @@ -721,12 +723,13 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": false, - "editable": false - }, + "metadata": {}, "source": [ - "The colored boxes are the distribution of the weights of the model, and the gray band illustrates the numerical range covered by the chosen fixed point precision. As we configured, this model uses a precision of ``ap_fixed<16,6>`` for all layers of the model. Let's now build our QKeras model" + "The colored boxes are the distribution of the weights of the model, and the gray band illustrates the numerical range covered by the chosen fixed point precision. As we configured, this model uses a precision of ``ap_fixed<16,6>`` for the weights and biases of all layers of the model. \n", + "\n", + "Let's now build our QKeras model. \n", + "\n", + "**NOTE** Using `auto` precision can lead to undesired side effects. In case of this model, the bit width used for the output of the last fully connected layer is larger than can be reasonably represented with the look-up table in the softmax implementation. We therefore need to restrict it by hand to achieve proper results." ] }, { @@ -737,6 +740,7 @@ "source": [ "# Then the QKeras model\n", "hls_config_q = hls4ml.utils.config_from_keras_model(qmodel, granularity='name', backend='Vitis')\n", + "hls_config_q['LayerName']['output_dense']['Precision']['result'] = 'fixed<16,6,RND,SAT>'\n", "\n", "plotting.print_dict(hls_config_q)\n", "\n", @@ -1315,7 +1319,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part7a_bitstream.ipynb b/part7a_bitstream.ipynb index 32fdcdc4..6beef064 100644 --- a/part7a_bitstream.ipynb +++ b/part7a_bitstream.ipynb @@ -282,7 +282,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/part8_symbolic_regression.ipynb b/part8_symbolic_regression.ipynb index 2f6a98f7..c46578e3 100644 --- a/part8_symbolic_regression.ipynb +++ b/part8_symbolic_regression.ipynb @@ -492,7 +492,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.14" } }, "nbformat": 4, From 44c5bfccba3738d38ca09d91dc03284263f67e7b Mon Sep 17 00:00:00 2001 From: Jan-Frederik Schulte Date: Tue, 16 Sep 2025 16:58:47 -0400 Subject: [PATCH 2/3] fix notebook version to allow extensions to be installed --- environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/environment.yml b/environment.yml index d3465c13..b66167e9 100644 --- a/environment.yml +++ b/environment.yml @@ -3,6 +3,7 @@ channels: - conda-forge dependencies: - python=3.10.16 + - notebook==6.4.12 - jupyter_contrib_nbextensions - jupyterhub - jupyter-book From 808493576c3fd9e82ae8b7d52f9cd7573857e63b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 16 Sep 2025 20:59:40 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit hooks --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index b66167e9..b49f482c 100644 --- a/environment.yml +++ b/environment.yml @@ -3,7 +3,7 @@ channels: - conda-forge dependencies: - python=3.10.16 - - notebook==6.4.12 + - notebook==6.4.12 - jupyter_contrib_nbextensions - jupyterhub - jupyter-book