summaryrefslogtreecommitdiff
path: root/compiler/luci/pass/src/ReplaceNonConstFCWithBatchMatMulPass.test.cpp
blob: 7606a6125ae3d08732247908f9988d6e6ce9f65c (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
/*
 * Copyright (c) 2022 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/ReplaceNonConstFCWithBatchMatMulPass.h"

#include <luci/test/TestIOGraph.h>
#include <luci/IR/CircleNodes.h>

#include <gtest/gtest.h>

namespace
{

using namespace luci::test;

// TODO Reduce duplicate codes in ResolveCustomOpMatMulPass.cpp
template <typename T>
luci::CircleConst *create_const_node(loco::Graph *g, const loco::DataType dtype,
                                     const std::vector<uint32_t> &shape,
                                     const std::vector<T> &values)
{
  auto node = g->nodes()->create<luci::CircleConst>();
  node->dtype(dtype);
  node->rank(shape.size());

  uint32_t size = 1;
  for (uint32_t i = 0; i < shape.size(); ++i)
  {
    node->dim(i) = shape.at(i);
    size *= shape.at(i);
  }
  node->shape_status(luci::ShapeStatus::VALID);

#define INIT_VALUES(DT)                          \
  {                                              \
    node->size<DT>(size);                        \
    for (uint32_t i = 0; i < values.size(); ++i) \
      node->at<DT>(i) = values[i];               \
  }

  switch (dtype)
  {
    case loco::DataType::U8:
      INIT_VALUES(loco::DataType::U8);
      break;
    case loco::DataType::S16:
      INIT_VALUES(loco::DataType::S16);
      break;
    case loco::DataType::S32:
      INIT_VALUES(loco::DataType::S32);
      break;
    case loco::DataType::FLOAT32:
      INIT_VALUES(loco::DataType::FLOAT32)
      break;
    default:
      INTERNAL_EXN("create_const_node called with unsupported type");
      break;
  }
  return node;
}

/**
 *  Simple graph for test
 *
 *  BEFORE
 *
 *   [IFM1] [IFM2] [BIAS]
 *        \   |   /
 *          [FC]
 *            |
 *          [Res]
 *
 *  AFTER
 *   [IFM1] [IFM2]
 *        \   |
 *      [BatchMatMul] [BIAS]
 *              \      /
 *               [Add]
 *                 |
 *               [Res]
 *
 */
struct FCGraphlet
{
public:
  FCGraphlet() = default;
  virtual ~FCGraphlet() = default;

  void init(loco::Graph *g, const ShapeU32 r_shape, const float bv)
  {
    _tr_y = g->nodes()->create<luci::CircleTranspose>();
    _tr_y->a(_y);
    std::vector<int32_t> tr_val = {1, 0};
    _tr_y->perm(create_const_node(g, loco::DataType::S32, {2}, tr_val));

    _fc = g->nodes()->create<luci::CircleFullyConnected>();
    _fc->input(_x);
    _fc->weights(_tr_y);
    _fc->fusedActivationFunction(luci::FusedActFunc::NONE);
    _fc->dtype(loco::DataType::FLOAT32);
    _fc->shape(r_shape);
    auto l = _fc->dim(_fc->rank() - 1).value();
    std::vector<float> bias_val(l, bv);
    _fc->bias(create_const_node(g, loco::DataType::FLOAT32, {l}, bias_val));
    _fc->name("fc");
  }

public:
  luci::CircleFullyConnected *fc() { return _fc; }

protected:
  luci::CircleFullyConnected *_fc = nullptr;
  luci::CircleTranspose *_tr_y = nullptr;
  luci::CircleInput *_x = nullptr;
  luci::CircleInput *_y = nullptr;
};

struct FCGraph : public TestIsGraphlet<2>, public TestOGraphlet, public FCGraphlet
{
  FCGraph() = default;
  virtual ~FCGraph() = default;
  void init(const ShapeU32 x_shape, const ShapeU32 y_shape, const ShapeU32 r_shape, const float bv)
  {
    TestIsGraphlet<2>::init(g(), {x_shape, y_shape});
    TestOGraphlet::init(g(), r_shape);
    _x = input(0);
    _y = input(1);
    FCGraphlet::init(g(), r_shape, bv);
    output()->from(_fc);
  }
};

class ReplaceNonConstFCWithBatchMatMulPassTest : public ::testing::Test
{
public:
  FCGraph g;
  luci::ReplaceNonConstFCWithBatchMatMulPass pass;
};

} // namespace

TEST_F(ReplaceNonConstFCWithBatchMatMulPassTest, simple_test)
{
  g.init({2, 3}, {2, 3}, {2, 2}, 0.0f);

  auto ret = pass.run(g.g());
  EXPECT_EQ(true, ret);

  auto mm = dynamic_cast<luci::CircleBatchMatMul *>(g.output()->from());
  EXPECT_NE(nullptr, mm);
}

TEST_F(ReplaceNonConstFCWithBatchMatMulPassTest, nonzero_bias_test)
{
  g.init({2, 3}, {2, 3}, {2, 2}, 1.0f);

  auto ret = pass.run(g.g());
  EXPECT_EQ(true, ret);

  auto mm = dynamic_cast<luci::CircleAdd *>(g.output()->from());
  EXPECT_NE(nullptr, mm);
}

TEST_F(ReplaceNonConstFCWithBatchMatMulPassTest, wrong_op_NEG)
{
  loco::Graph g;

  auto inp = g.nodes()->create<luci::CircleInput>();
  auto relu = g.nodes()->create<luci::CircleRelu>();
  relu->features(inp);

  luci::ReplaceNonConstFCWithBatchMatMulPass pass;
  auto changed = pass.run(&g);

  EXPECT_EQ(false, changed);
}