Compute Library
 22.08
depthwise_depthfirst_generic_multiplier.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #pragma once
26 
28 
29 #ifdef CYCLE_PROFILING
30 #include "profiler.hpp"
31 #endif
32 
33 #include <limits>
34 
35 namespace arm_conv {
36 namespace depthwise {
37 
38 template <class strategy>
40  public DepthwiseCommon<typename strategy::input_type,
41  typename strategy::weight_type,
42  typename strategy::return_type>
43 {
44  protected:
45 
46  using TInput = typename strategy::input_type;
47  using TWeight = typename strategy::weight_type;
48  using TOutput = typename strategy::return_type;
49  using TAccum = typename strategy::bias_type;
50 
51  unsigned int kernel_points(void) const
52  {
53  return this->m_args.kernel_rows * this->m_args.kernel_cols;
54  }
55 
56  unsigned int input_rows(void) const
57  {
58  return (strategy::output_rows() - 1) * this->m_args.stride_rows + this->m_args.kernel_rows;
59  }
60 
61  unsigned int input_cols(void) const
62  {
63  return (strategy::output_cols() - 1) * this->m_args.stride_cols + this->m_args.kernel_cols;
64  }
65 
66  size_t sizeof_inptr_array(void) const
67  {
68  return sizeof(TInput *) * kernel_points() * strategy::output_rows();
69  }
70 
71  size_t sizeof_input_samples(void) const
72  {
73  // We have a sample for each kernel point, for each point of the output array.
74  return sizeof(TInput) * kernel_points() *
75  strategy::output_rows() *
76  strategy::output_col_regs() *
77  (16 / sizeof(TAccum));
78  }
79 
80  size_t sizeof_outptr_array(void) const
81  {
82  return sizeof(TOutput *) * strategy::output_rows() * strategy::output_cols();
83  }
84 
85  size_t sizeof_output_buffer(unsigned int n_channels) const
86  {
87  const unsigned int vl = arm_gemm::utils::get_vector_length<TOutput>(strategy::vl_type);
88  const auto rounded_channels = arm_gemm::roundup(n_channels, vl);
89  return sizeof(TOutput) * rounded_channels;
90  }
91 
92  void pack_weights(TWeight *buffer, const TWeight *weights, size_t ld_weight_col, size_t ld_weight_row) const
93  {
94  const unsigned int vl = arm_gemm::utils::get_vector_length<TAccum>(strategy::vl_type);
95  ld_weight_col = (ld_weight_col == 0) ? this->m_args.channel_multiplier * this->m_args.input_channels : ld_weight_col;
96  ld_weight_row = (ld_weight_row == 0) ? this->m_args.kernel_cols * ld_weight_col : ld_weight_row;
97 
98  for (unsigned int in_c = 0; in_c < this->m_args.input_channels; in_c++)
99  {
100  for (unsigned int n = 0; n < this->m_args.channel_multiplier; n += vl)
101  {
102  const unsigned int out_c = in_c * this->m_args.channel_multiplier + n;
103  const unsigned int todo = std::min(vl, this->m_args.channel_multiplier - n);
104 
105  // Copy each of the weights in turn
106  auto weights_row = weights + out_c;
107  for (unsigned int i = 0; i < this->m_args.kernel_rows; i++)
108  {
109  auto weights_col = weights_row;
110 
111  for (unsigned int j = 0; j < this->m_args.kernel_cols; j++)
112  {
113  for (unsigned int m = 0; m < todo; m++)
114  {
115  buffer[m] = weights_col[m];
116  }
117  buffer += vl;
118 
119  weights_col += ld_weight_col;
120  }
121 
122  weights_row += ld_weight_row;
123  }
124  }
125  }
126  }
127 
128  void execute_tiles(
129  std::function<void(const TInput **, TOutput **, const TWeight *, unsigned int, unsigned int)> tile_fn,
130  const TInput pad_value,
131  const unsigned int batches,
132  const unsigned int input_height,
133  const unsigned int input_width,
134  const unsigned int input_channels,
135  const PaddingValues &padding,
136  const void *const _input,
137  const size_t ld_input_col,
138  const size_t ld_input_row,
139  const size_t ld_input_batch,
140  const void *const parameters,
141  const unsigned int output_height,
142  const unsigned int output_width,
143  void *const _output,
144  const size_t ld_output_col,
145  const size_t ld_output_row,
146  const size_t ld_output_batch,
147  void *const _working_space,
148  const unsigned int thread_id,
149  const unsigned int n_threads
150  ) const
151  {
152 #ifdef CYCLE_PROFILING
153  arm_gemm::profiler prof;
154 #endif
155 
156  // Determine what portion of the work to do.
157  const unsigned int n_rows_per_thread = arm_gemm::iceildiv(output_height, n_threads);
158  const int start_out_height = std::min(thread_id * n_rows_per_thread, output_height);
159  const int end_out_height = std::min(start_out_height + n_rows_per_thread, output_height);
160 
161  // Need a stride over blocks of parameters
162  const unsigned int vl = arm_gemm::utils::get_vector_length<TAccum>(strategy::vl_type);
163  const unsigned int param_stride = arm_gemm::roundup(this->m_args.channel_multiplier, vl) * kernel_points();
164 
165  // Cast input and output pointers into the right types
166  const TInput *const inptr = static_cast<const TInput *>(_input);
167  TOutput *const outptr = static_cast<TOutput *>(_output);
168 
169  // Allocate portions of the working space
170  uint8_t *working_space = static_cast<uint8_t *>(_working_space) +
171  get_working_size(thread_id, input_channels);
172 
173  const TInput **inptrs = reinterpret_cast<const TInput **>(working_space);
174  working_space += sizeof_inptr_array();
175 
176  // To simplify the kernel, we process padded or non-NCHW-ordered input into
177  // a form which can be consumed by the kernel. This data is stored here and
178  // passed into the kernel as an array of N pointers (one per row of the
179  // input).
180  TInput *rearranged_input = reinterpret_cast<TInput *>(working_space);
181  working_space += sizeof_input_samples();
182 
183  TOutput **outptr_array = reinterpret_cast<TOutput **>(working_space);
184  working_space += sizeof_outptr_array();
185 
186  TOutput *const output_buffer = reinterpret_cast<TOutput *>(working_space);
187 
188  // TODO Dynamically change the input pointer array in cases where we could
189  // read directly from the input tensor; for now though assume we will
190  // always read from the sample array.
191  {
192  auto my_inptrs = inptrs;
193  auto my_input_samples = rearranged_input;
194 
195  // For each kernel point; for each row of output; for each register of
196  // values containing a QUAD of source values.
197  const unsigned int quad_length = 16 / sizeof(TAccum);
198 
199  for (auto p = 0u; p < kernel_points() * strategy::output_rows(); p++)
200  {
201  *(my_inptrs)++ = my_input_samples;
202  my_input_samples += arm_gemm::roundup(strategy::output_cols(), quad_length);
203  }
204  }
205 
206  // For each output tile, construct the requisite set of pointers and call
207  // into the kernel.
208  for (unsigned int batch = 0; batch < batches; batch++)
209  {
210  // Get batch pointers
211  const auto inptr_batch = inptr + batch * ld_input_batch;
212  const auto outptr_batch = outptr + batch * ld_output_batch;
213 
214  for (int start_out_i = start_out_height;
215  start_out_i < end_out_height;
216  start_out_i += static_cast<int>(strategy::output_rows()))
217  {
218  const int end_out_i = std::min(start_out_i + static_cast<int>(strategy::output_rows()), end_out_height);
219  const int start_in_i = start_out_i * this->m_args.stride_rows - padding.top;
220  const int end_in_i = start_in_i + input_rows();
221 
222  // Compute top/bottom padding
223  const auto pad_top = static_cast<unsigned int>(-std::min(start_in_i, 0));
224  const auto pad_bottom = static_cast<unsigned int>(-std::min(static_cast<int>(input_height) - end_in_i, 0));
225  const unsigned int valid_output_rows = std::min(
226  end_out_i - start_out_i,
227  static_cast<int>(output_height) - start_out_i
228  );
229 
230  const int pad_rows = pad_top + pad_bottom;
231 
232  for (int start_out_j = 0; start_out_j < static_cast<int>(output_width);)
233  {
234  const int start_in_j = start_out_j * this->m_args.stride_cols - this->m_args.padding.left;
235  const int pad_left = -std::min(0, start_in_j);
236 
237  const int end_out_j = start_out_j + strategy::output_cols();
238  const int end_in_j = start_in_j + input_cols();
239 
240  const auto pad_right = static_cast<unsigned int>(-std::min(static_cast<int>(input_width) - end_in_j, 0));
241  const unsigned int valid_output_cols = std::min(
242  end_out_j - start_out_j,
243  static_cast<int>(output_width) - start_out_j
244  );
245 
246  const int pad_cols = pad_left + pad_right;
247 
248  // Construct the output pointer array.
249  TOutput **outptr_pos = outptr_array;
250  for (auto i = 0u; i < valid_output_rows; i++)
251  {
252  unsigned int j = 0u;
253  TOutput *colptr = outptr_batch + (start_out_i + i) * ld_output_row + start_out_j * ld_output_col;
254  for (; j < valid_output_cols; j++)
255  {
256  *(outptr_pos++) = colptr;
257  colptr += ld_output_col;
258  }
259  for (; j < strategy::output_cols(); j++)
260  {
261  *(outptr_pos++) = output_buffer;
262  }
263  }
264  for (auto i = valid_output_rows; i < strategy::output_rows(); i++)
265  {
266  for (auto j = 0u; j < strategy::output_cols(); j++)
267  {
268  *(outptr_pos++) = output_buffer;
269  }
270  }
271 
272  start_out_j += strategy::output_cols();
273 
274  const TWeight *params = static_cast<const TWeight *>(parameters);
275 
276  // Fill the input samples with padding. We can do this outside of
277  // the channel loop, as the position of padding isn't going to
278  // change as a function of channel.
279  for (auto i = 0u; i < kernel_points() * strategy::output_rows() * strategy::output_cols(); i++)
280  {
281  rearranged_input[i] = pad_value;
282  }
283 
284  // Loop over the input channels
285  for (unsigned int in_c = 0; in_c < input_channels; in_c++)
286  {
287  auto inptr_row = inptr_batch + in_c +
288  (start_in_i + pad_top) * ld_input_row +
289  (start_in_j + pad_left) * ld_input_col;
290 
291  // Construct the array of input samples; for each point of the
292  // kernel we provide an input value for each output point.
293  auto input_samples = rearranged_input;
294  for (auto ki = 0u; ki < this->m_args.kernel_rows; ki++)
295  {
296  for (auto kj = 0u; kj < this->m_args.kernel_cols; kj++)
297  {
298  // Copy the pointer for the input samples associated with this
299  // kernel point. Then update the main pointer to account for
300  // this point.
301  auto point_input_samples = input_samples;
302  input_samples += strategy::output_rows() * strategy::output_cols();
303 
304  int ii = static_cast<int>(ki) - static_cast<int>(pad_top);
305  for (auto oi = 0u;
306  oi < strategy::output_rows() &&
307  ii < static_cast<int>(input_rows()) - pad_rows;
308  oi++, ii += this->m_args.stride_rows)
309  {
310  if (0 <= ii) // Fill in values only if this row is in range.
311  {
312  int ij = static_cast<int>(kj) - static_cast<int>(pad_left);
313  for (auto oj = 0u;
314  oj < strategy::output_cols() &&
315  ij < static_cast<int>(input_cols()) - pad_cols;
316  oj++, ij += this->m_args.stride_cols)
317  {
318  if (0 <= ij) // Sample if the point is in range.
319  {
320  point_input_samples[oj] = *(inptr_row + ii*ld_input_row + ij*ld_input_col);
321  }
322  }
323  }
324 
325  point_input_samples += strategy::output_cols();
326  }
327  }
328  }
329 
330  tile_fn(inptrs, outptr_array, params, in_c, in_c*this->m_args.channel_multiplier);
331 
332  // Progress the output pointers
333  TOutput **outptr_pos = outptr_array;
334  for (auto i = 0u; i < strategy::output_rows() * strategy::output_cols(); i++)
335  {
336  outptr_pos[i] += this->m_args.channel_multiplier;
337  }
338 
339  // Progress the pointer into the parameters
340  params += param_stride;
341  }
342  }
343  }
344  }
345  }
346 
347  public:
348  DepthwiseDepthfirstGenericWithMultiplierBase(const DepthwiseArgs &args) : DepthwiseCommon<TInput, TWeight, TOutput>(args)
349  {
350  }
351 
354 
355  size_t get_storage_size(void) const override
356  {
357  const unsigned int vl = arm_gemm::utils::get_vector_length<TAccum>(strategy::vl_type);
358  const auto rounded_channels = this->m_args.input_channels * arm_gemm::roundup(this->m_args.channel_multiplier, vl);
359  return kernel_points() * rounded_channels * sizeof(TWeight);
360  }
361 
362  size_t get_working_size(const unsigned int n_threads, const unsigned int n_channels) const override
363  {
364  const unsigned int n_output_channels = n_channels * this->m_args.channel_multiplier;
365  return n_threads * (sizeof_inptr_array() +
366  sizeof_input_samples() +
367  sizeof_outptr_array() +
368  sizeof_output_buffer(n_output_channels));
369  }
370 };
371 
372 template <class strategy>
374 {
375  using TInput = typename strategy::input_type;
376  using TWeight = typename strategy::weight_type;
377  using TOutput = typename strategy::return_type;
378  using TAccum = typename strategy::bias_type;
379 
381 
382  const TAccum *m_biases; // Pointer to bias vector
383 
384  public:
386  : Parent(args), m_biases(nullptr)
387  {
388  }
389 
392 
393  void pack_parameters(void *buffer, const void *biases, const void *weights, size_t ld_weight_col, size_t ld_weight_row) override
394  {
395  m_biases = static_cast<const TAccum *>(biases);
396  Parent::pack_weights(static_cast<TAccum *>(buffer), static_cast<const TWeight *>(weights), ld_weight_col, ld_weight_row);
397  }
398 
400  void execute(
401  const unsigned int batches,
402  const unsigned int input_height,
403  const unsigned int input_width,
404  const unsigned int input_channels,
405  const PaddingValues &padding,
406  const void *const _input,
407  const size_t ld_input_col,
408  const size_t ld_input_row,
409  const size_t ld_input_batch,
410  const void *const parameters,
411  const unsigned int output_height,
412  const unsigned int output_width,
413  void *const _output,
414  const size_t ld_output_col,
415  const size_t ld_output_row,
416  const size_t ld_output_batch,
417  void *const _working_space,
418  const unsigned int thread_id,
419  const unsigned int n_threads
420  ) const override
421  {
422  strategy strat(this->m_args.cpu_info);
423 #ifdef CYCLE_PROFILING
424  arm_gemm::profiler prof;
425 #endif
426 
427  // Compute activation values
429  std::tie(activation_min, activation_max) = get_default_activation_values<TAccum>();
430 
431  switch (this->m_args.activation.type)
432  {
434  activation_max = static_cast<TAccum>(this->m_args.activation.param1);
435  // Fall through
437  activation_min = static_cast<TAccum>(0);
438  break;
439  default:
440  break;
441  }
442 
443  // Get a function to call for each point of the output
444  auto tile_fn = [&] (const TInput **inptrs,
445  TOutput **outptrs,
446  const TWeight *weights,
447  const unsigned int,
448  const unsigned int start_output_channel) {
449 #ifdef CYCLE_PROFILING
450  auto p = prof.ScopedProfiler(PROFILE_KERNEL, (unsigned long)(strategy::output_rows() * strategy::output_cols() * this->m_args.channel_multiplier * this->m_args.kernel_rows * this->m_args.kernel_cols));
451 #endif
452  strat.kernel(
453  inptrs, outptrs, weights,
454  m_biases ? m_biases + start_output_channel : nullptr,
455  this->kernel_points(), this->m_args.channel_multiplier,
456  activation_min, activation_max
457  );
458  };
459 
460  Parent::execute_tiles(
461  tile_fn, 0.0f,
462  batches, input_height, input_width, input_channels, padding,
463  _input, ld_input_col, ld_input_row, ld_input_batch,
464  parameters,
465  output_height, output_width,
466  _output, ld_output_col, ld_output_row, ld_output_batch,
467  _working_space, thread_id, n_threads
468  );
469  }
470 };
471 
472 } // namespace depthwise
473 } // namespace arm_conv
T roundup(const T a, const T b)
Definition: utils.hpp:70
T * output_buffer
T activation_min
T iceildiv(const T a, const T b)
Definition: utils.hpp:65
const size_t input_height
Definition: impl.cpp:61
DepthwiseDepthfirstGenericWithMultiplierBase & operator=(DepthwiseDepthfirstGenericWithMultiplierBase &)=delete
const size_t input_width
Definition: impl.cpp:62
void execute(const unsigned int batches, const unsigned int input_height, const unsigned int input_width, const unsigned int input_channels, const PaddingValues &padding, const void *const _input, const size_t ld_input_col, const size_t ld_input_row, const size_t ld_input_batch, const void *const parameters, const unsigned int output_height, const unsigned int output_width, void *const _output, const size_t ld_output_col, const size_t ld_output_row, const size_t ld_output_batch, void *const _working_space, const unsigned int thread_id, const unsigned int n_threads) const override
std::unique_ptr< ParametersLibrary > parameters
Definition: Framework.cpp:46
const StratType * strategy
size_t get_working_size(const unsigned int n_threads, const unsigned int n_channels) const override
T activation_max
void pack_parameters(void *buffer, const void *biases, const void *weights, size_t ld_weight_col, size_t ld_weight_row) override
template UniqueDepthwiseCommon< float > depthwise(const DepthwiseArgs &, const Nothing &)
unsigned int batches
T ** outptr_array