From a9b403765a2283f5cbbf9e9e04cc8f41ffc718dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 6 Aug 2019 10:54:21 +0900 Subject: [PATCH] [locomotiv] Support EltwiseDiv (#6258) This will support EltwiseDiv node Signed-off-by: SaeHie Park --- compiler/locomotiv/src/Node.lst | 1 + compiler/locomotiv/src/Node/EltwiseDiv.cpp | 81 ++++++++++++++++ compiler/locomotiv/src/Node/EltwiseDiv.test.cpp | 121 ++++++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 compiler/locomotiv/src/Node/EltwiseDiv.cpp create mode 100644 compiler/locomotiv/src/Node/EltwiseDiv.test.cpp diff --git a/compiler/locomotiv/src/Node.lst b/compiler/locomotiv/src/Node.lst index 676f6d2..f575cc1 100644 --- a/compiler/locomotiv/src/Node.lst +++ b/compiler/locomotiv/src/Node.lst @@ -12,6 +12,7 @@ NODE(Conv2D) NODE(DepthwiseConv2D) NODE(DepthwiseFilterEncode) NODE(EltwiseAdd) +NODE(EltwiseDiv) NODE(EltwiseMul) NODE(EltwiseSub) NODE(FeatureDecode) diff --git a/compiler/locomotiv/src/Node/EltwiseDiv.cpp b/compiler/locomotiv/src/Node/EltwiseDiv.cpp new file mode 100644 index 0000000..599e015 --- /dev/null +++ b/compiler/locomotiv/src/Node/EltwiseDiv.cpp @@ -0,0 +1,81 @@ +/* + * 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 "Validation.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::EltwiseDiv *eltwise_div) +{ + auto lhs_data = annot_data(eltwise_div->lhs()); + auto rhs_data = annot_data(eltwise_div->rhs()); + + validate(lhs_data && rhs_data, "Input not ready"); + validate(annot_domain(eltwise_div->lhs()) == annot_domain(eltwise_div->rhs()), + "Wrong input domain"); + validate(lhs_data->dtype() == rhs_data->dtype(), "Wrong input type"); + validate(*lhs_data->shape() == *rhs_data->shape(), "Wrong input shape"); + + std::unique_ptr eltwise_div_data = nullptr; + + switch (lhs_data->dtype()) + { + case loco::DataType::FLOAT32: + { + auto lhs_bufptr = lhs_data->as_f32_bufptr(); + auto rhs_bufptr = rhs_data->as_f32_bufptr(); + auto eltwise_div_bufptr = make_buffer(*lhs_data->shape()); + + auto *shape = lhs_data->shape(); + + for (IndexEnumerator e{*shape}; e.valid(); e.advance()) + { + const auto &index = e.current(); + assert(rhs_bufptr->at(index) != 0.0f); + eltwise_div_bufptr.at(index) = lhs_bufptr->at(index) / rhs_bufptr->at(index); + } + + eltwise_div_data = make_data(eltwise_div_bufptr); + break; + } + default: + throw std::runtime_error("NYI for this DataType"); + } + + assert(eltwise_div_data != nullptr); + erase_annot_data(eltwise_div); + annot_data(eltwise_div, std::move(eltwise_div_data)); + annot_domain(eltwise_div, annot_domain(eltwise_div->lhs())); +} + +} // namespace locomotiv diff --git a/compiler/locomotiv/src/Node/EltwiseDiv.test.cpp b/compiler/locomotiv/src/Node/EltwiseDiv.test.cpp new file mode 100644 index 0000000..60950c1 --- /dev/null +++ b/compiler/locomotiv/src/Node/EltwiseDiv.test.cpp @@ -0,0 +1,121 @@ +/* + * 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: + +x = 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) +y = 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) +out = tf.div(x, y) + +with tf.Session() as sess: + print(sess.run(out)) +*/ +TEST(NodeExecution_EltwiseDiv, f32) +{ + float x_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + float y_val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + float out_val[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + + // make EltwiseDiv(Pull, Pull) + auto g = loco::make_graph(); + Shape input_shape{1, 3, 3, 2}; // NHWC + + auto inp_lhs = g->nodes()->create(); + { + inp_lhs->dtype(loco::DataType::FLOAT32); + inp_lhs->shape({1, 3, 3, 2}); + } + + auto inp_rhs = g->nodes()->create(); + { + inp_rhs->dtype(loco::DataType::FLOAT32); + inp_rhs->shape({1, 3, 3, 2}); + } + + auto eltwise_div = g->nodes()->create(); + { + eltwise_div->lhs(inp_lhs); + eltwise_div->rhs(inp_rhs); + } + + // Make and assign data to two pull nodes + auto inp_lhs_buf = make_buffer(input_shape); + { + int n = 0; + for (IndexEnumerator e{inp_lhs_buf.shape()}; e.valid(); e.advance()) + { + inp_lhs_buf.at(e.current()) = x_val[n++]; + } + } + + auto inp_rhs_buf = make_buffer(input_shape); + { + int n = 0; + for (IndexEnumerator e{inp_rhs_buf.shape()}; e.valid(); e.advance()) + { + inp_rhs_buf.at(e.current()) = y_val[n++]; + } + } + + auto inp_lhs_data = locomotiv::make_data(inp_lhs_buf); + locomotiv::annot_data(inp_lhs, std::move(inp_lhs_data)); + locomotiv::annot_domain(inp_lhs, loco::Domain::Tensor); + + auto inp_rhs_data = locomotiv::make_data(inp_rhs_buf); + locomotiv::annot_data(inp_rhs, std::move(inp_rhs_data)); + locomotiv::annot_domain(inp_rhs, loco::Domain::Tensor); + + // run the network + locomotiv::NodeExecution::get().run(eltwise_div); + + // get result + auto eltwise_div_data = locomotiv::annot_data(eltwise_div); + + // comparing the result + ASSERT_NE(eltwise_div_data, nullptr); + ASSERT_EQ(eltwise_div_data->dtype(), loco::DataType::FLOAT32); + ASSERT_EQ(*(eltwise_div_data->shape()), Shape({1, 3, 3, 2})); + + uint32_t n = 0; + for (IndexEnumerator e{*(eltwise_div_data->shape())}; e.valid(); e.advance()) + { + ASSERT_FLOAT_EQ(eltwise_div_data->as_f32_bufptr()->at(e.current()), out_val[n++]); + } + + ASSERT_EQ(locomotiv::annot_domain(eltwise_div), loco::Domain::Tensor); +} -- 2.7.4