summaryrefslogtreecommitdiff
path: root/compiler/enco/core/src
diff options
context:
space:
mode:
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>2019-09-17 19:19:08 +0900
committerGitHub Enterprise <noreply-CODE@samsung.com>2019-09-17 19:19:08 +0900
commit89257ecbe9504d37afca6254159a5d5f09a640ac (patch)
treee3d9801c03ca20473db5f8adb4db7e7252ef3239 /compiler/enco/core/src
parent3c4f2e5ed17a169bd9d5759b2d5a88f2e92b51a1 (diff)
downloadnnfw-89257ecbe9504d37afca6254159a5d5f09a640ac.tar.gz
nnfw-89257ecbe9504d37afca6254159a5d5f09a640ac.tar.bz2
nnfw-89257ecbe9504d37afca6254159a5d5f09a640ac.zip
[enco] Extract Boilerplate as Helper (#7523)
This commit extracts boilerplate in ConstantFoldingTest as a helper in order to reduce code duplication. Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
Diffstat (limited to 'compiler/enco/core/src')
-rw-r--r--compiler/enco/core/src/Transforms/ConstantFolding.test.cpp276
1 files changed, 120 insertions, 156 deletions
diff --git a/compiler/enco/core/src/Transforms/ConstantFolding.test.cpp b/compiler/enco/core/src/Transforms/ConstantFolding.test.cpp
index 8ad0a28d1..5ac71ac14 100644
--- a/compiler/enco/core/src/Transforms/ConstantFolding.test.cpp
+++ b/compiler/enco/core/src/Transforms/ConstantFolding.test.cpp
@@ -20,6 +20,90 @@
#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();
@@ -83,26 +167,16 @@ TEST(ConstantFoldingTest, element_wise_add)
auto module = coco::Module::create();
auto data = coco::Data::create();
- // Create lhs bag and object
- auto lhs_bag = module->entity()->bag()->create(12);
- auto lhs_obj = module->entity()->object()->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));
+ BinaryNetwork net{module.get(), data.get()};
- // Create rhs bag and object
- auto rhs_bag = module->entity()->bag()->create(12);
- auto rhs_obj = module->entity()->object()->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));
+ // Build a network
+ net.build<coco::Add>();
- // 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));
+ // 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);
@@ -115,28 +189,8 @@ TEST(ConstantFoldingTest, element_wise_add)
rhs[idx] = 1.5;
}
- // Create instruction and operations
- auto block = module->entity()->block()->create();
- auto eval = module->entity()->instr()->create<coco::Eval>();
- auto load_lhs = module->entity()->op()->create<coco::Load>();
- auto load_rhs = module->entity()->op()->create<coco::Load>();
- auto add_op = module->entity()->op()->create<coco::Add>();
-
- 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);
-
// Execute constant folding
- enco::make_session(module.get(), data.get());
- enco::Code code{module.get(), data.get()};
- enco::fold_constants(&code);
+ net.fold();
// Validate the result
ASSERT_EQ(data->allocated(output_bag), true);
@@ -154,26 +208,16 @@ TEST(ConstantFoldingTest, element_wise_sub)
auto module = coco::Module::create();
auto data = coco::Data::create();
- // Create lhs bag and object
- auto lhs_bag = module->entity()->bag()->create(12);
- auto lhs_obj = module->entity()->object()->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));
+ BinaryNetwork net{module.get(), data.get()};
- // Create rhs bag and object
- auto rhs_bag = module->entity()->bag()->create(12);
- auto rhs_obj = module->entity()->object()->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));
+ // Build a network
+ net.build<coco::Sub>();
- // 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));
+ // 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);
@@ -186,28 +230,8 @@ TEST(ConstantFoldingTest, element_wise_sub)
rhs[idx] = 1.5;
}
- // Create instruction and operations
- auto block = module->entity()->block()->create();
- auto eval = module->entity()->instr()->create<coco::Eval>();
- auto load_lhs = module->entity()->op()->create<coco::Load>();
- auto load_rhs = module->entity()->op()->create<coco::Load>();
- auto add_op = module->entity()->op()->create<coco::Sub>();
-
- 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);
-
// Execute constant folding
- enco::make_session(module.get(), data.get());
- enco::Code code{module.get(), data.get()};
- enco::fold_constants(&code);
+ net.fold();
// Validate the result
ASSERT_EQ(data->allocated(output_bag), true);
@@ -225,26 +249,16 @@ TEST(ConstantFoldingTest, element_wise_mul)
auto module = coco::Module::create();
auto data = coco::Data::create();
- // Create lhs bag and object
- auto lhs_bag = module->entity()->bag()->create(12);
- auto lhs_obj = module->entity()->object()->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));
+ BinaryNetwork net{module.get(), data.get()};
- // Create rhs bag and object
- auto rhs_bag = module->entity()->bag()->create(12);
- auto rhs_obj = module->entity()->object()->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));
+ // Build a network
+ net.build<coco::Mul>();
- // 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));
+ // 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);
@@ -257,28 +271,8 @@ TEST(ConstantFoldingTest, element_wise_mul)
rhs[idx] = 1.5;
}
- // Create instruction and operations
- auto block = module->entity()->block()->create();
- auto eval = module->entity()->instr()->create<coco::Eval>();
- auto load_lhs = module->entity()->op()->create<coco::Load>();
- auto load_rhs = module->entity()->op()->create<coco::Load>();
- auto add_op = module->entity()->op()->create<coco::Mul>();
-
- 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);
-
// Execute constant folding
- enco::make_session(module.get(), data.get());
- enco::Code code{module.get(), data.get()};
- enco::fold_constants(&code);
+ net.fold();
// Validate the result
ASSERT_EQ(data->allocated(output_bag), true);
@@ -296,26 +290,16 @@ TEST(ConstantFoldingTest, element_wise_div)
auto module = coco::Module::create();
auto data = coco::Data::create();
- // Create lhs bag and object
- auto lhs_bag = module->entity()->bag()->create(12);
- auto lhs_obj = module->entity()->object()->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));
+ BinaryNetwork net{module.get(), data.get()};
- // Create rhs bag and object
- auto rhs_bag = module->entity()->bag()->create(12);
- auto rhs_obj = module->entity()->object()->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));
+ // Build a network
+ net.build<coco::Div>();
- // 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));
+ // 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);
@@ -328,28 +312,8 @@ TEST(ConstantFoldingTest, element_wise_div)
rhs[idx] = 1.5;
}
- // Create instruction and operations
- auto block = module->entity()->block()->create();
- auto eval = module->entity()->instr()->create<coco::Eval>();
- auto load_lhs = module->entity()->op()->create<coco::Load>();
- auto load_rhs = module->entity()->op()->create<coco::Load>();
- auto add_op = module->entity()->op()->create<coco::Div>();
-
- 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);
-
// Execute constant folding
- enco::make_session(module.get(), data.get());
- enco::Code code{module.get(), data.get()};
- enco::fold_constants(&code);
+ net.fold();
// Validate the result
ASSERT_EQ(data->allocated(output_bag), true);