summaryrefslogtreecommitdiff
path: root/libs/ARMComputeEx/src/core/CL/kernels/CLReduceMaxKernel.cpp
blob: 168b246bf46933390aae826b09c370eeeffe420b (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright (c) 2016-2018 ARM Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "arm_compute/core/CL/kernels/CLReduceMaxKernel.h"

#include "arm_compute/core/CL/CLHelpers.h"
#include "arm_compute/core/CL/CLKernelLibrary.h"
#include "arm_compute/core/CL/CLKernelLibraryEx.h"
#include "arm_compute/core/CL/ICLTensor.h"
#include "arm_compute/core/CL/OpenCL.h"
#include "arm_compute/core/Error.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/core/Validate.h"
#include "arm_compute/core/Window.h"

#include <cmath>
#include <cstdlib>
#include <set>
#include <string>

using namespace arm_compute;

namespace
{
constexpr unsigned int num_elems_processed_per_iteration = 16;

Status validate_arguments(const ITensorInfo *input, int32_t axis, const ITensorInfo *output)
{
  // We can handle for simple case only
  // Input rank: 2
  // Output rank: 1
  // Axis: one axis value, restrict to 1

  ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
  ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis != 1, "Axis only allowed 1");

  ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() == 0,
                                  "Inputs are not broadcast compatible");

  // Validate in case of configured output
  if (output->total_size() > 0)
  {
    ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() != input->data_type(),
                                    "Output same type allowed for input and output");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().num_dimensions() != 1,
                                    "Only support for output dimension 1");
    ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->tensor_shape().num_dimensions() != 2,
                                    "Only support for input dimension 2");
  }

  return Status{};
}

} // namespace

CLReduceMaxKernel::CLReduceMaxKernel() : _input(nullptr), _output(nullptr), _axis(0) {}

void CLReduceMaxKernel::configure(const ICLTensor *input, int32_t axis, ICLTensor *output)
{
  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, output->info()));

  _input = input;
  _output = output;
  _axis = axis;

  // Configure kernel window
  int cols = _input->info()->tensor_shape()[0];
  int rows = _input->info()->tensor_shape()[1];
  Window win;
  win.set(0, Window::Dimension(0, cols, 1));
  win.set(1, Window::Dimension(0, rows, 1));

  // Construct kernel name
  std::string kernel_name = "reduce_max";

  // Set kernel build options
  std::set<std::string> build_opts;
  build_opts.emplace("-DWIDTH=" + support::cpp11::to_string(cols));

  // Create kernel
  _kernel =
      static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts));

  ICLKernel::configure(win);
}

Status CLReduceMaxKernel::validate(const ITensorInfo *input, int32_t axis,
                                   const ITensorInfo *output)
{
  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, output));

  return Status{};
}

void CLReduceMaxKernel::run(const Window &window, cl::CommandQueue &queue)
{
  ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
  ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);

  Window window_input = window;
  Window slice_input = window_input.first_slice_window_1D();

  do
  {
    Window slice_output = slice_input.shift_dimensions(1);
    unsigned int idx = 0;
    add_1D_tensor_argument(idx, _input, slice_input);
    add_1D_tensor_argument(idx, _output, slice_output);
    enqueue(queue, *this, slice_input);

  } while (window_input.slide_window_slice_1D(slice_input));
}