summaryrefslogtreecommitdiff
path: root/runtimes/neurun/src/compiler/Compiler.cc
blob: 92ec69afbaaf64b3d1036cddfd5afc571cfe40e6 (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) 2018 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 "Compiler.h"

#include "OperationValidator.h"
#include "SubTensorAnalyzer.h"
#include "PlanBuilder.h"
#include "ConstantInitializer.h"

#include "graph/dumper/Dumper.h"
#include "graph/operation/LowerInfo.h"
#include "dumper/dot/DotDumper.h"
#include "linear/Linear.h"

namespace neurun
{

namespace compiler
{

void Compiler::compile(void)
{
  auto &plan = this->plan();
  auto &graph = plan.model();
  const auto &operands = graph.operands();

  // Disable compile phase
  // When ready to use interpreter backend, remove this config and use backend setting
  const auto env_disable_compile = config::ConfigManager::instance().get<bool>("DISABLE_COMPILE");
  if (env_disable_compile)
  {
    plan.state(State::NOT_COMPILED);
    return;
  }

  /***************************************************
   * Backend independent analysis & optimization phase
   ***************************************************/

  /*************************************************************
   *  Backend independent analysis & optimization phase finished
   *************************************************************/

  // dump graph to .dot
  neurun::dumper::dot::DotDumper dot_dumper(graph);
  dot_dumper.dumpIfNeeded("before_lower");

  // Lower: decide backend
  graph.lower();
  plan.state(State::LOWERED);

  dot_dumper.dumpIfNeeded("after_lower");

  auto linear = graph.linearize();
  plan.state(State::LINEARIZED);

  // Dump ops
  linear->accept(neurun::graph::dumper::Dumper{});

  linear->accept(OperationValidator{operands});

  /*************************************************
   * Backend dependent analysis & optimization phase
   *************************************************/

  // SubTensorInfo should be generated after lower, before stage generation and finalize
  // because SubTensorAnalyzer assume that insert permutation is already finished
  //    lower: decide backend and insert permutation
  //    stage generation: prepare codegen to optimization
  //    finalize: generate tensor using subtensor info, then execute stage
  // Generated SubTensorInfo is in operand(Object)
  // for easy pass SubTensorInfo to plan builder and tensor builder
  linear->accept(SubTensorAnalyzer{graph.operands()});

  /**********************************************************
   * Backend dependent analysis & optimization phase finished
   **********************************************************/

  /***********************
   * Code generation phase
   ***********************/

  PlanBuilder plan_builder{plan};

  // Plan building
  linear->iterate([&](const linear::Element &element) {
    auto backend = element.lower_info->backend();

    // Generate Stage
    auto stage_gen = backend->stage_gen();
    plan_builder.addStage(stage_gen->generate(*element.node));
  });

  auto tensor_builders = linear->planTensors();

  // TODO Add optimization passes
  plan_builder.finalize(tensor_builders);

  ConstantInitializer{graph, plan}();

  /********************************
   * Code generation phase finished
   ********************************/

  plan.state(State::COMPILED);
}

} // namespace compiler

} // namespace neurun