From e095da79cc3575c8cbf818acd8a856ba136b0927 Mon Sep 17 00:00:00 2001 From: Tai Ly Date: Thu, 25 Jan 2024 22:00:18 +0000 Subject: [ref model] Change PadOp's padding to Shape Changed to use pad input of PadOp for testing. The pad input is now a tensor of tosa.shape type. moved padding error checking from checkTensorAttributes to eval modified pad's PadOutputShapeMismatch test generation to avoid generating output shapes with dimensions <= 0 Signed-off-by: Tai Ly Change-Id: I437c86d9a012903458a648667f6693db67b97d76 --- reference_model/src/ops/data_layout.cc | 36 +++++++++++++++++----------------- reference_model/src/ops/data_layout.h | 11 +++++++---- 2 files changed, 25 insertions(+), 22 deletions(-) (limited to 'reference_model') diff --git a/reference_model/src/ops/data_layout.cc b/reference_model/src/ops/data_layout.cc index a4b4e0a..14f2918 100644 --- a/reference_model/src/ops/data_layout.cc +++ b/reference_model/src/ops/data_layout.cc @@ -126,7 +126,7 @@ template OpPad::OpPad(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_PAD, id_) { - setRequiredOperands(1, 1); + setRequiredOperands(2, 1); setRequiredRank(1); INIT_ATTRIBUTE(Pad); @@ -158,25 +158,11 @@ int OpPad::checkTensorAttributes() return 1; } - in = dynamic_cast*>(inputs[0]); - out = dynamic_cast*>(outputs[0]); + in = dynamic_cast*>(inputs[0]); + padding = dynamic_cast*>(inputs[1]); + out = dynamic_cast*>(outputs[0]); ASSERT_MEM(in && out); - // padding in spec is 2D array in shape of [Rank, 2] - // Reference model implement this as 1D array of [Rank * 2], with ordering: - // [Rank0_front, Rank0_back, Rank1_front, Rank1_back, ..., Rank(N-1)_front, Rank(N-1)_back] - ERROR_IF(attribute->padding().size() != (Rank * 2), "OpPad: padding length needs to be (rank(input1) * 2)"); - - for (int i = 0; i < Rank; i++) - { - int32_t pad_front = attribute->padding()[2 * i]; - int32_t pad_back = attribute->padding()[2 * i + 1]; - ERROR_IF((pad_front < 0) || (pad_back < 0), "OpPad: padding can't be smaller than 0"); - ERROR_IF(out->getShape()[i] != pad_front + in->getShape()[i] + pad_back, - "OpPad: output shape not equal to input plus padding"); - paddings_array[i] = std::make_pair(pad_front, pad_back); - } - return 0; } @@ -204,6 +190,20 @@ int OpPad::eval() break; } + // padding is an 1D array of [Rank * 2], with ordering: + // [Rank0_front, Rank0_back, Rank1_front, Rank1_back, ..., Rank(N-1)_front, Rank(N-1)_back] + TPadding padding_val = this->padding->getTensor(); + ERROR_IF(padding_val.size() != (Rank * 2), "OpPad: padding length needs to be (rank(input1) * 2)"); + for (int i = 0; i < Rank; i++) + { + auto pad_front = padding_val(2 * i); + auto pad_back = padding_val(2 * i + 1); + ERROR_IF((pad_front < 0) || (pad_back < 0), "OpPad: padding can't be smaller than 0"); + ERROR_IF(out->getShape()[i] != pad_front + in->getShape()[i] + pad_back, + "OpPad: output shape not equal to input plus padding"); + paddings_array[i] = std::make_pair(pad_front, pad_back); + } + this->out->getTensor() = this->in->getTensor().pad(this->paddings_array, pad_value); return GraphNode::eval(); diff --git a/reference_model/src/ops/data_layout.h b/reference_model/src/ops/data_layout.h index 9341709..e085b8e 100644 --- a/reference_model/src/ops/data_layout.h +++ b/reference_model/src/ops/data_layout.h @@ -54,14 +54,17 @@ public: virtual int checkTensorAttributes(); virtual int eval(); - using InEigenType = typename GetEigenType::type; - using OutEigenType = typename GetEigenType::type; - using TIn = Eigen::Tensor; - using TOut = Eigen::Tensor; + using InEigenType = typename GetEigenType::type; + using InEigenShapeType = typename GetEigenType::type; + using OutEigenType = typename GetEigenType::type; + using TIn = Eigen::Tensor; + using TPadding = Eigen::Tensor; + using TOut = Eigen::Tensor; protected: Eigen::array, Rank> paddings_array; TosaReference::TensorTemplate* in; + TosaReference::TensorTemplate* padding; TosaReference::TensorTemplate* out; TosaPadAttribute* attribute; }; -- cgit v1.2.1