summaryrefslogtreecommitdiff
path: root/compiler/exo/src/Pass/FuseReluPass.cpp
blob: d7af0c506ed92596bbac39168ccdee7a23467e3a (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
/*
 * 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 "FuseReluPass.h"

#include "Dialect/IR/TFLNodes.h"
#include "Dialect/IR/TFLDialect.h"
#include "Dialect/IR/TFLNodeVisitor.h"

#include <set>

namespace
{

bool is_pred_fusable(loco::Node *node)
{
  using namespace locoex;

  auto fusable_node = dynamic_cast<TFLNodeMixin<TFLNodeTrait::FusedActFunc> *>(node);

  return (fusable_node and fusable_node->fusedActivationFunction() == FusedActFunc::NONE);
};

struct Collector final : public locoex::TFLNodeMutableVisitor<void>
{
  void visit(locoex::TFLRelu *node) final
  {
    if (is_pred_fusable(node->features()))
      candidates.insert(node);
  }

  void visit(locoex::TFLRelu6 *node) final
  {
    if (is_pred_fusable(node->features()))
      candidates.insert(node);
  }

  void visit(locoex::TFLNode *) final { return; }

  std::set<locoex::TFLNode *> candidates;
};

void set_activation_fusion(loco::Node *node, locoex::FusedActFunc f)
{
  using namespace locoex;

  if (auto fusable_node = dynamic_cast<TFLNodeMixin<TFLNodeTrait::FusedActFunc> *>(node))
    fusable_node->fusedActivationFunction(f);
  else
    assert(false);
}

struct Performer final : public locoex::TFLNodeMutableVisitor<void>
{
  void visit(locoex::TFLRelu *the_relu) final
  {
    set_activation_fusion(the_relu->features(), locoex::FusedActFunc::RELU);

    loco::replace(the_relu).with(the_relu->features());
    the_relu->features(nullptr);
  }

  void visit(locoex::TFLRelu6 *the_relu6) final
  {
    set_activation_fusion(the_relu6->features(), locoex::FusedActFunc::RELU6);

    loco::replace(the_relu6).with(the_relu6->features());
    the_relu6->features(nullptr);
  }

  void visit(locoex::TFLNode *) final { assert(false && "should not be called"); }
};

} // namespace

namespace exo
{

bool FuseReluPass::run(loco::Graph *g)
{
  Collector collector;

  for (auto node : loco::active_nodes(loco::output_nodes(g)))
  {
    if (node->dialect() == locoex::TFLDialect::get())
    {
      auto tfl_node = dynamic_cast<locoex::TFLNode *>(node);
      tfl_node->accept(&collector);
    }
  }

  Performer performer;

  for (auto node : collector.candidates)
  {
    node->accept(&performer);
  }

  return collector.candidates.size() > 0;
}

} // namespace exo