diff --git a/include/tim/vx/ops/maxpoolwithargmax.h b/include/tim/vx/ops/maxpoolwithargmax.h new file mode 100644 index 0000000..23d6f04 --- /dev/null +++ b/include/tim/vx/ops/maxpoolwithargmax.h @@ -0,0 +1,66 @@ +/**************************************************************************** +* +* 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_MAXPOOLWITHARGMAX_H_ +#define TIM_VX_OPS_MAXPOOLWITHARGMAX_H_ + +#include + +#include "tim/vx/operation.h" +#include "tim/vx/types.h" + +namespace tim { +namespace vx { +namespace ops { + +/** + * ## MaxpoolWithArgmax + * + * Performs an 2-D Max pooling operation and return indices + * + * - padding : AUTO, VALID or SAME. + * - ksize : filter size. + * - stride : stride along each spatial axis. + * - round_type : CEILING or FLOOR. + */ + +class MaxpoolWithArgmax : public Operation { + public: + MaxpoolWithArgmax(Graph* graph, PadType padding, + const std::array& ksize, + const std::array& stride, + RoundType round_type = RoundType::FLOOR, + DataLayout layout = DataLayout::WHCN); + + protected: + const PadType padding_; + const std::array ksize_; + const std::array stride_; + const RoundType round_type_; +}; + +} // namespace ops +} // namespace vx +} // namespace tim + +#endif /* TIM_VX_OPS_MAXPOOLWITHARGMAX_H_ */ diff --git a/src/tim/vx/internal/src/ops/vsi_nn_op_poolwithargmax.c b/src/tim/vx/internal/src/ops/vsi_nn_op_poolwithargmax.c index 2f88825..6171449 100644 --- a/src/tim/vx/internal/src/ops/vsi_nn_op_poolwithargmax.c +++ b/src/tim/vx/internal/src/ops/vsi_nn_op_poolwithargmax.c @@ -244,6 +244,14 @@ static vsi_bool op_setup ) { vsi_bool ret = TRUE; + vsi_nn_compute_padding( + inputs[0]->attr.size, + self->nn_param.pool.ksize, + self->nn_param.pool.stride, + NULL, + self->nn_param.pool.pad_type, + self->nn_param.pool.pad + ); if( VSI_NN_DIM_AUTO == outputs[0]->attr.dim_num ) { diff --git a/src/tim/vx/ops/README.md b/src/tim/vx/ops/README.md index 0cc429e..8dc5540 100644 --- a/src/tim/vx/ops/README.md +++ b/src/tim/vx/ops/README.md @@ -25,7 +25,7 @@ Reorg|REORG|Mapped|[darknet.reorg](https://github.com/pjreddie/darknet/blob/mast ||VARIABLE|Unmapped|[tf.variable](https://tensorflow.google.cn/api_docs/python/tf/Variable) L2Normalization|L2_NORMALIZE|Mapped|[tf.math.l2_normalize](https://tensorflow.google.cn/api_docs/python/tf/math/l2_normalize) FullyConnected|FCL2|Mapped|[tf.keras.layers.Dense](https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Dense) -||POOLWITHARGMAX|Unmapped|[tf.nn.max_pool_with_argmax](https://tensorflow.google.cn/api_docs/python/tf/nn/max_pool_with_argmax) +|MaxpoolWithArgmax|POOLWITHARGMAX|Mapped|[tf.nn.max_pool_with_argmax](https://tensorflow.google.cn/api_docs/python/tf/nn/max_pool_with_argmax) ArgMax|ARGMAX|Mapped|[tf.math.argmax](https://tensorflow.google.cn/api_docs/python/tf/math/argmax) Maximum|MAXIMUM|Mapped|[tf.math.maximum](https://tensorflow.google.cn/api_docs/python/tf/math/maximum) ||CROP|Unmapped diff --git a/src/tim/vx/ops/maxpoolwithargmax.cc b/src/tim/vx/ops/maxpoolwithargmax.cc new file mode 100644 index 0000000..cd71161 --- /dev/null +++ b/src/tim/vx/ops/maxpoolwithargmax.cc @@ -0,0 +1,57 @@ +/**************************************************************************** +* +* 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/ops/maxpoolwithargmax.h" + +#include "operation_private.h" +#include "type_utils.h" +#include "vsi_nn_pub.h" + +namespace tim { +namespace vx { +namespace ops { + +MaxpoolWithArgmax::MaxpoolWithArgmax(Graph* graph, PadType padding, + const std::array& ksize, + const std::array& stride, + RoundType round_type, + DataLayout layout) + : Operation(graph, VSI_NN_OP_POOLWITHARGMAX, 1, 2, layout), + padding_(padding), + ksize_(ksize), + stride_(stride), + round_type_(round_type) { + this->impl()->node()->nn_param.pool.type = TranslatePoolType(PoolType::MAX); + this->impl()->node()->nn_param.pool.round_type = + TranslateRoundType(round_type_); + this->impl()->node()->nn_param.pool.ksize[0] = ksize_[0]; + this->impl()->node()->nn_param.pool.ksize[1] = ksize_[1]; + this->impl()->node()->nn_param.pool.stride[0] = stride_[0]; + this->impl()->node()->nn_param.pool.stride[1] = stride_[1]; + this->impl()->node()->nn_param.pool.pad_type = TranslatePadType(padding_); + this->SetRoundingPolicy(OverflowPolicy::SATURATE, RoundingPolicy::RTNE, round_type_); +} + +} // namespace ops +} // namespace vx +} // namespace tim diff --git a/src/tim/vx/ops/maxpoolwithargmax_test.cc b/src/tim/vx/ops/maxpoolwithargmax_test.cc new file mode 100644 index 0000000..ab2709b --- /dev/null +++ b/src/tim/vx/ops/maxpoolwithargmax_test.cc @@ -0,0 +1,123 @@ +/**************************************************************************** +* +* 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/maxpoolwithargmax.h" + +#include "gtest/gtest.h" + +TEST(MaxpoolWithArgmax, shape_3_3_1_fp32_kernel_2_stride_2) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({3, 3, 1}); + tim::vx::ShapeType out_shape({2, 2, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec_indices(tim::vx::DataType::UINT8, + out_shape, tim::vx::TensorAttribute::OUTPUT); + tim::vx::TensorSpec output_spec_values(tim::vx::DataType::FLOAT32, + out_shape, tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor_indices = graph->CreateTensor(output_spec_indices); + auto output_tensor_values = graph->CreateTensor(output_spec_values); + + std::vector in_data = { + 1, 2, 3, + 4, 5, 6, + 7, 8, 9 }; + std::vector values_golden = { + 5, 6, + 8, 9 }; + std::vector indices_golden = { + 3, 2, + 1, 0 }; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), in_data.size()*4)); + std::array ksize = {2, 2}; + std::array stride = {2, 2}; + auto op = graph->CreateOperation( + tim::vx::PadType::VALID, ksize, stride); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor_values, output_tensor_indices}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + std::vector output_values(4); + std::vector output_indices(4); + + EXPECT_TRUE(output_tensor_values->CopyDataFromTensor(output_values.data())); + EXPECT_TRUE(output_tensor_indices->CopyDataFromTensor(output_indices.data())); + EXPECT_EQ(values_golden, output_values); + EXPECT_EQ(indices_golden, output_indices); +} + +TEST(MaxpoolWithArgmax, shape_4_4_1_uint8_kernel_2_stride_2) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({4, 4, 1}); + tim::vx::ShapeType out_shape({2, 2, 1}); + tim::vx::Quantization io_quant(tim::vx::QuantType::ASYMMETRIC, 1, 0); + tim::vx::TensorSpec input_spec(tim::vx::DataType::UINT8, + in_shape, tim::vx::TensorAttribute::INPUT, io_quant); + tim::vx::TensorSpec output_spec_indices(tim::vx::DataType::UINT8, + out_shape, tim::vx::TensorAttribute::OUTPUT); + tim::vx::TensorSpec output_spec_values(tim::vx::DataType::UINT8, + out_shape, tim::vx::TensorAttribute::OUTPUT, io_quant); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor_indices = graph->CreateTensor(output_spec_indices); + auto output_tensor_values = graph->CreateTensor(output_spec_values); + + std::vector in_data = { + 1, 2, 3, 3, + 4, 5, 6, 6, + 7, 8, 9, 9, + 10, 11, 12, 12 }; + std::vector values_golden = { + 5, 6, + 11, 12}; + std::vector indices_golden = { + 3, 2, + 3, 2}; + + EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), in_data.size())); + std::array ksize = {2, 2}; + std::array stride = {2, 2}; + auto op = graph->CreateOperation( + tim::vx::PadType::VALID, ksize, stride); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor_values, output_tensor_indices}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + std::vector output_values(4); + std::vector output_indices(4); + + EXPECT_TRUE(output_tensor_values->CopyDataFromTensor(output_values.data())); + EXPECT_TRUE(output_tensor_indices->CopyDataFromTensor(output_indices.data())); + EXPECT_EQ(values_golden, output_values); + EXPECT_EQ(indices_golden, output_indices); +} +