ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BiasAdd.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
20
21#include <moco/Names.h>
22
23#include <loco.h>
25#include <plier/tf/Convert.h>
26#include <oops/UserExn.h>
27
28#include <memory>
29#include <cassert>
30#include <vector>
31
32namespace
33{
34using namespace moco;
35
36class TFBiasAddGraphUpdate final : public GraphUpdate
37{
38public:
39 TFBiasAddGraphUpdate(TFBiasAdd *biasadd, std::vector<TensorName> &names)
40 : _biasadd(biasadd), _names(names)
41 {
42 }
43
44 void input(const SymbolTable *) const override;
45
46private:
47 TFBiasAdd *_biasadd;
48 std::vector<TensorName> _names;
49};
50
51void TFBiasAddGraphUpdate::input(const SymbolTable *node_table) const
52{
53 assert(_names.size() == 2);
54
55 auto value_node = node_table->node(_names[0]);
56 auto bias_node = node_table->node(_names[1]);
57 assert(value_node != nullptr);
58 assert(bias_node != nullptr);
59
60 _biasadd->value(value_node);
61 _biasadd->bias(bias_node);
62}
63
64} // namespace
65
66namespace moco
67{
68
69bool BiasAddGraphBuilder::validate(const tensorflow::NodeDef &node) const
70{
71 if (node.input_size() != 2)
72 return false;
73
74 // note: even though "data_format" is not entered when a model is written,
75 // TF seems to generate "data_format" field into a pb file
76 if (!plier::tf::has_attrs(node, {"T", "data_format"}))
77 return false;
78
79 // TODO add type check
80 // type of input and bias should be same (except using quantization)
81
82 // Note In case of TF.nn.bias_add,
83 // "value may have any number of dimensions." ...
84 // but "data_format: A string. 'NHWC' and 'NCHW' are supported."
85 // Not sure if value should be 4-D tensor. Let's skip this check for now.
86
87 auto data_layout = plier::tf::get_string_attr(node, "data_format");
88 if (!(data_layout == "NHWC" || data_layout == "NCHW"))
89 {
90 throw oops::UserExn("BiasAdd Unsupported data_format", node.name());
91 }
92
93 return true;
94}
95
96void BiasAddGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
97{
98 assert(context != nullptr);
99
100 loco::Graph *graph = context->graph();
101 SymbolTable *tensor_names = context->tensor_names();
102 UpdateQueue *updates = context->updates();
103
104 // tensorflow data_format: one of NHWC or NCHW.
105 auto data_layout = plier::tf::get_string_attr(node, "data_format");
106 auto tf_bias_add = graph->nodes()->create<TFBiasAdd>();
107 tf_bias_add->name(node.name());
108 tf_bias_add->data_layout(data_layout);
109
110 // To set the input node of encode_node with biasAdd_name
111 TensorName output_name(node.name(), 0);
112 tensor_names->enroll(output_name, tf_bias_add);
113
114 std::vector<TensorName> input_names;
115 input_names.push_back(TensorName(node.input(0)));
116 input_names.push_back(TensorName(node.input(1)));
117
118 auto update = std::make_unique<TFBiasAddGraphUpdate>(tf_bias_add, input_names);
119 updates->enroll(std::move(update));
120}
121
122} // namespace moco
A neural network graph.
Definition Graph.h:161
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const final
Definition BiasAdd.cpp:96
bool validate(const tensorflow::NodeDef &) const final
Definition BiasAdd.cpp:69
Class to store context to build loco graph IR from TensorFlow.
Interface to connect the graph.
virtual void input(const SymbolTable *) const =0
Do the graph input connections using the SymbolTable.
Class to store and query loco::Node* with string name key.
void enroll(const TensorName &tensor_name, loco::Node *node)
Registers a name with corresponding loco::Node *.
loco::Node * node(const TensorName &tensor_name) const
Queries enrolled(registered) with name and return node if found Will throw runtime_error if not found...
Class to store GraphUpdate objects.
void enroll(std::unique_ptr< GraphUpdate > &&update)
Registers GraphUpdate objects.
Exception to user.
Definition UserExn.h:42
Definition Log.h:23
FeatureShapeUpdater update(loco::FeatureShape &feature_shape)
bool has_attrs(const tensorflow::NodeDef &node, const std::vector< std::string > &attr_names)
Definition Convert.cpp:35
const std::string & get_string_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:79
NodeName name(void) const
Definition TFNodeDecl.h:50