From a21b2e88d19d8cb11a9120d40bacbb594d600565 Mon Sep 17 00:00:00 2001 From: Won Jeon Date: Thu, 10 Aug 2023 10:33:01 +0000 Subject: Add DIM operator to reference model Signed-off-by: Won Jeon Change-Id: Iea11ee5d3d98773e9c5e9b827593c05afb41ce3b --- reference_model/src/ops/data_layout.cc | 62 ++++++++++++++++++++++++++++++++ reference_model/src/ops/data_layout.h | 21 +++++++++++ reference_model/src/ops/op_factory.cc | 9 +++++ reference_model/src/ops/template_types.h | 5 +++ 4 files changed, 97 insertions(+) (limited to 'reference_model/src/ops') diff --git a/reference_model/src/ops/data_layout.cc b/reference_model/src/ops/data_layout.cc index bc97c89..2d1fdb0 100644 --- a/reference_model/src/ops/data_layout.cc +++ b/reference_model/src/ops/data_layout.cc @@ -209,6 +209,60 @@ int OpPad::eval() return GraphNode::eval(); } +template +OpDim::OpDim(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) + : GraphNode(sgt_, Op_DIM, id_) +{ + setRequiredOperands(1, 1); + + INIT_ATTRIBUTE(Axis); +} + +template +OpDim::~OpDim() +{ + if (attribute) + delete attribute; +} + +template +int OpDim::checkTensorAttributes() +{ + // Check Tosa Level + auto tosa_level = g_func_config.tosa_level; + LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK"); + + if (validateRequiredOperands()) + return 1; + + if (validateRequiredRank(inputs[0])) + return 1; + + if (attribute->axis() < 0 || (size_t)attribute->axis() >= Rank) + { + printNodeValidationError("OpDim: axis must between [0, input_rank - 1]"); + return 1; + } + + in = dynamic_cast*>(inputs[0]); + out = dynamic_cast*>(outputs[0]); + + ASSERT_MEM(in && out); + + return 0; +} + +template +int OpDim::eval() +{ + int32_t axis = attribute->axis(); + int64_t out_val = in->getShape()[axis]; + + this->out->getTensor().setConstant(out_val); + + return GraphNode::eval(); +} + template OpReshape::OpReshape(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RESHAPE, id_) @@ -780,6 +834,14 @@ DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpPad, INT32); DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpPad, BOOL); DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpPad, FP64); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, FP16); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, BF16); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, FP32); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT8); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT16); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT32); +DEF_INSTANTIATE_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, BOOL); + DEF_INSTANTIATE_RESHAPE(OpReshape, FP16); DEF_INSTANTIATE_RESHAPE(OpReshape, BF16); DEF_INSTANTIATE_RESHAPE(OpReshape, FP32); diff --git a/reference_model/src/ops/data_layout.h b/reference_model/src/ops/data_layout.h index 94ce248..024f9a2 100644 --- a/reference_model/src/ops/data_layout.h +++ b/reference_model/src/ops/data_layout.h @@ -66,6 +66,27 @@ protected: TosaPadAttribute* attribute; }; +template +class OpDim : public GraphNode +{ +public: + OpDim(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); + virtual ~OpDim(); + + virtual int checkTensorAttributes(); + virtual int eval(); + + using InEigenType = typename GetEigenType::type; + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TOut = Eigen::Tensor; + +protected: + TosaReference::TensorTemplate* in; + TosaReference::TensorTemplate* out; + TosaAxisAttribute* attribute; +}; + template class OpReshape : public GraphNode { diff --git a/reference_model/src/ops/op_factory.cc b/reference_model/src/ops/op_factory.cc index a3069dc..d834b74 100644 --- a/reference_model/src/ops/op_factory.cc +++ b/reference_model/src/ops/op_factory.cc @@ -419,6 +419,15 @@ GraphNode* OpFactory::newOp(SubgraphTraverser* sgt, DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpPad, BOOL); DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpPad, FP64); break; + case Op_DIM: + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, FP16); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, BF16); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, FP32); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT32); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT8); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, INT16); + DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OpDim, BOOL); + break; case Op_RESHAPE: DEF_FACTORY_RESHAPE(OpReshape, FP16); DEF_FACTORY_RESHAPE(OpReshape, BF16); diff --git a/reference_model/src/ops/template_types.h b/reference_model/src/ops/template_types.h index 6dd6e76..342d5c2 100644 --- a/reference_model/src/ops/template_types.h +++ b/reference_model/src/ops/template_types.h @@ -98,6 +98,11 @@ struct GetEigenType using type = int64_t; }; template <> +struct GetEigenType +{ + using type = int64_t; +}; +template <> struct GetEigenType { using type = bool; -- cgit v1.2.1