From 6f2e92ffa6b2303fc21b0284dd91ccfa0b5f8705 Mon Sep 17 00:00:00 2001 From: Chen Xin Date: Mon, 30 Aug 2021 17:28:40 +0800 Subject: [PATCH] Add shuffle_channel support & test for tim::vx Signed-off-by: Chen Xin --- .clang-format | 1 + include/tim/vx/ops/shuffle_channel.h | 51 ++++++ src/tim/vx/graph.cc | 22 +-- src/tim/vx/ops/README.md | 2 + src/tim/vx/ops/addn.cc | 4 +- src/tim/vx/ops/addn_test.cc | 7 +- src/tim/vx/ops/shuffle_channel.cc | 47 ++++++ src/tim/vx/ops/shuffle_channel_test.cc | 205 +++++++++++++++++++++++++ 8 files changed, 324 insertions(+), 15 deletions(-) create mode 100644 include/tim/vx/ops/shuffle_channel.h create mode 100644 src/tim/vx/ops/shuffle_channel.cc create mode 100644 src/tim/vx/ops/shuffle_channel_test.cc diff --git a/.clang-format b/.clang-format index 2b3234a..577593d 100644 --- a/.clang-format +++ b/.clang-format @@ -3,3 +3,4 @@ BasedOnStyle: Google DerivePointerAlignment: false ReflowComments: false +SortIncludes: false \ No newline at end of file diff --git a/include/tim/vx/ops/shuffle_channel.h b/include/tim/vx/ops/shuffle_channel.h new file mode 100644 index 0000000..382aa89 --- /dev/null +++ b/include/tim/vx/ops/shuffle_channel.h @@ -0,0 +1,51 @@ +/**************************************************************************** +* +* 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. +* +*****************************************************************************/ +#ifndef TIM_VX_OPS_SHUFFLE_H_ +#define TIM_VX_OPS_SHUFFLE_H_ +#include "tim/vx/operation.h" + +namespace tim { +namespace vx { +namespace ops { + +/** + * ## Channel_Shuffle + * + * ``` + * channel_shuffle(in_tensor, num_groups, index_axis) : output_channel[k * num_groups + g] = input_channel[g * group_size + k] + where group_size = num_channels(Corresponding index_axis) / num_groups. The number of channels must be divisible by num_groups + * ``` + */ + +class shuffle_channel : public Operation { + public: + explicit shuffle_channel(Graph* graph, int32_t num_groups, int32_t index_axis); + std::shared_ptr Clone(std::shared_ptr& graph) const override; +}; + +} // namespace ops +} // namespace vx +} // namespace tim + +#endif /* TIM_VX_OPS_SHUFFLE_H_ */ \ No newline at end of file diff --git a/src/tim/vx/graph.cc b/src/tim/vx/graph.cc index 72651f6..e8db240 100644 --- a/src/tim/vx/graph.cc +++ b/src/tim/vx/graph.cc @@ -22,14 +22,12 @@ * *****************************************************************************/ #include "tim/vx/graph.h" - #include #include "context_private.h" #include "graph_private.h" -#include "tensor_private.h" #include "operation_private.h" - +#include "tensor_private.h" #include "tim/vx/context.h" #include "tim/vx/ops/nbg.h" #include "vsi_nn_pub.h" @@ -122,14 +120,16 @@ bool GraphImpl::Compile() { auto minor = vsi_nn_GetVersionMinor(); auto patch = vsi_nn_GetVersionPatch(); - vsi_nn_SetGraphVersion(graph_,major,minor,patch); + vsi_nn_SetGraphVersion(graph_, major, minor, patch); std::call_once(setio_once_, [&status, this]() { - status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), this->inputs_.size()) && - vsi_nn_SetGraphOutputs(this->graph_, this->outputs_.data(), this->outputs_.size())); + status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), + this->inputs_.size()) && + vsi_nn_SetGraphOutputs(this->graph_, this->outputs_.data(), + this->outputs_.size())); }); - std::call_once(setup_once_, [&status, this](){ + std::call_once(setup_once_, [&status, this]() { status = (VSI_SUCCESS == vsi_nn_SetupGraph(this->graph_, true)); }); @@ -143,11 +143,13 @@ bool GraphImpl::Compile() { bool GraphImpl::CompileToBinary(void* buf, size_t* size) { bool status = true; std::call_once(setio_once_, [&status, this]() { - status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), this->inputs_.size()) && - vsi_nn_SetGraphOutputs(this->graph_,this->outputs_.data(), this->outputs_.size())); + status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), + this->inputs_.size()) && + vsi_nn_SetGraphOutputs(this->graph_, this->outputs_.data(), + this->outputs_.size())); }); - std::call_once(setup_once_, [&status, this](){ + std::call_once(setup_once_, [&status, this]() { status = (VSI_SUCCESS == vsi_nn_SetupGraph(this->graph_, true)); }); diff --git a/src/tim/vx/ops/README.md b/src/tim/vx/ops/README.md index 6e307a4..d04791f 100644 --- a/src/tim/vx/ops/README.md +++ b/src/tim/vx/ops/README.md @@ -97,6 +97,8 @@ Unstack|UNSTACK|Mapped|[tf.unstack](https://tensorflow.google.cn/api_docs/python Tile|TILE|Mapped|[tf.tile](https://tensorflow.google.cn/api_docs/python/tf/tile) GroupedConv2d|GROUPED_CONV2D|Mapped|[ANEURALNETWORKS_GROUPED_CONV_2D](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a847acf8d9f3d2343328c3dbe6d447c50) SpatialTransformer|SPATIAL_TRANSFORMER|Mapped|[SpatialTransformer](https://github.com/daerduoCarey/SpatialTransformerLayer) +shuffle_channel|SHUFFLECHANNEL|Mapped|[ANEURALNETWORKS_CHANNEL_SHUFFLE](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a5b993c1211c4b1bc52fb595a3025251d) +Gelu|GELU|Mapped|[tf.nn.gelu](https://tensorflow.google.cn/api_docs/python/tf/nn/gelu) ||PROPOSAL|Planned 21Q3|[Faster-RCNN Proposal Layer](https://github.com/intel/caffe/blob/master/examples/faster-rcnn/lib/rpn/proposal_layer.py) ||ROI_POOL|Planned 21Q3|[ANEURALNETWORKS_ROI_POOLING](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a6736198af337b2efbdb0b6b64dee7fe4) ||ROI_ALIGN|Planned 21Q3|[ANEURALNETWORKS_ROI_ALIGN](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a2848b39dd4bfba78f2438fda0d9397a4) diff --git a/src/tim/vx/ops/addn.cc b/src/tim/vx/ops/addn.cc index a8ae283..fd6c86c 100644 --- a/src/tim/vx/ops/addn.cc +++ b/src/tim/vx/ops/addn.cc @@ -1,6 +1,6 @@ /**************************************************************************** * -* Copyright (c) 2020 Vivante Corporation +* 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"), @@ -35,7 +35,7 @@ AddN::AddN(Graph* graph, uint32_t num_inputs) std::shared_ptr AddN::Clone(std::shared_ptr& graph) const { return graph->CreateOperation(this->impl_->input_cnt_); -}; +} } // namespace ops } // namespace vx diff --git a/src/tim/vx/ops/addn_test.cc b/src/tim/vx/ops/addn_test.cc index 88e9ecf..ae50b27 100644 --- a/src/tim/vx/ops/addn_test.cc +++ b/src/tim/vx/ops/addn_test.cc @@ -70,11 +70,12 @@ TEST(AddN, shape_3_1_float32) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); - tim::vx::ShapeType io_shape({3, 1}); + tim::vx::ShapeType in_shape({3, 1}); + tim::vx::ShapeType out_shape({3, 1}); tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, - io_shape, tim::vx::TensorAttribute::INPUT); + in_shape, tim::vx::TensorAttribute::INPUT); tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, - io_shape, tim::vx::TensorAttribute::OUTPUT); + out_shape, tim::vx::TensorAttribute::OUTPUT); auto input_tensor_x = graph->CreateTensor(input_spec); auto input_tensor_y = graph->CreateTensor(input_spec); diff --git a/src/tim/vx/ops/shuffle_channel.cc b/src/tim/vx/ops/shuffle_channel.cc new file mode 100644 index 0000000..18b3b45 --- /dev/null +++ b/src/tim/vx/ops/shuffle_channel.cc @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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 "operation_private.h" +#include "tim/vx/ops/shuffle_channel.h" +#include "vsi_nn_pub.h" +namespace tim { +namespace vx { +namespace ops { + +shuffle_channel::shuffle_channel(Graph* graph, int32_t num_groups, + int32_t index_axis) + : Operation(graph, VSI_NN_OP_SHUFFLECHANNEL, 1, 1) { + this->impl()->node()->nn_param.shufflechannel.group_number = num_groups; + this->impl()->node()->nn_param.shufflechannel.axis = index_axis; +} + +std::shared_ptr shuffle_channel::Clone( + std::shared_ptr& graph) const { + return graph->CreateOperation( + this->impl()->node()->nn_param.shufflechannel.group_number, + this->impl()->node()->nn_param.shufflechannel.axis); +} + +} // namespace ops +} // namespace vx +} // namespace tim diff --git a/src/tim/vx/ops/shuffle_channel_test.cc b/src/tim/vx/ops/shuffle_channel_test.cc new file mode 100644 index 0000000..03ec497 --- /dev/null +++ b/src/tim/vx/ops/shuffle_channel_test.cc @@ -0,0 +1,205 @@ +/**************************************************************************** +* +* 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/shuffle_channel.h" +#include "tim/vx/types.h" +#include "src/tim/vx/test_utils.h" + +#include "gtest/gtest.h" + +TEST(shuffle_channel, shape_3_6_groupnum2_dim1_float32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({3, 6}); //3 columns and 4 rows, w h c n + tim::vx::ShapeType out_shape({3, 6}); + tim::vx::TensorSpec in_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto in_tensor = graph->CreateTensor(in_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector in_data = { + 1, 2, 3, + 4, 5, 6, + 7, 8, 9, + 10, 11, 12, + 13, 14, 15, + 16, 17, 18 + }; + std::vector golden = { + 1, 2, 3, + 10, 11, 12, + 4, 5, 6, + 13, 14, 15, + 7, 8, 9, + 16, 17, 18 + }; + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(float))); + auto op = graph->CreateOperation(2, 1); + (*op).BindInput(in_tensor).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(shuffle_channel, shape_4_2_2_groupnum2_dim0_float32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({4, 2, 2}); + tim::vx::ShapeType out_shape({4, 2, 2}); + tim::vx::TensorSpec in_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto in_tensor = graph->CreateTensor(in_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector in_data = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + std::vector golden = { + 1, 3, 2, 4, 5, 7, 6, 8, 9, 11, 10, 12, 13, 15, 14, 16 + }; + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(float))); + auto op = graph->CreateOperation(2, 0); + (*op).BindInput(in_tensor).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(shuffle_channel, shape_1_4_2_2_groupnum2_dim1_float32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({1, 4, 2, 2}); + tim::vx::ShapeType out_shape({1, 4, 2, 2}); + tim::vx::TensorSpec in_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto in_tensor = graph->CreateTensor(in_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector in_data = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + std::vector golden = { + 1, 3, 2, 4, 5, 7, 6, 8, 9, 11, 10, 12, 13, 15, 14, 16 + }; + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(float))); + auto op = graph->CreateOperation(2, 1); + (*op).BindInput(in_tensor).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(shuffle_channel, shape_4_1_2_2_groupnum4_dim0_float32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({4, 1, 2, 2}); + tim::vx::ShapeType out_shape({4, 1, 2, 2}); + tim::vx::TensorSpec in_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto in_tensor = graph->CreateTensor(in_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector in_data = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + std::vector golden = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(float))); + auto op = graph->CreateOperation(4, 0); + (*op).BindInput(in_tensor).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(shuffle_channel, shape_4_1_2_2_groupnum1_dim3_float32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({4, 1, 2, 2}); + tim::vx::ShapeType out_shape({4, 1, 2, 2}); + tim::vx::TensorSpec in_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto in_tensor = graph->CreateTensor(in_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector in_data = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + std::vector golden = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 + }; + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(float))); + auto op = graph->CreateOperation(1, 3); + (*op).BindInput(in_tensor).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} \ No newline at end of file