}
/**
+ * @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>
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;
}