From d985378af0c9a4db6a483634dd40526cd4031dee Mon Sep 17 00:00:00 2001 From: Giuseppe Rossini Date: Fri, 25 Oct 2019 11:11:44 +0100 Subject: COMPMID-2588: Optimize the output detection kernel required by MobileNet-SSD (~27% improvement) Change-Id: Ic6ce570af3878a0666ec680e0efabba3fcfd1222 Signed-off-by: Giuseppe Rossini Reviewed-on: https://review.mlplatform.org/c/2160 Comments-Addressed: Arm Jenkins Reviewed-by: Georgios Pinitas Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins --- src/graph/backends/NEON/NEFunctionFactory.cpp | 2 +- src/graph/backends/NEON/NENodeValidator.cpp | 2 +- .../CPP/functions/CPPDetectionPostProcessLayer.cpp | 14 ++-- .../NEON/functions/NEDetectionPostProcessLayer.cpp | 98 ++++++++++++++++++++++ 4 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/runtime/NEON/functions/NEDetectionPostProcessLayer.cpp (limited to 'src') diff --git a/src/graph/backends/NEON/NEFunctionFactory.cpp b/src/graph/backends/NEON/NEFunctionFactory.cpp index d8b0ae92ea..12f44e303e 100644 --- a/src/graph/backends/NEON/NEFunctionFactory.cpp +++ b/src/graph/backends/NEON/NEFunctionFactory.cpp @@ -210,7 +210,7 @@ std::unique_ptr NEFunctionFactory::create(INode *node, GraphContext & case NodeType::DetectionOutputLayer: return detail::create_detection_output_layer(*polymorphic_downcast(node)); case NodeType::DetectionPostProcessLayer: - return detail::create_detection_post_process_layer(*polymorphic_downcast(node)); + return detail::create_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::EltwiseLayer: return detail::create_eltwise_layer(*polymorphic_downcast(node)); case NodeType::FlattenLayer: diff --git a/src/graph/backends/NEON/NENodeValidator.cpp b/src/graph/backends/NEON/NENodeValidator.cpp index 0b53657c42..f17b116892 100644 --- a/src/graph/backends/NEON/NENodeValidator.cpp +++ b/src/graph/backends/NEON/NENodeValidator.cpp @@ -62,7 +62,7 @@ Status NENodeValidator::validate(INode *node) case NodeType::DetectionOutputLayer: return detail::validate_detection_output_layer(*polymorphic_downcast(node)); case NodeType::DetectionPostProcessLayer: - return detail::validate_detection_post_process_layer(*polymorphic_downcast(node)); + return detail::validate_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::GenerateProposalsLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : GenerateProposalsLayer"); case NodeType::NormalizePlanarYUVLayer: diff --git a/src/runtime/CPP/functions/CPPDetectionPostProcessLayer.cpp b/src/runtime/CPP/functions/CPPDetectionPostProcessLayer.cpp index 0addb0ead3..bc88f71af4 100644 --- a/src/runtime/CPP/functions/CPPDetectionPostProcessLayer.cpp +++ b/src/runtime/CPP/functions/CPPDetectionPostProcessLayer.cpp @@ -42,7 +42,7 @@ Status validate_arguments(const ITensorInfo *input_box_encoding, const ITensorIn { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input_box_encoding, input_class_score, input_anchors); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_box_encoding, 1, DataType::F32, DataType::QASYMM8); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_box_encoding, input_class_score, input_anchors); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_box_encoding, input_anchors); ARM_COMPUTE_RETURN_ERROR_ON_MSG(input_box_encoding->num_dimensions() > 3, "The location input tensor shape should be [4, N, kBatchSize]."); if(input_box_encoding->num_dimensions() > 2) { @@ -183,8 +183,8 @@ void SaveOutputs(const Tensor *decoded_boxes, const std::vector &result_idx CPPDetectionPostProcessLayer::CPPDetectionPostProcessLayer(std::shared_ptr memory_manager) : _memory_group(std::move(memory_manager)), _nms(), _input_box_encoding(nullptr), _input_scores(nullptr), _input_anchors(nullptr), _output_boxes(nullptr), _output_classes(nullptr), - _output_scores(nullptr), _num_detection(nullptr), _info(), _num_boxes(), _num_classes_with_background(), _num_max_detected_boxes(), _decoded_boxes(), _decoded_scores(), _selected_indices(), - _class_scores(), _input_scores_to_use(nullptr) + _output_scores(nullptr), _num_detection(nullptr), _info(), _num_boxes(), _num_classes_with_background(), _num_max_detected_boxes(), _dequantize_scores(false), _decoded_boxes(), _decoded_scores(), + _selected_indices(), _class_scores(), _input_scores_to_use(nullptr) { } @@ -214,6 +214,7 @@ void CPPDetectionPostProcessLayer::configure(const ITensor *input_box_encoding, _info = info; _num_boxes = input_box_encoding->info()->dimension(1); _num_classes_with_background = _input_scores->info()->dimension(0); + _dequantize_scores = (info.dequantize_scores() && is_data_type_quantized(input_box_encoding->info()->data_type())); auto_init_if_empty(*_decoded_boxes.info(), TensorInfo(TensorShape(_kNumCoordBox, _input_box_encoding->info()->dimension(1), _kBatchSize), 1, DataType::F32)); auto_init_if_empty(*_decoded_scores.info(), TensorInfo(TensorShape(_input_scores->info()->dimension(0), _input_scores->info()->dimension(1), _kBatchSize), 1, DataType::F32)); @@ -221,7 +222,7 @@ void CPPDetectionPostProcessLayer::configure(const ITensor *input_box_encoding, const unsigned int num_classes_per_box = std::min(info.max_classes_per_detection(), info.num_classes()); auto_init_if_empty(*_class_scores.info(), TensorInfo(info.use_regular_nms() ? TensorShape(_num_boxes) : TensorShape(_num_boxes * num_classes_per_box), 1, DataType::F32)); - _input_scores_to_use = is_data_type_quantized(input_box_encoding->info()->data_type()) ? &_decoded_scores : _input_scores; + _input_scores_to_use = _dequantize_scores ? &_decoded_scores : _input_scores; // Manage intermediate buffers _memory_group.manage(&_decoded_boxes); @@ -261,7 +262,7 @@ void CPPDetectionPostProcessLayer::run() DecodeCenterSizeBoxes(_input_box_encoding, _input_anchors, _info, &_decoded_boxes); // Decode scores if necessary - if(is_data_type_quantized(_input_box_encoding->info()->data_type())) + if(_dequantize_scores) { for(unsigned int idx_c = 0; idx_c < _num_classes_with_background; ++idx_c) { @@ -365,7 +366,6 @@ void CPPDetectionPostProcessLayer::run() // Run Non-maxima Suppression _nms.run(); - std::vector selected_indices; for(unsigned int i = 0; i < max_detections; ++i) { @@ -384,4 +384,4 @@ void CPPDetectionPostProcessLayer::run() num_output, max_detections, _output_boxes, _output_classes, _output_scores, _num_detection); } } -} // namespace arm_compute \ No newline at end of file +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEDetectionPostProcessLayer.cpp b/src/runtime/NEON/functions/NEDetectionPostProcessLayer.cpp new file mode 100644 index 0000000000..d1d13432a1 --- /dev/null +++ b/src/runtime/NEON/functions/NEDetectionPostProcessLayer.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * 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 "arm_compute/runtime/NEON/functions/NEDetectionPostProcessLayer.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/Validate.h" +#include "support/ToolchainSupport.h" + +#include +#include +#include + +namespace arm_compute +{ +NEDetectionPostProcessLayer::NEDetectionPostProcessLayer(std::shared_ptr memory_manager) + : _memory_group(std::move(memory_manager)), _dequantize(), _detection_post_process(), _decoded_scores(), _run_dequantize(false) +{ +} + +void NEDetectionPostProcessLayer::configure(const ITensor *input_box_encoding, const ITensor *input_scores, const ITensor *input_anchors, + ITensor *output_boxes, ITensor *output_classes, ITensor *output_scores, ITensor *num_detection, DetectionPostProcessLayerInfo info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, output_scores); + ARM_COMPUTE_ERROR_THROW_ON(NEDetectionPostProcessLayer::validate(input_box_encoding->info(), input_scores->info(), input_anchors->info(), output_boxes->info(), output_classes->info(), + output_scores->info(), + num_detection->info(), info)); + + const ITensor *input_scores_to_use = input_scores; + DetectionPostProcessLayerInfo info_to_use = info; + _run_dequantize = is_data_type_quantized(input_box_encoding->info()->data_type()); + + if(_run_dequantize) + { + _memory_group.manage(&_decoded_scores); + + _dequantize.configure(input_scores, &_decoded_scores); + + input_scores_to_use = &_decoded_scores; + + // Create a new info struct to avoid dequantizing in the CPP layer + std::array scales_values{ info.scale_value_y(), info.scale_value_x(), info.scale_value_h(), info.scale_value_w() }; + DetectionPostProcessLayerInfo info_quantized(info.max_detections(), info.max_classes_per_detection(), info.nms_score_threshold(), info.iou_threshold(), info.num_classes(), + scales_values, info.use_regular_nms(), info.detection_per_class(), false); + info_to_use = info_quantized; + } + + _detection_post_process.configure(input_box_encoding, input_scores_to_use, input_anchors, output_boxes, output_classes, output_scores, num_detection, info_to_use); + _decoded_scores.allocator()->allocate(); +} + +Status NEDetectionPostProcessLayer::validate(const ITensorInfo *input_box_encoding, const ITensorInfo *input_scores, const ITensorInfo *input_anchors, + ITensorInfo *output_boxes, ITensorInfo *output_classes, ITensorInfo *output_scores, ITensorInfo *num_detection, DetectionPostProcessLayerInfo info) +{ + bool run_dequantize = is_data_type_quantized(input_box_encoding->data_type()); + if(run_dequantize) + { + TensorInfo decoded_classes_info = input_scores->clone()->set_is_resizable(true).set_data_type(DataType::F32); + ARM_COMPUTE_RETURN_ON_ERROR(NEDequantizationLayer::validate(input_scores, &decoded_classes_info)); + } + ARM_COMPUTE_RETURN_ON_ERROR(CPPDetectionPostProcessLayer::validate(input_box_encoding, input_scores, input_anchors, output_boxes, output_classes, output_scores, num_detection, info)); + + return Status{}; +} + +void NEDetectionPostProcessLayer::run() +{ + MemoryGroupResourceScope scope_mg(_memory_group); + + // Decode scores if necessary + if(_run_dequantize) + { + _dequantize.run(); + } + _detection_post_process.run(); +} +} // namespace arm_compute -- cgit v1.2.1