summaryrefslogtreecommitdiff
path: root/compiler/luci/pass/src/DecomposeHardSwishPass.cpp
blob: bd99d2de08888d7cb7aeb77e4476f751dd7eecbf (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
/*
 * 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 "luci/Pass/DecomposeHardSwishPass.h"

#include "helpers/NodeFiller.h"
#include "helpers/TypeMapper.h"

#include <luci/IR/CircleNodes.h>
#include <luci/Profile/CircleNodeOrigin.h>

namespace
{
/**
 *  BEFORE
 *        [CircleNode]
 *              |
 *              |
 *      [CircleHardSwish]
 *              |
 *              |
 *        [CircleNode]
 *
 *
 *  AFTER
 *
 *      [CircleNode]  [CircleConst]
 *          |    \       /
 *          |     \     /
 *          |   [CircleAdd]
 *          |        |
 *          |        |
 *          \  [CircleRelu6] [CircleConst]
 *           \        \        /
 *            \        \      /
 *             \      [CircleMul]
 *              \       /
 *               \     /
 *             [CircleMul]
 *                  |
 *                  |
 *             [CircleNode]
 *
 */
bool decompose_hardswish(luci::CircleHardSwish *hardswish)
{
  if (not hardswish)
    return false;

  if (hardswish->dtype() != loco::DataType::FLOAT32)
    return false;

  auto g = hardswish->graph();

  auto name = hardswish->name();
  assert(name.length() > 0);

  // Create a const for CircleAdd operation
  auto add_const = g->nodes()->create<luci::CircleConst>();
  add_const->shape({}); // scalar
  add_const->dtype(loco::DataType::FLOAT32);
  add_const->rank(0);
  add_const->size<loco::DataType::FLOAT32>(1);
  add_const->at<loco::DataType::FLOAT32>(0) = 3.;
  add_const->name(name + "/Add/const");
  luci::add_origin(add_const, luci::get_origin(hardswish));

  // Create an Add operation
  auto add = g->nodes()->create<luci::CircleAdd>();
  add->fusedActivationFunction(luci::FusedActFunc::NONE);
  add->x(hardswish->features());
  add->y(add_const);
  add->name(name + "/Add");
  luci::add_origin(add, luci::get_origin(hardswish));

  // Create a Relu6 operation
  auto relu6 = g->nodes()->create<luci::CircleRelu6>();
  relu6->features(add);
  relu6->name(name + "/Relu6");
  luci::add_origin(relu6, luci::get_origin(hardswish));

  // Create a const for CircleMul operation
  auto mul_const = g->nodes()->create<luci::CircleConst>();
  mul_const->shape({}); // scalar
  mul_const->dtype(loco::DataType::FLOAT32);
  mul_const->rank(0);
  mul_const->size<loco::DataType::FLOAT32>(1);
  mul_const->at<loco::DataType::FLOAT32>(0) = 1. / 6.;
  mul_const->name(name + "/Mul/const");
  luci::add_origin(mul_const, luci::get_origin(hardswish));

  // Create first Mul operation
  auto mul1 = g->nodes()->create<luci::CircleMul>();
  mul1->fusedActivationFunction(luci::FusedActFunc::NONE);
  mul1->x(relu6);
  mul1->y(mul_const);
  mul1->name(name + "/Mul1");
  luci::add_origin(mul1, luci::get_origin(hardswish));

  // Create second Mul operation
  auto mul2 = g->nodes()->create<luci::CircleMul>();
  mul2->fusedActivationFunction(luci::FusedActFunc::NONE);
  mul2->x(hardswish->features());
  mul2->y(mul1);
  mul2->name(name + "/Mul2");
  luci::add_origin(mul2, luci::get_origin(hardswish));

  replace(hardswish).with(mul2);

  return true;
}

} // namespace

namespace luci
{

bool DecomposeHardSwishPass::run(loco::Graph *g)
{
  bool changed = false;

  for (auto node : loco::active_nodes(loco::output_nodes(g)))
  {
    if (auto hardswish = dynamic_cast<luci::CircleHardSwish *>(node))
    {
      if (decompose_hardswish(hardswish))
        changed = true;
    }
  }

  return changed;
}

} // namespace luci