• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 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 "impl_base.hpp"
26 
27 // TODO Move to common utilities somewhere
28 template <size_t Size> struct DType { };
29 template <> struct DType<1> { using scalar_type = uint8_t; };
30 template <> struct DType<2> { using scalar_type = uint16_t; };
31 template <> struct DType<4> { using scalar_type = uint32_t; };
32 
33 namespace depthwise
34 {
35 
36 template <unsigned int KernelRows, unsigned int KernelColumns, size_t WeightSize, size_t BiasSize>
execute(unsigned int n_channels,void * buffer,const void * weights,const unsigned int weight_row_stride,const unsigned int weight_col_stride,const void * biases)37 void PackParameters<KernelRows, KernelColumns, WeightSize, BiasSize>::execute(
38   unsigned int n_channels,
39   void *buffer,
40   const void *weights,
41   const unsigned int weight_row_stride,
42   const unsigned int weight_col_stride,
43   const void *biases
44 )
45 {
46   using TWeight = typename DType<WeightSize>::scalar_type;
47   using TBias = typename DType<BiasSize>::scalar_type;
48 
49   auto buffer_ptr = static_cast<uint8_t *>(buffer);
50   auto weights_ptr = static_cast<const TWeight *>(weights);
51   auto biases_ptr = static_cast<const TBias *>(biases);
52 
53   const unsigned int veclen = 16 / WeightSize;
54   for (; n_channels >= veclen; n_channels -= veclen)
55   {
56     // Copy biases
57     for (unsigned int i = 0; i < veclen; i++)
58     {
59       auto ptr = reinterpret_cast<TBias *>(buffer_ptr);
60       *ptr = (biases_ptr == nullptr) ? 0x0 : *(biases_ptr++);
61       buffer_ptr += BiasSize;
62     }
63 
64     // Copy weights
65     for (unsigned int i = 0; i < KernelRows; i++)
66     {
67       for (unsigned int j = 0; j < KernelColumns; j++)
68       {
69         for (unsigned int c = 0; c < veclen; c++)
70         {
71           *(reinterpret_cast<TWeight *>(buffer_ptr)) = weights_ptr[i*weight_row_stride + j*weight_col_stride + c];
72           buffer_ptr += WeightSize;
73         }
74       }
75     }
76     weights_ptr += veclen;
77   }
78   for (; n_channels; n_channels--)
79   {
80     // Copy bias
81     auto ptr = reinterpret_cast<TBias *>(buffer_ptr);
82     *ptr = (biases_ptr == nullptr) ? 0x0 : *(biases_ptr++);
83     buffer_ptr += BiasSize;
84 
85     // Copy weights
86     for (unsigned int i = 0; i < KernelRows; i++)
87     {
88       for (unsigned int j = 0; j < KernelColumns; j++)
89       {
90         *(reinterpret_cast<TWeight *>(buffer_ptr)) = weights_ptr[i*weight_row_stride + j*weight_col_stride];
91         buffer_ptr += WeightSize;
92       }
93     }
94     weights_ptr++;
95   }
96 }
97 
98 template struct PackParameters<3, 3, 2ul, 2ul>;
99 template struct PackParameters<3, 3, 4ul, 4ul>;
100 template struct PackParameters<5, 5, 2ul, 2ul>;
101 template struct PackParameters<5, 5, 4ul, 4ul>;
102 }  // namespace
103