2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __NNFW_CKER_TRANSPOSE_CONV_H__
19 #define __NNFW_CKER_TRANSPOSE_CONV_H__
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23 #include "cker/Utils.h"
30 inline void TransposeConv(const TransposeConvParams ¶ms, 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)
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;
40 assert(input_shape.DimensionsCount() == 4);
41 assert(filter_shape.DimensionsCount() == 4);
42 assert(output_shape.DimensionsCount() == 4);
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);
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++)
64 output_data[i] = 0.0f;
67 // Loop through input elements one at a time.
68 for (int batch = 0; batch < batches; ++batch)
70 for (int in_y = 0; in_y < input_height; ++in_y)
72 for (int in_x = 0; in_x < input_width; ++in_x)
74 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
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)
81 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
83 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
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))
93 input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
94 float filter_value = filter_data[Offset(filter_shape, out_channel, filter_y,
95 filter_x, in_channel)];
96 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] +=
97 input_value * filter_value;
111 #endif // __NNFW_CKER_TRANSPOSE_CONV_H__