Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions hls4ml/model/optimizer/passes/infer_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from collections.abc import Iterable

import numpy as np
from fxpmath import Fxp

from hls4ml.model.optimizer import ConfigurableOptimizerPass
from hls4ml.model.optimizer.passes.bit_exact import minimal_kif
from hls4ml.model.types import (
FixedPrecisionType,
IntegerPrecisionType,
Expand Down Expand Up @@ -618,18 +618,6 @@ def _get_precision_from_constant(value: int | float, max_width=8):
if value == 0:
return FixedPrecisionType(width=1, integer=1, signed=False)

signed = value < 0
absval = abs(value)
# check if power of 2
mantissa, exp = np.frexp(absval)
if mantissa == 0.5: # is it a power of 2?
# One could consider returning an ExponentPrecisionType here.
# Decided on FixedPrecisionType everywhere since ExponentPrecisionType is less supported
return FixedPrecisionType(1 + signed, exp, signed)

# now is the general case. First try Fxp
fxpval = Fxp(value, signed=signed)
if isinstance(fxpval.n_word, int) and fxpval.n_word <= max_width:
return FixedPrecisionType(fxpval.n_word, signed + fxpval.n_int, signed)

return FixedPrecisionType(signed + max_width, signed + exp, signed)
signed, integer, fraction = map(int, minimal_kif(np.array(value)))
width = min(signed + integer + fraction, signed + max_width)
return FixedPrecisionType(width, signed + integer, bool(signed))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
dynamic = [ "version" ]
dependencies = [ "fxpmath", "h5py", "numpy", "pydigitalwavetools==1.1", "pyyaml", "quantizers" ]
dependencies = [ "h5py", "numpy", "pydigitalwavetools==1.1", "pyyaml", "quantizers" ]

optional-dependencies.da = [ "da4ml>=0.2.1,<=0.4" ]
optional-dependencies.doc = [
Expand Down
4 changes: 2 additions & 2 deletions test/pytest/test_auto_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ def test_auto_precision_dense(keras_model_dense, data_1d, io_type, backend):
"val, expected_width",
[
(0, 1),
(-1024, 2),
(-1024, 1),
(1024, 1),
(0.03125, 1),
(-0.03125, 2),
(-0.03125, 1),
(1.25, 3),
(-1.25, 4),
(1.1, 8),
Expand Down
Loading