summaryrefslogtreecommitdiff
path: root/runtimes/neurun/backend/cpu/KernelGenerator.cc
blob: 61de7549367842e33bbb6ec16509acbb12d28eed (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * 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 "KernelGenerator.h"

#include <stdexcept>

#include "cpp14/memory.h"
#include "util/Padding.h"
#include "kernel/OperationUtils.h"
#include "kernel/ConvolutionLayer.h"
#include "kernel/AvgPoolLayer.h"
#include "kernel/MaxPoolLayer.h"
#include "kernel/ConcatLayer.h"
#include "kernel/FullyConnectedLayer.h"
#include "kernel/ReshapeLayer.h"
#include "kernel/SoftMaxLayer.h"
#include "kernel/PermuteLayer.h"
#include "kernel/DepthwiseConvolutionLayer.h"
#include "kernel/AddLayer.h"

#include <backend/Backend.h>
#include <backend/IConfig.h>

#include "util/logging.h"

#include "util/Utils.h"

namespace neurun
{
namespace backend
{
namespace cpu
{

KernelGenerator::KernelGenerator(const neurun::model::Operands &operand_ctx,
                                 const std::shared_ptr<TensorBuilder> &tensor_builder,
                                 const std::shared_ptr<custom::KernelRegistry> &kernel_registry)
    : _ctx(operand_ctx), _tensor_builder(tensor_builder), _kernel_registry(kernel_registry),
      _current_subg_layout(model::Layout::UNKNOWN)
{
  // DO NOTHING
}

void KernelGenerator::visit(const model::Subgraph &subgraph)
{
  _current_subg_layout = subgraph.getLayout();
  for (const auto &e : subgraph.operations())
  {
    const auto &node = *(e.node);
    _tensor_builder->preVisit(node);
    node.accept(*this);
    _tensor_builder->postVisit(node);
  }
}

void KernelGenerator::visit(const model::operation::Conv2DNode &node)
{
  using model::operation::Conv2DNode;

  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(Conv2DNode::Input::INPUT)};
  const auto ker_index{node.getInputs().at(Conv2DNode::Input::KERNEL)};
  const auto bias_index{node.getInputs().at(Conv2DNode::Input::BIAS)};

  const auto stride = node.param().stride;
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
  const auto &ker_shape = _ctx.at(ker_index).shape();
  const auto ker_height = ker_shape.dim(1);
  const auto ker_width = ker_shape.dim(2);
  const auto padding = neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape,
                                                      stride, ker_width, ker_height);
  const auto activation = node.param().activation;

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ifm_index), _current_subg_layout);
  const auto ker_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ker_index), model::Layout::UNKNOWN);
  const auto bias_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(bias_index), model::Layout::UNKNOWN);

  auto ofm_alloc = _tensor_builder->at(ofm_index);
  auto ifm_alloc = _tensor_builder->at(ifm_index);
  auto ker_alloc = _tensor_builder->at(ker_index);
  auto bias_alloc = _tensor_builder->at(bias_index);

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::ConvolutionLayer>();

  fn->configure(ifm_alloc->buffer(), ifm_backend_shape, ker_alloc->buffer(), ker_backend_shape,
                bias_alloc->buffer(), bias_backend_shape, padding.left, padding.right, padding.top,
                padding.bottom, stride.horizontal, stride.vertical, activation, ofm_alloc->buffer(),
                ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::DepthwiseConv2DNode &node)
{
  using model::operation::DepthwiseConv2DNode;

  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(DepthwiseConv2DNode::Input::INPUT)};
  const auto ker_index{node.getInputs().at(DepthwiseConv2DNode::Input::KERNEL)};
  const auto bias_index{node.getInputs().at(DepthwiseConv2DNode::Input::BIAS)};

  const auto stride = node.param().stride;
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  // Kernel format is [1, kernel_height, kernel_width, depth_out].
  const auto &ker_shape = _ctx.at(ker_index).shape();
  const auto ker_height = ker_shape.dim(1);
  const auto ker_width = ker_shape.dim(2);
  const auto padding = neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape,
                                                      stride, ker_width, ker_height);

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ifm_index), _current_subg_layout);
  const auto ker_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ker_index), model::Layout::UNKNOWN);
  const auto bias_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(bias_index), model::Layout::UNKNOWN);

  const auto multiplier = node.param().multiplier;
  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index);
  auto ifm_alloc = _tensor_builder->at(ifm_index);
  auto ker_alloc = _tensor_builder->at(ker_index);
  auto bias_alloc = _tensor_builder->at(bias_index);

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::DepthwiseConvolutionLayer>();

  fn->configure(ifm_alloc->buffer(), ifm_backend_shape, ker_alloc->buffer(), ker_backend_shape,
                bias_alloc->buffer(), bias_backend_shape, padding.left, padding.right, padding.top,
                padding.bottom, stride.horizontal, stride.vertical, multiplier, activation,
                ofm_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::MaxPool2DNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::MaxPool2DNode::Input::INPUT)};

  const auto kh = node.param().kh;
  const auto kw = node.param().kw;

  const auto stride = node.param().stride;
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto padding =
      neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, kw, kh);
  const auto activation = node.param().activation;

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ifm_index), _current_subg_layout);

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::MaxPoolLayer>();

  fn->configure(ifm_alloc->buffer(), ifm_backend_shape, padding.left, padding.right, padding.top,
                padding.bottom, stride.horizontal, stride.vertical, kw, kh, activation,
                ofm_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::AvgPool2DNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto ifm_index{node.getInputs().at(model::operation::AvgPool2DNode::Input::INPUT)};

  const auto kh = node.param().kh;
  const auto kw = node.param().kw;
  const auto stride = node.param().stride;
  const auto ifm_shape = _ctx.at(ifm_index).shape().asFeature(_current_subg_layout);
  const auto ofm_shape = _ctx.at(ofm_index).shape().asFeature(_current_subg_layout);
  const auto padding =
      neurun::util::calculatePadding(node.param().padding, ifm_shape, ofm_shape, stride, kw, kh);
  const auto activation = node.param().activation;

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ifm_index), _current_subg_layout);

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto ifm_alloc = _tensor_builder->at(ifm_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::AvgPoolLayer>();

  fn->configure(ifm_alloc->buffer(), ifm_backend_shape, padding.left, padding.right, padding.top,
                padding.bottom, stride.horizontal, stride.vertical, kw, kh, activation,
                ofm_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::ConcatNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};

  const auto rank = _ctx.at(ofm_index).shape().rank();
  const auto axis =
      ::neurun::backend::cpu::kernel::getAxis(rank, node.param().axis, _current_subg_layout);

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  std::vector<::neurun::backend::cpu::kernel::Shape> ifm_backend_shapes;
  for (auto &in_idx : node.getInputs())
    ifm_backend_shapes.emplace_back(
        ::neurun::backend::cpu::kernel::getShape(_ctx.at(in_idx), _current_subg_layout));

  auto output_alloc = _tensor_builder->at(ofm_index).get();

  std::vector<const uint8_t *> input_buffers;
  for (auto &ifm_idx : node.getInputs())
    input_buffers.emplace_back(_tensor_builder->at(ifm_idx).get()->buffer());

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::ConcatLayer>();

  fn->configure(input_buffers, ifm_backend_shapes, axis, output_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::FullyConnectedNode &node)
{
  using model::operation::FullyConnectedNode;

  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(FullyConnectedNode::Input::INPUT)};
  const auto weight_index{node.getInputs().at(FullyConnectedNode::Input::WEIGHT)};
  const auto bias_index{node.getInputs().at(FullyConnectedNode::Input::BIAS)};

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(output_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(input_index), _current_subg_layout);
  const auto weight_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(weight_index), model::Layout::UNKNOWN);
  const auto bias_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(bias_index), model::Layout::UNKNOWN);

  const auto activation = node.param().activation;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();
  auto weight_alloc = _tensor_builder->at(weight_index).get();
  auto bias_alloc = _tensor_builder->at(bias_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::FullyConnectedLayer>();

  fn->configure(input_alloc->buffer(), ifm_backend_shape, weight_alloc->buffer(),
                weight_backend_shape, bias_alloc->buffer(), bias_backend_shape, activation,
                output_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::MulNode &) { throw std::runtime_error("NYI"); }

void KernelGenerator::visit(const model::operation::ReshapeNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::ReshapeNode::Input::INPUT)};

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(output_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(input_index), _current_subg_layout);

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::ReshapeLayer>();

  fn->configure(input_alloc->buffer(), ifm_backend_shape, output_alloc->buffer(),
                ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::SoftmaxNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(model::operation::SoftmaxNode::Input::INPUT)};

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(output_index), _current_subg_layout);
  const auto ifm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(input_index), _current_subg_layout);

  const auto beta = node.param().beta;

  auto output_alloc = _tensor_builder->at(output_index).get();
  auto input_alloc = _tensor_builder->at(input_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::SoftMaxLayer>();

  fn->configure(input_alloc->buffer(), ifm_backend_shape, beta, output_alloc->buffer(),
                ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::AddNode &node)
{
  const auto ofm_index{node.getOutputs().at(0)};
  const auto lhs_index{node.getInputs().at(model::operation::AddNode::Input::LHS)};
  const auto rhs_index{node.getInputs().at(model::operation::AddNode::Input::RHS)};

  const auto ofm_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
  const auto lhs_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(lhs_index), _current_subg_layout);
  const auto rhs_backend_shape =
      ::neurun::backend::cpu::kernel::getShape(_ctx.at(rhs_index), _current_subg_layout);

  const auto activation = node.param().activation;

  auto ofm_alloc = _tensor_builder->at(ofm_index).get();
  auto lhs_alloc = _tensor_builder->at(lhs_index).get();
  auto rhs_alloc = _tensor_builder->at(rhs_index).get();

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::AddLayer>();

  fn->configure(lhs_alloc->buffer(), lhs_backend_shape, rhs_alloc->buffer(), rhs_backend_shape,
                activation, ofm_alloc->buffer(), ofm_backend_shape);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::PermuteNode &node)
{
  const auto output_index{node.getOutputs().at(0)};
  const auto input_index{node.getInputs().at(0)};

  const auto &shape = _ctx.at(output_index).shape();
  const auto input_backend_ctx = node.param().input_backend_ctx;
  const auto output_backend_ctx = node.param().output_backend_ctx;
  const auto data_type = node.getDataType();

  output_backend_ctx->tensor_builder->preVisit(node);

  auto output_object = output_backend_ctx->tensor_builder->wrapTensor(output_index);
  auto input_object = input_backend_ctx->tensor_builder->wrapTensor(input_index);

  auto fn = nnfw::cpp14::make_unique<::neurun::backend::cpu::kernel::PermuteLayer>();

  // TODO Support NCHW frontend
  auto out_shape = shape;
  if (shape.rank() == 4 && output_object->ptr()->layout() == model::Layout::NCHW)
  {
    out_shape.dim(1) = shape.dim(3);
    out_shape.dim(2) = shape.dim(1);
    out_shape.dim(3) = shape.dim(2);
  }

  const auto permute_type = node.getPermuteType();
  // Check Permutation Type
  const auto inferPermuteType = [&]() {
    if (input_object->ptr()->layout() == model::Layout::NHWC &&
        output_object->ptr()->layout() == model::Layout::NCHW)
    {
      return model::operation::PermuteNode::Type::NHWC_TO_NCHW;
    }
    else if (input_object->ptr()->layout() == model::Layout::NCHW &&
             output_object->ptr()->layout() == model::Layout::NHWC)
    {
      return model::operation::PermuteNode::Type::NCHW_TO_NHWC;
    }
    else
    {
      return model::operation::PermuteNode::Type::COPY;
    }
  }();
  UNUSED_RELEASE(inferPermuteType);
  assert(permute_type == inferPermuteType);

  fn->configure(input_object, output_object, out_shape, permute_type, data_type);

  input_backend_ctx->tensor_builder->postVisit(node);

  _execution_builder->append(std::move(fn));
}

void KernelGenerator::visit(const model::operation::CustomNode &node)
{
  auto get_type_info = [this](const model::Operand &operand) -> custom::TypeInfo {
    auto backendShape = ::neurun::backend::cpu::kernel::getShape(operand, _current_subg_layout);

    custom::Shape shape(backendShape.dimensions.size());
    for (size_t d = 0; d < backendShape.dimensions.size(); ++d)
    {
      shape.dim(d) = backendShape.dimensions[d];
    }

    return {shape, backendShape.type};
  };

  auto fill_op_info = [&](const model::OperandIndexSequence &opSeq,
                          std::vector<custom::TypeInfo> &types, std::vector<void *> &allocs) {
    for (auto &idx : opSeq)
    {
      const auto &operand = _ctx.at(idx);
      // TODO make sure using `_current_subg_layout` is correct for custom operations
      types.emplace_back(get_type_info(operand));
      auto in_alloc = _tensor_builder->at(idx)->buffer();
      allocs.emplace_back(in_alloc);
    }
  };

  custom::Kernel::CustomKernelConfigParams params{};

  fill_op_info(node.getInputs(), params.input_types, params.input_allocations);
  fill_op_info(node.getOutputs(), params.output_types, params.output_allocations);

  params.userdata = node.userdata().data;
  params.userdata_size = node.userdata().size;

  auto fn = _kernel_registry->buildKernelForOp(node.id());

  fn->configure(std::move(params));

  _execution_builder->append(std::move(fn));
}

} // namespace cpu
} // namespace backend
} // namespace neurun