|
| 1 | +High Granularity Quantization (HGQ) |
| 2 | + |
| 3 | +[](https://calad0i.github.io/HGQ/) |
| 4 | +[](https://badge.fury.io/py/hgq) |
| 5 | +[](https://arxiv.org/abs/2405.00645) |
| 6 | + |
| 7 | +Text taken and adopted from the HGQ [README.md](https://github.com/calad0i/HGQ/blob/master/README.md). |
| 8 | + |
| 9 | +[High Granularity Quantization (HGQ)](https://github.com/calad0i/HGQ/) is a library that performs gradient-based automatic bitwidth optimization and quantization-aware training algorithm for neural networks to be deployed on FPGAs. By laveraging gradients, it allows for bitwidth optimization at arbitrary granularity, up to per-weight and per-activation level. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Conversion of models made with HGQ library is fully supported. The HGQ models are first converted to proxy model format, which can then be parsed by hls4ml bit-accurately. Below is an example of how to create a model with HGQ and convert it to hls4ml model. |
| 14 | + |
| 15 | +```python |
| 16 | + import keras |
| 17 | + from HGQ.layers import HDense, HDenseBatchNorm, HQuantize |
| 18 | + from HGQ import ResetMinMax, FreeBOPs |
| 19 | + |
| 20 | + model = keras.models.Sequential([ |
| 21 | + HQuantize(beta=1.e-5), |
| 22 | + HDenseBatchNorm(32, beta=1.e-5, activation='relu'), |
| 23 | + HDenseBatchNorm(32, beta=1.e-5, activation='relu'), |
| 24 | + HDense(10, beta=1.e-5), |
| 25 | + ]) |
| 26 | + |
| 27 | + opt = keras.optimizers.Adam(learning_rate=0.001) |
| 28 | + loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True) |
| 29 | + model.compile(optimizer=opt, loss=loss, metrics=['accuracy']) |
| 30 | + callbacks = [ResetMinMax(), FreeBOPs()] |
| 31 | + |
| 32 | + model.fit(..., callbacks=callbacks) |
| 33 | + |
| 34 | + from HGQ import trace_minmax, to_proxy_model |
| 35 | + from hls4ml.converters import convert_from_keras_model |
| 36 | + |
| 37 | + trace_minmax(model, x_train, cover_factor=1.0) |
| 38 | + proxy = to_proxy_model(model, aggressive=True) |
| 39 | + |
| 40 | + model_hls = convert_from_keras_model( |
| 41 | + proxy, |
| 42 | + backend='vivado', |
| 43 | + output_dir=..., |
| 44 | + part=... |
| 45 | + ) |
| 46 | +``` |
| 47 | + |
| 48 | +An interactive example of HGQ can be found in the [kaggle notebook](https://www.kaggle.com/code/calad0i/small-jet-tagger-with-hgq-1). Full documentation can be found at [calad0i.github.io/HGQ](https://calad0i.github.io/HGQ/>). |
0 commit comments