aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/arm_conv/pooling/pooling_depthfirst.hpp
blob: 1ca478513c8f691ef903311f56f6d819cfb48a63 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (c) 2021-2023 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.
 */

#pragma once

#include "depthfirst_driver.hpp"
#include "src/core/NEON/kernels/arm_conv/addressing.hpp"
#include "utils.hpp"
#if !defined(_WIN64) && !defined(__OpenBSD__)
#include <alloca.h>
#endif /* !defined(_WIN64) && !defined(__OpenBSD__) */
#include <limits>

namespace arm_conv {
namespace pooling {

template <typename TInput, typename TOutput>
class DepthfirstStrategy : public IDepthfirstStrategy
{
  unsigned int input_rows, input_cols, output_rows, output_cols;

  public:
  DepthfirstStrategy(unsigned int window_rows, unsigned int window_cols,
                     unsigned int stride_rows, unsigned int stride_cols,
                     unsigned int output_rows, unsigned int output_cols)
  : input_rows(output_rows + (window_rows - 1) * stride_rows),
    input_cols(output_cols + (window_cols - 1) * stride_cols),
    output_rows(output_rows), output_cols(output_cols)
  {
  }

  unsigned int get_input_rows() const override { return input_rows; }
  unsigned int get_input_cols() const override { return input_cols; }
  unsigned int get_output_rows() const override { return output_rows; }
  unsigned int get_output_cols() const override { return output_cols; }

  typedef void (*KernelType)(
    unsigned int n_channels,
    const TInput *const *,
    TOutput *const *,
    bool exclude_padding,
    unsigned int pad_left,
    unsigned int pad_top,
    unsigned int pad_right,
    unsigned int pad_bottom
  );
  virtual KernelType get_kernel(void) const = 0;
};


struct WorkingSpace
{
  void *input_buffer;
  void *output_buffer;
};


template <typename TInput, typename TOutput=TInput, class OutputStage=Nothing>
class PoolingDepthfirst : public DepthfirstDriver<TInput, TOutput>
{
  size_t sizeof_input_buffer(void) const
  {
    return sizeof(TInput) * this->m_args.n_channels;
  }

  size_t sizeof_output_buffer(void) const
  {
    return sizeof(TOutput) * this->m_args.n_channels;
  }

  protected:
  /* Compute the amount of working space required for a single thread. */
  size_t get_working_size_per_thread() const override
  {
    return sizeof(WorkingSpace) + this->m_args.n_channels * (sizeof(TInput) + sizeof(TOutput));
  }

  /* Initialise the working space for a thread. */
  void initialise_working_space(void *raw_ws) const override
  {
    auto ws = reinterpret_cast<WorkingSpace *>(raw_ws);
    ws->input_buffer = ws + 1;
    ws->output_buffer = reinterpret_cast<char *>(ws + 1) + sizeof(TInput) * this->m_args.n_channels;

    // Fill the input buffer with an appropriate value
    TInput fill_val = 0;
    if (this->m_args.pool_type == PoolingType::MAX)
    {
      using limits = std::numeric_limits<TInput>;
      if (limits::has_infinity)
      {
        fill_val = -limits::infinity();
      }
      else
      {
        fill_val = limits::min();
      }
    }

    auto ptr = reinterpret_cast<TInput *>(ws->input_buffer);
    auto n_channels = this->m_args.n_channels;
    for (; n_channels; n_channels--)
    {
      *(ptr++) = fill_val;
    }
  }

  /* Compute a portion of the output tensor with padding. */
  void compute_tile_padded(
    unsigned int output_i, unsigned int output_j,
    unsigned int channel_start, unsigned int channel_end,
    const TensorSpec<const TInput *> &input,
    const TensorSpec<TOutput *> &output,
    void *working_space
  ) const override
  {
    const auto kern = reinterpret_cast<const DepthfirstStrategy<TInput, TOutput> *>(
      this->m_strat.get())->get_kernel();

    // Get the working space, and some space on the stack for pointer arrays
    auto ws = reinterpret_cast<WorkingSpace *>(working_space);
    auto inptr_array = reinterpret_cast<const TInput **>(alloca(
        sizeof(TInput *) * this->m_strat->get_input_rows() * this->m_strat->get_input_cols()));
    auto outptr_array = reinterpret_cast<TOutput **>(alloca(
        sizeof(TOutput *) * this->m_strat->get_output_rows() * this->m_strat->get_output_cols()));

    // Prepare the input pointers
    const int ii = static_cast<int>(output_i * this->m_args.pool_stride.rows) - this->m_args.padding.top;
    const auto input_pad_top = static_cast<unsigned int>(ii < 0 ? -ii : 0);
    const auto input_i = static_cast<unsigned int>(ii < 0 ? 0 : ii);

    const unsigned int end_ii = ii + this->m_strat->get_input_rows();
    const auto input_pad_bottom = end_ii < this->m_args.input_rows ? 0 : end_ii - this->m_args.input_rows;

    const int ij = static_cast<int>(output_j * this->m_args.pool_stride.cols) - this->m_args.padding.left;
    const auto input_pad_left = static_cast<unsigned int>(ij < 0 ? -ij : 0);
    const auto input_j = static_cast<unsigned int>(ij < 0 ? 0 : ij);

    const unsigned int end_ij = ij + this->m_strat->get_input_cols();
    const auto input_pad_right = end_ij < this->m_args.input_cols ? 0 : end_ij - this->m_args.input_cols;

    fill_pointer_array<const TInput>(
      inptr_array, this->m_strat->get_input_rows(), this->m_strat->get_input_cols(),
      input.base + input_i*input.ld_row + input_j*input.ld_col + channel_start,
      input.ld_row, input.ld_col,
      reinterpret_cast<const TInput *>(ws->input_buffer),
      input_pad_top, this->m_args.input_rows - input_i,
      input_pad_left, this->m_args.input_cols - input_j
    );

    // Prepare the output pointers
    fill_pointer_array(
      outptr_array, this->m_strat->get_output_rows(), this->m_strat->get_output_cols(),
      output.base + output_i*output.ld_row + output_j*output.ld_col + channel_start,
      output.ld_row, output.ld_col,
      reinterpret_cast<TOutput *>(ws->output_buffer),
      0, this->m_args.output_rows - output_i, // Top padding, # valid rows
      0, this->m_args.output_cols - output_j  // Left padding, # valid columns
    );

    // Call the kernel
    kern(
      channel_end - channel_start, inptr_array, outptr_array,
      this->m_args.exclude_padding,
      input_pad_left, input_pad_top,
      input_pad_right, input_pad_bottom
    );
  }

  // Compute a portion of the work with only top/bottom padding.
  void compute_row_padded_tile_row(
    const unsigned int output_i, unsigned int output_j, unsigned int n_tile_cols,
    const unsigned int channel_start, const unsigned int channel_end,
    const TensorSpec<const TInput *> &input,
    const TensorSpec<TOutput *> &output,
    void *working_space
  ) const override
  {
    const auto kern = reinterpret_cast<const DepthfirstStrategy<TInput, TOutput> *>(
      this->m_strat.get())->get_kernel();

    // Get the working space, and some space on the stack for pointer arrays
    auto ws = reinterpret_cast<WorkingSpace *>(working_space);
    auto inptr_array = reinterpret_cast<const TInput **>(alloca(
        sizeof(TInput *) * this->m_strat->get_input_rows() * this->m_strat->get_input_cols()));
    auto outptr_array = reinterpret_cast<TOutput **>(alloca(
        sizeof(TOutput *) * this->m_strat->get_output_rows() * this->m_strat->get_output_cols()));

    // Prepare the initial input pointers
    const int ii = static_cast<int>(output_i * this->m_args.pool_stride.rows) - this->m_args.padding.top;
    const auto input_pad_top = static_cast<unsigned int>(ii < 0 ? -ii : 0);
    const auto input_i = static_cast<unsigned int>(ii < 0 ? 0 : ii);

    const unsigned int end_ii = ii + this->m_strat->get_input_rows();
    const auto input_pad_bottom = end_ii < this->m_args.input_rows ? 0 : end_ii - this->m_args.input_rows;

    const int ij = static_cast<int>(output_j * this->m_args.pool_stride.cols) - this->m_args.padding.left;
    const auto input_j = static_cast<unsigned int>(ij < 0 ? 0 : ij);

    const auto end_oi = output_i + this->m_strat->get_output_cols();
    const auto output_pad_bottom = end_oi < this->m_args.output_rows ? 0 : end_oi - this->m_args.output_rows;

    fill_pointer_array<const TInput>(
      inptr_array, this->m_strat->get_input_rows(), this->m_strat->get_input_cols(),
      input.base + input_i*input.ld_row + input_j*input.ld_col + channel_start,
      input.ld_row, input.ld_col,
      reinterpret_cast<const TInput *>(ws->input_buffer),
      input_pad_top, this->m_args.input_rows - input_i,
      0, this->m_args.input_cols - input_j
    );

    // Prepare the initial output pointers
    fill_pointer_array(
      outptr_array, this->m_strat->get_output_rows(), this->m_strat->get_output_cols(),
      output.base + output_i*output.ld_row + output_j*output.ld_col + channel_start,
      output.ld_row, output.ld_col,
      reinterpret_cast<TOutput *>(ws->output_buffer),
      0, this->m_args.output_rows - output_i, // Top padding, # valid rows
      0, this->m_args.output_cols - output_j  // Left padding, # valid columns
    );

    // Call the kernel
    for (; n_tile_cols; n_tile_cols--)
    {
      kern(
        channel_end - channel_start, inptr_array, outptr_array,
        this->m_args.exclude_padding,
        0, input_pad_top,
        0, input_pad_bottom
      );

      // Progress the input and output pointer arrays
      const auto input_col_stride = input.ld_col * this->m_strat->get_output_cols() * this->m_args.pool_stride.cols;
      for (
        auto n = input_pad_top * this->m_strat->get_input_cols();
        n < (this->m_strat->get_input_rows() - input_pad_bottom) * this->m_strat->get_input_cols();
        n++
      )
      {
        inptr_array[n] += input_col_stride;
      }

      const auto output_col_stride = output.ld_col * this->m_strat->get_output_cols();
      for (
        auto n = 0u;
        n < (this->m_strat->get_output_rows() - output_pad_bottom) * this->m_strat->get_output_cols();
        n++
      )
      {
        outptr_array[n] += output_col_stride;
      }
    }
  }

  public:
  PoolingDepthfirst(const DepthfirstStrategy<TInput, TOutput> *strat,
                    const PoolingArgs &args, const OutputStage &os = {})
  : DepthfirstDriver<TInput, TOutput>(strat, args)
  {
    ARM_COMPUTE_UNUSED(os);
  }
};

}  // namespace pooling
}  // namespace arm_conv