From 75b414b070d9651a7e6ebbaf9204cef3051bee5f Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu <maltanar@gmail.com> Date: Tue, 18 Aug 2020 14:12:34 +0200 Subject: [PATCH] [Util] introduce calculate_matvec_accumulator_range --- src/finn/util/basic.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/finn/util/basic.py b/src/finn/util/basic.py index 6c92e9b27..62d5947e3 100644 --- a/src/finn/util/basic.py +++ b/src/finn/util/basic.py @@ -259,6 +259,33 @@ def pad_tensor_to_multiple_of(ndarray, pad_to_dims, val=0, distr_pad=False): return ret +def calculate_matvec_accumulator_range(matrix, vec_dt): + """Calculate the minimum and maximum possible result (accumulator) values + for a dot product x * A, given matrix A of dims (MW, MH), and vector (1, MW) + with datatype vec_dt. Returns (acc_min, acc_max). + """ + min_weight = matrix.min() + max_weight = matrix.max() + perceptive_field_elems = matrix.shape[0] + min_input = vec_dt.min() + max_input = vec_dt.max() + # calculate minimum and maximum values of accumulator + # assume inputs span the whole range of the input datatype + acc_min = perceptive_field_elems * min( + min_weight * max_input, + min_weight * min_input, + max_weight * max_input, + max_weight * min_input, + ) + acc_max = perceptive_field_elems * max( + min_weight * max_input, + min_weight * min_input, + max_weight * max_input, + max_weight * min_input, + ) + return (acc_min, acc_max) + + def gen_finn_dt_tensor(finn_dt, tensor_shape): """Generates random tensor in given shape and with given FINN DataType.""" if type(tensor_shape) == list: -- GitLab