summaryrefslogtreecommitdiff
path: root/compiler/loco/src/Service/TypeInference.test.cpp
blob: 4660401db30ecd271a0646d73d0fbbf82fad1a14 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * 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 "loco/Service/TypeInference.h"

#include "GraphTestcase.h"

#include <loco/IR/CanonicalDialect.h>
#include <loco/Service/TypeInference.h>

#include <vector>

#include <gtest/gtest.h>

// This test validates whether framework works as expected.
TEST(TypeInferenceTest, framework)
{
  // Create a sample network
  auto g = loco::make_graph();

  auto pull_node = g->nodes()->create<loco::Pull>();
  auto push_node = g->nodes()->create<loco::Push>();

  push_node->from(pull_node);

  // Create Graph Input & Output
  auto graph_input = g->inputs()->create();

  graph_input->name("input");
  loco::link(graph_input, pull_node);

  auto graph_output = g->outputs()->create();

  graph_output->name("output");
  loco::link(graph_output, push_node);

  // Mock-up Type Inference Rule
  struct SampleTypeInferenceRule final : public loco::TypeInferenceRule
  {
  public:
    SampleTypeInferenceRule(std::vector<const loco::Node *> *nodes) : _nodes{nodes}
    {
      // DO NOTHING
    }

  public:
    bool recognize(const loco::Dialect *) const final
    {
      // Accept all the dialects
      return true;
    }

    bool infer(const loco::Node *node, loco::DataType &dtype) const final
    {
      // Record the order of inference
      _nodes->emplace_back(node);

      if (_nodes->size() != 1)
      {
        return false;
      }

      // Annotate the first node as "U8"
      dtype = loco::DataType::U8;
      return true;
    }

  private:
    std::vector<const loco::Node *> *_nodes;
  };

  std::vector<const loco::Node *> nodes;

  SampleTypeInferenceRule rule{&nodes};

  loco::apply(&rule).to(g.get());

  ASSERT_EQ(nodes.size(), 2);        // Framework SHOULD visit all the nodes
  ASSERT_EQ(nodes.at(0), pull_node); // Framework SHOULD visit "pull" before "push"
  ASSERT_EQ(nodes.at(1), push_node);

  // Framework SHOULD NOT make any annotation if "rule" returns FALSE
  ASSERT_TRUE(loco::dtype_known(pull_node));
  // Framework SHOULD make an annotation if "rule" returns TRUE
  ASSERT_EQ(loco::dtype_get(pull_node), loco::DataType::U8);
  ASSERT_FALSE(loco::dtype_known(push_node));
}

TEST(CanonicalTypeInferenceRuleTest, minimal)
{
  // Create a simple network
  auto g = loco::make_graph();

  auto pull_node = g->nodes()->create<loco::Pull>();

  pull_node->dtype(loco::DataType::U8);

  auto push_node = g->nodes()->create<loco::Push>();

  push_node->from(pull_node);

  auto graph_input = g->inputs()->create();

  graph_input->name("input");
  loco::link(graph_input, pull_node);

  auto graph_output = g->outputs()->create();

  graph_output->name("output");
  loco::link(graph_output, push_node);

  // Run Type Inference
  loco::CanonicalTypeInferenceRule rule;

  loco::apply(&rule).to(g.get());

  // Verify!
  ASSERT_TRUE(loco::dtype_known(push_node));
  ASSERT_EQ(loco::dtype_get(push_node), loco::DataType::U8);
}

TEST(CanonicalTypeInferenceRuleTest, relu6)
{
  // Create a simple Relu6 network
  auto g = loco::make_graph();

  auto pull_node = g->nodes()->create<loco::Pull>();

  pull_node->dtype(loco::DataType::FLOAT32);

  auto relu6_node = g->nodes()->create<loco::ReLU6>();

  relu6_node->input(pull_node);

  auto push_node = g->nodes()->create<loco::Push>();

  push_node->from(relu6_node);

  auto graph_input = g->inputs()->create();

  graph_input->name("input");
  loco::link(graph_input, pull_node);

  auto graph_output = g->outputs()->create();

  graph_output->name("output");
  loco::link(graph_output, push_node);

  // Run Type Inference
  loco::CanonicalTypeInferenceRule rule;

  loco::apply(&rule).to(g.get());

  // Verify!
  ASSERT_TRUE(loco::dtype_known(relu6_node));
  ASSERT_EQ(loco::dtype_get(relu6_node), loco::DataType::FLOAT32);
}

TEST(CanonicalTypeInferenceRuleTest, tensor_broadcast)
{
  // Create a sample network
  GraphTestcase<GraphCode::TensorBroadcast> testcase{1, 2};

  testcase.graph()->inputs()->at(0)->dtype(loco::DataType::U8);

  // Run Type Inference
  loco::CanonicalTypeInferenceRule rule;

  loco::apply(&rule).to(testcase.graph());

  // Verify!
  ASSERT_TRUE(loco::dtype_known(testcase.push_node));
  ASSERT_EQ(loco::dtype_get(testcase.push_node), loco::DataType::U8);
}

// mockup for MultiDialectTypeInferenceRule
// OpNode of a specific loco datatype (defined in template) will be used.
// And a Dialect for the OpNode and its inference rules are created.
#include <loco/IR/Dialect.h>

namespace
{

template <loco::DataType N> class TestDialect final : public loco::Dialect
{
public:
  static Dialect *get(void)
  {
    static TestDialect<N> d;
    return &d;
  }
};

template <loco::DataType N>
struct TestOpNode final : public loco::FixedArity<1>::Mixin<loco::Node>,
                          public loco::NodeMixin<loco::NodeTrait::DataType>
{
  void input(Node *node) { at(0)->node(node); }
  const loco::Dialect *dialect(void) const final { return TestDialect<N>::get(); }
  uint32_t opnum(void) const final { return static_cast<uint32_t>(N); }
};

template <loco::DataType N> struct TestTypeInferenceRule final : public loco::TypeInferenceRule
{
public:
  bool recognize(const loco::Dialect *d) const final { return (d == TestDialect<N>::get()); }

  bool infer(const loco::Node *node, loco::DataType &dtype) const final
  {
    assert(node->dialect() == TestDialect<N>::get());
    auto test_node = dynamic_cast<const TestOpNode<N> *>(node);
    assert(test_node != nullptr);

    dtype = N;
    return true;
  }
};

} // namespace

TEST(MultiDialectTypeInferenceRuleTest, test1)
{
  // Create a simple network : Pull - S8 - U8 - Push
  auto g = loco::make_graph();

  auto pull_node = g->nodes()->create<loco::Pull>();
  pull_node->dtype(loco::DataType::FLOAT32);

  auto s8_node = g->nodes()->create<TestOpNode<loco::DataType::S8>>();
  s8_node->input(pull_node);

  auto u8_node = g->nodes()->create<TestOpNode<loco::DataType::U8>>();
  u8_node->input(s8_node);

  auto push_node = g->nodes()->create<loco::Push>();
  push_node->from(u8_node);

  auto graph_input = g->inputs()->create();
  graph_input->name("input");
  loco::link(graph_input, pull_node);

  auto graph_output = g->outputs()->create();
  graph_output->name("output");
  loco::link(graph_output, push_node);

  // initially they don't have type info
  ASSERT_FALSE(loco::dtype_known(s8_node));
  ASSERT_FALSE(loco::dtype_known(u8_node));

  // Run Type Inference
  TestTypeInferenceRule<loco::DataType::U8> u8_rule;
  TestTypeInferenceRule<loco::DataType::S8> s8_rule;
  loco::CanonicalTypeInferenceRule canon_rule;

  loco::MultiDialectTypeInferenceRule rules;

  rules.bind(TestDialect<loco::DataType::S8>::get(), &s8_rule)
      .bind(TestDialect<loco::DataType::U8>::get(), &u8_rule)
      .bind(loco::CanonicalDialect::get(), &canon_rule);

  loco::apply(&rules).to(g.get());

  // Verify!
  ASSERT_TRUE(loco::dtype_known(s8_node));
  ASSERT_EQ(loco::dtype_get(s8_node), loco::DataType::S8);

  ASSERT_TRUE(loco::dtype_known(u8_node));
  ASSERT_EQ(loco::dtype_get(u8_node), loco::DataType::U8);
}