Suported specifying CRD_mode & DCR_mode in depthtospace (#362)

https://github.com/VeriSilicon/TIM-VX/issues/304

Signed-off-by: Chen Xin <jack.chen@verisilicon.com>

Co-authored-by: Chen Xin <jack.chen@verisilicon.com>
This commit is contained in:
chxin66 2022-04-14 19:53:32 +08:00 committed by GitHub
parent 0dc38eac2e
commit 479fc576ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 4 deletions

View File

@ -47,13 +47,20 @@ namespace ops {
class DepthToSpace : public DirectMapOp { class DepthToSpace : public DirectMapOp {
public: public:
enum depth2space_mode {
CRD_mode = 0,
DCR_mode = 1,
};
DepthToSpace(Graph* Graph, int block_size, DepthToSpace(Graph* Graph, int block_size,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
DepthToSpace(Graph* Graph, int block_size, depth2space_mode mode,
DataLayout layout = DataLayout::WHCN);
std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override; std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
protected: protected:
int block_size_; int block_size_;
depth2space_mode mode_;
}; };
} // namespace ops } // namespace ops

View File

@ -30,15 +30,22 @@ namespace tim {
namespace vx { namespace vx {
namespace ops { namespace ops {
DepthToSpace::DepthToSpace(Graph* graph, int block_size, DataLayout layout) DepthToSpace::DepthToSpace(Graph* Graph, int block_size, DataLayout layout)
: DirectMapOp(graph, VSI_NN_OP_DEPTH2SPACE, 0, 0, layout), : DepthToSpace(Graph, block_size, CRD_mode, layout) {}
block_size_(block_size) {
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.block_size = block_size_;
this->impl()->node()->nn_param.depth2space.mode =
(vsi_nn_depth2space_mode_e)mode_;
} }
std::shared_ptr<Operation> DepthToSpace::Clone( std::shared_ptr<Operation> DepthToSpace::Clone(
std::shared_ptr<Graph>& graph) const { std::shared_ptr<Graph>& graph) const {
return graph->CreateOperation<DepthToSpace>(this->block_size_, return graph->CreateOperation<DepthToSpace>(this->block_size_, this->mode_,
this->impl_->layout_); this->impl_->layout_);
} }

View File

@ -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<float> in_data = {
3, 5, 4, 8, 3, 5, 4, 8,
};
std::vector<float> 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<tim::vx::ops::DepthToSpace>(
2, tim::vx::ops::DepthToSpace::DCR_mode);
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(golden.size());
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
EXPECT_EQ(golden, output);
}