Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / TransposeConv.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __NNFW_CKER_TRANSPOSE_CONV_H__
19 #define __NNFW_CKER_TRANSPOSE_CONV_H__
20
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23 #include "cker/Utils.h"
24
25 namespace nnfw
26 {
27 namespace cker
28 {
29
30 inline void TransposeConv(const TransposeConvParams &params, const Shape &input_shape,
31                           const float *input_data, const Shape &filter_shape,
32                           const float *filter_data, const Shape &output_shape, float *output_data)
33 {
34
35   const int stride_width = params.stride_width;
36   const int stride_height = params.stride_height;
37   const int pad_width = params.padding_values.width;
38   const int pad_height = params.padding_values.height;
39
40   assert(input_shape.DimensionsCount() == 4);
41   assert(filter_shape.DimensionsCount() == 4);
42   assert(output_shape.DimensionsCount() == 4);
43
44   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
45   const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
46   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
47   const int input_height = input_shape.Dims(1);
48   const int input_width = input_shape.Dims(2);
49   const int filter_height = filter_shape.Dims(1);
50   const int filter_width = filter_shape.Dims(2);
51   const int output_height = output_shape.Dims(1);
52   const int output_width = output_shape.Dims(2);
53
54   // Although transpose convolution simplifies to convolution with transposed
55   // weights for strides of 1, non-unitary striding complicates matters. To
56   // keep this reference implementation as clear as possible, we use a
57   // "scatter" access pattern, where we loop through all the input elements,
58   // computing their influence on the output, rather than looping through the
59   // output elements in the typical "gather" access pattern of a conv. We
60   // therefore must initialize the output array to zero.
61   const int num_elements = output_shape.FlatSize();
62   for (int i = 0; i < num_elements; i++)
63   {
64     output_data[i] = 0.0f;
65   }
66
67   // Loop through input elements one at a time.
68   for (int batch = 0; batch < batches; ++batch)
69   {
70     for (int in_y = 0; in_y < input_height; ++in_y)
71     {
72       for (int in_x = 0; in_x < input_width; ++in_x)
73       {
74         for (int in_channel = 0; in_channel < input_depth; ++in_channel)
75         {
76           // Loop through the output elements it will influence
77           const int out_x_origin = (in_x * stride_width) - pad_width;
78           const int out_y_origin = (in_y * stride_height) - pad_height;
79           for (int filter_y = 0; filter_y < filter_height; ++filter_y)
80           {
81             for (int filter_x = 0; filter_x < filter_width; ++filter_x)
82             {
83               for (int out_channel = 0; out_channel < output_depth; ++out_channel)
84               {
85                 // Compute output element location
86                 const int out_x = out_x_origin + filter_x;
87                 const int out_y = out_y_origin + filter_y;
88                 // We cannot accumulate out of bounds
89                 if ((out_x >= 0) && (out_x < output_width) && (out_y >= 0) &&
90                     (out_y < output_height))
91                 {
92                   float input_value =
93                     input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
94                   float filter_value =
95                     filter_data[Offset(filter_shape, out_channel, filter_y, filter_x, in_channel)];
96                   output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] +=
97                     input_value * filter_value;
98                 }
99               }
100             }
101           }
102         }
103       }
104     }
105   }
106 }
107
108 } // namespace cker
109 } // namespace nnfw
110
111 #endif // __NNFW_CKER_TRANSPOSE_CONV_H__