summaryrefslogtreecommitdiff
path: root/compute/ARMComputeEx/src/runtime/NEON/functions/NEArgMinMax.cpp
blob: 5ba465b6157c51cd6a9f9690aa96a4bce9dbfb8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright (c) 2018-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/NEArgMinMax.h"

#include "arm_compute/core/CPP/Validate.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/runtime/NEON/NEScheduler.h"

namespace arm_compute
{

template <ReductionOperation OP>
NEArgMinMaxStatic<OP>::NEArgMinMaxStatic(std::shared_ptr<IMemoryManager> memory_manager)
    : _memory_group(std::move(memory_manager)), _reduction_kernel(), _reduced_out(), _reshape()
{
}

template <ReductionOperation OP>
Status NEArgMinMaxStatic<OP>::validate(const ITensorInfo *input, int axis,
                                       const ITensorInfo *output)
{
  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
  ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
  ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16,
                                                       DataType::F32);

  TensorShape out_shape = input->tensor_shape();
  const int input_dims = input->num_dimensions();
  int axis_local = axis;

  // Convert negative axis
  axis_local = wrap_around(axis_local, input_dims);

  ARM_COMPUTE_RETURN_ERROR_ON(axis_local > 3);
  ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local) > input->num_dimensions() - 1);
  out_shape.remove_dimension(axis_local);

  const TensorInfo out_info = output->clone()->set_tensor_shape(out_shape);
  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info);

  return Status{};
}

template <ReductionOperation OP>
void NEArgMinMaxStatic<OP>::configure(ITensor *input, int axis, ITensor *output)
{
  ARM_COMPUTE_ERROR_ON_NULLPTR(input);

  int axis_local = axis;
  const int input_dims = input->info()->num_dimensions();

  // Convert negative axis
  axis_local = wrap_around(axis_local, input_dims);

  // Perform reduction for axis
  TensorShape intermediate_shape = input->info()->tensor_shape();
  intermediate_shape.set(axis_local, 1);
  auto in = input;

  _reduced_out.allocator()->init(TensorInfo(intermediate_shape, output->info()->num_channels(),
                                            output->info()->data_type(),
                                            output->info()->quantization_info()));
  _memory_group.manage(&_reduced_out);
  _reduction_kernel.configure(in, axis_local, &_reduced_out, OP);

  // Allocate intermediate tensor
  _reduced_out.allocator()->allocate();

  // Configure reshape layer if we want to drop the dimensions
  TensorShape out_shape = input->info()->tensor_shape();
  out_shape.remove_dimension(axis_local);
  auto_init_if_empty(*output->info(), output->info()->clone()->set_tensor_shape(out_shape));
  _reshape.configure(&_reduced_out, output);
}

template <ReductionOperation OP> void NEArgMinMaxStatic<OP>::run()
{
  MemoryGroupResourceScope scope_mg(_memory_group);

  _reduction_kernel.run();
  _reshape.run();
}

// Supported Specializations
template class NEArgMinMaxStatic<ReductionOperation::ARG_IDX_MAX>;
template class NEArgMinMaxStatic<ReductionOperation::ARG_IDX_MIN>;
} // namespace arm_compute