summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/src/graph/pass/PermutationEliminationPass.cc
blob: 1fc9b69cf82d8ac192fd9ea48de2d6808a50f469 (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
/*
 * 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 "PermutationEliminationPass.h"

#include "model/Operand.h"
#include "graph/operand/LowerInfo.h"
#include "graph/Graph.h"
#include "backend/IConfig.h"
#include "util/logging.h"
#include "compiler/BackendResolver.h"

namespace neurun
{
namespace graph
{
namespace pass
{
void PermutationEliminationPass::callback(const model::OperandIndex &inp_index,
                                          model::Operand &object)
{
  if (_graph.getInputs().contains(inp_index))
  {
    eliminateInput(inp_index, object);
  }
  else if (_graph.getOutputs().contains(inp_index))
  {
    eliminateOutput(inp_index, object);
  }
}

void PermutationEliminationPass::eliminateInput(const model::OperandIndex &inp_index,
                                                model::Operand &object)
{
  auto &model_inputs = _graph.getInputs();

  // get uses of the model's given input
  auto uses = object.getUses();

  // input must be used just by permutation
  if (uses.size() != 1)
  {
    return;
  }

  for (auto input_use : uses.list())
  {
    auto &perm_operation = _graph.operations().at(input_use);
    auto perm_inputs = perm_operation.getInputs();

    auto perm_outputs = perm_operation.getOutputs();

    if (!isPermuteLayerToEliminate(perm_inputs, perm_outputs, true))
    {
      return;
    }

    assert(perm_inputs.at(0) == inp_index);

    VERBOSE(PermutationEliminationPass::EliminateInput) << "remove NHWC_TO_NCHW permutation\n";

    // set model's new input, which was output of permutation
    model_inputs.replace(inp_index, perm_outputs.at(0));

    // remove model's input, which is also input of permutation
    _graph.removeOperand(inp_index);

    // remove permutation operation
    assert(_graph.subgraphs().containsOperation(input_use));
    auto subg_idx = _graph.subgraphs().getOperation(input_use);
    _graph.subgraphs().remove(subg_idx);
    _graph.operations().remove(input_use);

    VERBOSE(PermutationEliminationPass::EliminateInput)
        << inp_index.value() << " is model's input and is removed. New input is "
        << perm_outputs.at(0).value() << "\n"
        << input_use.value() << " is removed permutation operation\n";
  }
}

void PermutationEliminationPass::eliminateOutput(const model::OperandIndex &out_index,
                                                 model::Operand &object)
{
  auto &model_outputs = _graph.getOutputs();

  // get defs of the model's given output
  auto defs = object.getDef();

  // output must use just permutation
  if (defs.size() != 1)
  {
    return;
  }

  for (auto output_def : defs.list())
  {
    auto &perm_operation = _graph.operations().at(output_def);
    auto perm_outputs = perm_operation.getOutputs();

    auto perm_inputs = perm_operation.getInputs();
    if (!isPermuteLayerToEliminate(perm_inputs, perm_outputs, false))
    {
      return;
    }

    assert(perm_outputs.at(0) == out_index);

    VERBOSE(PermutationEliminationPass::EliminateOutput) << "remove NCHW_TO_NHWC permutation\n";

    // Update operations' output that is used by permute operand
    for (auto perm_input_index : perm_inputs)
    {
      auto &perm_input_operand = _graph.operands().at(perm_input_index);
      perm_input_operand.removeUse(output_def);
    }

    // set model's new output, which was input of permutation
    model_outputs.replace(out_index, perm_inputs.at(0));

    // remove model's output, which is also output of permutation
    _graph.removeOperand(out_index);

    // remove permutation operation
    assert(_graph.subgraphs().containsOperation(output_def));
    auto subg_idx = _graph.subgraphs().getOperation(output_def);
    _graph.subgraphs().remove(subg_idx);
    _graph.operations().remove(output_def);

    VERBOSE(PermutationEliminationPass::EliminateOutput)
        << out_index.value() << " is model's output and is removed. New output is "
        << perm_inputs.at(0).value() << "\n"
        << output_def.value() << " is removed permutation operation\n";
  }
}

bool PermutationEliminationPass::isPermuteLayerToEliminate(
    const model::OperandIndexSequence &inp_indexes, const model::OperandIndexSequence &out_indexes,
    bool is_for_model_input)
{
  auto input_def_factors = _graph.getLowerInfo(inp_indexes.at(0))->def_factors();
  auto output_def_factors = _graph.getLowerInfo(out_indexes.at(0))->def_factors();

  auto input_layout = input_def_factors.getOnlyElement().layout();
  auto output_layout = output_def_factors.getOnlyElement().layout();

  if (input_def_factors.size() != 1 || output_def_factors.size() != 1)
  {
    return false;
  }

  // all operands' factor must be the same
  for (auto index : inp_indexes)
  {
    auto op_factor_set = _graph.getLowerInfo(index)->def_factors();
    if (op_factor_set.size() != 1 ||
        input_layout != _graph.getLowerInfo(index)->def_factors().getOnlyElement().layout())
    {
      return false;
    }
  }
  // all operands' factor must be the same
  for (auto index : out_indexes)
  {
    auto op_factor_set = _graph.getLowerInfo(index)->def_factors();
    if (op_factor_set.size() != 1 ||
        output_layout != _graph.getLowerInfo(index)->def_factors().getOnlyElement().layout())
    {
      return false;
    }
  }

  if (is_for_model_input)
  {
    // check if this is NHWC_TO_NCHW permutation: must have single input, which is model's input
    return (inp_indexes.size() == 1 && input_layout == model::Layout::NHWC &&
            output_layout == model::Layout::NCHW);
  }

  // check if this is NCHW_TO_NHWC permutation: must have single output, which is model's output
  return (out_indexes.size() == 1 && input_layout == model::Layout::NCHW &&
          output_layout == model::Layout::NHWC);
}

} // namespace pass
} // namespace graph
} // namespace neurun