From a4f38910cb27d2d30dadc190ecbfdf1a2b325cc5 Mon Sep 17 00:00:00 2001 From: Philipp Plank Date: Thu, 26 Jan 2023 05:17:05 -0800 Subject: [PATCH 1/2] fixed using all 0 weight matrix --- src/lava/utils/weightutils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lava/utils/weightutils.py b/src/lava/utils/weightutils.py index ac81ef46c..4b9502e1b 100644 --- a/src/lava/utils/weightutils.py +++ b/src/lava/utils/weightutils.py @@ -135,7 +135,11 @@ def _determine_weight_exp(weights: np.ndarray, min_weight = np.min(weights) scale = 0 - + + if max_weight == min_weight == 0: + weight_exp = -0 + return weight_exp + if sign_mode == SignMode.MIXED: pos_scale = 127 / max_weight if max_weight > 0 else np.inf neg_scale = -128 / min_weight if min_weight < 0 else np.inf From 34790049afec1350322f40c784af123df8180a97 Mon Sep 17 00:00:00 2001 From: Philipp Plank Date: Thu, 26 Jan 2023 05:28:42 -0800 Subject: [PATCH 2/2] added unit test --- src/lava/utils/weightutils.py | 4 ++-- tests/lava/utils/test_weightutils.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lava/utils/weightutils.py b/src/lava/utils/weightutils.py index 4b9502e1b..587bf9282 100644 --- a/src/lava/utils/weightutils.py +++ b/src/lava/utils/weightutils.py @@ -135,11 +135,11 @@ def _determine_weight_exp(weights: np.ndarray, min_weight = np.min(weights) scale = 0 - + if max_weight == min_weight == 0: weight_exp = -0 return weight_exp - + if sign_mode == SignMode.MIXED: pos_scale = 127 / max_weight if max_weight > 0 else np.inf neg_scale = -128 / min_weight if min_weight < 0 else np.inf diff --git a/tests/lava/utils/test_weightutils.py b/tests/lava/utils/test_weightutils.py index 01645f129..5af40a773 100644 --- a/tests/lava/utils/test_weightutils.py +++ b/tests/lava/utils/test_weightutils.py @@ -138,6 +138,18 @@ def test_optimize_weight_bits_weight_exp(self) -> None: self.assertEqual(optimized.weight_exp, -4) self.assertEqual(optimized.num_weight_bits, 3) + def test_optimize_weight_bits_wgts_all_zero(self) -> None: + weights = np.array([0, 0, 0]) + sign_mode = SignMode.EXCITATORY + + optimized = optimize_weight_bits(weights=weights, + sign_mode=sign_mode, + loihi2=True) + + np.testing.assert_array_equal(optimized.weights, np.array([0, 0, 0])) + self.assertEqual(optimized.weight_exp, -0) + self.assertEqual(optimized.num_weight_bits, 0) + def test_determine_weight_exp_inhibitory_0(self) -> None: weight_exp = _determine_weight_exp(weights=np.array([-255, -128, -1]), sign_mode=SignMode.INHIBITORY)