summaryrefslogtreecommitdiff
path: root/runtime/neurun/backend/srcn/kernel/AddLayer.cc
blob: b53dfe89d089ad214ae237cee0063b0687d30ea4 (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
/*
 * 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 "AddLayer.h"

#include "OperationUtils.h"
#include "ncnn/layer/binaryop.h"

#include "cpp14/memory.h"

namespace
{
std::unique_ptr<nnfw::ncnn::Mat>
convertMatIgnoreLayout(neurun::backend::srcn::kernel::TensorDescriptor &desc, void *data)
{
  if (desc.dimensions.size() == 1)
  {
    return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[0], data);
  }
  else if (desc.dimensions.size() == 2)
  {
    return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[1], desc.dimensions[0], data);
  }
  else if (desc.dimensions.size() == 3)
  {
    return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[2], desc.dimensions[1],
                                                     desc.dimensions[0], data);
  }
  else // rank == 4 and N == 1
  {
    return nnfw::cpp14::make_unique<nnfw::ncnn::Mat>(desc.dimensions[3], desc.dimensions[2],
                                                     desc.dimensions[1], data);
  }
}
} // namespace

namespace neurun
{
namespace backend
{
namespace srcn
{
namespace kernel
{

void AddLayer::addFloat32()
{
  assert(_activation == ir::Activation::NONE);

  // ncnn kernel support
  // 1. rank < 4
  // 2. broadcasting
  //   2-1 lhs, rhs have same rank, or
  //   2-2 model layout and backend layout is same
  // For safety, block all broadcasting (enable when ready)

  assert(_lhsDescr.dimensions.size() < 4 ||
         (_lhsDescr.dimensions.size() == 4 && _lhsDescr.dimensions[0] == 1));
  assert(_rhsDescr.dimensions.size() < 4 ||
         (_rhsDescr.dimensions.size() == 4 && _rhsDescr.dimensions[0] == 1));
  assert((_lhsDescr.dimensions.size() == _rhsDescr.dimensions.size()));

  nnfw::ncnn::BinaryOpParam param;
  param.op_type = nnfw::ncnn::BinaryOp::Operation_ADD;

  auto lhs_mat = convertMatIgnoreLayout(_lhsDescr, _lhsData.v);
  auto rhs_mat = convertMatIgnoreLayout(_rhsDescr, _rhsData.v);
  auto out_mat = convertMatIgnoreLayout(_outputDescr, _outputData.v);

  ::nnfw::ncnn::ncnn_binary_op(param, *lhs_mat.get(), *rhs_mat.get(), *out_mat.get());
}

void AddLayer::addQuant8()
{
  // quant8 add is not implemented yet
  throw std::runtime_error{"NYI"};
}

void AddLayer::configure(uint8_t *lhsData, const TensorDescriptor &lhsDescr, uint8_t *rhsData,
                         const TensorDescriptor &rhsDescr, const ir::Activation activation,
                         uint8_t *outputData, const TensorDescriptor &outputDescr,
                         const ir::Layout backendLayout)
{
  _lhsData.u8 = lhsData;
  _lhsDescr = lhsDescr;
  _rhsData.u8 = rhsData;
  _rhsDescr = rhsDescr;
  _inputType = lhsDescr.type;
  _activation = activation;
  _outputData.u8 = outputData;
  _outputDescr = outputDescr;
  _backendLayout = backendLayout;
}

void AddLayer::run()
{
  if (_inputType == OperandType::FLOAT32)
  {
    addFloat32();
  }
  else if (_inputType == OperandType::QUANT8_ASYMM)
  {
    addQuant8();
  }
}

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