summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/src/exec/ExecutorBase.h
blob: c283e7f61cdda93b72a6a38fe3f129f4bff14bca (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
/*
 * 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.
 */

#ifndef __NEURUN_EXEC_EXECUTOR_BASE_H__
#define __NEURUN_EXEC_EXECUTOR_BASE_H__

#include <mutex>

#include "Source.h"
#include "exec/ExecutionObservers.h"
#include "Sink.h"
#include "exec/IExecutor.h"
#include "model/Model.h"
#include "graph/LowerInfoMap.h"
#include "backend/IConfig.h"
#include "model/OperandInfo.h"
#include "backend/Backend.h"
#include "compiler/OperandContext.h"
#include "model/Subgraphs.h"
#include "model/Subgraph.h"
#include "backend/ExecTime.h"
#include "exec/IFunction.h"
#include "backend/ITensorManager.h"
#include <list>

namespace neurun
{
namespace exec
{

class ExecutorBase : public IExecutor
{
public:
  ExecutorBase(const std::shared_ptr<const model::Model> &model,
               std::unique_ptr<model::Subgraphs> subgraphs,
               const std::shared_ptr<compiler::OperandContext> &operand_context,
               std::unique_ptr<graph::LowerInfoMap> lower_info,
               std::unique_ptr<backend::TensorManagerSet> tensor_mgrs);

  virtual ~ExecutorBase() = default;

  const model::Model &model() override { return *_model; }

  void execute(const IODescription &desc) final;

  // Used only in Dataflow and Parallel Executors
  void setIndexedRanks(std::shared_ptr<model::OperationIndexMap<int64_t>> ranks) final
  {
    _indexed_ranks = std::move(ranks);
  };

  virtual void executeImpl(void) = 0;

private:
  std::unique_ptr<ISource> source(const model::IOIndex &index, const model::TypeInfo &type,
                                  const void *buffer, size_t length);
  std::unique_ptr<ISink> sink(const model::IOIndex &index, const model::TypeInfo &type,
                              void *buffer, size_t length);

  template <typename T>
  std::unique_ptr<ISource> source(const model::IOIndex &index, const void *buffer, size_t length)
  {
    const auto operand_index = _model->inputs.at(index);
    const auto &operand = _model->operands.at(operand_index);

    const auto tensor = _operand_context->at(operand_index)->ptr();
    const auto output_layout = tensor->layout();
    // TODO Set input_layout as frontend model's input layout
    auto input_layout = model::Layout::NHWC;
    if ((input_layout == model::Layout::NHWC) && (output_layout == model::Layout::NCHW))
    {
      return nnfw::cpp14::make_unique<PermutateSource<T>>(buffer, length, operand.shape());
    }
    // TODO Supports NCHW -> NHWC

    return nnfw::cpp14::make_unique<CopySource<T>>(buffer, length, operand.shape());
  }

  template <typename T>
  std::unique_ptr<ISink> sink(const model::IOIndex &index, void *buffer, size_t length)
  {
    const auto operand_index = _model->outputs.at(index);
    const auto &operand = _model->operands.at(operand_index);
    const auto tensor = _operand_context->at(operand_index)->ptr();
    const auto input_layout = tensor->layout();
    // TODO Set output_layout as frontend model's output layout
    auto output_layout = model::Layout::NHWC;
    if ((input_layout == model::Layout::NCHW) && (output_layout == model::Layout::NHWC))
    {
      return nnfw::cpp14::make_unique<PermutateSink<T>>(buffer, length, operand.shape());
    }
    // TODO Supports NHWC -> NCHW

    return nnfw::cpp14::make_unique<CopySink<T>>(buffer, length, operand.shape());
  }

protected:
  std::list<std::unique_ptr<IExecutionObserver>> _observers;
  std::shared_ptr<model::OperationIndexMap<int64_t>> _indexed_ranks;
  std::shared_ptr<const model::Model> _model;
  std::unique_ptr<model::Subgraphs> _subgraphs;
  std::shared_ptr<compiler::OperandContext> _operand_context;
  std::unique_ptr<graph::LowerInfoMap> _lower_info;
  std::unique_ptr<backend::TensorManagerSet> _tensor_mgrs;
  std::mutex _mutex;
};

} // namespace exec
} // namespace neurun

#endif // __NEURUN_EXEC_EXECUTOR_BASE_H__