[moco-tf] Introduce function to create BiasAdd operation (#6292)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Wed, 7 Aug 2019 00:03:26 +0000 (09:03 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 7 Aug 2019 00:03:26 +0000 (09:03 +0900)
This commit will introduce a new function to create `BiasAdd` operation
and insert it after preceding operation.

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
compiler/moco-tf/src/Transforms/FuseBinaryIntoPreceding.cpp

index 636b0d9..b1b1f16 100644 (file)
@@ -337,6 +337,42 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node)
 }
 
 /**
+ * @brief Create zero-filled BiasAdd opertion and insert after precedingOp
+ *        The plan is to fuse 'addparam' to TFBiasAdd bias
+ * @return Zero-filled BiasAdd operation
+ */
+template <class T>
+moco::tf::TFBiasAdd *create_biasadd_node(loco::Graph *graph, moco::tf::TFConst *addparam,
+                                         T *precedingOp)
+{
+  auto dtype = addparam->dtype();
+  assert(dtype == loco::DataType::FLOAT32);
+
+  // Create TFConst(bias of TFBiasAdd) with same shape and dtype of 'addparam' but
+  // with values 0.0
+  auto biasadd_param = graph->nodes()->create<moco::tf::TFConst>();
+  biasadd_param->dtype(dtype);
+  copy_shape(addparam, biasadd_param);
+  auto biasadd_num_elements = addparam->size<loco::DataType::FLOAT32>();
+  biasadd_param->size<loco::DataType::FLOAT32>(biasadd_num_elements);
+  for (int32_t i = 0; i < biasadd_num_elements; i++)
+  {
+    biasadd_param->at<loco::DataType::FLOAT32>(i) = 0.0f;
+  }
+
+  // Create TFBiasAdd with same shape as TFAdd
+  auto data_layout = precedingOp->data_layout();
+  auto tf_biasadd = graph->nodes()->create<moco::tf::TFBiasAdd>();
+  tf_biasadd->data_layout(data_layout);
+
+  loco::replace(precedingOp).with(tf_biasadd);
+  tf_biasadd->value(precedingOp);
+  tf_biasadd->bias(biasadd_param);
+
+  return tf_biasadd;
+}
+
+/**
  * @note TFAdd will be fused to TFBiasAdd
  *
  * <Before>
@@ -422,35 +458,7 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFAdd *node)
 
   if (conv2d != nullptr && addparam != nullptr)
   {
-    // precedingOp is TFConv2D. Let's insert TFConst - TFBiasAdd
-    // The plan is to fuse 'addparam' to TFBiasAdd bias
-
-    auto dtype = addparam->dtype();
-    assert(dtype == loco::DataType::FLOAT32);
-
-    // Create TFBiasAdd bias TFConst with same shape and dtype of 'addparam' but
-    // with values 0.0
-    auto biasadd_param = graph->nodes()->create<moco::tf::TFConst>();
-    biasadd_param->dtype(dtype);
-    copy_shape(addparam, biasadd_param);
-    auto biasadd_num_elements = addparam->size<loco::DataType::FLOAT32>();
-    biasadd_param->size<loco::DataType::FLOAT32>(biasadd_num_elements);
-    for (int32_t i = 0; i < biasadd_num_elements; i++)
-    {
-      biasadd_param->at<loco::DataType::FLOAT32>(i) = 0.0f;
-    }
-
-    // Create TFBiasAdd with same shape as TFAdd
-    auto data_layout = conv2d->data_layout();
-    auto tf_biasadd = graph->nodes()->create<moco::tf::TFBiasAdd>();
-    tf_biasadd->data_layout(data_layout);
-
-    replace(conv2d).with(tf_biasadd);
-    tf_biasadd->value(conv2d);
-    tf_biasadd->bias(biasadd_param);
-
-    biasadd = tf_biasadd;
-
+    biasadd = create_biasadd_node<moco::tf::TFConv2D>(graph, addparam, conv2d);
     bchanged = true;
   }