From 15a12e4a5e8447a1ad7b2c12571f9d6d09690aef 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, 15 Oct 2019 07:19:18 +0900 Subject: [PATCH] [moco-tf] Graph build BiasAdd as TF dialect (#8130) This will remove Graph builder for BiasAdd as Canonical dialect and build only for TF dialect Signed-off-by: SaeHie Park --- compiler/moco-tf/src/Op/BiasAdd.cpp | 114 +------------------------------ compiler/moco-tf/src/Op/BiasAdd.h | 19 +----- compiler/moco-tf/src/Op/BiasAdd.test.cpp | 75 +------------------- 3 files changed, 5 insertions(+), 203 deletions(-) diff --git a/compiler/moco-tf/src/Op/BiasAdd.cpp b/compiler/moco-tf/src/Op/BiasAdd.cpp index 6862a52..377f98f 100644 --- a/compiler/moco-tf/src/Op/BiasAdd.cpp +++ b/compiler/moco-tf/src/Op/BiasAdd.cpp @@ -39,48 +39,6 @@ namespace { using namespace moco::tf; -class ValueInputUpdate final : public GraphUpdate -{ -public: - ValueInputUpdate(loco::BiasAdd *bias_add, const TensorName &&input_name) - : _bias_add(bias_add), _input_name(input_name) - { - } - - void input(const SymbolTable *) const override; - -private: - loco::BiasAdd *_bias_add; - const TensorName _input_name; -}; - -void ValueInputUpdate::input(const SymbolTable *node_table) const -{ - loco::Node *input_node = node_table->node(_input_name); - _bias_add->value(input_node); -} - -class BiasInputUpdate final : public GraphUpdate -{ -public: - BiasInputUpdate(loco::BiasEncode *bias_enc, const TensorName &&input_name) - : _bias_enc(bias_enc), _input_name(input_name) - { - } - - void input(const SymbolTable *) const override; - -private: - loco::BiasEncode *_bias_enc; - const TensorName _input_name; -}; - -void BiasInputUpdate::input(const SymbolTable *node_table) const -{ - loco::Node *input_node = node_table->node(_input_name); - _bias_enc->input(input_node); -} - class TFBiasAddGraphUpdate final : public GraphUpdate { public: @@ -116,16 +74,7 @@ namespace moco namespace tf { -/** - * @brief GraphBuilder for BiasAdd node - */ -class BiasAddGraphBuilder final : public BiasAddGraphBuilderBase -{ -public: - void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override; -}; - -bool BiasAddGraphBuilderBase::validate(const tensorflow::NodeDef &node) const +bool BiasAddGraphBuilder::validate(const tensorflow::NodeDef &node) const { assert(node.input_size() == 2); @@ -149,67 +98,6 @@ void BiasAddGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderCon { assert(context != nullptr); - if (moco::tf::get()) - { - BiasAddGraphBuilderImpl builder; - return builder.build(node, context); - } - else - { - BiasAddGraphBuilderImpl builder; - return builder.build(node, context); - } -} - -void BiasAddGraphBuilderImpl::build(const tensorflow::NodeDef &node, - GraphBuilderContext *context) const -{ - loco::Graph *graph = context->graph(); - SymbolTable *tensor_names = context->tensor_names(); - UpdateQueue *updates = context->updates(); - - // tensorflow data_format: one of NHWC or NCHW. - auto data_layout = plier::tf::get_data_layout(node, "data_format"); - - // creating loco nodes - auto bias_enc = graph->nodes()->create(); - - auto bias_add = graph->nodes()->create>(); - { - using plier::tf::DataLayout; - - if (data_layout == DataLayout::NHWC) - { - bias_add->axis(3); - } - else if (data_layout == DataLayout::NCHW) - { - bias_add->axis(1); // Channel - // Note: the following descrition of TF 1.13 at - // https://www.tensorflow.org/api_docs/python/tf/nn/bias_add seems wrong: - // "bias: A 1-D Tensor with size matching the last dimension of value." - // because providing the size of W (last dimension) to bias throws an error with TensorFlow - } - } - - // link nodes - bias_add->bias(bias_enc); - - // To set the input node of encode_node with biasAdd_name - TensorName output_name(node.name(), 0); - tensor_names->enroll(output_name, bias_add); - - // Record ifm inputs to featureEncode_node - auto value_update = stdex::make_unique(bias_add, TensorName(node.input(0))); - auto bias_update = stdex::make_unique(bias_enc, TensorName(node.input(1))); - - updates->enroll(std::move(value_update)); - updates->enroll(std::move(bias_update)); -} - -void BiasAddGraphBuilderImpl::build(const tensorflow::NodeDef &node, - GraphBuilderContext *context) const -{ loco::Graph *graph = context->graph(); SymbolTable *tensor_names = context->tensor_names(); UpdateQueue *updates = context->updates(); diff --git a/compiler/moco-tf/src/Op/BiasAdd.h b/compiler/moco-tf/src/Op/BiasAdd.h index 890ca65..1e395c5 100644 --- a/compiler/moco-tf/src/Op/BiasAdd.h +++ b/compiler/moco-tf/src/Op/BiasAdd.h @@ -18,31 +18,16 @@ #define __OP_BIAS_ADD_H__ #include "GraphBuilder.h" -#include "ImportTarget.h" namespace moco { namespace tf { -struct BiasAddGraphBuilderBase : public GraphBuilder +class BiasAddGraphBuilder : public GraphBuilder { - virtual ~BiasAddGraphBuilderBase() = default; - +public: bool validate(const tensorflow::NodeDef &) const final; -}; - -template class BiasAddGraphBuilderImpl; - -template <> -struct BiasAddGraphBuilderImpl final : public BiasAddGraphBuilderBase -{ - void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; -}; - -template <> -struct BiasAddGraphBuilderImpl final : public BiasAddGraphBuilderBase -{ void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final; }; diff --git a/compiler/moco-tf/src/Op/BiasAdd.test.cpp b/compiler/moco-tf/src/Op/BiasAdd.test.cpp index 823b66f..535f002 100644 --- a/compiler/moco-tf/src/Op/BiasAdd.test.cpp +++ b/compiler/moco-tf/src/Op/BiasAdd.test.cpp @@ -105,13 +105,8 @@ TEST(TensorFlowImport, bias_add_01) tensorflow::GraphDef graph_def; EXPECT_TRUE(plier::tf::parse_graphdef(bias_add_01_pbtxtdata, graph_def)); - // Test "BiasAddGraphBuilderImpl" { - using BiasAddGraphBuilder = BiasAddGraphBuilderImpl; - - moco::tf::GraphBuilderRegistry r{&moco::tf::GraphBuilderRegistry::get()}; - r.add("BiasAdd", stdex::make_unique()); - moco::tf::Importer importer{&r}; + moco::tf::Importer importer; std::unique_ptr graph = importer.import(signature, graph_def); @@ -131,36 +126,6 @@ TEST(TensorFlowImport, bias_add_01) ASSERT_TRUE(bias_add->data_layout() == "NHWC"); } - // Test "BiasAddGraphBuilderImpl" - { - using BiasAddGraphBuilder = BiasAddGraphBuilderImpl; - - moco::tf::GraphBuilderRegistry r{&moco::tf::GraphBuilderRegistry::get()}; - r.add("BiasAdd", stdex::make_unique()); - moco::tf::Importer importer{&r}; - - std::unique_ptr graph = importer.import(signature, graph_def); - - // what to test: - // - there should exist BiasAdd - // - value() should not be nullptr - // - bias() input should be BiasEncode - // - axis should match - - // loco node : ------------+-- BiasAdd -- - // BiasEncode -/ - - loco::BiasAdd *bias_add = - moco::tf::test::find_first_node_bytype>(graph.get()); - - ASSERT_NE(bias_add, nullptr); - ASSERT_NE(bias_add->value(), nullptr); - - auto bias_enc = dynamic_cast(bias_add->bias()); - ASSERT_NE(bias_enc, nullptr); - - ASSERT_EQ(bias_add->axis(), 3); // NHWC - } } namespace @@ -240,15 +205,9 @@ TEST(TensorFlowImport, bias_add_NCHW_axis) tensorflow::GraphDef graph_def; EXPECT_TRUE(plier::tf::parse_graphdef(bias_add_NCHW_pbtxtdata, graph_def)); - std::unique_ptr graph = importer.import(signature, graph_def); - // Test "BiasAddGraphBuilderImpl" { - using BiasAddGraphBuilder = BiasAddGraphBuilderImpl; - - moco::tf::GraphBuilderRegistry r{&moco::tf::GraphBuilderRegistry::get()}; - r.add("BiasAdd", stdex::make_unique()); - moco::tf::Importer importer{&r}; + moco::tf::Importer importer; std::unique_ptr graph = importer.import(signature, graph_def); @@ -268,34 +227,4 @@ TEST(TensorFlowImport, bias_add_NCHW_axis) ASSERT_TRUE(bias_add->data_layout() == "NCHW"); } - // Test "BiasAddGraphBuilderImpl" - { - using BiasAddGraphBuilder = BiasAddGraphBuilderImpl; - - moco::tf::GraphBuilderRegistry r{&moco::tf::GraphBuilderRegistry::get()}; - r.add("BiasAdd", stdex::make_unique()); - moco::tf::Importer importer{&r}; - - std::unique_ptr graph = importer.import(signature, graph_def); - - // what to test: - // - there should exist BiasAdd - // - value() should not be nullptr - // - bias() input should be BiasEncode - // - axis should match - - // loco node : ------------+-- BiasAdd -- - // BiasEncode -/ - - loco::BiasAdd *bias_add = - moco::tf::test::find_first_node_bytype>(graph.get()); - - ASSERT_NE(bias_add, nullptr); - ASSERT_NE(bias_add->value(), nullptr); - - auto bias_enc = dynamic_cast(bias_add->bias()); - ASSERT_NE(bias_enc, nullptr); - - ASSERT_EQ(bias_add->axis(), 1); // NCHW - } } -- 2.7.4