blob: 26384651f1994da993a2d0983487ec7cacf13fb3 [file] [log] [blame]
Georgios Pinitas358ca202017-12-07 16:47:52 +00001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas358ca202017-12-07 16:47:52 +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#ifndef __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__
25#define __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__
26
27#include "arm_compute/core/ITensorInfo.h"
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000028#include "arm_compute/core/Utils.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000029
30namespace arm_compute
31{
32namespace misc
33{
34namespace shape_calculator
35{
Pablo Tello00afd112018-01-04 10:34:24 +000036inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
37{
38 TensorShape output_shape = input.tensor_shape();
39 permute(output_shape, perm);
40 return output_shape;
41}
Gian Marco36a0a462018-01-12 10:21:40 +000042inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +000043{
Gian Marco36a0a462018-01-12 10:21:40 +000044 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
45 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
46 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +000047 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +000048 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
49 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
Georgios Pinitas358ca202017-12-07 16:47:52 +000050
51 return shape_interleaved_a;
52}
53inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
54{
55 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
56 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
57 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
58 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
59
60 return shape_transposed1xW_b;
61}
Gian Marco36a0a462018-01-12 10:21:40 +000062inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +000063{
Gian Marco36a0a462018-01-12 10:21:40 +000064 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
65 // The transpose1xW output matrix will have the following shape:
66 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
67 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +000068 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +000069 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +000070 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
71 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
72
73 return shape_transposed1xW_b;
74}
75inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
76{
77 TensorShape shape_vector_sum_col{ b.tensor_shape() };
78 if(shape_vector_sum_col.num_dimensions() > 1)
79 {
80 shape_vector_sum_col.remove_dimension(1);
81 }
82
83 return shape_vector_sum_col;
84}
85inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
86{
87 TensorShape shape_vector_sum_row{ a.tensor_shape() };
88 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
89 if(a.num_dimensions() > 1)
90 {
91 shape_vector_sum_row.remove_dimension(1);
92 }
93
94 return shape_vector_sum_row;
95}
96inline TensorShape compute_im2col_shape(const ITensorInfo &input)
97{
98 TensorShape shape_im2col{ input.tensor_shape() };
99 shape_im2col.collapse(3);
100
101 return shape_im2col;
102}
103inline TensorShape compute_transposed_shape(const ITensorInfo &input)
104{
105 TensorShape shape_transposed{ input.tensor_shape() };
106
107 shape_transposed.set(0, input.dimension(1));
108 shape_transposed.set(1, input.dimension(0));
109
110 return shape_transposed;
111}
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000112inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
113{
114 const TensorShape input_shape{ input.tensor_shape() };
115 const TensorShape weights_shape{ weights.tensor_shape() };
116
117 unsigned int output_width = 0;
118 unsigned int output_height = 0;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000119 std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(),
120 weights_shape.x(), weights_shape.y(),
121 conv_info);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000122
123 TensorShape output_shape{ input_shape };
124 output_shape.set(0, output_width);
125 output_shape.set(1, output_height);
126
127 return output_shape;
128}
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000129inline TensorShape compute_deconvolution_shape(const ITensorInfo &input, unsigned int sx, unsigned int sy, unsigned int inner_border_right, unsigned int inner_border_top, const PadStrideInfo &info)
130{
131 TensorShape scale_out_shape(input.tensor_shape());
132 const unsigned int out_x = input.dimension(0) + (input.dimension(0) - 1) * (sx - 1) + inner_border_right + 2 * info.pad().first;
133 const unsigned int out_y = input.dimension(1) + (input.dimension(1) - 1) * (sy - 1) + inner_border_top + 2 * info.pad().second;
134 scale_out_shape.set(0, out_x);
135 scale_out_shape.set(1, out_y);
136
137 return scale_out_shape;
138}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000139} // namespace shape_calculator
140} // namespace misc
141} // namespace arm_compute
142#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */