From 81ee53d65a6e3e7d454eda967e6f9f157cae69f1 Mon Sep 17 00:00:00 2001 From: Jeremy Johnson Date: Wed, 23 Mar 2022 15:32:34 +0000 Subject: Add missing REQUIRE to NEGATE op And update test generation to create values in predictable range Signed-off-by: Jeremy Johnson Change-Id: I4ba1ff445bf6caeec9f8782902fc45929fe0ee77 --- reference_model/src/ops/ewise_unary.cc | 25 +++++++++++++++++----- verif/generator/tosa_test_gen.py | 38 ++++++++++++++++++++++++++++++---- 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::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(std::numeric_limits::max()); + min_in_64 = static_cast(std::numeric_limits::min()); + } + else + { + max_in_64 = static_cast(std::numeric_limits::max()); + min_in_64 = static_cast(std::numeric_limits::min()); + } + REQUIRE(res_in_64 <= max_in_64 && res_in_64 >= min_in_64, "OpNegate: result not in input type range"); + return static_cast(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(QMin)), static_cast(QMax)); + int32_t res_in_32 = 0 - (a - this->qinfo->input_zp()); + int32_t max_in_32 = static_cast(std::numeric_limits::max()); + int32_t min_in_32 = static_cast(std::numeric_limits::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(std::min(std::max(res_in_32, static_cast(QMin)), static_cast(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 -- cgit v1.2.1