From 30b2c22acfebee462ad5bec6fd242c29e52cc0de Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 21 Jun 2019 08:18:53 +0900 Subject: [PATCH] [locomotiv] BiasAdd (#3877) * [locomotiv] BiasAdd This commit adds BiasAdd and test case for locomotiv. Signed-off-by: Hyun Sik Yoon * checking loco::Domain::Tensor * msg * setting the domain of input[1] to loco::Domain::Bias * fix message * more check --- contrib/locomotiv/src/Node.lst | 1 + contrib/locomotiv/src/Node/BiasAdd.cpp | 92 +++++++++++++++++++++ contrib/locomotiv/src/Node/BiasAdd.test.cpp | 120 ++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 contrib/locomotiv/src/Node/BiasAdd.cpp create mode 100644 contrib/locomotiv/src/Node/BiasAdd.test.cpp diff --git a/contrib/locomotiv/src/Node.lst b/contrib/locomotiv/src/Node.lst index 1a39639..b20623d 100644 --- a/contrib/locomotiv/src/Node.lst +++ b/contrib/locomotiv/src/Node.lst @@ -4,6 +4,7 @@ // NODE(Name) : alphabetic order please +NODE(BiasAdd) 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 index 0000000..3a5145e --- /dev/null +++ b/contrib/locomotiv/src/Node/BiasAdd.cpp @@ -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 +#include +#include +#include + +using nncc::core::ADT::tensor::IndexEnumerator; +using nncc::core::ADT::tensor::LexicalLayout; +using nncc::core::ADT::tensor::make_buffer; + +#include +#include + +namespace locomotiv +{ + +void NodeExecution::execute(loco::BiasAdd *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 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(*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 index 0000000..dcaa7ea --- /dev/null +++ b/contrib/locomotiv/src/Node/BiasAdd.test.cpp @@ -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 +#include +#include +#include +#include + +#include + +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(); + { + inp->dtype(loco::DataType::FLOAT32); + inp->shape({1, 3, 3, 2}); + } + + auto bias = g->nodes()->create(); + { + // nothing to do + } + + auto bias_add = g->nodes()->create>(); + { + 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(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(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); +} -- 2.7.4