From 9c9c8dafe8f9a32bd70aee268cd537b93865a3ba Mon Sep 17 00:00:00 2001 From: Jerry Ge Date: Wed, 19 Jul 2023 23:08:16 +0000 Subject: Run clang-format and update copyright - Also added run clang-format to pre-commit runs Signed-off-by: Jerry Ge Change-Id: I4e59ac0afbaa30dce0773aa63d92a1a3b119e2f3 --- reference_model/src/ops/activation_funcs.cc | 22 +- reference_model/src/ops/activation_funcs.h | 4 +- reference_model/src/ops/control_flow.cc | 12 +- reference_model/src/ops/data_layout.cc | 67 +++--- reference_model/src/ops/data_nodes.cc | 4 +- reference_model/src/ops/ewise_binary.cc | 50 +++-- reference_model/src/ops/ewise_binary.h | 4 +- reference_model/src/ops/ewise_ternary.cc | 30 ++- reference_model/src/ops/ewise_unary.cc | 20 +- reference_model/src/ops/image.cc | 22 +- reference_model/src/ops/op_factory.h | 23 +-- reference_model/src/ops/reduction.cc | 72 +++++-- reference_model/src/ops/scatter_gather.cc | 8 +- reference_model/src/ops/tensor_ops.cc | 307 +++++++++++++--------------- reference_model/src/ops/tensor_ops.h | 55 ++--- reference_model/src/ops/type_conversion.cc | 40 ++-- 16 files changed, 369 insertions(+), 371 deletions(-) (limited to 'reference_model/src/ops') diff --git a/reference_model/src/ops/activation_funcs.cc b/reference_model/src/ops/activation_funcs.cc index 12d0697..8ca77f7 100644 --- a/reference_model/src/ops/activation_funcs.cc +++ b/reference_model/src/ops/activation_funcs.cc @@ -14,9 +14,9 @@ // limitations under the License. #include "activation_funcs.h" +#include "arith_util.h" #include "quant_util.h" #include "template_types.h" -#include "arith_util.h" #include using namespace TosaReference; @@ -34,17 +34,17 @@ int OpClamp::register_fcn() { case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: - case TOSA_REF_TYPE_FP32: - { + case TOSA_REF_TYPE_FP32: { InEigenType min = (InEigenType)attribute->min_fp(); InEigenType max = (InEigenType)attribute->max_fp(); ERROR_IF(max < min, "OpClamp: max smaller than min"); - this->fcn = [min, max](InEigenType a) -> OutEigenType { return fpTrunc(a <= min ? min : a >= max ? max : a); }; + this->fcn = [min, max](InEigenType a) -> OutEigenType { + return fpTrunc(a <= min ? min : a >= max ? max : a); + }; } break; - case TOSA_REF_TYPE_FP64: - { + case TOSA_REF_TYPE_FP64: { InEigenType min = (InEigenType)attribute->min_fp(); InEigenType max = (InEigenType)attribute->max_fp(); ERROR_IF(max < min, "OpClamp: max smaller than min"); @@ -53,8 +53,7 @@ int OpClamp::register_fcn() } break; case TOSA_REF_TYPE_INT8: - case TOSA_REF_TYPE_INT16: - { + case TOSA_REF_TYPE_INT16: { InEigenType min = (InEigenType)attribute->min_int(); InEigenType max = (InEigenType)attribute->max_int(); ERROR_IF(max < min, "OpClamp: max smaller than min"); @@ -71,7 +70,8 @@ int OpClamp::register_fcn() template OpClamp::~OpClamp() { - if (attribute) delete attribute; + if (attribute) + delete attribute; } template @@ -86,9 +86,7 @@ int OpSigmoid::register_fcn() case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: case TOSA_REF_TYPE_FP32: - this->fcn = [](InEigenType a) -> OutEigenType { - return fpTrunc(1.f / (1.f + (expf(-1.f * a)))); - }; + this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc(1.f / (1.f + (expf(-1.f * a)))); }; break; case TOSA_REF_TYPE_FP64: this->fcn = [](InEigenType a) -> OutEigenType { return (1.L / (1.L + (exp(-1.L * a)))); }; diff --git a/reference_model/src/ops/activation_funcs.h b/reference_model/src/ops/activation_funcs.h index a7e1275..1696668 100644 --- a/reference_model/src/ops/activation_funcs.h +++ b/reference_model/src/ops/activation_funcs.h @@ -86,8 +86,8 @@ public: { register_fcn(); } - using InEigenType = typename GetEigenType::type; - using OutEigenType = typename GetEigenType::type; + using InEigenType = typename GetEigenType::type; + using OutEigenType = typename GetEigenType::type; virtual int register_fcn(); }; diff --git a/reference_model/src/ops/control_flow.cc b/reference_model/src/ops/control_flow.cc index a0e1fc2..0afb7e2 100644 --- a/reference_model/src/ops/control_flow.cc +++ b/reference_model/src/ops/control_flow.cc @@ -95,20 +95,17 @@ int OpControlFlow::evalBlock(TosaSerializationBasicBlock* block, // pass block status back switch (block_sgt.getGraphStatus()) { - case GraphStatus::TOSA_VALID: - { + case GraphStatus::TOSA_VALID: { DEBUG_MED(OP, "Successfully evaluating block %s", block_name.c_str()); break; } - case GraphStatus::TOSA_UNPREDICTABLE: - { + case GraphStatus::TOSA_UNPREDICTABLE: { DEBUG_MED(OP, "Finish evaluating block %s but result is UNPREDICTABLE", block_name.c_str()); DEBUG_MED(OP, "Setting parent graph status to UNPREDICTABLE"); parent_sgt->setGraphStatus(GraphStatus::TOSA_UNPREDICTABLE); break; } - case GraphStatus::TOSA_ERROR: - { + case GraphStatus::TOSA_ERROR: { DEBUG_MED(OP, "Fail evaluating block %s. Result is ERROR", block_name.c_str()); if (parent_sgt->getGraphStatus() != GraphStatus::TOSA_UNPREDICTABLE) { @@ -311,7 +308,8 @@ OpWhileLoop::~OpWhileLoop() int OpWhileLoop::checkTensorAttributes() { - if (!tsh) { + if (!tsh) + { WARNING("OpWhileLoop: tosa serialization handler must not be null"); return 1; } diff --git a/reference_model/src/ops/data_layout.cc b/reference_model/src/ops/data_layout.cc index 86cd752..bc97c89 100644 --- a/reference_model/src/ops/data_layout.cc +++ b/reference_model/src/ops/data_layout.cc @@ -21,9 +21,7 @@ using namespace Eigen; using namespace tosa; template -OpConcat::OpConcat(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpConcat::OpConcat(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_CONCAT, id_) { setRequiredOperands(-1, 1); @@ -125,9 +123,7 @@ int OpConcat::eval() } template -OpPad::OpPad(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpPad::OpPad(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_PAD, id_) { setRequiredOperands(1, 1); @@ -138,8 +134,7 @@ OpPad::OpPad(SubgraphTraverser* sgt_, template OpPad::~OpPad() -{ -} +{} template int OpPad::checkTensorAttributes() @@ -215,9 +210,7 @@ int OpPad::eval() } template -OpReshape::OpReshape(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpReshape::OpReshape(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RESHAPE, id_) { setRequiredOperands(1, 1); @@ -251,40 +244,47 @@ int OpReshape::checkTensorAttributes() } // -1 shape inferencing - auto inferred_size = -1; - auto inferred_dim = -1; - auto total_size = getInputs()[0]->getElementCount(); + auto inferred_size = -1; + auto inferred_dim = -1; + auto total_size = getInputs()[0]->getElementCount(); uint32_t accum_size = 1; for (int32_t d = 0; d < OutRank; d++) { auto curr_new_shape = attribute->new_shape()[d]; - if (curr_new_shape != -1) { + if (curr_new_shape != -1) + { accum_size *= curr_new_shape; - } else { + } + else + { ERROR_IF(inferred_dim != -1, "OpReshape: only 1 inferred dimension in output shape is supported"); inferred_dim = d; } } - ERROR_IF((total_size % accum_size) != 0, "OpReshape: shape inference failed, missing dimension would be non-integer"); + ERROR_IF((total_size % accum_size) != 0, + "OpReshape: shape inference failed, missing dimension would be non-integer"); inferred_size = total_size / accum_size; - if (inferred_dim != -1) { + if (inferred_dim != -1) + { getOutputs()[0]->setDimSize(inferred_dim, inferred_size); // Need to also edit the serializedTensor's shape at inferred_dim TosaSerializationTensor* serializedTensor; - for (auto region : parent_sgt->getTsh()->GetRegions()) { - for (auto block : region->GetBlocks()) { - if (block->GetTensorByName(getOutputs()[0]->getName())) { + for (auto region : parent_sgt->getTsh()->GetRegions()) + { + for (auto block : region->GetBlocks()) + { + if (block->GetTensorByName(getOutputs()[0]->getName())) + { serializedTensor = block->GetTensorByName(getOutputs()[0]->getName()); serializedTensor->SetDimSize(inferred_dim, inferred_size); break; } } } - } ERROR_IF(inputs[0]->getElementCount() != outputs[0]->getElementCount(), @@ -294,7 +294,7 @@ int OpReshape::checkTensorAttributes() { auto curr_new_shape = attribute->new_shape()[d]; ERROR_IF(curr_new_shape != -1 && curr_new_shape != outputs[0]->getShape()[d], - "OpReshape: new_shape doesn't match output shape"); + "OpReshape: new_shape doesn't match output shape"); } in = dynamic_cast*>(inputs[0]); @@ -308,7 +308,7 @@ int OpReshape::eval() { for (int32_t d = 0; d < OutRank; d++) { - array_shape[d] = getOutputs()[0]->getShape()[OutRank - 1 - d]; + array_shape[d] = getOutputs()[0]->getShape()[OutRank - 1 - d]; out_reverser[d] = OutRank - 1 - d; } @@ -347,9 +347,7 @@ int OpReshape::eval() } template -OpReverse::OpReverse(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpReverse::OpReverse(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_REVERSE, id_) { setRequiredOperands(1, 1); @@ -418,9 +416,7 @@ int OpReverse::eval() } template -OpSlice::OpSlice(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpSlice::OpSlice(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_SLICE, id_) { setRequiredOperands(1, 1); @@ -491,9 +487,7 @@ int OpSlice::eval() } template -OpTileBase::OpTileBase(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpTileBase::OpTileBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_TILE, id_) { setRequiredOperands(1, 1); @@ -693,9 +687,7 @@ int OpTile<6, Dtype>::eval() } template -OpTranspose::OpTranspose(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpTranspose::OpTranspose(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_TRANSPOSE, id_) { setRequiredOperands(1, 1); @@ -707,7 +699,8 @@ OpTranspose::OpTranspose(SubgraphTraverser* sgt_, template OpTranspose::~OpTranspose() { - if (attribute) delete attribute; + if (attribute) + delete attribute; } template diff --git a/reference_model/src/ops/data_nodes.cc b/reference_model/src/ops/data_nodes.cc index b7f987a..07e7e97 100644 --- a/reference_model/src/ops/data_nodes.cc +++ b/reference_model/src/ops/data_nodes.cc @@ -43,9 +43,7 @@ int OpConst::eval() } template -OpIdentity::OpIdentity(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpIdentity::OpIdentity(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_IDENTITY, id_) { setRequiredOperands(1, 1); diff --git a/reference_model/src/ops/ewise_binary.cc b/reference_model/src/ops/ewise_binary.cc index 8578527..3fa4194 100644 --- a/reference_model/src/ops/ewise_binary.cc +++ b/reference_model/src/ops/ewise_binary.cc @@ -23,9 +23,7 @@ using namespace Eigen; using namespace tosa; template -BinaryNodeBase::BinaryNodeBase(SubgraphTraverser* sgt_, - const Op& op_, - uint64_t id_) +BinaryNodeBase::BinaryNodeBase(SubgraphTraverser* sgt_, const Op& op_, uint64_t id_) : GraphNode(sgt_, op_, id_) { setRequiredOperands(2, 1); @@ -100,11 +98,16 @@ int BinaryNodeBase::broadcast(std::vector& calcula // calculates the broadcasted output shape calculated_shape = a_shape; - for (size_t i = 0; i < calculated_shape.size(); i++) { - if (calculated_shape[i] == 1) { + for (size_t i = 0; i < calculated_shape.size(); i++) + { + if (calculated_shape[i] == 1) + { calculated_shape[i] = b_shape[i]; - } else { - ERROR_IF(b_shape[i] != 1 && b_shape[i] != calculated_shape[i], "Broadcast_shape failure, input shapes are not compatible"); + } + else + { + ERROR_IF(b_shape[i] != 1 && b_shape[i] != calculated_shape[i], + "Broadcast_shape failure, input shapes are not compatible"); } } @@ -118,7 +121,8 @@ int BinaryNode::eval() this->broadcast(calculated_shape); auto result_shape = this->result->getShape(); - ERROR_IF(calculated_shape != result_shape, "Broadcast_shape failure, calculated_shape and result_shape don't match"); + ERROR_IF(calculated_shape != result_shape, + "Broadcast_shape failure, calculated_shape and result_shape don't match"); Eigen::array reshaper; reshaper.fill(1); @@ -210,7 +214,8 @@ int OpArithmeticRightShift::register_fcn() template OpArithmeticRightShift::~OpArithmeticRightShift() { - if (attribute) delete attribute; + if (attribute) + delete attribute; } template @@ -309,21 +314,21 @@ int OpLogicalLeftShift::register_fcn() case TOSA_REF_TYPE_INT8: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a << b)); }; break; case TOSA_REF_TYPE_INT16: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a << b)); }; break; case TOSA_REF_TYPE_INT32: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a << b)); }; break; @@ -342,21 +347,21 @@ int OpLogicalRightShift::register_fcn() case TOSA_REF_TYPE_INT8: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a) >> b); }; break; case TOSA_REF_TYPE_INT16: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a) >> b); }; break; case TOSA_REF_TYPE_INT32: this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType { REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]", - (int32_t)b); + (int32_t)b); return static_cast(static_cast(a) >> b); }; break; @@ -494,7 +499,8 @@ int OpMul::register_fcn() template OpMul::~OpMul() { - if (attribute) delete attribute; + if (attribute) + delete attribute; } template @@ -547,9 +553,7 @@ int OpSub::register_fcn() } template -OpTable::OpTable(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpTable::OpTable(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_TABLE, id_) { setRequiredOperands(1, 1); @@ -561,7 +565,8 @@ OpTable::OpTable(SubgraphTraverser* sgt_, template OpTable::~OpTable() { - if (attribute) delete attribute; + if (attribute) + delete attribute; } template @@ -624,10 +629,11 @@ int OpTable::eval() int32_t base = table[index]; int32_t next = table[index + 1]; int32_t slope = next - base; - REQUIRE(slope <= std::numeric_limits::max() && slope >= std::numeric_limits::min(), "OpTable: slope out of int16_t range"); + REQUIRE(slope <= std::numeric_limits::max() && slope >= std::numeric_limits::min(), + "OpTable: slope out of int16_t range"); // 4. interpolate, generate 16.7 (23-bit) output - int32_t value = (base << 7) + (slope) * frac; + int32_t value = (base << 7) + (slope)*frac; return value; }); diff --git a/reference_model/src/ops/ewise_binary.h b/reference_model/src/ops/ewise_binary.h index 3a6f24c..1215c93 100644 --- a/reference_model/src/ops/ewise_binary.h +++ b/reference_model/src/ops/ewise_binary.h @@ -137,9 +137,7 @@ template class OpArithmeticRightShift : public BinaryNode { public: - OpArithmeticRightShift(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) + OpArithmeticRightShift(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : BinaryNode(sgt_, Op_ARITHMETIC_RIGHT_SHIFT, id_) { INIT_ATTRIBUTE(ArithmeticRightShift); diff --git a/reference_model/src/ops/ewise_ternary.cc b/reference_model/src/ops/ewise_ternary.cc index fd2510f..5861cb2 100644 --- a/reference_model/src/ops/ewise_ternary.cc +++ b/reference_model/src/ops/ewise_ternary.cc @@ -20,9 +20,7 @@ using namespace Eigen; using namespace tosa; template -OpSelectBase::OpSelectBase(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpSelectBase::OpSelectBase(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_SELECT, id_) { setRequiredOperands(3, 1); @@ -83,17 +81,26 @@ int OpSelect::broadcast(std::vector& calculated_shape) // calculates the broadcasted output shape calculated_shape = cond_shape; - for (size_t i = 0; i < calculated_shape.size(); i++) { - if (calculated_shape[i] == 1) { + for (size_t i = 0; i < calculated_shape.size(); i++) + { + if (calculated_shape[i] == 1) + { calculated_shape[i] = then_shape[i]; - } else { - ERROR_IF(then_shape[i] != 1 && then_shape[i] != calculated_shape[i], "Broadcast_shape failure, input shapes are not compatible"); + } + else + { + ERROR_IF(then_shape[i] != 1 && then_shape[i] != calculated_shape[i], + "Broadcast_shape failure, input shapes are not compatible"); } - if (calculated_shape[i] == 1) { + if (calculated_shape[i] == 1) + { calculated_shape[i] = else_shape[i]; - } else { - ERROR_IF(else_shape[i] != 1 && else_shape[i] != calculated_shape[i], "Broadcast_shape failure, input shapes are not compatible"); + } + else + { + ERROR_IF(else_shape[i] != 1 && else_shape[i] != calculated_shape[i], + "Broadcast_shape failure, input shapes are not compatible"); } } @@ -107,7 +114,8 @@ int OpSelect::eval() this->broadcast(calculated_shape); auto result_shape = this->out->getShape(); - ERROR_IF(calculated_shape != result_shape, "Broadcast_shape failure, calculated_shape and result_shape don't match"); + ERROR_IF(calculated_shape != result_shape, + "Broadcast_shape failure, calculated_shape and result_shape don't match"); this->out->getTensor() = this->cond->getTensor() .broadcast(this->bcast_cond) diff --git a/reference_model/src/ops/ewise_unary.cc b/reference_model/src/ops/ewise_unary.cc index e6e870e..05c1f4b 100644 --- a/reference_model/src/ops/ewise_unary.cc +++ b/reference_model/src/ops/ewise_unary.cc @@ -237,9 +237,7 @@ int OpLogicalNot::register_fcn() } template -OpNegate::OpNegate(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpNegate::OpNegate(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : UnaryNode(sgt_, Op_NEGATE, id_) { INIT_ATTRIBUTE(Negate); @@ -279,10 +277,11 @@ int OpNegate::register_fcn() case TOSA_REF_TYPE_INT16: case TOSA_REF_TYPE_INT32: this->fcn = [this](InEigenType a) -> OutEigenType { - int64_t res_in_64 = 0L - a; + int64_t res_in_64 = 0L - a; int64_t i32_max_in_64 = static_cast(std::numeric_limits::max()); int64_t i32_min_in_64 = static_cast(std::numeric_limits::min()); - REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpNegate: result not in acc type range (int32)"); + REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, + "OpNegate: result not in acc type range (int32)"); int64_t max_clip_in_64, min_clip_in_64; if (Dtype == TOSA_REF_TYPE_INT16) @@ -295,17 +294,20 @@ int OpNegate::register_fcn() max_clip_in_64 = i32_max_in_64; min_clip_in_64 = i32_min_in_64; } - return static_cast(std::min(max_clip_in_64, std::max(min_clip_in_64, res_in_64))); + return static_cast( + std::min(max_clip_in_64, std::max(min_clip_in_64, res_in_64))); }; break; case TOSA_REF_TYPE_INT8: this->fcn = [this](InEigenType a) -> OutEigenType { - int64_t res_in_64 = 0 - (a - attribute->input1_zp()); + int64_t res_in_64 = 0 - (a - attribute->input1_zp()); int64_t i32_max_in_64 = static_cast(std::numeric_limits::max()); int64_t i32_min_in_64 = static_cast(std::numeric_limits::min()); - REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpNegate: result not in acc type range (int32)"); + REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, + "OpNegate: result not in acc type range (int32)"); res_in_64 += attribute->output_zp(); - InEigenType result = static_cast(std::min(std::max(res_in_64, static_cast(QMin)), static_cast(QMax))); + InEigenType result = static_cast( + std::min(std::max(res_in_64, static_cast(QMin)), static_cast(QMax))); return result; }; break; diff --git a/reference_model/src/ops/image.cc b/reference_model/src/ops/image.cc index 575a500..cf2b60a 100644 --- a/reference_model/src/ops/image.cc +++ b/reference_model/src/ops/image.cc @@ -24,9 +24,7 @@ using namespace Eigen; using namespace tosa; template -OpResize::OpResize(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpResize::OpResize(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RESIZE, id_) { setRequiredOperands(1, 1); @@ -134,17 +132,19 @@ int OpResize::eval() // Check Tosa Level auto tosa_level = g_func_config.tosa_level; - LEVEL_CHECK(scale_y_n / scale_y_d <= tosa_level.MAX_SCALE, "scale_y_n / scale_y_d should be smaller than or equal to MAX_SCALE"); - LEVEL_CHECK(scale_x_n / scale_x_d <= tosa_level.MAX_SCALE, "scale_x_n / scale_x_d should be smaller than or equal to MAX_SCALE"); + LEVEL_CHECK(scale_y_n / scale_y_d <= tosa_level.MAX_SCALE, + "scale_y_n / scale_y_d should be smaller than or equal to MAX_SCALE"); + LEVEL_CHECK(scale_x_n / scale_x_d <= tosa_level.MAX_SCALE, + "scale_x_n / scale_x_d should be smaller than or equal to MAX_SCALE"); int32_t res_height = 0; - int32_t res_width = 0; + int32_t res_width = 0; if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height)) - return 1; + return 1; if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width)) - return 1; + return 1; ERROR_IF(out_height != res_height + 1, "OpResize: mismatch between output height dimension provided and expected shape"); @@ -247,8 +247,10 @@ int OpResize::eval() } acc = in->getTensor()(b, iy, ix, c); } - if ((typeid(resize_t) == typeid(Eigen::bfloat16))) { - ASSERT_MSG(checkValidBFloat(acc), "Resize accumulator float value is not a valid bfloat16 value."); + if ((typeid(resize_t) == typeid(Eigen::bfloat16))) + { + ASSERT_MSG(checkValidBFloat(acc), + "Resize accumulator float value is not a valid bfloat16 value."); } out->getTensor()(b, oy, ox, c) = acc; } diff --git a/reference_model/src/ops/op_factory.h b/reference_model/src/ops/op_factory.h index f276e03..06ef36e 100644 --- a/reference_model/src/ops/op_factory.h +++ b/reference_model/src/ops/op_factory.h @@ -110,7 +110,7 @@ FATAL_ERROR("Can't initialize Tosa" #ATTRIBUTE_NAME "Attribute.\nPre-initialization " \ "of this attribute is required in order to determine the accumulate type."); \ } \ - ConvertDType(accumDType); \ + ConvertDType(accumDType); \ }) #define DEF_FACTORY_TWO_TYPE_RESIZE_INT16(OP, DTYPE1, DTYPE2) \ @@ -192,8 +192,7 @@ { \ switch (inputRank) \ { \ - case 0: \ - { \ + case 0: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 0, DTYPE) \ @@ -205,8 +204,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 6, DTYPE) \ } \ } \ - case 1: \ - { \ + case 1: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 0, DTYPE) \ @@ -218,8 +216,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 6, DTYPE) \ } \ } \ - case 2: \ - { \ + case 2: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 0, DTYPE) \ @@ -231,8 +228,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 6, DTYPE) \ } \ } \ - case 3: \ - { \ + case 3: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 0, DTYPE) \ @@ -244,8 +240,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 6, DTYPE) \ } \ } \ - case 4: \ - { \ + case 4: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 0, DTYPE) \ @@ -257,8 +252,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 6, DTYPE) \ } \ } \ - case 5: \ - { \ + case 5: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 0, DTYPE) \ @@ -270,8 +264,7 @@ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 6, DTYPE) \ } \ } \ - case 6: \ - { \ + case 6: { \ switch (outputRank) \ { \ DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 0, DTYPE) \ diff --git a/reference_model/src/ops/reduction.cc b/reference_model/src/ops/reduction.cc index 639da8d..2eb764a 100644 --- a/reference_model/src/ops/reduction.cc +++ b/reference_model/src/ops/reduction.cc @@ -83,27 +83,44 @@ int ReduceNode::checkTensorAttributes() // These 2 reducers are to overcome a bug introduced in Eigen between 3.3.7 and 3.4.0 // The in-built .any and .all operations now fail on an assert in TensorMorphing.h:150 // which seems to be due to incorrect data being passed internally as m_impl -struct AllReducer { +struct AllReducer +{ static const bool PacketAccess = false; - void reduce(const bool val, bool* accum) { + void reduce(const bool val, bool* accum) + { *accum = *accum && val; } - bool initialize() const { return true; } - bool finalize(const bool accum) const { return accum; } + bool initialize() const + { + return true; + } + bool finalize(const bool accum) const + { + return accum; + } }; -struct AnyReducer { +struct AnyReducer +{ static const bool PacketAccess = false; - void reduce(const bool val, bool* accum) { + void reduce(const bool val, bool* accum) + { *accum = *accum || val; } - bool initialize() const { return false; } - bool finalize(const bool accum) const { return accum; } + bool initialize() const + { + return false; + } + bool finalize(const bool accum) const + { + return accum; + } }; template int OpReduceAll::eval() { - this->out->getTensor() = this->in->getTensor().reduce(this->dims, AllReducer()).reshape(this->out->getTensor().dimensions()); + this->out->getTensor() = + this->in->getTensor().reduce(this->dims, AllReducer()).reshape(this->out->getTensor().dimensions()); return GraphNode::eval(); } @@ -111,7 +128,8 @@ int OpReduceAll::eval() template int OpReduceAny::eval() { - this->out->getTensor() = this->in->getTensor().reduce(this->dims, AnyReducer()).reshape(this->out->getTensor().dimensions()); + this->out->getTensor() = + this->in->getTensor().reduce(this->dims, AnyReducer()).reshape(this->out->getTensor().dimensions()); return GraphNode::eval(); } @@ -135,14 +153,18 @@ int OpReduceMin::eval() template int OpReduceProduct::eval() { - switch(Dtype) + switch (Dtype) { case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: - this->out->getTensor() = this->in->getTensor().prod(this->dims).reshape(this->out->getTensor().dimensions()).unaryExpr([](float f){return fpTrunc(f);}); + this->out->getTensor() = this->in->getTensor() + .prod(this->dims) + .reshape(this->out->getTensor().dimensions()) + .unaryExpr([](float f) { return fpTrunc(f); }); break; case TOSA_REF_TYPE_FP32: - this->out->getTensor() = this->in->getTensor().prod(this->dims).reshape(this->out->getTensor().dimensions()); + this->out->getTensor() = + this->in->getTensor().prod(this->dims).reshape(this->out->getTensor().dimensions()); break; default: ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype)); @@ -188,7 +210,7 @@ int OpReduceProductDouble::eval() template int OpReduceSum::eval() { - switch(Dtype) + switch (Dtype) { case TOSA_REF_TYPE_FP16: case TOSA_REF_TYPE_BF16: @@ -208,20 +230,30 @@ int OpReduceSum::eval() return GraphNode::eval(); } -struct SumRequiresReducer { +struct SumRequiresReducer +{ static const bool PacketAccess = false; - SumRequiresReducer(SubgraphTraverser* parent_sgt) : parent_sgt(parent_sgt) {} - void reduce(const int32_t val, int32_t* accum) { + SumRequiresReducer(SubgraphTraverser* parent_sgt) + : parent_sgt(parent_sgt) + {} + void reduce(const int32_t val, int32_t* accum) + { int64_t res_in_64 = static_cast(*accum) + val; int64_t i32_max_in_64 = static_cast(std::numeric_limits::max()); int64_t i32_min_in_64 = static_cast(std::numeric_limits::min()); REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpReduceSum: result not in i32 range"); *accum = static_cast(res_in_64); } - int32_t initialize() const { return 0; } - int32_t finalize(const int32_t accum) const { return accum; } + int32_t initialize() const + { + return 0; + } + int32_t finalize(const int32_t accum) const + { + return accum; + } - private: +private: SubgraphTraverser* parent_sgt; }; diff --git a/reference_model/src/ops/scatter_gather.cc b/reference_model/src/ops/scatter_gather.cc index 80b6c58..65d61b6 100644 --- a/reference_model/src/ops/scatter_gather.cc +++ b/reference_model/src/ops/scatter_gather.cc @@ -21,9 +21,7 @@ using namespace Eigen; using namespace tosa; template -OpGather::OpGather(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpGather::OpGather(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_GATHER, id_) { setRequiredOperands(2, 1); @@ -117,9 +115,7 @@ int OpGather::eval() } template -OpScatter::OpScatter(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpScatter::OpScatter(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_SCATTER, id_) { setRequiredOperands(3, 1); diff --git a/reference_model/src/ops/tensor_ops.cc b/reference_model/src/ops/tensor_ops.cc index 5ba7a83..96fd41c 100644 --- a/reference_model/src/ops/tensor_ops.cc +++ b/reference_model/src/ops/tensor_ops.cc @@ -14,9 +14,9 @@ // limitations under the License. #include "tensor_ops.h" +#include "half.hpp" #include "quant_util.h" #include "template_types.h" -#include "half.hpp" using namespace TosaReference; using namespace Eigen; @@ -96,19 +96,16 @@ int check_pool2d_attribute(tosa::TosaPoolAttribute* attribute, int32_t full_H = IH + pad_top + pad_bottom - kernel_y; int32_t full_W = IW + pad_left + pad_right - kernel_x; - if ((full_H % stride_y != 0) || - (full_W % stride_x != 0)) + if ((full_H % stride_y != 0) || (full_W % stride_x != 0)) { msg = "Parameters must yield exact integer output dimensions"; return 1; } - if ((OH != (full_H / stride_y) + 1) || - (OW != (full_W / stride_x) + 1)) + if ((OH != (full_H / stride_y) + 1) || (OW != (full_W / stride_x) + 1)) { msg = "Mismatch between output shape provided and expected output shape (" + - std::to_string((full_H / stride_y) + 1) + "," + - std::to_string((full_W / stride_x) + 1) + ")"; + std::to_string((full_H / stride_y) + 1) + "," + std::to_string((full_W / stride_x) + 1) + ")"; return 1; } @@ -173,19 +170,19 @@ int check_conv_attribute(tosa::TosaConvAttribute* attribute, ASSERT_MSG(conv_dimension == 2 || conv_dimension == 3, "Unsupported convolution dimension") int32_t offset_d = conv_dimension == 3 ? 1 : 0; - int32_t ID = conv_dimension == 3 ? input_shape[1] : 1; - int32_t IH = input_shape[1 + offset_d]; - int32_t IW = input_shape[2 + offset_d]; - int32_t OD = conv_dimension == 3 ? output_shape[1] : 1; - int32_t OH = output_shape[1 + offset_d]; - int32_t OW = output_shape[2 + offset_d]; - - int32_t stride_d = conv_dimension == 3 ? attribute->stride()[0] : 1; - int32_t stride_y = attribute->stride()[0 + offset_d]; - int32_t stride_x = attribute->stride()[1 + offset_d]; - int32_t kernel_d = conv_dimension == 3 ? weights[offset_kernel] : 1; - int32_t kernel_h = weights[offset_kernel + offset_d]; - int32_t kernel_w = weights[offset_kernel + 1 + offset_d]; + int32_t ID = conv_dimension == 3 ? input_shape[1] : 1; + int32_t IH = input_shape[1 + offset_d]; + int32_t IW = input_shape[2 + offset_d]; + int32_t OD = conv_dimension == 3 ? output_shape[1] : 1; + int32_t OH = output_shape[1 + offset_d]; + int32_t OW = output_shape[2 + offset_d]; + + int32_t stride_d = conv_dimension == 3 ? attribute->stride()[0] : 1; + int32_t stride_y = attribute->stride()[0 + offset_d]; + int32_t stride_x = attribute->stride()[1 + offset_d]; + int32_t kernel_d = conv_dimension == 3 ? weights[offset_kernel] : 1; + int32_t kernel_h = weights[offset_kernel + offset_d]; + int32_t kernel_w = weights[offset_kernel + 1 + offset_d]; int32_t dilation_d = conv_dimension == 3 ? attribute->dilation()[0] : 1; int32_t dilation_y = attribute->dilation()[0 + offset_d]; int32_t dilation_x = attribute->dilation()[1 + offset_d]; @@ -202,27 +199,21 @@ int check_conv_attribute(tosa::TosaConvAttribute* attribute, int32_t full_H = IH - 1 + pad_top + pad_bottom - (kernel_h - 1) * dilation_y; int32_t full_W = IW - 1 + pad_left + pad_right - (kernel_w - 1) * dilation_x; - if ((full_H % stride_y != 0) || - (full_W % stride_x != 0) || - (full_D % stride_d != 0)) + if ((full_H % stride_y != 0) || (full_W % stride_x != 0) || (full_D % stride_d != 0)) { msg = "Parameters must yield exact integer output dimensions"; return 1; } - if ((OH != (full_H / stride_y) + 1) || - (OW != (full_W / stride_x) + 1) || - (OD != (full_D / stride_d) + 1)) + if ((OH != (full_H / stride_y) + 1) || (OW != (full_W / stride_x) + 1) || (OD != (full_D / stride_d) + 1)) { std::string msg_d = ""; if (conv_dimension == 3) { msg_d += std::to_string((full_D / stride_d) + 1) + ","; } - msg = "Mismatch between output shape provided and expected output shape (" + - msg_d + - std::to_string((full_H / stride_y) + 1) + "," + - std::to_string((full_W / stride_x) + 1) + ")"; + msg = "Mismatch between output shape provided and expected output shape (" + msg_d + + std::to_string((full_H / stride_y) + 1) + "," + std::to_string((full_W / stride_x) + 1) + ")"; return 1; } @@ -244,12 +235,10 @@ int check_fft_shape(const std::vector& in_real, const std::vector& in_imag, const std::vector& out_real, const std::vector& out_imag, - std::string& msg) { - const bool is_rfft = in_imag.empty(); - auto is_power_of_two = [](int32_t n) -> bool - { - return (n & (n-1)) == 0 && n > 0; - }; + std::string& msg) +{ + const bool is_rfft = in_imag.empty(); + auto is_power_of_two = [](int32_t n) -> bool { return (n & (n - 1)) == 0 && n > 0; }; if (!is_power_of_two(in_real[1]) || !is_power_of_two(in_real[2])) { @@ -309,7 +298,9 @@ int check_fft_shape(const std::vector& in_real, msg = "Output width is expected to match input width / 2 + 1"; return 1; } - } else { + } + else + { if (in_real[2] != out_real[2]) { msg = "Input and output width don't match"; @@ -321,9 +312,7 @@ int check_fft_shape(const std::vector& in_real, } template -OpArgMax::OpArgMax(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpArgMax::OpArgMax(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_ARGMAX, id_) { setRequiredOperands(1, 1); @@ -417,9 +406,7 @@ int OpArgMax::eval() } template -OpAvgPool2d::OpAvgPool2d(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpAvgPool2d::OpAvgPool2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_AVG_POOL2D, id_) { setRequiredOperands(1, 1); @@ -485,7 +472,8 @@ ETensor1 OpAvgPool2d::calculate_div_map_1d( // adjust divisors on the left side for padding // We start at the leftmost output element, and remove pad_left - (index * stride) elements // until we have no more padding being used - for(int index = 0; (index <= pad_left / stride) && (index < out_size); index++) { + for (int index = 0; (index <= pad_left / stride) && (index < out_size); index++) + { int32_t adjust = pad_left - (index * stride); result(index) -= adjust; } @@ -495,7 +483,8 @@ ETensor1 OpAvgPool2d::calculate_div_map_1d( // padding is defined in the initialization of index below. Then we keep moving // to the right, increasing padding until we get to the last output element. int index = std::max(0, ((pad_left + in_size - kernel_size) / stride) + 1); - for (; index < out_size; index++) { + for (; index < out_size; index++) + { int32_t adjust = ((index * stride) + kernel_size) - (pad_left + in_size); result(index) -= adjust; } @@ -524,10 +513,10 @@ int OpAvgPool2d::eval() int pad_bottom = this->attribute->pad()[1]; int pad_left = this->attribute->pad()[2]; int pad_right = this->attribute->pad()[3]; - int kernel_y = this->attribute->kernel()[0]; - int kernel_x = this->attribute->kernel()[1]; - int stride_y = this->attribute->stride()[0]; - int stride_x = this->attribute->stride()[1]; + int kernel_y = this->attribute->kernel()[0]; + int kernel_x = this->attribute->kernel()[1]; + int stride_y = this->attribute->stride()[0]; + int stride_x = this->attribute->stride()[1]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; @@ -613,12 +602,11 @@ int OpAvgPool2d::eval() Eigen::array, 1> contract_dims = { Eigen::IndexPair(1, 0) }; Eigen::array bcast{ out_batch, 1, 1, out_channels }; - ETensor2 dm2_w = div_map_w.reshape(Eigen::array{ 1, out_width }); - ETensor2 dm2_h = div_map_h.reshape(Eigen::array{ out_height, 1 }); - ETensor4 div_map = - dm2_h.contract(dm2_w, contract_dims) - .reshape(Eigen::array{ 1, out_height, out_width, 1 }) - .broadcast(bcast); + ETensor2 dm2_w = div_map_w.reshape(Eigen::array{ 1, out_width }); + ETensor2 dm2_h = div_map_h.reshape(Eigen::array{ out_height, 1 }); + ETensor4 div_map = dm2_h.contract(dm2_w, contract_dims) + .reshape(Eigen::array{ 1, out_height, out_width, 1 }) + .broadcast(bcast); if (Dtype != TOSA_REF_TYPE_FP32 && Dtype != TOSA_REF_TYPE_FP16 && Dtype != TOSA_REF_TYPE_BF16 && Dtype != TOSA_REF_TYPE_FP64) { @@ -649,9 +637,7 @@ int OpAvgPool2d::eval() } template -OpConv2d::OpConv2d(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpConv2d::OpConv2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_CONV2D, id_) { setRequiredOperands(3, 1); @@ -685,7 +671,7 @@ int OpConv2d::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpConv2d: Output data type not supported for this configuration of operator"); + "OpConv2d: Output data type not supported for this configuration of operator"); input = dynamic_cast*>(inputs[0]); weight = dynamic_cast*>(inputs[1]); @@ -694,7 +680,7 @@ int OpConv2d::checkTensorAttributes() std::string msg; if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), - weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) + weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) { msg = "OpConv2d: " + msg; printNodeValidationError(msg.c_str()); @@ -736,15 +722,17 @@ int OpConv2d::eval() int pad_left = this->attribute->pad()[2]; int pad_right = this->attribute->pad()[3]; - int stride_y = this->attribute->stride()[0]; - int stride_x = this->attribute->stride()[1]; - int dilation_y = this->attribute->dilation()[0]; - int dilation_x = this->attribute->dilation()[1]; + int stride_y = this->attribute->stride()[0]; + int stride_x = this->attribute->stride()[1]; + int dilation_y = this->attribute->dilation()[0]; + int dilation_x = this->attribute->dilation()[1]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; - LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); - LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, + "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, + "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); @@ -756,8 +744,8 @@ int OpConv2d::eval() "perform OpConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], output.shape=[%d,%d,%d,%d], " "stride=[%d,%d], dilation=[%d,%d], pad=[%d,%d,%d,%d]", in_batch, in_height, in_width, in_channels, f_height, f_width, f_in_channels, f_out_channels, out_batch, - out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, - pad_bottom, pad_left, pad_right); + out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, + pad_left, pad_right); // GEMM-conv2d, left matrix is input, right matrix is weight Eigen::array im2col_input_dims; @@ -827,7 +815,7 @@ int OpConv2d::eval() // transpose and reshape weight from [OC, H, W, IC] to [H * W * IC, OC] ETensor2 im2col_weight = - weight_val.shuffle(Eigen::array({ 1, 2, 3, 0 })).reshape(im2col_weight_dims); + weight_val.shuffle(Eigen::array({ 1, 2, 3, 0 })).reshape(im2col_weight_dims); // don't need to apply bias_multiplier ( * bias_scale and >> bias_shift) since tflite already scale it // and reshaped from [C] to [1, C], and broadcast to [N * H * W, C] @@ -835,8 +823,9 @@ int OpConv2d::eval() (bias_val.reshape(bias_reshaped_dims).broadcast(bias_bcast_dims)).template cast(); // output matrix is [N * H * W, C] - ETensor2 contracted_result = - (im2col_input.template cast().contract(im2col_weight.template cast(), contract_dims)).template cast(); + ETensor2 contracted_result = (im2col_input.template cast().contract( + im2col_weight.template cast(), contract_dims)) + .template cast(); // adding bias ETensor2 biased_output = contracted_result + bias_2d; @@ -854,9 +843,7 @@ int OpConv2d::eval() } template -OpConv3d::OpConv3d(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpConv3d::OpConv3d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_CONV3D, id_) { setRequiredOperands(3, 1); @@ -890,7 +877,7 @@ int OpConv3d::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpConv3d: Output data type not supported for this configuration of operator"); + "OpConv3d: Output data type not supported for this configuration of operator"); input = dynamic_cast*>(inputs[0]); weight = dynamic_cast*>(inputs[1]); @@ -899,7 +886,7 @@ int OpConv3d::checkTensorAttributes() std::string msg; if (check_conv_attribute(attribute, 3 /* conv_dimension */, input->getShape(), output->getShape(), - weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) + weight->getShape(), 1 /* offset_kernel */, InDtype, WeightDtype, msg)) { msg = "OpConv3d: " + msg; printNodeValidationError(msg.c_str()); @@ -946,19 +933,22 @@ int OpConv3d::eval() int pad_left = this->attribute->pad()[4]; int pad_right = this->attribute->pad()[5]; - int stride_d = this->attribute->stride()[0]; - int stride_y = this->attribute->stride()[1]; - int stride_x = this->attribute->stride()[2]; + int stride_d = this->attribute->stride()[0]; + int stride_y = this->attribute->stride()[1]; + int stride_x = this->attribute->stride()[2]; - int dilation_d = this->attribute->dilation()[0]; - int dilation_y = this->attribute->dilation()[1]; - int dilation_x = this->attribute->dilation()[2]; + int dilation_d = this->attribute->dilation()[0]; + int dilation_y = this->attribute->dilation()[1]; + int dilation_x = this->attribute->dilation()[2]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; - LEVEL_CHECK(dilation_d * f_depth <= tosa_level.MAX_KERNEL, "dilation_d * KD should be smaller than or equal to MAX_KERNEL"); - LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); - LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_d * f_depth <= tosa_level.MAX_KERNEL, + "dilation_d * KD should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, + "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, + "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_d0 <= tosa_level.MAX_KERNEL, "pad_d0 should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_d1 <= tosa_level.MAX_KERNEL, "pad_d1 should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); @@ -1103,7 +1093,7 @@ int OpDepthwiseConv2d::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpDepthwiseConv2d: Output data type not supported for this configuration of operator"); + "OpDepthwiseConv2d: Output data type not supported for this configuration of operator"); input = dynamic_cast*>(inputs[0]); weight = dynamic_cast*>(inputs[1]); @@ -1112,7 +1102,7 @@ int OpDepthwiseConv2d::checkTensorAttributes() std::string msg; if (check_conv_attribute(attribute, 2 /* conv_dimension */, input->getShape(), output->getShape(), - weight->getShape(), 0 /* offset_kernel */, InDtype, WeightDtype, msg)) + weight->getShape(), 0 /* offset_kernel */, InDtype, WeightDtype, msg)) { msg = "OpDepthwiseConv2d: " + msg; printNodeValidationError(msg.c_str()); @@ -1155,15 +1145,17 @@ int OpDepthwiseConv2d::eval() int pad_left = this->attribute->pad()[2]; int pad_right = this->attribute->pad()[3]; - int stride_y = this->attribute->stride()[0]; - int stride_x = this->attribute->stride()[1]; - int dilation_y = this->attribute->dilation()[0]; - int dilation_x = this->attribute->dilation()[1]; + int stride_y = this->attribute->stride()[0]; + int stride_x = this->attribute->stride()[1]; + int dilation_y = this->attribute->dilation()[0]; + int dilation_x = this->attribute->dilation()[1]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; - LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); - LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_y * f_height <= tosa_level.MAX_KERNEL, + "dilation_y * KH should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(dilation_x * f_width <= tosa_level.MAX_KERNEL, + "dilation_x * KW should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_top <= tosa_level.MAX_KERNEL, "pad_top should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_bottom <= tosa_level.MAX_KERNEL, "pad_bottom should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(pad_left <= tosa_level.MAX_KERNEL, "pad_left should be smaller than or equal to MAX_KERNEL"); @@ -1175,8 +1167,8 @@ int OpDepthwiseConv2d::eval() "perform OpDepthwiseConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], " "output.shape=[%d,%d,%d,%d], stride=[%d,%d], dilation=[%d,%d], pad=[%d,%d,%d,%d]", in_batch, in_height, in_width, in_channels, f_height, f_width, f_in_channels, f_multiplier, out_batch, - out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, - pad_bottom, pad_left, pad_right); + out_height, out_width, out_channels, stride_y, stride_x, dilation_y, dilation_x, pad_top, pad_bottom, + pad_left, pad_right); Eigen::array, 4> pad; pad[0] = std::make_pair(0, 0); @@ -1241,9 +1233,9 @@ int OpDepthwiseConv2d::eval() for (int fw = 0; fw < f_width; fw++) { // Perform multiplication in AccEigenType then cast to OutEigenType - this->output->getTensor()(ob, oh, ow, ic * f_multiplier + cm) += - (OutEigenType)((AccEigenType)input_extract_patches(ob, fh, fw, ow * out_height + oh, ic) * - (AccEigenType)weight_val(fh, fw, ic, cm)); + this->output->getTensor()(ob, oh, ow, ic * f_multiplier + cm) += (OutEigenType)( + (AccEigenType)input_extract_patches(ob, fh, fw, ow * out_height + oh, ic) * + (AccEigenType)weight_val(fh, fw, ic, cm)); } } } @@ -1308,7 +1300,7 @@ int OpFullyConnected::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpFullyConnected: Output data type not supported for this configuration of operator"); + "OpFullyConnected: Output data type not supported for this configuration of operator"); output = dynamic_cast*>(outputs[0]); @@ -1368,9 +1360,7 @@ int OpFullyConnected::eval() } template -OpMatMul::OpMatMul(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpMatMul::OpMatMul(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_MATMUL, id_) { setRequiredOperands(2, 1); @@ -1398,7 +1388,7 @@ int OpMatMul::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpMatMul: Output data type not supported for this configuration of operator"); + "OpMatMul: Output data type not supported for this configuration of operator"); a = dynamic_cast*>(inputs[0]); b = dynamic_cast*>(inputs[1]); @@ -1513,9 +1503,7 @@ int OpMatMul::eval() } template -OpMaxPool2d::OpMaxPool2d(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpMaxPool2d::OpMaxPool2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_MAX_POOL2D, id_) { setRequiredOperands(1, 1); @@ -1583,10 +1571,10 @@ int OpMaxPool2d::eval() int pad_left = this->attribute->pad()[2]; int pad_right = this->attribute->pad()[3]; - int kernel_y = this->attribute->kernel()[0]; - int kernel_x = this->attribute->kernel()[1]; - int stride_y = this->attribute->stride()[0]; - int stride_x = this->attribute->stride()[1]; + int kernel_y = this->attribute->kernel()[0]; + int kernel_x = this->attribute->kernel()[1]; + int stride_y = this->attribute->stride()[0]; + int stride_x = this->attribute->stride()[1]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; @@ -1678,14 +1666,13 @@ int OpFFT2d::checkTensorAttributes() if (validateRequiredOperands()) return 1; - if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || - validateRequiredRank(outputs[0]) || validateRequiredRank(outputs[1])) + if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0]) || + validateRequiredRank(outputs[1])) { return 1; } - if (inputs[0]->matchType(*outputs[0]) || inputs[1]->matchType(*outputs[1]) || - inputs[0]->matchType(*inputs[1])) + if (inputs[0]->matchType(*outputs[0]) || inputs[1]->matchType(*outputs[1]) || inputs[0]->matchType(*inputs[1])) { printNodeValidationError("OpFFT2d: input and output tensor type mismatch"); return 1; @@ -1699,8 +1686,7 @@ int OpFFT2d::checkTensorAttributes() ASSERT_MEM(in_real && in_imag && out_real && out_imag); std::string msg; - if (check_fft_shape(in_real->getShape(), in_imag->getShape(), - out_real->getShape(), out_imag->getShape(), msg)) + if (check_fft_shape(in_real->getShape(), in_imag->getShape(), out_real->getShape(), out_imag->getShape(), msg)) { msg = "OpFFT2d: " + msg; printNodeValidationError(msg.c_str()); @@ -1713,37 +1699,35 @@ int OpFFT2d::checkTensorAttributes() template int OpFFT2d::eval() { - int in_real_batch = this->in_real->getShape()[0]; + int in_real_batch = this->in_real->getShape()[0]; int in_real_height = this->in_real->getShape()[1]; - int in_real_width = this->in_real->getShape()[2]; + int in_real_width = this->in_real->getShape()[2]; - int in_imag_batch = this->in_imag->getShape()[0]; + int in_imag_batch = this->in_imag->getShape()[0]; int in_imag_height = this->in_imag->getShape()[1]; - int in_imag_width = this->in_imag->getShape()[2]; + int in_imag_width = this->in_imag->getShape()[2]; - int out_real_batch = this->out_real->getShape()[0]; + int out_real_batch = this->out_real->getShape()[0]; int out_real_height = this->out_real->getShape()[1]; - int out_real_width = this->out_real->getShape()[2]; + int out_real_width = this->out_real->getShape()[2]; - int out_imag_batch = this->out_imag->getShape()[0]; + int out_imag_batch = this->out_imag->getShape()[0]; int out_imag_height = this->out_imag->getShape()[1]; - int out_imag_width = this->out_imag->getShape()[2]; + int out_imag_width = this->out_imag->getShape()[2]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; LEVEL_CHECK(in_real_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(in_real_width <= tosa_level.MAX_KERNEL, "W should be smaller than or equal to MAX_KERNEL"); - DEBUG_INFO(OP, - "perform OpFFT2d, input.shapes=[[%d,%d,%d],[%d,%d,%d]], output.shapes=[[%d,%d,%d],[%d,%d,%d]]", - in_real_batch, in_real_height, in_real_width, - in_imag_batch, in_imag_height, in_imag_width, - out_real_batch, out_real_height, out_real_width, - out_imag_batch, out_imag_height, out_imag_width); + DEBUG_INFO(OP, "perform OpFFT2d, input.shapes=[[%d,%d,%d],[%d,%d,%d]], output.shapes=[[%d,%d,%d],[%d,%d,%d]]", + in_real_batch, in_real_height, in_real_width, in_imag_batch, in_imag_height, in_imag_width, + out_real_batch, out_real_height, out_real_width, out_imag_batch, out_imag_height, out_imag_width); OutEigenType sum_real, sum_imag, a, sign_val = 1.0; - if (attribute->inverse()) { + if (attribute->inverse()) + { sign_val = -1.0; } @@ -1772,7 +1756,8 @@ int OpFFT2d::eval() OutEigenType val_real = in_real_val(n, iy, ix); OutEigenType val_imag = in_imag_val(n, iy, ix); // Use explicit cast to ensure intermmediate calculations are completed using OutEigenType - a = sign_val * 2 * M_PI * ((iy * (OutEigenType)oy) / in_real_height + (ix * (OutEigenType)ox) / in_real_width); + a = sign_val * 2 * M_PI * + ((iy * (OutEigenType)oy) / in_real_height + (ix * (OutEigenType)ox) / in_real_width); sum_real += val_real * cos(a) + val_imag * sin(a); sum_imag += -val_real * sin(a) + val_imag * cos(a); } @@ -1787,9 +1772,7 @@ int OpFFT2d::eval() } template -OpRFFT2d::OpRFFT2d(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpRFFT2d::OpRFFT2d(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RFFT2D, id_) { setRequiredOperands(1, 2); @@ -1797,8 +1780,8 @@ OpRFFT2d::OpRFFT2d(SubgraphTraverser* sgt_, } template -OpRFFT2d::~OpRFFT2d() {} - +OpRFFT2d::~OpRFFT2d() +{} template int OpRFFT2d::checkTensorAttributes() @@ -1806,8 +1789,7 @@ int OpRFFT2d::checkTensorAttributes() if (validateRequiredOperands()) return 1; - if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]) || - validateRequiredRank(outputs[1])) + if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]) || validateRequiredRank(outputs[1])) { return 1; } @@ -1818,15 +1800,14 @@ int OpRFFT2d::checkTensorAttributes() return 1; } - in = dynamic_cast*>(inputs[0]); + in = dynamic_cast*>(inputs[0]); out_real = dynamic_cast*>(outputs[0]); out_imag = dynamic_cast*>(outputs[1]); ASSERT_MEM(in && out_real && out_imag); std::string msg; - if (check_fft_shape(in->getShape(), {}, - out_real->getShape(), out_imag->getShape(), msg)) + if (check_fft_shape(in->getShape(), {}, out_real->getShape(), out_imag->getShape(), msg)) { msg = "OpRFFT2d: " + msg; printNodeValidationError(msg.c_str()); @@ -1839,17 +1820,17 @@ int OpRFFT2d::checkTensorAttributes() template int OpRFFT2d::eval() { - int32_t in_batch = in->getShape()[0]; + int32_t in_batch = in->getShape()[0]; int32_t in_height = in->getShape()[1]; - int32_t in_width = in->getShape()[2]; + int32_t in_width = in->getShape()[2]; - int32_t out_real_batch = out_real->getShape()[0]; + int32_t out_real_batch = out_real->getShape()[0]; int32_t out_real_height = out_real->getShape()[1]; - int32_t out_real_width = out_real->getShape()[2]; + int32_t out_real_width = out_real->getShape()[2]; - int32_t out_imag_batch = out_imag->getShape()[0]; + int32_t out_imag_batch = out_imag->getShape()[0]; int32_t out_imag_height = out_imag->getShape()[1]; - int32_t out_imag_width = out_imag->getShape()[2]; + int32_t out_imag_width = out_imag->getShape()[2]; // Check Tosa Level auto tosa_level = g_func_config.tosa_level; @@ -1859,9 +1840,8 @@ int OpRFFT2d::eval() DEBUG_INFO(OP, "perform OpRFFT2d, input.shape=[%d,%d,%d], output_real.shape=[%d,%d,%d], " "output_imag.shape=[%d,%d,%d]", - in_batch, in_height, in_width, - out_real_batch, out_real_height, out_real_width, - out_imag_batch, out_imag_height, out_imag_width); + in_batch, in_height, in_width, out_real_batch, out_real_height, out_real_width, out_imag_batch, + out_imag_height, out_imag_width); OutEigenType sum_real, sum_imag, a; @@ -1931,7 +1911,7 @@ int OpTransposeConv2d::checkTensorAttributes() } ERROR_IF(outputs[0]->getDtype() != OutDtype, - "OpTransposeConv2d: Output data type not supported for this configuration of operator"); + "OpTransposeConv2d: Output data type not supported for this configuration of operator"); input = dynamic_cast*>(inputs[0]); weight = dynamic_cast*>(inputs[1]); @@ -1956,8 +1936,6 @@ int OpTransposeConv2d::checkTensorAttributes() return 1; } - - for (int32_t i : attribute->stride()) { if (i < 1) @@ -1993,7 +1971,8 @@ int OpTransposeConv2d::checkTensorAttributes() for (size_t i = 0; i < attribute->out_pad().size(); i++) { - ERROR_IF(attribute->out_pad()[i] <= -(weight->getShape()[(i / 2) + 1]), "OpTransposeConv2d: At least one out_pad value is larger than kernel size"); + ERROR_IF(attribute->out_pad()[i] <= -(weight->getShape()[(i / 2) + 1]), + "OpTransposeConv2d: At least one out_pad value is larger than kernel size"); } int32_t H = (IH - 1) * stride_y + out_pad_top + out_pad_bottom + kernel_h; @@ -2002,8 +1981,7 @@ int OpTransposeConv2d::checkTensorAttributes() if ((OH != H) || (OW != W)) { std::string msg = "OpTransposeConv2d: Mismatch between output shape provided and expected output shape (" + - std::to_string(H) + "," + - std::to_string(W) + ")"; + std::to_string(H) + "," + std::to_string(W) + ")"; printNodeValidationError(msg.c_str()); return 1; } @@ -2057,7 +2035,8 @@ int OpTransposeConv2d::eval() LEVEL_CHECK(f_height <= tosa_level.MAX_KERNEL, "KH should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(f_width <= tosa_level.MAX_KERNEL, "KW should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(out_pad_top <= tosa_level.MAX_KERNEL, "out_pad_top should be smaller than or equal to MAX_KERNEL"); - LEVEL_CHECK(out_pad_bottom <= tosa_level.MAX_KERNEL, "out_pad_bottom should be smaller than or equal to MAX_KERNEL"); + LEVEL_CHECK(out_pad_bottom <= tosa_level.MAX_KERNEL, + "out_pad_bottom should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(out_pad_left <= tosa_level.MAX_KERNEL, "out_pad_left should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(out_pad_right <= tosa_level.MAX_KERNEL, "out_pad_right should be smaller than or equal to MAX_KERNEL"); LEVEL_CHECK(stride_y <= tosa_level.MAX_STRIDE, "stride_y should be smaller than or equal to MAX_STRIDE"); @@ -2066,9 +2045,9 @@ int OpTransposeConv2d::eval() DEBUG_INFO(OP, "perform OpTransposeConv2d, input.shape=[%d,%d,%d,%d], weight.shape=[%d,%d,%d,%d], " "output.shape=[%d,%d,%d,%d], stride=[%d,%d], out_pad=[%d,%d,%d,%d]", - in_batch, in_height, in_width, in_channels, f_height, f_width, f_out_channels, f_in_channels, - out_batch, out_height, out_width, out_channels, stride_y, stride_x, out_pad_top, - out_pad_bottom, out_pad_left, out_pad_right); + in_batch, in_height, in_width, in_channels, f_height, f_width, f_out_channels, f_in_channels, out_batch, + out_height, out_width, out_channels, stride_y, stride_x, out_pad_top, out_pad_bottom, out_pad_left, + out_pad_right); TIn input_val = this->input->getTensor(); TWeight weight_val = this->weight->getTensor(); @@ -2126,8 +2105,8 @@ int OpTransposeConv2d::eval() if ((out_x >= 0 && out_x < out_width) && (out_y >= 0 && out_y < out_height)) { this->output->getTensor()(ob, out_y, out_x, oc) += - (OutEigenType) ((AccEigenType)input_val(ob, ih, iw, ic) * - (AccEigenType)weight_val(oc, fh, fw, ic)); + (OutEigenType)((AccEigenType)input_val(ob, ih, iw, ic) * + (AccEigenType)weight_val(oc, fh, fw, ic)); } } } @@ -2162,7 +2141,7 @@ DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT8, INT32); DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, INT16, INT32); DEF_INSTANTIATE_TWO_TYPE(OpAvgPool2d, FP64, FP64); - // [in_t, weight_t, out_t] +// [in_t, weight_t, out_t] DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP16, FP16, FP16); DEF_INSTANTIATE_THREE_TYPE(OpConv2d, FP16, FP16, FP32); DEF_INSTANTIATE_THREE_TYPE(OpConv2d, BF16, BF16, FP32); diff --git a/reference_model/src/ops/tensor_ops.h b/reference_model/src/ops/tensor_ops.h index df53f2b..f5fcd7f 100644 --- a/reference_model/src/ops/tensor_ops.h +++ b/reference_model/src/ops/tensor_ops.h @@ -55,11 +55,11 @@ public: virtual int checkTensorAttributes(); virtual int eval(); - using InEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType - using OutEigenType = typename GetEigenType::type; - using TIn = Eigen::Tensor; - using TOut = Eigen::Tensor; + using InEigenType = typename GetEigenType::type; + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TOut = Eigen::Tensor; static constexpr int64_t QMin = GetQMin::value; static constexpr int64_t QMax = GetQMax::value; @@ -71,7 +71,8 @@ protected: protected: // return a 1D [N] tensor that describes a how many valid elements covered in the input space - ETensor1 calculate_div_map_1d(int in_size, int out_size, int kernel_size, int stride, int32_t padding_left, int32_t padding_right); + ETensor1 calculate_div_map_1d( + int in_size, int out_size, int kernel_size, int stride, int32_t padding_left, int32_t padding_right); }; template @@ -86,7 +87,7 @@ public: using InEigenType = typename GetEigenType::type; using WeightEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; using TWeight = Eigen::Tensor; @@ -116,7 +117,7 @@ public: using InEigenType = typename GetEigenType::type; using WeightEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; using TWeight = Eigen::Tensor; @@ -146,7 +147,7 @@ public: using InEigenType = typename GetEigenType::type; using WeightEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; using TWeight = Eigen::Tensor; @@ -174,14 +175,14 @@ public: virtual int checkTensorAttributes() final; virtual int eval() final; - using InEigenType = typename GetEigenType::type; - using WeightEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType - using OutEigenType = typename GetEigenType::type; - using TIn = Eigen::Tensor; - using TWeight = Eigen::Tensor; - using TBias = Eigen::Tensor; - using TOut = Eigen::Tensor; + using InEigenType = typename GetEigenType::type; + using WeightEigenType = typename GetEigenType::type; + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TWeight = Eigen::Tensor; + using TBias = Eigen::Tensor; + using TOut = Eigen::Tensor; static constexpr int64_t AccQMin = GetQMin::value; static constexpr int64_t AccQMax = GetQMax::value; @@ -206,7 +207,7 @@ public: virtual int eval() final; using InEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; using TOut = Eigen::Tensor; @@ -258,10 +259,10 @@ public: virtual int checkTensorAttributes() final; virtual int eval() final; - using InEigenType = typename GetEigenType::type; - using OutEigenType = typename GetEigenType::type; - using TIn = Eigen::Tensor; - using TOut = Eigen::Tensor; + using InEigenType = typename GetEigenType::type; + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TOut = Eigen::Tensor; protected: TosaReference::TensorTemplate* in_real; @@ -281,10 +282,10 @@ public: virtual int checkTensorAttributes() final; virtual int eval() final; - using InEigenType = typename GetEigenType::type; - using OutEigenType = typename GetEigenType::type; - using TIn = Eigen::Tensor; - using TOut = Eigen::Tensor; + using InEigenType = typename GetEigenType::type; + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TOut = Eigen::Tensor; protected: TosaReference::TensorTemplate* in; @@ -304,7 +305,7 @@ public: using InEigenType = typename GetEigenType::type; using WeightEigenType = typename GetEigenType::type; - using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType + using AccEigenType = typename GetAccEigenType::type; // Note: different from GetEigenType using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; using TWeight = Eigen::Tensor; diff --git a/reference_model/src/ops/type_conversion.cc b/reference_model/src/ops/type_conversion.cc index fce8e7c..9464fd9 100644 --- a/reference_model/src/ops/type_conversion.cc +++ b/reference_model/src/ops/type_conversion.cc @@ -14,20 +14,18 @@ // limitations under the License. #include "type_conversion.h" -#include "quant_util.h" #include "arith_util.h" +#include "half.hpp" +#include "quant_util.h" #include "template_types.h" #include -#include "half.hpp" using namespace TosaReference; using namespace Eigen; using namespace tosa; template -OpRescale::OpRescale(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpRescale::OpRescale(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RESCALE, id_) { setRequiredOperands(1, 1); @@ -160,12 +158,13 @@ int OpRescale::eval() else scaled = TosaReference::QuantUtil::apply_scale_16(input_zp_shifted, channel_multiplier, channel_shift); - int64_t res_in_64 = static_cast(scaled) + output_zp; + int64_t res_in_64 = static_cast(scaled) + output_zp; int64_t i32_max_in_64 = static_cast(std::numeric_limits::max()); int64_t i32_min_in_64 = static_cast(std::numeric_limits::min()); if (res_in_64 > i32_max_in_64 || res_in_64 < i32_min_in_64) { - std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + std::to_string(output_zp) + "] not in i32 range"; + std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + + std::to_string(output_zp) + "] not in i32 range"; throw desc; } OutEigenType out_val = static_cast(res_in_64); @@ -201,12 +200,13 @@ int OpRescale::eval() else scaled = TosaReference::QuantUtil::apply_scale_16(input_zp_shifted, tensor_multiplier, tensor_shift); - int64_t res_in_64 = static_cast(scaled) + output_zp; + int64_t res_in_64 = static_cast(scaled) + output_zp; int64_t i32_max_in_64 = static_cast(std::numeric_limits::max()); int64_t i32_min_in_64 = static_cast(std::numeric_limits::min()); if (res_in_64 > i32_max_in_64 || res_in_64 < i32_min_in_64) { - std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + std::to_string(output_zp) + "] not in i32 range"; + std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + + std::to_string(output_zp) + "] not in i32 range"; throw desc; } @@ -234,9 +234,7 @@ int OpRescale::eval() } template -OpCast::OpCast(SubgraphTraverser* sgt_, - TosaAttributeBase* attribute_, - uint64_t id_) +OpCast::OpCast(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_CAST, id_) { setRequiredOperands(1, 1); @@ -315,7 +313,7 @@ CastHelper::CastHelper() // Integer data converted to fp16 (stored as fp32) fcn = [](InEigenType in) -> float { half_float::half h = half_float::half(in); - float out = half_float::half_cast(h); + float out = half_float::half_cast(h); return out; }; } @@ -354,10 +352,10 @@ CastHelper::CastHelper() fcn = [](float in) -> OutEigenType { // Cast from float representation back to half_float before rounding half_float::half h = half_float::half(in); - h = std::rint(h); - OutEigenType out = half_float::half_cast(h); - out = std::max(out, OutMin); - out = std::min(out, OutMax); + h = std::rint(h); + OutEigenType out = half_float::half_cast(h); + out = std::max(out, OutMin); + out = std::min(out, OutMax); return out; }; } @@ -365,9 +363,7 @@ CastHelper::CastHelper() CastHelper::CastHelper() { // No-op since fp16 values treated internally as their fp32 representation - fcn = [](float in) -> OutEigenType { - return in; - }; + fcn = [](float in) -> OutEigenType { return in; }; } template @@ -385,9 +381,7 @@ CastHelper::CastHelper() CastHelper::CastHelper() { // No-op since bf16 values treated as truncated fp32 internally - fcn = [](InEigenType in) -> OutEigenType { - return in; - }; + fcn = [](InEigenType in) -> OutEigenType { return in; }; } template -- cgit v1.2.1