From f322e60adc73405a03c71d1bf684783af14ba56f 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: Tue, 13 Aug 2019 08:33:30 +0900 Subject: [PATCH] [locomotiv] supporting FeatureBiasAdd (#6504) This code enables locomotiv to support FeatureBiasAdd. Signed-off-by: Hyun Sik Yoon --- compiler/locomotiv/src/Node.lst | 1 + compiler/locomotiv/src/Node/BiasAdd.cpp | 53 ++++++++++++++--- compiler/locomotiv/src/Node/BiasAdd.test.cpp | 86 +++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 8 deletions(-) diff --git a/compiler/locomotiv/src/Node.lst b/compiler/locomotiv/src/Node.lst index f575cc1..7aaf639 100644 --- a/compiler/locomotiv/src/Node.lst +++ b/compiler/locomotiv/src/Node.lst @@ -5,6 +5,7 @@ // NODE(Name) : alphabetic order please NODE(AvgPool2D) +NODE(BiasAdd) NODE(BiasAdd) NODE(BiasEncode) NODE(ConstGen) diff --git a/compiler/locomotiv/src/Node/BiasAdd.cpp b/compiler/locomotiv/src/Node/BiasAdd.cpp index 0e6de51..8bf7f1b 100644 --- a/compiler/locomotiv/src/Node/BiasAdd.cpp +++ b/compiler/locomotiv/src/Node/BiasAdd.cpp @@ -32,6 +32,15 @@ using nncc::core::ADT::tensor::make_buffer; #include #include +namespace +{ +using locomotiv::NodeData; + +std::unique_ptr calc(const NodeData *input_data, const NodeData *bias_data, + uint32_t axis); + +} // namespace + namespace locomotiv { @@ -45,8 +54,42 @@ void NodeExecution::execute(loco::BiasAdd *bias_add) locomotiv::annot_domain(bias_add->bias()) == loco::Domain::Bias, "Wrong input domain"); - uint32_t axis = bias_add->axis(); + std::unique_ptr 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 *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 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 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 bias_add_data = nullptr; @@ -74,11 +117,7 @@ void NodeExecution::execute(loco::BiasAdd *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 diff --git a/compiler/locomotiv/src/Node/BiasAdd.test.cpp b/compiler/locomotiv/src/Node/BiasAdd.test.cpp index dcaa7ea..0ca8266 100644 --- a/compiler/locomotiv/src/Node/BiasAdd.test.cpp +++ b/compiler/locomotiv/src/Node/BiasAdd.test.cpp @@ -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(); + { + // setting values is ignored for testing + } + + auto bias = g->nodes()->create(); + { + // nothing to do + } + + auto feature_bias_add = g->nodes()->create>(); + { + feature_bias_add->value(feature_encode); + feature_bias_add->bias(bias); + } + + // 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(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); +} -- 2.7.4