From 60dc48c4ddf30f2a76d4cfcf1b40ca57b6f3bf95 Mon Sep 17 00:00:00 2001 From: Tai Ly Date: Fri, 8 Mar 2024 22:19:41 +0000 Subject: [ref model] Change Clamp and Pad attribute fields This implements changes due to ClampAttribute and PadAttribute field changes. Signed-off-by: Tai Ly Change-Id: Ide01e2a27fe3c1ea7794e7a4b6780b7eae436caf --- ...v2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa | Bin 1504 -> 1484 bytes reference_model/src/ops/activation_funcs.cc | 29 +++++++++++++++------ reference_model/src/ops/data_layout.cc | 16 +++++++++--- thirdparty/serialization_lib | 2 +- verif/generator/tosa_arg_gen.py | 16 ++---------- verif/generator/tosa_test_gen.py | 24 +++++++++++------ verif/generator/tosa_utils.py | 5 ++++ 7 files changed, 57 insertions(+), 35 deletions(-) diff --git a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa index 87bafd1..01d8375 100644 Binary files a/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa and b/examples/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11/flatbuffer-tflite/test_conv2d_1x1_1x32x32x8_qi8_st11_padSAME_dilat11.tosa differ diff --git a/reference_model/src/ops/activation_funcs.cc b/reference_model/src/ops/activation_funcs.cc index 1f4c3b3..de7d8be 100644 --- a/reference_model/src/ops/activation_funcs.cc +++ b/reference_model/src/ops/activation_funcs.cc @@ -17,6 +17,7 @@ #include "arith_util.h" #include "quant_util.h" #include "template_types.h" +#include "tosa_serialization_handler.h" #include using namespace TosaReference; @@ -35,8 +36,11 @@ int OpClamp::register_fcn() case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: case TOSA_REF_TYPE_FP32: { - InEigenType min = (InEigenType)attribute->min_fp(); - InEigenType max = (InEigenType)attribute->max_fp(); + std::vector min_float_data, max_float_data; + TosaSerializationHandler::ConvertU8toF32(attribute->min_val(), /* size = */ 1, min_float_data); + TosaSerializationHandler::ConvertU8toF32(attribute->max_val(), /* size = */ 1, max_float_data); + InEigenType min = (InEigenType)min_float_data[0]; + InEigenType max = (InEigenType)max_float_data[0]; ERROR_IF(max < min, "OpClamp: max smaller than min"); this->fcn = [min, max](InEigenType a) -> OutEigenType { @@ -45,23 +49,32 @@ int OpClamp::register_fcn() } break; case TOSA_REF_TYPE_FP64: { - InEigenType min = (InEigenType)attribute->min_fp(); - InEigenType max = (InEigenType)attribute->max_fp(); + std::vector min_float_data, max_float_data; + TosaSerializationHandler::ConvertU8toF32(attribute->min_val(), /* size = */ 1, min_float_data); + TosaSerializationHandler::ConvertU8toF32(attribute->max_val(), /* size = */ 1, max_float_data); + InEigenType min = (InEigenType)min_float_data[0]; + InEigenType max = (InEigenType)max_float_data[0]; ERROR_IF(max < min, "OpClamp: max smaller than min"); this->fcn = [min, max](InEigenType a) -> OutEigenType { return (a <= min ? min : a >= max ? max : a); }; } break; case TOSA_REF_TYPE_INT8: { - int8_t min = (int8_t)attribute->min_int(); - int8_t max = (int8_t)attribute->max_int(); + std::vector min_int_data, max_int_data; + TosaSerializationHandler::ConvertU8toI32(attribute->min_val(), /* size = */ 1, min_int_data); + TosaSerializationHandler::ConvertU8toI32(attribute->max_val(), /* size = */ 1, max_int_data); + int8_t min = (int8_t)min_int_data[0]; + int8_t max = (int8_t)max_int_data[0]; ERROR_IF(max < min, "OpClamp: max smaller than min"); this->fcn = [min, max](int8_t a) -> int8_t { return a <= min ? min : a >= max ? max : a; }; } case TOSA_REF_TYPE_INT16: { - int16_t min = (int16_t)attribute->min_int(); - int16_t max = (int16_t)attribute->max_int(); + std::vector min_int_data, max_int_data; + TosaSerializationHandler::ConvertU8toI32(attribute->min_val(), /* size = */ 1, min_int_data); + TosaSerializationHandler::ConvertU8toI32(attribute->max_val(), /* size = */ 1, max_int_data); + int16_t min = (int16_t)min_int_data[0]; + int16_t max = (int16_t)max_int_data[0]; ERROR_IF(max < min, "OpClamp: max smaller than min"); this->fcn = [min, max](int16_t a) -> int16_t { return a <= min ? min : a >= max ? max : a; }; diff --git a/reference_model/src/ops/data_layout.cc b/reference_model/src/ops/data_layout.cc index b6ad704..4c17e78 100644 --- a/reference_model/src/ops/data_layout.cc +++ b/reference_model/src/ops/data_layout.cc @@ -176,17 +176,25 @@ int OpPad::eval() case TOSA_REF_TYPE_BOOL: case TOSA_REF_TYPE_INT8: case TOSA_REF_TYPE_INT16: - case TOSA_REF_TYPE_INT32: - pad_value = (InEigenType)attribute->pad_const_int(); + case TOSA_REF_TYPE_INT32: { + std::vector int32_data; + TosaSerializationHandler::ConvertU8toI32(attribute->pad_const(), + /* size = */ 1, int32_data); + pad_value = (InEigenType)int32_data[0]; break; + } case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: case TOSA_REF_TYPE_FP32: case TOSA_REF_TYPE_FP64: case TOSA_REF_TYPE_FP8E4M3: - case TOSA_REF_TYPE_FP8E5M2: - pad_value = (InEigenType)attribute->pad_const_fp(); + case TOSA_REF_TYPE_FP8E5M2: { + std::vector float_data; + TosaSerializationHandler::ConvertU8toF32(attribute->pad_const(), + /* size = */ 1, float_data); + pad_value = (InEigenType)float_data[0]; break; + } default: ASSERT_MSG(false, "TOSA_REF_TYPE %s is not supported.", EnumNameTOSAREFTYPE(Dtype)); break; diff --git a/thirdparty/serialization_lib b/thirdparty/serialization_lib index 758e73e..0b6d7c2 160000 --- a/thirdparty/serialization_lib +++ b/thirdparty/serialization_lib @@ -1 +1 @@ -Subproject commit 758e73e117c5cef17f8f0b1c543efc1df953b2fa +Subproject commit 0b6d7c271af1e6593e6a2cf14b32acea765f4b64 diff --git a/verif/generator/tosa_arg_gen.py b/verif/generator/tosa_arg_gen.py index 20572e8..a2ef5bf 100644 --- a/verif/generator/tosa_arg_gen.py +++ b/verif/generator/tosa_arg_gen.py @@ -1813,13 +1813,7 @@ class TosaArgGen: and "data_gen" in testGen.TOSA_OP_LIST[opName] and gtu.dtypeIsSupportedByCompliance(dtype) ): - if dtype in [ - DType.FP16, - DType.FP32, - DType.BF16, - DType.FP8E4M3, - DType.FP8E5M2, - ]: + if gtu.dtypeIsFloat(dtype): dataGenTypesList = testGen.TOSA_OP_LIST[opName]["data_gen"]["fp"] else: dataGenTypesList = testGen.TOSA_OP_LIST[opName]["data_gen"]["int"] @@ -2462,13 +2456,7 @@ class TosaArgGen: if dtype in [DType.BOOL, DType.INT8, DType.INT16, DType.INT32]: pad_const_int = testGen.getRandNumberDType(dtype) pad_const_fp = 0 - elif dtype in ( - DType.FP16, - DType.BF16, - DType.FP32, - DType.FP8E4M3, - DType.FP8E5M2, - ): + elif gtu.dtypeIsFloat(dtype): pad_const_int = 0 pad_const_fp = testGen.getRandNumberDType(dtype) else: diff --git a/verif/generator/tosa_test_gen.py b/verif/generator/tosa_test_gen.py index e7704f1..3173906 100644 --- a/verif/generator/tosa_test_gen.py +++ b/verif/generator/tosa_test_gen.py @@ -3,6 +3,7 @@ import json import logging import os +import struct from copy import deepcopy from datetime import datetime from pathlib import Path @@ -1428,13 +1429,17 @@ class TosaTestGen: # Non-tensor fp16 ops take fp16 values as fp32 in reference_model min_val = min_val.astype(np.float32) max_val = max_val.astype(np.float32) - - attr.ClampAttribute(self.ser.builder, 0, 0, min_val, max_val) + min_val_as_bytes = struct.pack("