From 6ef02301ce36a0d2b72171926376d62d42bd348a Mon Sep 17 00:00:00 2001 From: Fredrik Svedberg Date: Thu, 29 Sep 2022 13:29:37 +0200 Subject: MLBEDSW-2723 Handle int16 multiplier overflow test case Added unit tests for scaling including saturated multiplier test. Change-Id: I87bb3a4bed8f62f5ef5cf3851b97f09ce42bf2b6 Signed-off-by: Fredrik Svedberg --- ethosu/vela/test/test_scaling.py | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ethosu/vela/test/test_scaling.py (limited to 'ethosu') diff --git a/ethosu/vela/test/test_scaling.py b/ethosu/vela/test/test_scaling.py new file mode 100644 index 00000000..b4518302 --- /dev/null +++ b/ethosu/vela/test/test_scaling.py @@ -0,0 +1,50 @@ +# Copyright (C) 2022 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the License); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# Description: +# Unit tests for scaling +from ethosu.vela.scaling import quantise_scale +from ethosu.vela.scaling import reduced_quantise_scale + + +def test_scaling(): + multiplier, shift = quantise_scale(1) + assert multiplier == 1073741824 and shift == 30 + multiplier, shift = quantise_scale(0.5) + assert multiplier == 1073741824 and shift == 31 + multiplier, shift = quantise_scale(0.001) + assert multiplier == 1099511628 and shift == 40 + multiplier, shift = quantise_scale(0.008) + assert multiplier == 1099511628 and shift == 37 + multiplier, shift = quantise_scale(0.00097652) + assert multiplier == 2147390190 and shift == 41 + multiplier, shift = quantise_scale(0.0009765615959827986) + assert multiplier == 2147481660 and shift == 41 + + +def test_reduced_scaling(): + multiplier, shift = reduced_quantise_scale(1) + assert multiplier == 16384 and shift == 14 + multiplier, shift = reduced_quantise_scale(0.5) + assert multiplier == 16384 and shift == 15 + multiplier, shift = reduced_quantise_scale(0.001) + assert multiplier == 16777 and shift == 24 + multiplier, shift = reduced_quantise_scale(0.008) + assert multiplier == 16777 and shift == 21 + multiplier, shift = reduced_quantise_scale(0.00097652) + assert multiplier == 32767 and shift == 25 + # multiplier saturated + multiplier, shift = reduced_quantise_scale(0.0009765615959827986) + assert multiplier == 32767 and shift == 25 -- cgit v1.2.1