Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019 Arm Limited. |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 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 | |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 25 | #include "impl_base.hpp" |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 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> |
| 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>; |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 100 | template struct PackParameters<5, 5, 2ul, 2ul>; |
| 101 | template struct PackParameters<5, 5, 4ul, 4ul>; |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 102 | } // namespace |