Compute Library
 23.11
generic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023 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 #include "generic.hpp"
26 
27 #include <functional>
28 
29 namespace arm_conv {
30 namespace depthwise {
31 namespace interleaves {
32 
34  unsigned int kernel_rows, unsigned int kernel_cols, size_t weight_element_size,
35  bool include_bias, size_t bias_element_size, bool premultiply,
36  arm_gemm::VLType vl_type, size_t accumulator_element_size, unsigned int accumulator_depth_vl,
37  std::function<bool(unsigned int, unsigned int &, unsigned int &)> get_weight_pos
38 ) : kernel_rows(kernel_rows), kernel_cols(kernel_cols), weight_element_size(weight_element_size),
39  include_bias(include_bias), bias_element_size(bias_element_size), premultiply(premultiply),
40  vl_type(vl_type), accumulator_element_size(accumulator_element_size), accumulator_depth_vl(accumulator_depth_vl),
41  get_weight_pos(get_weight_pos)
42 {
43 }
44 
45 size_t get_storage_size_generic(const PackingArguments &packing_args, const DepthwiseArgs &args)
46 {
47  // If the channel multiplier is greater than one, then we treat this as a
48  // repeated packing of `channel_multiplier`-sized problems.
49  if (args.channel_multiplier > 1 && !packing_args.premultiply)
50  {
51  DepthwiseArgs args_per_input_channel(args);
52  args_per_input_channel.input_channels = args.channel_multiplier;
53  args_per_input_channel.channel_multiplier = 1;
54 
55  return args.input_channels * get_storage_size_generic(packing_args, args_per_input_channel);
56  }
57 
58  const unsigned int vl =
59  packing_args.accumulator_depth_vl *
60  arm_gemm::utils::get_vector_length<uint8_t>(packing_args.vl_type) / packing_args.accumulator_element_size;
61  const unsigned int n_packs = arm_gemm::iceildiv(args.input_channels * args.channel_multiplier, vl);
62  const auto pack_size = (packing_args.include_bias ? packing_args.bias_element_size : 0) +
63  packing_args.kernel_points() * packing_args.weight_element_size;
64  return n_packs * pack_size * vl;
65 }
66 
68  const PackingArguments &packing_args,
69  const DepthwiseArgs &args,
70  void *buffer_raw,
71  const void *biases_raw,
72  const void *weights_raw,
73  size_t ld_weight_col,
74  size_t ld_weight_row
75 )
76 {
77  // Cast the pointers to byte sizes
78  auto *buffer = static_cast<uint8_t *>(buffer_raw);
79  auto *biases = static_cast<const uint8_t *>(biases_raw);
80  auto *weights = static_cast<const uint8_t *>(weights_raw);
81 
82  // If the channel multiplier is greater than one, then we treat this as a
83  // repeated packing of `channel_multiplier`-sized problems.
84  if (args.channel_multiplier > 1 && !packing_args.premultiply)
85  {
86  // Get a modified copy of the depthwise arguments
87  DepthwiseArgs args_per_input_channel(args);
88  args_per_input_channel.input_channels = args.channel_multiplier;
89  args_per_input_channel.channel_multiplier = 1;
90 
91  // Resolve the strides here
92  ld_weight_col = ld_weight_col ? ld_weight_col : args.input_channels * args.channel_multiplier;
93  ld_weight_row = ld_weight_row ? ld_weight_row : ld_weight_col * packing_args.kernel_cols;
94 
95  auto per_input_channel_size = get_storage_size_generic(packing_args, args_per_input_channel);
96 
97  for (unsigned int c = 0; c < args.input_channels; c++)
98  {
100  packing_args, args_per_input_channel, buffer, biases, weights, ld_weight_col, ld_weight_row);
101 
102  // Update the pointers
103  buffer += per_input_channel_size;
104  biases += (biases == nullptr) ? 0 : packing_args.bias_element_size * args.channel_multiplier;
105  weights += packing_args.weight_element_size * args.channel_multiplier;
106  }
107  return;
108  }
109 
110  auto input_channels = args.input_channels * args.channel_multiplier;
111 
112  // Finalise the weight strides
113  ld_weight_col = (ld_weight_col == 0) ? input_channels : ld_weight_col;
114  ld_weight_row = (ld_weight_row == 0) ? packing_args.kernel_cols * ld_weight_col : ld_weight_row;
115 
116  const unsigned int vl =
117  packing_args.accumulator_depth_vl *
118  arm_gemm::utils::get_vector_length<uint8_t>(packing_args.vl_type) / packing_args.accumulator_element_size;
119 
120  for (unsigned int n = 0; n < input_channels; n += vl)
121  {
122  const unsigned int todo = std::min(vl, input_channels - n);
123 
124  if (packing_args.include_bias)
125  {
126  if (biases != nullptr)
127  {
128  memcpy(buffer, biases, todo * packing_args.bias_element_size);
129  biases += todo * packing_args.bias_element_size;
130  }
131  else
132  {
133  memset(buffer, 0, vl * packing_args.bias_element_size);
134  }
135 
136  buffer += vl * packing_args.bias_element_size;
137  }
138 
139  // Copy each of the weights in turn
140  unsigned int kx, ky;
141  for (int kindex = 0; packing_args.get_weight_pos(kindex, kx, ky); kindex++)
142  {
143  const auto src_ptr = weights + (kx*ld_weight_row + ky*ld_weight_col + n) * packing_args.weight_element_size;
144  memcpy(buffer, src_ptr, todo * packing_args.weight_element_size);
145  buffer += vl * packing_args.weight_element_size;
146  }
147  }
148 }
149 
150 } // namespace interleaves
151 } // namespace depthwise
152 } // namespace arm_conv
arm_conv::depthwise::interleaves::PackingArguments::vl_type
arm_gemm::VLType vl_type
Definition: generic.hpp:44
arm_conv::depthwise::interleaves::PackingArguments
Definition: generic.hpp:36
GemmTuner.args
args
Definition: GemmTuner.py:679
arm_conv::depthwise::depthwise
template UniqueDepthwiseCommon< float > depthwise(const DepthwiseArgs &, const Nothing &)
arm_conv::depthwise::interleaves::PackingArguments::kernel_points
unsigned int kernel_points(void) const
Definition: generic.hpp:49
arm_conv::depthwise::interleaves::pack_parameters_generic
void pack_parameters_generic(const PackingArguments &packing_args, const DepthwiseArgs &args, void *buffer_raw, const void *biases_raw, const void *weights_raw, size_t ld_weight_col, size_t ld_weight_row)
Definition: generic.cpp:67
arm_conv::depthwise::interleaves::PackingArguments::accumulator_depth_vl
const unsigned int accumulator_depth_vl
Definition: generic.hpp:46
arm_conv::depthwise::interleaves::get_storage_size_generic
size_t get_storage_size_generic(const PackingArguments &packing_args, const DepthwiseArgs &args)
Definition: generic.cpp:45
arm_gemm::iceildiv
T iceildiv(const T a, const T b)
Definition: utils.hpp:65
arm_conv::depthwise::interleaves::PackingArguments::PackingArguments
PackingArguments(unsigned int kernel_rows, unsigned int kernel_cols, size_t weight_element_size, bool include_bias, size_t bias_element_size, bool premultiply, arm_gemm::VLType vl_type, size_t accumulator_element_size, unsigned int accumulator_depth_vl, std::function< bool(unsigned int, unsigned int &, unsigned int &)> get_weight_pos)
Definition: generic.cpp:33
arm_conv::depthwise::interleaves::PackingArguments::premultiply
const bool premultiply
Definition: generic.hpp:43
arm_conv::depthwise::interleaves::PackingArguments::accumulator_element_size
const size_t accumulator_element_size
Definition: generic.hpp:45
arm_conv::depthwise::interleaves::PackingArguments::bias_element_size
const size_t bias_element_size
Definition: generic.hpp:42
arm_gemm::VLType
VLType
Definition: utils.hpp:80
arm_conv::depthwise::interleaves::PackingArguments::kernel_cols
const unsigned int kernel_cols
Definition: generic.hpp:39
arm_conv
Definition: addressing.cpp:30
arm_conv::depthwise::interleaves::PackingArguments::include_bias
const bool include_bias
Definition: generic.hpp:41
arm_conv::depthwise::interleaves::PackingArguments::weight_element_size
const size_t weight_element_size
Definition: generic.hpp:40
arm_conv::depthwise::interleaves::PackingArguments::get_weight_pos
std::function< bool(unsigned int, unsigned int &, unsigned int &)> get_weight_pos
Definition: generic.hpp:47
generic.hpp