summaryrefslogtreecommitdiff
path: root/runtime/onert/core/src/compiler/train/TrainingCompiler.cc
blob: 711af1651fd0e68d2b05c58629d0254671aa1019 (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
/*
 * Copyright (c) 2023 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 "TrainingCompiler.h"

#include "StaticDerivativeShapeInferer.h"
#include "TrainableOperationConverter.h"
#include "pass/LossInsertionPass.h"
#include "../CompilerHelpers.h"
#include "../ExecutorFactory.h"
#include "../pass/ConstantOutputPass.h"
#include "../pass/OddOutputPass.h"
#include "../pass/PassRunner.h"
#include "../pass/UnusedOperandEliminationPass.h"
#include "../ShapeValidator.h"
#include "../../dumper/dot/DotDumper.h"
#include "../../exec/train/TrainableExecutors.h"
#include "../../ir/OperationDumper.h"
#include "../../ir/verifier/Verifier.h"

#include <compiler/StaticShapeInferer.h>
#include <compiler/train/LoweredTrainableGraph.h>
#include <ir/train/TrainableGraph.h>
#include <exec/train/optimizer/SGD.h>

#include <misc/polymorphic_downcast.h>
#include <misc/string_helpers.h>

namespace onert
{
namespace compiler
{
namespace train
{

TrainingCompiler::TrainingCompiler(const std::shared_ptr<ir::NNPkg> &nnpkg,
                                   std::vector<std::unique_ptr<CompilerOptions>> &copts,
                                   const TrainingInfo &training_info)
  : _model{nnpkg->primary_model()}, _options{copts[0].get()}, _training_info{training_info}
{
  if (nnpkg->model_count() > 1)
    throw std::runtime_error("TrainingCompiler does not support multiple models yet");

  if (nnpkg->primary_model()->subgraphs_count() > 1)
    throw std::runtime_error("TrainingCompiler does not support multiple subgraphs yet");
}

std::shared_ptr<CompilerArtifact> TrainingCompiler::compile(void)
{
  /***************************************************
   * Prepare compilation phase
   ***************************************************/
  if (!_options)
    throw std::runtime_error{"Empty compile option"};

  // Mode check
  // TODO handle option for each model
  if (_options->he_profiling_mode)
  {
    if (!_options->he_scheduler)
      throw std::runtime_error("Heterogeneous scheduler must be enabled during profiling.");

    if (_options->executor != "Dataflow")
      throw std::runtime_error("Profiling mode works only with 'Dataflow' executor");
  }

  if (!_options->minmax_filepath.empty())
  {
    if (_options->executor != "Linear")
      throw std::runtime_error("Recording minmax works only with Linear executor");
  }

  _options->forceInternalOptions();
  _options->verboseOptions();

  auto custom_kernel_builder = _model->getKernelBuilder();

  _model->iterate([&](const ir::SubgraphIndex &, ir::IGraph &graph) {
    auto &subg = nnfw::misc::polymorphic_downcast<ir::Graph &>(graph);
    // Mandatory passes
    compiler::pass::PassRunner{}
      .append(std::make_unique<compiler::pass::ConstantOutputPass>(subg))
      .append(std::make_unique<compiler::pass::OddOutputPass>(subg))
      .run();

    // Optimizations
    compiler::pass::PassRunner{}
      .append(std::make_unique<compiler::pass::UnusedOperandEliminationPass>(subg))
      .run();
  });

  std::unordered_map<ir::SubgraphIndex, std::shared_ptr<ir::train::TrainableGraph>>
    trainable_subgraphs;

  if (_model->hasOnly<ir::Graph>())
  {
    // Create trainable subgraphs by copy and converting inference model
    _model->iterate([&](const ir::SubgraphIndex &subg_index, const ir::IGraph &graph) {
      const auto &subg = nnfw::misc::polymorphic_downcast<const ir::Graph &>(graph);
      // Create TrainableGraph by copying Graph
      auto trainable_subg = std::make_shared<ir::train::TrainableGraph>(subg);

      // Convert operations to trainable operations
      auto converter = TrainableOperationConverter{*trainable_subg, &_training_info};
      subg.operations().iterate(
        [&](const onert::ir::OperationIndex &op_index, const onert::ir::IOperation &op) {
          auto trainable_op = converter(op);
          auto gen_index = trainable_subg->replaceOperation(op_index, std::move(trainable_op));
          UNUSED_RELEASE(gen_index);
          assert(gen_index == op_index);
        });

      trainable_subgraphs[subg_index] = std::move(trainable_subg);
    });
  }
  else
  {
    // TODO Support models that have TrainableGraphs
    throw std::runtime_error("TrainingCompiler: Invalid model");
  }

  // operation
  _model.reset();

  // Apply pass for trainable subgraphs
  for (auto &&pair : trainable_subgraphs)
  {
    auto trainable_subg = pair.second;
    auto subg_index = pair.first;

    compiler::pass::PassRunner{}
      .append(std::make_unique<train::pass::LossInsertionPass>(*trainable_subg, &_training_info,
                                                               subg_index))
      .run();
  }

  // Change input shape according to batch_size
  for (auto &&pair : trainable_subgraphs)
  {
    auto trainable_subg = pair.second;

    for (const auto &ind : trainable_subg->getInputs())
    {
      auto &input = trainable_subg->operands().at(ind);
      auto new_shape = input.info().shape();
      // TODO Consider batch size index
      if (new_shape.dim(0) != 1)
        throw std::runtime_error("the first dim is not 1. It is not supported yet.");
      new_shape.dim(0) = _training_info.batchSize();
      input.info().shape(new_shape);
    }
  }

  /***************************************************
   * Backend independent analysis & optimization phase
   ***************************************************/
  // TODO Handle dump level for each model
  auto dump_level = static_cast<dumper::dot::DotDumper::Level>(_options->graph_dump_level);
  onert::dumper::dot::DotDumper dot_dumper(dump_level);

  // Tracing context
  auto tracing_ctx = std::make_unique<util::TracingCtx>();

  // Lower: Assign backend
  std::unordered_map<ir::SubgraphIndex, std::unique_ptr<compiler::train::LoweredTrainableGraph>>
    lowered_subgs;
  {
    for (auto &&pair : trainable_subgraphs)
    {
      auto &subg_index = pair.first;
      auto trainable_subg = pair.second;

      // Lower: Assign backend
      lowered_subgs[subg_index] =
        std::make_unique<compiler::train::LoweredTrainableGraph>(*trainable_subg, *_options);
      // Set tracing_ctx for copied graph
      if (tracing_ctx != nullptr)
        tracing_ctx->setSubgraphIndex(&(lowered_subgs[subg_index]->graph()), subg_index.value());
    }
  }

  for (const auto &pair : lowered_subgs)
  {
    const auto &subg_index = pair.first;
    const auto &lowered_subg = pair.second;
    dot_dumper.dump(*lowered_subg, nnfw::misc::str("after_lower_subg-", subg_index.value()));
  }

  // Set derivatives as default tensor info
  for (const auto &pair : lowered_subgs)
  {
    auto lowered_subg = pair.second.get();
    auto &tgraph = lowered_subg->trainable_graph();
    tgraph.operands().iterate([&](const ir::OperandIndex &index, const ir::Operand &obj) {
      if (!obj.isConstant())
      {
        auto deriv = std::make_unique<ir::Operand>(obj);
        const auto gen_index = tgraph.addDerivative(index, std::move(deriv));
        assert(gen_index == index);
        UNUSED_RELEASE(gen_index);
      }
    });
  }

  // Shape inference.
  {
    // Run the StaticShapeInfer of primary subg. All child StaticShapeInferers are called
    // recursively
    std::unordered_map<ir::SubgraphIndex, std::unique_ptr<StaticShapeInferer>> inferers =
      createStaticShapeInferers(lowered_subgs);

    const auto primary_subg_idx = ir::SubgraphIndex{0};
    inferers.at(primary_subg_idx)->infer();

    for (const auto &pair_inferer : inferers)
    {
      const auto inferer = pair_inferer.second.get();
      inferer->dump();
    }

    // NOTE StaticDerivativeShapeInferer is allocated for each subgraph,
    //      so it does not support models that have controlflow operations yet.
    for (auto &&pair : lowered_subgs)
    {
      auto &lowered_subg = pair.second;
      auto inferer = std::make_unique<StaticDerivativeShapeInferer>(lowered_subg.get());
      inferer->infer();
      inferer->dump();
    }
  }

  // Shape validation
  for (const auto &pair : lowered_subgs)
  {
    auto &lowered_subg = pair.second;
    compiler::ShapeValidator{lowered_subg->graph()}();
  }

  // TODO Validate shapes of derivative tensors

  // Create optimizer
  // TODO Set properties of optimizer
  std::shared_ptr<exec::train::optimizer::Optimizer> optimizer;
  const auto &optim_info = _training_info.optimizerInfo();
  if (optim_info.optim_code == exec::train::optimizer::OptimizerCode::SGD)
    optimizer = std::make_shared<exec::train::optimizer::SGD>(optim_info.learning_rate);
  else
    throw std::runtime_error("Invalid optimizer type, " +
                             exec::train::optimizer::toString(optim_info.optim_code));

  /*************************************************************
   *  Backend independent analysis & optimization phase finished
   *************************************************************/
  auto executors = std::make_shared<exec::train::TrainableExecutors>();
  for (auto &&pair : lowered_subgs)
  {
    auto const model_index = ir::ModelIndex{0};
    auto const subg_index = pair.first;
    auto &lowered_subg = pair.second;
    auto const indexed_ranks = lowered_subg->indexed_ranks();

    ir::OperationDumper dumper("Executor generation of Subgraph " +
                               std::to_string(subg_index.value()));
    lowered_subg->graph().operations().iterate(
      [&](const ir::OperationIndex &, const ir::IOperation &op) { op.accept(dumper); });

    ExecutorFactoryArgs args;
    args.tracing_ctx = tracing_ctx.get();
    args.options = _options;
    args.model_index = model_index;
    args.custom_kernel_builder = custom_kernel_builder;
    auto executor = std::unique_ptr<exec::IExecutor>{
      ExecutorFactory::get().create(std::move(lowered_subg), executors, args, optimizer)};
    executor->setIndexedRanks(indexed_ranks);
    executors->emplace(model_index, subg_index, std::move(executor));
  }

  /********************************
   * Code generation phase finished
   ********************************/
  return std::make_shared<CompilerArtifact>(executors, std::move(tracing_ctx));
}

} // namespace train
} // namespace compiler
} // namespace onert