From 97f1c0e602049ee43537ad6c8a08f476eb5c722b Mon Sep 17 00:00:00 2001 From: TatWai Chong Date: Mon, 5 Feb 2024 11:56:46 -0800 Subject: Fix comparing unsigned int and int in slice evaluation also check if tensors of start and size are allocated. Change-Id: I6a72d11ebcb8d0725fe267058dfd792102459427 Signed-off-by: TatWai Chong --- reference_model/src/ops/data_layout.cc | 22 +++++++++++++++------- reference_model/src/ops/data_layout.h | 6 +++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/reference_model/src/ops/data_layout.cc b/reference_model/src/ops/data_layout.cc index ddf0713..3bff047 100644 --- a/reference_model/src/ops/data_layout.cc +++ b/reference_model/src/ops/data_layout.cc @@ -453,11 +453,11 @@ int OpSlice::checkTensorAttributes() } in = dynamic_cast*>(inputs[0]); - start = dynamic_cast*>(inputs[1]); - size = dynamic_cast*>(inputs[2]); + start = dynamic_cast*>(inputs[1]); + size = dynamic_cast*>(inputs[2]); out = dynamic_cast*>(outputs[0]); - ASSERT_MEM(in && out); + ASSERT_MEM(in && out && start && size); return 0; } @@ -465,13 +465,21 @@ int OpSlice::checkTensorAttributes() template int OpSlice::eval() { - ERROR_IF(start->getElementCount() != in->getRank(), "OpSlice: start array length needs to be rank(input)"); - ERROR_IF(size->getElementCount() != in->getRank(), "OpSlice: size array length needs to be rank(input)"); + TSlicing start_tensor = start->getTensor(); + TSlicing size_tensor = size->getTensor(); + + // According to https://eigen.tuxfamily.org/dox/unsupported/eigen_tensors.html + // The type of size() is ::Index, but can always handily use it like an int. + // However, apply explicit cast to int32_t is preferred. + ERROR_IF(static_cast(start_tensor.size()) != in->getRank(), + "OpSlice: start array length needs to be rank(input)"); + ERROR_IF(static_cast(size_tensor.size()) != in->getRank(), + "OpSlice: size array length needs to be rank(input)"); for (int32_t i = 0; i < in->getRank(); i++) { - int32_t b = start->getTensor()(i); - int32_t s = size->getTensor()(i); + int32_t b = start_tensor(i); + int32_t s = size_tensor(i); ERROR_IF(b < 0 || b >= in->getShape()[i], "OpSlice: start out of boundary"); ERROR_IF((b + s) < 0 || (b + s) > in->getShape()[i], "OpSlice: (start+size) out of boundary"); ERROR_IF(s <= 0, "OpSlice: output must be positive"); diff --git a/reference_model/src/ops/data_layout.h b/reference_model/src/ops/data_layout.h index 6ab5ebd..dee2ae0 100644 --- a/reference_model/src/ops/data_layout.h +++ b/reference_model/src/ops/data_layout.h @@ -149,14 +149,14 @@ public: using InEigenShapeType = typename GetEigenType::type; using OutEigenType = typename GetEigenType::type; using TIn = Eigen::Tensor; - using TInShape = Eigen::Tensor; + using TSlicing = Eigen::Tensor; using TOut = Eigen::Tensor; protected: Eigen::array begin_array; Eigen::array size_array; - TosaReference::TensorTemplate* start; - TosaReference::TensorTemplate* size; + TosaReference::TensorTemplate* start; + TosaReference::TensorTemplate* size; TosaReference::TensorTemplate* in; TosaReference::TensorTemplate* out; }; -- cgit v1.2.1