ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Mean.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 <loco.h>
22#include <plier/tf/Convert.h>
23
24#include <memory>
25
26namespace
27{
28using namespace moco;
29
33class MeanGraphUpdate final : public GraphUpdate
34{
35public:
36 MeanGraphUpdate(TFMean *node, const TensorName &&input_name,
37 const TensorName &&reduction_indices_name)
38 : _node(node), _input_name(input_name), _reduction_indices_name(reduction_indices_name)
39 {
40 // DO NOTHING
41 }
42
43 void input(const SymbolTable *) const override;
44
45private:
46 TFMean *_node;
47 const TensorName _input_name;
48 const TensorName _reduction_indices_name;
49};
50
51void MeanGraphUpdate::input(const SymbolTable *table) const
52{
53 loco::Node *input_node = table->node(_input_name);
54 loco::Node *reduction_indices_node = table->node(_reduction_indices_name);
55 _node->input(input_node);
56 _node->reduction_indices(reduction_indices_node);
57}
58
59} // namespace
60
61namespace moco
62{
63
64bool MeanGraphBuilder::validate(const tensorflow::NodeDef &node) const
65{
66 if (node.input_size() != 2)
67 return false;
68
69 if (!plier::tf::has_attrs(node, {"T", "Tidx", "keep_dims"}))
70 return false;
71
72 auto dtype = plier::tf::get_datatype_attr(node, "Tidx");
73 if (dtype != tensorflow::DataType::DT_INT32 && dtype != tensorflow::DataType::DT_INT64)
74 return false;
75
76 return true;
77}
78
79void MeanGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
80{
81 assert(context != nullptr);
82
83 loco::Graph *graph = context->graph();
84 SymbolTable *tensor_names = context->tensor_names();
85 UpdateQueue *updates = context->updates();
86
87 // creating TF dialect Mean node
88 auto tf_mean = graph->nodes()->create<TFMean>();
89 tf_mean->name(node.name());
90 tf_mean->keep_dims(plier::tf::get_bool_attr(node, "keep_dims"));
91
92 TensorName output_name(node.name(), 0);
93 tensor_names->enroll(output_name, tf_mean);
94
95 auto update = std::make_unique<MeanGraphUpdate>(tf_mean, TensorName(node.input(0)),
96 TensorName(node.input(1)));
97 updates->enroll(std::move(update));
98}
99
100} // namespace moco
A neural network graph.
Definition Graph.h:161
Logical unit of computation.
Definition Node.h:54
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.
bool validate(const tensorflow::NodeDef &) const override
Definition Mean.cpp:64
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
Definition Mean.cpp:79
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.
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
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
bool get_bool_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:103
tensorflow::DataType get_datatype_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:43
NodeName name(void) const
Definition TFNodeDecl.h:50