summaryrefslogtreecommitdiff
path: root/compiler/coco/core/src/IR/Module.test.cpp
blob: b55ceacb84fd71dbc0c6b8b45101475a5a1fba20 (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
/*
 * Copyright (c) 2018 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 "coco/IR/Module.h"

#include <gtest/gtest.h>

TEST(IR_MODULE, create)
{
  auto m = coco::Module::create();

  ASSERT_NE(m.get(), nullptr);

  coco::Module *mutable_m = m.get();
  const coco::Module *immutable_m = m.get();

  ASSERT_NE(mutable_m->entity(), nullptr);
  ASSERT_NE(immutable_m->entity(), nullptr);

  ASSERT_NE(mutable_m->entity()->bag(), nullptr);
  ASSERT_EQ(immutable_m->entity()->bag(), mutable_m->entity()->bag());

  ASSERT_NE(mutable_m->entity()->object(), nullptr);
  ASSERT_EQ(immutable_m->entity()->object(), mutable_m->entity()->object());

  ASSERT_NE(mutable_m->entity()->op(), nullptr);
  ASSERT_EQ(immutable_m->entity()->op(), mutable_m->entity()->op());

  ASSERT_NE(mutable_m->entity()->instr(), nullptr);
  ASSERT_EQ(immutable_m->entity()->instr(), mutable_m->entity()->instr());

  ASSERT_NE(mutable_m->entity()->block(), nullptr);
  ASSERT_EQ(immutable_m->entity()->block(), mutable_m->entity()->block());

  ASSERT_NE(mutable_m->entity()->input(), nullptr);
  ASSERT_EQ(immutable_m->entity()->input(), mutable_m->entity()->input());

  ASSERT_NE(mutable_m->entity()->output(), nullptr);
  ASSERT_EQ(immutable_m->entity()->output(), mutable_m->entity()->output());

  ASSERT_NE(mutable_m->block(), nullptr);
  ASSERT_EQ(immutable_m->block(), mutable_m->block());

  ASSERT_NE(mutable_m->input(), nullptr);
  ASSERT_EQ(immutable_m->input(), mutable_m->input());

  ASSERT_NE(mutable_m->output(), nullptr);
  ASSERT_EQ(immutable_m->output(), mutable_m->output());
}

TEST(IR_MODULE, append_two_blocks)
{
  auto m = coco::Module::create();

  auto blk_1 = m->entity()->block()->create();
  m->block()->append(blk_1);

  auto blk_2 = m->entity()->block()->create();
  m->block()->append(blk_2);

  ASSERT_EQ(m->block()->head(), blk_1);
  ASSERT_EQ(m->block()->tail(), blk_2);

  ASSERT_EQ(blk_1->prev(), nullptr);
  ASSERT_EQ(blk_1->next(), blk_2);

  ASSERT_EQ(blk_2->prev(), blk_1);
  ASSERT_EQ(blk_2->next(), nullptr);

  ASSERT_EQ(blk_1->index().value(), 0);
  ASSERT_EQ(blk_2->index().value(), 1);
}

TEST(IR_MODULE, append_two_instrs)
{
  auto m = coco::Module::create();

  auto blk = m->entity()->block()->create();
  auto ins_1 = m->entity()->instr()->create<coco::Eval>();
  auto ins_2 = m->entity()->instr()->create<coco::Eval>();

  blk->instr()->append(ins_1);
  blk->instr()->append(ins_2);

  ASSERT_EQ(blk->instr()->head(), ins_1);
  ASSERT_EQ(blk->instr()->tail(), ins_2);

  ASSERT_EQ(ins_1->parent(), blk);
  ASSERT_EQ(ins_1->prev(), nullptr);
  ASSERT_EQ(ins_1->next(), ins_2);

  ASSERT_EQ(ins_2->parent(), blk);
  ASSERT_EQ(ins_2->prev(), ins_1);
  ASSERT_EQ(ins_2->next(), nullptr);

  ASSERT_EQ(ins_1->index().value(), 0);
  ASSERT_EQ(ins_2->index().value(), 1);
}

TEST(IR_MODULE, iterate_constant_block)
{
  auto m = coco::Module::create();
  auto blk = m->entity()->block()->create();
  auto ins_1 = m->entity()->instr()->create<coco::Eval>();
  auto ins_2 = m->entity()->instr()->create<coco::Eval>();

  blk->instr()->append(ins_1);
  blk->instr()->append(ins_2);

  const coco::Block *immutable_blk = blk;

  ASSERT_EQ(immutable_blk->instr()->head(), ins_1);
  ASSERT_EQ(immutable_blk->instr()->head()->next(), ins_2);
}

TEST(IR_MODULE, input_as_output)
{
  // Some NN frameworks allows users to use a network input as its output.
  //
  // For example, let us consider the following Caffe network
  //
  // name: "example"
  // layer {
  //   name: "l"
  //   type: "Input"
  //   top: "data"
  //   input_param { shape: { dim: 1 dim: 1 dim: 3 dim: 3 } }
  // }
  //
  // "data" blob is the input of this network, and it is also the output of this network.
  const nncc::core::ADT::tensor::Shape shape{1, 1, 3, 3};

  auto m = coco::Module::create();
  auto bag = m->entity()->bag()->create(9);

  auto input = m->entity()->input()->create(shape);
  auto output = m->entity()->output()->create(shape);

  input->name("data");
  input->bag(bag);

  output->name("data");
  output->bag(bag);

  ASSERT_TRUE(bag->isInput());
  ASSERT_TRUE(bag->isOutput());

  output->bag(nullptr);

  ASSERT_TRUE(bag->isInput());
  ASSERT_FALSE(bag->isOutput());
}

/**
 * This test ensures that IR entities allocated via EntityManager have a correct module link
 */
TEST(IR_Module, create_entites)
{
  using namespace coco;
  using namespace nncc::core::ADT;

  auto m = Module::create();
  auto entity = m->entity();

  ASSERT_EQ(entity->bag()->create(1)->module(), m.get());
  ASSERT_EQ(entity->object()->create<coco::FeatureObject>()->module(), m.get());
  ASSERT_EQ(entity->object()->create<coco::KernelObject>()->module(), m.get());
#define OP(Name) ASSERT_EQ(entity->op()->create<Name>()->module(), m.get());
#include "coco/IR/Op.lst"
#undef OP
#define INSTR(Name)                                 \
  {                                                 \
    auto ins = entity->instr()->create<Name>();     \
    ASSERT_EQ(ins->module(), m.get());              \
    ASSERT_TRUE(coco::isa<Name>(ins));              \
    ASSERT_NE(coco::safe_cast<Name>(ins), nullptr); \
  }
#include "coco/IR/Instr.lst"
#undef INSTR
  ASSERT_EQ(entity->block()->create()->module(), m.get());
  ASSERT_EQ(entity->input()->create(tensor::Shape{1})->module(), m.get());
  ASSERT_EQ(entity->output()->create(tensor::Shape{1})->module(), m.get());
}