From e8ca6b8ee376b6f201f257e9e8ed0d6d1d721a93 Mon Sep 17 00:00:00 2001 From: chxin66 <57057788+chxin66@users.noreply.github.com> Date: Tue, 12 Apr 2022 15:42:58 +0800 Subject: [PATCH] Added param step for slice & added unit test (#352) Signed-off-by: Chen Xin --- include/tim/vx/ops/slice.h | 11 ++- src/tim/vx/ops/slice.cc | 30 +++++++- src/tim/vx/ops/slice_test.cc | 144 +++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 src/tim/vx/ops/slice_test.cc diff --git a/include/tim/vx/ops/slice.h b/include/tim/vx/ops/slice.h index 880ebd8..a67b3c6 100644 --- a/include/tim/vx/ops/slice.h +++ b/include/tim/vx/ops/slice.h @@ -40,17 +40,20 @@ namespace ops { class Slice : public DirectMapOp { public: - Slice(Graph* graph, - uint32_t dims, - const std::vector& start, + Slice(Graph* graph, uint32_t dims, const std::vector& start, const std::vector& length); + Slice(Graph* graph, uint32_t dims, const std::vector& start, + const std::vector& length, const std::vector& step); - std::shared_ptr Clone(std::shared_ptr& graph) const override; + std::shared_ptr Clone( + std::shared_ptr& graph) const override; protected: uint32_t dims_; const std::vector start_; const std::vector length_; + std::vector end_dims_; + const std::vector step_; }; } // namespace ops diff --git a/src/tim/vx/ops/slice.cc b/src/tim/vx/ops/slice.cc index 94d35bd..32b6ea5 100644 --- a/src/tim/vx/ops/slice.cc +++ b/src/tim/vx/ops/slice.cc @@ -43,8 +43,36 @@ Slice::Slice(Graph* graph, uint32_t dims, const std::vector& start, reinterpret_cast(length_.data()); } +Slice::Slice(Graph* graph, uint32_t dims, const std::vector& start, + const std::vector& length, + const std::vector& step) + : DirectMapOp(graph, VSI_NN_OP_STRIDED_SLICE), + dims_(dims), + start_(std::move(start)), + length_(std::move(length)), + step_(std::move(step)) { + for (uint32_t i = 0; i < length_.size(); ++i) { + end_dims_.push_back(start_.at(i) + length_.at(i)); + } + this->impl()->node()->nn_param.strided_slice.begin_mask = 0; + this->impl()->node()->nn_param.strided_slice.end_mask = 0; + this->impl()->node()->nn_param.strided_slice.shrink_axis_mask = 0; + this->impl()->node()->nn_param.strided_slice.begin_dims = start_.data(); + this->impl()->node()->nn_param.strided_slice.begin_dims_num = start_.size(); + this->impl()->node()->nn_param.strided_slice.end_dims = end_dims_.data(); + this->impl()->node()->nn_param.strided_slice.end_dims_num = end_dims_.size(); + this->impl()->node()->nn_param.strided_slice.stride_dims = step_.data(); + this->impl()->node()->nn_param.strided_slice.stride_dims_num = step_.size(); +} + std::shared_ptr Slice::Clone(std::shared_ptr& graph) const { - return graph->CreateOperation(this->dims_, this->start_, this->length_); + if (this->impl()->kind_ == VSI_NN_OP_STRIDED_SLICE) { + return graph->CreateOperation(this->dims_, this->start_, + this->length_, this->step_); + } else { + return graph->CreateOperation(this->dims_, this->start_, + this->length_); + } } } // namespace ops diff --git a/src/tim/vx/ops/slice_test.cc b/src/tim/vx/ops/slice_test.cc new file mode 100644 index 0000000..656ed89 --- /dev/null +++ b/src/tim/vx/ops/slice_test.cc @@ -0,0 +1,144 @@ +/**************************************************************************** +* +* Copyright (c) 2021 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#include "tim/vx/context.h" +#include "tim/vx/graph.h" +#include "tim/vx/ops/slice.h" + +#include "gtest/gtest.h" + +TEST(Slice, shape_2_3) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({4, 3}); + tim::vx::ShapeType output_shape({2, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 1, 4, 2, 5, + 3, 6, 1, 4, + 2, 5, 3, 6, + }; + std::vector golden = { + 1, 4, + }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), + in_data.size() * sizeof(float))); + std::vector start = {0, 0}; + std::vector length = {2, 1}; + auto op = graph->CreateOperation(0, start, length); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(Slice, shape_2_3_step_1_1) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({4, 3}); + tim::vx::ShapeType output_shape({2, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 1, 4, 2, 5, + 3, 6, 1, 4, + 2, 5, 3, 6, + }; + std::vector golden = { + 1, 4, + }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), + in_data.size() * sizeof(float))); + std::vector start = {0, 0}; + std::vector length = {2, 1}; + std::vector step = {1, 1}; + auto op = graph->CreateOperation(0, start, length, step); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(Slice, shape_4_3_step_2_1) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType input_shape({4, 3}); + tim::vx::ShapeType output_shape({2, 2}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, input_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, output_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 1, 4, 2, 5, + 3, 6, 1, 4, + 2, 5, 3, 6, + }; + std::vector golden = { + 1, 2, 2, 3, + }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), + in_data.size() * sizeof(float))); + std::vector start = {0, 0}; + std::vector length = {4, 3}; + std::vector step = {2, 2}; + auto op = graph->CreateOperation(0, start, length, step); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} \ No newline at end of file