Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating matvec accumulator range calculation #29

Merged
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
29 changes: 8 additions & 21 deletions src/qonnx/util/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,31 +181,18 @@ 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):
def calculate_matvec_accumulator_range(matrix: np.ndarray, vec_dt: DataType):
"""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)
max_weight = abs(matrix).sum(axis=0).max()
max_input = max(abs(vec_dt.min()), abs(vec_dt.max()))
max_value = max_input * max_weight
# If either the weight and input datatypes are signed, then the minimum
# value that their accumulated product can be is -max_value. Else, it's 0.
min_value = -max_value if (matrix.min() < 0) or vec_dt.signed() else 0
return (min_value, max_value)


def gen_finn_dt_tensor(finn_dt, tensor_shape):
Expand Down
63 changes: 63 additions & 0 deletions tests/util/test_matvec_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2022 Xilinx, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Xilinx nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pytest

import numpy as np

import qonnx.util.basic as util
from qonnx.core.datatype import DataType

"""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).
"""

np.random.seed(0)

datatypes = [
DataType["UINT8"],
DataType["INT8"],
DataType["UINT3"],
DataType["INT3"],
DataType["BIPOLAR"],
DataType["TERNARY"],
]


@pytest.mark.parametrize("wdt", datatypes)
@pytest.mark.parametrize("idt", datatypes)
@pytest.mark.parametrize("size", [1, 10, 100])
def test_calculate_matvec_accumulator_range(wdt: DataType, idt: DataType, size: int):
weights = util.gen_finn_dt_tensor(wdt, tensor_shape=(size, 1)) # (MW, MH)
acc_min, acc_max = util.calculate_matvec_accumulator_range(weights, idt)
for _ in range(1000):
inputs = util.gen_finn_dt_tensor(idt, tensor_shape=(1, size))
acc_vals = inputs @ weights
assert acc_vals.min() >= acc_min
assert acc_vals.max() <= acc_max