[enco] Not apply constant folding when instruction has output bag (#2897)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Wed, 23 Jan 2019 01:55:09 +0000 (10:55 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 23 Jan 2019 01:55:09 +0000 (10:55 +0900)
* [enco] Not apply constant folding when instruction has output bag

Until now, constant folding was even applied to the instructions which have output bag.
If then, constant folding tries to update network output buffer but it is not impossible.
Furthermore, the total result would be abnormal.
This commit will fix it by not applying constant folding when instruction has output bag and add related testcases.

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
* add TODO comment

* rename testcase

contrib/enco/core/src/Transforms/ConstantFolding.cpp
contrib/enco/test/tflite/Regression_0004/INFERENCE [new file with mode: 0644]
contrib/enco/test/tflite/Regression_0004/test.recipe [new file with mode: 0644]

index 61374d2..cd6f223 100644 (file)
@@ -105,6 +105,13 @@ void fold_constant(std::queue<coco::Bag *> &q, coco::Copy *copy)
   auto dst_obj = copy->into();
   auto dst_bag = dst_obj->bag();
 
+  // Output calculation should not be folded
+  // TODO Reduce code duplication of this kind
+  if (dst_bag->isOutput())
+  {
+    return;
+  }
+
   // NOTE d->allocated(bag) returns true if bag has corresponding initial
   //      values (e.g. convolution kernel)
   assert(d->allocated(src_bag));
@@ -184,6 +191,13 @@ void fold_constant_op(std::queue<coco::Bag *> &q, coco::UnaryOp *op, Callable ev
   auto dst_obj = eval->out();
   auto dst_bag = dst_obj->bag();
 
+  // Output calculation should not be folded
+  // TODO Reduce code duplication of this kind
+  if (dst_bag->isOutput())
+  {
+    return;
+  }
+
   assert(d->allocated(src_bag));
   assert(!d->allocated(dst_bag));
 
@@ -268,6 +282,13 @@ void fold_constant_op(std::queue<coco::Bag *> &q, coco::BinaryOp *op, Callable e
   auto dst_obj = eval->out();
   auto dst_bag = dst_obj->bag();
 
+  // Output calculation should not be folded
+  // TODO Reduce code duplication of this kind
+  if (dst_bag->isOutput())
+  {
+    return;
+  }
+
   // The other bag is non-constant
   if (!d->allocated(lhs_bag) || !d->allocated(rhs_bag))
   {
diff --git a/contrib/enco/test/tflite/Regression_0004/INFERENCE b/contrib/enco/test/tflite/Regression_0004/INFERENCE
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/contrib/enco/test/tflite/Regression_0004/test.recipe b/contrib/enco/test/tflite/Regression_0004/test.recipe
new file mode 100644 (file)
index 0000000..80705ef
--- /dev/null
@@ -0,0 +1,27 @@
+operand {
+  name: "ifm0"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+  filler { tag: "constant" arg: "0.1" }
+}
+operand {
+  name: "ifm1"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+  filler { tag: "constant" arg: "0.1" }
+}
+operand {
+  name: "ofm"
+  type: FLOAT32
+  shape { dim: 1 dim: 4 dim: 4 dim: 3 }
+}
+operation {
+  type: "Div"
+  input: "ifm0"
+  input: "ifm1"
+  output: "ofm"
+  div_options {
+    activation: NONE
+  }
+}
+output: "ofm"