ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Shape.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 ShapeGraphUpdate final : public GraphUpdate
34{
35public:
36 ShapeGraphUpdate(TFShape *node, const TensorName &&input_name)
37 : _node(node), _input_name(input_name)
38 {
39 // DO NOTHING
40 }
41
42 void input(const SymbolTable *) const override;
43
44private:
45 TFShape *_node;
46 const TensorName _input_name;
47};
48
49void ShapeGraphUpdate::input(const SymbolTable *table) const
50{
51 loco::Node *input_node = table->node(_input_name);
52 _node->input(input_node);
53}
54
55} // namespace
56
57namespace moco
58{
59
60bool ShapeGraphBuilder::validate(const tensorflow::NodeDef &node) const
61{
62 if (node.input_size() != 1)
63 return false;
64
65 return plier::tf::has_attrs(node, {"T"});
66}
67
68void ShapeGraphBuilder::build(const tensorflow::NodeDef &node, GraphBuilderContext *context) const
69{
70 assert(context != nullptr);
71
72 loco::Graph *graph = context->graph();
73 SymbolTable *tensor_names = context->tensor_names();
74 UpdateQueue *updates = context->updates();
75
76 // create TF dialect Shape node
77 auto tf_shape = graph->nodes()->create<TFShape>();
78 tf_shape->name(node.name());
79
80 if (plier::tf::has_attrs(node, {"out_type"}))
81 {
82 auto dtype = plier::tf::as_loco_datatype(plier::tf::get_datatype_attr(node, "out_type"));
83 // TODO Support other dtype like S64
84 assert(dtype == loco::DataType::S32);
85
86 tf_shape->dtype(dtype);
87 }
88 else
89 {
90 // Set to S32, TF-documented default value for 'out_type'
91 tf_shape->dtype(loco::DataType::S32);
92 }
93
94 TensorName output_name(node.name(), 0);
95 tensor_names->enroll(output_name, tf_shape);
96
97 auto update = std::make_unique<ShapeGraphUpdate>(tf_shape, TensorName(node.input(0)));
98 updates->enroll(std::move(update));
99}
100
101} // 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.
void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override
Definition Shape.cpp:68
bool validate(const tensorflow::NodeDef &) const override
Definition Shape.cpp:60
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
tensorflow::DataType get_datatype_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
Definition Convert.cpp:43
loco::DataType as_loco_datatype(const tensorflow::DataType dtype)
Definition Convert.cpp:123
NodeName name(void) const
Definition TFNodeDecl.h:50