aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Johnson <jeremy.johnson@arm.com>2022-03-23 15:32:34 +0000
committerJeremy Johnson <jeremy.johnson@arm.com>2022-03-24 09:13:35 +0000
commit81ee53d65a6e3e7d454eda967e6f9f157cae69f1 (patch)
treea35d19a9fb64345f561277880fe476a2491e6dcb
parent25669b31bae45b16d4e96ec13fa9cdeb417975f6 (diff)
downloadreference_model-81ee53d65a6e3e7d454eda967e6f9f157cae69f1.tar.gz
Add missing REQUIRE to NEGATE op
And update test generation to create values in predictable range Signed-off-by: Jeremy Johnson <jeremy.johnson@arm.com> Change-Id: I4ba1ff445bf6caeec9f8782902fc45929fe0ee77
-rw-r--r--reference_model/src/ops/ewise_unary.cc25
-rw-r--r--verif/generator/tosa_test_gen.py38
2 files changed, 54 insertions, 9 deletions
diff --git a/reference_model/src/ops/ewise_unary.cc b/reference_model/src/ops/ewise_unary.cc
index 13e517b..0f38056 100644
--- a/reference_model/src/ops/ewise_unary.cc
+++ b/reference_model/src/ops/ewise_unary.cc
@@ -229,15 +229,30 @@ int OpNegate<Rank, Dtype>::register_fcn()
break;
case DType_INT16:
case DType_INT32:
- this->fcn = [](InEigenType a) -> OutEigenType {
- InEigenType result = -(a);
- return result;
+ this->fcn = [this](InEigenType a) -> OutEigenType {
+ int64_t res_in_64 = 0L - a;
+ int64_t max_in_64, min_in_64;
+ if (Dtype == DType_INT16) {
+ max_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::max());
+ min_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::min());
+ }
+ else
+ {
+ max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
+ min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min());
+ }
+ REQUIRE(res_in_64 <= max_in_64 && res_in_64 >= min_in_64, "OpNegate: result not in input type range");
+ return static_cast<InEigenType>(res_in_64);
};
break;
case DType_INT8:
this->fcn = [this](InEigenType a) -> OutEigenType {
- InEigenType result = -(a - this->qinfo->input_zp()) + this->qinfo->output_zp();
- result = std::min(std::max(result, static_cast<InEigenType>(QMin)), static_cast<InEigenType>(QMax));
+ int32_t res_in_32 = 0 - (a - this->qinfo->input_zp());
+ int32_t max_in_32 = static_cast<int32_t>(std::numeric_limits<int8_t>::max());
+ int32_t min_in_32 = static_cast<int32_t>(std::numeric_limits<int8_t>::min());
+ REQUIRE(res_in_32 <= max_in_32 && res_in_32 >= min_in_32, "OpNegate: result not in i8 range");
+ res_in_32 += this->qinfo->output_zp();
+ InEigenType result = static_cast<InEigenType>(std::min(std::max(res_in_32, static_cast<int32_t>(QMin)), static_cast<int32_t>(QMax)));
return result;
};
break;
diff --git a/verif/generator/tosa_test_gen.py b/verif/generator/tosa_test_gen.py
index 83081ee..b1f9938 100644
--- a/verif/generator/tosa_test_gen.py
+++ b/verif/generator/tosa_test_gen.py
@@ -5704,13 +5704,15 @@ class TosaTestGen:
# Build the random tensor operands and the test
tens = []
- tens = self.generate_tensors(op, dtypeList, shapeList, testArgs, error_name)
-
if qgen is not None:
qinfo = qgen(self, op, dtype_or_dtypeList, error_name)
else:
qinfo = None
+ tens = self.generate_tensors(
+ op, dtypeList, shapeList, testArgs, qinfo, error_name
+ )
+
try:
if error_if_validators is None:
if qinfo is not None:
@@ -5748,11 +5750,39 @@ class TosaTestGen:
# The test is not valid
print(f"Invalid ERROR_IF test created: {opName} {testStr}")
- def generate_tensors(self, op, dtypeList, shapeList, testArgs, error_name=None):
+ def generate_tensors(
+ self, op, dtypeList, shapeList, testArgs, qinfo, error_name=None
+ ):
pCount, cCount = op["operands"]
tens = []
- if (
+ if op["op"] == Op.NEGATE and dtypeList[0] != DType.FLOAT and error_name is None:
+ assert (
+ pCount == 1 and cCount == 0
+ ), "Op.NEGATE must have 1 placeholders, 0 consts"
+ # Must create tensors with values within negatable ranges
+ if dtypeList[0] == DType.INT8:
+ # For use: qinfo.ints[0][1] = input_zp, qinfo.ints[1][1] = output_zp
+ max_val = 127 + qinfo.ints[0][1]
+ min_val = -127 + qinfo.ints[0][1]
+ elif dtypeList[0] == DType.INT16:
+ max_val = 32767
+ min_val = -max_val
+ else:
+ assert (
+ dtypeList[0] == DType.INT32
+ ), "Op.NEGATE found with unsupported input type"
+ max_val = (1 << 31) - 1
+ min_val = -max_val
+ arr = np.int32(
+ self.rng.integers(low=min_val, high=(max_val + 1), size=shapeList[0])
+ )
+ placeholders = []
+ placeholders.append(
+ self.ser.addPlaceholder(shapeList[0], dtypeList[0], arr)
+ )
+ tens.extend(placeholders)
+ elif (
(op["op"] == Op.ADD or op["op"] == Op.SUB)
and dtypeList[0] == DType.INT32
and error_name is None