Skip to content

Commit d01a30a

Browse files
committed
make Brevitas use FixedPoint quantization, add back bias
1 parent 03c7f38 commit d01a30a

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

2_quantization/2b_brevitas.ipynb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@
8282
"- `QuantLinear` — linear layer with quantized weights (and optionally biases)\n",
8383
"- `QuantReLU` — ReLU that clips and quantizes activations to a fixed bit-width\n",
8484
"\n",
85-
"We use `weight_bit_width=6` and `bit_width=6` throughout, matching the 6-bit precision from Part 2a.\n",
86-
"Unlike regular PyTorch, every forward pass applies the quantization, so the model trains with the actual fixed-point precisions used on the FPGA."
85+
"We use `weight_bit_width=6`, `bias_bit_width=6`, and `bit_width=6` throughout, matching the 6-bit precision from Part 2a.\n",
86+
"Unlike regular PyTorch, every forward pass applies the quantization, so the model trains with the actual fixed-point precisions used on the FPGA. Additionally, we specify FixedPoint quantizers to enforce power of two scales, which are generally better handled by hls4ml.\n",
87+
"Note, these particular quantizers have a maximum bit width of 8-bits, though narrower is allowed."
8788
]
8889
},
8990
{
@@ -93,16 +94,22 @@
9394
"metadata": {},
9495
"outputs": [],
9596
"source": [
97+
"from brevitas.quant import Int8BiasPerTensorFixedPointInternalScaling, Int8WeightPerTensorFixedPoint, Uint8ActPerTensorFixedPoint\n",
98+
"\n",
9699
"class JetTaggerBrevitas(nn.Module):\n",
97100
" def __init__(self):\n",
98101
" super().__init__()\n",
99-
" self.fc1 = qnn.QuantLinear(16, 64, bias=False, weight_bit_width=6)\n",
100-
" self.relu1 = qnn.QuantReLU(bit_width=6)\n",
101-
" self.fc2 = qnn.QuantLinear(64, 32, bias=False, weight_bit_width=6)\n",
102-
" self.relu2 = qnn.QuantReLU(bit_width=6)\n",
103-
" self.fc3 = qnn.QuantLinear(32, 32, bias=False, weight_bit_width=6)\n",
104-
" self.relu3 = qnn.QuantReLU(bit_width=6)\n",
105-
" self.output = qnn.QuantLinear(32, 5, bias=False, weight_bit_width=6)\n",
102+
" self.fc1 = qnn.QuantLinear(16, 64, weight_quant=Int8WeightPerTensorFixedPoint, weight_bit_width=6,\n",
103+
" bias_quant=Int8BiasPerTensorFixedPointInternalScaling, bias_bit_width=6)\n",
104+
" self.relu1 = qnn.QuantReLU(act_quant=Uint8ActPerTensorFixedPoint, bit_width=6)\n",
105+
" self.fc2 = qnn.QuantLinear(64, 32, weight_quant=Int8WeightPerTensorFixedPoint, weight_bit_width=6,\n",
106+
" bias_quant=Int8BiasPerTensorFixedPointInternalScaling, bias_bit_width=6)\n",
107+
" self.relu2 = qnn.QuantReLU(act_quant=Uint8ActPerTensorFixedPoint, bit_width=6)\n",
108+
" self.fc3 = qnn.QuantLinear(32, 32, weight_quant=Int8WeightPerTensorFixedPoint, weight_bit_width=6,\n",
109+
" bias_quant=Int8BiasPerTensorFixedPointInternalScaling, bias_bit_width=6)\n",
110+
" self.relu3 = qnn.QuantReLU(act_quant=Uint8ActPerTensorFixedPoint, bit_width=6)\n",
111+
" self.output = qnn.QuantLinear(32, 5, weight_quant=Int8WeightPerTensorFixedPoint, weight_bit_width=6,\n",
112+
" bias_quant=Int8BiasPerTensorFixedPointInternalScaling, bias_bit_width=6)\n",
106113
"\n",
107114
" def forward(self, x):\n",
108115
" x = self.relu1(self.fc1(x))\n",
@@ -233,7 +240,7 @@
233240
"source": [
234241
"## Convert to hls4ml via QONNX\n",
235242
"\n",
236-
"`config_from_onnx_model` reads the Quant nodes embedded in the QONNX graph and derives per-layer precision automatically. "
243+
"`config_from_onnx_model` reads the Quant nodes embedded in the QONNX graph and derives per-layer precision automatically."
237244
]
238245
},
239246
{
@@ -251,10 +258,6 @@
251258
" backend='Vitis',\n",
252259
")\n",
253260
"\n",
254-
"for layer in config.get('LayerName', {}):\n",
255-
" if layer.startswith('Softmax'):\n",
256-
" config['LayerName'][layer]['Implementation'] = 'legacy'\n",
257-
"\n",
258261
"print('-----------------------------------')\n",
259262
"plotting.print_dict(config)\n",
260263
"print('-----------------------------------')\n",
@@ -408,7 +411,7 @@
408411
],
409412
"metadata": {
410413
"kernelspec": {
411-
"display_name": "hls4ml-tutorial",
414+
"display_name": "Python 3 (ipykernel)",
412415
"language": "python",
413416
"name": "python3"
414417
},
@@ -422,7 +425,7 @@
422425
"name": "python",
423426
"nbconvert_exporter": "python",
424427
"pygments_lexer": "ipython3",
425-
"version": "3.10.16"
428+
"version": "3.12.13"
426429
}
427430
},
428431
"nbformat": 4,

0 commit comments

Comments
 (0)