Add shuffle_channel support & test for tim::vx

Signed-off-by: Chen Xin <jack.chen@verisilicon.com>
This commit is contained in:
Chen Xin 2021-08-30 17:28:40 +08:00 committed by Sven
parent b226777ad3
commit 6f2e92ffa6
8 changed files with 324 additions and 15 deletions

View File

@ -3,3 +3,4 @@
BasedOnStyle: Google
DerivePointerAlignment: false
ReflowComments: false
SortIncludes: false

View File

@ -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<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
};
} // namespace ops
} // namespace vx
} // namespace tim
#endif /* TIM_VX_OPS_SHUFFLE_H_ */

View File

@ -22,14 +22,12 @@
*
*****************************************************************************/
#include "tim/vx/graph.h"
#include <algorithm>
#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"
@ -125,8 +123,10 @@ bool GraphImpl::Compile() {
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]() {
@ -143,8 +143,10 @@ 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]() {

View File

@ -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)

View File

@ -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<Operation> AddN::Clone(std::shared_ptr<Graph>& graph) const {
return graph->CreateOperation<AddN>(this->impl_->input_cnt_);
};
}
} // namespace ops
} // namespace vx

View File

@ -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);

View File

@ -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<Operation> shuffle_channel::Clone(
std::shared_ptr<Graph>& graph) const {
return graph->CreateOperation<shuffle_channel>(
this->impl()->node()->nn_param.shufflechannel.group_number,
this->impl()->node()->nn_param.shufflechannel.axis);
}
} // namespace ops
} // namespace vx
} // namespace tim

View File

@ -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<float> in_data = {
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
13, 14, 15,
16, 17, 18
};
std::vector<float> 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<tim::vx::ops::shuffle_channel>(2, 1);
(*op).BindInput(in_tensor).BindOutput(out_tensor);
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> 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<float> in_data = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
};
std::vector<float> 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<tim::vx::ops::shuffle_channel>(2, 0);
(*op).BindInput(in_tensor).BindOutput(out_tensor);
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> 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<float> in_data = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
};
std::vector<float> 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<tim::vx::ops::shuffle_channel>(2, 1);
(*op).BindInput(in_tensor).BindOutput(out_tensor);
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> 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<float> in_data = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
};
std::vector<float> 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<tim::vx::ops::shuffle_channel>(4, 0);
(*op).BindInput(in_tensor).BindOutput(out_tensor);
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> 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<float> in_data = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
};
std::vector<float> 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<tim::vx::ops::shuffle_channel>(1, 3);
(*op).BindInput(in_tensor).BindOutput(out_tensor);
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(golden.size());
EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data()));
EXPECT_EQ(golden, output);
}