summaryrefslogtreecommitdiff
path: root/compiler/locomotiv/src/Node/TransposedConv2D.cpp
blob: 3ea4f071d9d76e4b5aa4f848e4d3eb279693949f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "NodeExecution.h"

#include "NodeDataImpl.h"
#include "NodeDomain.h"
#include "Validation.h"

#include <nncc/core/ADT/tensor/Shape.h>
#include <nncc/core/ADT/tensor/Buffer.h>
#include <nncc/core/ADT/tensor/Index.h>
#include <nncc/core/ADT/tensor/IndexEnumerator.h>
#include <nncc/core/ADT/tensor/LexicalLayout.h>

#include <cassert>
#include <stdexcept>

namespace
{

using nncc::core::ADT::tensor::Buffer;
using nncc::core::ADT::tensor::Shape;
using nncc::core::ADT::tensor::Index;
using nncc::core::ADT::tensor::IndexEnumerator;
using nncc::core::ADT::tensor::LexicalLayout;
using nncc::core::ADT::tensor::make_buffer;

/**
 * @brief Compute 1D output size for transposed convolution based on given 1D arguments.
 *
 * @param whole_pad  Sum of front and rear pad
 */
inline uint32_t compute_transposed_out_size(uint32_t input_size, uint32_t whole_pad,
                                            uint32_t filter_size, uint32_t stride)
{
  return stride * (input_size - 1) + filter_size - whole_pad;
}

/**
 * @brief Calculates TransposedConv2D
 * @note  Both input_buf and filter_buf have NHWC format
 */
template <typename RET_T, typename IFM_T, typename FIL_T>
Buffer<RET_T> calc_tr_conv2D(const loco::TransposedConv2D *tr_conv2d,
                             const Buffer<IFM_T> *input_buf, const Buffer<FIL_T> *filter_buf)
{
  auto input_shape = input_buf->shape();
  auto filter_shape = filter_buf->shape();

  locomotiv::validate(input_shape.rank() == 4, "ifm rank must be 4");
  locomotiv::validate(filter_shape.rank() == 4, "filter rank must be 4");
  locomotiv::validate(input_shape.dim(3) /* depth of input */ ==
                          filter_shape.dim(3) /* depth of filter */,
                      "channel value mismatch");

  const uint32_t input_height = input_shape.dim(1);
  const uint32_t input_width = input_shape.dim(2);

  const uint32_t filter_height = filter_shape.dim(1);
  const uint32_t filter_width = filter_shape.dim(2);

  const uint32_t stride_width = tr_conv2d->stride()->horizontal();
  const uint32_t stride_height = tr_conv2d->stride()->vertical();

  const uint32_t pad_top = tr_conv2d->pad()->top();
  const uint32_t pad_bottom = tr_conv2d->pad()->bottom();

  const uint32_t pad_left = tr_conv2d->pad()->left();
  const uint32_t pad_right = tr_conv2d->pad()->right();

  // TODO Support dilations

  const uint32_t output_height =
      compute_transposed_out_size(input_height, pad_top + pad_bottom, filter_height, stride_height);
  const uint32_t output_width =
      compute_transposed_out_size(input_width, pad_left + pad_right, filter_width, stride_width);

  const uint32_t batches = input_shape.dim(0);
  const uint32_t input_depth = input_shape.dim(3);
  const uint32_t output_depth = filter_shape.dim(0); // count of filter

  Shape output_shape{batches, output_height, output_width, output_depth};
  auto output_buf = make_buffer<RET_T, LexicalLayout>(output_shape);

  // initialize output
  for (IndexEnumerator e{output_shape}; e.valid(); e.advance())
  {
    const auto &index = e.current();
    output_buf.at(index) = static_cast<RET_T>(0);
  }

  // Loop through input elements one at a time.
  for (uint32_t batch = 0; batch < batches; ++batch)
  {
    for (uint32_t in_y = 0; in_y < input_height; ++in_y)
    {
      for (uint32_t in_x = 0; in_x < input_width; ++in_x)
      {
        for (uint32_t in_channel = 0; in_channel < input_depth; ++in_channel)
        {
          // Loop through the output elements it will influence
          const int out_x_origin = (in_x * stride_width) - pad_left;
          const int out_y_origin = (in_y * stride_height) - pad_top;
          for (uint32_t filter_y = 0; filter_y < filter_height; ++filter_y)
          {
            for (uint32_t filter_x = 0; filter_x < filter_width; ++filter_x)
            {
              for (uint32_t out_channel = 0; out_channel < output_depth; ++out_channel)
              {
                // Compute output element location
                const int out_x = out_x_origin + filter_x;
                const int out_y = out_y_origin + filter_y;
                // We cannot accumulate out of bounds
                if ((out_x >= 0) && ((unsigned)out_x < output_width) && (out_y >= 0) &&
                    ((unsigned)out_y < output_height))
                {
                  auto input_value = input_buf->at(Index({batch, in_y, in_x, in_channel}));
                  auto filter_value =
                      filter_buf->at(Index({out_channel, filter_y, filter_x, in_channel}));
                  output_buf.at(Index({batch, (unsigned)out_y, (unsigned)out_x, out_channel})) +=
                      input_value * filter_value;
                }
              }
            }
          }
        }
      }
    }
  }
  return output_buf;
}

} // namespace

namespace locomotiv
{

void NodeExecution::execute(loco::TransposedConv2D *tr_conv2d)
{
  auto ifm_data = annot_data(tr_conv2d->ifm());
  auto ker_data = annot_data(tr_conv2d->ker());

  validate(ifm_data, "Can't find input data of TransposedConv2D");
  validate(ifm_data->shape()->rank() == 4, "ifm rank must be 4");

  validate(ker_data, "Can't find kernel data of TransposedConv2D");
  validate(ker_data->shape()->rank() == 4, "Kernel rank must be 4");

  validate(annot_domain(tr_conv2d->ifm()) == loco::Domain::Feature,
           "IFM of TransposedConv2D is not feature");
  validate(annot_domain(tr_conv2d->ker()) == loco::Domain::Filter,
           "Kernel of TransposedConv2D is not filter");

  std::unique_ptr<NodeData> tr_conv2d_result = nullptr;

  if (ifm_data->dtype() == loco::DataType::FLOAT32 && ker_data->dtype() == loco::DataType::FLOAT32)
  {
    auto ifm_buf = ifm_data->as_f32_bufptr();
    auto ker_buf = ker_data->as_f32_bufptr();

    auto tr_conv2d_buf = calc_tr_conv2D<float, float, float>(tr_conv2d, ifm_buf, ker_buf);

    tr_conv2d_result = make_data(tr_conv2d_buf);
  }
  else
    throw std::runtime_error("NYI for these DataTypes");

  assert(tr_conv2d_result != nullptr);

  annot_data(tr_conv2d, std::move(tr_conv2d_result));
  annot_domain(tr_conv2d, loco::Domain::Feature);
}

} // namespace locomotiv