diff --git a/include/tim/vx/ops/depth2space.h b/include/tim/vx/ops/depth2space.h index 275b8ae..96753bc 100644 --- a/include/tim/vx/ops/depth2space.h +++ b/include/tim/vx/ops/depth2space.h @@ -47,13 +47,20 @@ namespace ops { class DepthToSpace : public DirectMapOp { public: + enum depth2space_mode { + CRD_mode = 0, + DCR_mode = 1, + }; DepthToSpace(Graph* Graph, int block_size, DataLayout layout = DataLayout::WHCN); + DepthToSpace(Graph* Graph, int block_size, depth2space_mode mode, + DataLayout layout = DataLayout::WHCN); std::shared_ptr Clone(std::shared_ptr& graph) const override; protected: int block_size_; + depth2space_mode mode_; }; } // namespace ops diff --git a/src/tim/vx/ops/depth2space.cc b/src/tim/vx/ops/depth2space.cc index 20b0328..6d366c8 100644 --- a/src/tim/vx/ops/depth2space.cc +++ b/src/tim/vx/ops/depth2space.cc @@ -30,15 +30,22 @@ namespace tim { namespace vx { namespace ops { -DepthToSpace::DepthToSpace(Graph* graph, int block_size, DataLayout layout) - : DirectMapOp(graph, VSI_NN_OP_DEPTH2SPACE, 0, 0, layout), - block_size_(block_size) { +DepthToSpace::DepthToSpace(Graph* Graph, int block_size, DataLayout layout) + : DepthToSpace(Graph, block_size, CRD_mode, layout) {} + +DepthToSpace::DepthToSpace(Graph* Graph, int block_size, depth2space_mode mode, + DataLayout layout) + : DirectMapOp(Graph, VSI_NN_OP_DEPTH2SPACE, 0, 0, layout), + block_size_(block_size), + mode_(mode) { this->impl()->node()->nn_param.depth2space.block_size = block_size_; + this->impl()->node()->nn_param.depth2space.mode = + (vsi_nn_depth2space_mode_e)mode_; } std::shared_ptr DepthToSpace::Clone( std::shared_ptr& graph) const { - return graph->CreateOperation(this->block_size_, + return graph->CreateOperation(this->block_size_, this->mode_, this->impl_->layout_); } diff --git a/src/tim/vx/ops/depth2space_test.cc b/src/tim/vx/ops/depth2space_test.cc new file mode 100644 index 0000000..59b63d6 --- /dev/null +++ b/src/tim/vx/ops/depth2space_test.cc @@ -0,0 +1,65 @@ +/**************************************************************************** +* +* 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/depth2space.h" +#include "tim/vx/types.h" +#include "test_utils.h" + +#include "gtest/gtest.h" + +TEST(DepthToSpace, shape_6_4_2_1_int32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({2, 1, 4, 1}); + tim::vx::ShapeType out_shape({4, 2, 1, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, in_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, out_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = { + 3, 5, 4, 8, 3, 5, 4, 8, + }; + std::vector golden = { + 3, 4, 5, 8, 3, 4, 5, 8, + }; + + EXPECT_TRUE( + input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4)); + auto op = graph->CreateOperation( + 2, tim::vx::ops::DepthToSpace::DCR_mode); + (*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