blob: 5ff848e281748b49e9af372e8d6e493da9724488 [file] [log] [blame]
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001/*
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +00002 * Copyright (c) 2021-2023 Arm Limited.
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00003 *
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#pragma once
26
27#include "arm_gemm.hpp"
28#include "common.hpp"
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +000029#include <cstddef>
30#include <tuple>
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000031
32namespace arm_conv
33{
34namespace depthwise
35{
36using arm_gemm::Nothing;
37
38enum class DepthwiseMethod
39{
40 DEFAULT,
41 DEPTHFIRST,
42 PLANAR,
43};
44
45struct KernelDescription
46{
47 DepthwiseMethod method = DepthwiseMethod::DEFAULT;
48 std::string name = "";
49 bool is_default = false;
50 uint64_t cycle_estimate = 0;
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 KernelDescription(DepthwiseMethod method, std::string name, bool is_default, uint64_t cycle_estimate)
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000053 : method(method), name(name), is_default(is_default), cycle_estimate(cycle_estimate)
54 {
55 }
56
57 KernelDescription() noexcept {};
58};
59
60class IDepthwiseCommon
61{
62public:
63 virtual ~IDepthwiseCommon() = default;
64
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +000065 // Get the name of the depthwise implementation
66 virtual std::string name() const = 0;
67
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000068 // Determine the amount of storage space required for the rearranged weights
69 // and bias.
70 virtual size_t get_storage_size(void) const = 0;
71
72 // Rearrange the weights and biases into a storage buffer.
73 // Accepts a pointer to a buffer into which to store the packed parameters, a
74 // pointer the bias vector (which may be nullptr in the case of no bias) and
75 // a pointer to the array of weights (stored in HWIO order).
76 virtual void pack_parameters(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 void *buffer, const void *biases, const void *weights, size_t ld_weight_col = 0, size_t ld_weight_row = 0) = 0;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000078
79 // Determine the amount of working space required
Michael Tyler8deee9b2023-06-30 11:26:05 +010080 virtual size_t get_working_size(unsigned int n_threads) const = 0;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000081
82 // Execute the convolution over the specified area of memory.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 virtual void execute(const void *input, // Pointer to input tensor
84 const void *parameters, // Packed parameters buffer
85 void *output,
86 void *working_space,
87 unsigned int thread_id,
88 unsigned int n_threads) const = 0;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000089
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 virtual void execute(const void *input,
91 size_t ld_input_col,
92 size_t ld_input_row,
93 size_t ld_input_batch,
94 const void *parameters,
95 void *output,
96 size_t ld_output_col,
97 size_t ld_output_row,
98 size_t ld_output_batch,
99 void *working_space,
100 unsigned int thread_id,
101 unsigned int n_threads) const = 0;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 virtual void execute(unsigned int batches,
104 unsigned int input_height,
105 unsigned int input_width,
106 unsigned int channels,
107 const PaddingValues &,
108 const void *input,
109 size_t ld_input_col,
110 size_t ld_input_row,
111 size_t ld_input_batch,
112 const void *parameters,
113 unsigned int output_height,
114 unsigned int output_width,
115 void *output,
116 size_t ld_output_col,
117 size_t ld_output_row,
118 size_t ld_output_batch,
119 void *working_space,
120 unsigned int thread_id,
121 unsigned int n_threads) const = 0;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000122};
123
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000124// To handle a dilation factor of D execute the kernel once for each d in
125// [0..D). Each `d` corresponds to a portion or "view" of the input and output
126// tensors. The output view corresponds to every Dth pixel starting from `d`;
127// this function computes how many pixels are covered. The input view consists
128// of an amount of before padding, every Dth pixel starting from an offset, and
129// some after padding. This function computes the start padding, input offset,
130// number of valid input pixels, and the after padding.
131//
132// Returns
133// - Number of valid output pixels corresponding to `d`
134// - Number of valid input pixels corresponding to `d`
135// - Offset of the first pixel corresponding to `d`
136// - Amount of padding in the view for `d`
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137std::tuple<size_t, size_t, size_t, size_t, size_t> get_reduced_view_for_dilation(size_t out_size,
138 size_t in_size,
139 size_t d,
140 size_t dilation_factor,
141 size_t kernel_size,
142 size_t stride,
143 size_t pad_before);
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000144
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000145} // namespace depthwise
146} // namespace arm_conv