summaryrefslogtreecommitdiff
path: root/runtimes/neurun/backend/srcn/ConstantInitializer.cc
blob: f37ebe9a418b337af5c03dd7e7f3c159e0b239ff (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
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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 "ConstantInitializer.h"

#include "kernel/OperationUtils.h"

namespace
{

template <typename T>
static void
PermuteKernel(const neurun::model::Operand &model_obj, neurun::backend::operand::IObject &obj,
              const neurun::model::Layout frontend_layout = neurun::model::Layout::UNKNOWN)
{
  const auto shape = model_obj.shape();
  auto base = reinterpret_cast<const T *>(model_obj.data().base());

  assert(shape.rank() == 4);

  // TODO Support frontend layout
  UNUSED_RELEASE(frontend_layout);

  obj.access([&](::neurun::backend::operand::ITensor &tensor) {
    // NOTE The srcn takes a HWOI layout as kernel filter even though image layout is NHWC.
    //      This policy is the same with the tensorflow policy.
    //      So using srcn library, we need to change kernel layout to HWOI from OHWI.
    const int32_t outch = shape.dim(0);
    const int32_t height = shape.dim(1);
    const int32_t width = shape.dim(2);
    const int32_t inch = shape.dim(3);
    const auto to_dim = ::neurun::backend::srcn::kernel::convertCoordinates(
        {outch, height, width, inch}, ::neurun::backend::srcn::kernel::FilterLayout::OHWI,
        ::neurun::backend::srcn::kernel::FilterLayout::HWOI);
    for (auto i = 0; i < outch; ++i)
    {
      for (auto j = 0; j < height; ++j)
      {
        for (auto k = 0; k < width; ++k)
        {
          for (auto l = 0; l < inch; ++l)
          {
            const auto coords = ::neurun::backend::srcn::kernel::convertCoordinates(
                {i, j, k, l}, ::neurun::backend::srcn::kernel::FilterLayout::OHWI,
                ::neurun::backend::srcn::kernel::FilterLayout::HWOI);
            const size_t offset = coords[0] * to_dim[1] * to_dim[2] * to_dim[3] +
                                  coords[1] * to_dim[2] * to_dim[3] + coords[2] * to_dim[3] +
                                  coords[3];
            T *into = reinterpret_cast<T *>(tensor.buffer() + offset * sizeof(T));
            T value = *(base + i * height * width * inch + j * width * inch + k * inch + l);
            *into = value;
          }
        }
      }
    }
  });
}
}

namespace neurun
{
namespace backend
{
namespace srcn
{

ConstantInitializer::ConstantInitializer(const model::Operands &operands,
                                         const std::shared_ptr<TensorBuilder> &tensor_builder)
    : _operands{operands}, _tensor_builder{tensor_builder}
{
  // DO NOTHING
}

void ConstantInitializer::run()
{
  for (const auto &it : _init_map)
  {
    const auto &ind = it.first;
    const auto &fn = it.second;

    const auto &model_obj = _operands.at(ind);
    auto tensor_obj = _tensor_builder->wrapTensor(ind);
    fn(model_obj, *tensor_obj);
  }

  _init_map.clear();
}

void ConstantInitializer::registerPermuteKernelInitializer(const model::OperandIndex &index,
                                                           const model::Operand &obj)
{
  // For only CONSTANTS
  if (!obj.isConstant())
    return;

  VERBOSE(FillOperandData) << "[SRCN] Fill data for operand " << index.value() << std::endl;

  const auto type = obj.typeInfo().type();
  using neurun::model::DataType;
  using namespace std::placeholders;

  switch (type)
  {
    case DataType::FLOAT32:
      _init_map[index] = std::bind(PermuteKernel<float>, _1, _2, _current_subg_layout);
      break;
    case DataType::INT32:
      _init_map[index] = std::bind(PermuteKernel<int32_t>, _1, _2, _current_subg_layout);
      break;
    case DataType::UINT32:
      _init_map[index] = std::bind(PermuteKernel<uint32_t>, _1, _2, _current_subg_layout);
      break;
    case DataType::BOOL8:
    case DataType::QUANT8_ASYMM:
      _init_map[index] = std::bind(PermuteKernel<uint8_t>, _1, _2, _current_subg_layout);
      break;
    default:
      throw std::runtime_error("Not supported, yet");
      break;
  }
}

void ConstantInitializer::visit(const model::operation::TransposeConvNode &node)
{
  const auto &kernel_index = node.getInputs().at(model::operation::TransposeConvNode::KERNEL);
  const auto &kernel_obj = _operands.at(kernel_index);
  registerPermuteKernelInitializer(kernel_index, kernel_obj);
}

} // namespace srcn
} // namespace backend
} // namespace neurun