1 /*
2 * Copyright (c) 2022 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
PackingArguments(unsigned int kernel_rows,unsigned int kernel_cols,size_t weight_element_size,bool include_bias,size_t bias_element_size,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)33 PackingArguments::PackingArguments(
34 unsigned int kernel_rows, unsigned int kernel_cols, size_t weight_element_size,
35 bool include_bias, size_t bias_element_size,
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),
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
get_storage_size_generic(const PackingArguments & packing_args,const DepthwiseArgs & args)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)
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, 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
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)67 void pack_parameters_generic(
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)
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 {
99 pack_parameters_generic(
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 // Finalise the weight strides
111 ld_weight_col = (ld_weight_col == 0) ? args.input_channels : ld_weight_col;
112 ld_weight_row = (ld_weight_row == 0) ? packing_args.kernel_cols * ld_weight_col : ld_weight_row;
113
114 const unsigned int vl =
115 packing_args.accumulator_depth_vl *
116 arm_gemm::utils::get_vector_length<uint8_t>(packing_args.vl_type) / packing_args.accumulator_element_size;
117
118 for (unsigned int n = 0; n < args.input_channels; n += vl)
119 {
120 const unsigned int todo = std::min(vl, args.input_channels - n);
121
122 if (packing_args.include_bias)
123 {
124 if (biases != nullptr)
125 {
126 memcpy(buffer, biases, todo * packing_args.bias_element_size);
127 biases += todo * packing_args.bias_element_size;
128 }
129 else
130 {
131 memset(buffer, 0, vl * packing_args.bias_element_size);
132 }
133
134 buffer += vl * packing_args.bias_element_size;
135 }
136
137 // Copy each of the weights in turn
138 unsigned int kx, ky;
139 for (int kindex = 0; packing_args.get_weight_pos(kindex, kx, ky); kindex++)
140 {
141 const auto src_ptr = weights + (kx*ld_weight_row + ky*ld_weight_col + n) * packing_args.weight_element_size;
142 memcpy(buffer, src_ptr, todo * packing_args.weight_element_size);
143 buffer += vl * packing_args.weight_element_size;
144 }
145 }
146 }
147
148 } // namespace interleaves
149 } // namespace depthwise
150 } // namespace arm_conv
151