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_quantized_dot_product.hpp"
26 #include <cstdint>
27 
28 namespace arm_conv {
29 namespace depthwise {
30 namespace interleaves {
31 namespace quantized {
32 
get_storage_size(const DepthwiseArgs & args,const arm_gemm::VLType vl_type,const unsigned int accumulator_depth_vl)33 size_t get_storage_size(
34   const DepthwiseArgs &args,
35   const arm_gemm::VLType vl_type,
36   const unsigned int accumulator_depth_vl
37 )
38 {
39   // We produce VL<int32_t> channels at a time, for each of these blocks of
40   // channels we store a vector of biases, weights (complicated) and
41   // requantize parameters.
42   const unsigned int iter_length = accumulator_depth_vl * arm_gemm::utils::get_vector_length<int32_t>(vl_type);
43   const unsigned int n_iters = args.input_channels * arm_gemm::iceildiv(args.channel_multiplier, iter_length);
44 
45   // Compute the cost of storing the weights
46   const unsigned int n_dots_per_kernel_row = arm_gemm::iceildiv(args.kernel_cols, 4u);
47 
48   return n_iters * iter_length * (
49     sizeof(int32_t) +  // Bias
50     4 * n_dots_per_kernel_row * args.kernel_rows * sizeof(int8_t) +  // Weights
51     2 * sizeof(int32_t)  // Requantisation parameters
52   );
53 }
54 
55 template <typename T>
pack_parameters(void * _buffer,const int32_t * biases,const T * weights,size_t ld_weight_col,size_t ld_weight_row,const DepthwiseArgs & args,const arm_gemm::Requantize32 & qp,const arm_gemm::VLType vl_type,const unsigned int accumulator_depth_vl)56 void pack_parameters(
57   void *_buffer, const int32_t *biases,
58   const T *weights, size_t ld_weight_col, size_t ld_weight_row,
59   const DepthwiseArgs &args,
60   const arm_gemm::Requantize32 &qp,
61   const arm_gemm::VLType vl_type,
62   const unsigned int accumulator_depth_vl
63 )
64 {
65   auto buffer = static_cast<uint8_t *>(_buffer);
66   auto requant_muls = qp.per_channel_muls;
67   auto requant_shifts = qp.per_channel_right_shifts;
68 
69   const unsigned int iter_length = accumulator_depth_vl * arm_gemm::utils::get_vector_length<int32_t>(vl_type);
70   const unsigned int n_iters_per_input_channel = arm_gemm::iceildiv(args.channel_multiplier, iter_length);
71   const unsigned int n_dots_per_kernel_row = arm_gemm::iceildiv(args.kernel_cols, 4u);
72 
73   const size_t iter_stride = iter_length * (
74       sizeof(int32_t) +  // Bias
75       4 * n_dots_per_kernel_row * args.kernel_rows * sizeof(T) +  // Weights
76       2 * sizeof(int32_t)  // Requantisation parameters
77   );
78 
79   ld_weight_col = (ld_weight_col == 0) ? args.input_channels * args.channel_multiplier : ld_weight_col;
80   ld_weight_row = (ld_weight_row == 0) ? args.kernel_cols * ld_weight_col : ld_weight_row;
81 
82   for (unsigned int input_channel = 0; input_channel < args.input_channels; input_channel++)
83   {
84     auto buffer_input_channel = buffer + input_channel * n_iters_per_input_channel * iter_stride;
85     auto weights_input_channel = weights + input_channel * args.channel_multiplier;
86 
87     for (unsigned int iter = 0; iter < n_iters_per_input_channel; iter++)
88     {
89       // Get a pointer to the start of this portion of the buffer; consequently
90       // derive pointers to the bias, weight and requantisation portions of
91       // this frame.
92       auto buffer_base = buffer_input_channel + iter_stride * iter;
93       auto buffer_biases = reinterpret_cast<int32_t *>(buffer_base);
94       auto buffer_weights = buffer_base + sizeof(int32_t) * iter_length;
95       auto buffer_requant_mul = reinterpret_cast<int32_t *>(
96         buffer_weights + args.kernel_rows * n_dots_per_kernel_row * 4 * iter_length);
97       auto buffer_requant_shift = buffer_requant_mul + iter_length;
98       auto weights_base = weights_input_channel + iter * iter_length;
99 
100       // Hence work through the data for this iteration, on a
101       // channel-by-channel basis.
102       const auto this_iter_length = std::min<unsigned int>(
103         iter_length, args.channel_multiplier - iter * iter_length
104       );
105       for (unsigned int i = 0; i < this_iter_length; i++)
106       {
107         auto weights_channel = weights_base + i;
108 
109         // Read the bias value, we modify this as we read the weights.
110         auto bias_value = biases == nullptr ? 0 : *(biases++);
111         int32_t elements_sum = 0;
112 
113         // Read through the kernel; for each row, marshal together as many dot
114         // product terms as are required.
115         for (unsigned int ki = 0; ki < args.kernel_rows; ki++)
116         {
117           auto buffer_row = buffer_weights + i*4 + ki * 4 * n_dots_per_kernel_row * iter_length;
118           auto weights_row = weights_channel + ki * ld_weight_row;
119 
120           unsigned int kj = 0;
121           for (; kj < args.kernel_cols; kj++)
122           {
123             // Determine which element to which we're writing
124             const auto dot = kj / 4;
125             const auto elem = kj % 4;
126 
127             // Copy the value; include in the sum
128             const auto val = weights_row[kj * ld_weight_col];
129             buffer_row[dot * 4 * iter_length + elem] = val;
130             elements_sum += val;
131           }
132           for (; kj < 4 * n_dots_per_kernel_row; kj++)
133           {
134             const auto dot = kj / 4;
135             const auto elem = kj % 4;
136             buffer_row[dot * 4 * iter_length + elem] = 0;
137           }
138 
139           buffer_row += 4 * n_dots_per_kernel_row * iter_length;
140         }
141 
142         // Write back the bias and offset values
143         *(buffer_biases++) =
144           bias_value - qp.a_offset * elements_sum +
145           args.kernel_rows * args.kernel_cols * qp.a_offset * qp.b_offset;
146 
147         // Write out the requantisation parameters
148         *(buffer_requant_mul++) = qp.per_channel_requant ? *(requant_muls++) : qp.per_layer_mul;
149         *(buffer_requant_shift++) = qp.per_channel_requant ? *(requant_shifts++) : qp.per_layer_right_shift;
150       }
151     }
152   }
153 }
154 
155 template void pack_parameters(void *, const int32_t *, const int8_t *, size_t, size_t, const DepthwiseArgs &, const arm_gemm::Requantize32 &, arm_gemm::VLType, unsigned int);
156 template void pack_parameters(void *, const int32_t *, const uint8_t *, size_t, size_t, const DepthwiseArgs &, const arm_gemm::Requantize32 &, arm_gemm::VLType, unsigned int);
157 
158 }  // namespace quantized
159 }  // namespace interleaves
160 }  // namespace depthwise
161 }  // namespace arm_conv
162