[locomotiv] BiasAdd (#3877)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Thu, 20 Jun 2019 23:18:53 +0000 (08:18 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Thu, 20 Jun 2019 23:18:53 +0000 (08:18 +0900)
* [locomotiv] BiasAdd

This commit adds BiasAdd and test case for locomotiv.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* checking loco::Domain::Tensor

* msg

* setting the domain of input[1] to loco::Domain::Bias

* fix message

* more check

contrib/locomotiv/src/Node.lst
contrib/locomotiv/src/Node/BiasAdd.cpp [new file with mode: 0644]
contrib/locomotiv/src/Node/BiasAdd.test.cpp [new file with mode: 0644]

index 1a39639..b20623d 100644 (file)
@@ -4,6 +4,7 @@
 
 // NODE(Name) : alphabetic order please
 
+NODE(BiasAdd<loco::Domain::Tensor>)
 NODE(ConstGen)
 NODE(Conv2D)
 NODE(FeatureDecode)
diff --git a/contrib/locomotiv/src/Node/BiasAdd.cpp b/contrib/locomotiv/src/Node/BiasAdd.cpp
new file mode 100644 (file)
index 0000000..3a5145e
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NodeExecution.h"
+
+#include "NodeDataImpl.h"
+#include "NodeDomain.h"
+
+#include <nncc/core/ADT/tensor/Shape.h>
+#include <nncc/core/ADT/tensor/Buffer.h>
+#include <nncc/core/ADT/tensor/IndexEnumerator.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+
+using nncc::core::ADT::tensor::IndexEnumerator;
+using nncc::core::ADT::tensor::LexicalLayout;
+using nncc::core::ADT::tensor::make_buffer;
+
+#include <cassert>
+#include <stdexcept>
+
+namespace locomotiv
+{
+
+void NodeExecution::execute(loco::BiasAdd<loco::Domain::Tensor> *bias_add)
+{
+  auto input_data = locomotiv::annot_data(bias_add->value());
+  auto bias_data = locomotiv::annot_data(bias_add->bias());
+
+  if (!input_data || !bias_data)
+  {
+    throw std::runtime_error("Input not ready");
+  }
+
+  if (locomotiv::annot_domain(bias_add->value()) != loco::Domain::Tensor ||
+      locomotiv::annot_domain(bias_add->bias()) != loco::Domain::Bias)
+  {
+    throw std::runtime_error("Wrong input domain");
+  }
+
+  uint32_t axis = bias_add->axis();
+
+  if (input_data->shape()->dim(axis) != bias_data->shape()->dim(0))
+  {
+    throw std::runtime_error("Bias size mismatch");
+  }
+
+  std::unique_ptr<NodeData> bias_add_data = nullptr;
+
+  switch (input_data->dtype())
+  {
+  case loco::DataType::FLOAT32:
+  {
+    auto input_bufptr = input_data->as_f32_bufptr();
+    auto bias_bufptr = bias_data->as_f32_bufptr();
+    auto bias_add_buf = make_buffer<float, LexicalLayout>(*input_data->shape());
+
+    auto *shape = input_data->shape();
+
+    for (IndexEnumerator e{*shape}; e.valid(); e.advance())
+    {
+      const auto &index = e.current();
+      nncc::core::ADT::tensor::Index bias_index({index.at(axis)});
+      bias_add_buf.at(index) = input_bufptr->at(index) + bias_bufptr->at(bias_index);
+    }
+
+    bias_add_data = make_data(bias_add_buf);
+    break;
+  }
+  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()));
+}
+
+} // namespace locomotiv
diff --git a/contrib/locomotiv/src/Node/BiasAdd.test.cpp b/contrib/locomotiv/src/Node/BiasAdd.test.cpp
new file mode 100644 (file)
index 0000000..dcaa7ea
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "NodeExecution.h"
+
+#include "locomotiv/NodeData.h"
+#include "NodeDataImpl.h"
+#include "NodeDomain.h"
+
+#include <nncc/core/ADT/tensor/Shape.h>
+#include <nncc/core/ADT/tensor/Buffer.h>
+#include <nncc/core/ADT/tensor/LexicalLayout.h>
+#include <nncc/core/ADT/tensor/Index.h>
+#include <nncc/core/ADT/tensor/IndexEnumerator.h>
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::LexicalLayout;
+using nncc::core::ADT::tensor::make_buffer;
+using nncc::core::ADT::tensor::IndexEnumerator;
+
+/*
+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_BiasAdd, 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 BiasAdd(Pull, Const)
+  auto g = loco::make_graph();
+  Shape input_shape{1, 3, 3, 2}; // NHWC
+
+  auto inp = g->nodes()->create<loco::Pull>();
+  {
+    inp->dtype(loco::DataType::FLOAT32);
+    inp->shape({1, 3, 3, 2});
+  }
+
+  auto bias = g->nodes()->create<loco::BiasEncode>();
+  {
+    // nothing to do
+  }
+
+  auto bias_add = g->nodes()->create<loco::BiasAdd<loco::Domain::Tensor>>();
+  {
+    bias_add->value(inp);
+    bias_add->bias(bias);
+    bias_add->axis(3); // axis(3) means C in NHWC
+  }
+
+  // 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(inp, std::move(inp_data));
+  locomotiv::annot_domain(inp, loco::Domain::Tensor);
+
+  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(bias_add);
+
+  auto bias_add_data = locomotiv::annot_data(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(bias_add), loco::Domain::Tensor);
+}