summaryrefslogtreecommitdiff
path: root/runtime/onert/core/src/compiler/train/StaticDerivativeShapeInferer.cc
blob: d2153296f39bac1b64290c40354506c903e382f2 (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
/*
 * 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 "StaticDerivativeShapeInferer.h"
#include "util/ShapeInference.h"
#include "util/logging.h"

#include <misc/polymorphic_downcast.h>

#include <sstream>
#include <stdexcept>

namespace onert
{
namespace compiler
{
namespace train
{

void StaticDerivativeShapeInferer::infer()
{
  // It is not determined to iterate in reverse order.
  auto sorted_ops = _lowered_subg->graph().topolSortOperations();
  for (auto it = sorted_ops.rbegin(); it != sorted_ops.rend(); ++it)
  {
    const auto op_idx = *it;
    const auto &op = _lowered_subg->trainable_graph().operation(op_idx);
    if (checkDynamicInput(op))
    {
      std::stringstream msg;
      msg << "StaticDerivativeShapeInferer does not support dynamic shape yet, ";
      msg << op.name() << "(op index: " << op_idx << ") has dynamic shape.";
      throw std::runtime_error(msg.str());
    }

    checkOutput(op);

    op.accept(*this);
  }
}

void StaticDerivativeShapeInferer::dump()
{
  // TODO dump
}

bool StaticDerivativeShapeInferer::checkDynamicInput(const ir::IOperation &op)
{
  const auto &operands = _lowered_subg->graph().operands();
  for (auto input_idx : op.getInputs() | ir::Remove::UNDEFINED | ir::Remove::DUPLICATED)
  {
    if (operands.at(input_idx).info().isDynamic())
    {
      return true;
    }
  }

  return false;
}

void StaticDerivativeShapeInferer::checkOutput(const ir::IOperation &op)
{
  const auto &derivatives = _lowered_subg->trainable_graph().derivatives();
  for (auto output_idx : op.getOutputs() | ir::Remove::UNDEFINED | ir::Remove::DUPLICATED)
  {
    if (!derivatives.exist(output_idx))
    {
      std::stringstream msg;
      msg << "StaticDerivativeShapeInferer : Invalid output, ";
      msg << op.name() << "'s derivative output(index: " << output_idx << ") does not exist.";
      throw std::runtime_error(msg.str());
    }
  }
}

void StaticDerivativeShapeInferer::setShape(const ir::OperandIndex &index, const ir::Shape &shape)
{
  auto &tgraph = _lowered_subg->trainable_graph();

  if (tgraph.derivatives().exist(index))
    tgraph.changeDerivativeShape(index, shape);
  else
  {
    // NOTE This code assumes the types are always the same, but I'm not sure.
    const auto &type = tgraph.operands().at(index).typeInfo();
    const auto new_index = tgraph.addDerivative(index, std::make_unique<ir::Operand>(shape, type));
    assert(new_index == index);
    UNUSED_RELEASE(new_index);
  }
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Conv2D &)
{
  // NYI
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::ElementwiseActivation &)
{
  // NYI
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Loss &)
{
  // NYI
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Permute &op)
{
  const auto &derivatives = _lowered_subg->trainable_graph().derivatives();

  const auto &output_idx = op.getOutputs().at(0);
  const auto &output = derivatives.at(output_idx);

  // re-sizing input derivative shape
  const auto &input_idx = op.getInputs().at(0);
  const auto &new_shape = output.info().shape();
  setShape(input_idx, new_shape);
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Pool2D &)
{
  // NYI
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Reshape &)
{
  // NYI
}

void StaticDerivativeShapeInferer::visit(const ir::train::operation::Softmax &)
{
  // NYI
}

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