summaryrefslogtreecommitdiff
path: root/compiler/enco/core/src/Transforms/ConstantFolding.test.cpp
blob: 5ac71ac14c891caba6ffa9510fec82a4358497ed (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * 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 "ConstantFolding.h"
#include "Session.h"

#include <cmath>
#include <gtest/gtest.h>

namespace
{

class BinaryNetwork
{
public:
  BinaryNetwork(coco::Module *module, coco::Data *data) : _module{module}, _data{data}
  {
    // DO NOTHING
  }

  template <typename Op> void build(void);

  void fold(void)
  {
    // Execute constant folding
    enco::make_session(_module, _data);
    enco::Code code{_module, _data};
    enco::fold_constants(&code);
  }

public:
  coco::Bag *out;
  coco::Bag *lhs;
  coco::Bag *rhs;

  coco::Eval *eval;

private:
  coco::Module *_module;
  coco::Data *_data;
};

template <typename Op> void BinaryNetwork::build(void)
{
  // Create lhs bag and object
  auto lhs_bag = _module->entity()->bag()->create(12);
  auto lhs_obj = _module->entity()->object()->template create<coco::FeatureObject>();
  coco::FeatureShape lhs_shape(1, 2, 2, 3);
  lhs_obj->bag(lhs_bag);
  lhs_obj->layout(coco::FeatureLayouts::BHWC::create(lhs_shape));

  // Create rhs bag and object
  auto rhs_bag = _module->entity()->bag()->create(12);
  auto rhs_obj = _module->entity()->object()->template create<coco::FeatureObject>();
  coco::FeatureShape rhs_shape(1, 2, 2, 3);
  rhs_obj->bag(rhs_bag);
  rhs_obj->layout(coco::FeatureLayouts::BHWC::create(rhs_shape));

  // Create output bag and object
  auto output_bag = _module->entity()->bag()->create(12);
  auto output_obj = _module->entity()->object()->template create<coco::FeatureObject>();
  coco::FeatureShape ofm_shape(1, 2, 2, 3);
  output_obj->bag(output_bag);
  output_obj->layout(coco::FeatureLayouts::BHWC::create(ofm_shape));

  // Create instruction and operations
  auto block = _module->entity()->block()->create();
  auto eval = _module->entity()->instr()->template create<coco::Eval>();
  auto load_lhs = _module->entity()->op()->template create<coco::Load>();
  auto load_rhs = _module->entity()->op()->template create<coco::Load>();
  auto add_op = _module->entity()->op()->template create<Op>();

  _module->block()->append(block);
  block->instr()->append(eval);

  load_lhs->object(lhs_obj);
  load_rhs->object(rhs_obj);
  add_op->left(load_lhs);
  add_op->right(load_rhs);

  eval->op(add_op);
  eval->out(output_obj);

  // Create a handle
  this->lhs = lhs_bag;
  this->rhs = rhs_bag;
  this->out = output_bag;

  this->eval = eval;
}

} // namespace

TEST(ConstantFoldingTest, sqrt)
{
  auto module = coco::Module::create();
  auto data = coco::Data::create();

  // Create input bag and object
  auto input_bag = module->entity()->bag()->create(12);
  auto input_obj = module->entity()->object()->create<coco::FeatureObject>();
  coco::FeatureShape ifm_shape(1, 2, 2, 3);
  input_obj->bag(input_bag);
  input_obj->layout(coco::FeatureLayouts::BHWC::create(ifm_shape));

  // Create output bag and object
  auto output_bag = module->entity()->bag()->create(12);
  auto output_obj = module->entity()->object()->create<coco::FeatureObject>();
  coco::FeatureShape ofm_shape(1, 2, 2, 3);
  output_obj->bag(output_bag);
  output_obj->layout(coco::FeatureLayouts::BHWC::create(ofm_shape));

  // Insert values into input bag
  data->f32()->allocate(input_bag);
  auto input = data->f32()->weight(input_bag);
  for (uint32_t idx = 0; idx < input.size(); ++idx)
  {
    input[idx] = (float)idx;
  }

  // Create instruction and operations
  auto block = module->entity()->block()->create();
  auto eval = module->entity()->instr()->create<coco::Eval>();
  auto load = module->entity()->op()->create<coco::Load>();
  auto sqrt_op = module->entity()->op()->create<coco::Sqrt>();

  module->block()->append(block);
  block->instr()->append(eval);

  load->object(input_obj);
  sqrt_op->arg(load);

  eval->op(sqrt_op);
  eval->out(output_obj);

  // Execute constant folding
  enco::make_session(module.get(), data.get());
  enco::Code code{module.get(), data.get()};
  enco::fold_constants(&code);

  // Validate the result
  ASSERT_EQ(data->allocated(output_bag), true);
  ASSERT_EQ(eval->out(), nullptr);

  auto output = data->f32()->weight(output_bag);
  for (uint32_t idx = 0; idx < output.size(); ++idx)
  {
    ASSERT_FLOAT_EQ(output[idx], std::sqrt(input[idx]));
  }
}

TEST(ConstantFoldingTest, element_wise_add)
{
  auto module = coco::Module::create();
  auto data = coco::Data::create();

  BinaryNetwork net{module.get(), data.get()};

  // Build a network
  net.build<coco::Add>();

  // Create alises
  auto lhs_bag = net.lhs;
  auto rhs_bag = net.rhs;
  auto output_bag = net.out;
  auto eval = net.eval;

  // Insert values into lhs and rhs bag
  data->f32()->allocate(lhs_bag);
  data->f32()->allocate(rhs_bag);
  auto lhs = data->f32()->weight(lhs_bag);
  auto rhs = data->f32()->weight(rhs_bag);
  for (uint32_t idx = 0; idx < lhs.size(); ++idx)
  {
    lhs[idx] = (float)idx;
    rhs[idx] = 1.5;
  }

  // Execute constant folding
  net.fold();

  // Validate the result
  ASSERT_EQ(data->allocated(output_bag), true);
  ASSERT_EQ(eval->out(), nullptr);

  auto output = data->f32()->weight(output_bag);
  for (uint32_t idx = 0; idx < output.size(); ++idx)
  {
    ASSERT_FLOAT_EQ(output[idx], lhs[idx] + rhs[idx]);
  }
}

TEST(ConstantFoldingTest, element_wise_sub)
{
  auto module = coco::Module::create();
  auto data = coco::Data::create();

  BinaryNetwork net{module.get(), data.get()};

  // Build a network
  net.build<coco::Sub>();

  // Create alises
  auto lhs_bag = net.lhs;
  auto rhs_bag = net.rhs;
  auto output_bag = net.out;
  auto eval = net.eval;

  // Insert values into lhs and rhs bag
  data->f32()->allocate(lhs_bag);
  data->f32()->allocate(rhs_bag);
  auto lhs = data->f32()->weight(lhs_bag);
  auto rhs = data->f32()->weight(rhs_bag);
  for (uint32_t idx = 0; idx < lhs.size(); ++idx)
  {
    lhs[idx] = (float)idx;
    rhs[idx] = 1.5;
  }

  // Execute constant folding
  net.fold();

  // Validate the result
  ASSERT_EQ(data->allocated(output_bag), true);
  ASSERT_EQ(eval->out(), nullptr);

  auto output = data->f32()->weight(output_bag);
  for (uint32_t idx = 0; idx < output.size(); ++idx)
  {
    ASSERT_FLOAT_EQ(output[idx], lhs[idx] - rhs[idx]);
  }
}

TEST(ConstantFoldingTest, element_wise_mul)
{
  auto module = coco::Module::create();
  auto data = coco::Data::create();

  BinaryNetwork net{module.get(), data.get()};

  // Build a network
  net.build<coco::Mul>();

  // Create alises
  auto lhs_bag = net.lhs;
  auto rhs_bag = net.rhs;
  auto output_bag = net.out;
  auto eval = net.eval;

  // Insert values into lhs and rhs bag
  data->f32()->allocate(lhs_bag);
  data->f32()->allocate(rhs_bag);
  auto lhs = data->f32()->weight(lhs_bag);
  auto rhs = data->f32()->weight(rhs_bag);
  for (uint32_t idx = 0; idx < lhs.size(); ++idx)
  {
    lhs[idx] = (float)idx;
    rhs[idx] = 1.5;
  }

  // Execute constant folding
  net.fold();

  // Validate the result
  ASSERT_EQ(data->allocated(output_bag), true);
  ASSERT_EQ(eval->out(), nullptr);

  auto output = data->f32()->weight(output_bag);
  for (uint32_t idx = 0; idx < output.size(); ++idx)
  {
    ASSERT_FLOAT_EQ(output[idx], lhs[idx] * rhs[idx]);
  }
}

TEST(ConstantFoldingTest, element_wise_div)
{
  auto module = coco::Module::create();
  auto data = coco::Data::create();

  BinaryNetwork net{module.get(), data.get()};

  // Build a network
  net.build<coco::Div>();

  // Create alises
  auto lhs_bag = net.lhs;
  auto rhs_bag = net.rhs;
  auto output_bag = net.out;
  auto eval = net.eval;

  // Insert values into lhs and rhs bag
  data->f32()->allocate(lhs_bag);
  data->f32()->allocate(rhs_bag);
  auto lhs = data->f32()->weight(lhs_bag);
  auto rhs = data->f32()->weight(rhs_bag);
  for (uint32_t idx = 0; idx < lhs.size(); ++idx)
  {
    lhs[idx] = (float)idx;
    rhs[idx] = 1.5;
  }

  // Execute constant folding
  net.fold();

  // Validate the result
  ASSERT_EQ(data->allocated(output_bag), true);
  ASSERT_EQ(eval->out(), nullptr);

  auto output = data->f32()->weight(output_bag);
  for (uint32_t idx = 0; idx < output.size(); ++idx)
  {
    ASSERT_FLOAT_EQ(output[idx], lhs[idx] / rhs[idx]);
  }
}