[locomotiv] supporting FeatureBiasAdd (#6504)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Mon, 12 Aug 2019 23:33:30 +0000 (08:33 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 12 Aug 2019 23:33:30 +0000 (08:33 +0900)
This code enables locomotiv to support FeatureBiasAdd.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
compiler/locomotiv/src/Node.lst
compiler/locomotiv/src/Node/BiasAdd.cpp
compiler/locomotiv/src/Node/BiasAdd.test.cpp

index f575cc1..7aaf639 100644 (file)
@@ -5,6 +5,7 @@
 // NODE(Name) : alphabetic order please
 
 NODE(AvgPool2D)
+NODE(BiasAdd<loco::Domain::Feature>)
 NODE(BiasAdd<loco::Domain::Tensor>)
 NODE(BiasEncode)
 NODE(ConstGen)
index 0e6de51..8bf7f1b 100644 (file)
@@ -32,6 +32,15 @@ using nncc::core::ADT::tensor::make_buffer;
 #include <cassert>
 #include <stdexcept>
 
+namespace
+{
+using locomotiv::NodeData;
+
+std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data,
+                               uint32_t axis);
+
+} // namespace
+
 namespace locomotiv
 {
 
@@ -45,8 +54,42 @@ void NodeExecution::execute(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
                locomotiv::annot_domain(bias_add->bias()) == loco::Domain::Bias,
            "Wrong input domain");
 
-  uint32_t axis = bias_add->axis();
+  std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, bias_add->axis());
+
+  assert(bias_add_data != nullptr);
+  erase_annot_data(bias_add);
+  annot_data(bias_add, std::move(bias_add_data));
+  annot_domain(bias_add, annot_domain(bias_add->value()));
+}
+
+void NodeExecution::execute(loco::BiasAdd<loco::Domain::Feature> *bias_add)
+{
+  auto input_data = locomotiv::annot_data(bias_add->value());
+  auto bias_data = locomotiv::annot_data(bias_add->bias());
+
+  validate(input_data && bias_data, "Input not ready");
+  validate(locomotiv::annot_domain(bias_add->value()) == loco::Domain::Feature &&
+               locomotiv::annot_domain(bias_add->bias()) == loco::Domain::Bias,
+           "Wrong input domain");
 
+  std::unique_ptr<NodeData> bias_add_data = calc(input_data, bias_data, 3);
+
+  assert(bias_add_data != nullptr);
+  erase_annot_data(bias_add);
+  annot_data(bias_add, std::move(bias_add_data));
+  annot_domain(bias_add, loco::Domain::Feature);
+}
+
+} // namespace locomotiv
+
+namespace
+{
+using locomotiv::NodeData;
+using locomotiv::validate;
+using locomotiv::make_data;
+
+std::unique_ptr<NodeData> calc(const NodeData *input_data, const NodeData *bias_data, uint32_t axis)
+{
   validate(input_data->shape()->dim(axis) == bias_data->shape()->dim(0), "Bias size mismatch");
 
   std::unique_ptr<NodeData> bias_add_data = nullptr;
@@ -74,11 +117,7 @@ void NodeExecution::execute(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
     default:
       throw std::runtime_error("NYI for this DataType");
   }
-
-  assert(bias_add_data != nullptr);
-  erase_annot_data(bias_add);
-  annot_data(bias_add, std::move(bias_add_data));
-  annot_domain(bias_add, annot_domain(bias_add->value()));
+  return bias_add_data;
 }
 
-} // namespace locomotiv
+} // namespace
index dcaa7ea..0ca8266 100644 (file)
@@ -45,7 +45,7 @@ test case generated from the following:
       print(sess.run(out))
  */
 
-TEST(NodeExecution_BiasAdd, f32)
+TEST(NodeExecution_TensorBiasAdd, f32)
 {
   float in_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
   float bias_val[] = {1.1, 2.1};
@@ -118,3 +118,87 @@ TEST(NodeExecution_BiasAdd, f32)
 
   ASSERT_EQ(locomotiv::annot_domain(bias_add), loco::Domain::Tensor);
 }
+
+/*
+test case generated from the following:
+
+  inp = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
+                    shape=[1, 3, 3, 2], dtype=tf.float32)
+  bias = tf.constant([1.1, 2.1], shape=[2], dtype=tf.float32)
+  out = tf.nn.bias_add(inp, bias)
+
+  with tf.Session() as sess:
+      print(sess.run(out))
+ */
+
+TEST(NodeExecution_FeatureBiasAdd, f32)
+{
+  float in_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
+  float bias_val[] = {1.1, 2.1};
+  float out_val[] = {2.1,  4.1,  4.1,  6.1,  6.1,  8.1,  8.1,  10.1, 10.1,
+                     12.1, 12.1, 14.1, 14.1, 16.1, 16.1, 18.1, 18.1, 20.1};
+
+  // make FeatureBiasAdd(FeatureEncode, BiasEncode)
+  auto g = loco::make_graph();
+  Shape input_shape{1, 3, 3, 2}; // NHWC
+
+  auto feature_encode = g->nodes()->create<loco::FeatureEncode>();
+  {
+    // setting values is ignored for testing
+  }
+
+  auto bias = g->nodes()->create<loco::BiasEncode>();
+  {
+    // nothing to do
+  }
+
+  auto feature_bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Feature>>();
+  {
+    feature_bias_add->value(feature_encode);
+    feature_bias_add->bias(bias);
+  }
+
+  // Make and assign data to pull node
+  auto inp_buf = make_buffer<float, LexicalLayout>(input_shape);
+  {
+    int n = 0;
+    for (IndexEnumerator e{inp_buf.shape()}; e.valid(); e.advance())
+    {
+      inp_buf.at(e.current()) = in_val[n++];
+    }
+  }
+
+  auto bias_buf = make_buffer<float, LexicalLayout>(Shape{2});
+  {
+    int n = 0;
+    for (IndexEnumerator e{bias_buf.shape()}; e.valid(); e.advance())
+    {
+      bias_buf.at(e.current()) = bias_val[n++];
+    }
+  }
+
+  auto inp_data = locomotiv::make_data(inp_buf);
+  locomotiv::annot_data(feature_encode, std::move(inp_data));
+  locomotiv::annot_domain(feature_encode, loco::Domain::Feature);
+
+  auto bias_data = locomotiv::make_data(bias_buf);
+  locomotiv::annot_data(bias, std::move(bias_data));
+  locomotiv::annot_domain(bias, loco::Domain::Bias);
+
+  locomotiv::NodeExecution::get().run(feature_bias_add);
+
+  auto bias_add_data = locomotiv::annot_data(feature_bias_add);
+
+  // comparing the result
+  ASSERT_NE(bias_add_data, nullptr);
+  ASSERT_EQ(bias_add_data->dtype(), loco::DataType::FLOAT32);
+  ASSERT_EQ(*(bias_add_data->shape()), Shape({1, 3, 3, 2}));
+
+  uint32_t n = 0;
+  for (IndexEnumerator e{*(bias_add_data->shape())}; e.valid(); e.advance())
+  {
+    ASSERT_FLOAT_EQ(bias_add_data->as_f32_bufptr()->at(e.current()), out_val[n++]);
+  }
+
+  ASSERT_EQ(locomotiv::annot_domain(feature_bias_add), loco::Domain::Feature);
+}