summaryrefslogtreecommitdiff
path: root/compiler/moco-tf/src/Canonicalization/ConstCanonicalizer.cpp
blob: dea97f94a6bed4dccb1e2c87659f4e7c5c587ab1 (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
/*
 * 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.
 */

#include "ConstCanonicalizer.h"

#include "Dialect/TFDialect.h"
#include "Dialect/TFNodes.h"
#include "Dialect/TFNodeVisitor.h"
#include "Dialect/TFNodeImpl.h"

#include <moco/tf/Names.h>
#include <moco/Log.h>

namespace
{

bool canonicalize_const(loco::Graph *graph, moco::tf::TFConst *node)
{
  LOGGER(l);

  /**
   * @note This will replace TFConst node with Canonical Const
   *
   *       Before
   *                 TFConst -- C
   *
   *       After
   *                 TFConst -
   *                 ConstGen -- C
   *
   *       Where
   *                 C : a node that uses TFConst as an input
   *                 TFConst is disconnected from other nodes
   */

  INFO(l) << "TFNodeCanonicalize TFConst begin";

  auto const_node = graph->nodes()->create<loco::ConstGen>();

  // copy properties
  auto dtype = node->dtype();
  const_node->dtype(dtype);

  auto rank = node->rank();
  const_node->rank(rank);
  for (uint32_t r = 0; r < rank; ++r)
  {
    if (node->dim(r).known())
      const_node->dim(r) = node->dim(r);
    else
      const_node->dim(r).unset();
  }

  switch (dtype)
  {
    case loco::DataType::S32:
    {
      uint32_t input_elements = node->size<loco::DataType::S32>();
      const_node->size<loco::DataType::S32>(input_elements);
      for (uint32_t i = 0; i < input_elements; ++i)
      {
        const_node->at<loco::DataType::S32>(i) = node->at<loco::DataType::S32>(i);
      }
      break;
    }
    case loco::DataType::FLOAT32:
    {
      uint32_t input_elements = node->size<loco::DataType::FLOAT32>();
      const_node->size<loco::DataType::FLOAT32>(input_elements);
      for (uint32_t i = 0; i < input_elements; ++i)
      {
        const_node->at<loco::DataType::FLOAT32>(i) = node->at<loco::DataType::FLOAT32>(i);
      }
      break;
    }
    default:
      throw std::runtime_error("NYI for this DataType");
  }

  // update graph
  replace(node).with(const_node);

  INFO(l) << "TFNodeCanonicalize TFConst done";

  return true;
}

} // namespace

namespace moco
{
namespace tf
{

bool ConstCanonicalizer::run(loco::Graph *graph)
{
  auto active_nodes = loco::active_nodes(loco::output_nodes(graph));
  bool changed = false;

  for (auto node : active_nodes)
  {
    if (node->dialect() == TFDialect::get())
    {
      auto tf_const = dynamic_cast<moco::tf::TFConst *>(node);
      if (tf_const != nullptr)
      {
        if (canonicalize_const(graph, tf_const))
          changed = true;
      }
    }
  }

  return changed;
}

} // namespace tf
} // namespace moco